javascript.js 25 KB

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