PageSourceSearch

https://www.gladiator.nl/assets/js/codemirror/javascript.js

js gladiator.nl collected 2026-10-09 01:18:41 UTC 28,078 bytes, 742 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.