Index: Source/devtools/front_end/cm/javascript.js |
diff --git a/Source/devtools/front_end/cm/javascript.js b/Source/devtools/front_end/cm/javascript.js |
index f27c06346c63ee9118048f25226597cf1a4b26c2..315674be74c46ca189cf946f8016e5d7b1dfd9ae 100644 |
--- a/Source/devtools/front_end/cm/javascript.js |
+++ b/Source/devtools/front_end/cm/javascript.js |
@@ -1,9 +1,23 @@ |
+// CodeMirror, copyright (c) by Marijn Haverbeke and others |
+// Distributed under an MIT license: http://codemirror.net/LICENSE |
+ |
// TODO actually recognize syntax of TypeScript constructs |
+(function(mod) { |
+ if (typeof exports == "object" && typeof module == "object") // CommonJS |
+ mod(require("../../lib/codemirror")); |
+ else if (typeof define == "function" && define.amd) // AMD |
+ define(["../../lib/codemirror"], mod); |
+ else // Plain browser env |
+ mod(CodeMirror); |
+})(function(CodeMirror) { |
+"use strict"; |
+ |
CodeMirror.defineMode("javascript", function(config, parserConfig) { |
var indentUnit = config.indentUnit; |
var statementIndent = parserConfig.statementIndent; |
- var jsonMode = parserConfig.json; |
+ var jsonldMode = parserConfig.jsonld; |
+ var jsonMode = parserConfig.json || jsonldMode; |
var isTS = parserConfig.typescript; |
// Tokenizer |
@@ -15,7 +29,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
var jsKeywords = { |
"if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B, |
- "return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C, |
+ "return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C, "debugger": C, |
"var": kw("var"), "const": kw("var"), "let": kw("var"), |
"function": kw("function"), "catch": kw("catch"), |
"for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"), |
@@ -53,15 +67,18 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
}(); |
var isOperatorChar = /[+\-*&%=<>!?|~^]/; |
+ var isJsonldKeyword = /^@(context|id|value|language|type|container|list|set|reverse|index|base|vocab|graph)"/; |
- function nextUntilUnescaped(stream, end) { |
- var escaped = false, next; |
+ function readRegexp(stream) { |
+ var escaped = false, next, inSet = false; |
while ((next = stream.next()) != null) { |
- if (next == end && !escaped) |
- return false; |
+ if (!escaped) { |
+ if (next == "/" && !inSet) return; |
+ if (next == "[") inSet = true; |
+ else if (inSet && next == "]") inSet = false; |
+ } |
escaped = !escaped && next == "\\"; |
} |
- return escaped; |
} |
// Used as scratch variables to communicate multiple values without |
@@ -83,7 +100,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
} else if (/[\[\]{}\(\),;\:\.]/.test(ch)) { |
return ret(ch); |
} else if (ch == "=" && stream.eat(">")) { |
- return ret("=>"); |
+ return ret("=>", "operator"); |
} else if (ch == "0" && stream.eat(/x/i)) { |
stream.eatWhile(/[\da-f]/i); |
return ret("number", "number"); |
@@ -99,12 +116,12 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
return ret("comment", "comment"); |
} else if (state.lastType == "operator" || state.lastType == "keyword c" || |
state.lastType == "sof" || /^[\[{}\(,;:]$/.test(state.lastType)) { |
- nextUntilUnescaped(stream, "/"); |
+ readRegexp(stream); |
stream.eatWhile(/[gimy]/); // 'y' is "sticky" option in Mozilla |
return ret("regexp", "string-2"); |
} else { |
stream.eatWhile(isOperatorChar); |
- return ret("operator", null, stream.current()); |
+ return ret("operator", "operator", stream.current()); |
} |
} else if (ch == "`") { |
state.tokenize = tokenQuasi; |
@@ -114,7 +131,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
return ret("error", "error"); |
} else if (isOperatorChar.test(ch)) { |
stream.eatWhile(isOperatorChar); |
- return ret("operator", null, stream.current()); |
+ return ret("operator", "operator", stream.current()); |
} else { |
stream.eatWhile(/[\w\$_]/); |
var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word]; |
@@ -125,8 +142,16 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
function tokenString(quote) { |
return function(stream, state) { |
- if (!nextUntilUnescaped(stream, quote)) |
+ var escaped = false, next; |
+ if (jsonldMode && stream.peek() == "@" && stream.match(isJsonldKeyword)){ |
state.tokenize = tokenBase; |
+ return ret("jsonld-keyword", "meta"); |
+ } |
+ while ((next = stream.next()) != null) { |
+ if (next == quote && !escaped) break; |
+ escaped = !escaped && next == "\\"; |
+ } |
+ if (!escaped) state.tokenize = tokenBase; |
return ret("string", "string"); |
}; |
} |
@@ -189,7 +214,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
// Parser |
- var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true, "this": true}; |
+ var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true, "this": true, "jsonld-keyword": true}; |
function JSLexical(indented, column, type, align, prev, info) { |
this.indented = indented; |
@@ -213,7 +238,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
var cc = state.cc; |
// Communicate our context to the combinators. |
// (Less wasteful than consing up a hundred closures on every call.) |
- cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc; |
+ cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc; cx.style = style; |
if (!state.lexical.hasOwnProperty("align")) |
state.lexical.align = true; |
@@ -289,11 +314,12 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
poplex.lex = true; |
function expect(wanted) { |
- return function(type) { |
+ function exp(type) { |
if (type == wanted) return cont(); |
else if (wanted == ";") return pass(); |
- else return cont(arguments.callee); |
+ else return cont(exp); |
}; |
+ return exp; |
} |
function statement(type, value) { |
@@ -302,9 +328,13 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
if (type == "keyword b") return cont(pushlex("form"), statement, poplex); |
if (type == "{") return cont(pushlex("}"), block, poplex); |
if (type == ";") return cont(); |
- if (type == "if") return cont(pushlex("form"), expression, statement, poplex, maybeelse); |
+ if (type == "if") { |
+ if (cx.state.lexical.info == "else" && cx.state.cc[cx.state.cc.length - 1] == poplex) |
+ cx.state.cc.pop()(); |
+ return cont(pushlex("form"), expression, statement, poplex, maybeelse); |
+ } |
if (type == "function") return cont(functiondef); |
- if (type == "for") return cont(pushlex("form"), forspec, poplex, statement, poplex); |
+ if (type == "for") return cont(pushlex("form"), forspec, statement, poplex); |
if (type == "variable") return cont(pushlex("stat"), maybelabel); |
if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"), |
block, poplex, poplex); |
@@ -313,7 +343,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"), |
statement, poplex, popcontext); |
if (type == "module") return cont(pushlex("form"), pushcontext, afterModule, popcontext, poplex); |
- if (type == "class") return cont(pushlex("form"), className, objlit, poplex); |
+ if (type == "class") return cont(pushlex("form"), className, poplex); |
if (type == "export") return cont(pushlex("form"), afterExport, poplex); |
if (type == "import") return cont(pushlex("form"), afterImport, poplex); |
return pass(pushlex("stat"), expression, expect(";"), poplex); |
@@ -327,18 +357,19 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
function expressionInner(type, noComma) { |
if (cx.state.fatArrowAt == cx.stream.start) { |
var body = noComma ? arrowBodyNoComma : arrowBody; |
- if (type == "(") return cont(pushcontext, commasep(pattern, ")"), expect("=>"), body, popcontext); |
+ if (type == "(") return cont(pushcontext, pushlex(")"), commasep(pattern, ")"), poplex, expect("=>"), body, popcontext); |
else if (type == "variable") return pass(pushcontext, pattern, expect("=>"), body, popcontext); |
} |
var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma; |
if (atomicTypes.hasOwnProperty(type)) return cont(maybeop); |
- if (type == "function") return cont(functiondef); |
+ if (type == "function") return cont(functiondef, maybeop); |
if (type == "keyword c") return cont(noComma ? maybeexpressionNoComma : maybeexpression); |
if (type == "(") return cont(pushlex(")"), maybeexpression, comprehension, expect(")"), poplex, maybeop); |
if (type == "operator" || type == "spread") return cont(noComma ? expressionNoComma : expression); |
- if (type == "[") return cont(pushlex("]"), expressionNoComma, maybeArrayComprehension, poplex, maybeop); |
- if (type == "{") return cont(commasep(objprop, "}"), maybeop); |
+ if (type == "[") return cont(pushlex("]"), arrayLiteral, poplex, maybeop); |
+ if (type == "{") return contCommasep(objprop, "}", null, maybeop); |
+ if (type == "quasi") { return pass(quasi, maybeop); } |
return cont(); |
} |
function maybeexpression(type) { |
@@ -363,22 +394,22 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
if (value == "?") return cont(expression, expect(":"), expr); |
return cont(expr); |
} |
- if (type == "quasi") { cx.cc.push(me); return quasi(value); } |
+ if (type == "quasi") { return pass(quasi, me); } |
if (type == ";") return; |
- if (type == "(") return cont(commasep(expressionNoComma, ")", "call"), me); |
+ if (type == "(") return contCommasep(expressionNoComma, ")", "call", me); |
if (type == ".") return cont(property, me); |
if (type == "[") return cont(pushlex("]"), maybeexpression, expect("]"), poplex, me); |
} |
- function quasi(value) { |
- if (!value) debugger; |
- if (value.slice(value.length - 2) != "${") return cont(); |
+ function quasi(type, value) { |
+ if (type != "quasi") return pass(); |
+ if (value.slice(value.length - 2) != "${") return cont(quasi); |
return cont(expression, continueQuasi); |
} |
function continueQuasi(type) { |
if (type == "}") { |
cx.marked = "string-2"; |
cx.state.tokenize = tokenQuasi; |
- return cont(); |
+ return cont(quasi); |
} |
} |
function arrowBody(type) { |
@@ -399,15 +430,18 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
if (type == "variable") {cx.marked = "property"; return cont();} |
} |
function objprop(type, value) { |
- if (type == "variable") { |
+ if (type == "variable" || cx.style == "keyword") { |
cx.marked = "property"; |
if (value == "get" || value == "set") return cont(getterSetter); |
+ return cont(afterprop); |
} else if (type == "number" || type == "string") { |
- cx.marked = type + " property"; |
+ cx.marked = jsonldMode ? "property" : (cx.style + " property"); |
+ return cont(afterprop); |
+ } else if (type == "jsonld-keyword") { |
+ return cont(afterprop); |
} else if (type == "[") { |
return cont(expression, expect("]"), afterprop); |
} |
- if (atomicTypes.hasOwnProperty(type)) return cont(afterprop); |
} |
function getterSetter(type) { |
if (type != "variable") return pass(afterprop); |
@@ -418,7 +452,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
if (type == ":") return cont(expressionNoComma); |
if (type == "(") return pass(functiondef); |
} |
- function commasep(what, end, info) { |
+ function commasep(what, end) { |
function proceed(type) { |
if (type == ",") { |
var lex = cx.state.lexical; |
@@ -430,10 +464,14 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
} |
return function(type) { |
if (type == end) return cont(); |
- if (info === false) return pass(what, proceed); |
- return pass(pushlex(end, info), what, proceed, poplex); |
+ return pass(what, proceed); |
}; |
} |
+ function contCommasep(what, end, info) { |
+ for (var i = 3; i < arguments.length; i++) |
+ cx.cc.push(arguments[i]); |
+ return cont(pushlex(end, info), commasep(what, end), poplex); |
+ } |
function block(type) { |
if (type == "}") return cont(); |
return pass(statement, block); |
@@ -449,8 +487,8 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
} |
function pattern(type, value) { |
if (type == "variable") { register(value); return cont(); } |
- if (type == "[") return cont(commasep(pattern, "]")); |
- if (type == "{") return cont(commasep(proppattern, "}")); |
+ if (type == "[") return contCommasep(pattern, "]"); |
+ if (type == "{") return contCommasep(proppattern, "}"); |
} |
function proppattern(type, value) { |
if (type == "variable" && !cx.stream.match(/^\s*:/, false)) { |
@@ -467,10 +505,10 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
if (type == ",") return cont(vardef); |
} |
function maybeelse(type, value) { |
- if (type == "keyword b" && value == "else") return cont(pushlex("form"), statement, poplex); |
+ if (type == "keyword b" && value == "else") return cont(pushlex("form", "else"), statement, poplex); |
} |
function forspec(type) { |
- if (type == "(") return cont(pushlex(")"), forspec1, expect(")")); |
+ if (type == "(") return cont(pushlex(")"), forspec1, expect(")"), poplex); |
} |
function forspec1(type) { |
if (type == "var") return cont(vardef, expect(";"), forspec2); |
@@ -493,7 +531,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
function functiondef(type, value) { |
if (value == "*") {cx.marked = "keyword"; return cont(functiondef);} |
if (type == "variable") {register(value); return cont(functiondef);} |
- if (type == "(") return cont(pushcontext, commasep(funarg, ")"), statement, popcontext); |
+ if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, statement, popcontext); |
} |
function funarg(type) { |
if (type == "spread") return cont(funarg); |
@@ -502,11 +540,27 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
function className(type, value) { |
if (type == "variable") {register(value); return cont(classNameAfter);} |
} |
- function classNameAfter(_type, value) { |
- if (value == "extends") return cont(expression); |
+ function classNameAfter(type, value) { |
+ if (value == "extends") return cont(expression, classNameAfter); |
+ if (type == "{") return cont(pushlex("}"), classBody, poplex); |
} |
- function objlit(type) { |
- if (type == "{") return cont(commasep(objprop, "}")); |
+ function classBody(type, value) { |
+ if (type == "variable" || cx.style == "keyword") { |
+ cx.marked = "property"; |
+ if (value == "get" || value == "set") return cont(classGetterSetter, functiondef, classBody); |
+ return cont(functiondef, classBody); |
+ } |
+ if (value == "*") { |
+ cx.marked = "keyword"; |
+ return cont(classBody); |
+ } |
+ if (type == ";") return cont(classBody); |
+ if (type == "}") return cont(); |
+ } |
+ function classGetterSetter(type) { |
+ if (type != "variable") return pass(); |
+ cx.marked = "property"; |
+ return cont(); |
} |
function afterModule(type, value) { |
if (type == "string") return cont(statement); |
@@ -522,17 +576,21 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
return pass(importSpec, maybeFrom); |
} |
function importSpec(type, value) { |
- if (type == "{") return cont(commasep(importSpec, "}")); |
+ if (type == "{") return contCommasep(importSpec, "}"); |
if (type == "variable") register(value); |
return cont(); |
} |
function maybeFrom(_type, value) { |
if (value == "from") { cx.marked = "keyword"; return cont(expression); } |
} |
+ function arrayLiteral(type) { |
+ if (type == "]") return cont(); |
+ return pass(expressionNoComma, maybeArrayComprehension); |
+ } |
function maybeArrayComprehension(type) { |
- if (type == "for") return pass(comprehension); |
- if (type == ",") return cont(commasep(expressionNoComma, "]", false)); |
- return pass(commasep(expressionNoComma, "]", false)); |
+ if (type == "for") return pass(comprehension, expect("]")); |
+ if (type == ",") return cont(commasep(expressionNoComma, "]")); |
+ return pass(commasep(expressionNoComma, "]")); |
} |
function comprehension(type) { |
if (type == "for") return cont(forspec, comprehension); |
@@ -552,7 +610,8 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
context: parserConfig.localVars && {vars: parserConfig.localVars}, |
indented: 0 |
}; |
- if (parserConfig.globalVars) state.globalVars = parserConfig.globalVars; |
+ if (parserConfig.globalVars && typeof parserConfig.globalVars == "object") |
+ state.globalVars = parserConfig.globalVars; |
return state; |
}, |
@@ -575,7 +634,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
if (state.tokenize != tokenBase) return 0; |
var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical; |
// Kludge to prevent 'maybelse' from blocking lexical scope pops |
- for (var i = state.cc.length - 1; i >= 0; --i) { |
+ if (!/^\s*else\b/.test(textAfter)) for (var i = state.cc.length - 1; i >= 0; --i) { |
var c = state.cc[i]; |
if (c == poplex) lexical = lexical.prev; |
else if (c != maybeelse) break; |
@@ -603,15 +662,22 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) { |
fold: "brace", |
helperType: jsonMode ? "json" : "javascript", |
+ jsonldMode: jsonldMode, |
jsonMode: jsonMode |
}; |
}); |
+CodeMirror.registerHelper("wordChars", "javascript", /[\\w$]/); |
+ |
CodeMirror.defineMIME("text/javascript", "javascript"); |
CodeMirror.defineMIME("text/ecmascript", "javascript"); |
CodeMirror.defineMIME("application/javascript", "javascript"); |
+CodeMirror.defineMIME("application/x-javascript", "javascript"); |
CodeMirror.defineMIME("application/ecmascript", "javascript"); |
CodeMirror.defineMIME("application/json", {name: "javascript", json: true}); |
CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true}); |
+CodeMirror.defineMIME("application/ld+json", {name: "javascript", jsonld: true}); |
CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true }); |
CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true }); |
+ |
+}); |