]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/stc/scintilla/src/LexVB.cxx
Updated Watcom/OS2 makefiles.
[wxWidgets.git] / contrib / src / stc / scintilla / src / LexVB.cxx
CommitLineData
65ec6247
RD
1// Scintilla source code edit control
2/** @file LexVB.cxx
3 ** Lexer for Visual Basic and VBScript.
4 **/
1e9bafca 5// Copyright 1998-2005 by Neil Hodgson <neilh@scintilla.org>
f6bcfd97
BP
6// The License.txt file describes the conditions under which this software may be distributed.
7
65ec6247
RD
8#include <stdlib.h>
9#include <string.h>
10#include <ctype.h>
11#include <stdio.h>
12#include <stdarg.h>
f6bcfd97
BP
13
14#include "Platform.h"
15
16#include "PropSet.h"
17#include "Accessor.h"
1a2fb4cd 18#include "StyleContext.h"
f6bcfd97
BP
19#include "KeyWords.h"
20#include "Scintilla.h"
21#include "SciLexer.h"
22
1e9bafca
RD
23// Internal state, highlighted as number
24#define SCE_B_FILENUMBER SCE_B_DEFAULT+100
25
26
1a2fb4cd 27static bool IsVBComment(Accessor &styler, int pos, int len) {
1e9bafca 28 return len > 0 && styler[pos] == '\'';
1a2fb4cd 29}
f6bcfd97 30
8e54aaed 31static inline bool IsTypeCharacter(int ch) {
1a2fb4cd 32 return ch == '%' || ch == '&' || ch == '@' || ch == '!' || ch == '#' || ch == '$';
f6bcfd97
BP
33}
34
8e54aaed
RD
35// Extended to accept accented characters
36static inline bool IsAWordChar(int ch) {
37 return ch >= 0x80 ||
38 (isalnum(ch) || ch == '.' || ch == '_');
1a2fb4cd
RD
39}
40
8e54aaed
RD
41static inline bool IsAWordStart(int ch) {
42 return ch >= 0x80 ||
1e9bafca 43 (isalpha(ch) || ch == '_');
1a2fb4cd
RD
44}
45
1e9bafca
RD
46static 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.
9e730a78 49 return (ch < 0x80) &&
1e9bafca
RD
50 (isdigit(ch) || toupper(ch) == 'E' ||
51 ch == '.' || ch == '-' || ch == '+');
65ec6247
RD
52}
53
f6bcfd97 54static void ColouriseVBDoc(unsigned int startPos, int length, int initStyle,
1a2fb4cd 55 WordList *keywordlists[], Accessor &styler, bool vbScriptSyntax) {
f6bcfd97
BP
56
57 WordList &keywords = *keywordlists[0];
8e54aaed
RD
58 WordList &keywords2 = *keywordlists[1];
59 WordList &keywords3 = *keywordlists[2];
60 WordList &keywords4 = *keywordlists[3];
65ec6247 61
f6bcfd97
BP
62 styler.StartAt(startPos);
63
d134f170 64 int visibleChars = 0;
1e9bafca
RD
65 int fileNbDigits = 0;
66
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;
70 }
9e730a78 71
1a2fb4cd
RD
72 StyleContext sc(startPos, length, initStyle, styler);
73
74 for (; sc.More(); sc.Forward()) {
75
76 if (sc.state == SCE_B_OPERATOR) {
77 sc.SetState(SCE_B_DEFAULT);
8e54aaed 78 } else if (sc.state == SCE_B_IDENTIFIER) {
1a2fb4cd 79 if (!IsAWordChar(sc.ch)) {
8e54aaed
RD
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
82 // held or returned.
83 bool skipType = false;
84 if (!vbScriptSyntax && IsTypeCharacter(sc.ch)) {
85 sc.Forward(); // Skip it
86 skipType = true;
87 }
88 if (sc.ch == ']') {
89 sc.Forward();
90 }
91 char s[100];
92 sc.GetCurrentLowered(s, sizeof(s));
93 if (skipType) {
94 s[strlen(s) - 1] = '\0';
95 }
96 if (strcmp(s, "rem") == 0) {
97 sc.ChangeState(SCE_B_COMMENT);
98 } else {
1a2fb4cd 99 if (keywords.InList(s)) {
8e54aaed
RD
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);
f6bcfd97
BP
109 }
110 }
1a2fb4cd 111 } else if (sc.state == SCE_B_NUMBER) {
1e9bafca
RD
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')) {
1a2fb4cd
RD
115 sc.SetState(SCE_B_DEFAULT);
116 }
117 } else if (sc.state == SCE_B_STRING) {
9e730a78 118 // VB doubles quotes to preserve them, so just end this string
1a2fb4cd
RD
119 // state now as a following quote will start again
120 if (sc.ch == '\"') {
121 if (tolower(sc.chNext) == 'c') {
122 sc.Forward();
f6bcfd97 123 }
1a2fb4cd 124 sc.ForwardSetState(SCE_B_DEFAULT);
8e54aaed
RD
125 } else if (sc.atLineEnd) {
126 sc.ChangeState(SCE_B_STRINGEOL);
127 sc.ForwardSetState(SCE_B_DEFAULT);
1a2fb4cd
RD
128 }
129 } else if (sc.state == SCE_B_COMMENT) {
130 if (sc.atLineEnd) {
1e9bafca 131 sc.ForwardSetState(SCE_B_DEFAULT);
1a2fb4cd
RD
132 }
133 } else if (sc.state == SCE_B_PREPROCESSOR) {
134 if (sc.atLineEnd) {
1e9bafca
RD
135 sc.ForwardSetState(SCE_B_DEFAULT);
136 }
137 } else if (sc.state == SCE_B_FILENUMBER) {
138 if (IsADigit(sc.ch)) {
139 fileNbDigits++;
140 if (fileNbDigits > 3) {
141 sc.ChangeState(SCE_B_DATE);
142 }
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);
1a2fb4cd 148 sc.SetState(SCE_B_DEFAULT);
1e9bafca
RD
149 } else if (sc.ch == '#') {
150 sc.ChangeState(SCE_B_DATE);
151 sc.ForwardSetState(SCE_B_DEFAULT);
152 } else {
153 sc.ChangeState(SCE_B_DATE);
154 }
155 if (sc.state != SCE_B_FILENUMBER) {
156 fileNbDigits = 0;
f6bcfd97 157 }
1a2fb4cd 158 } else if (sc.state == SCE_B_DATE) {
1e9bafca
RD
159 if (sc.atLineEnd) {
160 sc.ChangeState(SCE_B_STRINGEOL);
161 sc.ForwardSetState(SCE_B_DEFAULT);
162 } else if (sc.ch == '#') {
1a2fb4cd
RD
163 sc.ForwardSetState(SCE_B_DEFAULT);
164 }
165 }
9e730a78 166
1a2fb4cd
RD
167 if (sc.state == SCE_B_DEFAULT) {
168 if (sc.ch == '\'') {
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 == '#') {
1e9bafca
RD
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);
1a2fb4cd 181 } else if (sc.ch == '&' && tolower(sc.chNext) == 'h') {
1e9bafca 182 // Hexadecimal number
1a2fb4cd 183 sc.SetState(SCE_B_NUMBER);
1e9bafca 184 sc.Forward();
1a2fb4cd 185 } else if (sc.ch == '&' && tolower(sc.chNext) == 'o') {
1e9bafca 186 // Octal number
1a2fb4cd 187 sc.SetState(SCE_B_NUMBER);
1e9bafca 188 sc.Forward();
1a2fb4cd
RD
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 == '[')) {
8e54aaed 192 sc.SetState(SCE_B_IDENTIFIER);
1e9bafca 193 } else if (isoperator(static_cast<char>(sc.ch)) || (sc.ch == '\\')) { // Integer division
1a2fb4cd 194 sc.SetState(SCE_B_OPERATOR);
f6bcfd97
BP
195 }
196 }
9e730a78 197
1a2fb4cd
RD
198 if (sc.atLineEnd) {
199 visibleChars = 0;
200 }
201 if (!IsASpace(sc.ch)) {
202 visibleChars++;
203 }
f6bcfd97 204 }
1a2fb4cd 205 sc.Complete();
f6bcfd97
BP
206}
207
1a2fb4cd 208static void FoldVBDoc(unsigned int startPos, int length, int,
65ec6247 209 WordList *[], Accessor &styler) {
1a2fb4cd 210 int endPos = startPos + length;
65ec6247
RD
211
212 // Backtrack to previous line in case need to fix its fold status
213 int lineCurrent = styler.GetLine(startPos);
214 if (startPos > 0) {
215 if (lineCurrent > 0) {
216 lineCurrent--;
217 startPos = styler.LineStart(lineCurrent);
65ec6247
RD
218 }
219 }
65ec6247
RD
220 int spaceFlags = 0;
221 int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, IsVBComment);
65ec6247 222 char chNext = styler[startPos];
1a2fb4cd 223 for (int i = startPos; i < endPos; i++) {
65ec6247
RD
224 char ch = chNext;
225 chNext = styler.SafeGetCharAt(i + 1);
65ec6247 226
1a2fb4cd 227 if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == endPos)) {
65ec6247
RD
228 int lev = indentCurrent;
229 int indentNext = styler.IndentAmount(lineCurrent + 1, &spaceFlags, IsVBComment);
65ec6247
RD
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?
236 int spaceFlags2 = 0;
237 int indentNext2 = styler.IndentAmount(lineCurrent + 2, &spaceFlags2, IsVBComment);
238 if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext2 & SC_FOLDLEVELNUMBERMASK)) {
239 lev |= SC_FOLDLEVELHEADERFLAG;
240 }
241 }
242 }
243 indentCurrent = indentNext;
244 styler.SetLevel(lineCurrent, lev);
245 lineCurrent++;
246 }
247 }
248}
249
1a2fb4cd
RD
250static void ColouriseVBNetDoc(unsigned int startPos, int length, int initStyle,
251 WordList *keywordlists[], Accessor &styler) {
252 ColouriseVBDoc(startPos, length, initStyle, keywordlists, styler, false);
253}
254
255static void ColouriseVBScriptDoc(unsigned int startPos, int length, int initStyle,
256 WordList *keywordlists[], Accessor &styler) {
257 ColouriseVBDoc(startPos, length, initStyle, keywordlists, styler, true);
258}
259
9e730a78
RD
260static const char * const vbWordListDesc[] = {
261 "Keywords",
8e54aaed
RD
262 "user1",
263 "user2",
264 "user3",
9e730a78
RD
265 0
266};
267
268LexerModule lmVB(SCLEX_VB, ColouriseVBNetDoc, "vb", FoldVBDoc, vbWordListDesc);
269LexerModule lmVBScript(SCLEX_VBSCRIPT, ColouriseVBScriptDoc, "vbscript", FoldVBDoc, vbWordListDesc);
1a2fb4cd 270