michael@0: // TODO actually recognize syntax of TypeScript constructs michael@0: michael@0: (function(mod) { michael@0: if (typeof exports == "object" && typeof module == "object") // CommonJS michael@0: mod(require("../../lib/codemirror")); michael@0: else if (typeof define == "function" && define.amd) // AMD michael@0: define(["../../lib/codemirror"], mod); michael@0: else // Plain browser env michael@0: mod(CodeMirror); michael@0: })(function(CodeMirror) { michael@0: "use strict"; michael@0: michael@0: CodeMirror.defineMode("javascript", function(config, parserConfig) { michael@0: var indentUnit = config.indentUnit; michael@0: var statementIndent = parserConfig.statementIndent; michael@0: var jsonldMode = parserConfig.jsonld; michael@0: var jsonMode = parserConfig.json || jsonldMode; michael@0: var isTS = parserConfig.typescript; michael@0: michael@0: // Tokenizer michael@0: michael@0: var keywords = function(){ michael@0: function kw(type) {return {type: type, style: "keyword"};} michael@0: var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c"); michael@0: var operator = kw("operator"), atom = {type: "atom", style: "atom"}; michael@0: michael@0: var jsKeywords = { michael@0: "if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B, michael@0: "return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C, "debugger": C, michael@0: "var": kw("var"), "const": kw("var"), "let": kw("var"), michael@0: "function": kw("function"), "catch": kw("catch"), michael@0: "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"), michael@0: "in": operator, "typeof": operator, "instanceof": operator, michael@0: "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom, michael@0: "this": kw("this"), "module": kw("module"), "class": kw("class"), "super": kw("atom"), michael@0: "yield": C, "export": kw("export"), "import": kw("import"), "extends": C michael@0: }; michael@0: michael@0: // Extend the 'normal' keywords with the TypeScript language extensions michael@0: if (isTS) { michael@0: var type = {type: "variable", style: "variable-3"}; michael@0: var tsKeywords = { michael@0: // object-like things michael@0: "interface": kw("interface"), michael@0: "extends": kw("extends"), michael@0: "constructor": kw("constructor"), michael@0: michael@0: // scope modifiers michael@0: "public": kw("public"), michael@0: "private": kw("private"), michael@0: "protected": kw("protected"), michael@0: "static": kw("static"), michael@0: michael@0: // types michael@0: "string": type, "number": type, "bool": type, "any": type michael@0: }; michael@0: michael@0: for (var attr in tsKeywords) { michael@0: jsKeywords[attr] = tsKeywords[attr]; michael@0: } michael@0: } michael@0: michael@0: return jsKeywords; michael@0: }(); michael@0: michael@0: var isOperatorChar = /[+\-*&%=<>!?|~^]/; michael@0: var isJsonldKeyword = /^@(context|id|value|language|type|container|list|set|reverse|index|base|vocab|graph)"/; michael@0: michael@0: function readRegexp(stream) { michael@0: var escaped = false, next, inSet = false; michael@0: while ((next = stream.next()) != null) { michael@0: if (!escaped) { michael@0: if (next == "/" && !inSet) return; michael@0: if (next == "[") inSet = true; michael@0: else if (inSet && next == "]") inSet = false; michael@0: } michael@0: escaped = !escaped && next == "\\"; michael@0: } michael@0: } michael@0: michael@0: // Used as scratch variables to communicate multiple values without michael@0: // consing up tons of objects. michael@0: var type, content; michael@0: function ret(tp, style, cont) { michael@0: type = tp; content = cont; michael@0: return style; michael@0: } michael@0: function tokenBase(stream, state) { michael@0: var ch = stream.next(); michael@0: if (ch == '"' || ch == "'") { michael@0: state.tokenize = tokenString(ch); michael@0: return state.tokenize(stream, state); michael@0: } else if (ch == "." && stream.match(/^\d+(?:[eE][+\-]?\d+)?/)) { michael@0: return ret("number", "number"); michael@0: } else if (ch == "." && stream.match("..")) { michael@0: return ret("spread", "meta"); michael@0: } else if (/[\[\]{}\(\),;\:\.]/.test(ch)) { michael@0: return ret(ch); michael@0: } else if (ch == "=" && stream.eat(">")) { michael@0: return ret("=>", "operator"); michael@0: } else if (ch == "0" && stream.eat(/x/i)) { michael@0: stream.eatWhile(/[\da-f]/i); michael@0: return ret("number", "number"); michael@0: } else if (/\d/.test(ch)) { michael@0: stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/); michael@0: return ret("number", "number"); michael@0: } else if (ch == "/") { michael@0: if (stream.eat("*")) { michael@0: state.tokenize = tokenComment; michael@0: return tokenComment(stream, state); michael@0: } else if (stream.eat("/")) { michael@0: stream.skipToEnd(); michael@0: return ret("comment", "comment"); michael@0: } else if (state.lastType == "operator" || state.lastType == "keyword c" || michael@0: state.lastType == "sof" || /^[\[{}\(,;:]$/.test(state.lastType)) { michael@0: readRegexp(stream); michael@0: stream.eatWhile(/[gimy]/); // 'y' is "sticky" option in Mozilla michael@0: return ret("regexp", "string-2"); michael@0: } else { michael@0: stream.eatWhile(isOperatorChar); michael@0: return ret("operator", "operator", stream.current()); michael@0: } michael@0: } else if (ch == "`") { michael@0: state.tokenize = tokenQuasi; michael@0: return tokenQuasi(stream, state); michael@0: } else if (ch == "#") { michael@0: stream.skipToEnd(); michael@0: return ret("error", "error"); michael@0: } else if (isOperatorChar.test(ch)) { michael@0: stream.eatWhile(isOperatorChar); michael@0: return ret("operator", "operator", stream.current()); michael@0: } else { michael@0: stream.eatWhile(/[\w\$_]/); michael@0: var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word]; michael@0: return (known && state.lastType != ".") ? ret(known.type, known.style, word) : michael@0: ret("variable", "variable", word); michael@0: } michael@0: } michael@0: michael@0: function tokenString(quote) { michael@0: return function(stream, state) { michael@0: var escaped = false, next; michael@0: if (jsonldMode && stream.peek() == "@" && stream.match(isJsonldKeyword)){ michael@0: state.tokenize = tokenBase; michael@0: return ret("jsonld-keyword", "meta"); michael@0: } michael@0: while ((next = stream.next()) != null) { michael@0: if (next == quote && !escaped) break; michael@0: escaped = !escaped && next == "\\"; michael@0: } michael@0: if (!escaped) state.tokenize = tokenBase; michael@0: return ret("string", "string"); michael@0: }; michael@0: } michael@0: michael@0: function tokenComment(stream, state) { michael@0: var maybeEnd = false, ch; michael@0: while (ch = stream.next()) { michael@0: if (ch == "/" && maybeEnd) { michael@0: state.tokenize = tokenBase; michael@0: break; michael@0: } michael@0: maybeEnd = (ch == "*"); michael@0: } michael@0: return ret("comment", "comment"); michael@0: } michael@0: michael@0: function tokenQuasi(stream, state) { michael@0: var escaped = false, next; michael@0: while ((next = stream.next()) != null) { michael@0: if (!escaped && (next == "`" || next == "$" && stream.eat("{"))) { michael@0: state.tokenize = tokenBase; michael@0: break; michael@0: } michael@0: escaped = !escaped && next == "\\"; michael@0: } michael@0: return ret("quasi", "string-2", stream.current()); michael@0: } michael@0: michael@0: var brackets = "([{}])"; michael@0: // This is a crude lookahead trick to try and notice that we're michael@0: // parsing the argument patterns for a fat-arrow function before we michael@0: // actually hit the arrow token. It only works if the arrow is on michael@0: // the same line as the arguments and there's no strange noise michael@0: // (comments) in between. Fallback is to only notice when we hit the michael@0: // arrow, and not declare the arguments as locals for the arrow michael@0: // body. michael@0: function findFatArrow(stream, state) { michael@0: if (state.fatArrowAt) state.fatArrowAt = null; michael@0: var arrow = stream.string.indexOf("=>", stream.start); michael@0: if (arrow < 0) return; michael@0: michael@0: var depth = 0, sawSomething = false; michael@0: for (var pos = arrow - 1; pos >= 0; --pos) { michael@0: var ch = stream.string.charAt(pos); michael@0: var bracket = brackets.indexOf(ch); michael@0: if (bracket >= 0 && bracket < 3) { michael@0: if (!depth) { ++pos; break; } michael@0: if (--depth == 0) break; michael@0: } else if (bracket >= 3 && bracket < 6) { michael@0: ++depth; michael@0: } else if (/[$\w]/.test(ch)) { michael@0: sawSomething = true; michael@0: } else if (sawSomething && !depth) { michael@0: ++pos; michael@0: break; michael@0: } michael@0: } michael@0: if (sawSomething && !depth) state.fatArrowAt = pos; michael@0: } michael@0: michael@0: // Parser michael@0: michael@0: var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true, "this": true, "jsonld-keyword": true}; michael@0: michael@0: function JSLexical(indented, column, type, align, prev, info) { michael@0: this.indented = indented; michael@0: this.column = column; michael@0: this.type = type; michael@0: this.prev = prev; michael@0: this.info = info; michael@0: if (align != null) this.align = align; michael@0: } michael@0: michael@0: function inScope(state, varname) { michael@0: for (var v = state.localVars; v; v = v.next) michael@0: if (v.name == varname) return true; michael@0: for (var cx = state.context; cx; cx = cx.prev) { michael@0: for (var v = cx.vars; v; v = v.next) michael@0: if (v.name == varname) return true; michael@0: } michael@0: } michael@0: michael@0: function parseJS(state, style, type, content, stream) { michael@0: var cc = state.cc; michael@0: // Communicate our context to the combinators. michael@0: // (Less wasteful than consing up a hundred closures on every call.) michael@0: cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc; michael@0: michael@0: if (!state.lexical.hasOwnProperty("align")) michael@0: state.lexical.align = true; michael@0: michael@0: while(true) { michael@0: var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement; michael@0: if (combinator(type, content)) { michael@0: while(cc.length && cc[cc.length - 1].lex) michael@0: cc.pop()(); michael@0: if (cx.marked) return cx.marked; michael@0: if (type == "variable" && inScope(state, content)) return "variable-2"; michael@0: return style; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Combinator utils michael@0: michael@0: var cx = {state: null, column: null, marked: null, cc: null}; michael@0: function pass() { michael@0: for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]); michael@0: } michael@0: function cont() { michael@0: pass.apply(null, arguments); michael@0: return true; michael@0: } michael@0: function register(varname) { michael@0: function inList(list) { michael@0: for (var v = list; v; v = v.next) michael@0: if (v.name == varname) return true; michael@0: return false; michael@0: } michael@0: var state = cx.state; michael@0: if (state.context) { michael@0: cx.marked = "def"; michael@0: if (inList(state.localVars)) return; michael@0: state.localVars = {name: varname, next: state.localVars}; michael@0: } else { michael@0: if (inList(state.globalVars)) return; michael@0: if (parserConfig.globalVars) michael@0: state.globalVars = {name: varname, next: state.globalVars}; michael@0: } michael@0: } michael@0: michael@0: // Combinators michael@0: michael@0: var defaultVars = {name: "this", next: {name: "arguments"}}; michael@0: function pushcontext() { michael@0: cx.state.context = {prev: cx.state.context, vars: cx.state.localVars}; michael@0: cx.state.localVars = defaultVars; michael@0: } michael@0: function popcontext() { michael@0: cx.state.localVars = cx.state.context.vars; michael@0: cx.state.context = cx.state.context.prev; michael@0: } michael@0: function pushlex(type, info) { michael@0: var result = function() { michael@0: var state = cx.state, indent = state.indented; michael@0: if (state.lexical.type == "stat") indent = state.lexical.indented; michael@0: state.lexical = new JSLexical(indent, cx.stream.column(), type, null, state.lexical, info); michael@0: }; michael@0: result.lex = true; michael@0: return result; michael@0: } michael@0: function poplex() { michael@0: var state = cx.state; michael@0: if (state.lexical.prev) { michael@0: if (state.lexical.type == ")") michael@0: state.indented = state.lexical.indented; michael@0: state.lexical = state.lexical.prev; michael@0: } michael@0: } michael@0: poplex.lex = true; michael@0: michael@0: function expect(wanted) { michael@0: function exp(type) { michael@0: if (type == wanted) return cont(); michael@0: else if (wanted == ";") return pass(); michael@0: else return cont(exp); michael@0: }; michael@0: return exp; michael@0: } michael@0: michael@0: function statement(type, value) { michael@0: if (type == "var") return cont(pushlex("vardef", value.length), vardef, expect(";"), poplex); michael@0: if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex); michael@0: if (type == "keyword b") return cont(pushlex("form"), statement, poplex); michael@0: if (type == "{") return cont(pushlex("}"), block, poplex); michael@0: if (type == ";") return cont(); michael@0: if (type == "if") return cont(pushlex("form"), expression, statement, poplex, maybeelse); michael@0: if (type == "function") return cont(functiondef); michael@0: if (type == "for") return cont(pushlex("form"), forspec, statement, poplex); michael@0: if (type == "variable") return cont(pushlex("stat"), maybelabel); michael@0: if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"), michael@0: block, poplex, poplex); michael@0: if (type == "case") return cont(expression, expect(":")); michael@0: if (type == "default") return cont(expect(":")); michael@0: if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"), michael@0: statement, poplex, popcontext); michael@0: if (type == "module") return cont(pushlex("form"), pushcontext, afterModule, popcontext, poplex); michael@0: if (type == "class") return cont(pushlex("form"), className, objlit, poplex); michael@0: if (type == "export") return cont(pushlex("form"), afterExport, poplex); michael@0: if (type == "import") return cont(pushlex("form"), afterImport, poplex); michael@0: return pass(pushlex("stat"), expression, expect(";"), poplex); michael@0: } michael@0: function expression(type) { michael@0: return expressionInner(type, false); michael@0: } michael@0: function expressionNoComma(type) { michael@0: return expressionInner(type, true); michael@0: } michael@0: function expressionInner(type, noComma) { michael@0: if (cx.state.fatArrowAt == cx.stream.start) { michael@0: var body = noComma ? arrowBodyNoComma : arrowBody; michael@0: if (type == "(") return cont(pushcontext, pushlex(")"), commasep(pattern, ")"), poplex, expect("=>"), body, popcontext); michael@0: else if (type == "variable") return pass(pushcontext, pattern, expect("=>"), body, popcontext); michael@0: } michael@0: michael@0: var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma; michael@0: if (atomicTypes.hasOwnProperty(type)) return cont(maybeop); michael@0: if (type == "function") return cont(functiondef); michael@0: if (type == "keyword c") return cont(noComma ? maybeexpressionNoComma : maybeexpression); michael@0: if (type == "(") return cont(pushlex(")"), maybeexpression, comprehension, expect(")"), poplex, maybeop); michael@0: if (type == "operator" || type == "spread") return cont(noComma ? expressionNoComma : expression); michael@0: if (type == "[") return cont(pushlex("]"), arrayLiteral, poplex, maybeop); michael@0: if (type == "{") return contCommasep(objprop, "}", null, maybeop); michael@0: return cont(); michael@0: } michael@0: function maybeexpression(type) { michael@0: if (type.match(/[;\}\)\],]/)) return pass(); michael@0: return pass(expression); michael@0: } michael@0: function maybeexpressionNoComma(type) { michael@0: if (type.match(/[;\}\)\],]/)) return pass(); michael@0: return pass(expressionNoComma); michael@0: } michael@0: michael@0: function maybeoperatorComma(type, value) { michael@0: if (type == ",") return cont(expression); michael@0: return maybeoperatorNoComma(type, value, false); michael@0: } michael@0: function maybeoperatorNoComma(type, value, noComma) { michael@0: var me = noComma == false ? maybeoperatorComma : maybeoperatorNoComma; michael@0: var expr = noComma == false ? expression : expressionNoComma; michael@0: if (value == "=>") return cont(pushcontext, noComma ? arrowBodyNoComma : arrowBody, popcontext); michael@0: if (type == "operator") { michael@0: if (/\+\+|--/.test(value)) return cont(me); michael@0: if (value == "?") return cont(expression, expect(":"), expr); michael@0: return cont(expr); michael@0: } michael@0: if (type == "quasi") { cx.cc.push(me); return quasi(value); } michael@0: if (type == ";") return; michael@0: if (type == "(") return contCommasep(expressionNoComma, ")", "call", me); michael@0: if (type == ".") return cont(property, me); michael@0: if (type == "[") return cont(pushlex("]"), maybeexpression, expect("]"), poplex, me); michael@0: } michael@0: function quasi(value) { michael@0: if (value.slice(value.length - 2) != "${") return cont(); michael@0: return cont(expression, continueQuasi); michael@0: } michael@0: function continueQuasi(type) { michael@0: if (type == "}") { michael@0: cx.marked = "string-2"; michael@0: cx.state.tokenize = tokenQuasi; michael@0: return cont(); michael@0: } michael@0: } michael@0: function arrowBody(type) { michael@0: findFatArrow(cx.stream, cx.state); michael@0: if (type == "{") return pass(statement); michael@0: return pass(expression); michael@0: } michael@0: function arrowBodyNoComma(type) { michael@0: findFatArrow(cx.stream, cx.state); michael@0: if (type == "{") return pass(statement); michael@0: return pass(expressionNoComma); michael@0: } michael@0: function maybelabel(type) { michael@0: if (type == ":") return cont(poplex, statement); michael@0: return pass(maybeoperatorComma, expect(";"), poplex); michael@0: } michael@0: function property(type) { michael@0: if (type == "variable") {cx.marked = "property"; return cont();} michael@0: } michael@0: function objprop(type, value) { michael@0: if (type == "variable") { michael@0: cx.marked = "property"; michael@0: if (value == "get" || value == "set") return cont(getterSetter); michael@0: } else if (type == "number" || type == "string") { michael@0: cx.marked = jsonldMode ? "property" : (type + " property"); michael@0: } else if (type == "[") { michael@0: return cont(expression, expect("]"), afterprop); michael@0: } michael@0: if (atomicTypes.hasOwnProperty(type)) return cont(afterprop); michael@0: } michael@0: function getterSetter(type) { michael@0: if (type != "variable") return pass(afterprop); michael@0: cx.marked = "property"; michael@0: return cont(functiondef); michael@0: } michael@0: function afterprop(type) { michael@0: if (type == ":") return cont(expressionNoComma); michael@0: if (type == "(") return pass(functiondef); michael@0: } michael@0: function commasep(what, end) { michael@0: function proceed(type) { michael@0: if (type == ",") { michael@0: var lex = cx.state.lexical; michael@0: if (lex.info == "call") lex.pos = (lex.pos || 0) + 1; michael@0: return cont(what, proceed); michael@0: } michael@0: if (type == end) return cont(); michael@0: return cont(expect(end)); michael@0: } michael@0: return function(type) { michael@0: if (type == end) return cont(); michael@0: return pass(what, proceed); michael@0: }; michael@0: } michael@0: function contCommasep(what, end, info) { michael@0: for (var i = 3; i < arguments.length; i++) michael@0: cx.cc.push(arguments[i]); michael@0: return cont(pushlex(end, info), commasep(what, end), poplex); michael@0: } michael@0: function block(type) { michael@0: if (type == "}") return cont(); michael@0: return pass(statement, block); michael@0: } michael@0: function maybetype(type) { michael@0: if (isTS && type == ":") return cont(typedef); michael@0: } michael@0: function typedef(type) { michael@0: if (type == "variable"){cx.marked = "variable-3"; return cont();} michael@0: } michael@0: function vardef() { michael@0: return pass(pattern, maybetype, maybeAssign, vardefCont); michael@0: } michael@0: function pattern(type, value) { michael@0: if (type == "variable") { register(value); return cont(); } michael@0: if (type == "[") return contCommasep(pattern, "]"); michael@0: if (type == "{") return contCommasep(proppattern, "}"); michael@0: } michael@0: function proppattern(type, value) { michael@0: if (type == "variable" && !cx.stream.match(/^\s*:/, false)) { michael@0: register(value); michael@0: return cont(maybeAssign); michael@0: } michael@0: if (type == "variable") cx.marked = "property"; michael@0: return cont(expect(":"), pattern, maybeAssign); michael@0: } michael@0: function maybeAssign(_type, value) { michael@0: if (value == "=") return cont(expressionNoComma); michael@0: } michael@0: function vardefCont(type) { michael@0: if (type == ",") return cont(vardef); michael@0: } michael@0: function maybeelse(type, value) { michael@0: if (type == "keyword b" && value == "else") return cont(pushlex("form"), statement, poplex); michael@0: } michael@0: function forspec(type) { michael@0: if (type == "(") return cont(pushlex(")"), forspec1, expect(")"), poplex); michael@0: } michael@0: function forspec1(type) { michael@0: if (type == "var") return cont(vardef, expect(";"), forspec2); michael@0: if (type == ";") return cont(forspec2); michael@0: if (type == "variable") return cont(formaybeinof); michael@0: return pass(expression, expect(";"), forspec2); michael@0: } michael@0: function formaybeinof(_type, value) { michael@0: if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); } michael@0: return cont(maybeoperatorComma, forspec2); michael@0: } michael@0: function forspec2(type, value) { michael@0: if (type == ";") return cont(forspec3); michael@0: if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); } michael@0: return pass(expression, expect(";"), forspec3); michael@0: } michael@0: function forspec3(type) { michael@0: if (type != ")") cont(expression); michael@0: } michael@0: function functiondef(type, value) { michael@0: if (value == "*") {cx.marked = "keyword"; return cont(functiondef);} michael@0: if (type == "variable") {register(value); return cont(functiondef);} michael@0: if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, statement, popcontext); michael@0: } michael@0: function funarg(type) { michael@0: if (type == "spread") return cont(funarg); michael@0: return pass(pattern, maybetype); michael@0: } michael@0: function className(type, value) { michael@0: if (type == "variable") {register(value); return cont(classNameAfter);} michael@0: } michael@0: function classNameAfter(_type, value) { michael@0: if (value == "extends") return cont(expression); michael@0: } michael@0: function objlit(type) { michael@0: if (type == "{") return contCommasep(objprop, "}"); michael@0: } michael@0: function afterModule(type, value) { michael@0: if (type == "string") return cont(statement); michael@0: if (type == "variable") { register(value); return cont(maybeFrom); } michael@0: } michael@0: function afterExport(_type, value) { michael@0: if (value == "*") { cx.marked = "keyword"; return cont(maybeFrom, expect(";")); } michael@0: if (value == "default") { cx.marked = "keyword"; return cont(expression, expect(";")); } michael@0: return pass(statement); michael@0: } michael@0: function afterImport(type) { michael@0: if (type == "string") return cont(); michael@0: return pass(importSpec, maybeFrom); michael@0: } michael@0: function importSpec(type, value) { michael@0: if (type == "{") return contCommasep(importSpec, "}"); michael@0: if (type == "variable") register(value); michael@0: return cont(); michael@0: } michael@0: function maybeFrom(_type, value) { michael@0: if (value == "from") { cx.marked = "keyword"; return cont(expression); } michael@0: } michael@0: function arrayLiteral(type) { michael@0: if (type == "]") return cont(); michael@0: return pass(expressionNoComma, maybeArrayComprehension); michael@0: } michael@0: function maybeArrayComprehension(type) { michael@0: if (type == "for") return pass(comprehension, expect("]")); michael@0: if (type == ",") return cont(commasep(expressionNoComma, "]")); michael@0: return pass(commasep(expressionNoComma, "]")); michael@0: } michael@0: function comprehension(type) { michael@0: if (type == "for") return cont(forspec, comprehension); michael@0: if (type == "if") return cont(expression, comprehension); michael@0: } michael@0: michael@0: // Interface michael@0: michael@0: return { michael@0: startState: function(basecolumn) { michael@0: var state = { michael@0: tokenize: tokenBase, michael@0: lastType: "sof", michael@0: cc: [], michael@0: lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false), michael@0: localVars: parserConfig.localVars, michael@0: context: parserConfig.localVars && {vars: parserConfig.localVars}, michael@0: indented: 0 michael@0: }; michael@0: if (parserConfig.globalVars && typeof parserConfig.globalVars == "object") michael@0: state.globalVars = parserConfig.globalVars; michael@0: return state; michael@0: }, michael@0: michael@0: token: function(stream, state) { michael@0: if (stream.sol()) { michael@0: if (!state.lexical.hasOwnProperty("align")) michael@0: state.lexical.align = false; michael@0: state.indented = stream.indentation(); michael@0: findFatArrow(stream, state); michael@0: } michael@0: if (state.tokenize != tokenComment && stream.eatSpace()) return null; michael@0: var style = state.tokenize(stream, state); michael@0: if (type == "comment") return style; michael@0: state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type; michael@0: return parseJS(state, style, type, content, stream); michael@0: }, michael@0: michael@0: indent: function(state, textAfter) { michael@0: if (state.tokenize == tokenComment) return CodeMirror.Pass; michael@0: if (state.tokenize != tokenBase) return 0; michael@0: var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical; michael@0: // Kludge to prevent 'maybelse' from blocking lexical scope pops michael@0: for (var i = state.cc.length - 1; i >= 0; --i) { michael@0: var c = state.cc[i]; michael@0: if (c == poplex) lexical = lexical.prev; michael@0: else if (c != maybeelse) break; michael@0: } michael@0: if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev; michael@0: if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat") michael@0: lexical = lexical.prev; michael@0: var type = lexical.type, closing = firstChar == type; michael@0: michael@0: if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? lexical.info + 1 : 0); michael@0: else if (type == "form" && firstChar == "{") return lexical.indented; michael@0: else if (type == "form") return lexical.indented + indentUnit; michael@0: else if (type == "stat") michael@0: return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? statementIndent || indentUnit : 0); michael@0: else if (lexical.info == "switch" && !closing && parserConfig.doubleIndentSwitch != false) michael@0: return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit); michael@0: else if (lexical.align) return lexical.column + (closing ? 0 : 1); michael@0: else return lexical.indented + (closing ? 0 : indentUnit); michael@0: }, michael@0: michael@0: electricChars: ":{}", michael@0: blockCommentStart: jsonMode ? null : "/*", michael@0: blockCommentEnd: jsonMode ? null : "*/", michael@0: lineComment: jsonMode ? null : "//", michael@0: fold: "brace", michael@0: michael@0: helperType: jsonMode ? "json" : "javascript", michael@0: jsonldMode: jsonldMode, michael@0: jsonMode: jsonMode michael@0: }; michael@0: }); michael@0: michael@0: CodeMirror.defineMIME("text/javascript", "javascript"); michael@0: CodeMirror.defineMIME("text/ecmascript", "javascript"); michael@0: CodeMirror.defineMIME("application/javascript", "javascript"); michael@0: CodeMirror.defineMIME("application/ecmascript", "javascript"); michael@0: CodeMirror.defineMIME("application/json", {name: "javascript", json: true}); michael@0: CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true}); michael@0: CodeMirror.defineMIME("application/ld+json", {name: "javascript", jsonld: true}); michael@0: CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true }); michael@0: CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true }); michael@0: michael@0: });