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