haxe.js 15 KB

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