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