javascript.js 28 KB

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