-
Notifications
You must be signed in to change notification settings - Fork 30
/
ObnHighlighter.cpp
264 lines (241 loc) · 7.77 KB
/
ObnHighlighter.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
/*
* Copyright 2019 Rochus Keller <mailto:me@rochus-keller.ch>
*
* This file is part of the Oberon Viewer application.
*
* 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 me@rochus-keller.ch.
*
* 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.
*/
#include "ObnHighlighter.h"
#include "ObLexer.h"
#include "ObCodeModel.h"
#include <QBuffer>
using namespace Ob;
Highlighter::Highlighter(QTextDocument* parent) :
QSyntaxHighlighter(parent),d_enableExt(false)
{
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_Kw].setForeground(QColor(68, 85, 136));
d_format[C_Kw].setFontWeight(QFont::Bold);
d_format[C_Op].setForeground(QColor(153, 0, 0));
d_format[C_Op].setFontWeight(QFont::Bold);
d_format[C_Type].setForeground(QColor(153, 0, 115));
d_format[C_Type].setFontWeight(QFont::Bold);
//d_format[C_Pp].setForeground(QColor(0, 134, 179));
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();
}
void Highlighter::setEnableExt(bool b)
{
const bool old = d_enableExt;
d_enableExt = b;
if( old != b )
{
//d_builtins = createBuiltins(d_enableExt);
rehighlight();
}
}
void Highlighter::addBuiltIn(const QByteArray& bi)
{
d_builtins << bi;
if( d_enableExt )
d_builtins << bi.toLower();
}
QTextCharFormat Highlighter::formatForCategory(int c) const
{
return d_format[c];
}
#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
void Highlighter::highlightBlock(const QString& text)
{
const int previousBlockState_ = previousBlockState();
int lexerState = 0, initialBraceDepth = 0;
if (previousBlockState_ != -1) {
lexerState = previousBlockState_ & 0xff;
initialBraceDepth = previousBlockState_ >> 8;
}
int braceDepth = initialBraceDepth;
int start = 0;
if( lexerState == 1 )
{
// wir sind in einem Multi Line Comment
// suche das Ende
QTextCharFormat f = formatForCategory(C_Cmt);
// f.setProperty( TokenProp, int(Tok_Comment) );
int pos = 0;
Lexer::parseComment( text.toLatin1(), pos, lexerState );
if( lexerState > 0 )
{
// the whole block ist part of the comment
lexerState = 1; // lexerState can have higher value if nested
setFormat( start, text.size(), f );
setCurrentBlockState( (braceDepth << 8) | lexerState);
return;
}else
{
// End of Comment found
setFormat( start, pos , f );
lexerState = 0;
braceDepth--;
start = pos;
}
}else if( lexerState == 2 )
{
// wir sind in einem multi line hex string
QTextCharFormat f = formatForCategory(C_Str);
// f.setProperty( TokenProp, int(Tok_hexstring) );
const int pos = text.indexOf('$');
if( pos == -1 )
{
// the whole block ist part of the hex string
setFormat( start, text.size(), f );
setCurrentBlockState( (braceDepth << 8) | lexerState);
return;
}else
{
// End of hex string found
setFormat( start, pos , f );
lexerState = 0;
braceDepth--;
start = pos;
}
}else if( lexerState == 3 )
{
// wir sind in einem multi preprocessor command
QTextCharFormat f = formatForCategory(C_Pp);
int pos = text.indexOf("*>");
if( pos == -1 )
{
// the whole block ist part of the hex string
setFormat( start, text.size(), f );
setCurrentBlockState( (braceDepth << 8) | lexerState);
return;
}else
{
// End of preprocessor command found
pos += 2;
setFormat( start, pos , f );
lexerState = 0;
braceDepth--;
start = pos;
}
}
Ob::Lexer lex;
lex.setIgnoreComments(false);
lex.setPackComments(false);
lex.setEnableExt(d_enableExt);
int startPp;
QList<Token> tokens = lex.tokens(text.mid(start));
for( int i = 0; i < tokens.size(); ++i )
{
Token &t = tokens[i];
t.d_colNr += start;
QTextCharFormat f;
if( t.d_type == Tok_Comment )
f = formatForCategory(C_Cmt); // one line comment
else if( t.d_type == Tok_Latt )
{
braceDepth++;
f = formatForCategory(C_Cmt);
lexerState = 1;
}else if( t.d_type == Tok_LtStar )
{
braceDepth++;
f = formatForCategory(C_Pp);
startPp = t.d_colNr-1;
lexerState = 3;
}else if( t.d_type == Tok_StarGt )
{
braceDepth--;
f = formatForCategory(C_Pp);
lexerState = 0;
}else if(t.d_type == Tok_hexstring )
{
f = formatForCategory(C_Str);
}else if( t.d_type == Tok_Dlr )
{
if( !t.d_val.isEmpty() )
{
braceDepth++;
lexerState = 2;
// multi line hex string
}else
{
braceDepth--;
lexerState = 0;
}
f = formatForCategory(C_Str);
}else if( t.d_type == Tok_Ratt )
{
braceDepth--;
f = formatForCategory(C_Cmt);
lexerState = 0;
}else if( t.d_type == Tok_string || t.d_type == Tok_hexchar )
f = formatForCategory(C_Str);
else if( t.d_type == Tok_real || t.d_type == Tok_integer )
f = formatForCategory(C_Num);
else if( tokenTypeIsLiteral(t.d_type) )
{
f = formatForCategory(C_Op);
}else if( tokenTypeIsKeyword(t.d_type) )
{
f = formatForCategory(C_Kw);
}else if( t.d_type == Tok_ident )
{
if( d_builtins.contains(t.d_val) )
f = formatForCategory(C_Type);
else
f = formatForCategory(C_Ident);
}
if( lexerState == 3 )
setFormat( startPp, t.d_colNr - startPp + t.d_len, formatForCategory(C_Pp) );
else if( f.isValid() )
setFormat( t.d_colNr-1, t.d_len, f );
}
setCurrentBlockState((braceDepth << 8) | lexerState );
}
LogPainter::LogPainter(QTextDocument* parent):QSyntaxHighlighter(parent)
{
}
void LogPainter::highlightBlock(const QString& text)
{
QColor c = Qt::black;
if( text.startsWith("WRN:") )
c = Qt::blue;
else if( text.startsWith("ERR:") )
c = Qt::red;
setFormat( 0, text.size(), c );
}