PluginProbe
Smart Forms – when you need more than just a contact form / trunk
Smart Forms – when you need more than just a contact form vtrunk
trunk 0.5 0.5.5 0.6 0.7 0.8 0.8.5 0.9.1
smart-forms / js / utilities / codeMirror / mode / javascript / javascript.js

javascript.js in Smart Forms – when you need more than just a contact form trunk, at js/utilities/codeMirror/mode/javascript/javascript.js

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