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