You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

516 lines
17KB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode("haxe", function(config, parserConfig) {
  13. var indentUnit = config.indentUnit;
  14. // Tokenizer
  15. function kw(type) {return {type: type, style: "keyword"};}
  16. var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
  17. var operator = kw("operator"), atom = {type: "atom", style: "atom"}, attribute = {type:"attribute", style: "attribute"};
  18. var type = kw("typedef");
  19. var keywords = {
  20. "if": A, "while": A, "else": B, "do": B, "try": B,
  21. "return": C, "break": C, "continue": C, "new": C, "throw": C,
  22. "var": kw("var"), "inline":attribute, "static": attribute, "using":kw("import"),
  23. "public": attribute, "private": attribute, "cast": kw("cast"), "import": kw("import"), "macro": kw("macro"),
  24. "function": kw("function"), "catch": kw("catch"), "untyped": kw("untyped"), "callback": kw("cb"),
  25. "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
  26. "in": operator, "never": kw("property_access"), "trace":kw("trace"),
  27. "class": type, "abstract":type, "enum":type, "interface":type, "typedef":type, "extends":type, "implements":type, "dynamic":type,
  28. "true": atom, "false": atom, "null": atom
  29. };
  30. var isOperatorChar = /[+\-*&%=<>!?|]/;
  31. function chain(stream, state, f) {
  32. state.tokenize = f;
  33. return f(stream, state);
  34. }
  35. function toUnescaped(stream, end) {
  36. var escaped = false, next;
  37. while ((next = stream.next()) != null) {
  38. if (next == end && !escaped)
  39. return true;
  40. escaped = !escaped && next == "\\";
  41. }
  42. }
  43. // Used as scratch variables to communicate multiple values without
  44. // consing up tons of objects.
  45. var type, content;
  46. function ret(tp, style, cont) {
  47. type = tp; content = cont;
  48. return style;
  49. }
  50. function haxeTokenBase(stream, state) {
  51. var ch = stream.next();
  52. if (ch == '"' || ch == "'") {
  53. return chain(stream, state, haxeTokenString(ch));
  54. } else if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  55. return ret(ch);
  56. } else if (ch == "0" && stream.eat(/x/i)) {
  57. stream.eatWhile(/[\da-f]/i);
  58. return ret("number", "number");
  59. } else if (/\d/.test(ch) || ch == "-" && stream.eat(/\d/)) {
  60. stream.match(/^\d*(?:\.\d*(?!\.))?(?:[eE][+\-]?\d+)?/);
  61. return ret("number", "number");
  62. } else if (state.reAllowed && (ch == "~" && stream.eat(/\//))) {
  63. toUnescaped(stream, "/");
  64. stream.eatWhile(/[gimsu]/);
  65. return ret("regexp", "string-2");
  66. } else if (ch == "/") {
  67. if (stream.eat("*")) {
  68. return chain(stream, state, haxeTokenComment);
  69. } else if (stream.eat("/")) {
  70. stream.skipToEnd();
  71. return ret("comment", "comment");
  72. } else {
  73. stream.eatWhile(isOperatorChar);
  74. return ret("operator", null, stream.current());
  75. }
  76. } else if (ch == "#") {
  77. stream.skipToEnd();
  78. return ret("conditional", "meta");
  79. } else if (ch == "@") {
  80. stream.eat(/:/);
  81. stream.eatWhile(/[\w_]/);
  82. return ret ("metadata", "meta");
  83. } else if (isOperatorChar.test(ch)) {
  84. stream.eatWhile(isOperatorChar);
  85. return ret("operator", null, stream.current());
  86. } else {
  87. var word;
  88. if(/[A-Z]/.test(ch)) {
  89. stream.eatWhile(/[\w_<>]/);
  90. word = stream.current();
  91. return ret("type", "variable-3", word);
  92. } else {
  93. stream.eatWhile(/[\w_]/);
  94. var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];
  95. return (known && state.kwAllowed) ? ret(known.type, known.style, word) :
  96. ret("variable", "variable", word);
  97. }
  98. }
  99. }
  100. function haxeTokenString(quote) {
  101. return function(stream, state) {
  102. if (toUnescaped(stream, quote))
  103. state.tokenize = haxeTokenBase;
  104. return ret("string", "string");
  105. };
  106. }
  107. function haxeTokenComment(stream, state) {
  108. var maybeEnd = false, ch;
  109. while (ch = stream.next()) {
  110. if (ch == "/" && maybeEnd) {
  111. state.tokenize = haxeTokenBase;
  112. break;
  113. }
  114. maybeEnd = (ch == "*");
  115. }
  116. return ret("comment", "comment");
  117. }
  118. // Parser
  119. var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true};
  120. function HaxeLexical(indented, column, type, align, prev, info) {
  121. this.indented = indented;
  122. this.column = column;
  123. this.type = type;
  124. this.prev = prev;
  125. this.info = info;
  126. if (align != null) this.align = align;
  127. }
  128. function inScope(state, varname) {
  129. for (var v = state.localVars; v; v = v.next)
  130. if (v.name == varname) return true;
  131. }
  132. function parseHaxe(state, style, type, content, stream) {
  133. var cc = state.cc;
  134. // Communicate our context to the combinators.
  135. // (Less wasteful than consing up a hundred closures on every call.)
  136. cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc;
  137. if (!state.lexical.hasOwnProperty("align"))
  138. state.lexical.align = true;
  139. while(true) {
  140. var combinator = cc.length ? cc.pop() : statement;
  141. if (combinator(type, content)) {
  142. while(cc.length && cc[cc.length - 1].lex)
  143. cc.pop()();
  144. if (cx.marked) return cx.marked;
  145. if (type == "variable" && inScope(state, content)) return "variable-2";
  146. if (type == "variable" && imported(state, content)) return "variable-3";
  147. return style;
  148. }
  149. }
  150. }
  151. function imported(state, typename) {
  152. if (/[a-z]/.test(typename.charAt(0)))
  153. return false;
  154. var len = state.importedtypes.length;
  155. for (var i = 0; i<len; i++)
  156. if(state.importedtypes[i]==typename) return true;
  157. }
  158. function registerimport(importname) {
  159. var state = cx.state;
  160. for (var t = state.importedtypes; t; t = t.next)
  161. if(t.name == importname) return;
  162. state.importedtypes = { name: importname, next: state.importedtypes };
  163. }
  164. // Combinator utils
  165. var cx = {state: null, column: null, marked: null, cc: null};
  166. function pass() {
  167. for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
  168. }
  169. function cont() {
  170. pass.apply(null, arguments);
  171. return true;
  172. }
  173. function inList(name, list) {
  174. for (var v = list; v; v = v.next)
  175. if (v.name == name) return true;
  176. return false;
  177. }
  178. function register(varname) {
  179. var state = cx.state;
  180. if (state.context) {
  181. cx.marked = "def";
  182. if (inList(varname, state.localVars)) return;
  183. state.localVars = {name: varname, next: state.localVars};
  184. } else if (state.globalVars) {
  185. if (inList(varname, state.globalVars)) return;
  186. state.globalVars = {name: varname, next: state.globalVars};
  187. }
  188. }
  189. // Combinators
  190. var defaultVars = {name: "this", next: null};
  191. function pushcontext() {
  192. if (!cx.state.context) cx.state.localVars = defaultVars;
  193. cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
  194. }
  195. function popcontext() {
  196. cx.state.localVars = cx.state.context.vars;
  197. cx.state.context = cx.state.context.prev;
  198. }
  199. popcontext.lex = true;
  200. function pushlex(type, info) {
  201. var result = function() {
  202. var state = cx.state;
  203. state.lexical = new HaxeLexical(state.indented, cx.stream.column(), type, null, state.lexical, info);
  204. };
  205. result.lex = true;
  206. return result;
  207. }
  208. function poplex() {
  209. var state = cx.state;
  210. if (state.lexical.prev) {
  211. if (state.lexical.type == ")")
  212. state.indented = state.lexical.indented;
  213. state.lexical = state.lexical.prev;
  214. }
  215. }
  216. poplex.lex = true;
  217. function expect(wanted) {
  218. function f(type) {
  219. if (type == wanted) return cont();
  220. else if (wanted == ";") return pass();
  221. else return cont(f);
  222. }
  223. return f;
  224. }
  225. function statement(type) {
  226. if (type == "@") return cont(metadef);
  227. if (type == "var") return cont(pushlex("vardef"), vardef1, expect(";"), poplex);
  228. if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);
  229. if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
  230. if (type == "{") return cont(pushlex("}"), pushcontext, block, poplex, popcontext);
  231. if (type == ";") return cont();
  232. if (type == "attribute") return cont(maybeattribute);
  233. if (type == "function") return cont(functiondef);
  234. if (type == "for") return cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"),
  235. poplex, statement, poplex);
  236. if (type == "variable") return cont(pushlex("stat"), maybelabel);
  237. if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
  238. block, poplex, poplex);
  239. if (type == "case") return cont(expression, expect(":"));
  240. if (type == "default") return cont(expect(":"));
  241. if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
  242. statement, poplex, popcontext);
  243. if (type == "import") return cont(importdef, expect(";"));
  244. if (type == "typedef") return cont(typedef);
  245. return pass(pushlex("stat"), expression, expect(";"), poplex);
  246. }
  247. function expression(type) {
  248. if (atomicTypes.hasOwnProperty(type)) return cont(maybeoperator);
  249. if (type == "type" ) return cont(maybeoperator);
  250. if (type == "function") return cont(functiondef);
  251. if (type == "keyword c") return cont(maybeexpression);
  252. if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeoperator);
  253. if (type == "operator") return cont(expression);
  254. if (type == "[") return cont(pushlex("]"), commasep(maybeexpression, "]"), poplex, maybeoperator);
  255. if (type == "{") return cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeoperator);
  256. return cont();
  257. }
  258. function maybeexpression(type) {
  259. if (type.match(/[;\}\)\],]/)) return pass();
  260. return pass(expression);
  261. }
  262. function maybeoperator(type, value) {
  263. if (type == "operator" && /\+\+|--/.test(value)) return cont(maybeoperator);
  264. if (type == "operator" || type == ":") return cont(expression);
  265. if (type == ";") return;
  266. if (type == "(") return cont(pushlex(")"), commasep(expression, ")"), poplex, maybeoperator);
  267. if (type == ".") return cont(property, maybeoperator);
  268. if (type == "[") return cont(pushlex("]"), expression, expect("]"), poplex, maybeoperator);
  269. }
  270. function maybeattribute(type) {
  271. if (type == "attribute") return cont(maybeattribute);
  272. if (type == "function") return cont(functiondef);
  273. if (type == "var") return cont(vardef1);
  274. }
  275. function metadef(type) {
  276. if(type == ":") return cont(metadef);
  277. if(type == "variable") return cont(metadef);
  278. if(type == "(") return cont(pushlex(")"), commasep(metaargs, ")"), poplex, statement);
  279. }
  280. function metaargs(type) {
  281. if(type == "variable") return cont();
  282. }
  283. function importdef (type, value) {
  284. if(type == "variable" && /[A-Z]/.test(value.charAt(0))) { registerimport(value); return cont(); }
  285. else if(type == "variable" || type == "property" || type == "." || value == "*") return cont(importdef);
  286. }
  287. function typedef (type, value)
  288. {
  289. if(type == "variable" && /[A-Z]/.test(value.charAt(0))) { registerimport(value); return cont(); }
  290. else if (type == "type" && /[A-Z]/.test(value.charAt(0))) { return cont(); }
  291. }
  292. function maybelabel(type) {
  293. if (type == ":") return cont(poplex, statement);
  294. return pass(maybeoperator, expect(";"), poplex);
  295. }
  296. function property(type) {
  297. if (type == "variable") {cx.marked = "property"; return cont();}
  298. }
  299. function objprop(type) {
  300. if (type == "variable") cx.marked = "property";
  301. if (atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expression);
  302. }
  303. function commasep(what, end) {
  304. function proceed(type) {
  305. if (type == ",") return cont(what, proceed);
  306. if (type == end) return cont();
  307. return cont(expect(end));
  308. }
  309. return function(type) {
  310. if (type == end) return cont();
  311. else return pass(what, proceed);
  312. };
  313. }
  314. function block(type) {
  315. if (type == "}") return cont();
  316. return pass(statement, block);
  317. }
  318. function vardef1(type, value) {
  319. if (type == "variable"){register(value); return cont(typeuse, vardef2);}
  320. return cont();
  321. }
  322. function vardef2(type, value) {
  323. if (value == "=") return cont(expression, vardef2);
  324. if (type == ",") return cont(vardef1);
  325. }
  326. function forspec1(type, value) {
  327. if (type == "variable") {
  328. register(value);
  329. return cont(forin, expression)
  330. } else {
  331. return pass()
  332. }
  333. }
  334. function forin(_type, value) {
  335. if (value == "in") return cont();
  336. }
  337. function functiondef(type, value) {
  338. //function names starting with upper-case letters are recognised as types, so cludging them together here.
  339. if (type == "variable" || type == "type") {register(value); return cont(functiondef);}
  340. if (value == "new") return cont(functiondef);
  341. if (type == "(") return cont(pushlex(")"), pushcontext, commasep(funarg, ")"), poplex, typeuse, statement, popcontext);
  342. }
  343. function typeuse(type) {
  344. if(type == ":") return cont(typestring);
  345. }
  346. function typestring(type) {
  347. if(type == "type") return cont();
  348. if(type == "variable") return cont();
  349. if(type == "{") return cont(pushlex("}"), commasep(typeprop, "}"), poplex);
  350. }
  351. function typeprop(type) {
  352. if(type == "variable") return cont(typeuse);
  353. }
  354. function funarg(type, value) {
  355. if (type == "variable") {register(value); return cont(typeuse);}
  356. }
  357. // Interface
  358. return {
  359. startState: function(basecolumn) {
  360. var defaulttypes = ["Int", "Float", "String", "Void", "Std", "Bool", "Dynamic", "Array"];
  361. var state = {
  362. tokenize: haxeTokenBase,
  363. reAllowed: true,
  364. kwAllowed: true,
  365. cc: [],
  366. lexical: new HaxeLexical((basecolumn || 0) - indentUnit, 0, "block", false),
  367. localVars: parserConfig.localVars,
  368. importedtypes: defaulttypes,
  369. context: parserConfig.localVars && {vars: parserConfig.localVars},
  370. indented: 0
  371. };
  372. if (parserConfig.globalVars && typeof parserConfig.globalVars == "object")
  373. state.globalVars = parserConfig.globalVars;
  374. return state;
  375. },
  376. token: function(stream, state) {
  377. if (stream.sol()) {
  378. if (!state.lexical.hasOwnProperty("align"))
  379. state.lexical.align = false;
  380. state.indented = stream.indentation();
  381. }
  382. if (stream.eatSpace()) return null;
  383. var style = state.tokenize(stream, state);
  384. if (type == "comment") return style;
  385. state.reAllowed = !!(type == "operator" || type == "keyword c" || type.match(/^[\[{}\(,;:]$/));
  386. state.kwAllowed = type != '.';
  387. return parseHaxe(state, style, type, content, stream);
  388. },
  389. indent: function(state, textAfter) {
  390. if (state.tokenize != haxeTokenBase) return 0;
  391. var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;
  392. if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;
  393. var type = lexical.type, closing = firstChar == type;
  394. if (type == "vardef") return lexical.indented + 4;
  395. else if (type == "form" && firstChar == "{") return lexical.indented;
  396. else if (type == "stat" || type == "form") return lexical.indented + indentUnit;
  397. else if (lexical.info == "switch" && !closing)
  398. return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
  399. else if (lexical.align) return lexical.column + (closing ? 0 : 1);
  400. else return lexical.indented + (closing ? 0 : indentUnit);
  401. },
  402. electricChars: "{}",
  403. blockCommentStart: "/*",
  404. blockCommentEnd: "*/",
  405. lineComment: "//"
  406. };
  407. });
  408. CodeMirror.defineMIME("text/x-haxe", "haxe");
  409. CodeMirror.defineMode("hxml", function () {
  410. return {
  411. startState: function () {
  412. return {
  413. define: false,
  414. inString: false
  415. };
  416. },
  417. token: function (stream, state) {
  418. var ch = stream.peek();
  419. var sol = stream.sol();
  420. ///* comments */
  421. if (ch == "#") {
  422. stream.skipToEnd();
  423. return "comment";
  424. }
  425. if (sol && ch == "-") {
  426. var style = "variable-2";
  427. stream.eat(/-/);
  428. if (stream.peek() == "-") {
  429. stream.eat(/-/);
  430. style = "keyword a";
  431. }
  432. if (stream.peek() == "D") {
  433. stream.eat(/[D]/);
  434. style = "keyword c";
  435. state.define = true;
  436. }
  437. stream.eatWhile(/[A-Z]/i);
  438. return style;
  439. }
  440. var ch = stream.peek();
  441. if (state.inString == false && ch == "'") {
  442. state.inString = true;
  443. stream.next();
  444. }
  445. if (state.inString == true) {
  446. if (stream.skipTo("'")) {
  447. } else {
  448. stream.skipToEnd();
  449. }
  450. if (stream.peek() == "'") {
  451. stream.next();
  452. state.inString = false;
  453. }
  454. return "string";
  455. }
  456. stream.next();
  457. return null;
  458. },
  459. lineComment: "#"
  460. };
  461. });
  462. CodeMirror.defineMIME("text/x-hxml", "hxml");
  463. });