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