1
// LUA mode. Ported to CodeMirror 2 from Franciszek Wawrzak's
3
// highlights keywords, strings, comments (no leveling supported! ("[==[")), tokens, basic indenting
5
CodeMirror.defineMode("lua", function(config, parserConfig) {
6
var indentUnit = config.indentUnit;
8
function prefixRE(words) {
9
return new RegExp("^(?:" + words.join("|") + ")", "i");
11
function wordRE(words) {
12
return new RegExp("^(?:" + words.join("|") + ")$", "i");
14
var specials = wordRE(parserConfig.specials || []);
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",
22
"coroutine.create","coroutine.resume","coroutine.running","coroutine.status","coroutine.wrap","coroutine.yield",
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",
28
"close","flush","lines","read","seek","setvbuf","write",
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",
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",
38
"os.clock","os.date","os.difftime","os.execute","os.exit","os.getenv","os.remove","os.rename","os.setlocale",
39
"os.time","os.tmpname",
41
"package.cpath","package.loaded","package.loaders","package.loadlib","package.path","package.preload",
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",
47
"table.concat","table.insert","table.maxn","table.remove","table.sort"
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" ]);
53
var indentTokens = wordRE(["function", "if","repeat","do", "\\(", "{"]);
54
var dedentTokens = wordRE(["end", "until", "\\)", "}"]);
55
var dedentPartial = prefixRE(["end", "until", "\\)", "}", "else", "elseif"]);
57
function readBracket(stream) {
59
while (stream.eat("=")) ++level;
64
function normal(stream, state) {
65
var ch = stream.next();
66
if (ch == "-" && stream.eat("-")) {
68
return (state.cur = bracketed(readBracket(stream), "comment"))(stream, state);
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);
77
stream.eatWhile(/[\w.%]/);
80
if (/[\w_]/.test(ch)) {
81
stream.eatWhile(/[\w\\\-_.]/);
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; }
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 == "\\";
107
if (!escaped) state.cur = normal;
113
startState: function(basecol) {
114
return {basecol: basecol || 0, indentDepth: 0, cur: normal};
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";
126
if ((style != "comment") && (style != "string")){
127
if (indentTokens.test(word)) ++state.indentDepth;
128
else if (dedentTokens.test(word)) --state.indentDepth;
133
indent: function(state, textAfter) {
134
var closing = dedentPartial.test(textAfter);
135
return state.basecol + indentUnit * (state.indentDepth - (closing ? 1 : 0));
140
CodeMirror.defineMIME("text/x-lua", "lua");