lua.js 5.8 KB

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