You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

960 lines
38 KiB

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