codemirror.css
5 years ago
codemirror.js
5 years ago
css.js
5 years ago
htmlmixed.js
5 years ago
index.php
5 years ago
javascript.js
5 years ago
xml.js
5 years ago
javascript.js
901 lines
| 1 | // CodeMirror, copyright (c) by Marijn Haverbeke and others |
| 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 | if (type == "[") return cont(expression, expect(']'), expect(':'), proppattern); |
| 659 | return cont(expect(":"), pattern, maybeAssign); |
| 660 | } |
| 661 | function eltpattern() { |
| 662 | return pass(pattern, maybeAssign) |
| 663 | } |
| 664 | function maybeAssign(_type, value) { |
| 665 | if (value == "=") return cont(expressionNoComma); |
| 666 | } |
| 667 | function vardefCont(type) { |
| 668 | if (type == ",") return cont(vardef); |
| 669 | } |
| 670 | function maybeelse(type, value) { |
| 671 | if (type == "keyword b" && value == "else") return cont(pushlex("form", "else"), statement, poplex); |
| 672 | } |
| 673 | function forspec(type, value) { |
| 674 | if (value == "await") return cont(forspec); |
| 675 | if (type == "(") return cont(pushlex(")"), forspec1, expect(")"), poplex); |
| 676 | } |
| 677 | function forspec1(type) { |
| 678 | if (type == "var") return cont(vardef, expect(";"), forspec2); |
| 679 | if (type == ";") return cont(forspec2); |
| 680 | if (type == "variable") return cont(formaybeinof); |
| 681 | return pass(expression, expect(";"), forspec2); |
| 682 | } |
| 683 | function formaybeinof(_type, value) { |
| 684 | if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); } |
| 685 | return cont(maybeoperatorComma, forspec2); |
| 686 | } |
| 687 | function forspec2(type, value) { |
| 688 | if (type == ";") return cont(forspec3); |
| 689 | if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); } |
| 690 | return pass(expression, expect(";"), forspec3); |
| 691 | } |
| 692 | function forspec3(type) { |
| 693 | if (type != ")") cont(expression); |
| 694 | } |
| 695 | function functiondef(type, value) { |
| 696 | if (value == "*") {cx.marked = "keyword"; return cont(functiondef);} |
| 697 | if (type == "variable") {register(value); return cont(functiondef);} |
| 698 | if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, mayberettype, statement, popcontext); |
| 699 | if (isTS && value == "<") return cont(pushlex(">"), commasep(typeparam, ">"), poplex, functiondef) |
| 700 | } |
| 701 | function funarg(type, value) { |
| 702 | if (value == "@") cont(expression, funarg) |
| 703 | if (type == "spread") return cont(funarg); |
| 704 | if (isTS && isModifier(value)) { cx.marked = "keyword"; return cont(funarg); } |
| 705 | return pass(pattern, maybetype, maybeAssign); |
| 706 | } |
| 707 | function classExpression(type, value) { |
| 708 | // Class expressions may have an optional name. |
| 709 | if (type == "variable") return className(type, value); |
| 710 | return classNameAfter(type, value); |
| 711 | } |
| 712 | function className(type, value) { |
| 713 | if (type == "variable") {register(value); return cont(classNameAfter);} |
| 714 | } |
| 715 | function classNameAfter(type, value) { |
| 716 | if (value == "<") return cont(pushlex(">"), commasep(typeparam, ">"), poplex, classNameAfter) |
| 717 | if (value == "extends" || value == "implements" || (isTS && type == ",")) { |
| 718 | if (value == "implements") cx.marked = "keyword"; |
| 719 | return cont(isTS ? typeexpr : expression, classNameAfter); |
| 720 | } |
| 721 | if (type == "{") return cont(pushlex("}"), classBody, poplex); |
| 722 | } |
| 723 | function classBody(type, value) { |
| 724 | if (type == "async" || |
| 725 | (type == "variable" && |
| 726 | (value == "static" || value == "get" || value == "set" || (isTS && isModifier(value))) && |
| 727 | cx.stream.match(/^\s+[\w$\xa1-\uffff]/, false))) { |
| 728 | cx.marked = "keyword"; |
| 729 | return cont(classBody); |
| 730 | } |
| 731 | if (type == "variable" || cx.style == "keyword") { |
| 732 | cx.marked = "property"; |
| 733 | return cont(isTS ? classfield : functiondef, classBody); |
| 734 | } |
| 735 | if (type == "[") |
| 736 | return cont(expression, maybetype, expect("]"), isTS ? classfield : functiondef, classBody) |
| 737 | if (value == "*") { |
| 738 | cx.marked = "keyword"; |
| 739 | return cont(classBody); |
| 740 | } |
| 741 | if (type == ";") return cont(classBody); |
| 742 | if (type == "}") return cont(); |
| 743 | if (value == "@") return cont(expression, classBody) |
| 744 | } |
| 745 | function classfield(type, value) { |
| 746 | if (value == "?") return cont(classfield) |
| 747 | if (type == ":") return cont(typeexpr, maybeAssign) |
| 748 | if (value == "=") return cont(expressionNoComma) |
| 749 | return pass(functiondef) |
| 750 | } |
| 751 | function afterExport(type, value) { |
| 752 | if (value == "*") { cx.marked = "keyword"; return cont(maybeFrom, expect(";")); } |
| 753 | if (value == "default") { cx.marked = "keyword"; return cont(expression, expect(";")); } |
| 754 | if (type == "{") return cont(commasep(exportField, "}"), maybeFrom, expect(";")); |
| 755 | return pass(statement); |
| 756 | } |
| 757 | function exportField(type, value) { |
| 758 | if (value == "as") { cx.marked = "keyword"; return cont(expect("variable")); } |
| 759 | if (type == "variable") return pass(expressionNoComma, exportField); |
| 760 | } |
| 761 | function afterImport(type) { |
| 762 | if (type == "string") return cont(); |
| 763 | if (type == "(") return pass(expression); |
| 764 | return pass(importSpec, maybeMoreImports, maybeFrom); |
| 765 | } |
| 766 | function importSpec(type, value) { |
| 767 | if (type == "{") return contCommasep(importSpec, "}"); |
| 768 | if (type == "variable") register(value); |
| 769 | if (value == "*") cx.marked = "keyword"; |
| 770 | return cont(maybeAs); |
| 771 | } |
| 772 | function maybeMoreImports(type) { |
| 773 | if (type == ",") return cont(importSpec, maybeMoreImports) |
| 774 | } |
| 775 | function maybeAs(_type, value) { |
| 776 | if (value == "as") { cx.marked = "keyword"; return cont(importSpec); } |
| 777 | } |
| 778 | function maybeFrom(_type, value) { |
| 779 | if (value == "from") { cx.marked = "keyword"; return cont(expression); } |
| 780 | } |
| 781 | function arrayLiteral(type) { |
| 782 | if (type == "]") return cont(); |
| 783 | return pass(commasep(expressionNoComma, "]")); |
| 784 | } |
| 785 | function enumdef() { |
| 786 | return pass(pushlex("form"), pattern, expect("{"), pushlex("}"), commasep(enummember, "}"), poplex, poplex) |
| 787 | } |
| 788 | function enummember() { |
| 789 | return pass(pattern, maybeAssign); |
| 790 | } |
| 791 | |
| 792 | function isContinuedStatement(state, textAfter) { |
| 793 | return state.lastType == "operator" || state.lastType == "," || |
| 794 | isOperatorChar.test(textAfter.charAt(0)) || |
| 795 | /[,.]/.test(textAfter.charAt(0)); |
| 796 | } |
| 797 | |
| 798 | function expressionAllowed(stream, state, backUp) { |
| 799 | return state.tokenize == tokenBase && |
| 800 | /^(?:operator|sof|keyword [bcd]|case|new|export|default|spread|[\[{}\(,;:]|=>)$/.test(state.lastType) || |
| 801 | (state.lastType == "quasi" && /\{\s*$/.test(stream.string.slice(0, stream.pos - (backUp || 0)))) |
| 802 | } |
| 803 | |
| 804 | // Interface |
| 805 | |
| 806 | return { |
| 807 | startState: function(basecolumn) { |
| 808 | var state = { |
| 809 | tokenize: tokenBase, |
| 810 | lastType: "sof", |
| 811 | cc: [], |
| 812 | lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false), |
| 813 | localVars: parserConfig.localVars, |
| 814 | context: parserConfig.localVars && new Context(null, null, false), |
| 815 | indented: basecolumn || 0 |
| 816 | }; |
| 817 | if (parserConfig.globalVars && typeof parserConfig.globalVars == "object") |
| 818 | state.globalVars = parserConfig.globalVars; |
| 819 | return state; |
| 820 | }, |
| 821 | |
| 822 | token: function(stream, state) { |
| 823 | if (stream.sol()) { |
| 824 | if (!state.lexical.hasOwnProperty("align")) |
| 825 | state.lexical.align = false; |
| 826 | state.indented = stream.indentation(); |
| 827 | findFatArrow(stream, state); |
| 828 | } |
| 829 | if (state.tokenize != tokenComment && stream.eatSpace()) return null; |
| 830 | var style = state.tokenize(stream, state); |
| 831 | if (type == "comment") return style; |
| 832 | state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type; |
| 833 | return parseJS(state, style, type, content, stream); |
| 834 | }, |
| 835 | |
| 836 | indent: function(state, textAfter) { |
| 837 | if (state.tokenize == tokenComment) return CodeMirror.Pass; |
| 838 | if (state.tokenize != tokenBase) return 0; |
| 839 | var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical, top |
| 840 | // Kludge to prevent 'maybelse' from blocking lexical scope pops |
| 841 | if (!/^\s*else\b/.test(textAfter)) for (var i = state.cc.length - 1; i >= 0; --i) { |
| 842 | var c = state.cc[i]; |
| 843 | if (c == poplex) lexical = lexical.prev; |
| 844 | else if (c != maybeelse) break; |
| 845 | } |
| 846 | while ((lexical.type == "stat" || lexical.type == "form") && |
| 847 | (firstChar == "}" || ((top = state.cc[state.cc.length - 1]) && |
| 848 | (top == maybeoperatorComma || top == maybeoperatorNoComma) && |
| 849 | !/^[,\.=+\-*:?[\(]/.test(textAfter)))) |
| 850 | lexical = lexical.prev; |
| 851 | if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat") |
| 852 | lexical = lexical.prev; |
| 853 | var type = lexical.type, closing = firstChar == type; |
| 854 | |
| 855 | if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? lexical.info.length + 1 : 0); |
| 856 | else if (type == "form" && firstChar == "{") return lexical.indented; |
| 857 | else if (type == "form") return lexical.indented + indentUnit; |
| 858 | else if (type == "stat") |
| 859 | return lexical.indented + (isContinuedStatement(state, textAfter) ? statementIndent || indentUnit : 0); |
| 860 | else if (lexical.info == "switch" && !closing && parserConfig.doubleIndentSwitch != false) |
| 861 | return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit); |
| 862 | else if (lexical.align) return lexical.column + (closing ? 0 : 1); |
| 863 | else return lexical.indented + (closing ? 0 : indentUnit); |
| 864 | }, |
| 865 | |
| 866 | electricInput: /^\s*(?:case .*?:|default:|\{|\})$/, |
| 867 | blockCommentStart: jsonMode ? null : "/*", |
| 868 | blockCommentEnd: jsonMode ? null : "*/", |
| 869 | blockCommentContinue: jsonMode ? null : " * ", |
| 870 | lineComment: jsonMode ? null : "//", |
| 871 | fold: "brace", |
| 872 | closeBrackets: "()[]{}''\"\"``", |
| 873 | |
| 874 | helperType: jsonMode ? "json" : "javascript", |
| 875 | jsonldMode: jsonldMode, |
| 876 | jsonMode: jsonMode, |
| 877 | |
| 878 | expressionAllowed: expressionAllowed, |
| 879 | |
| 880 | skipExpression: function(state) { |
| 881 | var top = state.cc[state.cc.length - 1] |
| 882 | if (top == expression || top == expressionNoComma) state.cc.pop() |
| 883 | } |
| 884 | }; |
| 885 | }); |
| 886 | |
| 887 | CodeMirror.registerHelper("wordChars", "javascript", /[\w$]/); |
| 888 | |
| 889 | CodeMirror.defineMIME("text/javascript", "javascript"); |
| 890 | CodeMirror.defineMIME("text/ecmascript", "javascript"); |
| 891 | CodeMirror.defineMIME("application/javascript", "javascript"); |
| 892 | CodeMirror.defineMIME("application/x-javascript", "javascript"); |
| 893 | CodeMirror.defineMIME("application/ecmascript", "javascript"); |
| 894 | CodeMirror.defineMIME("application/json", {name: "javascript", json: true}); |
| 895 | CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true}); |
| 896 | CodeMirror.defineMIME("application/ld+json", {name: "javascript", jsonld: true}); |
| 897 | CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true }); |
| 898 | CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true }); |
| 899 | |
| 900 | }); |
| 901 |