mode-lua.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. ace.define("ace/mode/lua_highlight_rules",[], function(require, exports, module) {
  2. "use strict";
  3. var oop = require("../lib/oop");
  4. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  5. var LuaHighlightRules = function() {
  6. var keywords = (
  7. "break|do|else|elseif|end|for|function|if|in|local|repeat|"+
  8. "return|then|until|while|or|and|not"
  9. );
  10. var builtinConstants = ("true|false|nil|_G|_VERSION");
  11. var functions = (
  12. "string|xpcall|package|tostring|print|os|unpack|require|"+
  13. "getfenv|setmetatable|next|assert|tonumber|io|rawequal|"+
  14. "collectgarbage|getmetatable|module|rawset|math|debug|"+
  15. "pcall|table|newproxy|type|coroutine|_G|select|gcinfo|"+
  16. "pairs|rawget|loadstring|ipairs|_VERSION|dofile|setfenv|"+
  17. "load|error|loadfile|"+
  18. "sub|upper|len|gfind|rep|find|match|char|dump|gmatch|"+
  19. "reverse|byte|format|gsub|lower|preload|loadlib|loaded|"+
  20. "loaders|cpath|config|path|seeall|exit|setlocale|date|"+
  21. "getenv|difftime|remove|time|clock|tmpname|rename|execute|"+
  22. "lines|write|close|flush|open|output|type|read|stderr|"+
  23. "stdin|input|stdout|popen|tmpfile|log|max|acos|huge|"+
  24. "ldexp|pi|cos|tanh|pow|deg|tan|cosh|sinh|random|randomseed|"+
  25. "frexp|ceil|floor|rad|abs|sqrt|modf|asin|min|mod|fmod|log10|"+
  26. "atan2|exp|sin|atan|getupvalue|debug|sethook|getmetatable|"+
  27. "gethook|setmetatable|setlocal|traceback|setfenv|getinfo|"+
  28. "setupvalue|getlocal|getregistry|getfenv|setn|insert|getn|"+
  29. "foreachi|maxn|foreach|concat|sort|remove|resume|yield|"+
  30. "status|wrap|create|running|"+
  31. "__add|__sub|__mod|__unm|__concat|__lt|__index|__call|__gc|__metatable|"+
  32. "__mul|__div|__pow|__len|__eq|__le|__newindex|__tostring|__mode|__tonumber"
  33. );
  34. var stdLibaries = ("string|package|os|io|math|debug|table|coroutine");
  35. var deprecatedIn5152 = ("setn|foreach|foreachi|gcinfo|log10|maxn");
  36. var keywordMapper = this.createKeywordMapper({
  37. "keyword": keywords,
  38. "support.function": functions,
  39. "keyword.deprecated": deprecatedIn5152,
  40. "constant.library": stdLibaries,
  41. "constant.language": builtinConstants,
  42. "variable.language": "self"
  43. }, "identifier");
  44. var decimalInteger = "(?:(?:[1-9]\\d*)|(?:0))";
  45. var hexInteger = "(?:0[xX][\\dA-Fa-f]+)";
  46. var integer = "(?:" + decimalInteger + "|" + hexInteger + ")";
  47. var fraction = "(?:\\.\\d+)";
  48. var intPart = "(?:\\d+)";
  49. var pointFloat = "(?:(?:" + intPart + "?" + fraction + ")|(?:" + intPart + "\\.))";
  50. var floatNumber = "(?:" + pointFloat + ")";
  51. this.$rules = {
  52. "start" : [{
  53. stateName: "bracketedComment",
  54. onMatch : function(value, currentState, stack){
  55. stack.unshift(this.next, value.length - 2, currentState);
  56. return "comment";
  57. },
  58. regex : /\-\-\[=*\[/,
  59. next : [
  60. {
  61. onMatch : function(value, currentState, stack) {
  62. if (value.length == stack[1]) {
  63. stack.shift();
  64. stack.shift();
  65. this.next = stack.shift();
  66. } else {
  67. this.next = "";
  68. }
  69. return "comment";
  70. },
  71. regex : /\]=*\]/,
  72. next : "start"
  73. }, {
  74. defaultToken : "comment"
  75. }
  76. ]
  77. },
  78. {
  79. token : "comment",
  80. regex : "\\-\\-.*$"
  81. },
  82. {
  83. stateName: "bracketedString",
  84. onMatch : function(value, currentState, stack){
  85. stack.unshift(this.next, value.length, currentState);
  86. return "string.start";
  87. },
  88. regex : /\[=*\[/,
  89. next : [
  90. {
  91. onMatch : function(value, currentState, stack) {
  92. if (value.length == stack[1]) {
  93. stack.shift();
  94. stack.shift();
  95. this.next = stack.shift();
  96. } else {
  97. this.next = "";
  98. }
  99. return "string.end";
  100. },
  101. regex : /\]=*\]/,
  102. next : "start"
  103. }, {
  104. defaultToken : "string"
  105. }
  106. ]
  107. },
  108. {
  109. token : "string", // " string
  110. regex : '"(?:[^\\\\]|\\\\.)*?"'
  111. }, {
  112. token : "string", // ' string
  113. regex : "'(?:[^\\\\]|\\\\.)*?'"
  114. }, {
  115. token : "constant.numeric", // float
  116. regex : floatNumber
  117. }, {
  118. token : "constant.numeric", // integer
  119. regex : integer + "\\b"
  120. }, {
  121. token : keywordMapper,
  122. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
  123. }, {
  124. token : "keyword.operator",
  125. regex : "\\+|\\-|\\*|\\/|%|\\#|\\^|~|<|>|<=|=>|==|~=|=|\\:|\\.\\.\\.|\\.\\."
  126. }, {
  127. token : "paren.lparen",
  128. regex : "[\\[\\(\\{]"
  129. }, {
  130. token : "paren.rparen",
  131. regex : "[\\]\\)\\}]"
  132. }, {
  133. token : "text",
  134. regex : "\\s+|\\w+"
  135. } ]
  136. };
  137. this.normalizeRules();
  138. };
  139. oop.inherits(LuaHighlightRules, TextHighlightRules);
  140. exports.LuaHighlightRules = LuaHighlightRules;
  141. });
  142. ace.define("ace/mode/folding/lua",[], function(require, exports, module) {
  143. "use strict";
  144. var oop = require("../../lib/oop");
  145. var BaseFoldMode = require("./fold_mode").FoldMode;
  146. var Range = require("../../range").Range;
  147. var TokenIterator = require("../../token_iterator").TokenIterator;
  148. var FoldMode = exports.FoldMode = function() {};
  149. oop.inherits(FoldMode, BaseFoldMode);
  150. (function() {
  151. this.foldingStartMarker = /\b(function|then|do|repeat)\b|{\s*$|(\[=*\[)/;
  152. this.foldingStopMarker = /\bend\b|^\s*}|\]=*\]/;
  153. this.getFoldWidget = function(session, foldStyle, row) {
  154. var line = session.getLine(row);
  155. var isStart = this.foldingStartMarker.test(line);
  156. var isEnd = this.foldingStopMarker.test(line);
  157. if (isStart && !isEnd) {
  158. var match = line.match(this.foldingStartMarker);
  159. if (match[1] == "then" && /\belseif\b/.test(line))
  160. return;
  161. if (match[1]) {
  162. if (session.getTokenAt(row, match.index + 1).type === "keyword")
  163. return "start";
  164. } else if (match[2]) {
  165. var type = session.bgTokenizer.getState(row) || "";
  166. if (type[0] == "bracketedComment" || type[0] == "bracketedString")
  167. return "start";
  168. } else {
  169. return "start";
  170. }
  171. }
  172. if (foldStyle != "markbeginend" || !isEnd || isStart && isEnd)
  173. return "";
  174. var match = line.match(this.foldingStopMarker);
  175. if (match[0] === "end") {
  176. if (session.getTokenAt(row, match.index + 1).type === "keyword")
  177. return "end";
  178. } else if (match[0][0] === "]") {
  179. var type = session.bgTokenizer.getState(row - 1) || "";
  180. if (type[0] == "bracketedComment" || type[0] == "bracketedString")
  181. return "end";
  182. } else
  183. return "end";
  184. };
  185. this.getFoldWidgetRange = function(session, foldStyle, row) {
  186. var line = session.doc.getLine(row);
  187. var match = this.foldingStartMarker.exec(line);
  188. if (match) {
  189. if (match[1])
  190. return this.luaBlock(session, row, match.index + 1);
  191. if (match[2])
  192. return session.getCommentFoldRange(row, match.index + 1);
  193. return this.openingBracketBlock(session, "{", row, match.index);
  194. }
  195. var match = this.foldingStopMarker.exec(line);
  196. if (match) {
  197. if (match[0] === "end") {
  198. if (session.getTokenAt(row, match.index + 1).type === "keyword")
  199. return this.luaBlock(session, row, match.index + 1);
  200. }
  201. if (match[0][0] === "]")
  202. return session.getCommentFoldRange(row, match.index + 1);
  203. return this.closingBracketBlock(session, "}", row, match.index + match[0].length);
  204. }
  205. };
  206. this.luaBlock = function(session, row, column) {
  207. var stream = new TokenIterator(session, row, column);
  208. var indentKeywords = {
  209. "function": 1,
  210. "do": 1,
  211. "then": 1,
  212. "elseif": -1,
  213. "end": -1,
  214. "repeat": 1,
  215. "until": -1
  216. };
  217. var token = stream.getCurrentToken();
  218. if (!token || token.type != "keyword")
  219. return;
  220. var val = token.value;
  221. var stack = [val];
  222. var dir = indentKeywords[val];
  223. if (!dir)
  224. return;
  225. var startColumn = dir === -1 ? stream.getCurrentTokenColumn() : session.getLine(row).length;
  226. var startRow = row;
  227. stream.step = dir === -1 ? stream.stepBackward : stream.stepForward;
  228. while(token = stream.step()) {
  229. if (token.type !== "keyword")
  230. continue;
  231. var level = dir * indentKeywords[token.value];
  232. if (level > 0) {
  233. stack.unshift(token.value);
  234. } else if (level <= 0) {
  235. stack.shift();
  236. if (!stack.length && token.value != "elseif")
  237. break;
  238. if (level === 0)
  239. stack.unshift(token.value);
  240. }
  241. }
  242. var row = stream.getCurrentTokenRow();
  243. if (dir === -1)
  244. return new Range(row, session.getLine(row).length, startRow, startColumn);
  245. else
  246. return new Range(startRow, startColumn, row, stream.getCurrentTokenColumn());
  247. };
  248. }).call(FoldMode.prototype);
  249. });
  250. ace.define("ace/mode/lua",[], function(require, exports, module) {
  251. "use strict";
  252. var oop = require("../lib/oop");
  253. var TextMode = require("./text").Mode;
  254. var LuaHighlightRules = require("./lua_highlight_rules").LuaHighlightRules;
  255. var LuaFoldMode = require("./folding/lua").FoldMode;
  256. var Range = require("../range").Range;
  257. var WorkerClient = require("../worker/worker_client").WorkerClient;
  258. var Mode = function() {
  259. this.HighlightRules = LuaHighlightRules;
  260. this.foldingRules = new LuaFoldMode();
  261. this.$behaviour = this.$defaultBehaviour;
  262. };
  263. oop.inherits(Mode, TextMode);
  264. (function() {
  265. this.lineCommentStart = "--";
  266. this.blockComment = {start: "--[", end: "]--"};
  267. var indentKeywords = {
  268. "function": 1,
  269. "then": 1,
  270. "do": 1,
  271. "else": 1,
  272. "elseif": 1,
  273. "repeat": 1,
  274. "end": -1,
  275. "until": -1
  276. };
  277. var outdentKeywords = [
  278. "else",
  279. "elseif",
  280. "end",
  281. "until"
  282. ];
  283. function getNetIndentLevel(tokens) {
  284. var level = 0;
  285. for (var i = 0; i < tokens.length; i++) {
  286. var token = tokens[i];
  287. if (token.type == "keyword") {
  288. if (token.value in indentKeywords) {
  289. level += indentKeywords[token.value];
  290. }
  291. } else if (token.type == "paren.lparen") {
  292. level += token.value.length;
  293. } else if (token.type == "paren.rparen") {
  294. level -= token.value.length;
  295. }
  296. }
  297. if (level < 0) {
  298. return -1;
  299. } else if (level > 0) {
  300. return 1;
  301. } else {
  302. return 0;
  303. }
  304. }
  305. this.getNextLineIndent = function(state, line, tab) {
  306. var indent = this.$getIndent(line);
  307. var level = 0;
  308. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  309. var tokens = tokenizedLine.tokens;
  310. if (state == "start") {
  311. level = getNetIndentLevel(tokens);
  312. }
  313. if (level > 0) {
  314. return indent + tab;
  315. } else if (level < 0 && indent.substr(indent.length - tab.length) == tab) {
  316. if (!this.checkOutdent(state, line, "\n")) {
  317. return indent.substr(0, indent.length - tab.length);
  318. }
  319. }
  320. return indent;
  321. };
  322. this.checkOutdent = function(state, line, input) {
  323. if (input != "\n" && input != "\r" && input != "\r\n")
  324. return false;
  325. if (line.match(/^\s*[\)\}\]]$/))
  326. return true;
  327. var tokens = this.getTokenizer().getLineTokens(line.trim(), state).tokens;
  328. if (!tokens || !tokens.length)
  329. return false;
  330. return (tokens[0].type == "keyword" && outdentKeywords.indexOf(tokens[0].value) != -1);
  331. };
  332. this.autoOutdent = function(state, session, row) {
  333. var prevLine = session.getLine(row - 1);
  334. var prevIndent = this.$getIndent(prevLine).length;
  335. var prevTokens = this.getTokenizer().getLineTokens(prevLine, "start").tokens;
  336. var tabLength = session.getTabString().length;
  337. var expectedIndent = prevIndent + tabLength * getNetIndentLevel(prevTokens);
  338. var curIndent = this.$getIndent(session.getLine(row)).length;
  339. if (curIndent <= expectedIndent) {
  340. return;
  341. }
  342. session.outdentRows(new Range(row, 0, row + 2, 0));
  343. };
  344. this.createWorker = function(session) {
  345. var worker = new WorkerClient(["ace"], "ace/mode/lua_worker", "Worker");
  346. worker.attachToDocument(session.getDocument());
  347. worker.on("annotate", function(e) {
  348. session.setAnnotations(e.data);
  349. });
  350. worker.on("terminate", function() {
  351. session.clearAnnotations();
  352. });
  353. return worker;
  354. };
  355. this.$id = "ace/mode/lua";
  356. }).call(Mode.prototype);
  357. exports.Mode = Mode;
  358. });
  359. (function() {
  360. ace.require(["ace/mode/lua"], function(m) {
  361. if (typeof module == "object" && typeof exports == "object" && module) {
  362. module.exports = m;
  363. }
  364. });
  365. })();