]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexLua.cxx
fixed wxListBox inheritance
[wxWidgets.git] / src / stc / scintilla / src / LexLua.cxx
1 // LexLua.cxx - lexer for Lua language
2 // Written by Paul Winwood
3
4 #include <stdlib.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include <stdarg.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10
11 #include "Platform.h"
12
13 #include "PropSet.h"
14 #include "Accessor.h"
15 #include "KeyWords.h"
16 #include "Scintilla.h"
17 #include "SciLexer.h"
18
19 static void classifyWordLua(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler)
20 {
21 char s[100];
22 bool wordIsNumber = isdigit(styler[start]) || (styler[start] == '.');
23
24 for (unsigned int i = 0; i < end - start + 1 && i < 30; i++)
25 {
26 s[i] = styler[start + i];
27 s[i + 1] = '\0';
28 }
29
30 char chAttr = SCE_LUA_IDENTIFIER;
31
32 if (wordIsNumber)
33 chAttr = SCE_LUA_NUMBER;
34 else
35 {
36 if (keywords.InList(s))
37 {
38 chAttr = SCE_LUA_WORD;
39 }
40 }
41 styler.ColourTo(end, chAttr);
42 }
43
44 static void ColouriseLuaDoc(unsigned int startPos,
45 int length,
46 int initStyle,
47 WordList *keywordlists[],
48 Accessor &styler)
49 {
50
51 WordList &keywords = *keywordlists[0];
52
53 styler.StartAt(startPos);
54 styler.GetLine(startPos);
55
56 int state = initStyle;
57 char chPrev = ' ';
58 char chNext = styler[startPos];
59 unsigned int lengthDoc = startPos + length;
60 bool firstChar = true;
61 int literalString = 0;
62
63 styler.StartSegment(startPos);
64 for (unsigned int i = startPos; i <= lengthDoc; i++)
65 {
66 char ch = chNext;
67 chNext = styler.SafeGetCharAt(i + 1);
68
69 if (styler.IsLeadByte(ch))
70 {
71 chNext = styler.SafeGetCharAt(i + 2);
72 chPrev = ' ';
73 i += 1;
74 continue;
75 }
76
77 if (state == SCE_LUA_STRINGEOL)
78 {
79 if (ch != '\r' && ch != '\n')
80 {
81 styler.ColourTo(i-1, state);
82 state = SCE_LUA_DEFAULT;
83 }
84 }
85
86 if (state == SCE_LUA_LITERALSTRING && ch == '[' && chNext == '[')
87 {
88 literalString++;
89 }
90 else
91 if (state == SCE_LUA_DEFAULT)
92 {
93 if (ch == '-' && chNext == '-')
94 {
95 styler.ColourTo(i-1, state);
96 state = SCE_LUA_COMMENTLINE;
97 }
98 else
99 if (ch == '[' && chNext == '[')
100 {
101 state = SCE_LUA_LITERALSTRING;
102 literalString = 1;
103 }
104 else
105 if (iswordstart(ch))
106 {
107 styler.ColourTo(i-1, state);
108 state = SCE_LUA_WORD;
109 }
110 else
111 if (ch == '\"')
112 {
113 styler.ColourTo(i-1, state);
114 state = SCE_LUA_STRING;
115 }
116 else
117 if (ch == '\'')
118 {
119 styler.ColourTo(i-1, state);
120 state = SCE_LUA_CHARACTER;
121 }
122 else
123 if (ch == '$' && firstChar)
124 {
125 styler.ColourTo(i-1, state);
126 state = SCE_LUA_PREPROCESSOR;
127 }
128 else
129 if (isoperator(ch))
130 {
131 styler.ColourTo(i-1, state);
132 styler.ColourTo(i, SCE_LUA_OPERATOR);
133 }
134 }
135 else
136 if (state == SCE_LUA_WORD)
137 {
138 if (!iswordchar(ch))
139 {
140 classifyWordLua(styler.GetStartSegment(), i - 1, keywords, styler);
141 state = SCE_LUA_DEFAULT;
142 if (ch == '[' && chNext == '[')
143 {
144 literalString = 1;
145 state = SCE_LUA_LITERALSTRING;
146 }
147 else
148 if (ch == '-' && chNext == '-')
149 {
150 state = SCE_LUA_COMMENTLINE;
151 }
152 else
153 if (ch == '\"')
154 {
155 state = SCE_LUA_STRING;
156 }
157 else
158 if (ch == '\'')
159 {
160 state = SCE_LUA_CHARACTER;
161 }
162 else
163 if (ch == '$' && firstChar)
164 {
165 state = SCE_LUA_PREPROCESSOR;
166 }
167 else
168 if (isoperator(ch))
169 {
170 styler.ColourTo(i, SCE_LUA_OPERATOR);
171 }
172 }
173 }
174 else
175 {
176 if (state == SCE_LUA_LITERALSTRING)
177 {
178 if (ch == ']' && (chPrev == ']') && (--literalString == 0))
179 {
180 styler.ColourTo(i, state);
181 state = SCE_LUA_DEFAULT;
182 }
183 }
184 else
185 if (state == SCE_LUA_PREPROCESSOR)
186 {
187 if ((ch == '\r' || ch == '\n') && (chPrev != '\\'))
188 {
189 styler.ColourTo(i-1, state);
190 state = SCE_LUA_DEFAULT;
191 }
192 }
193 else
194 if (state == SCE_LUA_COMMENTLINE)
195 {
196 if (ch == '\r' || ch == '\n')
197 {
198 styler.ColourTo(i-1, state);
199 state = SCE_LUA_DEFAULT;
200 }
201 }
202 else
203 if (state == SCE_LUA_STRING)
204 {
205 if ((ch == '\r' || ch == '\n') && (chPrev != '\\'))
206 {
207 styler.ColourTo(i-1, state);
208 state = SCE_LUA_STRINGEOL;
209 }
210 else
211 if (ch == '\\')
212 {
213 if (chNext == '\"' || chNext == '\\')
214 {
215 i++;
216 ch = chNext;
217 chNext = styler.SafeGetCharAt(i + 1);
218 }
219 }
220 else
221 if (ch == '\"')
222 {
223 styler.ColourTo(i, state);
224 state = SCE_LUA_DEFAULT;
225 i++;
226 ch = chNext;
227 chNext = styler.SafeGetCharAt(i + 1);
228 }
229 }
230 else
231 if (state == SCE_LUA_CHARACTER)
232 {
233 if ((ch == '\r' || ch == '\n') && (chPrev != '\\'))
234 {
235 styler.ColourTo(i-1, state);
236 state = SCE_LUA_STRINGEOL;
237 }
238 else
239 if (ch == '\\')
240 {
241 if (chNext == '\'' || chNext == '\\')
242 {
243 i++;
244 ch = chNext;
245 chNext = styler.SafeGetCharAt(i + 1);
246 }
247 }
248 else
249 if (ch == '\'')
250 {
251 styler.ColourTo(i, state);
252 state = SCE_LUA_DEFAULT;
253 i++;
254 ch = chNext;
255 chNext = styler.SafeGetCharAt(i + 1);
256 }
257 }
258
259 if (state == SCE_LUA_DEFAULT)
260 {
261 if (ch == '-' && chNext == '-')
262 {
263 state = SCE_LUA_COMMENTLINE;
264 }
265 else
266 if (ch == '\"')
267 {
268 state = SCE_LUA_STRING;
269 }
270 else
271 if (ch == '\'')
272 {
273 state = SCE_LUA_CHARACTER;
274 }
275 else
276 if (ch == '$' && firstChar)
277 {
278 state = SCE_LUA_PREPROCESSOR;
279 }
280 else
281 if (iswordstart(ch))
282 {
283 state = SCE_LUA_WORD;
284 }
285 else
286 if (isoperator(ch))
287 {
288 styler.ColourTo(i, SCE_LUA_OPERATOR);
289 }
290 }
291 }
292 chPrev = ch;
293 firstChar = (ch == '\r' || ch == '\n');
294 }
295 styler.ColourTo(lengthDoc - 1, state);
296 }
297
298 LexerModule lmLua(SCLEX_LUA, ColouriseLuaDoc);