e2f8aa903dd13141571c656879246c94a1483070.svn-base 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. // TODO actually recognize syntax of TypeScript constructs
  4. (function(mod) {
  5. // if (typeof exports == "object" && typeof module == "object") // CommonJS
  6. // mod(require("./codemirror"));
  7. // else if (typeof define == "function" && define.amd) // AMD
  8. // define(["./codemirror"], mod);
  9. // else // Plain browser env
  10. mod(CodeMirror);
  11. })(function(CodeMirror) {
  12. "use strict";
  13. CodeMirror.defineMode("javascript", function(config, parserConfig) {
  14. var indentUnit = config.indentUnit;
  15. var statementIndent = parserConfig.statementIndent;
  16. var jsonldMode = parserConfig.jsonld;
  17. var jsonMode = parserConfig.json || jsonldMode;
  18. var isTS = parserConfig.typescript;
  19. var wordRE = parserConfig.wordCharacters || /[\w$\xa1-\uffff]/;
  20. // Tokenizer
  21. var keywords = function(){
  22. function kw(type) {return {type: type, style: "keyword"};}
  23. var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
  24. var operator = kw("operator"), atom = {type: "atom", style: "atom"};
  25. var jsKeywords = {
  26. "if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
  27. "return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C, "debugger": C,
  28. "var": kw("var"), "const": kw("var"), "let": kw("var"),
  29. "function": kw("function"), "catch": kw("catch"),
  30. "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
  31. "in": operator, "typeof": operator, "instanceof": operator,
  32. "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom,
  33. "this": kw("this"), "module": kw("module"), "class": kw("class"), "super": kw("atom"),
  34. "yield": C, "export": kw("export"), "import": kw("import"), "extends": C
  35. };
  36. // Extend the 'normal' keywords with the TypeScript language extensions
  37. if (isTS) {
  38. var type = {type: "variable", style: "variable-3"};
  39. var tsKeywords = {
  40. // object-like things
  41. "interface": kw("interface"),
  42. "extends": kw("extends"),
  43. "constructor": kw("constructor"),
  44. // scope modifiers
  45. "public": kw("public"),
  46. "private": kw("private"),
  47. "protected": kw("protected"),
  48. "static": kw("static"),
  49. // types
  50. "string": type, "number": type, "bool": type, "any": type
  51. };
  52. for (var attr in tsKeywords) {
  53. jsKeywords[attr] = tsKeywords[attr];
  54. }
  55. }
  56. return jsKeywords;
  57. }();
  58. var isOperatorChar = /[+\-*&%=<>!?|~^]/;
  59. var isJsonldKeyword = /^@(context|id|value|language|type|container|list|set|reverse|index|base|vocab|graph)"/;
  60. function readRegexp(stream) {
  61. var escaped = false, next, inSet = false;
  62. while ((next = stream.next()) != null) {
  63. if (!escaped) {
  64. if (next == "/" && !inSet) return;
  65. if (next == "[") inSet = true;
  66. else if (inSet && next == "]") inSet = false;
  67. }
  68. escaped = !escaped && next == "\\";
  69. }
  70. }
  71. // Used as scratch variables to communicate multiple values without
  72. // consing up tons of objects.
  73. var type, content;
  74. function ret(tp, style, cont) {
  75. type = tp; content = cont;
  76. return style;
  77. }
  78. function tokenBase(stream, state) {
  79. var ch = stream.next();
  80. if (ch == '"' || ch == "'") {
  81. state.tokenize = tokenString(ch);
  82. return state.tokenize(stream, state);
  83. } else if (ch == "." && stream.match(/^\d+(?:[eE][+\-]?\d+)?/)) {
  84. return ret("number", "number");
  85. } else if (ch == "." && stream.match("..")) {
  86. return ret("spread", "meta");
  87. } else if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  88. return ret(ch);
  89. } else if (ch == "=" && stream.eat(">")) {
  90. return ret("=>", "operator");
  91. } else if (ch == "0" && stream.eat(/x/i)) {
  92. stream.eatWhile(/[\da-f]/i);
  93. return ret("number", "number");
  94. } else if (/\d/.test(ch)) {
  95. stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/);
  96. return ret("number", "number");
  97. } else if (ch == "/") {
  98. if (stream.eat("*")) {
  99. state.tokenize = tokenComment;
  100. return tokenComment(stream, state);
  101. } else if (stream.eat("/")) {
  102. stream.skipToEnd();
  103. return ret("comment", "comment");
  104. } else if (state.lastType == "operator" || state.lastType == "keyword c" ||
  105. state.lastType == "sof" || /^[\[{}\(,;:]$/.test(state.lastType)) {
  106. readRegexp(stream);
  107. stream.match(/^\b(([gimyu])(?![gimyu]*\2))+\b/);
  108. return ret("regexp", "string-2");
  109. } else {
  110. stream.eatWhile(isOperatorChar);
  111. return ret("operator", "operator", stream.current());
  112. }
  113. } else if (ch == "`") {
  114. state.tokenize = tokenQuasi;
  115. return tokenQuasi(stream, state);
  116. } else if (ch == "#") {
  117. stream.skipToEnd();
  118. return ret("error", "error");
  119. } else if (isOperatorChar.test(ch)) {
  120. stream.eatWhile(isOperatorChar);
  121. return ret("operator", "operator", stream.current());
  122. } else if (wordRE.test(ch)) {
  123. stream.eatWhile(wordRE);
  124. var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];
  125. return (known && state.lastType != ".") ? ret(known.type, known.style, word) :
  126. ret("variable", "variable", word);
  127. }
  128. }
  129. function tokenString(quote) {
  130. return function(stream, state) {
  131. var escaped = false, next;
  132. if (jsonldMode && stream.peek() == "@" && stream.match(isJsonldKeyword)){
  133. state.tokenize = tokenBase;
  134. return ret("jsonld-keyword", "meta");
  135. }
  136. while ((next = stream.next()) != null) {
  137. if (next == quote && !escaped) break;
  138. escaped = !escaped && next == "\\";
  139. }
  140. if (!escaped) state.tokenize = tokenBase;
  141. return ret("string", "string");
  142. };
  143. }
  144. function tokenComment(stream, state) {
  145. var maybeEnd = false, ch;
  146. while (ch = stream.next()) {
  147. if (ch == "/" && maybeEnd) {
  148. state.tokenize = tokenBase;
  149. break;
  150. }
  151. maybeEnd = (ch == "*");
  152. }
  153. return ret("comment", "comment");
  154. }
  155. function tokenQuasi(stream, state) {
  156. var escaped = false, next;
  157. while ((next = stream.next()) != null) {
  158. if (!escaped && (next == "`" || next == "$" && stream.eat("{"))) {
  159. state.tokenize = tokenBase;
  160. break;
  161. }
  162. escaped = !escaped && next == "\\";
  163. }
  164. return ret("quasi", "string-2", stream.current());
  165. }
  166. var brackets = "([{}])";
  167. // This is a crude lookahead trick to try and notice that we're
  168. // parsing the argument patterns for a fat-arrow function before we
  169. // actually hit the arrow token. It only works if the arrow is on
  170. // the same line as the arguments and there's no strange noise
  171. // (comments) in between. Fallback is to only notice when we hit the
  172. // arrow, and not declare the arguments as locals for the arrow
  173. // body.
  174. function findFatArrow(stream, state) {
  175. if (state.fatArrowAt) state.fatArrowAt = null;
  176. var arrow = stream.string.indexOf("=>", stream.start);
  177. if (arrow < 0) return;
  178. var depth = 0, sawSomething = false;
  179. for (var pos = arrow - 1; pos >= 0; --pos) {
  180. var ch = stream.string.charAt(pos);
  181. var bracket = brackets.indexOf(ch);
  182. if (bracket >= 0 && bracket < 3) {
  183. if (!depth) { ++pos; break; }
  184. if (--depth == 0) break;
  185. } else if (bracket >= 3 && bracket < 6) {
  186. ++depth;
  187. } else if (wordRE.test(ch)) {
  188. sawSomething = true;
  189. } else if (/["'\/]/.test(ch)) {
  190. return;
  191. } else if (sawSomething && !depth) {
  192. ++pos;
  193. break;
  194. }
  195. }
  196. if (sawSomething && !depth) state.fatArrowAt = pos;
  197. }
  198. // Parser
  199. var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true, "this": true, "jsonld-keyword": true};
  200. function JSLexical(indented, column, type, align, prev, info) {
  201. this.indented = indented;
  202. this.column = column;
  203. this.type = type;
  204. this.prev = prev;
  205. this.info = info;
  206. if (align != null) this.align = align;
  207. }
  208. function inScope(state, varname) {
  209. for (var v = state.localVars; v; v = v.next)
  210. if (v.name == varname) return true;
  211. for (var cx = state.context; cx; cx = cx.prev) {
  212. for (var v = cx.vars; v; v = v.next)
  213. if (v.name == varname) return true;
  214. }
  215. }
  216. function parseJS(state, style, type, content, stream) {
  217. var cc = state.cc;
  218. // Communicate our context to the combinators.
  219. // (Less wasteful than consing up a hundred closures on every call.)
  220. cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc; cx.style = style;
  221. if (!state.lexical.hasOwnProperty("align"))
  222. state.lexical.align = true;
  223. while(true) {
  224. var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement;
  225. if (combinator(type, content)) {
  226. while(cc.length && cc[cc.length - 1].lex)
  227. cc.pop()();
  228. if (cx.marked) return cx.marked;
  229. if (type == "variable" && inScope(state, content)) return "variable-2";
  230. return style;
  231. }
  232. }
  233. }
  234. // Combinator utils
  235. var cx = {state: null, column: null, marked: null, cc: null};
  236. function pass() {
  237. for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
  238. }
  239. function cont() {
  240. pass.apply(null, arguments);
  241. return true;
  242. }
  243. function register(varname) {
  244. function inList(list) {
  245. for (var v = list; v; v = v.next)
  246. if (v.name == varname) return true;
  247. return false;
  248. }
  249. var state = cx.state;
  250. if (state.context) {
  251. cx.marked = "def";
  252. if (inList(state.localVars)) return;
  253. state.localVars = {name: varname, next: state.localVars};
  254. } else {
  255. if (inList(state.globalVars)) return;
  256. if (parserConfig.globalVars)
  257. state.globalVars = {name: varname, next: state.globalVars};
  258. }
  259. }
  260. // Combinators
  261. var defaultVars = {name: "this", next: {name: "arguments"}};
  262. function pushcontext() {
  263. cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
  264. cx.state.localVars = defaultVars;
  265. }
  266. function popcontext() {
  267. cx.state.localVars = cx.state.context.vars;
  268. cx.state.context = cx.state.context.prev;
  269. }
  270. function pushlex(type, info) {
  271. var result = function() {
  272. var state = cx.state, indent = state.indented;
  273. if (state.lexical.type == "stat") indent = state.lexical.indented;
  274. else for (var outer = state.lexical; outer && outer.type == ")" && outer.align; outer = outer.prev)
  275. indent = outer.indented;
  276. state.lexical = new JSLexical(indent, cx.stream.column(), type, null, state.lexical, info);
  277. };
  278. result.lex = true;
  279. return result;
  280. }
  281. function poplex() {
  282. var state = cx.state;
  283. if (state.lexical.prev) {
  284. if (state.lexical.type == ")")
  285. state.indented = state.lexical.indented;
  286. state.lexical = state.lexical.prev;
  287. }
  288. }
  289. poplex.lex = true;
  290. function expect(wanted) {
  291. function exp(type) {
  292. if (type == wanted) return cont();
  293. else if (wanted == ";") return pass();
  294. else return cont(exp);
  295. };
  296. return exp;
  297. }
  298. function statement(type, value) {
  299. if (type == "var") return cont(pushlex("vardef", value.length), vardef, expect(";"), poplex);
  300. if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);
  301. if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
  302. if (type == "{") return cont(pushlex("}"), block, poplex);
  303. if (type == ";") return cont();
  304. if (type == "if") {
  305. if (cx.state.lexical.info == "else" && cx.state.cc[cx.state.cc.length - 1] == poplex)
  306. cx.state.cc.pop()();
  307. return cont(pushlex("form"), expression, statement, poplex, maybeelse);
  308. }
  309. if (type == "function") return cont(functiondef);
  310. if (type == "for") return cont(pushlex("form"), forspec, statement, poplex);
  311. if (type == "variable") return cont(pushlex("stat"), maybelabel);
  312. if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
  313. block, poplex, poplex);
  314. if (type == "case") return cont(expression, expect(":"));
  315. if (type == "default") return cont(expect(":"));
  316. if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
  317. statement, poplex, popcontext);
  318. if (type == "module") return cont(pushlex("form"), pushcontext, afterModule, popcontext, poplex);
  319. if (type == "class") return cont(pushlex("form"), className, poplex);
  320. if (type == "export") return cont(pushlex("form"), afterExport, poplex);
  321. if (type == "import") return cont(pushlex("form"), afterImport, poplex);
  322. return pass(pushlex("stat"), expression, expect(";"), poplex);
  323. }
  324. function expression(type) {
  325. return expressionInner(type, false);
  326. }
  327. function expressionNoComma(type) {
  328. return expressionInner(type, true);
  329. }
  330. function expressionInner(type, noComma) {
  331. if (cx.state.fatArrowAt == cx.stream.start) {
  332. var body = noComma ? arrowBodyNoComma : arrowBody;
  333. if (type == "(") return cont(pushcontext, pushlex(")"), commasep(pattern, ")"), poplex, expect("=>"), body, popcontext);
  334. else if (type == "variable") return pass(pushcontext, pattern, expect("=>"), body, popcontext);
  335. }
  336. var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma;
  337. if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);
  338. if (type == "function") return cont(functiondef, maybeop);
  339. if (type == "keyword c") return cont(noComma ? maybeexpressionNoComma : maybeexpression);
  340. if (type == "(") return cont(pushlex(")"), maybeexpression, comprehension, expect(")"), poplex, maybeop);
  341. if (type == "operator" || type == "spread") return cont(noComma ? expressionNoComma : expression);
  342. if (type == "[") return cont(pushlex("]"), arrayLiteral, poplex, maybeop);
  343. if (type == "{") return contCommasep(objprop, "}", null, maybeop);
  344. if (type == "quasi") { return pass(quasi, maybeop); }
  345. return cont();
  346. }
  347. function maybeexpression(type) {
  348. if (type.match(/[;\}\)\],]/)) return pass();
  349. return pass(expression);
  350. }
  351. function maybeexpressionNoComma(type) {
  352. if (type.match(/[;\}\)\],]/)) return pass();
  353. return pass(expressionNoComma);
  354. }
  355. function maybeoperatorComma(type, value) {
  356. if (type == ",") return cont(expression);
  357. return maybeoperatorNoComma(type, value, false);
  358. }
  359. function maybeoperatorNoComma(type, value, noComma) {
  360. var me = noComma == false ? maybeoperatorComma : maybeoperatorNoComma;
  361. var expr = noComma == false ? expression : expressionNoComma;
  362. if (type == "=>") return cont(pushcontext, noComma ? arrowBodyNoComma : arrowBody, popcontext);
  363. if (type == "operator") {
  364. if (/\+\+|--/.test(value)) return cont(me);
  365. if (value == "?") return cont(expression, expect(":"), expr);
  366. return cont(expr);
  367. }
  368. if (type == "quasi") { return pass(quasi, me); }
  369. if (type == ";") return;
  370. if (type == "(") return contCommasep(expressionNoComma, ")", "call", me);
  371. if (type == ".") return cont(property, me);
  372. if (type == "[") return cont(pushlex("]"), maybeexpression, expect("]"), poplex, me);
  373. }
  374. function quasi(type, value) {
  375. if (type != "quasi") return pass();
  376. if (value.slice(value.length - 2) != "${") return cont(quasi);
  377. return cont(expression, continueQuasi);
  378. }
  379. function continueQuasi(type) {
  380. if (type == "}") {
  381. cx.marked = "string-2";
  382. cx.state.tokenize = tokenQuasi;
  383. return cont(quasi);
  384. }
  385. }
  386. function arrowBody(type) {
  387. findFatArrow(cx.stream, cx.state);
  388. return pass(type == "{" ? statement : expression);
  389. }
  390. function arrowBodyNoComma(type) {
  391. findFatArrow(cx.stream, cx.state);
  392. return pass(type == "{" ? statement : expressionNoComma);
  393. }
  394. function maybelabel(type) {
  395. if (type == ":") return cont(poplex, statement);
  396. return pass(maybeoperatorComma, expect(";"), poplex);
  397. }
  398. function property(type) {
  399. if (type == "variable") {cx.marked = "property"; return cont();}
  400. }
  401. function objprop(type, value) {
  402. if (type == "variable" || cx.style == "keyword") {
  403. cx.marked = "property";
  404. if (value == "get" || value == "set") return cont(getterSetter);
  405. return cont(afterprop);
  406. } else if (type == "number" || type == "string") {
  407. cx.marked = jsonldMode ? "property" : (cx.style + " property");
  408. return cont(afterprop);
  409. } else if (type == "jsonld-keyword") {
  410. return cont(afterprop);
  411. } else if (type == "[") {
  412. return cont(expression, expect("]"), afterprop);
  413. }
  414. }
  415. function getterSetter(type) {
  416. if (type != "variable") return pass(afterprop);
  417. cx.marked = "property";
  418. return cont(functiondef);
  419. }
  420. function afterprop(type) {
  421. if (type == ":") return cont(expressionNoComma);
  422. if (type == "(") return pass(functiondef);
  423. }
  424. function commasep(what, end) {
  425. function proceed(type) {
  426. if (type == ",") {
  427. var lex = cx.state.lexical;
  428. if (lex.info == "call") lex.pos = (lex.pos || 0) + 1;
  429. return cont(what, proceed);
  430. }
  431. if (type == end) return cont();
  432. return cont(expect(end));
  433. }
  434. return function(type) {
  435. if (type == end) return cont();
  436. return pass(what, proceed);
  437. };
  438. }
  439. function contCommasep(what, end, info) {
  440. for (var i = 3; i < arguments.length; i++)
  441. cx.cc.push(arguments[i]);
  442. return cont(pushlex(end, info), commasep(what, end), poplex);
  443. }
  444. function block(type) {
  445. if (type == "}") return cont();
  446. return pass(statement, block);
  447. }
  448. function maybetype(type) {
  449. if (isTS && type == ":") return cont(typedef);
  450. }
  451. function typedef(type) {
  452. if (type == "variable"){cx.marked = "variable-3"; return cont();}
  453. }
  454. function vardef() {
  455. return pass(pattern, maybetype, maybeAssign, vardefCont);
  456. }
  457. function pattern(type, value) {
  458. if (type == "variable") { register(value); return cont(); }
  459. if (type == "[") return contCommasep(pattern, "]");
  460. if (type == "{") return contCommasep(proppattern, "}");
  461. }
  462. function proppattern(type, value) {
  463. if (type == "variable" && !cx.stream.match(/^\s*:/, false)) {
  464. register(value);
  465. return cont(maybeAssign);
  466. }
  467. if (type == "variable") cx.marked = "property";
  468. return cont(expect(":"), pattern, maybeAssign);
  469. }
  470. function maybeAssign(_type, value) {
  471. if (value == "=") return cont(expressionNoComma);
  472. }
  473. function vardefCont(type) {
  474. if (type == ",") return cont(vardef);
  475. }
  476. function maybeelse(type, value) {
  477. if (type == "keyword b" && value == "else") return cont(pushlex("form", "else"), statement, poplex);
  478. }
  479. function forspec(type) {
  480. if (type == "(") return cont(pushlex(")"), forspec1, expect(")"), poplex);
  481. }
  482. function forspec1(type) {
  483. if (type == "var") return cont(vardef, expect(";"), forspec2);
  484. if (type == ";") return cont(forspec2);
  485. if (type == "variable") return cont(formaybeinof);
  486. return pass(expression, expect(";"), forspec2);
  487. }
  488. function formaybeinof(_type, value) {
  489. if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }
  490. return cont(maybeoperatorComma, forspec2);
  491. }
  492. function forspec2(type, value) {
  493. if (type == ";") return cont(forspec3);
  494. if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }
  495. return pass(expression, expect(";"), forspec3);
  496. }
  497. function forspec3(type) {
  498. if (type != ")") cont(expression);
  499. }
  500. function functiondef(type, value) {
  501. if (value == "*") {cx.marked = "keyword"; return cont(functiondef);}
  502. if (type == "variable") {register(value); return cont(functiondef);}
  503. if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, statement, popcontext);
  504. }
  505. function funarg(type) {
  506. if (type == "spread") return cont(funarg);
  507. return pass(pattern, maybetype);
  508. }
  509. function className(type, value) {
  510. if (type == "variable") {register(value); return cont(classNameAfter);}
  511. }
  512. function classNameAfter(type, value) {
  513. if (value == "extends") return cont(expression, classNameAfter);
  514. if (type == "{") return cont(pushlex("}"), classBody, poplex);
  515. }
  516. function classBody(type, value) {
  517. if (type == "variable" || cx.style == "keyword") {
  518. if (value == "static") {
  519. cx.marked = "keyword";
  520. return cont(classBody);
  521. }
  522. cx.marked = "property";
  523. if (value == "get" || value == "set") return cont(classGetterSetter, functiondef, classBody);
  524. return cont(functiondef, classBody);
  525. }
  526. if (value == "*") {
  527. cx.marked = "keyword";
  528. return cont(classBody);
  529. }
  530. if (type == ";") return cont(classBody);
  531. if (type == "}") return cont();
  532. }
  533. function classGetterSetter(type) {
  534. if (type != "variable") return pass();
  535. cx.marked = "property";
  536. return cont();
  537. }
  538. function afterModule(type, value) {
  539. if (type == "string") return cont(statement);
  540. if (type == "variable") { register(value); return cont(maybeFrom); }
  541. }
  542. function afterExport(_type, value) {
  543. if (value == "*") { cx.marked = "keyword"; return cont(maybeFrom, expect(";")); }
  544. if (value == "default") { cx.marked = "keyword"; return cont(expression, expect(";")); }
  545. return pass(statement);
  546. }
  547. function afterImport(type) {
  548. if (type == "string") return cont();
  549. return pass(importSpec, maybeFrom);
  550. }
  551. function importSpec(type, value) {
  552. if (type == "{") return contCommasep(importSpec, "}");
  553. if (type == "variable") register(value);
  554. if (value == "*") cx.marked = "keyword";
  555. return cont(maybeAs);
  556. }
  557. function maybeAs(_type, value) {
  558. if (value == "as") { cx.marked = "keyword"; return cont(importSpec); }
  559. }
  560. function maybeFrom(_type, value) {
  561. if (value == "from") { cx.marked = "keyword"; return cont(expression); }
  562. }
  563. function arrayLiteral(type) {
  564. if (type == "]") return cont();
  565. return pass(expressionNoComma, maybeArrayComprehension);
  566. }
  567. function maybeArrayComprehension(type) {
  568. if (type == "for") return pass(comprehension, expect("]"));
  569. if (type == ",") return cont(commasep(maybeexpressionNoComma, "]"));
  570. return pass(commasep(expressionNoComma, "]"));
  571. }
  572. function comprehension(type) {
  573. if (type == "for") return cont(forspec, comprehension);
  574. if (type == "if") return cont(expression, comprehension);
  575. }
  576. function isContinuedStatement(state, textAfter) {
  577. return state.lastType == "operator" || state.lastType == "," ||
  578. isOperatorChar.test(textAfter.charAt(0)) ||
  579. /[,.]/.test(textAfter.charAt(0));
  580. }
  581. // Interface
  582. return {
  583. startState: function(basecolumn) {
  584. var state = {
  585. tokenize: tokenBase,
  586. lastType: "sof",
  587. cc: [],
  588. lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
  589. localVars: parserConfig.localVars,
  590. context: parserConfig.localVars && {vars: parserConfig.localVars},
  591. indented: 0
  592. };
  593. if (parserConfig.globalVars && typeof parserConfig.globalVars == "object")
  594. state.globalVars = parserConfig.globalVars;
  595. return state;
  596. },
  597. token: function(stream, state) {
  598. if (stream.sol()) {
  599. if (!state.lexical.hasOwnProperty("align"))
  600. state.lexical.align = false;
  601. state.indented = stream.indentation();
  602. findFatArrow(stream, state);
  603. }
  604. if (state.tokenize != tokenComment && stream.eatSpace()) return null;
  605. var style = state.tokenize(stream, state);
  606. if (type == "comment") return style;
  607. state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type;
  608. return parseJS(state, style, type, content, stream);
  609. },
  610. indent: function(state, textAfter) {
  611. if (state.tokenize == tokenComment) return CodeMirror.Pass;
  612. if (state.tokenize != tokenBase) return 0;
  613. var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;
  614. // Kludge to prevent 'maybelse' from blocking lexical scope pops
  615. if (!/^\s*else\b/.test(textAfter)) for (var i = state.cc.length - 1; i >= 0; --i) {
  616. var c = state.cc[i];
  617. if (c == poplex) lexical = lexical.prev;
  618. else if (c != maybeelse) break;
  619. }
  620. if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;
  621. if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat")
  622. lexical = lexical.prev;
  623. var type = lexical.type, closing = firstChar == type;
  624. if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? lexical.info + 1 : 0);
  625. else if (type == "form" && firstChar == "{") return lexical.indented;
  626. else if (type == "form") return lexical.indented + indentUnit;
  627. else if (type == "stat")
  628. return lexical.indented + (isContinuedStatement(state, textAfter) ? statementIndent || indentUnit : 0);
  629. else if (lexical.info == "switch" && !closing && parserConfig.doubleIndentSwitch != false)
  630. return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
  631. else if (lexical.align) return lexical.column + (closing ? 0 : 1);
  632. else return lexical.indented + (closing ? 0 : indentUnit);
  633. },
  634. electricInput: /^\s*(?:case .*?:|default:|\{|\})$/,
  635. blockCommentStart: jsonMode ? null : "/*",
  636. blockCommentEnd: jsonMode ? null : "*/",
  637. lineComment: jsonMode ? null : "//",
  638. fold: "brace",
  639. closeBrackets: "()[]{}''\"\"``",
  640. helperType: jsonMode ? "json" : "javascript",
  641. jsonldMode: jsonldMode,
  642. jsonMode: jsonMode
  643. };
  644. });
  645. CodeMirror.registerHelper("wordChars", "javascript", /[\w$]/);
  646. CodeMirror.defineMIME("text/javascript", "javascript");
  647. CodeMirror.defineMIME("text/ecmascript", "javascript");
  648. CodeMirror.defineMIME("application/javascript", "javascript");
  649. CodeMirror.defineMIME("application/x-javascript", "javascript");
  650. CodeMirror.defineMIME("application/ecmascript", "javascript");
  651. CodeMirror.defineMIME("application/json", {name: "javascript", json: true});
  652. CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true});
  653. CodeMirror.defineMIME("application/ld+json", {name: "javascript", jsonld: true});
  654. CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true });
  655. CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });
  656. });