github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/app/public/codemirror/mode/javascript/javascript.js (about)

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