Debian Codegraph @

codemirror-legacy-modes/mode/lua.js

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