]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/lexers/LexVB.cxx
1 // Scintilla source code edit control
3 ** Lexer for Visual Basic and VBScript.
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.
16 #include "Scintilla.h"
20 #include "LexAccessor.h"
22 #include "StyleContext.h"
23 #include "CharacterSet.h"
24 #include "LexerModule.h"
27 using namespace Scintilla
;
30 // Internal state, highlighted as number
31 #define SCE_B_FILENUMBER SCE_B_DEFAULT+100
34 static bool IsVBComment(Accessor
&styler
, int pos
, int len
) {
35 return len
> 0 && styler
[pos
] == '\'';
38 static inline bool IsTypeCharacter(int ch
) {
39 return ch
== '%' || ch
== '&' || ch
== '@' || ch
== '!' || ch
== '#' || ch
== '$';
42 // Extended to accept accented characters
43 static inline bool IsAWordChar(int ch
) {
45 (isalnum(ch
) || ch
== '.' || ch
== '_');
48 static inline bool IsAWordStart(int ch
) {
50 (isalpha(ch
) || ch
== '_');
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.
57 (isdigit(ch
) || toupper(ch
) == 'E' ||
58 ch
== '.' || ch
== '-' || ch
== '+');
61 static void ColouriseVBDoc(unsigned int startPos
, int length
, int initStyle
,
62 WordList
*keywordlists
[], Accessor
&styler
, bool vbScriptSyntax
) {
64 WordList
&keywords
= *keywordlists
[0];
65 WordList
&keywords2
= *keywordlists
[1];
66 WordList
&keywords3
= *keywordlists
[2];
67 WordList
&keywords4
= *keywordlists
[3];
69 styler
.StartAt(startPos
);
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
;
79 StyleContext
sc(startPos
, length
, initStyle
, styler
);
81 for (; sc
.More(); sc
.Forward()) {
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
90 bool skipType
= false;
91 if (!vbScriptSyntax
&& IsTypeCharacter(sc
.ch
)) {
92 sc
.Forward(); // Skip it
99 sc
.GetCurrentLowered(s
, sizeof(s
));
101 s
[strlen(s
) - 1] = '\0';
103 if (strcmp(s
, "rem") == 0) {
104 sc
.ChangeState(SCE_B_COMMENT
);
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
);
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
);
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
128 if (sc
.chNext
== '\"') {
131 if (tolower(sc
.chNext
) == 'c') {
134 sc
.ForwardSetState(SCE_B_DEFAULT
);
136 } else if (sc
.atLineEnd
) {
138 sc
.ChangeState(SCE_B_STRINGEOL
);
139 sc
.ForwardSetState(SCE_B_DEFAULT
);
141 } else if (sc
.state
== SCE_B_COMMENT
) {
144 sc
.ForwardSetState(SCE_B_DEFAULT
);
146 } else if (sc
.state
== SCE_B_PREPROCESSOR
) {
149 sc
.ForwardSetState(SCE_B_DEFAULT
);
151 } else if (sc
.state
== SCE_B_FILENUMBER
) {
152 if (IsADigit(sc
.ch
)) {
154 if (fileNbDigits
> 3) {
155 sc
.ChangeState(SCE_B_DATE
);
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
);
167 sc
.ChangeState(SCE_B_DATE
);
169 if (sc
.state
!= SCE_B_FILENUMBER
) {
172 } else if (sc
.state
== SCE_B_DATE
) {
175 sc
.ChangeState(SCE_B_STRINGEOL
);
176 sc
.ForwardSetState(SCE_B_DEFAULT
);
177 } else if (sc
.ch
== '#') {
178 sc
.ForwardSetState(SCE_B_DEFAULT
);
182 if (sc
.state
== SCE_B_DEFAULT
) {
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
);
200 } else if (sc
.ch
== '&' && tolower(sc
.chNext
) == 'o') {
202 sc
.SetState(SCE_B_NUMBER
);
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
);
216 if (!IsASpace(sc
.ch
)) {
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
225 bool skipType
= false;
226 if (!vbScriptSyntax
&& IsTypeCharacter(sc
.ch
)) {
227 sc
.Forward(); // Skip it
234 sc
.GetCurrentLowered(s
, sizeof(s
));
236 s
[strlen(s
) - 1] = '\0';
238 if (strcmp(s
, "rem") == 0) {
239 sc
.ChangeState(SCE_B_COMMENT
);
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
);
257 static void FoldVBDoc(unsigned int startPos
, int length
, int,
258 WordList
*[], Accessor
&styler
) {
259 int endPos
= startPos
+ length
;
261 // Backtrack to previous line in case need to fix its fold status
262 int lineCurrent
= styler
.GetLine(startPos
);
264 if (lineCurrent
> 0) {
266 startPos
= styler
.LineStart(lineCurrent
);
270 int indentCurrent
= styler
.IndentAmount(lineCurrent
, &spaceFlags
, IsVBComment
);
271 char chNext
= styler
[startPos
];
272 for (int i
= startPos
; i
< endPos
; i
++) {
274 chNext
= styler
.SafeGetCharAt(i
+ 1);
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?
286 int indentNext2
= styler
.IndentAmount(lineCurrent
+ 2, &spaceFlags2
, IsVBComment
);
287 if ((indentCurrent
& SC_FOLDLEVELNUMBERMASK
) < (indentNext2
& SC_FOLDLEVELNUMBERMASK
)) {
288 lev
|= SC_FOLDLEVELHEADERFLAG
;
292 indentCurrent
= indentNext
;
293 styler
.SetLevel(lineCurrent
, lev
);
299 static void ColouriseVBNetDoc(unsigned int startPos
, int length
, int initStyle
,
300 WordList
*keywordlists
[], Accessor
&styler
) {
301 ColouriseVBDoc(startPos
, length
, initStyle
, keywordlists
, styler
, false);
304 static void ColouriseVBScriptDoc(unsigned int startPos
, int length
, int initStyle
,
305 WordList
*keywordlists
[], Accessor
&styler
) {
306 ColouriseVBDoc(startPos
, length
, initStyle
, keywordlists
, styler
, true);
309 static const char * const vbWordListDesc
[] = {
317 LexerModule
lmVB(SCLEX_VB
, ColouriseVBNetDoc
, "vb", FoldVBDoc
, vbWordListDesc
);
318 LexerModule
lmVBScript(SCLEX_VBSCRIPT
, ColouriseVBScriptDoc
, "vbscript", FoldVBDoc
, vbWordListDesc
);