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