javascript.js 25 KB

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