all repos — NoPaste @ 5c12c8a8f515faa64a6cd1d3f04d9aa60561322d

Resurrected - The PussTheCat.org fork of NoPaste

scripts/CodeMirror/mode/javascript/javascript.js (view raw)

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