javascript.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. function expressionAllowed(stream, state, backUp) {
  13. return /^(?:operator|sof|keyword c|case|new|[\[{}\(,;:]|=>)$/.test(state.lastType) ||
  14. (state.lastType == "quasi" && /\{\s*$/.test(stream.string.slice(0, stream.pos - (backUp || 0))))
  15. }
  16. CodeMirror.defineMode("javascript", function(config, parserConfig) {
  17. var indentUnit = config.indentUnit;
  18. var statementIndent = parserConfig.statementIndent;
  19. var jsonldMode = parserConfig.jsonld;
  20. var jsonMode = parserConfig.json || jsonldMode;
  21. var isTS = parserConfig.typescript;
  22. var wordRE = parserConfig.wordCharacters || /[\w$\xa1-\uffff]/;
  23. // Tokenizer
  24. var keywords = function(){
  25. function kw(type) {return {type: type, style: "keyword"};}
  26. var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
  27. var operator = kw("operator"), atom = {type: "atom", style: "atom"};
  28. var jsKeywords = {
  29. "if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
  30. "return": C, "break": C, "continue": C, "new": kw("new"), "delete": C, "throw": C, "debugger": C,
  31. "var": kw("var"), "const": kw("var"), "let": kw("var"),
  32. "function": kw("function"), "catch": kw("catch"),
  33. "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
  34. "in": operator, "typeof": operator, "instanceof": operator,
  35. "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom,
  36. "this": kw("this"), "class": kw("class"), "super": kw("atom"),
  37. "yield": C, "export": kw("export"), "import": kw("import"), "extends": C,
  38. "await": C, "async": kw("async")
  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) { if (ch == "(") sawSomething = true; 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. if (type == "async") return cont(statement)
  336. return pass(pushlex("stat"), expression, expect(";"), poplex);
  337. }
  338. function expression(type) {
  339. return expressionInner(type, false);
  340. }
  341. function expressionNoComma(type) {
  342. return expressionInner(type, true);
  343. }
  344. function expressionInner(type, noComma) {
  345. if (cx.state.fatArrowAt == cx.stream.start) {
  346. var body = noComma ? arrowBodyNoComma : arrowBody;
  347. if (type == "(") return cont(pushcontext, pushlex(")"), commasep(pattern, ")"), poplex, expect("=>"), body, popcontext);
  348. else if (type == "variable") return pass(pushcontext, pattern, expect("=>"), body, popcontext);
  349. }
  350. var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma;
  351. if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);
  352. if (type == "function") return cont(functiondef, maybeop);
  353. if (type == "keyword c" || type == "async") return cont(noComma ? maybeexpressionNoComma : maybeexpression);
  354. if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeop);
  355. if (type == "operator" || type == "spread") return cont(noComma ? expressionNoComma : expression);
  356. if (type == "[") return cont(pushlex("]"), arrayLiteral, poplex, maybeop);
  357. if (type == "{") return contCommasep(objprop, "}", null, maybeop);
  358. if (type == "quasi") return pass(quasi, maybeop);
  359. if (type == "new") return cont(maybeTarget(noComma));
  360. return cont();
  361. }
  362. function maybeexpression(type) {
  363. if (type.match(/[;\}\)\],]/)) return pass();
  364. return pass(expression);
  365. }
  366. function maybeexpressionNoComma(type) {
  367. if (type.match(/[;\}\)\],]/)) return pass();
  368. return pass(expressionNoComma);
  369. }
  370. function maybeoperatorComma(type, value) {
  371. if (type == ",") return cont(expression);
  372. return maybeoperatorNoComma(type, value, false);
  373. }
  374. function maybeoperatorNoComma(type, value, noComma) {
  375. var me = noComma == false ? maybeoperatorComma : maybeoperatorNoComma;
  376. var expr = noComma == false ? expression : expressionNoComma;
  377. if (type == "=>") return cont(pushcontext, noComma ? arrowBodyNoComma : arrowBody, popcontext);
  378. if (type == "operator") {
  379. if (/\+\+|--/.test(value)) return cont(me);
  380. if (value == "?") return cont(expression, expect(":"), expr);
  381. return cont(expr);
  382. }
  383. if (type == "quasi") { return pass(quasi, me); }
  384. if (type == ";") return;
  385. if (type == "(") return contCommasep(expressionNoComma, ")", "call", me);
  386. if (type == ".") return cont(property, me);
  387. if (type == "[") return cont(pushlex("]"), maybeexpression, expect("]"), poplex, me);
  388. }
  389. function quasi(type, value) {
  390. if (type != "quasi") return pass();
  391. if (value.slice(value.length - 2) != "${") return cont(quasi);
  392. return cont(expression, continueQuasi);
  393. }
  394. function continueQuasi(type) {
  395. if (type == "}") {
  396. cx.marked = "string-2";
  397. cx.state.tokenize = tokenQuasi;
  398. return cont(quasi);
  399. }
  400. }
  401. function arrowBody(type) {
  402. findFatArrow(cx.stream, cx.state);
  403. return pass(type == "{" ? statement : expression);
  404. }
  405. function arrowBodyNoComma(type) {
  406. findFatArrow(cx.stream, cx.state);
  407. return pass(type == "{" ? statement : expressionNoComma);
  408. }
  409. function maybeTarget(noComma) {
  410. return function(type) {
  411. if (type == ".") return cont(noComma ? targetNoComma : target);
  412. else return pass(noComma ? expressionNoComma : expression);
  413. };
  414. }
  415. function target(_, value) {
  416. if (value == "target") { cx.marked = "keyword"; return cont(maybeoperatorComma); }
  417. }
  418. function targetNoComma(_, value) {
  419. if (value == "target") { cx.marked = "keyword"; return cont(maybeoperatorNoComma); }
  420. }
  421. function maybelabel(type) {
  422. if (type == ":") return cont(poplex, statement);
  423. return pass(maybeoperatorComma, expect(";"), poplex);
  424. }
  425. function property(type) {
  426. if (type == "variable") {cx.marked = "property"; return cont();}
  427. }
  428. function objprop(type, value) {
  429. if (type == "async") {
  430. cx.marked = "property";
  431. return cont(objprop);
  432. } else if (type == "variable" || cx.style == "keyword") {
  433. cx.marked = "property";
  434. if (value == "get" || value == "set") return cont(getterSetter);
  435. return cont(afterprop);
  436. } else if (type == "number" || type == "string") {
  437. cx.marked = jsonldMode ? "property" : (cx.style + " property");
  438. return cont(afterprop);
  439. } else if (type == "jsonld-keyword") {
  440. return cont(afterprop);
  441. } else if (type == "modifier") {
  442. return cont(objprop)
  443. } else if (type == "[") {
  444. return cont(expression, expect("]"), afterprop);
  445. } else if (type == "spread") {
  446. return cont(expression);
  447. } else if (type == ":") {
  448. return pass(afterprop)
  449. }
  450. }
  451. function getterSetter(type) {
  452. if (type != "variable") return pass(afterprop);
  453. cx.marked = "property";
  454. return cont(functiondef);
  455. }
  456. function afterprop(type) {
  457. if (type == ":") return cont(expressionNoComma);
  458. if (type == "(") return pass(functiondef);
  459. }
  460. function commasep(what, end) {
  461. function proceed(type, value) {
  462. if (type == ",") {
  463. var lex = cx.state.lexical;
  464. if (lex.info == "call") lex.pos = (lex.pos || 0) + 1;
  465. return cont(function(type, value) {
  466. if (type == end || value == end) return pass()
  467. return pass(what)
  468. }, proceed);
  469. }
  470. if (type == end || value == end) return cont();
  471. return cont(expect(end));
  472. }
  473. return function(type, value) {
  474. if (type == end || value == end) return cont();
  475. return pass(what, proceed);
  476. };
  477. }
  478. function contCommasep(what, end, info) {
  479. for (var i = 3; i < arguments.length; i++)
  480. cx.cc.push(arguments[i]);
  481. return cont(pushlex(end, info), commasep(what, end), poplex);
  482. }
  483. function block(type) {
  484. if (type == "}") return cont();
  485. return pass(statement, block);
  486. }
  487. function maybetype(type) {
  488. if (isTS && type == ":") return cont(typeexpr);
  489. }
  490. function maybedefault(_, value) {
  491. if (value == "=") return cont(expressionNoComma);
  492. }
  493. function typeexpr(type) {
  494. if (type == "variable") {cx.marked = "variable-3"; return cont(afterType);}
  495. if (type == "{") return cont(commasep(typeprop, "}"))
  496. if (type == "(") return cont(commasep(typearg, ")"), maybeReturnType)
  497. }
  498. function maybeReturnType(type) {
  499. if (type == "=>") return cont(typeexpr)
  500. }
  501. function typeprop(type) {
  502. if (type == "variable" || cx.style == "keyword") {
  503. cx.marked = "property"
  504. return cont(typeprop)
  505. } else if (type == ":") {
  506. return cont(typeexpr)
  507. }
  508. }
  509. function typearg(type) {
  510. if (type == "variable") return cont(typearg)
  511. else if (type == ":") return cont(typeexpr)
  512. }
  513. function afterType(type, value) {
  514. if (value == "<") return cont(commasep(typeexpr, ">"), afterType)
  515. if (type == "[") return cont(expect("]"), afterType)
  516. }
  517. function vardef() {
  518. return pass(pattern, maybetype, maybeAssign, vardefCont);
  519. }
  520. function pattern(type, value) {
  521. if (type == "modifier") return cont(pattern)
  522. if (type == "variable") { register(value); return cont(); }
  523. if (type == "spread") return cont(pattern);
  524. if (type == "[") return contCommasep(pattern, "]");
  525. if (type == "{") return contCommasep(proppattern, "}");
  526. }
  527. function proppattern(type, value) {
  528. if (type == "variable" && !cx.stream.match(/^\s*:/, false)) {
  529. register(value);
  530. return cont(maybeAssign);
  531. }
  532. if (type == "variable") cx.marked = "property";
  533. if (type == "spread") return cont(pattern);
  534. if (type == "}") return pass();
  535. return cont(expect(":"), pattern, maybeAssign);
  536. }
  537. function maybeAssign(_type, value) {
  538. if (value == "=") return cont(expressionNoComma);
  539. }
  540. function vardefCont(type) {
  541. if (type == ",") return cont(vardef);
  542. }
  543. function maybeelse(type, value) {
  544. if (type == "keyword b" && value == "else") return cont(pushlex("form", "else"), statement, poplex);
  545. }
  546. function forspec(type) {
  547. if (type == "(") return cont(pushlex(")"), forspec1, expect(")"), poplex);
  548. }
  549. function forspec1(type) {
  550. if (type == "var") return cont(vardef, expect(";"), forspec2);
  551. if (type == ";") return cont(forspec2);
  552. if (type == "variable") return cont(formaybeinof);
  553. return pass(expression, expect(";"), forspec2);
  554. }
  555. function formaybeinof(_type, value) {
  556. if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }
  557. return cont(maybeoperatorComma, forspec2);
  558. }
  559. function forspec2(type, value) {
  560. if (type == ";") return cont(forspec3);
  561. if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }
  562. return pass(expression, expect(";"), forspec3);
  563. }
  564. function forspec3(type) {
  565. if (type != ")") cont(expression);
  566. }
  567. function functiondef(type, value) {
  568. if (value == "*") {cx.marked = "keyword"; return cont(functiondef);}
  569. if (type == "variable") {register(value); return cont(functiondef);}
  570. if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, maybetype, statement, popcontext);
  571. }
  572. function funarg(type) {
  573. if (type == "spread") return cont(funarg);
  574. return pass(pattern, maybetype, maybedefault);
  575. }
  576. function className(type, value) {
  577. if (type == "variable") {register(value); return cont(classNameAfter);}
  578. }
  579. function classNameAfter(type, value) {
  580. if (value == "extends") return cont(isTS ? typeexpr : expression, classNameAfter);
  581. if (type == "{") return cont(pushlex("}"), classBody, poplex);
  582. }
  583. function classBody(type, value) {
  584. if (type == "variable" || cx.style == "keyword") {
  585. if (value == "static") {
  586. cx.marked = "keyword";
  587. return cont(classBody);
  588. }
  589. cx.marked = "property";
  590. if (value == "get" || value == "set") return cont(classGetterSetter, functiondef, classBody);
  591. return cont(functiondef, classBody);
  592. }
  593. if (value == "*") {
  594. cx.marked = "keyword";
  595. return cont(classBody);
  596. }
  597. if (type == ";") return cont(classBody);
  598. if (type == "}") return cont();
  599. }
  600. function classGetterSetter(type) {
  601. if (type != "variable") return pass();
  602. cx.marked = "property";
  603. return cont();
  604. }
  605. function afterExport(_type, value) {
  606. if (value == "*") { cx.marked = "keyword"; return cont(maybeFrom, expect(";")); }
  607. if (value == "default") { cx.marked = "keyword"; return cont(expression, expect(";")); }
  608. return pass(statement);
  609. }
  610. function afterImport(type) {
  611. if (type == "string") return cont();
  612. return pass(importSpec, maybeFrom);
  613. }
  614. function importSpec(type, value) {
  615. if (type == "{") return contCommasep(importSpec, "}");
  616. if (type == "variable") register(value);
  617. if (value == "*") cx.marked = "keyword";
  618. return cont(maybeAs);
  619. }
  620. function maybeAs(_type, value) {
  621. if (value == "as") { cx.marked = "keyword"; return cont(importSpec); }
  622. }
  623. function maybeFrom(_type, value) {
  624. if (value == "from") { cx.marked = "keyword"; return cont(expression); }
  625. }
  626. function arrayLiteral(type) {
  627. if (type == "]") return cont();
  628. return pass(commasep(expressionNoComma, "]"));
  629. }
  630. function isContinuedStatement(state, textAfter) {
  631. return state.lastType == "operator" || state.lastType == "," ||
  632. isOperatorChar.test(textAfter.charAt(0)) ||
  633. /[,.]/.test(textAfter.charAt(0));
  634. }
  635. // Interface
  636. return {
  637. startState: function(basecolumn) {
  638. var state = {
  639. tokenize: tokenBase,
  640. lastType: "sof",
  641. cc: [],
  642. lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
  643. localVars: parserConfig.localVars,
  644. context: parserConfig.localVars && {vars: parserConfig.localVars},
  645. indented: basecolumn || 0
  646. };
  647. if (parserConfig.globalVars && typeof parserConfig.globalVars == "object")
  648. state.globalVars = parserConfig.globalVars;
  649. return state;
  650. },
  651. token: function(stream, state) {
  652. if (stream.sol()) {
  653. if (!state.lexical.hasOwnProperty("align"))
  654. state.lexical.align = false;
  655. state.indented = stream.indentation();
  656. findFatArrow(stream, state);
  657. }
  658. if (state.tokenize != tokenComment && stream.eatSpace()) return null;
  659. var style = state.tokenize(stream, state);
  660. if (type == "comment") return style;
  661. state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type;
  662. return parseJS(state, style, type, content, stream);
  663. },
  664. indent: function(state, textAfter) {
  665. if (state.tokenize == tokenComment) return CodeMirror.Pass;
  666. if (state.tokenize != tokenBase) return 0;
  667. var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;
  668. // Kludge to prevent 'maybelse' from blocking lexical scope pops
  669. if (!/^\s*else\b/.test(textAfter)) for (var i = state.cc.length - 1; i >= 0; --i) {
  670. var c = state.cc[i];
  671. if (c == poplex) lexical = lexical.prev;
  672. else if (c != maybeelse) break;
  673. }
  674. if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;
  675. if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat")
  676. lexical = lexical.prev;
  677. var type = lexical.type, closing = firstChar == type;
  678. if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? lexical.info + 1 : 0);
  679. else if (type == "form" && firstChar == "{") return lexical.indented;
  680. else if (type == "form") return lexical.indented + indentUnit;
  681. else if (type == "stat")
  682. return lexical.indented + (isContinuedStatement(state, textAfter) ? statementIndent || indentUnit : 0);
  683. else if (lexical.info == "switch" && !closing && parserConfig.doubleIndentSwitch != false)
  684. return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
  685. else if (lexical.align) return lexical.column + (closing ? 0 : 1);
  686. else return lexical.indented + (closing ? 0 : indentUnit);
  687. },
  688. electricInput: /^\s*(?:case .*?:|default:|\{|\})$/,
  689. blockCommentStart: jsonMode ? null : "/*",
  690. blockCommentEnd: jsonMode ? null : "*/",
  691. lineComment: jsonMode ? null : "//",
  692. fold: "brace",
  693. closeBrackets: "()[]{}''\"\"``",
  694. helperType: jsonMode ? "json" : "javascript",
  695. jsonldMode: jsonldMode,
  696. jsonMode: jsonMode,
  697. expressionAllowed: expressionAllowed,
  698. skipExpression: function(state) {
  699. var top = state.cc[state.cc.length - 1]
  700. if (top == expression || top == expressionNoComma) state.cc.pop()
  701. }
  702. };
  703. });
  704. CodeMirror.registerHelper("wordChars", "javascript", /[\w$]/);
  705. CodeMirror.defineMIME("text/javascript", "javascript");
  706. CodeMirror.defineMIME("text/ecmascript", "javascript");
  707. CodeMirror.defineMIME("application/javascript", "javascript");
  708. CodeMirror.defineMIME("application/x-javascript", "javascript");
  709. CodeMirror.defineMIME("application/ecmascript", "javascript");
  710. CodeMirror.defineMIME("application/json", {name: "javascript", json: true});
  711. CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true});
  712. CodeMirror.defineMIME("application/ld+json", {name: "javascript", jsonld: true});
  713. CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true });
  714. CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });
  715. });