]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/lexers/LexVB.cxx
Search for font instead of creating it each time
[wxWidgets.git] / src / stc / scintilla / lexers / 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 <stdio.h>
11 #include <stdarg.h>
12 #include <assert.h>
13 #include <ctype.h>
14
15 #include "ILexer.h"
16 #include "Scintilla.h"
17 #include "SciLexer.h"
18
19 #include "WordList.h"
20 #include "LexAccessor.h"
21 #include "Accessor.h"
22 #include "StyleContext.h"
23 #include "CharacterSet.h"
24 #include "LexerModule.h"
25
26 #ifdef SCI_NAMESPACE
27 using namespace Scintilla;
28 #endif
29
30 // Internal state, highlighted as number
31 #define SCE_B_FILENUMBER SCE_B_DEFAULT+100
32
33
34 static bool IsVBComment(Accessor &styler, int pos, int len) {
35 return len > 0 && styler[pos] == '\'';
36 }
37
38 static inline bool IsTypeCharacter(int ch) {
39 return ch == '%' || ch == '&' || ch == '@' || ch == '!' || ch == '#' || ch == '$';
40 }
41
42 // Extended to accept accented characters
43 static inline bool IsAWordChar(int ch) {
44 return ch >= 0x80 ||
45 (isalnum(ch) || ch == '.' || ch == '_');
46 }
47
48 static inline bool IsAWordStart(int ch) {
49 return ch >= 0x80 ||
50 (isalpha(ch) || ch == '_');
51 }
52
53 static 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.
56 return (ch < 0x80) &&
57 (isdigit(ch) || toupper(ch) == 'E' ||
58 ch == '.' || ch == '-' || ch == '+');
59 }
60
61 static void ColouriseVBDoc(unsigned int startPos, int length, int initStyle,
62 WordList *keywordlists[], Accessor &styler, bool vbScriptSyntax) {
63
64 WordList &keywords = *keywordlists[0];
65 WordList &keywords2 = *keywordlists[1];
66 WordList &keywords3 = *keywordlists[2];
67 WordList &keywords4 = *keywordlists[3];
68
69 styler.StartAt(startPos);
70
71 int visibleChars = 0;
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 }
78
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);
85 } else if (sc.state == SCE_B_IDENTIFIER) {
86 if (!IsAWordChar(sc.ch)) {
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 {
106 if (keywords.InList(s)) {
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);
116 }
117 }
118 } else if (sc.state == SCE_B_NUMBER) {
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')) {
122 sc.SetState(SCE_B_DEFAULT);
123 }
124 } else if (sc.state == SCE_B_STRING) {
125 // VB doubles quotes to preserve them, so just end this string
126 // state now as a following quote will start again
127 if (sc.ch == '\"') {
128 if (sc.chNext == '\"') {
129 sc.Forward();
130 } else {
131 if (tolower(sc.chNext) == 'c') {
132 sc.Forward();
133 }
134 sc.ForwardSetState(SCE_B_DEFAULT);
135 }
136 } else if (sc.atLineEnd) {
137 visibleChars = 0;
138 sc.ChangeState(SCE_B_STRINGEOL);
139 sc.ForwardSetState(SCE_B_DEFAULT);
140 }
141 } else if (sc.state == SCE_B_COMMENT) {
142 if (sc.atLineEnd) {
143 visibleChars = 0;
144 sc.ForwardSetState(SCE_B_DEFAULT);
145 }
146 } else if (sc.state == SCE_B_PREPROCESSOR) {
147 if (sc.atLineEnd) {
148 visibleChars = 0;
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);
162 sc.SetState(SCE_B_DEFAULT);
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;
171 }
172 } else if (sc.state == SCE_B_DATE) {
173 if (sc.atLineEnd) {
174 visibleChars = 0;
175 sc.ChangeState(SCE_B_STRINGEOL);
176 sc.ForwardSetState(SCE_B_DEFAULT);
177 } else if (sc.ch == '#') {
178 sc.ForwardSetState(SCE_B_DEFAULT);
179 }
180 }
181
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 == '#') {
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);
196 } else if (sc.ch == '&' && tolower(sc.chNext) == 'h') {
197 // Hexadecimal number
198 sc.SetState(SCE_B_NUMBER);
199 sc.Forward();
200 } else if (sc.ch == '&' && tolower(sc.chNext) == 'o') {
201 // Octal number
202 sc.SetState(SCE_B_NUMBER);
203 sc.Forward();
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 == '[')) {
207 sc.SetState(SCE_B_IDENTIFIER);
208 } else if (isoperator(static_cast<char>(sc.ch)) || (sc.ch == '\\')) { // Integer division
209 sc.SetState(SCE_B_OPERATOR);
210 }
211 }
212
213 if (sc.atLineEnd) {
214 visibleChars = 0;
215 }
216 if (!IsASpace(sc.ch)) {
217 visibleChars++;
218 }
219 }
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
254 sc.Complete();
255 }
256
257 static void FoldVBDoc(unsigned int startPos, int length, int,
258 WordList *[], Accessor &styler) {
259 int endPos = startPos + length;
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);
267 }
268 }
269 int spaceFlags = 0;
270 int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, IsVBComment);
271 char chNext = styler[startPos];
272 for (int i = startPos; i < endPos; i++) {
273 char ch = chNext;
274 chNext = styler.SafeGetCharAt(i + 1);
275
276 if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == endPos)) {
277 int lev = indentCurrent;
278 int indentNext = styler.IndentAmount(lineCurrent + 1, &spaceFlags, IsVBComment);
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
299 static void ColouriseVBNetDoc(unsigned int startPos, int length, int initStyle,
300 WordList *keywordlists[], Accessor &styler) {
301 ColouriseVBDoc(startPos, length, initStyle, keywordlists, styler, false);
302 }
303
304 static void ColouriseVBScriptDoc(unsigned int startPos, int length, int initStyle,
305 WordList *keywordlists[], Accessor &styler) {
306 ColouriseVBDoc(startPos, length, initStyle, keywordlists, styler, true);
307 }
308
309 static const char * const vbWordListDesc[] = {
310 "Keywords",
311 "user1",
312 "user2",
313 "user3",
314 0
315 };
316
317 LexerModule lmVB(SCLEX_VB, ColouriseVBNetDoc, "vb", FoldVBDoc, vbWordListDesc);
318 LexerModule lmVBScript(SCLEX_VBSCRIPT, ColouriseVBScriptDoc, "vbscript", FoldVBDoc, vbWordListDesc);
319