openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

javascript.js (33974B)


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