all repos — NoPaste @ 3e3c433f026c49e04d0efa0f0f39fc688ed32bb1

Resurrected - The PussTheCat.org fork of NoPaste

scripts/CodeMirror/mode/lua/lua.js (view raw)

  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) {
  9  if (typeof exports == "object" && typeof module == "object") // CommonJS
 10    mod(require("../../lib/codemirror"));
 11  else if (typeof define == "function" && define.amd) // AMD
 12    define(["../../lib/codemirror"], mod);
 13  else // Plain browser env
 14    mod(CodeMirror);
 15})(function(CodeMirror) {
 16"use strict";
 17
 18CodeMirror.defineMode("lua", function(config, parserConfig) {
 19  var indentUnit = config.indentUnit;
 20
 21  function prefixRE(words) {
 22    return new RegExp("^(?:" + words.join("|") + ")", "i");
 23  }
 24  function wordRE(words) {
 25    return new RegExp("^(?:" + words.join("|") + ")$", "i");
 26  }
 27  var specials = wordRE(parserConfig.specials || []);
 28
 29  // long list of standard functions from lua manual
 30  var 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  ]);
 62  var 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
 66  var indentTokens = wordRE(["function", "if","repeat","do", "\\(", "{"]);
 67  var dedentTokens = wordRE(["end", "until", "\\)", "}"]);
 68  var dedentPartial = prefixRE(["end", "until", "\\)", "}", "else", "elseif"]);
 69
 70  function readBracket(stream) {
 71    var level = 0;
 72    while (stream.eat("=")) ++level;
 73    stream.eat("[");
 74    return level;
 75  }
 76
 77  function normal(stream, state) {
 78    var ch = stream.next();
 79    if (ch == "-" && stream.eat("-")) {
 80      if (stream.eat("[") && stream.eat("["))
 81        return (state.cur = bracketed(readBracket(stream), "comment"))(stream, state);
 82      stream.skipToEnd();
 83      return "comment";
 84    }
 85    if (ch == "\"" || ch == "'")
 86      return (state.cur = string(ch))(stream, state);
 87    if (ch == "[" && /[\[=]/.test(stream.peek()))
 88      return (state.cur = bracketed(readBracket(stream), "string"))(stream, state);
 89    if (/\d/.test(ch)) {
 90      stream.eatWhile(/[\w.%]/);
 91      return "number";
 92    }
 93    if (/[\w_]/.test(ch)) {
 94      stream.eatWhile(/[\w\\\-_.]/);
 95      return "variable";
 96    }
 97    return null;
 98  }
 99
100  function bracketed(level, style) {
101    return function(stream, state) {
102      var curlev = null, ch;
103      while ((ch = stream.next()) != null) {
104        if (curlev == null) {if (ch == "]") curlev = 0;}
105        else if (ch == "=") ++curlev;
106        else if (ch == "]" && curlev == level) { state.cur = normal; break; }
107        else curlev = null;
108      }
109      return style;
110    };
111  }
112
113  function string(quote) {
114    return function(stream, state) {
115      var escaped = false, ch;
116      while ((ch = stream.next()) != null) {
117        if (ch == quote && !escaped) break;
118        escaped = !escaped && ch == "\\";
119      }
120      if (!escaped) state.cur = normal;
121      return "string";
122    };
123  }
124
125  return {
126    startState: function(basecol) {
127      return {basecol: basecol || 0, indentDepth: 0, cur: normal};
128    },
129
130    token: function(stream, state) {
131      if (stream.eatSpace()) return null;
132      var style = state.cur(stream, state);
133      var word = stream.current();
134      if (style == "variable") {
135        if (keywords.test(word)) style = "keyword";
136        else if (builtins.test(word)) style = "builtin";
137        else if (specials.test(word)) style = "variable-2";
138      }
139      if ((style != "comment") && (style != "string")){
140        if (indentTokens.test(word)) ++state.indentDepth;
141        else if (dedentTokens.test(word)) --state.indentDepth;
142      }
143      return style;
144    },
145
146    indent: function(state, textAfter) {
147      var closing = dedentPartial.test(textAfter);
148      return state.basecol + indentUnit * (state.indentDepth - (closing ? 1 : 0));
149    },
150
151    lineComment: "--",
152    blockCommentStart: "--[[",
153    blockCommentEnd: "]]"
154  };
155});
156
157CodeMirror.defineMIME("text/x-lua", "lua");
158
159});