Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/mode/javascript/javascript.js
1293 views
// TODO actually recognize syntax of TypeScript constructs12CodeMirror.defineMode("javascript", function(config, parserConfig) {3var indentUnit = config.indentUnit;4var statementIndent = parserConfig.statementIndent;5var jsonMode = parserConfig.json;6var isTS = parserConfig.typescript;78// Tokenizer910var keywords = function(){11function kw(type) {return {type: type, style: "keyword"};}12var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");13var operator = kw("operator"), atom = {type: "atom", style: "atom"};1415var jsKeywords = {16"if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,17"return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C, "debugger": C,18"var": kw("var"), "const": kw("var"), "let": kw("var"),19"function": kw("function"), "catch": kw("catch"),20"for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),21"in": operator, "typeof": operator, "instanceof": operator,22"true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom,23"this": kw("this"), "module": kw("module"), "class": kw("class"), "super": kw("atom"),24"yield": C, "export": kw("export"), "import": kw("import"), "extends": C25};2627// Extend the 'normal' keywords with the TypeScript language extensions28if (isTS) {29var type = {type: "variable", style: "variable-3"};30var tsKeywords = {31// object-like things32"interface": kw("interface"),33"extends": kw("extends"),34"constructor": kw("constructor"),3536// scope modifiers37"public": kw("public"),38"private": kw("private"),39"protected": kw("protected"),40"static": kw("static"),4142// types43"string": type, "number": type, "bool": type, "any": type44};4546for (var attr in tsKeywords) {47jsKeywords[attr] = tsKeywords[attr];48}49}5051return jsKeywords;52}();5354var isOperatorChar = /[+\-*&%=<>!?|~^]/;5556function readRegexp(stream) {57var escaped = false, next, inSet = false;58while ((next = stream.next()) != null) {59if (!escaped) {60if (next == "/" && !inSet) return;61if (next == "[") inSet = true;62else if (inSet && next == "]") inSet = false;63}64escaped = !escaped && next == "\\";65}66}6768// Used as scratch variables to communicate multiple values without69// consing up tons of objects.70var type, content;71function ret(tp, style, cont) {72type = tp; content = cont;73return style;74}75function tokenBase(stream, state) {76var ch = stream.next();77if (ch == '"' || ch == "'") {78state.tokenize = tokenString(ch);79return state.tokenize(stream, state);80} else if (ch == "." && stream.match(/^\d+(?:[eE][+\-]?\d+)?/)) {81return ret("number", "number");82} else if (ch == "." && stream.match("..")) {83return ret("spread", "meta");84} else if (/[\[\]{}\(\),;\:\.]/.test(ch)) {85return ret(ch);86} else if (ch == "=" && stream.eat(">")) {87return ret("=>", "operator");88} else if (ch == "0" && stream.eat(/x/i)) {89stream.eatWhile(/[\da-f]/i);90return ret("number", "number");91} else if (/\d/.test(ch)) {92stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/);93return ret("number", "number");94} else if (ch == "/") {95if (stream.eat("*")) {96state.tokenize = tokenComment;97return tokenComment(stream, state);98} else if (stream.eat("/")) {99stream.skipToEnd();100return ret("comment", "comment");101} else if (state.lastType == "operator" || state.lastType == "keyword c" ||102state.lastType == "sof" || /^[\[{}\(,;:]$/.test(state.lastType)) {103readRegexp(stream);104stream.eatWhile(/[gimy]/); // 'y' is "sticky" option in Mozilla105return ret("regexp", "string-2");106} else {107stream.eatWhile(isOperatorChar);108return ret("operator", "operator", stream.current());109}110} else if (ch == "`") {111state.tokenize = tokenQuasi;112return tokenQuasi(stream, state);113} else if (ch == "#") {114stream.skipToEnd();115return ret("error", "error");116} else if (isOperatorChar.test(ch)) {117stream.eatWhile(isOperatorChar);118return ret("operator", "operator", stream.current());119} else {120stream.eatWhile(/[\w\$_]/);121var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];122return (known && state.lastType != ".") ? ret(known.type, known.style, word) :123ret("variable", "variable", word);124}125}126127function tokenString(quote) {128return function(stream, state) {129var escaped = false, next;130while ((next = stream.next()) != null) {131if (next == quote && !escaped) break;132escaped = !escaped && next == "\\";133}134if (!escaped) state.tokenize = tokenBase;135return ret("string", "string");136};137}138139function tokenComment(stream, state) {140var maybeEnd = false, ch;141while (ch = stream.next()) {142if (ch == "/" && maybeEnd) {143state.tokenize = tokenBase;144break;145}146maybeEnd = (ch == "*");147}148return ret("comment", "comment");149}150151function tokenQuasi(stream, state) {152var escaped = false, next;153while ((next = stream.next()) != null) {154if (!escaped && (next == "`" || next == "$" && stream.eat("{"))) {155state.tokenize = tokenBase;156break;157}158escaped = !escaped && next == "\\";159}160return ret("quasi", "string-2", stream.current());161}162163var brackets = "([{}])";164// This is a crude lookahead trick to try and notice that we're165// parsing the argument patterns for a fat-arrow function before we166// actually hit the arrow token. It only works if the arrow is on167// the same line as the arguments and there's no strange noise168// (comments) in between. Fallback is to only notice when we hit the169// arrow, and not declare the arguments as locals for the arrow170// body.171function findFatArrow(stream, state) {172if (state.fatArrowAt) state.fatArrowAt = null;173var arrow = stream.string.indexOf("=>", stream.start);174if (arrow < 0) return;175176var depth = 0, sawSomething = false;177for (var pos = arrow - 1; pos >= 0; --pos) {178var ch = stream.string.charAt(pos);179var bracket = brackets.indexOf(ch);180if (bracket >= 0 && bracket < 3) {181if (!depth) { ++pos; break; }182if (--depth == 0) break;183} else if (bracket >= 3 && bracket < 6) {184++depth;185} else if (/[$\w]/.test(ch)) {186sawSomething = true;187} else if (sawSomething && !depth) {188++pos;189break;190}191}192if (sawSomething && !depth) state.fatArrowAt = pos;193}194195// Parser196197var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true, "this": true};198199function JSLexical(indented, column, type, align, prev, info) {200this.indented = indented;201this.column = column;202this.type = type;203this.prev = prev;204this.info = info;205if (align != null) this.align = align;206}207208function inScope(state, varname) {209for (var v = state.localVars; v; v = v.next)210if (v.name == varname) return true;211for (var cx = state.context; cx; cx = cx.prev) {212for (var v = cx.vars; v; v = v.next)213if (v.name == varname) return true;214}215}216217function parseJS(state, style, type, content, stream) {218var cc = state.cc;219// Communicate our context to the combinators.220// (Less wasteful than consing up a hundred closures on every call.)221cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc;222223if (!state.lexical.hasOwnProperty("align"))224state.lexical.align = true;225226while(true) {227var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement;228if (combinator(type, content)) {229while(cc.length && cc[cc.length - 1].lex)230cc.pop()();231if (cx.marked) return cx.marked;232if (type == "variable" && inScope(state, content)) return "variable-2";233return style;234}235}236}237238// Combinator utils239240var cx = {state: null, column: null, marked: null, cc: null};241function pass() {242for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);243}244function cont() {245pass.apply(null, arguments);246return true;247}248function register(varname) {249function inList(list) {250for (var v = list; v; v = v.next)251if (v.name == varname) return true;252return false;253}254var state = cx.state;255if (state.context) {256cx.marked = "def";257if (inList(state.localVars)) return;258state.localVars = {name: varname, next: state.localVars};259} else {260if (inList(state.globalVars)) return;261if (parserConfig.globalVars)262state.globalVars = {name: varname, next: state.globalVars};263}264}265266// Combinators267268var defaultVars = {name: "this", next: {name: "arguments"}};269function pushcontext() {270cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};271cx.state.localVars = defaultVars;272}273function popcontext() {274cx.state.localVars = cx.state.context.vars;275cx.state.context = cx.state.context.prev;276}277function pushlex(type, info) {278var result = function() {279var state = cx.state, indent = state.indented;280if (state.lexical.type == "stat") indent = state.lexical.indented;281state.lexical = new JSLexical(indent, cx.stream.column(), type, null, state.lexical, info);282};283result.lex = true;284return result;285}286function poplex() {287var state = cx.state;288if (state.lexical.prev) {289if (state.lexical.type == ")")290state.indented = state.lexical.indented;291state.lexical = state.lexical.prev;292}293}294poplex.lex = true;295296function expect(wanted) {297return function(type) {298if (type == wanted) return cont();299else if (wanted == ";") return pass();300else return cont(arguments.callee);301};302}303304function statement(type, value) {305if (type == "var") return cont(pushlex("vardef", value.length), vardef, expect(";"), poplex);306if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);307if (type == "keyword b") return cont(pushlex("form"), statement, poplex);308if (type == "{") return cont(pushlex("}"), block, poplex);309if (type == ";") return cont();310if (type == "if") return cont(pushlex("form"), expression, statement, poplex, maybeelse);311if (type == "function") return cont(functiondef);312if (type == "for") return cont(pushlex("form"), forspec, statement, poplex);313if (type == "variable") return cont(pushlex("stat"), maybelabel);314if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),315block, poplex, poplex);316if (type == "case") return cont(expression, expect(":"));317if (type == "default") return cont(expect(":"));318if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),319statement, poplex, popcontext);320if (type == "module") return cont(pushlex("form"), pushcontext, afterModule, popcontext, poplex);321if (type == "class") return cont(pushlex("form"), className, objlit, poplex);322if (type == "export") return cont(pushlex("form"), afterExport, poplex);323if (type == "import") return cont(pushlex("form"), afterImport, poplex);324return pass(pushlex("stat"), expression, expect(";"), poplex);325}326function expression(type) {327return expressionInner(type, false);328}329function expressionNoComma(type) {330return expressionInner(type, true);331}332function expressionInner(type, noComma) {333if (cx.state.fatArrowAt == cx.stream.start) {334var body = noComma ? arrowBodyNoComma : arrowBody;335if (type == "(") return cont(pushcontext, pushlex(")"), commasep(pattern, ")"), poplex, expect("=>"), body, popcontext);336else if (type == "variable") return pass(pushcontext, pattern, expect("=>"), body, popcontext);337}338339var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma;340if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);341if (type == "function") return cont(functiondef);342if (type == "keyword c") return cont(noComma ? maybeexpressionNoComma : maybeexpression);343if (type == "(") return cont(pushlex(")"), maybeexpression, comprehension, expect(")"), poplex, maybeop);344if (type == "operator" || type == "spread") return cont(noComma ? expressionNoComma : expression);345if (type == "[") return cont(pushlex("]"), arrayLiteral, poplex, maybeop);346if (type == "{") return contCommasep(objprop, "}", null, maybeop);347return cont();348}349function maybeexpression(type) {350if (type.match(/[;\}\)\],]/)) return pass();351return pass(expression);352}353function maybeexpressionNoComma(type) {354if (type.match(/[;\}\)\],]/)) return pass();355return pass(expressionNoComma);356}357358function maybeoperatorComma(type, value) {359if (type == ",") return cont(expression);360return maybeoperatorNoComma(type, value, false);361}362function maybeoperatorNoComma(type, value, noComma) {363var me = noComma == false ? maybeoperatorComma : maybeoperatorNoComma;364var expr = noComma == false ? expression : expressionNoComma;365if (value == "=>") return cont(pushcontext, noComma ? arrowBodyNoComma : arrowBody, popcontext);366if (type == "operator") {367if (/\+\+|--/.test(value)) return cont(me);368if (value == "?") return cont(expression, expect(":"), expr);369return cont(expr);370}371if (type == "quasi") { cx.cc.push(me); return quasi(value); }372if (type == ";") return;373if (type == "(") return contCommasep(expressionNoComma, ")", "call", me);374if (type == ".") return cont(property, me);375if (type == "[") return cont(pushlex("]"), maybeexpression, expect("]"), poplex, me);376}377function quasi(value) {378if (value.slice(value.length - 2) != "${") return cont();379return cont(expression, continueQuasi);380}381function continueQuasi(type) {382if (type == "}") {383cx.marked = "string-2";384cx.state.tokenize = tokenQuasi;385return cont();386}387}388function arrowBody(type) {389findFatArrow(cx.stream, cx.state);390if (type == "{") return pass(statement);391return pass(expression);392}393function arrowBodyNoComma(type) {394findFatArrow(cx.stream, cx.state);395if (type == "{") return pass(statement);396return pass(expressionNoComma);397}398function maybelabel(type) {399if (type == ":") return cont(poplex, statement);400return pass(maybeoperatorComma, expect(";"), poplex);401}402function property(type) {403if (type == "variable") {cx.marked = "property"; return cont();}404}405function objprop(type, value) {406if (type == "variable") {407cx.marked = "property";408if (value == "get" || value == "set") return cont(getterSetter);409} else if (type == "number" || type == "string") {410cx.marked = type + " property";411} else if (type == "[") {412return cont(expression, expect("]"), afterprop);413}414if (atomicTypes.hasOwnProperty(type)) return cont(afterprop);415}416function getterSetter(type) {417if (type != "variable") return pass(afterprop);418cx.marked = "property";419return cont(functiondef);420}421function afterprop(type) {422if (type == ":") return cont(expressionNoComma);423if (type == "(") return pass(functiondef);424}425function commasep(what, end) {426function proceed(type) {427if (type == ",") {428var lex = cx.state.lexical;429if (lex.info == "call") lex.pos = (lex.pos || 0) + 1;430return cont(what, proceed);431}432if (type == end) return cont();433return cont(expect(end));434}435return function(type) {436if (type == end) return cont();437return pass(what, proceed);438};439}440function contCommasep(what, end, info) {441for (var i = 3; i < arguments.length; i++)442cx.cc.push(arguments[i]);443return cont(pushlex(end, info), commasep(what, end), poplex);444}445function block(type) {446if (type == "}") return cont();447return pass(statement, block);448}449function maybetype(type) {450if (isTS && type == ":") return cont(typedef);451}452function typedef(type) {453if (type == "variable"){cx.marked = "variable-3"; return cont();}454}455function vardef() {456return pass(pattern, maybetype, maybeAssign, vardefCont);457}458function pattern(type, value) {459if (type == "variable") { register(value); return cont(); }460if (type == "[") return contCommasep(pattern, "]");461if (type == "{") return contCommasep(proppattern, "}");462}463function proppattern(type, value) {464if (type == "variable" && !cx.stream.match(/^\s*:/, false)) {465register(value);466return cont(maybeAssign);467}468if (type == "variable") cx.marked = "property";469return cont(expect(":"), pattern, maybeAssign);470}471function maybeAssign(_type, value) {472if (value == "=") return cont(expressionNoComma);473}474function vardefCont(type) {475if (type == ",") return cont(vardef);476}477function maybeelse(type, value) {478if (type == "keyword b" && value == "else") return cont(pushlex("form"), statement, poplex);479}480function forspec(type) {481if (type == "(") return cont(pushlex(")"), forspec1, expect(")"), poplex);482}483function forspec1(type) {484if (type == "var") return cont(vardef, expect(";"), forspec2);485if (type == ";") return cont(forspec2);486if (type == "variable") return cont(formaybeinof);487return pass(expression, expect(";"), forspec2);488}489function formaybeinof(_type, value) {490if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }491return cont(maybeoperatorComma, forspec2);492}493function forspec2(type, value) {494if (type == ";") return cont(forspec3);495if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }496return pass(expression, expect(";"), forspec3);497}498function forspec3(type) {499if (type != ")") cont(expression);500}501function functiondef(type, value) {502if (value == "*") {cx.marked = "keyword"; return cont(functiondef);}503if (type == "variable") {register(value); return cont(functiondef);}504if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, statement, popcontext);505}506function funarg(type) {507if (type == "spread") return cont(funarg);508return pass(pattern, maybetype);509}510function className(type, value) {511if (type == "variable") {register(value); return cont(classNameAfter);}512}513function classNameAfter(_type, value) {514if (value == "extends") return cont(expression);515}516function objlit(type) {517if (type == "{") return contCommasep(objprop, "}");518}519function afterModule(type, value) {520if (type == "string") return cont(statement);521if (type == "variable") { register(value); return cont(maybeFrom); }522}523function afterExport(_type, value) {524if (value == "*") { cx.marked = "keyword"; return cont(maybeFrom, expect(";")); }525if (value == "default") { cx.marked = "keyword"; return cont(expression, expect(";")); }526return pass(statement);527}528function afterImport(type) {529if (type == "string") return cont();530return pass(importSpec, maybeFrom);531}532function importSpec(type, value) {533if (type == "{") return contCommasep(importSpec, "}");534if (type == "variable") register(value);535return cont();536}537function maybeFrom(_type, value) {538if (value == "from") { cx.marked = "keyword"; return cont(expression); }539}540function arrayLiteral(type) {541if (type == "]") return cont();542return pass(expressionNoComma, maybeArrayComprehension);543}544function maybeArrayComprehension(type) {545if (type == "for") return pass(comprehension, expect("]"));546if (type == ",") return cont(commasep(expressionNoComma, "]"));547return pass(commasep(expressionNoComma, "]"));548}549function comprehension(type) {550if (type == "for") return cont(forspec, comprehension);551if (type == "if") return cont(expression, comprehension);552}553554// Interface555556return {557startState: function(basecolumn) {558var state = {559tokenize: tokenBase,560lastType: "sof",561cc: [],562lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),563localVars: parserConfig.localVars,564context: parserConfig.localVars && {vars: parserConfig.localVars},565indented: 0566};567if (parserConfig.globalVars) state.globalVars = parserConfig.globalVars;568return state;569},570571token: function(stream, state) {572if (stream.sol()) {573if (!state.lexical.hasOwnProperty("align"))574state.lexical.align = false;575state.indented = stream.indentation();576findFatArrow(stream, state);577}578if (state.tokenize != tokenComment && stream.eatSpace()) return null;579var style = state.tokenize(stream, state);580if (type == "comment") return style;581state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type;582return parseJS(state, style, type, content, stream);583},584585indent: function(state, textAfter) {586if (state.tokenize == tokenComment) return CodeMirror.Pass;587if (state.tokenize != tokenBase) return 0;588var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;589// Kludge to prevent 'maybelse' from blocking lexical scope pops590for (var i = state.cc.length - 1; i >= 0; --i) {591var c = state.cc[i];592if (c == poplex) lexical = lexical.prev;593else if (c != maybeelse) break;594}595if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;596if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat")597lexical = lexical.prev;598var type = lexical.type, closing = firstChar == type;599600if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? lexical.info + 1 : 0);601else if (type == "form" && firstChar == "{") return lexical.indented;602else if (type == "form") return lexical.indented + indentUnit;603else if (type == "stat")604return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? statementIndent || indentUnit : 0);605else if (lexical.info == "switch" && !closing && parserConfig.doubleIndentSwitch != false)606return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);607else if (lexical.align) return lexical.column + (closing ? 0 : 1);608else return lexical.indented + (closing ? 0 : indentUnit);609},610611electricChars: ":{}",612blockCommentStart: jsonMode ? null : "/*",613blockCommentEnd: jsonMode ? null : "*/",614lineComment: jsonMode ? null : "//",615fold: "brace",616617helperType: jsonMode ? "json" : "javascript",618jsonMode: jsonMode619};620});621622CodeMirror.defineMIME("text/javascript", "javascript");623CodeMirror.defineMIME("text/ecmascript", "javascript");624CodeMirror.defineMIME("application/javascript", "javascript");625CodeMirror.defineMIME("application/ecmascript", "javascript");626CodeMirror.defineMIME("application/json", {name: "javascript", json: true});627CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true});628CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true });629CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });630631632