]>
Commit | Line | Data |
---|---|---|
f6bcfd97 BP |
1 | // SciTE - Scintilla based Text Editor |
2 | // LexVB.cxx - lexer for Visual Basic and VBScript | |
3 | // Copyright 1998-2000 by Neil Hodgson <neilh@scintilla.org> | |
4 | // The License.txt file describes the conditions under which this software may be distributed. | |
5 | ||
6 | #include <stdlib.h> | |
7 | #include <string.h> | |
8 | #include <ctype.h> | |
9 | #include <stdio.h> | |
10 | #include <stdarg.h> | |
11 | ||
12 | #include "Platform.h" | |
13 | ||
14 | #include "PropSet.h" | |
15 | #include "Accessor.h" | |
16 | #include "KeyWords.h" | |
17 | #include "Scintilla.h" | |
18 | #include "SciLexer.h" | |
19 | ||
20 | static int classifyWordVB(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler) { | |
21 | ||
22 | char s[100]; | |
d134f170 RD |
23 | bool wordIsNumber = isdigit(styler[start]) || (styler[start] == '.') || |
24 | (styler[start] == '&' && tolower(styler[start+1]) == 'h'); | |
25 | unsigned int i; | |
26 | for (i = 0; i < end - start + 1 && i < 30; i++) { | |
f6bcfd97 | 27 | s[i] = static_cast<char>(tolower(styler[start + i])); |
f6bcfd97 | 28 | } |
d134f170 | 29 | s[i] = '\0'; |
f6bcfd97 BP |
30 | char chAttr = SCE_C_DEFAULT; |
31 | if (wordIsNumber) | |
32 | chAttr = SCE_C_NUMBER; | |
33 | else { | |
d134f170 RD |
34 | if (strcmp(s, "rem") == 0) |
35 | chAttr = SCE_C_COMMENTLINE; | |
36 | else if (keywords.InList(s)) | |
f6bcfd97 | 37 | chAttr = SCE_C_WORD; |
f6bcfd97 BP |
38 | } |
39 | styler.ColourTo(end, chAttr); | |
40 | if (chAttr == SCE_C_COMMENTLINE) | |
41 | return SCE_C_COMMENTLINE; | |
42 | else | |
43 | return SCE_C_DEFAULT; | |
44 | } | |
45 | ||
46 | static void ColouriseVBDoc(unsigned int startPos, int length, int initStyle, | |
47 | WordList *keywordlists[], Accessor &styler) { | |
48 | ||
49 | WordList &keywords = *keywordlists[0]; | |
50 | ||
51 | styler.StartAt(startPos); | |
52 | ||
d134f170 | 53 | int visibleChars = 0; |
f6bcfd97 BP |
54 | int state = initStyle; |
55 | char chNext = styler[startPos]; | |
56 | styler.StartSegment(startPos); | |
57 | int lengthDoc = startPos + length; | |
58 | for (int i = startPos; i < lengthDoc; i++) { | |
59 | char ch = chNext; | |
60 | chNext = styler.SafeGetCharAt(i + 1); | |
61 | ||
62 | if (styler.IsLeadByte(ch)) { | |
63 | chNext = styler.SafeGetCharAt(i + 2); | |
64 | i += 1; | |
65 | continue; | |
66 | } | |
67 | ||
d134f170 RD |
68 | if (ch == '\r' || ch == '\n') { |
69 | // End of line | |
70 | if (state == SCE_C_COMMENTLINE || state == SCE_C_PREPROCESSOR) { | |
71 | styler.ColourTo(i - 1, state); | |
72 | state = SCE_C_DEFAULT; | |
73 | } | |
74 | visibleChars = 0; | |
75 | } | |
76 | if (!isspace(ch)) | |
77 | visibleChars++; | |
78 | ||
f6bcfd97 BP |
79 | if (state == SCE_C_DEFAULT) { |
80 | if (iswordstart(ch)) { | |
81 | styler.ColourTo(i - 1, state); | |
82 | state = SCE_C_WORD; | |
83 | } else if (ch == '\'') { | |
84 | styler.ColourTo(i - 1, state); | |
85 | state = SCE_C_COMMENTLINE; | |
86 | } else if (ch == '\"') { | |
87 | styler.ColourTo(i - 1, state); | |
88 | state = SCE_C_STRING; | |
d134f170 RD |
89 | } else if (ch == '#' && visibleChars == 1) { |
90 | // Preprocessor commands are alone on their line | |
91 | styler.ColourTo(i - 1, state); | |
92 | state = SCE_C_PREPROCESSOR; | |
93 | } else if (ch == '&' && tolower(chNext) == 'h') { | |
94 | styler.ColourTo(i - 1, state); | |
95 | state = SCE_C_WORD; | |
96 | } else if (isoperator(ch)) { | |
97 | styler.ColourTo(i - 1, state); | |
98 | styler.ColourTo(i, SCE_C_OPERATOR); | |
f6bcfd97 BP |
99 | } |
100 | } else if (state == SCE_C_WORD) { | |
101 | if (!iswordchar(ch)) { | |
102 | state = classifyWordVB(styler.GetStartSegment(), i - 1, keywords, styler); | |
103 | if (state == SCE_C_DEFAULT) { | |
104 | if (ch == '\'') { | |
105 | state = SCE_C_COMMENTLINE; | |
106 | } else if (ch == '\"') { | |
107 | state = SCE_C_STRING; | |
d134f170 RD |
108 | } else if (isoperator(ch)) { |
109 | styler.ColourTo(i - 1, state); | |
110 | styler.ColourTo(i, SCE_C_OPERATOR); | |
f6bcfd97 BP |
111 | } |
112 | } | |
113 | } | |
114 | } else { | |
d134f170 | 115 | if (state == SCE_C_STRING) { |
f6bcfd97 BP |
116 | // VB doubles quotes to preserve them |
117 | if (ch == '\"') { | |
118 | styler.ColourTo(i, state); | |
119 | state = SCE_C_DEFAULT; | |
120 | i++; | |
121 | ch = chNext; | |
122 | chNext = styler.SafeGetCharAt(i + 1); | |
123 | } | |
124 | } | |
125 | if (state == SCE_C_DEFAULT) { // One of the above succeeded | |
126 | if (ch == '\'') { | |
127 | state = SCE_C_COMMENTLINE; | |
128 | } else if (ch == '\"') { | |
129 | state = SCE_C_STRING; | |
130 | } else if (iswordstart(ch)) { | |
131 | state = SCE_C_WORD; | |
132 | } | |
133 | } | |
134 | } | |
135 | } | |
136 | styler.ColourTo(lengthDoc, state); | |
137 | } | |
138 | ||
139 | LexerModule lmVB(SCLEX_VB, ColouriseVBDoc); |