1c396939d503de7cc34ff9e809ed92ee.js 15 KB

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