mode-lua.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. ace.define("ace/mode/lua_highlight_rules",["require","exports","module","ace/lib/oop","ace/mode/text_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",["require","exports","module","ace/lib/oop","ace/mode/folding/fold_mode","ace/range","ace/token_iterator"], 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, tokenRange) {
  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. if (!token)
  243. return null;
  244. if (tokenRange)
  245. return stream.getCurrentTokenRange();
  246. var row = stream.getCurrentTokenRow();
  247. if (dir === -1)
  248. return new Range(row, session.getLine(row).length, startRow, startColumn);
  249. else
  250. return new Range(startRow, startColumn, row, stream.getCurrentTokenColumn());
  251. };
  252. }).call(FoldMode.prototype);
  253. });
  254. ace.define("ace/mode/lua",["require","exports","module","ace/lib/oop","ace/mode/text","ace/mode/lua_highlight_rules","ace/mode/folding/lua","ace/range","ace/worker/worker_client"], function(require, exports, module) {
  255. "use strict";
  256. var oop = require("../lib/oop");
  257. var TextMode = require("./text").Mode;
  258. var LuaHighlightRules = require("./lua_highlight_rules").LuaHighlightRules;
  259. var LuaFoldMode = require("./folding/lua").FoldMode;
  260. var Range = require("../range").Range;
  261. var WorkerClient = require("../worker/worker_client").WorkerClient;
  262. var Mode = function() {
  263. this.HighlightRules = LuaHighlightRules;
  264. this.foldingRules = new LuaFoldMode();
  265. this.$behaviour = this.$defaultBehaviour;
  266. };
  267. oop.inherits(Mode, TextMode);
  268. (function() {
  269. this.lineCommentStart = "--";
  270. this.blockComment = {start: "--[", end: "]--"};
  271. var indentKeywords = {
  272. "function": 1,
  273. "then": 1,
  274. "do": 1,
  275. "else": 1,
  276. "elseif": 1,
  277. "repeat": 1,
  278. "end": -1,
  279. "until": -1
  280. };
  281. var outdentKeywords = [
  282. "else",
  283. "elseif",
  284. "end",
  285. "until"
  286. ];
  287. function getNetIndentLevel(tokens) {
  288. var level = 0;
  289. for (var i = 0; i < tokens.length; i++) {
  290. var token = tokens[i];
  291. if (token.type == "keyword") {
  292. if (token.value in indentKeywords) {
  293. level += indentKeywords[token.value];
  294. }
  295. } else if (token.type == "paren.lparen") {
  296. level += token.value.length;
  297. } else if (token.type == "paren.rparen") {
  298. level -= token.value.length;
  299. }
  300. }
  301. if (level < 0) {
  302. return -1;
  303. } else if (level > 0) {
  304. return 1;
  305. } else {
  306. return 0;
  307. }
  308. }
  309. this.getNextLineIndent = function(state, line, tab) {
  310. var indent = this.$getIndent(line);
  311. var level = 0;
  312. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  313. var tokens = tokenizedLine.tokens;
  314. if (state == "start") {
  315. level = getNetIndentLevel(tokens);
  316. }
  317. if (level > 0) {
  318. return indent + tab;
  319. } else if (level < 0 && indent.substr(indent.length - tab.length) == tab) {
  320. if (!this.checkOutdent(state, line, "\n")) {
  321. return indent.substr(0, indent.length - tab.length);
  322. }
  323. }
  324. return indent;
  325. };
  326. this.checkOutdent = function(state, line, input) {
  327. if (input != "\n" && input != "\r" && input != "\r\n")
  328. return false;
  329. if (line.match(/^\s*[\)\}\]]$/))
  330. return true;
  331. var tokens = this.getTokenizer().getLineTokens(line.trim(), state).tokens;
  332. if (!tokens || !tokens.length)
  333. return false;
  334. return (tokens[0].type == "keyword" && outdentKeywords.indexOf(tokens[0].value) != -1);
  335. };
  336. this.getMatching = function(session, row, column) {
  337. if (row == undefined) {
  338. var pos = session.selection.lead;
  339. column = pos.column;
  340. row = pos.row;
  341. }
  342. var startToken = session.getTokenAt(row, column);
  343. if (startToken && startToken.value in indentKeywords)
  344. return this.foldingRules.luaBlock(session, row, column, true);
  345. };
  346. this.autoOutdent = function(state, session, row) {
  347. var line = session.getLine(row);
  348. var column = line.match(/^\s*/)[0].length;
  349. if (!column || !row) return;
  350. var startRange = this.getMatching(session, row, column + 1);
  351. if (!startRange || startRange.start.row == row)
  352. return;
  353. var indent = this.$getIndent(session.getLine(startRange.start.row));
  354. if (indent.length != column) {
  355. session.replace(new Range(row, 0, row, column), indent);
  356. session.outdentRows(new Range(row + 1, 0, row + 1, 0));
  357. }
  358. };
  359. this.createWorker = function(session) {
  360. var worker = new WorkerClient(["ace"], "ace/mode/lua_worker", "Worker");
  361. worker.attachToDocument(session.getDocument());
  362. worker.on("annotate", function(e) {
  363. session.setAnnotations(e.data);
  364. });
  365. worker.on("terminate", function() {
  366. session.clearAnnotations();
  367. });
  368. return worker;
  369. };
  370. this.$id = "ace/mode/lua";
  371. this.snippetFileId = "ace/snippets/lua";
  372. }).call(Mode.prototype);
  373. exports.Mode = Mode;
  374. }); (function() {
  375. ace.require(["ace/mode/lua"], function(m) {
  376. if (typeof module == "object" && typeof exports == "object" && module) {
  377. module.exports = m;
  378. }
  379. });
  380. })();