javascript.js 32 KB

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