1 | // Scintilla source code edit control |
2 | // @file LexPB.cxx |
3 | // Lexer for PowerBasic by Roland Walter, roland@rowalt.de (for PowerBasic see www.powerbasic.com) |
4 | // |
5 | // Changes: |
6 | // 17.10.2003: Toggling of subs/functions now until next sub/function - this gives better results |
7 | // 29.10.2003: 1. Bug: Toggling didn't work for subs/functions added in editor |
8 | // 2. Own colors for PB constants and Inline Assembler SCE_B_CONSTANT and SCE_B_ASM |
9 | // 3. Several smaller syntax coloring improvements and speed optimizations |
10 | // 12.07.2004: 1. Toggling for macros added |
11 | // 2. Further folding speed optimitations (for people dealing with very large listings) |
12 | // |
13 | // Necessary changes for the PB lexer in Scintilla project: |
14 | // - In SciLexer.h and Scintilla.iface: |
15 | // |
16 | // #define SCLEX_POWERBASIC 51 //ID for PowerBasic lexer |
17 | // (...) |
18 | // #define SCE_B_DEFAULT 0 //in both VB and PB lexer |
19 | // #define SCE_B_COMMENT 1 //in both VB and PB lexer |
20 | // #define SCE_B_NUMBER 2 //in both VB and PB lexer |
21 | // #define SCE_B_KEYWORD 3 //in both VB and PB lexer |
22 | // #define SCE_B_STRING 4 //in both VB and PB lexer |
23 | // #define SCE_B_PREPROCESSOR 5 //VB lexer only, not in PB lexer |
24 | // #define SCE_B_OPERATOR 6 //in both VB and PB lexer |
25 | // #define SCE_B_IDENTIFIER 7 //in both VB and PB lexer |
26 | // #define SCE_B_DATE 8 //VB lexer only, not in PB lexer |
27 | // #define SCE_B_CONSTANT 13 //PB lexer only, not in VB lexer |
28 | // #define SCE_B_ASM 14 //PB lexer only, not in VB lexer |
29 | |
30 | // - Statement added to KeyWords.cxx: 'LINK_LEXER(lmPB);' |
31 | // - Statement added to scintilla_vc6.mak: '$(DIR_O)\LexPB.obj: ...\src\LexPB.cxx $(LEX_HEADERS)' |
32 | // |
33 | // Copyright for Scintilla: 1998-2001 by Neil Hodgson <neilh@scintilla.org> |
34 | // The License.txt file describes the conditions under which this software may be distributed. |
35 | |
36 | #include <stdlib.h> |
37 | #include <string.h> |
38 | #include <stdio.h> |
39 | #include <stdarg.h> |
40 | #include <assert.h> |
41 | #include <ctype.h> |
42 | |
43 | #include <string> |
44 | #include <string_view> |
45 | |
46 | #include "ILexer.h" |
47 | #include "Scintilla.h" |
48 | #include "SciLexer.h" |
49 | |
50 | #include "WordList.h" |
51 | #include "LexAccessor.h" |
52 | #include "Accessor.h" |
53 | #include "StyleContext.h" |
54 | #include "CharacterSet.h" |
55 | #include "LexerModule.h" |
56 | |
57 | using namespace Lexilla; |
58 | |
59 | static inline bool IsTypeCharacter(const int ch) |
60 | { |
61 | return ch == '%' || ch == '&' || ch == '@' || ch == '!' || ch == '#' || ch == '$' || ch == '?'; |
62 | } |
63 | |
64 | static inline bool IsAWordChar(const int ch) |
65 | { |
66 | return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_'); |
67 | } |
68 | |
69 | static inline bool IsAWordStart(const int ch) |
70 | { |
71 | return (ch < 0x80) && (isalnum(ch) || ch == '_'); |
72 | } |
73 | |
74 | static bool MatchUpperCase(Accessor &styler, Sci_Position pos, const char *s) //Same as styler.Match() but uppercase comparison (a-z,A-Z and space only) |
75 | { |
76 | char ch; |
77 | for (Sci_Position i=0; *s; i++) |
78 | { |
79 | ch=styler.SafeGetCharAt(pos+i); |
80 | if (ch > 0x60) ch -= '\x20'; |
81 | if (*s != ch) return false; |
82 | s++; |
83 | } |
84 | return true; |
85 | } |
86 | |
87 | static void ColourisePBDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,WordList *keywordlists[],Accessor &styler) { |
88 | |
89 | WordList &keywords = *keywordlists[0]; |
90 | |
91 | styler.StartAt(startPos); |
92 | |
93 | StyleContext sc(startPos, length, initStyle, styler); |
94 | |
95 | for (; sc.More(); sc.Forward()) { |
96 | switch (sc.state) |
97 | { |
98 | case SCE_B_OPERATOR: |
99 | { |
100 | sc.SetState(SCE_B_DEFAULT); |
101 | break; |
102 | } |
103 | case SCE_B_KEYWORD: |
104 | { |
105 | if (!IsAWordChar(sc.ch)) |
106 | { |
107 | if (!IsTypeCharacter(sc.ch)) |
108 | { |
109 | char s[100]; |
110 | sc.GetCurrentLowered(s, sizeof(s)); |
111 | if (keywords.InList(s)) |
112 | { |
113 | if (strcmp(s, "rem" ) == 0) |
114 | { |
115 | sc.ChangeState(SCE_B_COMMENT); |
116 | if (sc.atLineEnd) {sc.SetState(SCE_B_DEFAULT);} |
117 | } |
118 | else if (strcmp(s, "asm" ) == 0) |
119 | { |
120 | sc.ChangeState(SCE_B_ASM); |
121 | if (sc.atLineEnd) {sc.SetState(SCE_B_DEFAULT);} |
122 | } |
123 | else |
124 | { |
125 | sc.SetState(SCE_B_DEFAULT); |
126 | } |
127 | } |
128 | else |
129 | { |
130 | sc.ChangeState(SCE_B_IDENTIFIER); |
131 | sc.SetState(SCE_B_DEFAULT); |
132 | } |
133 | } |
134 | } |
135 | break; |
136 | } |
137 | case SCE_B_NUMBER: |
138 | { |
139 | if (!IsAWordChar(sc.ch)) {sc.SetState(SCE_B_DEFAULT);} |
140 | break; |
141 | } |
142 | case SCE_B_STRING: |
143 | { |
144 | if (sc.ch == '\"'){sc.ForwardSetState(SCE_B_DEFAULT);} |
145 | break; |
146 | } |
147 | case SCE_B_CONSTANT: |
148 | { |
149 | if (!IsAWordChar(sc.ch)) {sc.SetState(SCE_B_DEFAULT);} |
150 | break; |
151 | } |
152 | case SCE_B_COMMENT: |
153 | { |
154 | if (sc.atLineEnd) {sc.SetState(SCE_B_DEFAULT);} |
155 | break; |
156 | } |
157 | case SCE_B_ASM: |
158 | { |
159 | if (sc.atLineEnd) {sc.SetState(SCE_B_DEFAULT);} |
160 | break; |
161 | } |
162 | } //switch (sc.state) |
163 | |
164 | // Determine if a new state should be entered: |
165 | if (sc.state == SCE_B_DEFAULT) |
166 | { |
167 | if (sc.ch == '\'') {sc.SetState(SCE_B_COMMENT);} |
168 | else if (sc.ch == '\"') {sc.SetState(SCE_B_STRING);} |
169 | else if (sc.ch == '&' && tolower(sc.chNext) == 'h') {sc.SetState(SCE_B_NUMBER);} |
170 | else if (sc.ch == '&' && tolower(sc.chNext) == 'b') {sc.SetState(SCE_B_NUMBER);} |
171 | else if (sc.ch == '&' && tolower(sc.chNext) == 'o') {sc.SetState(SCE_B_NUMBER);} |
172 | else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {sc.SetState(SCE_B_NUMBER);} |
173 | else if (IsAWordStart(sc.ch)) {sc.SetState(SCE_B_KEYWORD);} |
174 | else if (sc.ch == '%') {sc.SetState(SCE_B_CONSTANT);} |
175 | else if (sc.ch == '$') {sc.SetState(SCE_B_CONSTANT);} |
176 | else if (sc.ch == '#') {sc.SetState(SCE_B_KEYWORD);} |
177 | else if (sc.ch == '!') {sc.SetState(SCE_B_ASM);} |
178 | else if (isoperator(static_cast<char>(sc.ch)) || (sc.ch == '\\')) {sc.SetState(SCE_B_OPERATOR);} |
179 | } |
180 | } //for (; sc.More(); sc.Forward()) |
181 | sc.Complete(); |
182 | } |
183 | |
184 | //The folding routine for PowerBasic toggles SUBs and FUNCTIONs only. This was exactly what I wanted, |
185 | //nothing more. I had worked with this kind of toggling for several years when I used the great good old |
186 | //GFA Basic which is dead now. After testing the feature of toggling FOR-NEXT loops, WHILE-WEND loops |
187 | //and so on too I found this is more disturbing then helping (for me). So if You think in another way |
188 | //you can (or must) write Your own toggling routine ;-) |
189 | static void FoldPBDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) |
190 | { |
191 | // No folding enabled, no reason to continue... |
192 | if( styler.GetPropertyInt("fold" ) == 0 ) |
193 | return; |
194 | |
195 | Sci_PositionU endPos = startPos + length; |
196 | Sci_Position lineCurrent = styler.GetLine(startPos); |
197 | int levelCurrent = SC_FOLDLEVELBASE; |
198 | if (lineCurrent > 0) |
199 | levelCurrent = styler.LevelAt(lineCurrent-1) >> 16; |
200 | int levelNext = levelCurrent; |
201 | char chNext = styler[startPos]; |
202 | |
203 | bool fNewLine=true; |
204 | bool fMightBeMultiLineMacro=false; |
205 | bool =false; |
206 | for (Sci_PositionU i = startPos; i < endPos; i++) |
207 | { |
208 | char ch = chNext; |
209 | chNext = styler.SafeGetCharAt(i + 1); |
210 | |
211 | if (fNewLine) //Begin of a new line (The Sub/Function/Macro keywords may occur at begin of line only) |
212 | { |
213 | fNewLine=false; |
214 | fBeginOfCommentFound=false; |
215 | switch (ch) |
216 | { |
217 | case ' ': //Most lines start with space - so check this first, the code is the same as for 'default:' |
218 | case '\t': //Handle tab too |
219 | { |
220 | int levelUse = levelCurrent; |
221 | int lev = levelUse | levelNext << 16; |
222 | styler.SetLevel(lineCurrent, lev); |
223 | break; |
224 | } |
225 | case 'F': |
226 | case 'f': |
227 | { |
228 | switch (chNext) |
229 | { |
230 | case 'U': |
231 | case 'u': |
232 | { |
233 | if( MatchUpperCase(styler,i,"FUNCTION" ) ) |
234 | { |
235 | styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG); |
236 | levelNext=SC_FOLDLEVELBASE+1; |
237 | } |
238 | break; |
239 | } |
240 | } |
241 | break; |
242 | } |
243 | case 'S': |
244 | case 's': |
245 | { |
246 | switch (chNext) |
247 | { |
248 | case 'U': |
249 | case 'u': |
250 | { |
251 | if( MatchUpperCase(styler,i,"SUB" ) ) |
252 | { |
253 | styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG); |
254 | levelNext=SC_FOLDLEVELBASE+1; |
255 | } |
256 | break; |
257 | } |
258 | case 'T': |
259 | case 't': |
260 | { |
261 | if( MatchUpperCase(styler,i,"STATIC FUNCTION" ) ) |
262 | { |
263 | styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG); |
264 | levelNext=SC_FOLDLEVELBASE+1; |
265 | } |
266 | else if( MatchUpperCase(styler,i,"STATIC SUB" ) ) |
267 | { |
268 | styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG); |
269 | levelNext=SC_FOLDLEVELBASE+1; |
270 | } |
271 | break; |
272 | } |
273 | } |
274 | break; |
275 | } |
276 | case 'C': |
277 | case 'c': |
278 | { |
279 | switch (chNext) |
280 | { |
281 | case 'A': |
282 | case 'a': |
283 | { |
284 | if( MatchUpperCase(styler,i,"CALLBACK FUNCTION" ) ) |
285 | { |
286 | styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG); |
287 | levelNext=SC_FOLDLEVELBASE+1; |
288 | } |
289 | break; |
290 | } |
291 | } |
292 | break; |
293 | } |
294 | case 'M': |
295 | case 'm': |
296 | { |
297 | switch (chNext) |
298 | { |
299 | case 'A': |
300 | case 'a': |
301 | { |
302 | if( MatchUpperCase(styler,i,"MACRO" ) ) |
303 | { |
304 | fMightBeMultiLineMacro=true; //Set folder level at end of line, we have to check for single line macro |
305 | } |
306 | break; |
307 | } |
308 | } |
309 | break; |
310 | } |
311 | default: |
312 | { |
313 | int levelUse = levelCurrent; |
314 | int lev = levelUse | levelNext << 16; |
315 | styler.SetLevel(lineCurrent, lev); |
316 | break; |
317 | } |
318 | } //switch (ch) |
319 | } //if( fNewLine ) |
320 | |
321 | switch (ch) |
322 | { |
323 | case '=': //To test single line macros |
324 | { |
325 | if (fBeginOfCommentFound==false) |
326 | fMightBeMultiLineMacro=false; //The found macro is a single line macro only; |
327 | break; |
328 | } |
329 | case '\'': //A comment starts |
330 | { |
331 | fBeginOfCommentFound=true; |
332 | break; |
333 | } |
334 | case '\n': |
335 | { |
336 | if (fMightBeMultiLineMacro) //The current line is the begin of a multi line macro |
337 | { |
338 | fMightBeMultiLineMacro=false; |
339 | styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG); |
340 | levelNext=SC_FOLDLEVELBASE+1; |
341 | } |
342 | lineCurrent++; |
343 | levelCurrent = levelNext; |
344 | fNewLine=true; |
345 | break; |
346 | } |
347 | case '\r': |
348 | { |
349 | if (chNext != '\n') |
350 | { |
351 | lineCurrent++; |
352 | levelCurrent = levelNext; |
353 | fNewLine=true; |
354 | } |
355 | break; |
356 | } |
357 | } //switch (ch) |
358 | } //for (Sci_PositionU i = startPos; i < endPos; i++) |
359 | } |
360 | |
361 | static const char * const pbWordListDesc[] = { |
362 | "Keywords" , |
363 | 0 |
364 | }; |
365 | |
366 | LexerModule lmPB(SCLEX_POWERBASIC, ColourisePBDoc, "powerbasic" , FoldPBDoc, pbWordListDesc); |
367 | |