|
@@ -0,0 +1,780 @@
|
|
|
+// 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 jsonldMode = parserConfig.jsonld;
|
|
|
+ var jsonMode = parserConfig.json || jsonldMode;
|
|
|
+ var isTS = parserConfig.typescript;
|
|
|
+ var wordRE = parserConfig.wordCharacters || /[\w$\xa1-\uffff]/;
|
|
|
+
|
|
|
+ // Tokenizer
|
|
|
+
|
|
|
+ var keywords = function(){
|
|
|
+ function kw(type) {return {type: type, style: "keyword"};}
|
|
|
+ var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
|
|
|
+ var operator = kw("operator"), atom = {type: "atom", style: "atom"};
|
|
|
+
|
|
|
+ var FUNCTION_NAME=kw('functionname'); //函数名
|
|
|
+ var DATA_NAME={type: "dataname", style:'variable-2'}; //行情数据变量
|
|
|
+ var LINE_WIDTH_NAME=kw('linewidth');
|
|
|
+ var DRAW_NAME=kw("drawname");
|
|
|
+
|
|
|
+ //颜色
|
|
|
+ var COLORBLACK={type: "colorname", style:'colorblack'};
|
|
|
+ var COLORBLUE={type: "colorname", style:'colorblue'};
|
|
|
+ var COLORGREEN={type: "colorname", style:'colorgreen'};
|
|
|
+ var COLORCYAN={type: "colorname", style:'colorcyan'};
|
|
|
+ var COLORRED={type: "colorname", style:'colorred'};
|
|
|
+ var COLORMAGENTA={type: "colorname", style:'colormagenta'};
|
|
|
+ var COLORBROWN={type: "colorname", style:'colorbrown'};
|
|
|
+ var COLORLIGRAY={type: "colorname", style:'colorligray'};
|
|
|
+ var COLORGRAY={type: "colorname", style:'colorgray'};
|
|
|
+ var COLORLIBLUE={type: "colorname", style:'colorliblue'};
|
|
|
+ var COLORLIGREEN={type: "colorname", style:'colorligreen'};
|
|
|
+ var COLORLICYAN={type: "colorname", style:'colorlicyan'};
|
|
|
+ var COLORLIRED={type: "colorname", style:'colorlired'};
|
|
|
+ var COLORLIMAGENTA={type: "colorname", style:'colorlimagenta'};
|
|
|
+ var COLORWHITE={type: "colorname", style:'colorwhite'};
|
|
|
+ var COLORYELLOW={type: "colorname", style:'coloryellow'};
|
|
|
+
|
|
|
+ /*
|
|
|
+ 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, "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"),
|
|
|
+ "in": operator, "typeof": operator, "instanceof": operator,
|
|
|
+ "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom,
|
|
|
+ "this": kw("this"), "class": kw("class"), "super": kw("atom"),
|
|
|
+ "yield": C, "export": kw("export"), "import": kw("import"), "extends": C
|
|
|
+ };
|
|
|
+ */
|
|
|
+
|
|
|
+ var jsKeywords =
|
|
|
+ {
|
|
|
+ "MA":FUNCTION_NAME,"MAX":FUNCTION_NAME,"REF":FUNCTION_NAME,"EXPMEMA":FUNCTION_NAME,"IF":FUNCTION_NAME,'IFF':FUNCTION_NAME,
|
|
|
+ "ABS":FUNCTION_NAME,"SUM":FUNCTION_NAME,"HHV":FUNCTION_NAME,"LLV":FUNCTION_NAME,"EMA":FUNCTION_NAME,
|
|
|
+ "SUM":FUNCTION_NAME,'RANGE':FUNCTION_NAME,'EXIST':FUNCTION_NAME,'TFILTER':FUNCTION_NAME,'CODELIKE':FUNCTION_NAME,
|
|
|
+ 'BARSLAST':FUNCTION_NAME,'SMA':FUNCTION_NAME,'BARSCOUNT':FUNCTION_NAME,'VOLSTICK':FUNCTION_NAME,'CROSS':FUNCTION_NAME,
|
|
|
+ 'NOT':FUNCTION_NAME,'SLOPE':FUNCTION_NAME,'NAMELIKE':FUNCTION_NAME,'BARSSINCEN':FUNCTION_NAME,'BARSSINCE':FUNCTION_NAME,
|
|
|
+ 'ATAN':FUNCTION_NAME,'ACOS':FUNCTION_NAME,'ASIN':FUNCTION_NAME,'COS':FUNCTION_NAME,'SIN':FUNCTION_NAME,'TAN':FUNCTION_NAME,
|
|
|
+ 'LAST':FUNCTION_NAME,'LN':FUNCTION_NAME,'LOG':FUNCTION_NAME,'EXP':FUNCTION_NAME,'SQRT':FUNCTION_NAME,"REFDATE":FUNCTION_NAME,
|
|
|
+ 'DEVSQ':FUNCTION_NAME,'MIN':FUNCTION_NAME,'ZIG':FUNCTION_NAME,'TROUGHBARS':FUNCTION_NAME,'PEAKBARS':FUNCTION_NAME,'EVERY':FUNCTION_NAME,
|
|
|
+ 'COST':FUNCTION_NAME,'WINNER':FUNCTION_NAME,'COUNT':FUNCTION_NAME,'FORCAST':FUNCTION_NAME,'STDP':FUNCTION_NAME,'VAR':FUNCTION_NAME,
|
|
|
+ 'VARP':FUNCTION_NAME,'NDAY':FUNCTION_NAME,'UPNDAY':FUNCTION_NAME,'DOWNNDAY':FUNCTION_NAME,'LONGCROSS':FUNCTION_NAME,'EXISTR':FUNCTION_NAME,
|
|
|
+ 'RELATE':FUNCTION_NAME,'COVAR':FUNCTION_NAME,'HHVBARS':FUNCTION_NAME,'LLVBARS':FUNCTION_NAME,'BETA':FUNCTION_NAME,
|
|
|
+ 'DRAWKLINE_IF':FUNCTION_NAME,'BACKSET':FUNCTION_NAME,'SARTURN':FUNCTION_NAME,'SAR':FUNCTION_NAME,'REVERSE':FUNCTION_NAME,'SUMBARS':FUNCTION_NAME,
|
|
|
+ 'MEMA':FUNCTION_NAME,'WMA':FUNCTION_NAME,
|
|
|
+
|
|
|
+ //动态行情函数
|
|
|
+ "DYNAINFO":FUNCTION_NAME,
|
|
|
+
|
|
|
+ //财务数据函数
|
|
|
+ 'FINANCE':FUNCTION_NAME,
|
|
|
+
|
|
|
+ //融资融券
|
|
|
+ 'MARGIN':FUNCTION_NAME,
|
|
|
+
|
|
|
+ //新闻统计
|
|
|
+ 'NEWS':FUNCTION_NAME,
|
|
|
+
|
|
|
+ //日期类
|
|
|
+ 'DATE':DATA_NAME,'MONTH':DATA_NAME,'YEAR':DATA_NAME,'WEEK':DATA_NAME,
|
|
|
+
|
|
|
+ //绘图函数
|
|
|
+ 'DRAWTEXT':FUNCTION_NAME,'STICKLINE':FUNCTION_NAME,'DRAWBAND':FUNCTION_NAME,'DRAWKLINE':FUNCTION_NAME,'DRAWCHANNEL':FUNCTION_NAME,
|
|
|
+ 'PLOYLINE':FUNCTION_NAME,'POLYLINE':FUNCTION_NAME,'DRAWNUMBER':FUNCTION_NAME,'DRAWICON':FUNCTION_NAME,'SUPERDRAWTEXT':FUNCTION_NAME,
|
|
|
+
|
|
|
+ //个股行情数据
|
|
|
+ "CLOSE":DATA_NAME, "C":DATA_NAME,"VOL":DATA_NAME, "V":DATA_NAME,
|
|
|
+ "OPEN":DATA_NAME, "O":DATA_NAME, "HIGH":DATA_NAME, "H":DATA_NAME, "LOW":DATA_NAME,"L":DATA_NAME,"AMOUNT":DATA_NAME,
|
|
|
+ "CURRBARSCOUNT":DATA_NAME,
|
|
|
+
|
|
|
+ //大盘数据
|
|
|
+ "INDEXA":DATA_NAME,"INDEXC":DATA_NAME,"INDEXH":DATA_NAME,"INDEXL":DATA_NAME,"INDEXO":DATA_NAME,"INDEXV":DATA_NAME,'INDEXADV':DATA_NAME,'INDEXDEC':DATA_NAME,
|
|
|
+ 'UPCOUNT':FUNCTION_NAME,'DOWNCOUNT':FUNCTION_NAME,
|
|
|
+
|
|
|
+ //线段宽度
|
|
|
+ 'LINETHICK1':LINE_WIDTH_NAME,'LINETHICK2':LINE_WIDTH_NAME,'LINETHICK3':LINE_WIDTH_NAME,'LINETHICK4':LINE_WIDTH_NAME,'LINETHICK5':LINE_WIDTH_NAME,
|
|
|
+ 'LINETHICK6':LINE_WIDTH_NAME,'LINETHICK7':LINE_WIDTH_NAME,'LINETHICK8':LINE_WIDTH_NAME,'LINETHICK9':LINE_WIDTH_NAME,'LINETHICK10':LINE_WIDTH_NAME,
|
|
|
+
|
|
|
+ 'COLORSTICK':DRAW_NAME,'CIRCLEDOT':DRAW_NAME,'POINTDOT':DRAW_NAME,'LINESTICK':DRAW_NAME,'STICK':DRAW_NAME,'NODRAW':DRAW_NAME,
|
|
|
+
|
|
|
+ //颜色
|
|
|
+ 'COLORYELLOW':COLORYELLOW,'COLORBLACK':COLORBLACK,'COLORBLUE':COLORBLUE,'COLORGREEN':COLORGREEN,'COLORCYAN':COLORCYAN,'COLORRED':COLORRED,
|
|
|
+ 'COLORMAGENTA':COLORMAGENTA,'COLORBROWN':COLORBROWN,'COLORLIGRAY':COLORLIGRAY,'COLORGRAY':COLORGRAY,'COLORLIBLUE':COLORLIBLUE,'COLORLIGREEN':COLORLIGREEN,
|
|
|
+ 'COLORLICYAN':COLORLICYAN,'COLORLIRED':COLORLIRED,'COLORLIMAGENTA':COLORLIMAGENTA,'COLORWHITE':COLORWHITE
|
|
|
+ };
|
|
|
+
|
|
|
+ // Extend the 'normal' keywords with the TypeScript language extensions
|
|
|
+ if (isTS) {
|
|
|
+ var type = {type: "variable", style: "variable-3"};
|
|
|
+ var tsKeywords = {
|
|
|
+ // object-like things
|
|
|
+ "interface": kw("interface"),
|
|
|
+ "extends": kw("extends"),
|
|
|
+ "constructor": kw("constructor"),
|
|
|
+
|
|
|
+ // scope modifiers
|
|
|
+ "public": kw("public"),
|
|
|
+ "private": kw("private"),
|
|
|
+ "protected": kw("protected"),
|
|
|
+ "static": kw("static"),
|
|
|
+
|
|
|
+ // types
|
|
|
+ "string": type, "number": type, "bool": type, "any": type
|
|
|
+ };
|
|
|
+
|
|
|
+ for (var attr in tsKeywords) {
|
|
|
+ jsKeywords[attr] = tsKeywords[attr];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return jsKeywords;
|
|
|
+ }();
|
|
|
+
|
|
|
+ var isOperatorChar = /[+\-*&%=<>!?|~^]/;
|
|
|
+ var isJsonldKeyword = /^@(context|id|value|language|type|container|list|set|reverse|index|base|vocab|graph)"/;
|
|
|
+
|
|
|
+ function readRegexp(stream) {
|
|
|
+ var escaped = false, next, inSet = false;
|
|
|
+ while ((next = stream.next()) != null) {
|
|
|
+ if (!escaped) {
|
|
|
+ if (next == "/" && !inSet) return;
|
|
|
+ if (next == "[") inSet = true;
|
|
|
+ else if (inSet && next == "]") inSet = false;
|
|
|
+ }
|
|
|
+ escaped = !escaped && next == "\\";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Used as scratch variables to communicate multiple values without
|
|
|
+ // consing up tons of objects.
|
|
|
+ var type, content;
|
|
|
+ function ret(tp, style, cont) {
|
|
|
+ type = tp; content = cont;
|
|
|
+ return style;
|
|
|
+ }
|
|
|
+ function tokenBase(stream, state) {
|
|
|
+ var ch = stream.next();
|
|
|
+ if (ch == '"' || ch == "'") {
|
|
|
+ state.tokenize = tokenString(ch);
|
|
|
+ return state.tokenize(stream, state);
|
|
|
+ } else if (ch == "." && stream.match(/^\d+(?:[eE][+\-]?\d+)?/)) {
|
|
|
+ return ret("number", "number");
|
|
|
+ } else if (ch == "." && stream.match("..")) {
|
|
|
+ return ret("spread", "meta");
|
|
|
+ } else if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
|
|
|
+ return ret(ch);
|
|
|
+ } else if (ch == "=" && stream.eat(">")) {
|
|
|
+ return ret("=>", "operator");
|
|
|
+ } else if (ch == "0" && stream.eat(/x/i)) {
|
|
|
+ stream.eatWhile(/[\da-f]/i);
|
|
|
+ return ret("number", "number");
|
|
|
+ } else if (/\d/.test(ch)) {
|
|
|
+ stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/);
|
|
|
+ return ret("number", "number");
|
|
|
+ } else if (ch == "/") {
|
|
|
+ if (stream.eat("*")) {
|
|
|
+ state.tokenize = tokenComment;
|
|
|
+ return tokenComment(stream, state);
|
|
|
+ } else if (stream.eat("/")) {
|
|
|
+ stream.skipToEnd();
|
|
|
+ return ret("comment", "comment");
|
|
|
+ } else if (state.lastType == "operator" || state.lastType == "keyword c" ||
|
|
|
+ state.lastType == "sof" || /^[\[{}\(,;:]$/.test(state.lastType)) {
|
|
|
+ readRegexp(stream);
|
|
|
+ stream.match(/^\b(([gimyu])(?![gimyu]*\2))+\b/);
|
|
|
+ return ret("regexp", "string-2");
|
|
|
+ } else {
|
|
|
+ stream.eatWhile(isOperatorChar);
|
|
|
+ return ret("operator", "operator", stream.current());
|
|
|
+ }
|
|
|
+ } else if (ch == "`") {
|
|
|
+ state.tokenize = tokenQuasi;
|
|
|
+ return tokenQuasi(stream, state);
|
|
|
+ } else if (ch == "#") {
|
|
|
+ stream.skipToEnd();
|
|
|
+ return ret("error", "error");
|
|
|
+ } else if (isOperatorChar.test(ch)) {
|
|
|
+ stream.eatWhile(isOperatorChar);
|
|
|
+ return ret("operator", "operator", stream.current());
|
|
|
+ } else if (wordRE.test(ch)) {
|
|
|
+ stream.eatWhile(wordRE);
|
|
|
+ var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];
|
|
|
+ return (known && state.lastType != ".") ? ret(known.type, known.style, word) :
|
|
|
+ ret("variable", "variable", word);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function tokenString(quote) {
|
|
|
+ return function(stream, state) {
|
|
|
+ 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");
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ function tokenComment(stream, state) {
|
|
|
+ var maybeEnd = false, ch;
|
|
|
+ while (ch = stream.next()) {
|
|
|
+ if (ch == "/" && maybeEnd) {
|
|
|
+ state.tokenize = tokenBase;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ maybeEnd = (ch == "*");
|
|
|
+ }
|
|
|
+ return ret("comment", "comment");
|
|
|
+ }
|
|
|
+
|
|
|
+ function tokenQuasi(stream, state) {
|
|
|
+ var escaped = false, next;
|
|
|
+ while ((next = stream.next()) != null) {
|
|
|
+ if (!escaped && (next == "`" || next == "$" && stream.eat("{"))) {
|
|
|
+ state.tokenize = tokenBase;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ escaped = !escaped && next == "\\";
|
|
|
+ }
|
|
|
+ return ret("quasi", "string-2", stream.current());
|
|
|
+ }
|
|
|
+
|
|
|
+ var brackets = "([{}])";
|
|
|
+ // This is a crude lookahead trick to try and notice that we're
|
|
|
+ // parsing the argument patterns for a fat-arrow function before we
|
|
|
+ // actually hit the arrow token. It only works if the arrow is on
|
|
|
+ // the same line as the arguments and there's no strange noise
|
|
|
+ // (comments) in between. Fallback is to only notice when we hit the
|
|
|
+ // arrow, and not declare the arguments as locals for the arrow
|
|
|
+ // body.
|
|
|
+ function findFatArrow(stream, state) {
|
|
|
+ if (state.fatArrowAt) state.fatArrowAt = null;
|
|
|
+ var arrow = stream.string.indexOf("=>", stream.start);
|
|
|
+ if (arrow < 0) return;
|
|
|
+
|
|
|
+ var depth = 0, sawSomething = false;
|
|
|
+ for (var pos = arrow - 1; pos >= 0; --pos) {
|
|
|
+ var ch = stream.string.charAt(pos);
|
|
|
+ var bracket = brackets.indexOf(ch);
|
|
|
+ if (bracket >= 0 && bracket < 3) {
|
|
|
+ if (!depth) { ++pos; break; }
|
|
|
+ if (--depth == 0) break;
|
|
|
+ } else if (bracket >= 3 && bracket < 6) {
|
|
|
+ ++depth;
|
|
|
+ } else if (wordRE.test(ch)) {
|
|
|
+ sawSomething = true;
|
|
|
+ } else if (/["'\/]/.test(ch)) {
|
|
|
+ return;
|
|
|
+ } else if (sawSomething && !depth) {
|
|
|
+ ++pos;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (sawSomething && !depth) state.fatArrowAt = pos;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Parser
|
|
|
+
|
|
|
+ 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;
|
|
|
+ this.column = column;
|
|
|
+ this.type = type;
|
|
|
+ this.prev = prev;
|
|
|
+ this.info = info;
|
|
|
+ if (align != null) this.align = align;
|
|
|
+ }
|
|
|
+
|
|
|
+ function inScope(state, varname) {
|
|
|
+ for (var v = state.localVars; v; v = v.next)
|
|
|
+ if (v.name == varname) return true;
|
|
|
+ for (var cx = state.context; cx; cx = cx.prev) {
|
|
|
+ for (var v = cx.vars; v; v = v.next)
|
|
|
+ if (v.name == varname) return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function parseJS(state, style, type, content, stream) {
|
|
|
+ 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.style = style;
|
|
|
+
|
|
|
+ if (!state.lexical.hasOwnProperty("align"))
|
|
|
+ state.lexical.align = true;
|
|
|
+
|
|
|
+ while(true) {
|
|
|
+ var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement;
|
|
|
+ if (combinator(type, content)) {
|
|
|
+ while(cc.length && cc[cc.length - 1].lex)
|
|
|
+ cc.pop()();
|
|
|
+ if (cx.marked) return cx.marked;
|
|
|
+ if (type == "variable" && inScope(state, content)) return "variable-2";
|
|
|
+ return style;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Combinator utils
|
|
|
+
|
|
|
+ var cx = {state: null, column: null, marked: null, cc: null};
|
|
|
+ function pass() {
|
|
|
+ for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
|
|
|
+ }
|
|
|
+ function cont() {
|
|
|
+ pass.apply(null, arguments);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ function register(varname) {
|
|
|
+ function inList(list) {
|
|
|
+ for (var v = list; v; v = v.next)
|
|
|
+ if (v.name == varname) return true;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ var state = cx.state;
|
|
|
+ if (state.context) {
|
|
|
+ cx.marked = "def";
|
|
|
+ if (inList(state.localVars)) return;
|
|
|
+ state.localVars = {name: varname, next: state.localVars};
|
|
|
+ } else {
|
|
|
+ if (inList(state.globalVars)) return;
|
|
|
+ if (parserConfig.globalVars)
|
|
|
+ state.globalVars = {name: varname, next: state.globalVars};
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Combinators
|
|
|
+
|
|
|
+ var defaultVars = {name: "this", next: {name: "arguments"}};
|
|
|
+ function pushcontext() {
|
|
|
+ cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
|
|
|
+ cx.state.localVars = defaultVars;
|
|
|
+ }
|
|
|
+ function popcontext() {
|
|
|
+ cx.state.localVars = cx.state.context.vars;
|
|
|
+ cx.state.context = cx.state.context.prev;
|
|
|
+ }
|
|
|
+ function pushlex(type, info) {
|
|
|
+ var result = function() {
|
|
|
+ var state = cx.state, indent = state.indented;
|
|
|
+ if (state.lexical.type == "stat") indent = state.lexical.indented;
|
|
|
+ else for (var outer = state.lexical; outer && outer.type == ")" && outer.align; outer = outer.prev)
|
|
|
+ indent = outer.indented;
|
|
|
+ state.lexical = new JSLexical(indent, cx.stream.column(), type, null, state.lexical, info);
|
|
|
+ };
|
|
|
+ result.lex = true;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ function poplex() {
|
|
|
+ var state = cx.state;
|
|
|
+ if (state.lexical.prev) {
|
|
|
+ if (state.lexical.type == ")")
|
|
|
+ state.indented = state.lexical.indented;
|
|
|
+ state.lexical = state.lexical.prev;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ poplex.lex = true;
|
|
|
+
|
|
|
+ function expect(wanted) {
|
|
|
+ function exp(type) {
|
|
|
+ if (type == wanted) return cont();
|
|
|
+ else if (wanted == ";") return pass();
|
|
|
+ else return cont(exp);
|
|
|
+ };
|
|
|
+ return exp;
|
|
|
+ }
|
|
|
+
|
|
|
+ function statement(type, value) {
|
|
|
+ if (type == "var") return cont(pushlex("vardef", value.length), vardef, expect(";"), poplex);
|
|
|
+ if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);
|
|
|
+ if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
|
|
|
+ if (type == "{") return cont(pushlex("}"), block, poplex);
|
|
|
+ if (type == ";") return cont();
|
|
|
+ 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, statement, poplex);
|
|
|
+ if (type == "variable") return cont(pushlex("stat"), maybelabel);
|
|
|
+ if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
|
|
|
+ block, poplex, poplex);
|
|
|
+ if (type == "case") return cont(expression, expect(":"));
|
|
|
+ if (type == "default") return cont(expect(":"));
|
|
|
+ if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
|
|
|
+ statement, poplex, popcontext);
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ function expression(type) {
|
|
|
+ return expressionInner(type, false);
|
|
|
+ }
|
|
|
+ function expressionNoComma(type) {
|
|
|
+ return expressionInner(type, true);
|
|
|
+ }
|
|
|
+ function expressionInner(type, noComma) {
|
|
|
+ if (cx.state.fatArrowAt == cx.stream.start) {
|
|
|
+ var body = noComma ? arrowBodyNoComma : arrowBody;
|
|
|
+ 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, 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("]"), arrayLiteral, poplex, maybeop);
|
|
|
+ if (type == "{") return contCommasep(objprop, "}", null, maybeop);
|
|
|
+ if (type == "quasi") { return pass(quasi, maybeop); }
|
|
|
+ return cont();
|
|
|
+ }
|
|
|
+ function maybeexpression(type) {
|
|
|
+ if (type.match(/[;\}\)\],]/)) return pass();
|
|
|
+ return pass(expression);
|
|
|
+ }
|
|
|
+ function maybeexpressionNoComma(type) {
|
|
|
+ if (type.match(/[;\}\)\],]/)) return pass();
|
|
|
+ return pass(expressionNoComma);
|
|
|
+ }
|
|
|
+
|
|
|
+ function maybeoperatorComma(type, value) {
|
|
|
+ if (type == ",") return cont(expression);
|
|
|
+ return maybeoperatorNoComma(type, value, false);
|
|
|
+ }
|
|
|
+ function maybeoperatorNoComma(type, value, noComma) {
|
|
|
+ var me = noComma == false ? maybeoperatorComma : maybeoperatorNoComma;
|
|
|
+ var expr = noComma == false ? expression : expressionNoComma;
|
|
|
+ if (type == "=>") return cont(pushcontext, noComma ? arrowBodyNoComma : arrowBody, popcontext);
|
|
|
+ if (type == "operator") {
|
|
|
+ if (/\+\+|--/.test(value)) return cont(me);
|
|
|
+ if (value == "?") return cont(expression, expect(":"), expr);
|
|
|
+ return cont(expr);
|
|
|
+ }
|
|
|
+ if (type == "quasi") { return pass(quasi, me); }
|
|
|
+ if (type == ";") return;
|
|
|
+ if (type == "(") return contCommasep(expressionNoComma, ")", "call", me);
|
|
|
+ if (type == ".") return cont(property, me);
|
|
|
+ if (type == "[") return cont(pushlex("]"), maybeexpression, expect("]"), poplex, me);
|
|
|
+ }
|
|
|
+ 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(quasi);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ function arrowBody(type) {
|
|
|
+ findFatArrow(cx.stream, cx.state);
|
|
|
+ return pass(type == "{" ? statement : expression);
|
|
|
+ }
|
|
|
+ function arrowBodyNoComma(type) {
|
|
|
+ findFatArrow(cx.stream, cx.state);
|
|
|
+ return pass(type == "{" ? statement : expressionNoComma);
|
|
|
+ }
|
|
|
+ function maybelabel(type) {
|
|
|
+ if (type == ":") return cont(poplex, statement);
|
|
|
+ return pass(maybeoperatorComma, expect(";"), poplex);
|
|
|
+ }
|
|
|
+ function property(type) {
|
|
|
+ if (type == "variable") {cx.marked = "property"; return cont();}
|
|
|
+ }
|
|
|
+ function objprop(type, value) {
|
|
|
+ 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 = jsonldMode ? "property" : (cx.style + " property");
|
|
|
+ return cont(afterprop);
|
|
|
+ } else if (type == "jsonld-keyword") {
|
|
|
+ return cont(afterprop);
|
|
|
+ } else if (type == "[") {
|
|
|
+ return cont(expression, expect("]"), afterprop);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ function getterSetter(type) {
|
|
|
+ if (type != "variable") return pass(afterprop);
|
|
|
+ cx.marked = "property";
|
|
|
+ return cont(functiondef);
|
|
|
+ }
|
|
|
+ function afterprop(type) {
|
|
|
+ if (type == ":") return cont(expressionNoComma);
|
|
|
+ if (type == "(") return pass(functiondef);
|
|
|
+ }
|
|
|
+ function commasep(what, end) {
|
|
|
+ function proceed(type) {
|
|
|
+ if (type == ",") {
|
|
|
+ var lex = cx.state.lexical;
|
|
|
+ if (lex.info == "call") lex.pos = (lex.pos || 0) + 1;
|
|
|
+ return cont(what, proceed);
|
|
|
+ }
|
|
|
+ if (type == end) return cont();
|
|
|
+ return cont(expect(end));
|
|
|
+ }
|
|
|
+ return function(type) {
|
|
|
+ if (type == end) return cont();
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ function maybetype(type) {
|
|
|
+ if (isTS && type == ":") return cont(typedef);
|
|
|
+ }
|
|
|
+ function maybedefault(_, value) {
|
|
|
+ if (value == "=") return cont(expressionNoComma);
|
|
|
+ }
|
|
|
+ function typedef(type) {
|
|
|
+ if (type == "variable") {cx.marked = "variable-3"; return cont();}
|
|
|
+ }
|
|
|
+ function vardef() {
|
|
|
+ return pass(pattern, maybetype, maybeAssign, vardefCont);
|
|
|
+ }
|
|
|
+ function pattern(type, value) {
|
|
|
+ if (type == "variable") { register(value); return cont(); }
|
|
|
+ if (type == "[") return contCommasep(pattern, "]");
|
|
|
+ if (type == "{") return contCommasep(proppattern, "}");
|
|
|
+ }
|
|
|
+ function proppattern(type, value) {
|
|
|
+ if (type == "variable" && !cx.stream.match(/^\s*:/, false)) {
|
|
|
+ register(value);
|
|
|
+ return cont(maybeAssign);
|
|
|
+ }
|
|
|
+ if (type == "variable") cx.marked = "property";
|
|
|
+ return cont(expect(":"), pattern, maybeAssign);
|
|
|
+ }
|
|
|
+ function maybeAssign(_type, value) {
|
|
|
+ if (value == "=") return cont(expressionNoComma);
|
|
|
+ }
|
|
|
+ function vardefCont(type) {
|
|
|
+ if (type == ",") return cont(vardef);
|
|
|
+ }
|
|
|
+ function maybeelse(type, value) {
|
|
|
+ if (type == "keyword b" && value == "else") return cont(pushlex("form", "else"), statement, poplex);
|
|
|
+ }
|
|
|
+ function forspec(type) {
|
|
|
+ if (type == "(") return cont(pushlex(")"), forspec1, expect(")"), poplex);
|
|
|
+ }
|
|
|
+ function forspec1(type) {
|
|
|
+ if (type == "var") return cont(vardef, expect(";"), forspec2);
|
|
|
+ if (type == ";") return cont(forspec2);
|
|
|
+ if (type == "variable") return cont(formaybeinof);
|
|
|
+ return pass(expression, expect(";"), forspec2);
|
|
|
+ }
|
|
|
+ function formaybeinof(_type, value) {
|
|
|
+ if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }
|
|
|
+ return cont(maybeoperatorComma, forspec2);
|
|
|
+ }
|
|
|
+ function forspec2(type, value) {
|
|
|
+ if (type == ";") return cont(forspec3);
|
|
|
+ if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }
|
|
|
+ return pass(expression, expect(";"), forspec3);
|
|
|
+ }
|
|
|
+ function forspec3(type) {
|
|
|
+ if (type != ")") cont(expression);
|
|
|
+ }
|
|
|
+ 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, pushlex(")"), commasep(funarg, ")"), poplex, statement, popcontext);
|
|
|
+ }
|
|
|
+ function funarg(type) {
|
|
|
+ if (type == "spread") return cont(funarg);
|
|
|
+ return pass(pattern, maybetype, maybedefault);
|
|
|
+ }
|
|
|
+ function className(type, value) {
|
|
|
+ if (type == "variable") {register(value); return cont(classNameAfter);}
|
|
|
+ }
|
|
|
+ function classNameAfter(type, value) {
|
|
|
+ if (value == "extends") return cont(expression, classNameAfter);
|
|
|
+ if (type == "{") return cont(pushlex("}"), classBody, poplex);
|
|
|
+ }
|
|
|
+ function classBody(type, value) {
|
|
|
+ if (type == "variable" || cx.style == "keyword") {
|
|
|
+ if (value == "static") {
|
|
|
+ cx.marked = "keyword";
|
|
|
+ return cont(classBody);
|
|
|
+ }
|
|
|
+ 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 afterExport(_type, value) {
|
|
|
+ if (value == "*") { cx.marked = "keyword"; return cont(maybeFrom, expect(";")); }
|
|
|
+ if (value == "default") { cx.marked = "keyword"; return cont(expression, expect(";")); }
|
|
|
+ return pass(statement);
|
|
|
+ }
|
|
|
+ function afterImport(type) {
|
|
|
+ if (type == "string") return cont();
|
|
|
+ return pass(importSpec, maybeFrom);
|
|
|
+ }
|
|
|
+ function importSpec(type, value) {
|
|
|
+ if (type == "{") return contCommasep(importSpec, "}");
|
|
|
+ if (type == "variable") register(value);
|
|
|
+ if (value == "*") cx.marked = "keyword";
|
|
|
+ return cont(maybeAs);
|
|
|
+ }
|
|
|
+ function maybeAs(_type, value) {
|
|
|
+ if (value == "as") { cx.marked = "keyword"; return cont(importSpec); }
|
|
|
+ }
|
|
|
+ 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, expect("]"));
|
|
|
+ if (type == ",") return cont(commasep(maybeexpressionNoComma, "]"));
|
|
|
+ return pass(commasep(expressionNoComma, "]"));
|
|
|
+ }
|
|
|
+ function comprehension(type) {
|
|
|
+ if (type == "for") return cont(forspec, comprehension);
|
|
|
+ if (type == "if") return cont(expression, comprehension);
|
|
|
+ }
|
|
|
+
|
|
|
+ function isContinuedStatement(state, textAfter) {
|
|
|
+ return state.lastType == "operator" || state.lastType == "," ||
|
|
|
+ isOperatorChar.test(textAfter.charAt(0)) ||
|
|
|
+ /[,.]/.test(textAfter.charAt(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ // Interface
|
|
|
+
|
|
|
+ return {
|
|
|
+ startState: function(basecolumn) {
|
|
|
+ var state = {
|
|
|
+ tokenize: tokenBase,
|
|
|
+ lastType: "sof",
|
|
|
+ cc: [],
|
|
|
+ lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
|
|
|
+ localVars: parserConfig.localVars,
|
|
|
+ context: parserConfig.localVars && {vars: parserConfig.localVars},
|
|
|
+ indented: 0
|
|
|
+ };
|
|
|
+ if (parserConfig.globalVars && typeof parserConfig.globalVars == "object")
|
|
|
+ state.globalVars = parserConfig.globalVars;
|
|
|
+ return state;
|
|
|
+ },
|
|
|
+
|
|
|
+ token: function(stream, state) {
|
|
|
+ if (stream.sol()) {
|
|
|
+ if (!state.lexical.hasOwnProperty("align"))
|
|
|
+ state.lexical.align = false;
|
|
|
+ state.indented = stream.indentation();
|
|
|
+ findFatArrow(stream, state);
|
|
|
+ }
|
|
|
+ if (state.tokenize != tokenComment && stream.eatSpace()) return null;
|
|
|
+ var style = state.tokenize(stream, state);
|
|
|
+ if (type == "comment") return style;
|
|
|
+ state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type;
|
|
|
+ return parseJS(state, style, type, content, stream);
|
|
|
+ },
|
|
|
+
|
|
|
+ indent: function(state, textAfter) {
|
|
|
+ if (state.tokenize == tokenComment) return CodeMirror.Pass;
|
|
|
+ if (state.tokenize != tokenBase) return 0;
|
|
|
+ var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;
|
|
|
+ // Kludge to prevent 'maybelse' from blocking lexical scope pops
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;
|
|
|
+ if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat")
|
|
|
+ lexical = lexical.prev;
|
|
|
+ var type = lexical.type, closing = firstChar == type;
|
|
|
+
|
|
|
+ if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? lexical.info + 1 : 0);
|
|
|
+ else if (type == "form" && firstChar == "{") return lexical.indented;
|
|
|
+ else if (type == "form") return lexical.indented + indentUnit;
|
|
|
+ else if (type == "stat")
|
|
|
+ return lexical.indented + (isContinuedStatement(state, textAfter) ? statementIndent || indentUnit : 0);
|
|
|
+ else if (lexical.info == "switch" && !closing && parserConfig.doubleIndentSwitch != false)
|
|
|
+ return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
|
|
|
+ else if (lexical.align) return lexical.column + (closing ? 0 : 1);
|
|
|
+ else return lexical.indented + (closing ? 0 : indentUnit);
|
|
|
+ },
|
|
|
+
|
|
|
+ electricInput: /^\s*(?:case .*?:|default:|\{|\})$/,
|
|
|
+ blockCommentStart: jsonMode ? null : "/*",
|
|
|
+ blockCommentEnd: jsonMode ? null : "*/",
|
|
|
+ lineComment: jsonMode ? null : "//",
|
|
|
+ fold: "brace",
|
|
|
+ closeBrackets: "()[]{}''\"\"``",
|
|
|
+
|
|
|
+ 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 });
|
|
|
+
|
|
|
+});
|