]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/LexLua.cxx
18612c9ee3558bd4574a397ce8f3d68a911a8477
[wxWidgets.git] / contrib / src / stc / scintilla / src / LexLua.cxx
1 // Scintilla source code edit control
2 /** @file LexLua.cxx
3 ** Lexer for Lua language.
4 **
5 ** Written by Paul Winwood.
6 ** Folder by Alexey Yutkin.
7 ** Modified by Marcos E. Wurzius & Philippe Lhoste
8 **/
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16
17 #include "Platform.h"
18
19 #include "PropSet.h"
20 #include "Accessor.h"
21 #include "StyleContext.h"
22 #include "KeyWords.h"
23 #include "Scintilla.h"
24 #include "SciLexer.h"
25
26 static inline bool IsAWordChar(const int ch) {
27 return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '.');
28 }
29
30 inline bool IsAWordStart(const int ch) {
31 return (ch < 0x80) && (isalnum(ch) || ch == '_');
32 }
33
34 inline bool isLuaOperator(char ch) {
35 if (isalnum(ch))
36 return false;
37 // '.' left out as it is used to make up numbers
38 if (ch == '*' || ch == '/' || ch == '-' || ch == '+' ||
39 ch == '(' || ch == ')' || ch == '=' ||
40 ch == '{' || ch == '}' || ch == '~' ||
41 ch == '[' || ch == ']' || ch == ';' ||
42 ch == '<' || ch == '>' || ch == ',' ||
43 ch == '.' || ch == '^' || ch == '%' || ch == ':')
44 return true;
45 return false;
46 }
47
48 static void ColouriseLuaDoc(
49 unsigned int startPos,
50 int length,
51 int initStyle,
52 WordList *keywordlists[],
53 Accessor &styler) {
54
55 WordList &keywords = *keywordlists[0];
56 WordList &keywords2 = *keywordlists[1];
57 WordList &keywords3 = *keywordlists[2];
58 WordList &keywords4 = *keywordlists[3];
59 WordList &keywords5 = *keywordlists[4];
60 WordList &keywords6 = *keywordlists[5];
61 WordList &keywords7 = *keywordlists[6];
62 WordList &keywords8 = *keywordlists[7];
63
64 int currentLine = styler.GetLine(startPos);
65 // Initialize the literal string [[ ... ]] nesting level, if we are inside such a string.
66 int literalStringLevel = 0;
67 if (initStyle == SCE_LUA_LITERALSTRING) {
68 literalStringLevel = styler.GetLineState(currentLine - 1);
69 }
70 // Initialize the block comment --[[ ... ]] nesting level, if we are inside such a comment
71 int blockCommentLevel = 0;
72 if (initStyle == SCE_LUA_COMMENT) {
73 blockCommentLevel = styler.GetLineState(currentLine - 1);
74 }
75
76 // Do not leak onto next line
77 if (initStyle == SCE_LUA_STRINGEOL) {
78 initStyle = SCE_LUA_DEFAULT;
79 }
80
81 StyleContext sc(startPos, length, initStyle, styler);
82 if (startPos == 0 && sc.ch == '#') {
83 // shbang line: # is a comment only if first char of the script
84 sc.SetState(SCE_LUA_COMMENTLINE);
85 }
86 for (; sc.More(); sc.Forward()) {
87 if (sc.atLineEnd) {
88 // Update the line state, so it can be seen by next line
89 currentLine = styler.GetLine(sc.currentPos);
90 switch (sc.state) {
91 case SCE_LUA_LITERALSTRING:
92 // Inside a literal string, we set the line state
93 styler.SetLineState(currentLine, literalStringLevel);
94 break;
95 case SCE_LUA_COMMENT: // Block comment
96 // Inside a block comment, we set the line state
97 styler.SetLineState(currentLine, blockCommentLevel);
98 break;
99 default:
100 // Reset the line state
101 styler.SetLineState(currentLine, 0);
102 break;
103 }
104 }
105 if (sc.atLineStart && (sc.state == SCE_LUA_STRING)) {
106 // Prevent SCE_LUA_STRINGEOL from leaking back to previous line
107 sc.SetState(SCE_LUA_STRING);
108 }
109
110 // Handle string line continuation
111 if ((sc.state == SCE_LUA_STRING || sc.state == SCE_LUA_CHARACTER) &&
112 sc.ch == '\\') {
113 if (sc.chNext == '\n' || sc.chNext == '\r') {
114 sc.Forward();
115 if (sc.ch == '\r' && sc.chNext == '\n') {
116 sc.Forward();
117 }
118 continue;
119 }
120 }
121
122 // Determine if the current state should terminate.
123 if (sc.state == SCE_LUA_OPERATOR) {
124 sc.SetState(SCE_LUA_DEFAULT);
125 } else if (sc.state == SCE_LUA_NUMBER) {
126 // We stop the number definition on non-numerical non-dot non-eE non-sign char
127 if (!(isdigit(sc.ch) || sc.ch == '.' ||
128 toupper(sc.ch) == 'E' || sc.ch == '-' || sc.ch == '+')) {
129 // Not exactly following number definition (several dots are seen as OK, etc.)
130 // but probably enough in most cases.
131 sc.SetState(SCE_LUA_DEFAULT);
132 }
133 } else if (sc.state == SCE_LUA_IDENTIFIER) {
134 if (!IsAWordChar(sc.ch)) {
135 char s[100];
136 sc.GetCurrent(s, sizeof(s));
137 if (keywords.InList(s)) {
138 sc.ChangeState(SCE_LUA_WORD);
139 } else if (keywords2.InList(s)) {
140 sc.ChangeState(SCE_LUA_WORD2);
141 } else if (keywords3.InList(s)) {
142 sc.ChangeState(SCE_LUA_WORD3);
143 } else if (keywords4.InList(s)) {
144 sc.ChangeState(SCE_LUA_WORD4);
145 } else if (keywords5.InList(s)) {
146 sc.ChangeState(SCE_LUA_WORD5);
147 } else if (keywords6.InList(s)) {
148 sc.ChangeState(SCE_LUA_WORD6);
149 } else if (keywords6.InList(s)) {
150 sc.ChangeState(SCE_LUA_WORD6);
151 } else if (keywords7.InList(s)) {
152 sc.ChangeState(SCE_LUA_WORD7);
153 } else if (keywords8.InList(s)) {
154 sc.ChangeState(SCE_LUA_WORD8);
155 }
156 sc.SetState(SCE_LUA_DEFAULT);
157 }
158 } else if (sc.state == SCE_LUA_COMMENTLINE ) {
159 if (sc.atLineEnd) {
160 sc.SetState(SCE_LUA_DEFAULT);
161 }
162 } else if (sc.state == SCE_LUA_PREPROCESSOR ) {
163 if (sc.atLineEnd) {
164 sc.SetState(SCE_LUA_DEFAULT);
165 }
166 } else if (sc.state == SCE_LUA_STRING) {
167 if (sc.ch == '\\') {
168 if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
169 sc.Forward();
170 }
171 } else if (sc.ch == '\"') {
172 sc.ForwardSetState(SCE_LUA_DEFAULT);
173 } else if (sc.atLineEnd) {
174 sc.ChangeState(SCE_LUA_STRINGEOL);
175 sc.ForwardSetState(SCE_LUA_DEFAULT);
176 }
177 } else if (sc.state == SCE_LUA_CHARACTER) {
178 if (sc.ch == '\\') {
179 if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
180 sc.Forward();
181 }
182 } else if (sc.ch == '\'') {
183 sc.ForwardSetState(SCE_LUA_DEFAULT);
184 } else if (sc.atLineEnd) {
185 sc.ChangeState(SCE_LUA_STRINGEOL);
186 sc.ForwardSetState(SCE_LUA_DEFAULT);
187 }
188 } else if (sc.state == SCE_LUA_LITERALSTRING) {
189 if (sc.Match('[', '[')) {
190 literalStringLevel++;
191 sc.Forward();
192 sc.SetState(SCE_LUA_LITERALSTRING);
193 } else if (sc.Match(']', ']') && literalStringLevel > 0) {
194 literalStringLevel--;
195 sc.Forward();
196 if (literalStringLevel == 0) {
197 sc.ForwardSetState(SCE_LUA_DEFAULT);
198 }
199 }
200 } else if (sc.state == SCE_LUA_COMMENT) { // Lua 5.0's block comment
201 if (sc.Match('[', '[')) {
202 blockCommentLevel++;
203 sc.Forward();
204 } else if (sc.Match(']', ']') && blockCommentLevel > 0) {
205 blockCommentLevel--;
206 sc.Forward();
207 if (blockCommentLevel == 0) {
208 sc.ForwardSetState(SCE_LUA_DEFAULT);
209 }
210 }
211 }
212
213 // Determine if a new state should be entered.
214 if (sc.state == SCE_LUA_DEFAULT) {
215 if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
216 sc.SetState(SCE_LUA_NUMBER);
217 } else if (IsAWordStart(sc.ch)) {
218 sc.SetState(SCE_LUA_IDENTIFIER);
219 } else if (sc.Match('\"')) {
220 sc.SetState(SCE_LUA_STRING);
221 } else if (sc.Match('\'')) {
222 sc.SetState(SCE_LUA_CHARACTER);
223 } else if (sc.Match('[', '[')) {
224 literalStringLevel = 1;
225 sc.SetState(SCE_LUA_LITERALSTRING);
226 sc.Forward();
227 } else if (sc.Match("--[[")) { // Lua 5.0's block comment
228 blockCommentLevel = 1;
229 sc.SetState(SCE_LUA_COMMENT);
230 sc.Forward(3);
231 } else if (sc.Match('-', '-')) {
232 sc.SetState(SCE_LUA_COMMENTLINE);
233 sc.Forward();
234 } else if (sc.atLineStart && sc.Match('$')) {
235 sc.SetState(SCE_LUA_PREPROCESSOR); // Obsolete since Lua 4.0, but still in old code
236 } else if (isLuaOperator(static_cast<char>(sc.ch))) {
237 sc.SetState(SCE_LUA_OPERATOR);
238 }
239 }
240 }
241 sc.Complete();
242 }
243
244 static void FoldLuaDoc(unsigned int startPos, int length, int /* initStyle */, WordList *[],
245 Accessor &styler) {
246 unsigned int lengthDoc = startPos + length;
247 int visibleChars = 0;
248 int lineCurrent = styler.GetLine(startPos);
249 int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
250 int levelCurrent = levelPrev;
251 char chNext = styler[startPos];
252 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
253 int styleNext = styler.StyleAt(startPos);
254 char s[10];
255
256 for (unsigned int i = startPos; i < lengthDoc; i++) {
257 char ch = chNext;
258 chNext = styler.SafeGetCharAt(i + 1);
259 int style = styleNext;
260 styleNext = styler.StyleAt(i + 1);
261 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
262 if (style == SCE_LUA_WORD) {
263 if (ch == 'i' || ch == 'd' || ch == 'f' || ch == 'e') {
264 for (unsigned int j = 0; j < 8; j++) {
265 if (!iswordchar(styler[i + j])) {
266 break;
267 }
268 s[j] = styler[i + j];
269 s[j + 1] = '\0';
270 }
271
272 if ((strcmp(s, "if") == 0) || (strcmp(s, "do") == 0) || (strcmp(s, "function") == 0)) {
273 levelCurrent++;
274 }
275 if ((strcmp(s, "end") == 0) || (strcmp(s, "elseif") == 0)) {
276 levelCurrent--;
277 }
278 }
279 } else if (style == SCE_LUA_OPERATOR) {
280 if (ch == '{' || ch == '(') {
281 levelCurrent++;
282 } else if (ch == '}' || ch == ')') {
283 levelCurrent--;
284 }
285 }
286
287 if (atEOL) {
288 int lev = levelPrev;
289 if (visibleChars == 0 && foldCompact) {
290 lev |= SC_FOLDLEVELWHITEFLAG;
291 }
292 if ((levelCurrent > levelPrev) && (visibleChars > 0)) {
293 lev |= SC_FOLDLEVELHEADERFLAG;
294 }
295 if (lev != styler.LevelAt(lineCurrent)) {
296 styler.SetLevel(lineCurrent, lev);
297 }
298 lineCurrent++;
299 levelPrev = levelCurrent;
300 visibleChars = 0;
301 }
302 if (!isspacechar(ch)) {
303 visibleChars++;
304 }
305 }
306 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
307
308 int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
309 styler.SetLevel(lineCurrent, levelPrev | flagsNext);
310 }
311
312 static const char * const luaWordListDesc[] = {
313 "Keywords",
314 "Basic functions",
315 "String, (table) & math functions",
316 "(coroutines), I/O & system facilities",
317 "XXX",
318 "XXX",
319 0
320 };
321
322 LexerModule lmLua(SCLEX_LUA, ColouriseLuaDoc, "lua", FoldLuaDoc, luaWordListDesc);