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