]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/LexVB.cxx
1 // Scintilla source code edit control
3 ** Lexer for Visual Basic and VBScript.
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.
18 #include "StyleContext.h"
20 #include "Scintilla.h"
23 static bool IsVBComment(Accessor
&styler
, int pos
, int len
) {
24 return len
>0 && styler
[pos
]=='\'';
27 static inline bool IsTypeCharacter(int ch
) {
28 return ch
== '%' || ch
== '&' || ch
== '@' || ch
== '!' || ch
== '#' || ch
== '$';
31 // Extended to accept accented characters
32 static inline bool IsAWordChar(int ch
) {
34 (isalnum(ch
) || ch
== '.' || ch
== '_');
37 static inline bool IsAWordStart(int ch
) {
39 (isalnum(ch
) || ch
== '_');
42 static inline bool IsADateCharacter(const int ch
) {
44 (isalnum(ch
) || ch
== '|' || ch
== '-' || ch
== '/' || ch
== ':' || ch
== ' ' || ch
== '\t');
47 static void ColouriseVBDoc(unsigned int startPos
, int length
, int initStyle
,
48 WordList
*keywordlists
[], Accessor
&styler
, bool vbScriptSyntax
) {
50 WordList
&keywords
= *keywordlists
[0];
51 WordList
&keywords2
= *keywordlists
[1];
52 WordList
&keywords3
= *keywordlists
[2];
53 WordList
&keywords4
= *keywordlists
[3];
55 styler
.StartAt(startPos
);
59 StyleContext
sc(startPos
, length
, initStyle
, styler
);
61 for (; sc
.More(); sc
.Forward()) {
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
70 bool skipType
= false;
71 if (!vbScriptSyntax
&& IsTypeCharacter(sc
.ch
)) {
72 sc
.Forward(); // Skip it
79 sc
.GetCurrentLowered(s
, sizeof(s
));
81 s
[strlen(s
) - 1] = '\0';
83 if (strcmp(s
, "rem") == 0) {
84 sc
.ChangeState(SCE_B_COMMENT
);
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
);
98 } else if (sc
.state
== SCE_B_NUMBER
) {
99 if (!IsAWordChar(sc
.ch
)) {
100 sc
.SetState(SCE_B_DEFAULT
);
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
106 if (tolower(sc
.chNext
) == 'c') {
109 sc
.ForwardSetState(SCE_B_DEFAULT
);
110 } else if (sc
.atLineEnd
) {
111 sc
.ChangeState(SCE_B_STRINGEOL
);
112 sc
.ForwardSetState(SCE_B_DEFAULT
);
114 } else if (sc
.state
== SCE_B_COMMENT
) {
116 sc
.SetState(SCE_B_DEFAULT
);
118 } else if (sc
.state
== SCE_B_PREPROCESSOR
) {
120 sc
.SetState(SCE_B_DEFAULT
);
122 } else if (sc
.state
== SCE_B_DATE
) {
123 if (sc
.ch
== '#' || !IsADateCharacter(sc
.chNext
)) {
124 sc
.ForwardSetState(SCE_B_DEFAULT
);
128 if (sc
.state
== SCE_B_DEFAULT
) {
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
== '#') {
139 while ((n
< 100) && (chSeek
== ' ' || chSeek
== '\t')) {
140 chSeek
= sc
.GetRelative(n
);
143 if (IsADigit(chSeek
)) {
144 sc
.SetState(SCE_B_DATE
);
146 sc
.SetState(SCE_B_OPERATOR
);
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
);
164 if (!IsASpace(sc
.ch
)) {
171 static void FoldVBDoc(unsigned int startPos
, int length
, int,
172 WordList
*[], Accessor
&styler
) {
173 int endPos
= startPos
+ length
;
175 // Backtrack to previous line in case need to fix its fold status
176 int lineCurrent
= styler
.GetLine(startPos
);
178 if (lineCurrent
> 0) {
180 startPos
= styler
.LineStart(lineCurrent
);
184 int indentCurrent
= styler
.IndentAmount(lineCurrent
, &spaceFlags
, IsVBComment
);
185 char chNext
= styler
[startPos
];
186 for (int i
= startPos
; i
< endPos
; i
++) {
188 chNext
= styler
.SafeGetCharAt(i
+ 1);
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?
200 int indentNext2
= styler
.IndentAmount(lineCurrent
+ 2, &spaceFlags2
, IsVBComment
);
201 if ((indentCurrent
& SC_FOLDLEVELNUMBERMASK
) < (indentNext2
& SC_FOLDLEVELNUMBERMASK
)) {
202 lev
|= SC_FOLDLEVELHEADERFLAG
;
206 indentCurrent
= indentNext
;
207 styler
.SetLevel(lineCurrent
, lev
);
213 static void ColouriseVBNetDoc(unsigned int startPos
, int length
, int initStyle
,
214 WordList
*keywordlists
[], Accessor
&styler
) {
215 ColouriseVBDoc(startPos
, length
, initStyle
, keywordlists
, styler
, false);
218 static void ColouriseVBScriptDoc(unsigned int startPos
, int length
, int initStyle
,
219 WordList
*keywordlists
[], Accessor
&styler
) {
220 ColouriseVBDoc(startPos
, length
, initStyle
, keywordlists
, styler
, true);
223 static const char * const vbWordListDesc
[] = {
231 LexerModule
lmVB(SCLEX_VB
, ColouriseVBNetDoc
, "vb", FoldVBDoc
, vbWordListDesc
);
232 LexerModule
lmVBScript(SCLEX_VBSCRIPT
, ColouriseVBScriptDoc
, "vbscript", FoldVBDoc
, vbWordListDesc
);