Path: blob/main/website/GAUSS/js/javascript.js
2941 views
// CodeMirror, copyright (c) by Marijn Haverbeke and others1// Distributed under an MIT license: http://codemirror.net/LICENSE23// TODO actually recognize syntax of TypeScript constructs45(function(mod) {6if (typeof exports == "object" && typeof module == "object") // CommonJS7mod(require("../../lib/codemirror"));8else if (typeof define == "function" && define.amd) // AMD9define(["../../lib/codemirror"], mod);10else // Plain browser env11mod(CodeMirror);12})(function(CodeMirror) {13"use strict";1415CodeMirror.defineMode("javascript", function(config, parserConfig) {16var indentUnit = config.indentUnit;17var statementIndent = parserConfig.statementIndent;18var jsonldMode = parserConfig.jsonld;19var jsonMode = parserConfig.json || jsonldMode;20var isTS = parserConfig.typescript;21var wordRE = parserConfig.wordCharacters || /[\w$\xa1-\uffff]/;2223// Tokenizer2425var keywords = function(){26function kw(type) {return {type: type, style: "keyword"};}27var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");28var operator = kw("operator"), atom = {type: "atom", style: "atom"};2930var jsKeywords = {31"if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,32"return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C, "debugger": C,33"var": kw("var"), "const": kw("var"), "let": kw("var"),34"function": kw("function"), "catch": kw("catch"),35"for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),36"in": operator, "typeof": operator, "instanceof": operator,37"true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom,38"this": kw("this"), "module": kw("module"), "class": kw("class"), "super": kw("atom"),39"yield": C, "export": kw("export"), "import": kw("import"), "extends": C40};4142// Extend the 'normal' keywords with the TypeScript language extensions43if (isTS) {44var type = {type: "variable", style: "variable-3"};45var tsKeywords = {46// object-like things47"interface": kw("interface"),48"extends": kw("extends"),49"constructor": kw("constructor"),5051// scope modifiers52"public": kw("public"),53"private": kw("private"),54"protected": kw("protected"),55"static": kw("static"),5657// types58"string": type, "number": type, "bool": type, "any": type59};6061for (var attr in tsKeywords) {62jsKeywords[attr] = tsKeywords[attr];63}64}6566return jsKeywords;67}();6869var isOperatorChar = /[+\-*&%=<>!?|~^]/;70var isJsonldKeyword = /^@(context|id|value|language|type|container|list|set|reverse|index|base|vocab|graph)"/;7172function readRegexp(stream) {73var escaped = false, next, inSet = false;74while ((next = stream.next()) != null) {75if (!escaped) {76if (next == "/" && !inSet) return;77if (next == "[") inSet = true;78else if (inSet && next == "]") inSet = false;79}80escaped = !escaped && next == "\\";81}82}8384// Used as scratch variables to communicate multiple values without85// consing up tons of objects.86var type, content;87function ret(tp, style, cont) {88type = tp; content = cont;89return style;90}91function tokenBase(stream, state) {92var ch = stream.next();93if (ch == '"' || ch == "'") {94state.tokenize = tokenString(ch);95return state.tokenize(stream, state);96} else if (ch == "." && stream.match(/^\d+(?:[eE][+\-]?\d+)?/)) {97return ret("number", "number");98} else if (ch == "." && stream.match("..")) {99return ret("spread", "meta");100} else if (/[\[\]{}\(\),;\:\.]/.test(ch)) {101return ret(ch);102} else if (ch == "=" && stream.eat(">")) {103return ret("=>", "operator");104} else if (ch == "0" && stream.eat(/x/i)) {105stream.eatWhile(/[\da-f]/i);106return ret("number", "number");107} else if (/\d/.test(ch)) {108stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/);109return ret("number", "number");110} else if (ch == "/") {111if (stream.eat("*")) {112state.tokenize = tokenComment;113return tokenComment(stream, state);114} else if (stream.eat("/")) {115stream.skipToEnd();116return ret("comment", "comment");117} else if (state.lastType == "operator" || state.lastType == "keyword c" ||118state.lastType == "sof" || /^[\[{}\(,;:]$/.test(state.lastType)) {119readRegexp(stream);120stream.eatWhile(/[gimy]/); // 'y' is "sticky" option in Mozilla121return ret("regexp", "string-2");122} else {123stream.eatWhile(isOperatorChar);124return ret("operator", "operator", stream.current());125}126} else if (ch == "`") {127state.tokenize = tokenQuasi;128return tokenQuasi(stream, state);129} else if (ch == "#") {130stream.skipToEnd();131return ret("error", "error");132} else if (isOperatorChar.test(ch)) {133stream.eatWhile(isOperatorChar);134return ret("operator", "operator", stream.current());135} else if (wordRE.test(ch)) {136stream.eatWhile(wordRE);137var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];138return (known && state.lastType != ".") ? ret(known.type, known.style, word) :139ret("variable", "variable", word);140}141}142143function tokenString(quote) {144return function(stream, state) {145var escaped = false, next;146if (jsonldMode && stream.peek() == "@" && stream.match(isJsonldKeyword)){147state.tokenize = tokenBase;148return ret("jsonld-keyword", "meta");149}150while ((next = stream.next()) != null) {151if (next == quote && !escaped) break;152escaped = !escaped && next == "\\";153}154if (!escaped) state.tokenize = tokenBase;155return ret("string", "string");156};157}158159function tokenComment(stream, state) {160var maybeEnd = false, ch;161while (ch = stream.next()) {162if (ch == "/" && maybeEnd) {163state.tokenize = tokenBase;164break;165}166maybeEnd = (ch == "*");167}168return ret("comment", "comment");169}170171function tokenQuasi(stream, state) {172var escaped = false, next;173while ((next = stream.next()) != null) {174if (!escaped && (next == "`" || next == "$" && stream.eat("{"))) {175state.tokenize = tokenBase;176break;177}178escaped = !escaped && next == "\\";179}180return ret("quasi", "string-2", stream.current());181}182183var brackets = "([{}])";184// This is a crude lookahead trick to try and notice that we're185// parsing the argument patterns for a fat-arrow function before we186// actually hit the arrow token. It only works if the arrow is on187// the same line as the arguments and there's no strange noise188// (comments) in between. Fallback is to only notice when we hit the189// arrow, and not declare the arguments as locals for the arrow190// body.191function findFatArrow(stream, state) {192if (state.fatArrowAt) state.fatArrowAt = null;193var arrow = stream.string.indexOf("=>", stream.start);194if (arrow < 0) return;195196var depth = 0, sawSomething = false;197for (var pos = arrow - 1; pos >= 0; --pos) {198var ch = stream.string.charAt(pos);199var bracket = brackets.indexOf(ch);200if (bracket >= 0 && bracket < 3) {201if (!depth) { ++pos; break; }202if (--depth == 0) break;203} else if (bracket >= 3 && bracket < 6) {204++depth;205} else if (wordRE.test(ch)) {206sawSomething = true;207} else if (/["'\/]/.test(ch)) {208return;209} else if (sawSomething && !depth) {210++pos;211break;212}213}214if (sawSomething && !depth) state.fatArrowAt = pos;215}216217// Parser218219var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true, "this": true, "jsonld-keyword": true};220221function JSLexical(indented, column, type, align, prev, info) {222this.indented = indented;223this.column = column;224this.type = type;225this.prev = prev;226this.info = info;227if (align != null) this.align = align;228}229230function inScope(state, varname) {231for (var v = state.localVars; v; v = v.next)232if (v.name == varname) return true;233for (var cx = state.context; cx; cx = cx.prev) {234for (var v = cx.vars; v; v = v.next)235if (v.name == varname) return true;236}237}238239function parseJS(state, style, type, content, stream) {240var cc = state.cc;241// Communicate our context to the combinators.242// (Less wasteful than consing up a hundred closures on every call.)243cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc; cx.style = style;244245if (!state.lexical.hasOwnProperty("align"))246state.lexical.align = true;247248while(true) {249var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement;250if (combinator(type, content)) {251while(cc.length && cc[cc.length - 1].lex)252cc.pop()();253if (cx.marked) return cx.marked;254if (type == "variable" && inScope(state, content)) return "variable-2";255return style;256}257}258}259260// Combinator utils261262var cx = {state: null, column: null, marked: null, cc: null};263function pass() {264for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);265}266function cont() {267pass.apply(null, arguments);268return true;269}270function register(varname) {271function inList(list) {272for (var v = list; v; v = v.next)273if (v.name == varname) return true;274return false;275}276var state = cx.state;277if (state.context) {278cx.marked = "def";279if (inList(state.localVars)) return;280state.localVars = {name: varname, next: state.localVars};281} else {282if (inList(state.globalVars)) return;283if (parserConfig.globalVars)284state.globalVars = {name: varname, next: state.globalVars};285}286}287288// Combinators289290var defaultVars = {name: "this", next: {name: "arguments"}};291function pushcontext() {292cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};293cx.state.localVars = defaultVars;294}295function popcontext() {296cx.state.localVars = cx.state.context.vars;297cx.state.context = cx.state.context.prev;298}299function pushlex(type, info) {300var result = function() {301var state = cx.state, indent = state.indented;302if (state.lexical.type == "stat") indent = state.lexical.indented;303else for (var outer = state.lexical; outer && outer.type == ")" && outer.align; outer = outer.prev)304indent = outer.indented;305state.lexical = new JSLexical(indent, cx.stream.column(), type, null, state.lexical, info);306};307result.lex = true;308return result;309}310function poplex() {311var state = cx.state;312if (state.lexical.prev) {313if (state.lexical.type == ")")314state.indented = state.lexical.indented;315state.lexical = state.lexical.prev;316}317}318poplex.lex = true;319320function expect(wanted) {321function exp(type) {322if (type == wanted) return cont();323else if (wanted == ";") return pass();324else return cont(exp);325};326return exp;327}328329function statement(type, value) {330if (type == "var") return cont(pushlex("vardef", value.length), vardef, expect(";"), poplex);331if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);332if (type == "keyword b") return cont(pushlex("form"), statement, poplex);333if (type == "{") return cont(pushlex("}"), block, poplex);334if (type == ";") return cont();335if (type == "if") {336if (cx.state.lexical.info == "else" && cx.state.cc[cx.state.cc.length - 1] == poplex)337cx.state.cc.pop()();338return cont(pushlex("form"), expression, statement, poplex, maybeelse);339}340if (type == "function") return cont(functiondef);341if (type == "for") return cont(pushlex("form"), forspec, statement, poplex);342if (type == "variable") return cont(pushlex("stat"), maybelabel);343if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),344block, poplex, poplex);345if (type == "case") return cont(expression, expect(":"));346if (type == "default") return cont(expect(":"));347if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),348statement, poplex, popcontext);349if (type == "module") return cont(pushlex("form"), pushcontext, afterModule, popcontext, poplex);350if (type == "class") return cont(pushlex("form"), className, poplex);351if (type == "export") return cont(pushlex("form"), afterExport, poplex);352if (type == "import") return cont(pushlex("form"), afterImport, poplex);353return pass(pushlex("stat"), expression, expect(";"), poplex);354}355function expression(type) {356return expressionInner(type, false);357}358function expressionNoComma(type) {359return expressionInner(type, true);360}361function expressionInner(type, noComma) {362if (cx.state.fatArrowAt == cx.stream.start) {363var body = noComma ? arrowBodyNoComma : arrowBody;364if (type == "(") return cont(pushcontext, pushlex(")"), commasep(pattern, ")"), poplex, expect("=>"), body, popcontext);365else if (type == "variable") return pass(pushcontext, pattern, expect("=>"), body, popcontext);366}367368var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma;369if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);370if (type == "function") return cont(functiondef, maybeop);371if (type == "keyword c") return cont(noComma ? maybeexpressionNoComma : maybeexpression);372if (type == "(") return cont(pushlex(")"), maybeexpression, comprehension, expect(")"), poplex, maybeop);373if (type == "operator" || type == "spread") return cont(noComma ? expressionNoComma : expression);374if (type == "[") return cont(pushlex("]"), arrayLiteral, poplex, maybeop);375if (type == "{") return contCommasep(objprop, "}", null, maybeop);376if (type == "quasi") { return pass(quasi, maybeop); }377return cont();378}379function maybeexpression(type) {380if (type.match(/[;\}\)\],]/)) return pass();381return pass(expression);382}383function maybeexpressionNoComma(type) {384if (type.match(/[;\}\)\],]/)) return pass();385return pass(expressionNoComma);386}387388function maybeoperatorComma(type, value) {389if (type == ",") return cont(expression);390return maybeoperatorNoComma(type, value, false);391}392function maybeoperatorNoComma(type, value, noComma) {393var me = noComma == false ? maybeoperatorComma : maybeoperatorNoComma;394var expr = noComma == false ? expression : expressionNoComma;395if (type == "=>") return cont(pushcontext, noComma ? arrowBodyNoComma : arrowBody, popcontext);396if (type == "operator") {397if (/\+\+|--/.test(value)) return cont(me);398if (value == "?") return cont(expression, expect(":"), expr);399return cont(expr);400}401if (type == "quasi") { return pass(quasi, me); }402if (type == ";") return;403if (type == "(") return contCommasep(expressionNoComma, ")", "call", me);404if (type == ".") return cont(property, me);405if (type == "[") return cont(pushlex("]"), maybeexpression, expect("]"), poplex, me);406}407function quasi(type, value) {408if (type != "quasi") return pass();409if (value.slice(value.length - 2) != "${") return cont(quasi);410return cont(expression, continueQuasi);411}412function continueQuasi(type) {413if (type == "}") {414cx.marked = "string-2";415cx.state.tokenize = tokenQuasi;416return cont(quasi);417}418}419function arrowBody(type) {420findFatArrow(cx.stream, cx.state);421return pass(type == "{" ? statement : expression);422}423function arrowBodyNoComma(type) {424findFatArrow(cx.stream, cx.state);425return pass(type == "{" ? statement : expressionNoComma);426}427function maybelabel(type) {428if (type == ":") return cont(poplex, statement);429return pass(maybeoperatorComma, expect(";"), poplex);430}431function property(type) {432if (type == "variable") {cx.marked = "property"; return cont();}433}434function objprop(type, value) {435if (type == "variable" || cx.style == "keyword") {436cx.marked = "property";437if (value == "get" || value == "set") return cont(getterSetter);438return cont(afterprop);439} else if (type == "number" || type == "string") {440cx.marked = jsonldMode ? "property" : (cx.style + " property");441return cont(afterprop);442} else if (type == "jsonld-keyword") {443return cont(afterprop);444} else if (type == "[") {445return cont(expression, expect("]"), afterprop);446}447}448function getterSetter(type) {449if (type != "variable") return pass(afterprop);450cx.marked = "property";451return cont(functiondef);452}453function afterprop(type) {454if (type == ":") return cont(expressionNoComma);455if (type == "(") return pass(functiondef);456}457function commasep(what, end) {458function proceed(type) {459if (type == ",") {460var lex = cx.state.lexical;461if (lex.info == "call") lex.pos = (lex.pos || 0) + 1;462return cont(what, proceed);463}464if (type == end) return cont();465return cont(expect(end));466}467return function(type) {468if (type == end) return cont();469return pass(what, proceed);470};471}472function contCommasep(what, end, info) {473for (var i = 3; i < arguments.length; i++)474cx.cc.push(arguments[i]);475return cont(pushlex(end, info), commasep(what, end), poplex);476}477function block(type) {478if (type == "}") return cont();479return pass(statement, block);480}481function maybetype(type) {482if (isTS && type == ":") return cont(typedef);483}484function typedef(type) {485if (type == "variable"){cx.marked = "variable-3"; return cont();}486}487function vardef() {488return pass(pattern, maybetype, maybeAssign, vardefCont);489}490function pattern(type, value) {491if (type == "variable") { register(value); return cont(); }492if (type == "[") return contCommasep(pattern, "]");493if (type == "{") return contCommasep(proppattern, "}");494}495function proppattern(type, value) {496if (type == "variable" && !cx.stream.match(/^\s*:/, false)) {497register(value);498return cont(maybeAssign);499}500if (type == "variable") cx.marked = "property";501return cont(expect(":"), pattern, maybeAssign);502}503function maybeAssign(_type, value) {504if (value == "=") return cont(expressionNoComma);505}506function vardefCont(type) {507if (type == ",") return cont(vardef);508}509function maybeelse(type, value) {510if (type == "keyword b" && value == "else") return cont(pushlex("form", "else"), statement, poplex);511}512function forspec(type) {513if (type == "(") return cont(pushlex(")"), forspec1, expect(")"), poplex);514}515function forspec1(type) {516if (type == "var") return cont(vardef, expect(";"), forspec2);517if (type == ";") return cont(forspec2);518if (type == "variable") return cont(formaybeinof);519return pass(expression, expect(";"), forspec2);520}521function formaybeinof(_type, value) {522if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }523return cont(maybeoperatorComma, forspec2);524}525function forspec2(type, value) {526if (type == ";") return cont(forspec3);527if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }528return pass(expression, expect(";"), forspec3);529}530function forspec3(type) {531if (type != ")") cont(expression);532}533function functiondef(type, value) {534if (value == "*") {cx.marked = "keyword"; return cont(functiondef);}535if (type == "variable") {register(value); return cont(functiondef);}536if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, statement, popcontext);537}538function funarg(type) {539if (type == "spread") return cont(funarg);540return pass(pattern, maybetype);541}542function className(type, value) {543if (type == "variable") {register(value); return cont(classNameAfter);}544}545function classNameAfter(type, value) {546if (value == "extends") return cont(expression, classNameAfter);547if (type == "{") return cont(pushlex("}"), classBody, poplex);548}549function classBody(type, value) {550if (type == "variable" || cx.style == "keyword") {551cx.marked = "property";552if (value == "get" || value == "set") return cont(classGetterSetter, functiondef, classBody);553return cont(functiondef, classBody);554}555if (value == "*") {556cx.marked = "keyword";557return cont(classBody);558}559if (type == ";") return cont(classBody);560if (type == "}") return cont();561}562function classGetterSetter(type) {563if (type != "variable") return pass();564cx.marked = "property";565return cont();566}567function afterModule(type, value) {568if (type == "string") return cont(statement);569if (type == "variable") { register(value); return cont(maybeFrom); }570}571function afterExport(_type, value) {572if (value == "*") { cx.marked = "keyword"; return cont(maybeFrom, expect(";")); }573if (value == "default") { cx.marked = "keyword"; return cont(expression, expect(";")); }574return pass(statement);575}576function afterImport(type) {577if (type == "string") return cont();578return pass(importSpec, maybeFrom);579}580function importSpec(type, value) {581if (type == "{") return contCommasep(importSpec, "}");582if (type == "variable") register(value);583return cont();584}585function maybeFrom(_type, value) {586if (value == "from") { cx.marked = "keyword"; return cont(expression); }587}588function arrayLiteral(type) {589if (type == "]") return cont();590return pass(expressionNoComma, maybeArrayComprehension);591}592function maybeArrayComprehension(type) {593if (type == "for") return pass(comprehension, expect("]"));594if (type == ",") return cont(commasep(maybeexpressionNoComma, "]"));595return pass(commasep(expressionNoComma, "]"));596}597function comprehension(type) {598if (type == "for") return cont(forspec, comprehension);599if (type == "if") return cont(expression, comprehension);600}601602function isContinuedStatement(state, textAfter) {603return state.lastType == "operator" || state.lastType == "," ||604isOperatorChar.test(textAfter.charAt(0)) ||605/[,.]/.test(textAfter.charAt(0));606}607608// Interface609610return {611startState: function(basecolumn) {612var state = {613tokenize: tokenBase,614lastType: "sof",615cc: [],616lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),617localVars: parserConfig.localVars,618context: parserConfig.localVars && {vars: parserConfig.localVars},619indented: 0620};621if (parserConfig.globalVars && typeof parserConfig.globalVars == "object")622state.globalVars = parserConfig.globalVars;623return state;624},625626token: function(stream, state) {627if (stream.sol()) {628if (!state.lexical.hasOwnProperty("align"))629state.lexical.align = false;630state.indented = stream.indentation();631findFatArrow(stream, state);632}633if (state.tokenize != tokenComment && stream.eatSpace()) return null;634var style = state.tokenize(stream, state);635if (type == "comment") return style;636state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type;637return parseJS(state, style, type, content, stream);638},639640indent: function(state, textAfter) {641if (state.tokenize == tokenComment) return CodeMirror.Pass;642if (state.tokenize != tokenBase) return 0;643var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;644// Kludge to prevent 'maybelse' from blocking lexical scope pops645if (!/^\s*else\b/.test(textAfter)) for (var i = state.cc.length - 1; i >= 0; --i) {646var c = state.cc[i];647if (c == poplex) lexical = lexical.prev;648else if (c != maybeelse) break;649}650if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;651if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat")652lexical = lexical.prev;653var type = lexical.type, closing = firstChar == type;654655if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? lexical.info + 1 : 0);656else if (type == "form" && firstChar == "{") return lexical.indented;657else if (type == "form") return lexical.indented + indentUnit;658else if (type == "stat")659return lexical.indented + (isContinuedStatement(state, textAfter) ? statementIndent || indentUnit : 0);660else if (lexical.info == "switch" && !closing && parserConfig.doubleIndentSwitch != false)661return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);662else if (lexical.align) return lexical.column + (closing ? 0 : 1);663else return lexical.indented + (closing ? 0 : indentUnit);664},665666electricInput: /^\s*(?:case .*?:|default:|\{|\})$/,667blockCommentStart: jsonMode ? null : "/*",668blockCommentEnd: jsonMode ? null : "*/",669lineComment: jsonMode ? null : "//",670fold: "brace",671672helperType: jsonMode ? "json" : "javascript",673jsonldMode: jsonldMode,674jsonMode: jsonMode675};676});677678CodeMirror.registerHelper("wordChars", "javascript", /[\w$]/);679680CodeMirror.defineMIME("text/javascript", "javascript");681CodeMirror.defineMIME("text/ecmascript", "javascript");682CodeMirror.defineMIME("application/javascript", "javascript");683CodeMirror.defineMIME("application/x-javascript", "javascript");684CodeMirror.defineMIME("application/ecmascript", "javascript");685CodeMirror.defineMIME("application/json", {name: "javascript", json: true});686CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true});687CodeMirror.defineMIME("application/ld+json", {name: "javascript", jsonld: true});688CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true });689CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });690691});692693694