]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/LexLua.cxx
338a04e327f0661683fbed476cf9c0f10bfee388
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
21 #include "StyleContext.h"
23 #include "Scintilla.h"
27 static inline bool IsAWordChar(const int ch
) {
28 return (ch
< 0x80) && (isalnum(ch
) || ch
== '.' || ch
== '_');
31 inline bool IsAWordStart(const int ch
) {
32 return (ch
< 0x80) && (isalnum(ch
) || ch
== '_');
36 inline bool isLuaOperator(char ch
) {
39 // '.' left out as it is used to make up numbers
40 if (ch
== '*' || ch
== '/' || ch
== '-' || ch
== '+' ||
41 ch
== '(' || ch
== ')' || ch
== '=' ||
42 ch
== '{' || ch
== '}' || ch
== '~' ||
43 ch
== '[' || ch
== ']' || ch
== ';' ||
44 ch
== '<' || ch
== '>' || ch
== ',' ||
45 ch
== '.' || ch
== '^' || ch
== '%' || ch
== ':')
51 static void ColouriseLuaDoc(unsigned int startPos
, int length
, int initStyle
, WordList
*keywordlists
[],
54 WordList
&keywords
= *keywordlists
[0];
55 WordList
&keywords2
= *keywordlists
[1];
56 WordList
&keywords3
= *keywordlists
[2];
57 WordList
&keywords4
= *keywordlists
[3];
58 WordList
&keywords5
= *keywordlists
[4];
59 WordList
&keywords6
= *keywordlists
[5];
60 int literalString
= 0;
61 int literalStringFlag
=0;
63 // Do not leak onto next line
64 if (initStyle
== SCE_LUA_STRINGEOL
)
65 initStyle
= SCE_LUA_DEFAULT
;
67 StyleContext
sc(startPos
, length
, initStyle
, styler
);
68 if(startPos
== 0 && sc
.ch
== '#') sc
.SetState(SCE_LUA_COMMENTLINE
);
69 for (; sc
.More(); sc
.Forward()) {
71 // Handle line continuation generically.
73 if (sc
.Match("\\\n")) {
78 if (sc
.Match("\\\r\n")) {
86 // Determine if the current state should terminate.
87 if (sc
.state
== SCE_LUA_OPERATOR
) {
88 sc
.SetState(SCE_LUA_DEFAULT
);
89 } else if (sc
.state
== SCE_LUA_NUMBER
) {
90 if (!IsAWordChar(sc
.ch
)) {
91 sc
.SetState(SCE_LUA_DEFAULT
);
93 } else if (sc
.state
== SCE_LUA_IDENTIFIER
) {
94 if (!IsAWordChar(sc
.ch
) || (sc
.ch
== '.')) {
96 sc
.GetCurrent(s
, sizeof(s
));
97 if (keywords
.InList(s
)) {
98 sc
.ChangeState(SCE_LUA_WORD
);
99 } else if (keywords2
.InList(s
)) {
100 sc
.ChangeState(SCE_LUA_WORD2
);
101 } else if (keywords3
.InList(s
)) {
102 sc
.ChangeState(SCE_LUA_WORD3
);
103 } else if (keywords4
.InList(s
)) {
104 sc
.ChangeState(SCE_LUA_WORD4
);
105 } else if (keywords5
.InList(s
)) {
106 sc
.ChangeState(SCE_LUA_WORD5
);
107 } else if (keywords6
.InList(s
)) {
108 sc
.ChangeState(SCE_LUA_WORD6
);
110 sc
.SetState(SCE_LUA_DEFAULT
);
114 } else if (sc
.state
== SCE_LUA_COMMENTLINE
) {
116 sc
.SetState(SCE_LUA_DEFAULT
);
118 } else if (sc
.state
== SCE_LUA_STRING
) {
120 if (sc
.chNext
== '\"' || sc
.chNext
== '\'' || sc
.chNext
== '\\') {
123 } else if (sc
.ch
== '\"') {
124 sc
.ForwardSetState(SCE_LUA_DEFAULT
);
125 } else if (sc
.atLineEnd
) {
126 sc
.ChangeState(SCE_LUA_STRINGEOL
);
127 sc
.ForwardSetState(SCE_LUA_DEFAULT
);
130 } else if (sc
.state
== SCE_LUA_CHARACTER
) {
132 if (sc
.chNext
== '\"' || sc
.chNext
== '\'' || sc
.chNext
== '\\') {
135 } else if (sc
.ch
== '\'') {
136 sc
.ForwardSetState(SCE_LUA_DEFAULT
);
137 } else if (sc
.atLineEnd
) {
138 sc
.ChangeState(SCE_LUA_STRINGEOL
);
139 sc
.ForwardSetState(SCE_LUA_DEFAULT
);
141 } else if (sc
.state
== SCE_LUA_LITERALSTRING
) {
142 if (sc
.chPrev
== '[' && sc
.ch
== '[' && literalStringFlag
!= 1) {
144 literalStringFlag
= 1;
146 else if (sc
.chPrev
== ']' && sc
.ch
== ']' && literalStringFlag
!= 2 ) {
147 if((--literalString
== 1))
148 sc
.ForwardSetState(SCE_LUA_DEFAULT
);
149 literalStringFlag
= 2;
151 else literalStringFlag
= 0;
153 // Determine if a new state should be entered.
154 if (sc
.state
== SCE_LUA_DEFAULT
) {
155 if (IsADigit(sc
.ch
) || (sc
.ch
== '.' && IsADigit(sc
.chNext
))) {
156 sc
.SetState(SCE_LUA_NUMBER
);
157 } else if (IsAWordStart(sc
.ch
) || (sc
.ch
== '@')) {
158 sc
.SetState(SCE_LUA_IDENTIFIER
);
159 } else if (sc
.ch
== '\"') {
160 sc
.SetState(SCE_LUA_STRING
);
161 } else if (sc
.ch
== '\'') {
162 sc
.SetState(SCE_LUA_CHARACTER
);
163 } else if (sc
.ch
== '[' && sc
.chNext
== '[') {
164 sc
.SetState(SCE_LUA_LITERALSTRING
);
166 } else if (sc
.ch
== '-' && sc
.chNext
== '-') {
167 sc
.SetState(SCE_LUA_COMMENTLINE
);
168 } else if (isLuaOperator(static_cast<char>(sc
.ch
))) {
169 sc
.SetState(SCE_LUA_OPERATOR
);
177 static void FoldLuaDoc(unsigned int startPos
, int length
, int /* initStyle */, WordList
*[],
179 unsigned int lengthDoc
= startPos
+ length
;
180 int visibleChars
= 0;
181 int lineCurrent
= styler
.GetLine(startPos
);
182 int levelPrev
= styler
.LevelAt(lineCurrent
) & SC_FOLDLEVELNUMBERMASK
;
183 int levelCurrent
= levelPrev
;
184 char chNext
= styler
[startPos
];
185 bool foldCompact
= styler
.GetPropertyInt("fold.compact", 1) != 0;
186 int styleNext
= styler
.StyleAt(startPos
);
188 for (unsigned int i
= startPos
; i
< lengthDoc
; i
++) {
190 chNext
= styler
.SafeGetCharAt(i
+ 1);
191 int style
= styleNext
;
192 styleNext
= styler
.StyleAt(i
+ 1);
193 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
194 if (style
== SCE_LUA_WORD
) {
195 if ( ch
== 'i' || ch
== 'e' || ch
== 't' || ch
== 'd' || ch
== 'f') {
196 for (unsigned int j
= 0; j
< 8; j
++) {
197 if (!iswordchar(styler
[i
+ j
]))
199 s
[j
] = styler
[i
+ j
];
203 if ((strcmp(s
, "if") == 0) || (strcmp(s
, "do") == 0)
204 || (strcmp(s
, "function") == 0))
206 if ((strcmp(s
, "end") == 0) || (strcmp(s
, "elseif") == 0))
211 else if (style
== SCE_LUA_OPERATOR
)
213 if(ch
== '{' || ch
== '(')
215 else if(ch
== '}' || ch
== ')')
221 if (visibleChars
== 0 && foldCompact
)
222 lev
|= SC_FOLDLEVELWHITEFLAG
;
223 if ((levelCurrent
> levelPrev
) && (visibleChars
> 0))
224 lev
|= SC_FOLDLEVELHEADERFLAG
;
225 if (lev
!= styler
.LevelAt(lineCurrent
)) {
226 styler
.SetLevel(lineCurrent
, lev
);
229 levelPrev
= levelCurrent
;
232 if (!isspacechar(ch
))
235 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
237 int flagsNext
= styler
.LevelAt(lineCurrent
) & ~SC_FOLDLEVELNUMBERMASK
;
238 styler
.SetLevel(lineCurrent
, levelPrev
| flagsNext
);
241 LexerModule
lmLua(SCLEX_LUA
, ColouriseLuaDoc
, "lua", FoldLuaDoc
);