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