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