3
Written for Lua 5.1, based on parsecss and other parsers.
4
features: highlights keywords, strings, comments (no leveling supported! ("[==[")),tokens, basic indenting
6
to make this parser highlight your special functions pass table with this functions names to parserConfig argument of creator,
8
parserConfig: ["myfunction1","myfunction2"],
12
function findFirstRegexp(words) {
13
return new RegExp("^(?:" + words.join("|") + ")", "i");
16
function matchRegexp(words) {
17
return new RegExp("^(?:" + words.join("|") + ")$", "i");
22
var luaCustomFunctions= matchRegexp([]);
24
function configureLUA(parserConfig){
26
luaCustomFunctions= matchRegexp(parserConfig);
30
//long list of standard functions from lua manual
31
var luaStdFunctions = matchRegexp([
32
"_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",
34
"coroutine.create","coroutine.resume","coroutine.running","coroutine.status","coroutine.wrap","coroutine.yield",
36
"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",
38
"close","flush","lines","read","seek","setvbuf","write",
40
"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",
42
"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",
44
"os.clock","os.date","os.difftime","os.execute","os.exit","os.getenv","os.remove","os.rename","os.setlocale","os.time","os.tmpname",
46
"package.cpath","package.loaded","package.loaders","package.loadlib","package.path","package.preload","package.seeall",
48
"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",
50
"table.concat","table.insert","table.maxn","table.remove","table.sort"
55
var luaKeywords = matchRegexp(["and","break","elseif","false","nil","not","or","return",
56
"true","function", "end", "if", "then", "else", "do",
57
"while", "repeat", "until", "for", "in", "local" ]);
59
var luaIndentKeys = matchRegexp(["function", "if","repeat","for","while", "[\(]", "{"]);
60
var luaUnindentKeys = matchRegexp(["end", "until", "[\)]", "}"]);
62
var luaUnindentKeys2 = findFirstRegexp(["end", "until", "[\)]", "}"]);
63
var luaMiddleKeys = findFirstRegexp(["else","elseif"]);
67
var LUAParser = Editor.Parser = (function() {
68
var tokenizeLUA = (function() {
69
function normal(source, setState) {
70
var ch = source.next();
72
if (ch == "-" && source.equals("-")) {
74
setState(inSLComment);
77
else if (ch == "\"" || ch == "'") {
78
setState(inString(ch));
81
if (ch == "[" && (source.equals("[") || source.equals("="))) {
83
while(source.equals("=")){
87
if(! source.equals("[") )
89
setState(inMLSomething(level,"lua-string"));
94
if (source.equals("="))
100
if (source.equals("."))
102
if (source.equals("."))
107
else if (ch == "+" || ch == "-" || ch == "*" || ch == "/" || ch == "%" || ch == "^" || ch == "#" ) {
110
else if (ch == ">" || ch == "<" || ch == "(" || ch == ")" || ch == "{" || ch == "}" || ch == "[" ) {
113
else if (ch == "]" || ch == ";" || ch == ":" || ch == ",") {
116
else if (source.equals("=") && (ch == "~" || ch == "<" || ch == ">")) {
121
else if (/\d/.test(ch)) {
122
source.nextWhileMatches(/[\w.%]/);
126
source.nextWhileMatches(/[\w\\\-_.]/);
127
return "lua-identifier";
131
function inSLComment(source, setState) {
134
while (!source.endOfLine()) {
135
var ch = source.next();
137
if ((ch =="[") && start)
138
while(source.equals("=")){
142
if (source.equals("[")){
143
setState(inMLSomething(level,"lua-comment"));
149
return "lua-comment";
153
function inMLSomething(level,what) {
154
//wat sholud be "lua-string" or "lua-comment", level is the number of "=" in opening mark.
155
return function(source, setState){
157
while (!source.endOfLine()) {
158
var ch = source.next();
159
if (dashes == level+1 && ch == "]" ) {
164
dashes = (ch == "]") ? 1:0;
166
dashes = (ch == "=") ? dashes + 1 : 0;
173
function inString(quote) {
174
return function(source, setState) {
176
while (!source.endOfLine()) {
177
var ch = source.next();
178
if (ch == quote && !escaped)
180
escaped = !escaped && ch == "\\";
188
return function(source, startState) {
189
return tokenizer(source, startState || normal);
193
function indentLUA(indentDepth, base) {
194
return function(nextChars) {
196
var closing = (luaUnindentKeys2.test(nextChars) || luaMiddleKeys.test(nextChars));
199
return base + ( indentUnit * (indentDepth - (closing?1:0)) );
204
function parseLUA(source,basecolumn) {
205
basecolumn = basecolumn || 0;
207
var tokens = tokenizeLUA(source);
212
var token = tokens.next(), style = token.style, content = token.content;
216
if (style == "lua-identifier" && luaKeywords.test(content)){
217
token.style = "lua-keyword";
219
if (style == "lua-identifier" && luaStdFunctions.test(content)){
220
token.style = "lua-stdfunc";
222
if (style == "lua-identifier" && luaCustomFunctions.test(content)){
223
token.style = "lua-customfunc";
226
if (luaIndentKeys.test(content))
228
else if (luaUnindentKeys.test(content))
233
token.indentation = indentLUA( indentDepth, basecolumn);
239
var _tokenState = tokens.state, _indentDepth = indentDepth;
240
return function(source) {
241
tokens = tokenizeLUA(source, _tokenState);
243
indentDepth = _indentDepth;
251
return {make: parseLUA, configure:configureLUA, electricChars: "delf})"}; //en[d] els[e] unti[l] elsei[f] // this should be taken from Keys keywords