-
Notifications
You must be signed in to change notification settings - Fork 2
/
AoHighlighter.cpp
267 lines (242 loc) · 6.82 KB
/
AoHighlighter.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
/*
* Copyright 2023 Rochus Keller <mailto:me@rochus-keller.ch>
*
* This file is part of the ActiveOberon parser/navigator project.
*
* The following is the license that applies to this copy of the
* file. For a license to use the file 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.
*/
// Adopted from https://github.com/rochus-keller/LisaPascal/
#include "AoHighlighter.h"
#include "AoLexer.h"
#include <QBuffer>
using namespace Ao;
OberonPainter::OberonPainter(QObject* parent) :
QSyntaxHighlighter(parent)
{
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_Asm].setForeground(QColor(0, 128, 0));
}
void OberonPainter::addBuiltIn(const QByteArray& bi)
{
d_builtins << bi;
}
void OberonPainter::addKeyword(const QByteArray& kw)
{
d_keywords << kw;
}
void OberonPainter::addBuiltIns()
{
const char** str = s_builtInTypes;
while(*str)
{
d_builtins << *str;
str++;
}
str = s_builtInConsts;
while(*str)
{
d_builtins << *str;
str++;
}
str = s_builtInProcs;
while(*str)
{
d_builtins << *str;
str++;
}
}
QTextCharFormat OberonPainter::formatForCategory(int c) const
{
return d_format[c];
}
void OberonPainter::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;
// Protocol:
// lexerState == 1: multi-line (* *) comment
// lexerState == 2: multi-line CODE END region
int start = 0;
if( lexerState == 1 )
{
QTextCharFormat f = formatForCategory( C_Cmt );
int pos = 0;
Lexer::parseComment( text.toLatin1(), pos, lexerState );
if( lexerState > 0 )
{
// the whole block ist part of 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
pos += 2;
setFormat( start, pos , f );
lexerState = 0;
braceDepth--;
start = pos;
}
}else if( lexerState == 2 )
{
QTextCharFormat f = formatForCategory(C_Asm);
int pos = text.indexOf("END");
if( pos == -1 )
{
// the whole block ist part of comment
setFormat( start, text.size(), f );
setCurrentBlockState( (braceDepth << 8) | lexerState);
return;
}else
{
// End of section found
pos += 0; // END is not part of it
setFormat( start, pos , f );
lexerState = 0;
braceDepth--;
start = pos;
}
}
Lexer lex;
lex.setIgnoreComments(false);
lex.setPackComments(false);
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_Latt )
{
braceDepth++;
f = formatForCategory(C_Cmt);
lexerState = 1;
}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_integer || t.d_type == Tok_real )
f = formatForCategory(C_Num);
else if( tokenTypeIsLiteral(t.d_type) )
{
f = formatForCategory(C_Op);
}else if( tokenTypeIsKeyword(t.d_type) )
{
if( t.d_type == Tok_CODE )
{
const int pos = text.indexOf('END', t.d_colNr);
if( pos == -1 )
{
braceDepth++;
lexerState = 2;
}
setFormat( t.d_colNr + 4, t.d_val.size()-4, formatForCategory(C_Asm) );
t.d_val = QByteArray();
t.d_len = 4;
}
f = formatForCategory(C_Kw);
}else if( t.d_type == Tok_ident )
{
if( d_builtins.contains(t.d_val) )
f = formatForCategory(C_Type);
else if( d_keywords.contains(t.d_val) )
f = formatForCategory(C_Kw);
else
f = formatForCategory(C_Ident);
}
if( f.isValid() )
{
const int len = t.d_val.isEmpty() ? t.d_len : t.d_val.size();
setFormat( t.d_colNr-1, 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 );
}
const char* OberonPainter::s_builtInTypes[] = {
"BOOLEAN",
"CHAR",
"INTEGER",
"LONGREAL",
"REAL",
"SHORTINT",
"SET",
"LONGINT",
"HUGEINT",
"PTR",
0
};
const char* OberonPainter::s_builtInConsts[] = {
"FALSE",
"TRUE",
"NIL",
0
};
const char* OberonPainter::s_builtInProcs[] = {
"ABS",
"LEN",
"ASH",
"LONG",
"CAP",
"MAX",
"CHR",
"MIN",
"COPY",
"NEW",
"DEC",
"ODD",
"ENTIER",
"ORD",
"EXCL",
"SHORT",
"INC",
"INCL",
"SIZE",
"ASSERT",
0
};