]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/LexVB.cxx
Cast to void* before casting to Node** to make the compiler do no strict-aliasing...
[wxWidgets.git] / contrib / src / stc / scintilla / src / LexVB.cxx
1 // Scintilla source code edit control
2 /** @file LexVB.cxx
3 ** Lexer for Visual Basic and VBScript.
4 **/
5 // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13
14 #include "Platform.h"
15
16 #include "PropSet.h"
17 #include "Accessor.h"
18 #include "StyleContext.h"
19 #include "KeyWords.h"
20 #include "Scintilla.h"
21 #include "SciLexer.h"
22
23 static bool IsVBComment(Accessor &styler, int pos, int len) {
24 return len>0 && styler[pos]=='\'';
25 }
26
27 static inline bool IsTypeCharacter(int ch) {
28 return ch == '%' || ch == '&' || ch == '@' || ch == '!' || ch == '#' || ch == '$';
29 }
30
31 // Extended to accept accented characters
32 static inline bool IsAWordChar(int ch) {
33 return ch >= 0x80 ||
34 (isalnum(ch) || ch == '.' || ch == '_');
35 }
36
37 static inline bool IsAWordStart(int ch) {
38 return ch >= 0x80 ||
39 (isalnum(ch) || ch == '_');
40 }
41
42 static inline bool IsADateCharacter(const int ch) {
43 return (ch < 0x80) &&
44 (isalnum(ch) || ch == '|' || ch == '-' || ch == '/' || ch == ':' || ch == ' ' || ch == '\t');
45 }
46
47 static void ColouriseVBDoc(unsigned int startPos, int length, int initStyle,
48 WordList *keywordlists[], Accessor &styler, bool vbScriptSyntax) {
49
50 WordList &keywords = *keywordlists[0];
51 WordList &keywords2 = *keywordlists[1];
52 WordList &keywords3 = *keywordlists[2];
53 WordList &keywords4 = *keywordlists[3];
54
55 styler.StartAt(startPos);
56
57 int visibleChars = 0;
58
59 StyleContext sc(startPos, length, initStyle, styler);
60
61 for (; sc.More(); sc.Forward()) {
62
63 if (sc.state == SCE_B_OPERATOR) {
64 sc.SetState(SCE_B_DEFAULT);
65 } else if (sc.state == SCE_B_IDENTIFIER) {
66 if (!IsAWordChar(sc.ch)) {
67 // In Basic (except VBScript), a variable name or a function name
68 // can end with a special character indicating the type of the value
69 // held or returned.
70 bool skipType = false;
71 if (!vbScriptSyntax && IsTypeCharacter(sc.ch)) {
72 sc.Forward(); // Skip it
73 skipType = true;
74 }
75 if (sc.ch == ']') {
76 sc.Forward();
77 }
78 char s[100];
79 sc.GetCurrentLowered(s, sizeof(s));
80 if (skipType) {
81 s[strlen(s) - 1] = '\0';
82 }
83 if (strcmp(s, "rem") == 0) {
84 sc.ChangeState(SCE_B_COMMENT);
85 } else {
86 if (keywords.InList(s)) {
87 sc.ChangeState(SCE_B_KEYWORD);
88 } else if (keywords2.InList(s)) {
89 sc.ChangeState(SCE_B_KEYWORD2);
90 } else if (keywords3.InList(s)) {
91 sc.ChangeState(SCE_B_KEYWORD3);
92 } else if (keywords4.InList(s)) {
93 sc.ChangeState(SCE_B_KEYWORD4);
94 } // Else, it is really an identifier...
95 sc.SetState(SCE_B_DEFAULT);
96 }
97 }
98 } else if (sc.state == SCE_B_NUMBER) {
99 if (!IsAWordChar(sc.ch)) {
100 sc.SetState(SCE_B_DEFAULT);
101 }
102 } else if (sc.state == SCE_B_STRING) {
103 // VB doubles quotes to preserve them, so just end this string
104 // state now as a following quote will start again
105 if (sc.ch == '\"') {
106 if (tolower(sc.chNext) == 'c') {
107 sc.Forward();
108 }
109 sc.ForwardSetState(SCE_B_DEFAULT);
110 } else if (sc.atLineEnd) {
111 sc.ChangeState(SCE_B_STRINGEOL);
112 sc.ForwardSetState(SCE_B_DEFAULT);
113 }
114 } else if (sc.state == SCE_B_COMMENT) {
115 if (sc.atLineEnd) {
116 sc.SetState(SCE_B_DEFAULT);
117 }
118 } else if (sc.state == SCE_B_PREPROCESSOR) {
119 if (sc.atLineEnd) {
120 sc.SetState(SCE_B_DEFAULT);
121 }
122 } else if (sc.state == SCE_B_DATE) {
123 if (sc.ch == '#' || !IsADateCharacter(sc.chNext)) {
124 sc.ForwardSetState(SCE_B_DEFAULT);
125 }
126 }
127
128 if (sc.state == SCE_B_DEFAULT) {
129 if (sc.ch == '\'') {
130 sc.SetState(SCE_B_COMMENT);
131 } else if (sc.ch == '\"') {
132 sc.SetState(SCE_B_STRING);
133 } else if (sc.ch == '#' && visibleChars == 0) {
134 // Preprocessor commands are alone on their line
135 sc.SetState(SCE_B_PREPROCESSOR);
136 } else if (sc.ch == '#') {
137 int n = 1;
138 int chSeek = ' ';
139 while ((n < 100) && (chSeek == ' ' || chSeek == '\t')) {
140 chSeek = sc.GetRelative(n);
141 n++;
142 }
143 if (IsADigit(chSeek)) {
144 sc.SetState(SCE_B_DATE);
145 } else {
146 sc.SetState(SCE_B_OPERATOR);
147 }
148 } else if (sc.ch == '&' && tolower(sc.chNext) == 'h') {
149 sc.SetState(SCE_B_NUMBER);
150 } else if (sc.ch == '&' && tolower(sc.chNext) == 'o') {
151 sc.SetState(SCE_B_NUMBER);
152 } else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
153 sc.SetState(SCE_B_NUMBER);
154 } else if (IsAWordStart(sc.ch) || (sc.ch == '[')) {
155 sc.SetState(SCE_B_IDENTIFIER);
156 } else if (isoperator(static_cast<char>(sc.ch)) || (sc.ch == '\\')) {
157 sc.SetState(SCE_B_OPERATOR);
158 }
159 }
160
161 if (sc.atLineEnd) {
162 visibleChars = 0;
163 }
164 if (!IsASpace(sc.ch)) {
165 visibleChars++;
166 }
167 }
168 sc.Complete();
169 }
170
171 static void FoldVBDoc(unsigned int startPos, int length, int,
172 WordList *[], Accessor &styler) {
173 int endPos = startPos + length;
174
175 // Backtrack to previous line in case need to fix its fold status
176 int lineCurrent = styler.GetLine(startPos);
177 if (startPos > 0) {
178 if (lineCurrent > 0) {
179 lineCurrent--;
180 startPos = styler.LineStart(lineCurrent);
181 }
182 }
183 int spaceFlags = 0;
184 int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, IsVBComment);
185 char chNext = styler[startPos];
186 for (int i = startPos; i < endPos; i++) {
187 char ch = chNext;
188 chNext = styler.SafeGetCharAt(i + 1);
189
190 if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == endPos)) {
191 int lev = indentCurrent;
192 int indentNext = styler.IndentAmount(lineCurrent + 1, &spaceFlags, IsVBComment);
193 if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG)) {
194 // Only non whitespace lines can be headers
195 if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK)) {
196 lev |= SC_FOLDLEVELHEADERFLAG;
197 } else if (indentNext & SC_FOLDLEVELWHITEFLAG) {
198 // Line after is blank so check the next - maybe should continue further?
199 int spaceFlags2 = 0;
200 int indentNext2 = styler.IndentAmount(lineCurrent + 2, &spaceFlags2, IsVBComment);
201 if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext2 & SC_FOLDLEVELNUMBERMASK)) {
202 lev |= SC_FOLDLEVELHEADERFLAG;
203 }
204 }
205 }
206 indentCurrent = indentNext;
207 styler.SetLevel(lineCurrent, lev);
208 lineCurrent++;
209 }
210 }
211 }
212
213 static void ColouriseVBNetDoc(unsigned int startPos, int length, int initStyle,
214 WordList *keywordlists[], Accessor &styler) {
215 ColouriseVBDoc(startPos, length, initStyle, keywordlists, styler, false);
216 }
217
218 static void ColouriseVBScriptDoc(unsigned int startPos, int length, int initStyle,
219 WordList *keywordlists[], Accessor &styler) {
220 ColouriseVBDoc(startPos, length, initStyle, keywordlists, styler, true);
221 }
222
223 static const char * const vbWordListDesc[] = {
224 "Keywords",
225 "user1",
226 "user2",
227 "user3",
228 0
229 };
230
231 LexerModule lmVB(SCLEX_VB, ColouriseVBNetDoc, "vb", FoldVBDoc, vbWordListDesc);
232 LexerModule lmVBScript(SCLEX_VBSCRIPT, ColouriseVBScriptDoc, "vbscript", FoldVBDoc, vbWordListDesc);
233