-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLispHighlighter.cpp
277 lines (252 loc) · 8.74 KB
/
LispHighlighter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright 2024 Rochus Keller <mailto:[email protected]>
*
* This file is part of the Interlisp project.
*
* The following is the license that applies to this copy of the
* library. For a license to use the library under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
// Adopted from the Luon project
#include "LispHighlighter.h"
#include "LispLexer.h"
#include <QBuffer>
#include <QTextDocument>
#include <QtDebug>
using namespace Lisp;
static const char* QUOTE;
Highlighter::Highlighter(QTextDocument* parent) :
QSyntaxHighlighter(parent)
{
QUOTE = Token::getSymbol("QUOTE").constData();
const int pointSize = parent->defaultFont().pointSize();
for( int i = 0; i < C_Max; i++ )
{
d_format[i].setFontWeight(QFont::Normal);
d_format[i].setForeground(Qt::black);
d_format[i].setBackground(Qt::transparent);
}
d_format[C_Num].setForeground(QColor(0, 153, 153));
d_format[C_Str].setForeground(QColor(208, 16, 64));
d_format[C_Cmt].setForeground(QColor(153, 153, 136));
d_format[C_Func].setForeground(QColor(68, 85, 136));
d_format[C_Func].setFontWeight(QFont::Bold);
d_format[C_Op1].setForeground(QColor(153, 0, 0));
d_format[C_Op1].setFontWeight(QFont::Bold);
d_format[C_Op2].setForeground(QColor(153, 0, 0));
d_format[C_Op2].setFontWeight(QFont::Bold);
d_format[C_Op2].setFontPointSize(pointSize * 1.2);
//d_format[C_Op3].setForeground(QColor(153, 0, 0).lighter(125)); // QColor(0, 134, 179));
d_format[C_Op3].setFontWeight(QFont::Bold);
d_format[C_Var].setForeground(QColor(153, 0, 115));
d_format[C_Var].setFontWeight(QFont::Bold);
d_format[C_Pp].setFontWeight(QFont::Bold);
d_format[C_Pp].setForeground(QColor(0, 128, 0));
d_format[C_Pp].setBackground(QColor(230, 255, 230));
//d_builtins = createBuiltins();
d_syntax.insert(Token::getSymbol("NIL").constData());
d_syntax.insert(Token::getSymbol("T").constData());
d_syntax.insert(Token::getSymbol("LAMBDA").constData());
d_syntax.insert(Token::getSymbol("NLAMBDA").constData());
}
void Highlighter::addFunction(const char* bi)
{
d_functions << bi;
}
void Highlighter::addVariable(const char* bi)
{
d_variables << bi;
}
QTextCharFormat Highlighter::formatForCategory(int c) const
{
return d_format[c];
}
void Highlighter::clearFromHere(quint32 line)
{
LineState::iterator i = lineState.upperBound(line);
while( i != lineState.end() )
i = lineState.erase(i);
}
#if 0
QSet<QByteArray> Highlighter::createBuiltins(bool withLowercase)
{
QSet<QByteArray> res = CodeModel::getBuitinIdents().toSet();
if( withLowercase )
{
QSet<QByteArray> tmp = res;
QSet<QByteArray>::const_iterator i;
for( i = tmp.begin(); i != tmp.end(); ++i )
res.insert( (*i).toLower() );
}
return res;
}
#endif
static inline void setBackground(QTextCharFormat& f, int level)
{
if( level == 0 )
{
f.setBackground(Qt::white);
return;
}
QColor bgClr = QColor::fromRgb( 255, 254, 225 ); // Gelblich
bgClr = QColor::fromHsv( bgClr.hue(), bgClr.saturation(), bgClr.value() - ( level*2 - 1 ) * 3 );
f.setBackground(bgClr);
}
static bool punctuation(const QString& str, int pos, int len)
{
for( int i = pos; i < qMin(pos + len, str.size()); i++ )
{
if( str[i].isLetterOrNumber() )
return false;
}
return true;
}
void Highlighter::highlightBlock(const QString& text)
{
const int previousBlockState_ = previousBlockState();
quint8 lexerState = 0,
braceDepth = 0, // nesting level of [] or ()
commentLevel = 0; // 0 or braceDepth where comment started
if (previousBlockState_ != -1) {
lexerState = previousBlockState_ & 0xff;
braceDepth = (previousBlockState_ >> 8) & 0xff;
commentLevel = (previousBlockState_ >> 16) & 0xff;
}
const quint32 line = currentBlock().blockNumber() + 1;
clearFromHere(line);
// we can be in a (, [, (*, " or QUOTE
// [ requires memorizing the previous open occurences
// ( and [ are the same category, just with different termination rules
// QUOTE can start with ( or [
// A comment can include all others and behave compatible, even if the whole thing is not evaluated
// But a comment can terminate with ] which affects previous [ and (
// The [ ( (* QUOTE state is separate from the " state
bool inString = lexerState & 1;
bool inQuote = lexerState & 2;
Lexer lex;
lex.setEmitComments(true);
lex.setPacked(false);
lex.setStream(text.toLatin1(), "");
Token t;
if( inString )
{
t = lex.readString();
setFormat( 0, t.len, formatForCategory(C_Str) );
if( t.val.endsWith('"') && ((t.pos.col + t.len) < 2 || text[t.pos.col + t.len - 2] != '%') ) // ": -1, %: -2
inString = false;
else
{
// the whole line is in the string
// lexer state remains the same
setCurrentBlockState(previousBlockState_);
return;
}
}
t = lex.nextToken();
while( t.isValid() )
{
QTextCharFormat f;
switch(t.type)
{
case Tok_lpar:
braceDepth++;
if( commentLevel )
f = formatForCategory(C_Cmt);
else
f = formatForCategory(C_Op2);
break;
case Tok_rpar:
if( commentLevel )
{
if( braceDepth == commentLevel )
commentLevel = 0;
f = formatForCategory(C_Cmt);
}else
f = formatForCategory(C_Op2);
braceDepth--;
break;
case Tok_lbrack:
lineState[line] = braceDepth;
braceDepth++;
if( commentLevel )
f = formatForCategory(C_Cmt);
else
f = formatForCategory(C_Op2);
break;
case Tok_rbrack:
if( commentLevel )
f = formatForCategory(C_Cmt);
else
f = formatForCategory(C_Op2);
if( !lineState.isEmpty() )
{
const quint32 lastLine = lineState.lastKey();
braceDepth = lineState.value(lastLine);
lineState.remove(lastLine);
if( braceDepth <= commentLevel )
commentLevel = 0;
}
break;
case Tok_atom:
if( commentLevel )
f = formatForCategory(C_Cmt);
else if( d_syntax.contains(t.val.constData()) )
f = formatForCategory(C_Op1);
else if( d_functions.contains(t.val.constData()) )
f = formatForCategory(C_Func);
else if( d_variables.contains(t.val.constData() ) )
f = formatForCategory(C_Var);
//else if( punctuation(text, t.pos.col-1, t.len ) )
// f = formatForCategory(C_Op3);
else
f = formatForCategory(C_Ident);
if( !inString && commentLevel == 0 && t.val.constData() == QUOTE )
inQuote = true;
break;
case Tok_float:
case Tok_integer:
if( commentLevel )
f = formatForCategory(C_Cmt);
else
f = formatForCategory(C_Num);
break;
case Tok_Lattr:
braceDepth++;
if( commentLevel == 0 )
commentLevel = braceDepth;
f = formatForCategory(C_Cmt);
break;
case Tok_DblQuote:
Q_ASSERT( !inString );
inString = true;
const Token t2 = lex.readString();
t.len += t2.len;
if( t2.val.endsWith('"') )
inString = false; // string ended on the same line
// else look for string end on next line
if( commentLevel == 0 )
f = formatForCategory(C_Str);
else
f = formatForCategory(C_Cmt);
break;
}
if( f.isValid() )
setFormat( t.pos.col-1, t.len, f );
t = lex.nextToken();
}
lexerState = 0;
if( inString )
lexerState |= 1;
if( inQuote )
lexerState |= 2;
setCurrentBlockState((commentLevel << 16) | (braceDepth << 8) | lexerState );
}