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"
23
// Internal state, highlighted as number
24
#define SCE_B_FILENUMBER SCE_B_DEFAULT+100
27
static bool IsVBComment(Accessor &styler, int pos, int len) {
28
return len > 0 && styler[pos] == '\'';
31
static inline bool IsTypeCharacter(int ch) {
32
return ch == '%' || ch == '&' || ch == '@' || ch == '!' || ch == '#' || ch == '$';
35
// Extended to accept accented characters
36
static inline bool IsAWordChar(int ch) {
38
(isalnum(ch) || ch == '.' || ch == '_');
41
static inline bool IsAWordStart(int ch) {
43
(isalpha(ch) || ch == '_');
46
static inline bool IsANumberChar(int ch) {
47
// Not exactly following number definition (several dots are seen as OK, etc.)
48
// but probably enough in most cases.
50
(isdigit(ch) || toupper(ch) == 'E' ||
51
ch == '.' || ch == '-' || ch == '+');
54
static void ColouriseVBDoc(unsigned int startPos, int length, int initStyle,
55
WordList *keywordlists[], Accessor &styler, bool vbScriptSyntax) {
57
WordList &keywords = *keywordlists[0];
58
WordList &keywords2 = *keywordlists[1];
59
WordList &keywords3 = *keywordlists[2];
60
WordList &keywords4 = *keywordlists[3];
62
styler.StartAt(startPos);
67
// Do not leak onto next line
68
if (initStyle == SCE_B_STRINGEOL || initStyle == SCE_B_COMMENT || initStyle == SCE_B_PREPROCESSOR) {
69
initStyle = SCE_B_DEFAULT;
72
StyleContext sc(startPos, length, initStyle, styler);
74
for (; sc.More(); sc.Forward()) {
76
if (sc.state == SCE_B_OPERATOR) {
77
sc.SetState(SCE_B_DEFAULT);
78
} else if (sc.state == SCE_B_IDENTIFIER) {
79
if (!IsAWordChar(sc.ch)) {
80
// In Basic (except VBScript), a variable name or a function name
81
// can end with a special character indicating the type of the value
83
bool skipType = false;
84
if (!vbScriptSyntax && IsTypeCharacter(sc.ch)) {
85
sc.Forward(); // Skip it
92
sc.GetCurrentLowered(s, sizeof(s));
94
s[strlen(s) - 1] = '\0';
96
if (strcmp(s, "rem") == 0) {
97
sc.ChangeState(SCE_B_COMMENT);
99
if (keywords.InList(s)) {
100
sc.ChangeState(SCE_B_KEYWORD);
101
} else if (keywords2.InList(s)) {
102
sc.ChangeState(SCE_B_KEYWORD2);
103
} else if (keywords3.InList(s)) {
104
sc.ChangeState(SCE_B_KEYWORD3);
105
} else if (keywords4.InList(s)) {
106
sc.ChangeState(SCE_B_KEYWORD4);
107
} // Else, it is really an identifier...
108
sc.SetState(SCE_B_DEFAULT);
111
} else if (sc.state == SCE_B_NUMBER) {
112
// We stop the number definition on non-numerical non-dot non-eE non-sign char
113
// Also accepts A-F for hex. numbers
114
if (!IsANumberChar(sc.ch) && !(tolower(sc.ch) >= 'a' && tolower(sc.ch) <= 'f')) {
115
sc.SetState(SCE_B_DEFAULT);
117
} else if (sc.state == SCE_B_STRING) {
118
// VB doubles quotes to preserve them, so just end this string
119
// state now as a following quote will start again
121
if (tolower(sc.chNext) == 'c') {
124
sc.ForwardSetState(SCE_B_DEFAULT);
125
} else if (sc.atLineEnd) {
126
sc.ChangeState(SCE_B_STRINGEOL);
127
sc.ForwardSetState(SCE_B_DEFAULT);
129
} else if (sc.state == SCE_B_COMMENT) {
131
sc.ForwardSetState(SCE_B_DEFAULT);
133
} else if (sc.state == SCE_B_PREPROCESSOR) {
135
sc.ForwardSetState(SCE_B_DEFAULT);
137
} else if (sc.state == SCE_B_FILENUMBER) {
138
if (IsADigit(sc.ch)) {
140
if (fileNbDigits > 3) {
141
sc.ChangeState(SCE_B_DATE);
143
} else if (sc.ch == '\r' || sc.ch == '\n' || sc.ch == ',') {
144
// Regular uses: Close #1; Put #1, ...; Get #1, ... etc.
145
// Too bad if date is format #27, Oct, 2003# or something like that...
146
// Use regular number state
147
sc.ChangeState(SCE_B_NUMBER);
148
sc.SetState(SCE_B_DEFAULT);
149
} else if (sc.ch == '#') {
150
sc.ChangeState(SCE_B_DATE);
151
sc.ForwardSetState(SCE_B_DEFAULT);
153
sc.ChangeState(SCE_B_DATE);
155
if (sc.state != SCE_B_FILENUMBER) {
158
} else if (sc.state == SCE_B_DATE) {
160
sc.ChangeState(SCE_B_STRINGEOL);
161
sc.ForwardSetState(SCE_B_DEFAULT);
162
} else if (sc.ch == '#') {
163
sc.ForwardSetState(SCE_B_DEFAULT);
167
if (sc.state == SCE_B_DEFAULT) {
169
sc.SetState(SCE_B_COMMENT);
170
} else if (sc.ch == '\"') {
171
sc.SetState(SCE_B_STRING);
172
} else if (sc.ch == '#' && visibleChars == 0) {
173
// Preprocessor commands are alone on their line
174
sc.SetState(SCE_B_PREPROCESSOR);
175
} else if (sc.ch == '#') {
176
// It can be a date literal, ending with #, or a file number, from 1 to 511
177
// The date literal depends on the locale, so anything can go between #'s.
178
// Can be #January 1, 1993# or #1 Jan 93# or #05/11/2003#, etc.
179
// So we set the FILENUMBER state, and switch to DATE if it isn't a file number
180
sc.SetState(SCE_B_FILENUMBER);
181
} else if (sc.ch == '&' && tolower(sc.chNext) == 'h') {
182
// Hexadecimal number
183
sc.SetState(SCE_B_NUMBER);
185
} else if (sc.ch == '&' && tolower(sc.chNext) == 'o') {
187
sc.SetState(SCE_B_NUMBER);
189
} else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
190
sc.SetState(SCE_B_NUMBER);
191
} else if (IsAWordStart(sc.ch) || (sc.ch == '[')) {
192
sc.SetState(SCE_B_IDENTIFIER);
193
} else if (isoperator(static_cast<char>(sc.ch)) || (sc.ch == '\\')) { // Integer division
194
sc.SetState(SCE_B_OPERATOR);
201
if (!IsASpace(sc.ch)) {
208
static void FoldVBDoc(unsigned int startPos, int length, int,
209
WordList *[], Accessor &styler) {
210
int endPos = startPos + length;
212
// Backtrack to previous line in case need to fix its fold status
213
int lineCurrent = styler.GetLine(startPos);
215
if (lineCurrent > 0) {
217
startPos = styler.LineStart(lineCurrent);
221
int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, IsVBComment);
222
char chNext = styler[startPos];
223
for (int i = startPos; i < endPos; i++) {
225
chNext = styler.SafeGetCharAt(i + 1);
227
if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == endPos)) {
228
int lev = indentCurrent;
229
int indentNext = styler.IndentAmount(lineCurrent + 1, &spaceFlags, IsVBComment);
230
if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG)) {
231
// Only non whitespace lines can be headers
232
if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK)) {
233
lev |= SC_FOLDLEVELHEADERFLAG;
234
} else if (indentNext & SC_FOLDLEVELWHITEFLAG) {
235
// Line after is blank so check the next - maybe should continue further?
237
int indentNext2 = styler.IndentAmount(lineCurrent + 2, &spaceFlags2, IsVBComment);
238
if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext2 & SC_FOLDLEVELNUMBERMASK)) {
239
lev |= SC_FOLDLEVELHEADERFLAG;
243
indentCurrent = indentNext;
244
styler.SetLevel(lineCurrent, lev);
250
static void ColouriseVBNetDoc(unsigned int startPos, int length, int initStyle,
251
WordList *keywordlists[], Accessor &styler) {
252
ColouriseVBDoc(startPos, length, initStyle, keywordlists, styler, false);
255
static void ColouriseVBScriptDoc(unsigned int startPos, int length, int initStyle,
256
WordList *keywordlists[], Accessor &styler) {
257
ColouriseVBDoc(startPos, length, initStyle, keywordlists, styler, true);
260
static const char * const vbWordListDesc[] = {
268
LexerModule lmVB(SCLEX_VB, ColouriseVBNetDoc, "vb", FoldVBDoc, vbWordListDesc);
269
LexerModule lmVBScript(SCLEX_VBSCRIPT, ColouriseVBScriptDoc, "vbscript", FoldVBDoc, vbWordListDesc);