Debian Codegraph @

codemirror-legacy-modes/mode/javascript.js

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