javascript.js 27 KB

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