1
0

lua.js 5.2 KB

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