2
0

haxe.js 15 KB

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