]> git.saurik.com Git - wxWidgets.git/blame - src/stc/scintilla/lexers/LexVB.cxx
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / src / stc / scintilla / lexers / LexVB.cxx
CommitLineData
65ec6247
RD
1// Scintilla source code edit control
2/** @file LexVB.cxx
3 ** Lexer for Visual Basic and VBScript.
4 **/
1e9bafca 5// Copyright 1998-2005 by Neil Hodgson <neilh@scintilla.org>
f6bcfd97
BP
6// The License.txt file describes the conditions under which this software may be distributed.
7
65ec6247
RD
8#include <stdlib.h>
9#include <string.h>
65ec6247
RD
10#include <stdio.h>
11#include <stdarg.h>
1dcf666d
RD
12#include <assert.h>
13#include <ctype.h>
f6bcfd97 14
1dcf666d
RD
15#include "ILexer.h"
16#include "Scintilla.h"
17#include "SciLexer.h"
f6bcfd97 18
1dcf666d
RD
19#include "WordList.h"
20#include "LexAccessor.h"
f6bcfd97 21#include "Accessor.h"
1a2fb4cd 22#include "StyleContext.h"
1dcf666d
RD
23#include "CharacterSet.h"
24#include "LexerModule.h"
f6bcfd97 25
7e0c58e9
RD
26#ifdef SCI_NAMESPACE
27using namespace Scintilla;
28#endif
29
1e9bafca
RD
30// Internal state, highlighted as number
31#define SCE_B_FILENUMBER SCE_B_DEFAULT+100
32
33
1a2fb4cd 34static bool IsVBComment(Accessor &styler, int pos, int len) {
1e9bafca 35 return len > 0 && styler[pos] == '\'';
1a2fb4cd 36}
f6bcfd97 37
8e54aaed 38static inline bool IsTypeCharacter(int ch) {
1a2fb4cd 39 return ch == '%' || ch == '&' || ch == '@' || ch == '!' || ch == '#' || ch == '$';
f6bcfd97
BP
40}
41
8e54aaed
RD
42// Extended to accept accented characters
43static inline bool IsAWordChar(int ch) {
44 return ch >= 0x80 ||
45 (isalnum(ch) || ch == '.' || ch == '_');
1a2fb4cd
RD
46}
47
8e54aaed
RD
48static inline bool IsAWordStart(int ch) {
49 return ch >= 0x80 ||
1e9bafca 50 (isalpha(ch) || ch == '_');
1a2fb4cd
RD
51}
52
1e9bafca
RD
53static inline bool IsANumberChar(int ch) {
54 // Not exactly following number definition (several dots are seen as OK, etc.)
55 // but probably enough in most cases.
9e730a78 56 return (ch < 0x80) &&
1e9bafca
RD
57 (isdigit(ch) || toupper(ch) == 'E' ||
58 ch == '.' || ch == '-' || ch == '+');
65ec6247
RD
59}
60
f6bcfd97 61static void ColouriseVBDoc(unsigned int startPos, int length, int initStyle,
1a2fb4cd 62 WordList *keywordlists[], Accessor &styler, bool vbScriptSyntax) {
f6bcfd97
BP
63
64 WordList &keywords = *keywordlists[0];
8e54aaed
RD
65 WordList &keywords2 = *keywordlists[1];
66 WordList &keywords3 = *keywordlists[2];
67 WordList &keywords4 = *keywordlists[3];
65ec6247 68
f6bcfd97
BP
69 styler.StartAt(startPos);
70
d134f170 71 int visibleChars = 0;
1e9bafca
RD
72 int fileNbDigits = 0;
73
74 // Do not leak onto next line
75 if (initStyle == SCE_B_STRINGEOL || initStyle == SCE_B_COMMENT || initStyle == SCE_B_PREPROCESSOR) {
76 initStyle = SCE_B_DEFAULT;
77 }
9e730a78 78
1a2fb4cd
RD
79 StyleContext sc(startPos, length, initStyle, styler);
80
81 for (; sc.More(); sc.Forward()) {
82
83 if (sc.state == SCE_B_OPERATOR) {
84 sc.SetState(SCE_B_DEFAULT);
8e54aaed 85 } else if (sc.state == SCE_B_IDENTIFIER) {
1a2fb4cd 86 if (!IsAWordChar(sc.ch)) {
8e54aaed
RD
87 // In Basic (except VBScript), a variable name or a function name
88 // can end with a special character indicating the type of the value
89 // held or returned.
90 bool skipType = false;
91 if (!vbScriptSyntax && IsTypeCharacter(sc.ch)) {
92 sc.Forward(); // Skip it
93 skipType = true;
94 }
95 if (sc.ch == ']') {
96 sc.Forward();
97 }
98 char s[100];
99 sc.GetCurrentLowered(s, sizeof(s));
100 if (skipType) {
101 s[strlen(s) - 1] = '\0';
102 }
103 if (strcmp(s, "rem") == 0) {
104 sc.ChangeState(SCE_B_COMMENT);
105 } else {
1a2fb4cd 106 if (keywords.InList(s)) {
8e54aaed
RD
107 sc.ChangeState(SCE_B_KEYWORD);
108 } else if (keywords2.InList(s)) {
109 sc.ChangeState(SCE_B_KEYWORD2);
110 } else if (keywords3.InList(s)) {
111 sc.ChangeState(SCE_B_KEYWORD3);
112 } else if (keywords4.InList(s)) {
113 sc.ChangeState(SCE_B_KEYWORD4);
114 } // Else, it is really an identifier...
115 sc.SetState(SCE_B_DEFAULT);
f6bcfd97
BP
116 }
117 }
1a2fb4cd 118 } else if (sc.state == SCE_B_NUMBER) {
1e9bafca
RD
119 // We stop the number definition on non-numerical non-dot non-eE non-sign char
120 // Also accepts A-F for hex. numbers
121 if (!IsANumberChar(sc.ch) && !(tolower(sc.ch) >= 'a' && tolower(sc.ch) <= 'f')) {
1a2fb4cd
RD
122 sc.SetState(SCE_B_DEFAULT);
123 }
124 } else if (sc.state == SCE_B_STRING) {
9e730a78 125 // VB doubles quotes to preserve them, so just end this string
1a2fb4cd
RD
126 // state now as a following quote will start again
127 if (sc.ch == '\"') {
7e0c58e9 128 if (sc.chNext == '\"') {
1a2fb4cd 129 sc.Forward();
7e0c58e9
RD
130 } else {
131 if (tolower(sc.chNext) == 'c') {
132 sc.Forward();
133 }
134 sc.ForwardSetState(SCE_B_DEFAULT);
f6bcfd97 135 }
8e54aaed 136 } else if (sc.atLineEnd) {
7e0c58e9 137 visibleChars = 0;
8e54aaed
RD
138 sc.ChangeState(SCE_B_STRINGEOL);
139 sc.ForwardSetState(SCE_B_DEFAULT);
1a2fb4cd
RD
140 }
141 } else if (sc.state == SCE_B_COMMENT) {
142 if (sc.atLineEnd) {
7e0c58e9 143 visibleChars = 0;
1e9bafca 144 sc.ForwardSetState(SCE_B_DEFAULT);
1a2fb4cd
RD
145 }
146 } else if (sc.state == SCE_B_PREPROCESSOR) {
147 if (sc.atLineEnd) {
7e0c58e9 148 visibleChars = 0;
1e9bafca
RD
149 sc.ForwardSetState(SCE_B_DEFAULT);
150 }
151 } else if (sc.state == SCE_B_FILENUMBER) {
152 if (IsADigit(sc.ch)) {
153 fileNbDigits++;
154 if (fileNbDigits > 3) {
155 sc.ChangeState(SCE_B_DATE);
156 }
157 } else if (sc.ch == '\r' || sc.ch == '\n' || sc.ch == ',') {
158 // Regular uses: Close #1; Put #1, ...; Get #1, ... etc.
159 // Too bad if date is format #27, Oct, 2003# or something like that...
160 // Use regular number state
161 sc.ChangeState(SCE_B_NUMBER);
1a2fb4cd 162 sc.SetState(SCE_B_DEFAULT);
1e9bafca
RD
163 } else if (sc.ch == '#') {
164 sc.ChangeState(SCE_B_DATE);
165 sc.ForwardSetState(SCE_B_DEFAULT);
166 } else {
167 sc.ChangeState(SCE_B_DATE);
168 }
169 if (sc.state != SCE_B_FILENUMBER) {
170 fileNbDigits = 0;
f6bcfd97 171 }
1a2fb4cd 172 } else if (sc.state == SCE_B_DATE) {
1e9bafca 173 if (sc.atLineEnd) {
7e0c58e9 174 visibleChars = 0;
1e9bafca
RD
175 sc.ChangeState(SCE_B_STRINGEOL);
176 sc.ForwardSetState(SCE_B_DEFAULT);
177 } else if (sc.ch == '#') {
1a2fb4cd
RD
178 sc.ForwardSetState(SCE_B_DEFAULT);
179 }
180 }
9e730a78 181
1a2fb4cd
RD
182 if (sc.state == SCE_B_DEFAULT) {
183 if (sc.ch == '\'') {
184 sc.SetState(SCE_B_COMMENT);
185 } else if (sc.ch == '\"') {
186 sc.SetState(SCE_B_STRING);
187 } else if (sc.ch == '#' && visibleChars == 0) {
188 // Preprocessor commands are alone on their line
189 sc.SetState(SCE_B_PREPROCESSOR);
190 } else if (sc.ch == '#') {
1e9bafca
RD
191 // It can be a date literal, ending with #, or a file number, from 1 to 511
192 // The date literal depends on the locale, so anything can go between #'s.
193 // Can be #January 1, 1993# or #1 Jan 93# or #05/11/2003#, etc.
194 // So we set the FILENUMBER state, and switch to DATE if it isn't a file number
195 sc.SetState(SCE_B_FILENUMBER);
1a2fb4cd 196 } else if (sc.ch == '&' && tolower(sc.chNext) == 'h') {
1e9bafca 197 // Hexadecimal number
1a2fb4cd 198 sc.SetState(SCE_B_NUMBER);
1e9bafca 199 sc.Forward();
1a2fb4cd 200 } else if (sc.ch == '&' && tolower(sc.chNext) == 'o') {
1e9bafca 201 // Octal number
1a2fb4cd 202 sc.SetState(SCE_B_NUMBER);
1e9bafca 203 sc.Forward();
1a2fb4cd
RD
204 } else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
205 sc.SetState(SCE_B_NUMBER);
206 } else if (IsAWordStart(sc.ch) || (sc.ch == '[')) {
8e54aaed 207 sc.SetState(SCE_B_IDENTIFIER);
1e9bafca 208 } else if (isoperator(static_cast<char>(sc.ch)) || (sc.ch == '\\')) { // Integer division
1a2fb4cd 209 sc.SetState(SCE_B_OPERATOR);
f6bcfd97
BP
210 }
211 }
9e730a78 212
1a2fb4cd
RD
213 if (sc.atLineEnd) {
214 visibleChars = 0;
215 }
216 if (!IsASpace(sc.ch)) {
217 visibleChars++;
218 }
f6bcfd97 219 }
9e96e16f
RD
220
221 if (sc.state == SCE_B_IDENTIFIER && !IsAWordChar(sc.ch)) {
222 // In Basic (except VBScript), a variable name or a function name
223 // can end with a special character indicating the type of the value
224 // held or returned.
225 bool skipType = false;
226 if (!vbScriptSyntax && IsTypeCharacter(sc.ch)) {
227 sc.Forward(); // Skip it
228 skipType = true;
229 }
230 if (sc.ch == ']') {
231 sc.Forward();
232 }
233 char s[100];
234 sc.GetCurrentLowered(s, sizeof(s));
235 if (skipType) {
236 s[strlen(s) - 1] = '\0';
237 }
238 if (strcmp(s, "rem") == 0) {
239 sc.ChangeState(SCE_B_COMMENT);
240 } else {
241 if (keywords.InList(s)) {
242 sc.ChangeState(SCE_B_KEYWORD);
243 } else if (keywords2.InList(s)) {
244 sc.ChangeState(SCE_B_KEYWORD2);
245 } else if (keywords3.InList(s)) {
246 sc.ChangeState(SCE_B_KEYWORD3);
247 } else if (keywords4.InList(s)) {
248 sc.ChangeState(SCE_B_KEYWORD4);
249 } // Else, it is really an identifier...
250 sc.SetState(SCE_B_DEFAULT);
251 }
252 }
253
1a2fb4cd 254 sc.Complete();
f6bcfd97
BP
255}
256
1a2fb4cd 257static void FoldVBDoc(unsigned int startPos, int length, int,
65ec6247 258 WordList *[], Accessor &styler) {
1a2fb4cd 259 int endPos = startPos + length;
65ec6247
RD
260
261 // Backtrack to previous line in case need to fix its fold status
262 int lineCurrent = styler.GetLine(startPos);
263 if (startPos > 0) {
264 if (lineCurrent > 0) {
265 lineCurrent--;
266 startPos = styler.LineStart(lineCurrent);
65ec6247
RD
267 }
268 }
65ec6247
RD
269 int spaceFlags = 0;
270 int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, IsVBComment);
65ec6247 271 char chNext = styler[startPos];
1a2fb4cd 272 for (int i = startPos; i < endPos; i++) {
65ec6247
RD
273 char ch = chNext;
274 chNext = styler.SafeGetCharAt(i + 1);
65ec6247 275
1a2fb4cd 276 if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == endPos)) {
65ec6247
RD
277 int lev = indentCurrent;
278 int indentNext = styler.IndentAmount(lineCurrent + 1, &spaceFlags, IsVBComment);
65ec6247
RD
279 if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG)) {
280 // Only non whitespace lines can be headers
281 if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK)) {
282 lev |= SC_FOLDLEVELHEADERFLAG;
283 } else if (indentNext & SC_FOLDLEVELWHITEFLAG) {
284 // Line after is blank so check the next - maybe should continue further?
285 int spaceFlags2 = 0;
286 int indentNext2 = styler.IndentAmount(lineCurrent + 2, &spaceFlags2, IsVBComment);
287 if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext2 & SC_FOLDLEVELNUMBERMASK)) {
288 lev |= SC_FOLDLEVELHEADERFLAG;
289 }
290 }
291 }
292 indentCurrent = indentNext;
293 styler.SetLevel(lineCurrent, lev);
294 lineCurrent++;
295 }
296 }
297}
298
1a2fb4cd
RD
299static void ColouriseVBNetDoc(unsigned int startPos, int length, int initStyle,
300 WordList *keywordlists[], Accessor &styler) {
301 ColouriseVBDoc(startPos, length, initStyle, keywordlists, styler, false);
302}
303
304static void ColouriseVBScriptDoc(unsigned int startPos, int length, int initStyle,
305 WordList *keywordlists[], Accessor &styler) {
306 ColouriseVBDoc(startPos, length, initStyle, keywordlists, styler, true);
307}
308
9e730a78
RD
309static const char * const vbWordListDesc[] = {
310 "Keywords",
8e54aaed
RD
311 "user1",
312 "user2",
313 "user3",
9e730a78
RD
314 0
315};
316
317LexerModule lmVB(SCLEX_VB, ColouriseVBNetDoc, "vb", FoldVBDoc, vbWordListDesc);
318LexerModule lmVBScript(SCLEX_VBSCRIPT, ColouriseVBScriptDoc, "vbscript", FoldVBDoc, vbWordListDesc);
1a2fb4cd 319