1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/*
Simple parser for LUA
Written for Lua 5.1, based on parsecss and other parsers.
features: highlights keywords, strings, comments (no leveling supported! ("[==[")),tokens, basic indenting
to make this parser highlight your special functions pass table with this functions names to parserConfig argument of creator,
parserConfig: ["myfunction1","myfunction2"],
*/
function findFirstRegexp(words) {
return new RegExp("^(?:" + words.join("|") + ")", "i");
}
function matchRegexp(words) {
return new RegExp("^(?:" + words.join("|") + ")$", "i");
}
var luaCustomFunctions= matchRegexp([]);
function configureLUA(parserConfig){
if(parserConfig)
luaCustomFunctions= matchRegexp(parserConfig);
}
//long list of standard functions from lua manual
var luaStdFunctions = matchRegexp([
"_G","_VERSION","assert","collectgarbage","dofile","error","getfenv","getmetatable","ipairs","load","loadfile","loadstring","module","next","pairs","pcall","print","rawequal","rawget","rawset","require","select","setfenv","setmetatable","tonumber","tostring","type","unpack","xpcall",
"coroutine.create","coroutine.resume","coroutine.running","coroutine.status","coroutine.wrap","coroutine.yield",
"debug.debug","debug.getfenv","debug.gethook","debug.getinfo","debug.getlocal","debug.getmetatable","debug.getregistry","debug.getupvalue","debug.setfenv","debug.sethook","debug.setlocal","debug.setmetatable","debug.setupvalue","debug.traceback",
"close","flush","lines","read","seek","setvbuf","write",
"io.close","io.flush","io.input","io.lines","io.open","io.output","io.popen","io.read","io.stderr","io.stdin","io.stdout","io.tmpfile","io.type","io.write",
"math.abs","math.acos","math.asin","math.atan","math.atan2","math.ceil","math.cos","math.cosh","math.deg","math.exp","math.floor","math.fmod","math.frexp","math.huge","math.ldexp","math.log","math.log10","math.max","math.min","math.modf","math.pi","math.pow","math.rad","math.random","math.randomseed","math.sin","math.sinh","math.sqrt","math.tan","math.tanh",
"os.clock","os.date","os.difftime","os.execute","os.exit","os.getenv","os.remove","os.rename","os.setlocale","os.time","os.tmpname",
"package.cpath","package.loaded","package.loaders","package.loadlib","package.path","package.preload","package.seeall",
"string.byte","string.char","string.dump","string.find","string.format","string.gmatch","string.gsub","string.len","string.lower","string.match","string.rep","string.reverse","string.sub","string.upper",
"table.concat","table.insert","table.maxn","table.remove","table.sort"
]);
var luaKeywords = matchRegexp(["and","break","elseif","false","nil","not","or","return",
"true","function", "end", "if", "then", "else", "do",
"while", "repeat", "until", "for", "in", "local" ]);
var luaIndentKeys = matchRegexp(["function", "if","repeat","for","while", "[\(]", "{"]);
var luaUnindentKeys = matchRegexp(["end", "until", "[\)]", "}"]);
var luaUnindentKeys2 = findFirstRegexp(["end", "until", "[\)]", "}"]);
var luaMiddleKeys = findFirstRegexp(["else","elseif"]);
var LUAParser = Editor.Parser = (function() {
var tokenizeLUA = (function() {
function normal(source, setState) {
var ch = source.next();
if (ch == "-" && source.equals("-")) {
source.next();
setState(inSLComment);
return null;
}
else if (ch == "\"" || ch == "'") {
setState(inString(ch));
return null;
}
if (ch == "[" && (source.equals("[") || source.equals("="))) {
var level = 0;
while(source.equals("=")){
level ++;
source.next();
}
if(! source.equals("[") )
return "lua-error";
setState(inMLSomething(level,"lua-string"));
return null;
}
else if (ch == "=") {
if (source.equals("="))
source.next();
return "lua-token";
}
else if (ch == ".") {
if (source.equals("."))
source.next();
if (source.equals("."))
source.next();
return "lua-token";
}
else if (ch == "+" || ch == "-" || ch == "*" || ch == "/" || ch == "%" || ch == "^" || ch == "#" ) {
return "lua-token";
}
else if (ch == ">" || ch == "<" || ch == "(" || ch == ")" || ch == "{" || ch == "}" || ch == "[" ) {
return "lua-token";
}
else if (ch == "]" || ch == ";" || ch == ":" || ch == ",") {
return "lua-token";
}
else if (source.equals("=") && (ch == "~" || ch == "<" || ch == ">")) {
source.next();
return "lua-token";
}
else if (/\d/.test(ch)) {
source.nextWhileMatches(/[\w.%]/);
return "lua-number";
}
else {
source.nextWhileMatches(/[\w\\\-_.]/);
return "lua-identifier";
}
}
function inSLComment(source, setState) {
var start = true;
var count=0;
while (!source.endOfLine()) {
var ch = source.next();
var level = 0;
if ((ch =="[") && start)
while(source.equals("=")){
source.next();
level++;
}
if (source.equals("[")){
setState(inMLSomething(level,"lua-comment"));
return null;
}
start = false;
}
setState(normal);
return "lua-comment";
}
function inMLSomething(level,what) {
//wat sholud be "lua-string" or "lua-comment", level is the number of "=" in opening mark.
return function(source, setState){
var dashes = 0;
while (!source.endOfLine()) {
var ch = source.next();
if (dashes == level+1 && ch == "]" ) {
setState(normal);
break;
}
if (dashes == 0)
dashes = (ch == "]") ? 1:0;
else
dashes = (ch == "=") ? dashes + 1 : 0;
}
return what;
}
}
function inString(quote) {
return function(source, setState) {
var escaped = false;
while (!source.endOfLine()) {
var ch = source.next();
if (ch == quote && !escaped)
break;
escaped = !escaped && ch == "\\";
}
if (!escaped)
setState(normal);
return "lua-string";
};
}
return function(source, startState) {
return tokenizer(source, startState || normal);
};
})();
function indentLUA(indentDepth, base) {
return function(nextChars) {
var closing = (luaUnindentKeys2.test(nextChars) || luaMiddleKeys.test(nextChars));
return base + ( indentUnit * (indentDepth - (closing?1:0)) );
};
}
function parseLUA(source,basecolumn) {
basecolumn = basecolumn || 0;
var tokens = tokenizeLUA(source);
var indentDepth = 0;
var iter = {
next: function() {
var token = tokens.next(), style = token.style, content = token.content;
if (style == "lua-identifier" && luaKeywords.test(content)){
token.style = "lua-keyword";
}
if (style == "lua-identifier" && luaStdFunctions.test(content)){
token.style = "lua-stdfunc";
}
if (style == "lua-identifier" && luaCustomFunctions.test(content)){
token.style = "lua-customfunc";
}
if (luaIndentKeys.test(content))
indentDepth++;
else if (luaUnindentKeys.test(content))
indentDepth--;
if (content == "\n")
token.indentation = indentLUA( indentDepth, basecolumn);
return token;
},
copy: function() {
var _tokenState = tokens.state, _indentDepth = indentDepth;
return function(source) {
tokens = tokenizeLUA(source, _tokenState);
indentDepth = _indentDepth;
return iter;
};
}
};
return iter;
}
return {make: parseLUA, configure:configureLUA, electricChars: "delf})"}; //en[d] els[e] unti[l] elsei[f] // this should be taken from Keys keywords
})();
|