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.
18 #include "StyleContext.h"
20 #include "Scintilla.h"
24 using namespace Scintilla
;
27 // Internal state, highlighted as number
28 #define SCE_B_FILENUMBER SCE_B_DEFAULT+100
31 static bool IsVBComment(Accessor
&styler
, int pos
, int len
) {
32 return len
> 0 && styler
[pos
] == '\'';
35 static inline bool IsTypeCharacter(int ch
) {
36 return ch
== '%' || ch
== '&' || ch
== '@' || ch
== '!' || ch
== '#' || ch
== '$';
39 // Extended to accept accented characters
40 static inline bool IsAWordChar(int ch
) {
42 (isalnum(ch
) || ch
== '.' || ch
== '_');
45 static inline bool IsAWordStart(int ch
) {
47 (isalpha(ch
) || ch
== '_');
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.
54 (isdigit(ch
) || toupper(ch
) == 'E' ||
55 ch
== '.' || ch
== '-' || ch
== '+');
58 static void ColouriseVBDoc(unsigned int startPos
, int length
, int initStyle
,
59 WordList
*keywordlists
[], Accessor
&styler
, bool vbScriptSyntax
) {
61 WordList
&keywords
= *keywordlists
[0];
62 WordList
&keywords2
= *keywordlists
[1];
63 WordList
&keywords3
= *keywordlists
[2];
64 WordList
&keywords4
= *keywordlists
[3];
66 styler
.StartAt(startPos
);
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
;
76 StyleContext
sc(startPos
, length
, initStyle
, styler
);
78 for (; sc
.More(); sc
.Forward()) {
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
87 bool skipType
= false;
88 if (!vbScriptSyntax
&& IsTypeCharacter(sc
.ch
)) {
89 sc
.Forward(); // Skip it
96 sc
.GetCurrentLowered(s
, sizeof(s
));
98 s
[strlen(s
) - 1] = '\0';
100 if (strcmp(s
, "rem") == 0) {
101 sc
.ChangeState(SCE_B_COMMENT
);
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
);
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
);
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
125 if (sc
.chNext
== '\"') {
128 if (tolower(sc
.chNext
) == 'c') {
131 sc
.ForwardSetState(SCE_B_DEFAULT
);
133 } else if (sc
.atLineEnd
) {
135 sc
.ChangeState(SCE_B_STRINGEOL
);
136 sc
.ForwardSetState(SCE_B_DEFAULT
);
138 } else if (sc
.state
== SCE_B_COMMENT
) {
141 sc
.ForwardSetState(SCE_B_DEFAULT
);
143 } else if (sc
.state
== SCE_B_PREPROCESSOR
) {
146 sc
.ForwardSetState(SCE_B_DEFAULT
);
148 } else if (sc
.state
== SCE_B_FILENUMBER
) {
149 if (IsADigit(sc
.ch
)) {
151 if (fileNbDigits
> 3) {
152 sc
.ChangeState(SCE_B_DATE
);
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
);
164 sc
.ChangeState(SCE_B_DATE
);
166 if (sc
.state
!= SCE_B_FILENUMBER
) {
169 } else if (sc
.state
== SCE_B_DATE
) {
172 sc
.ChangeState(SCE_B_STRINGEOL
);
173 sc
.ForwardSetState(SCE_B_DEFAULT
);
174 } else if (sc
.ch
== '#') {
175 sc
.ForwardSetState(SCE_B_DEFAULT
);
179 if (sc
.state
== SCE_B_DEFAULT
) {
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
);
197 } else if (sc
.ch
== '&' && tolower(sc
.chNext
) == 'o') {
199 sc
.SetState(SCE_B_NUMBER
);
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
);
213 if (!IsASpace(sc
.ch
)) {
220 static void FoldVBDoc(unsigned int startPos
, int length
, int,
221 WordList
*[], Accessor
&styler
) {
222 int endPos
= startPos
+ length
;
224 // Backtrack to previous line in case need to fix its fold status
225 int lineCurrent
= styler
.GetLine(startPos
);
227 if (lineCurrent
> 0) {
229 startPos
= styler
.LineStart(lineCurrent
);
233 int indentCurrent
= styler
.IndentAmount(lineCurrent
, &spaceFlags
, IsVBComment
);
234 char chNext
= styler
[startPos
];
235 for (int i
= startPos
; i
< endPos
; i
++) {
237 chNext
= styler
.SafeGetCharAt(i
+ 1);
239 if ((ch
== '\r' && chNext
!= '\n') || (ch
== '\n') || (i
== endPos
)) {
240 int lev
= indentCurrent
;
241 int indentNext
= styler
.IndentAmount(lineCurrent
+ 1, &spaceFlags
, IsVBComment
);
242 if (!(indentCurrent
& SC_FOLDLEVELWHITEFLAG
)) {
243 // Only non whitespace lines can be headers
244 if ((indentCurrent
& SC_FOLDLEVELNUMBERMASK
) < (indentNext
& SC_FOLDLEVELNUMBERMASK
)) {
245 lev
|= SC_FOLDLEVELHEADERFLAG
;
246 } else if (indentNext
& SC_FOLDLEVELWHITEFLAG
) {
247 // Line after is blank so check the next - maybe should continue further?
249 int indentNext2
= styler
.IndentAmount(lineCurrent
+ 2, &spaceFlags2
, IsVBComment
);
250 if ((indentCurrent
& SC_FOLDLEVELNUMBERMASK
) < (indentNext2
& SC_FOLDLEVELNUMBERMASK
)) {
251 lev
|= SC_FOLDLEVELHEADERFLAG
;
255 indentCurrent
= indentNext
;
256 styler
.SetLevel(lineCurrent
, lev
);
262 static void ColouriseVBNetDoc(unsigned int startPos
, int length
, int initStyle
,
263 WordList
*keywordlists
[], Accessor
&styler
) {
264 ColouriseVBDoc(startPos
, length
, initStyle
, keywordlists
, styler
, false);
267 static void ColouriseVBScriptDoc(unsigned int startPos
, int length
, int initStyle
,
268 WordList
*keywordlists
[], Accessor
&styler
) {
269 ColouriseVBDoc(startPos
, length
, initStyle
, keywordlists
, styler
, true);
272 static const char * const vbWordListDesc
[] = {
280 LexerModule
lmVB(SCLEX_VB
, ColouriseVBNetDoc
, "vb", FoldVBDoc
, vbWordListDesc
);
281 LexerModule
lmVBScript(SCLEX_VBSCRIPT
, ColouriseVBScriptDoc
, "vbscript", FoldVBDoc
, vbWordListDesc
);