Fix: merge conflict
[myslice.git] / third-party / codemirror-3.15 / mode / lua / lua.js
1 // LUA mode. Ported to CodeMirror 2 from Franciszek Wawrzak's
2 // CodeMirror 1 mode.
3 // highlights keywords, strings, comments (no leveling supported! ("[==[")), tokens, basic indenting
4
5 CodeMirror.defineMode("lua", function(config, parserConfig) {
6   var indentUnit = config.indentUnit;
7
8   function prefixRE(words) {
9     return new RegExp("^(?:" + words.join("|") + ")", "i");
10   }
11   function wordRE(words) {
12     return new RegExp("^(?:" + words.join("|") + ")$", "i");
13   }
14   var specials = wordRE(parserConfig.specials || []);
15
16   // long list of standard functions from lua manual
17   var builtins = wordRE([
18     "_G","_VERSION","assert","collectgarbage","dofile","error","getfenv","getmetatable","ipairs","load",
19     "loadfile","loadstring","module","next","pairs","pcall","print","rawequal","rawget","rawset","require",
20     "select","setfenv","setmetatable","tonumber","tostring","type","unpack","xpcall",
21
22     "coroutine.create","coroutine.resume","coroutine.running","coroutine.status","coroutine.wrap","coroutine.yield",
23
24     "debug.debug","debug.getfenv","debug.gethook","debug.getinfo","debug.getlocal","debug.getmetatable",
25     "debug.getregistry","debug.getupvalue","debug.setfenv","debug.sethook","debug.setlocal","debug.setmetatable",
26     "debug.setupvalue","debug.traceback",
27
28     "close","flush","lines","read","seek","setvbuf","write",
29
30     "io.close","io.flush","io.input","io.lines","io.open","io.output","io.popen","io.read","io.stderr","io.stdin",
31     "io.stdout","io.tmpfile","io.type","io.write",
32
33     "math.abs","math.acos","math.asin","math.atan","math.atan2","math.ceil","math.cos","math.cosh","math.deg",
34     "math.exp","math.floor","math.fmod","math.frexp","math.huge","math.ldexp","math.log","math.log10","math.max",
35     "math.min","math.modf","math.pi","math.pow","math.rad","math.random","math.randomseed","math.sin","math.sinh",
36     "math.sqrt","math.tan","math.tanh",
37
38     "os.clock","os.date","os.difftime","os.execute","os.exit","os.getenv","os.remove","os.rename","os.setlocale",
39     "os.time","os.tmpname",
40
41     "package.cpath","package.loaded","package.loaders","package.loadlib","package.path","package.preload",
42     "package.seeall",
43
44     "string.byte","string.char","string.dump","string.find","string.format","string.gmatch","string.gsub",
45     "string.len","string.lower","string.match","string.rep","string.reverse","string.sub","string.upper",
46
47     "table.concat","table.insert","table.maxn","table.remove","table.sort"
48   ]);
49   var keywords = wordRE(["and","break","elseif","false","nil","not","or","return",
50                          "true","function", "end", "if", "then", "else", "do",
51                          "while", "repeat", "until", "for", "in", "local" ]);
52
53   var indentTokens = wordRE(["function", "if","repeat","do", "\\(", "{"]);
54   var dedentTokens = wordRE(["end", "until", "\\)", "}"]);
55   var dedentPartial = prefixRE(["end", "until", "\\)", "}", "else", "elseif"]);
56
57   function readBracket(stream) {
58     var level = 0;
59     while (stream.eat("=")) ++level;
60     stream.eat("[");
61     return level;
62   }
63
64   function normal(stream, state) {
65     var ch = stream.next();
66     if (ch == "-" && stream.eat("-")) {
67       if (stream.eat("[") && stream.eat("["))
68         return (state.cur = bracketed(readBracket(stream), "comment"))(stream, state);
69       stream.skipToEnd();
70       return "comment";
71     }
72     if (ch == "\"" || ch == "'")
73       return (state.cur = string(ch))(stream, state);
74     if (ch == "[" && /[\[=]/.test(stream.peek()))
75       return (state.cur = bracketed(readBracket(stream), "string"))(stream, state);
76     if (/\d/.test(ch)) {
77       stream.eatWhile(/[\w.%]/);
78       return "number";
79     }
80     if (/[\w_]/.test(ch)) {
81       stream.eatWhile(/[\w\\\-_.]/);
82       return "variable";
83     }
84     return null;
85   }
86
87   function bracketed(level, style) {
88     return function(stream, state) {
89       var curlev = null, ch;
90       while ((ch = stream.next()) != null) {
91         if (curlev == null) {if (ch == "]") curlev = 0;}
92         else if (ch == "=") ++curlev;
93         else if (ch == "]" && curlev == level) { state.cur = normal; break; }
94         else curlev = null;
95       }
96       return style;
97     };
98   }
99
100   function string(quote) {
101     return function(stream, state) {
102       var escaped = false, ch;
103       while ((ch = stream.next()) != null) {
104         if (ch == quote && !escaped) break;
105         escaped = !escaped && ch == "\\";
106       }
107       if (!escaped) state.cur = normal;
108       return "string";
109     };
110   }
111
112   return {
113     startState: function(basecol) {
114       return {basecol: basecol || 0, indentDepth: 0, cur: normal};
115     },
116
117     token: function(stream, state) {
118       if (stream.eatSpace()) return null;
119       var style = state.cur(stream, state);
120       var word = stream.current();
121       if (style == "variable") {
122         if (keywords.test(word)) style = "keyword";
123         else if (builtins.test(word)) style = "builtin";
124         else if (specials.test(word)) style = "variable-2";
125       }
126       if ((style != "comment") && (style != "string")){
127         if (indentTokens.test(word)) ++state.indentDepth;
128         else if (dedentTokens.test(word)) --state.indentDepth;
129       }
130       return style;
131     },
132
133     indent: function(state, textAfter) {
134       var closing = dedentPartial.test(textAfter);
135       return state.basecol + indentUnit * (state.indentDepth - (closing ? 1 : 0));
136     },
137
138     lineComment: "--",
139     blockCommentStart: "--[[",
140     blockCommentEnd: "]]"
141   };
142 });
143
144 CodeMirror.defineMIME("text/x-lua", "lua");