]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/LexBasic.cxx
4a1843aa0d05805a9fa203707549ff5a710cdbce
[wxWidgets.git] / contrib / src / stc / scintilla / src / LexBasic.cxx
1 // Scintilla source code edit control
2 /** @file LexBasic.cxx
3 ** Lexer for BlitzBasic and PureBasic.
4 **/
5 // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
7
8 // This tries to be a unified Lexer/Folder for all the BlitzBasic/BlitzMax/PurBasic basics
9 // and derivatives. Once they diverge enough, might want to split it into multiple
10 // lexers for more code clearity.
11 //
12 // Mail me (elias <at> users <dot> sf <dot> net) for any bugs.
13
14 // Folding only works for simple things like functions or types.
15
16 // You may want to have a look at my ctags lexer as well, if you additionally to coloring
17 // and folding need to extract things like label tags in your editor.
18
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <stdarg.h>
24
25 #include "Platform.h"
26
27 #include "PropSet.h"
28 #include "Accessor.h"
29 #include "StyleContext.h"
30 #include "KeyWords.h"
31 #include "Scintilla.h"
32 #include "SciLexer.h"
33
34 /* Bits:
35 * 1 - whitespace
36 * 2 - operator
37 * 4 - identifier
38 * 8 - decimal digit
39 * 16 - hex digit
40 * 32 - bin digit
41 */
42 static int character_classification[128] =
43 {
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46 1, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 10, 2,
47 60, 60, 28, 28, 28, 28, 28, 28, 28, 28, 2, 2, 2, 2, 2, 2,
48 2, 20, 20, 20, 20, 20, 20, 4, 4, 4, 4, 4, 4, 4, 4, 4,
49 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 4,
50 2, 20, 20, 20, 20, 20, 20, 4, 4, 4, 4, 4, 4, 4, 4, 4,
51 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 0
52 };
53
54 static bool IsSpace(int c) {
55 return c < 128 && (character_classification[c] & 1);
56 }
57
58 static bool IsOperator(int c) {
59 return c < 128 && (character_classification[c] & 2);
60 }
61
62 static bool IsIdentifier(int c) {
63 return c < 128 && (character_classification[c] & 4);
64 }
65
66 static bool IsDigit(int c) {
67 return c < 128 && (character_classification[c] & 8);
68 }
69
70 static bool IsHexDigit(int c) {
71 return c < 128 && (character_classification[c] & 16);
72 }
73
74 static bool IsBinDigit(int c) {
75 return c < 128 && (character_classification[c] & 32);
76 }
77
78 static int LowerCase(int c)
79 {
80 if (c >= 'A' && c <= 'Z')
81 return 'a' + c - 'A';
82 return c;
83 }
84
85 static void ColouriseBasicDoc(unsigned int startPos, int length, int initStyle,
86 WordList *keywordlists[], Accessor &styler, char comment_char) {
87 bool wasfirst = true, isfirst = true; // true if first token in a line
88 styler.StartAt(startPos);
89
90 StyleContext sc(startPos, length, initStyle, styler);
91
92 // Can't use sc.More() here else we miss the last character
93 for (; ; sc.Forward()) {
94 if (sc.state == SCE_B_IDENTIFIER) {
95 if (!IsIdentifier(sc.ch)) {
96 // Labels
97 if (wasfirst && sc.Match(':')) {
98 sc.ChangeState(SCE_B_LABEL);
99 sc.ForwardSetState(SCE_B_DEFAULT);
100 } else {
101 char s[100];
102 int kstates[4] = {
103 SCE_B_KEYWORD,
104 SCE_B_KEYWORD2,
105 SCE_B_KEYWORD3,
106 SCE_B_KEYWORD4,
107 };
108 sc.GetCurrentLowered(s, sizeof(s));
109 for (int i = 0; i < 4; i++) {
110 if (keywordlists[i]->InList(s)) {
111 sc.ChangeState(kstates[i]);
112 }
113 }
114 // Types, must set them as operator else they will be
115 // matched as number/constant
116 if (sc.Match('.') || sc.Match('$') || sc.Match('%') ||
117 sc.Match('#')) {
118 sc.SetState(SCE_B_OPERATOR);
119 } else {
120 sc.SetState(SCE_B_DEFAULT);
121 }
122 }
123 }
124 } else if (sc.state == SCE_B_OPERATOR) {
125 if (!IsOperator(sc.ch) || sc.Match('#'))
126 sc.SetState(SCE_B_DEFAULT);
127 } else if (sc.state == SCE_B_LABEL) {
128 if (!IsIdentifier(sc.ch))
129 sc.SetState(SCE_B_DEFAULT);
130 } else if (sc.state == SCE_B_CONSTANT) {
131 if (!IsIdentifier(sc.ch))
132 sc.SetState(SCE_B_DEFAULT);
133 } else if (sc.state == SCE_B_NUMBER) {
134 if (!IsDigit(sc.ch))
135 sc.SetState(SCE_B_DEFAULT);
136 } else if (sc.state == SCE_B_HEXNUMBER) {
137 if (!IsHexDigit(sc.ch))
138 sc.SetState(SCE_B_DEFAULT);
139 } else if (sc.state == SCE_B_BINNUMBER) {
140 if (!IsBinDigit(sc.ch))
141 sc.SetState(SCE_B_DEFAULT);
142 } else if (sc.state == SCE_B_STRING) {
143 if (sc.ch == '"') {
144 sc.ForwardSetState(SCE_B_DEFAULT);
145 }
146 if (sc.atLineEnd) {
147 sc.ChangeState(SCE_B_ERROR);
148 sc.SetState(SCE_B_DEFAULT);
149 }
150 } else if (sc.state == SCE_B_COMMENT) {
151 if (sc.atLineEnd) {
152 sc.SetState(SCE_B_DEFAULT);
153 }
154 }
155
156 if (sc.atLineStart)
157 isfirst = true;
158
159 if (sc.state == SCE_B_DEFAULT || sc.state == SCE_B_ERROR) {
160 if (isfirst && sc.Match('.')) {
161 sc.SetState(SCE_B_LABEL);
162 } else if (isfirst && sc.Match('#')) {
163 wasfirst = isfirst;
164 sc.SetState(SCE_B_IDENTIFIER);
165 } else if (sc.Match(comment_char)) {
166 sc.SetState(SCE_B_COMMENT);
167 } else if (sc.Match('"')) {
168 sc.SetState(SCE_B_STRING);
169 } else if (IsDigit(sc.ch)) {
170 sc.SetState(SCE_B_NUMBER);
171 } else if (sc.Match('$')) {
172 sc.SetState(SCE_B_HEXNUMBER);
173 } else if (sc.Match('%')) {
174 sc.SetState(SCE_B_BINNUMBER);
175 } else if (sc.Match('#')) {
176 sc.SetState(SCE_B_CONSTANT);
177 } else if (IsOperator(sc.ch)) {
178 sc.SetState(SCE_B_OPERATOR);
179 } else if (IsIdentifier(sc.ch)) {
180 wasfirst = isfirst;
181 sc.SetState(SCE_B_IDENTIFIER);
182 } else if (!IsSpace(sc.ch)) {
183 sc.SetState(SCE_B_ERROR);
184 }
185 }
186
187 if (!IsSpace(sc.ch))
188 isfirst = false;
189
190 if (!sc.More())
191 break;
192 }
193 sc.Complete();
194 }
195
196 static int CheckBlitzFoldPoint(char const *token, int &level) {
197 if (!strcmp(token, "function") ||
198 !strcmp(token, "type")) {
199 level |= SC_FOLDLEVELHEADERFLAG;
200 return 1;
201 }
202 if (!strcmp(token, "end function") ||
203 !strcmp(token, "end type")) {
204 return -1;
205 }
206 return 0;
207 }
208
209 static int CheckPureFoldPoint(char const *token, int &level) {
210 if (!strcmp(token, "procedure") ||
211 !strcmp(token, "enumeration") ||
212 !strcmp(token, "interface") ||
213 !strcmp(token, "structure")) {
214 level |= SC_FOLDLEVELHEADERFLAG;
215 return 1;
216 }
217 if (!strcmp(token, "endprocedure") ||
218 !strcmp(token, "endenumeration") ||
219 !strcmp(token, "endinterface") ||
220 !strcmp(token, "endstructure")) {
221 return -1;
222 }
223 return 0;
224 }
225
226 static int CheckFreeFoldPoint(char const *token, int &level) {
227 if (!strcmp(token, "function") ||
228 !strcmp(token, "sub") ||
229 !strcmp(token, "type")) {
230 level |= SC_FOLDLEVELHEADERFLAG;
231 return 1;
232 }
233 if (!strcmp(token, "end function") ||
234 !strcmp(token, "end sub") ||
235 !strcmp(token, "end type")) {
236 return -1;
237 }
238 return 0;
239 }
240
241 static void FoldBasicDoc(unsigned int startPos, int length,
242 Accessor &styler, int (*CheckFoldPoint)(char const *, int &)) {
243 int line = styler.GetLine(startPos);
244 int level = styler.LevelAt(line);
245 int go = 0, done = 0;
246 int endPos = startPos + length;
247 char word[256];
248 int wordlen = 0;
249 int i;
250 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
251 // Scan for tokens at the start of the line (they may include
252 // whitespace, for tokens like "End Function"
253 for (i = startPos; i < endPos; i++) {
254 int c = styler.SafeGetCharAt(i);
255 if (!done && !go) {
256 if (wordlen) { // are we scanning a token already?
257 word[wordlen] = static_cast<char>(LowerCase(c));
258 if (!IsIdentifier(c)) { // done with token
259 word[wordlen] = '\0';
260 go = CheckFoldPoint(word, level);
261 if (!go) {
262 // Treat any whitespace as single blank, for
263 // things like "End Function".
264 if (IsSpace(c) && IsIdentifier(word[wordlen - 1])) {
265 word[wordlen] = ' ';
266 if (wordlen < 255)
267 wordlen++;
268 }
269 else // done with this line
270 done = 1;
271 }
272 } else if (wordlen < 255) {
273 wordlen++;
274 }
275 } else { // start scanning at first non-whitespace character
276 if (!IsSpace(c)) {
277 if (IsIdentifier(c)) {
278 word[0] = static_cast<char>(LowerCase(c));
279 wordlen = 1;
280 } else // done with this line
281 done = 1;
282 }
283 }
284 }
285 if (c == '\n') { // line end
286 if (!done && wordlen == 0 && foldCompact) // line was only space
287 level |= SC_FOLDLEVELWHITEFLAG;
288 if (level != styler.LevelAt(line))
289 styler.SetLevel(line, level);
290 level += go;
291 line++;
292 // reset state
293 wordlen = 0;
294 level &= ~SC_FOLDLEVELHEADERFLAG;
295 level &= ~SC_FOLDLEVELWHITEFLAG;
296 go = 0;
297 done = 0;
298 }
299 }
300 }
301
302 static void ColouriseBlitzBasicDoc(unsigned int startPos, int length, int initStyle,
303 WordList *keywordlists[], Accessor &styler) {
304 ColouriseBasicDoc(startPos, length, initStyle, keywordlists, styler, ';');
305 }
306
307 static void ColourisePureBasicDoc(unsigned int startPos, int length, int initStyle,
308 WordList *keywordlists[], Accessor &styler) {
309 ColouriseBasicDoc(startPos, length, initStyle, keywordlists, styler, ';');
310 }
311
312 static void ColouriseFreeBasicDoc(unsigned int startPos, int length, int initStyle,
313 WordList *keywordlists[], Accessor &styler) {
314 ColouriseBasicDoc(startPos, length, initStyle, keywordlists, styler, '\'');
315 }
316
317 static void FoldBlitzBasicDoc(unsigned int startPos, int length, int,
318 WordList *[], Accessor &styler) {
319 FoldBasicDoc(startPos, length, styler, CheckBlitzFoldPoint);
320 }
321
322 static void FoldPureBasicDoc(unsigned int startPos, int length, int,
323 WordList *[], Accessor &styler) {
324 FoldBasicDoc(startPos, length, styler, CheckPureFoldPoint);
325 }
326
327 static void FoldFreeBasicDoc(unsigned int startPos, int length, int,
328 WordList *[], Accessor &styler) {
329 FoldBasicDoc(startPos, length, styler, CheckFreeFoldPoint);
330 }
331
332 static const char * const blitzbasicWordListDesc[] = {
333 "BlitzBasic Keywords",
334 "user1",
335 "user2",
336 "user3",
337 0
338 };
339
340 static const char * const purebasicWordListDesc[] = {
341 "PureBasic Keywords",
342 "PureBasic PreProcessor Keywords",
343 "user defined 1",
344 "user defined 2",
345 0
346 };
347
348 static const char * const freebasicWordListDesc[] = {
349 "FreeBasic Keywords",
350 "FreeBasic PreProcessor Keywords",
351 "user defined 1",
352 "user defined 2",
353 0
354 };
355
356 LexerModule lmBlitzBasic(SCLEX_BLITZBASIC, ColouriseBlitzBasicDoc, "blitzbasic",
357 FoldBlitzBasicDoc, blitzbasicWordListDesc);
358
359 LexerModule lmPureBasic(SCLEX_PUREBASIC, ColourisePureBasicDoc, "purebasic",
360 FoldPureBasicDoc, purebasicWordListDesc);
361
362 LexerModule lmFreeBasic(SCLEX_FREEBASIC, ColouriseFreeBasicDoc, "freebasic",
363 FoldFreeBasicDoc, freebasicWordListDesc);
364