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