lua.js 5.7 KB

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