]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/LexLua.cxx
18612c9ee3558bd4574a397ce8f3d68a911a8477
1 // Scintilla source code edit control
3 ** Lexer for Lua language.
5 ** Written by Paul Winwood.
6 ** Folder by Alexey Yutkin.
7 ** Modified by Marcos E. Wurzius & Philippe Lhoste
21 #include "StyleContext.h"
23 #include "Scintilla.h"
26 static inline bool IsAWordChar(const int ch
) {
27 return (ch
< 0x80) && (isalnum(ch
) || ch
== '_' || ch
== '.');
30 inline bool IsAWordStart(const int ch
) {
31 return (ch
< 0x80) && (isalnum(ch
) || ch
== '_');
34 inline bool isLuaOperator(char ch
) {
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
== ':')
48 static void ColouriseLuaDoc(
49 unsigned int startPos
,
52 WordList
*keywordlists
[],
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];
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);
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);
76 // Do not leak onto next line
77 if (initStyle
== SCE_LUA_STRINGEOL
) {
78 initStyle
= SCE_LUA_DEFAULT
;
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
);
86 for (; sc
.More(); sc
.Forward()) {
88 // Update the line state, so it can be seen by next line
89 currentLine
= styler
.GetLine(sc
.currentPos
);
91 case SCE_LUA_LITERALSTRING
:
92 // Inside a literal string, we set the line state
93 styler
.SetLineState(currentLine
, literalStringLevel
);
95 case SCE_LUA_COMMENT
: // Block comment
96 // Inside a block comment, we set the line state
97 styler
.SetLineState(currentLine
, blockCommentLevel
);
100 // Reset the line state
101 styler
.SetLineState(currentLine
, 0);
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
);
110 // Handle string line continuation
111 if ((sc
.state
== SCE_LUA_STRING
|| sc
.state
== SCE_LUA_CHARACTER
) &&
113 if (sc
.chNext
== '\n' || sc
.chNext
== '\r') {
115 if (sc
.ch
== '\r' && sc
.chNext
== '\n') {
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
);
133 } else if (sc
.state
== SCE_LUA_IDENTIFIER
) {
134 if (!IsAWordChar(sc
.ch
)) {
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
);
156 sc
.SetState(SCE_LUA_DEFAULT
);
158 } else if (sc
.state
== SCE_LUA_COMMENTLINE
) {
160 sc
.SetState(SCE_LUA_DEFAULT
);
162 } else if (sc
.state
== SCE_LUA_PREPROCESSOR
) {
164 sc
.SetState(SCE_LUA_DEFAULT
);
166 } else if (sc
.state
== SCE_LUA_STRING
) {
168 if (sc
.chNext
== '\"' || sc
.chNext
== '\'' || sc
.chNext
== '\\') {
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
);
177 } else if (sc
.state
== SCE_LUA_CHARACTER
) {
179 if (sc
.chNext
== '\"' || sc
.chNext
== '\'' || sc
.chNext
== '\\') {
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
);
188 } else if (sc
.state
== SCE_LUA_LITERALSTRING
) {
189 if (sc
.Match('[', '[')) {
190 literalStringLevel
++;
192 sc
.SetState(SCE_LUA_LITERALSTRING
);
193 } else if (sc
.Match(']', ']') && literalStringLevel
> 0) {
194 literalStringLevel
--;
196 if (literalStringLevel
== 0) {
197 sc
.ForwardSetState(SCE_LUA_DEFAULT
);
200 } else if (sc
.state
== SCE_LUA_COMMENT
) { // Lua 5.0's block comment
201 if (sc
.Match('[', '[')) {
204 } else if (sc
.Match(']', ']') && blockCommentLevel
> 0) {
207 if (blockCommentLevel
== 0) {
208 sc
.ForwardSetState(SCE_LUA_DEFAULT
);
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
);
227 } else if (sc
.Match("--[[")) { // Lua 5.0's block comment
228 blockCommentLevel
= 1;
229 sc
.SetState(SCE_LUA_COMMENT
);
231 } else if (sc
.Match('-', '-')) {
232 sc
.SetState(SCE_LUA_COMMENTLINE
);
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
);
244 static void FoldLuaDoc(unsigned int startPos
, int length
, int /* initStyle */, WordList
*[],
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
);
256 for (unsigned int i
= startPos
; i
< lengthDoc
; i
++) {
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
])) {
268 s
[j
] = styler
[i
+ j
];
272 if ((strcmp(s
, "if") == 0) || (strcmp(s
, "do") == 0) || (strcmp(s
, "function") == 0)) {
275 if ((strcmp(s
, "end") == 0) || (strcmp(s
, "elseif") == 0)) {
279 } else if (style
== SCE_LUA_OPERATOR
) {
280 if (ch
== '{' || ch
== '(') {
282 } else if (ch
== '}' || ch
== ')') {
289 if (visibleChars
== 0 && foldCompact
) {
290 lev
|= SC_FOLDLEVELWHITEFLAG
;
292 if ((levelCurrent
> levelPrev
) && (visibleChars
> 0)) {
293 lev
|= SC_FOLDLEVELHEADERFLAG
;
295 if (lev
!= styler
.LevelAt(lineCurrent
)) {
296 styler
.SetLevel(lineCurrent
, lev
);
299 levelPrev
= levelCurrent
;
302 if (!isspacechar(ch
)) {
306 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
308 int flagsNext
= styler
.LevelAt(lineCurrent
) & ~SC_FOLDLEVELNUMBERMASK
;
309 styler
.SetLevel(lineCurrent
, levelPrev
| flagsNext
);
312 static const char * const luaWordListDesc
[] = {
315 "String, (table) & math functions",
316 "(coroutines), I/O & system facilities",
322 LexerModule
lmLua(SCLEX_LUA
, ColouriseLuaDoc
, "lua", FoldLuaDoc
, luaWordListDesc
);