Add an UI to enable/disable specific overlay handlers.
[TortoiseGit.git] / ext / scintilla / src / LexVB.cxx
blob58335a469d92835efce99ef01393834e029fb55b
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.
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <stdarg.h>
14 #include "Platform.h"
16 #include "PropSet.h"
17 #include "Accessor.h"
18 #include "StyleContext.h"
19 #include "KeyWords.h"
20 #include "Scintilla.h"
21 #include "SciLexer.h"
23 #ifdef SCI_NAMESPACE
24 using namespace Scintilla;
25 #endif
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) {
41 return ch >= 0x80 ||
42 (isalnum(ch) || ch == '.' || ch == '_');
45 static inline bool IsAWordStart(int ch) {
46 return ch >= 0x80 ||
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.
53 return (ch < 0x80) &&
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);
68 int visibleChars = 0;
69 int fileNbDigits = 0;
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
86 // held or returned.
87 bool skipType = false;
88 if (!vbScriptSyntax && IsTypeCharacter(sc.ch)) {
89 sc.Forward(); // Skip it
90 skipType = true;
92 if (sc.ch == ']') {
93 sc.Forward();
95 char s[100];
96 sc.GetCurrentLowered(s, sizeof(s));
97 if (skipType) {
98 s[strlen(s) - 1] = '\0';
100 if (strcmp(s, "rem") == 0) {
101 sc.ChangeState(SCE_B_COMMENT);
102 } else {
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
124 if (sc.ch == '\"') {
125 if (sc.chNext == '\"') {
126 sc.Forward();
127 } else {
128 if (tolower(sc.chNext) == 'c') {
129 sc.Forward();
131 sc.ForwardSetState(SCE_B_DEFAULT);
133 } else if (sc.atLineEnd) {
134 visibleChars = 0;
135 sc.ChangeState(SCE_B_STRINGEOL);
136 sc.ForwardSetState(SCE_B_DEFAULT);
138 } else if (sc.state == SCE_B_COMMENT) {
139 if (sc.atLineEnd) {
140 visibleChars = 0;
141 sc.ForwardSetState(SCE_B_DEFAULT);
143 } else if (sc.state == SCE_B_PREPROCESSOR) {
144 if (sc.atLineEnd) {
145 visibleChars = 0;
146 sc.ForwardSetState(SCE_B_DEFAULT);
148 } else if (sc.state == SCE_B_FILENUMBER) {
149 if (IsADigit(sc.ch)) {
150 fileNbDigits++;
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);
163 } else {
164 sc.ChangeState(SCE_B_DATE);
166 if (sc.state != SCE_B_FILENUMBER) {
167 fileNbDigits = 0;
169 } else if (sc.state == SCE_B_DATE) {
170 if (sc.atLineEnd) {
171 visibleChars = 0;
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) {
180 if (sc.ch == '\'') {
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);
196 sc.Forward();
197 } else if (sc.ch == '&' && tolower(sc.chNext) == 'o') {
198 // Octal number
199 sc.SetState(SCE_B_NUMBER);
200 sc.Forward();
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);
210 if (sc.atLineEnd) {
211 visibleChars = 0;
213 if (!IsASpace(sc.ch)) {
214 visibleChars++;
217 sc.Complete();
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);
226 if (startPos > 0) {
227 if (lineCurrent > 0) {
228 lineCurrent--;
229 startPos = styler.LineStart(lineCurrent);
232 int spaceFlags = 0;
233 int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, IsVBComment);
234 char chNext = styler[startPos];
235 for (int i = startPos; i < endPos; i++) {
236 char ch = chNext;
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?
248 int spaceFlags2 = 0;
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);
257 lineCurrent++;
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[] = {
273 "Keywords",
274 "user1",
275 "user2",
276 "user3",
280 LexerModule lmVB(SCLEX_VB, ColouriseVBNetDoc, "vb", FoldVBDoc, vbWordListDesc);
281 LexerModule lmVBScript(SCLEX_VBSCRIPT, ColouriseVBScriptDoc, "vbscript", FoldVBDoc, vbWordListDesc);