2016-12-07 20:41:05 +01:00
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
2019-01-25 21:43:01 +01:00
|
|
|
// Distributed under an MIT license: https://codemirror.net/LICENSE
|
2016-12-07 20:41:05 +01:00
|
|
|
|
2019-01-25 21:43:01 +01:00
|
|
|
/*eslint-disable*/
|
2016-12-07 20:41:05 +01:00
|
|
|
(function(mod) {
|
|
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
|
|
mod(require("codemirror"));
|
|
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
|
|
define(["codemirror"], mod);
|
|
|
|
else // Plain browser env
|
|
|
|
mod(CodeMirror);
|
|
|
|
})(function(CodeMirror) {
|
|
|
|
"use strict";
|
|
|
|
|
2016-12-13 22:59:47 +01:00
|
|
|
CodeMirror.defineMode("javascript", function(config, parserConfig) {
|
2016-12-07 20:41:05 +01:00
|
|
|
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"};
|
2019-01-25 21:43:01 +01:00
|
|
|
var p5Function = {type: "variable", style: "p5-function"};
|
|
|
|
var p5Variable = {type: "variable", style: "p5-variable"};
|
2016-12-07 20:41:05 +01:00
|
|
|
|
|
|
|
var jsKeywords = {
|
|
|
|
"if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
|
|
|
|
"return": C, "break": C, "continue": C, "new": kw("new"), "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,
|
|
|
|
"await": C, "async": kw("async")
|
|
|
|
};
|
|
|
|
|
|
|
|
var p5FunctionKeywords = {
|
2019-04-12 18:59:06 +02:00
|
|
|
'alpha': p5Function, 'blue': p5Function, 'brightness': p5Function, 'color': p5Function, 'green': p5Function, 'hue': p5Function,
|
|
|
|
'lerpColor': p5Function, 'lightness': p5Function, 'red': p5Function, 'saturation': p5Function, 'background': p5Function, 'clear': p5Function,
|
|
|
|
'colorMode': p5Function, 'fill': p5Function, 'noFill': p5Function, 'noStroke': p5Function, 'stroke': p5Function, 'arc': p5Function,
|
|
|
|
'ellipse': p5Function, 'circle': p5Function, 'line': p5Function, 'point': p5Function, 'quad': p5Function, 'rect': p5Function,
|
|
|
|
'square': p5Function, 'triangle': p5Function, 'ellipseMode': p5Function, 'noSmooth': p5Function, 'rectMode': p5Function, 'smooth': p5Function,
|
|
|
|
'strokeCap': p5Function, 'strokeJoin': p5Function, 'strokeWeight': p5Function, 'bezier': p5Function, 'bezierDetail': p5Function, 'bezierPoint': p5Function,
|
|
|
|
'bezierTangent': p5Function, 'curve': p5Function, 'curveDetail': p5Function, 'curveTightness': p5Function, 'curvePoint': p5Function, 'curveTangent': p5Function,
|
|
|
|
'beginContour': p5Function, 'beginShape': p5Function, 'bezierVertex': p5Function, 'curveVertex': p5Function, 'endContour': p5Function, 'endShape': p5Function,
|
|
|
|
'quadraticVertex': p5Function, 'vertex': p5Function, 'print': p5Function, 'cursor': p5Function, 'frameRate': p5Function, 'noCursor': p5Function,
|
|
|
|
'windowResized': p5Function, 'fullscreen': p5Function, 'pixelDensity': p5Function, 'displayDensity': p5Function, 'getURL': p5Function, 'getURLPath': p5Function,
|
|
|
|
'getURLParams': p5Function, 'preload': p5Function, 'setup': p5Function, 'draw': p5Function, 'remove': p5Function, 'createCanvas': p5Function,
|
|
|
|
'resizeCanvas': p5Function, 'noCanvas': p5Function, 'createGraphics': p5Function, 'blendMode': p5Function, 'noLoop': p5Function, 'loop': p5Function,
|
|
|
|
'push': p5Function, 'pop': p5Function, 'redraw': p5Function, 'applyMatrix': p5Function, 'resetMatrix': p5Function, 'rotate': p5Function,
|
|
|
|
'rotateX': p5Function, 'rotateY': p5Function, 'rotateZ': p5Function, 'scale': p5Function, 'shearX': p5Function, 'shearY': p5Function,
|
|
|
|
'translate': p5Function, 'createStringDict': p5Function, 'createNumberDict': p5Function, 'setMoveThreshold': p5Function, 'setShakeThreshold': p5Function,
|
|
|
|
'deviceTurned': p5Function, 'deviceShaken': p5Function, 'keyPressed': p5Function, 'keyReleased': p5Function, 'keyTyped': p5Function, 'keyIsDown': p5Function,
|
|
|
|
'mouseMoved': p5Function, 'mouseDragged': p5Function, 'mousePressed': p5Function, 'mouseReleased': p5Function, 'mouseClicked': p5Function, 'doubleClicked': p5Function,
|
|
|
|
'mouseWheel': p5Function, 'touchStarted': p5Function, 'touchMoved': p5Function, 'touchEnded': p5Function, 'createImage': p5Function, 'saveCanvas': p5Function,
|
|
|
|
'saveFrames': p5Function, 'loadImage': p5Function, 'image': p5Function, 'tint': p5Function, 'noTint': p5Function, 'imageMode': p5Function,
|
|
|
|
'blend': p5Function, 'copy': p5Function, 'filter': p5Function, 'get': p5Function, 'loadPixels': p5Function, 'set': p5Function,
|
|
|
|
'updatePixels': p5Function, 'loadJSON': p5Function, 'loadStrings': p5Function, 'loadTable': p5Function, 'loadXML': p5Function, 'loadBytes': p5Function,
|
|
|
|
'httpGet': p5Function, 'httpPost': p5Function, 'httpDo': p5Function, 'createWriter': p5Function, 'save': p5Function, 'saveJSON': p5Function,
|
|
|
|
'saveStrings': p5Function, 'saveTable': p5Function, 'abs': p5Function, 'ceil': p5Function, 'constrain': p5Function, 'dist': p5Function,
|
|
|
|
'exp': p5Function, 'floor': p5Function, 'lerp': p5Function, 'log': p5Function, 'mag': p5Function, 'map': p5Function,
|
|
|
|
'max': p5Function, 'min': p5Function, 'norm': p5Function, 'pow': p5Function, 'round': p5Function, 'sq': p5Function,
|
|
|
|
'sqrt': p5Function, 'createVector': p5Function, 'noise': p5Function, 'noiseDetail': p5Function, 'noiseSeed': p5Function, 'randomSeed': p5Function,
|
|
|
|
'random': p5Function, 'randomGaussian': p5Function, 'acos': p5Function, 'asin': p5Function, 'atan': p5Function, 'atan2': p5Function,
|
|
|
|
'cos': p5Function, 'sin': p5Function, 'tan': p5Function, 'degrees': p5Function, 'radians': p5Function, 'angleMode': p5Function,
|
|
|
|
'textAlign': p5Function, 'textLeading': p5Function, 'textSize': p5Function, 'textStyle': p5Function, 'textWidth': p5Function, 'textAscent': p5Function,
|
|
|
|
'textDescent': p5Function, 'loadFont': p5Function, 'text': p5Function, 'textFont': p5Function, 'append': p5Function, 'arrayCopy': p5Function,
|
|
|
|
'concat': p5Function, 'reverse': p5Function, 'shorten': p5Function, 'shuffle': p5Function, 'sort': p5Function, 'splice': p5Function,
|
|
|
|
'subset': p5Function, 'float': p5Function, 'int': p5Function, 'str': p5Function, 'boolean': p5Function, 'byte': p5Function,
|
|
|
|
'char': p5Function, 'unchar': p5Function, 'hex': p5Function, 'unhex': p5Function, 'join': p5Function, 'match': p5Function,
|
|
|
|
'matchAll': p5Function, 'nf': p5Function, 'nfc': p5Function, 'nfp': p5Function, 'nfs': p5Function, 'split': p5Function,
|
|
|
|
'splitTokens': p5Function, 'trim': p5Function, 'day': p5Function, 'hour': p5Function, 'minute': p5Function, 'millis': p5Function,
|
|
|
|
'month': p5Function, 'second': p5Function, 'year': p5Function, 'plane': p5Function, 'box': p5Function, 'sphere': p5Function,
|
|
|
|
'cylinder': p5Function, 'cone': p5Function, 'ellipsoid': p5Function, 'torus': p5Function, 'orbitControl': p5Function, 'debugMode': p5Function,
|
|
|
|
'noDebugMode': p5Function, 'ambientLight': p5Function, 'directionalLight': p5Function, 'pointLight': p5Function, 'loadModel': p5Function,
|
|
|
|
'loadShader': p5Function, 'createShader': p5Function, 'shader': p5Function, 'normalMaterial': p5Function, 'texture': p5Function,
|
|
|
|
'ambientMaterial': p5Function, 'specularMaterial': p5Function, 'camera': p5Function, 'perspective': p5Function, 'ortho': p5Function,
|
|
|
|
'setCamera': p5Function, 'setAttributes': p5Function, 'select': p5Function, 'selectAll': p5Function, 'removeElements': p5Function, 'changed': p5Function,
|
|
|
|
'input': p5Function, 'createDiv': p5Function, 'createP': p5Function, 'createSpan': p5Function, 'createImg': p5Function, 'createA': p5Function,
|
|
|
|
'createSlider': p5Function, 'createButton': p5Function, 'createCheckbox': p5Function, 'createSelect': p5Function, 'createRadio': p5Function,
|
|
|
|
'createColorPicker': p5Function, 'textureMode': p5Function, 'createCamera': p5Function, 'model': p5Function,
|
|
|
|
'createInput': p5Function, 'createFileInput': p5Function, 'createVideo': p5Function, 'createAudio': p5Function, 'createCapture': p5Function,
|
|
|
|
'createElement': p5Function, 'deviceMoved': p5Function,
|
|
|
|
'sampleRate': p5Function, 'freqToMidi': p5Function, 'midiToFreq': p5Function, 'soundFormats': p5Function, 'saveSound': p5Function,
|
2016-12-07 20:41:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var p5VariableKeywords = {
|
2019-04-12 18:59:06 +02:00
|
|
|
'P2D': p5Variable, 'WEBGL': p5Variable, 'ARROW': p5Variable, 'CROSS': p5Variable, 'HAND': p5Variable,
|
|
|
|
'MOVE': p5Variable, 'TEXT': p5Variable,
|
|
|
|
'WAIT': p5Variable, 'HALF_PI': p5Variable, 'PI': p5Variable, 'QUARTER_PI': p5Variable,
|
|
|
|
'TAU': p5Variable, 'TWO_PI': p5Variable, 'DEGREES': p5Variable,
|
|
|
|
'RADIANS': p5Variable, 'CORNER': p5Variable, 'CORNERS': p5Variable, 'RADIUS': p5Variable,
|
|
|
|
'RIGHT': p5Variable, 'LEFT': p5Variable, 'CENTER': p5Variable, 'TOP': p5Variable, 'BOTTOM': p5Variable,
|
|
|
|
'BASELINE': p5Variable, 'POINTS': p5Variable, 'LINES': p5Variable, 'LINE_STRIP': p5Variable, 'LINE_LOOP': p5Variable,
|
|
|
|
'TRIANGLES': p5Variable, 'TRIANGLE_FAN': p5Variable, 'TRIANGLE_STRIP': p5Variable, 'QUADS': p5Variable,
|
|
|
|
'QUAD_STRIP': p5Variable, 'CLOSE': p5Variable, 'OPEN': p5Variable, 'CHORD': p5Variable, 'PIE': p5Variable,
|
|
|
|
'PROJECT': p5Variable, 'SQUARE': p5Variable, 'ROUND': p5Variable, 'BEVEL': p5Variable, 'MITER': p5Variable,
|
|
|
|
'RGB': p5Variable, 'HSB': p5Variable, 'HSL': p5Variable, 'AUTO': p5Variable, 'BLEND': p5Variable, 'ADD': p5Variable,
|
|
|
|
'DARKEST': p5Variable, 'LIGHTEST': p5Variable, 'DIFFERENCE': p5Variable, 'EXCLUSION': p5Variable, 'MULTIPLY': p5Variable,
|
|
|
|
'SCREEN': p5Variable, 'REPLACE': p5Variable, 'OVERLAY': p5Variable, 'HARD_LIGHT': p5Variable, 'SOFT_LIGHT': p5Variable,
|
|
|
|
'DODGE': p5Variable, 'BURN': p5Variable, 'THRESHOLD': p5Variable, 'GRAY': p5Variable, 'OPAQUE': p5Variable,
|
|
|
|
'INVERT': p5Variable, 'POSTERIZE': p5Variable, 'DILATE': p5Variable, 'ERODE': p5Variable, 'BLUR': p5Variable,
|
|
|
|
'NORMAL': p5Variable, 'ITALIC': p5Variable, 'BOLD': p5Variable, 'BOLDITALIC': p5Variable, 'LANDSCAPE': p5Variable,
|
|
|
|
'PORTRAIT': p5Variable, 'GRID': p5Variable, 'AXES': p5Variable, 'frameCount': p5Variable, 'focused': p5Variable,
|
|
|
|
'displayWidth': p5Variable, 'displayHeight': p5Variable, 'windowWidth': p5Variable, 'windowHeight': p5Variable,
|
|
|
|
'width': p5Variable, 'height': p5Variable, 'deviceOrientation': p5Variable, 'accelerationX': p5Variable,
|
|
|
|
'accelerationY': p5Variable, 'accelerationZ': p5Variable, 'pAccelerationX': p5Variable, 'pAccelerationY': p5Variable,
|
|
|
|
'pAccelerationZ': p5Variable, 'rotationX': p5Variable, 'rotationY': p5Variable, 'rotationZ': p5Variable,
|
|
|
|
'pRotationX': p5Variable, 'pRotationY': p5Variable, 'pRotationZ': p5Variable, 'turnAxis': p5Variable,
|
|
|
|
'keyIsPressed': p5Variable, 'key': p5Variable, 'keyCode': p5Variable, 'mouseX': p5Variable, 'mouseY': p5Variable,
|
|
|
|
'pmouseX': p5Variable, 'pmouseY': p5Variable, 'winMouseX': p5Variable, 'winMouseY': p5Variable,
|
|
|
|
'pwinMouseX': p5Variable, 'pwinMouseY': p5Variable, 'mouseButton': p5Variable,
|
|
|
|
'mouseIsPressed': p5Variable, 'touches': p5Variable, 'pixels': p5Variable, 'VIDEO': p5Variable, 'AUDIO': p5Variable
|
2016-12-07 20:41:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
for (var attr in p5FunctionKeywords) {
|
|
|
|
jsKeywords[attr] = p5FunctionKeywords[attr];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var attr in p5VariableKeywords) {
|
|
|
|
jsKeywords[attr] = p5VariableKeywords[attr];
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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("class"),
|
|
|
|
"implements": C,
|
|
|
|
"namespace": C,
|
|
|
|
"module": kw("module"),
|
|
|
|
"enum": kw("module"),
|
|
|
|
"type": kw("type"),
|
|
|
|
|
|
|
|
// scope modifiers
|
|
|
|
"public": kw("modifier"),
|
|
|
|
"private": kw("modifier"),
|
|
|
|
"protected": kw("modifier"),
|
|
|
|
"abstract": kw("modifier"),
|
|
|
|
|
|
|
|
// operators
|
|
|
|
"as": operator,
|
|
|
|
|
|
|
|
// types
|
|
|
|
"string": type, "number": type, "boolean": type, "any": type
|
|
|
|
};
|
|
|
|
|
|
|
|
for (var attr in tsKeywords) {
|
|
|
|
jsKeywords[attr] = tsKeywords[attr];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return jsKeywords;
|
2019-01-25 21:43:01 +01:00
|
|
|
}();
|
2016-12-07 20:41:05 +01:00
|
|
|
|
2019-01-25 21:43:01 +01:00
|
|
|
var isOperatorChar = /[+\-*&%=<>!?|~^@]/;
|
2016-12-07 20:41:05 +01:00
|
|
|
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");
|
2019-01-25 21:43:01 +01:00
|
|
|
} else if (ch == "0" && stream.match(/^(?:x[\da-f]+|o[0-7]+|b[01]+)n?/i)) {
|
2016-12-07 20:41:05 +01:00
|
|
|
return ret("number", "number");
|
|
|
|
} else if (/\d/.test(ch)) {
|
2019-01-25 21:43:01 +01:00
|
|
|
stream.match(/^\d*(?:n|(?:\.\d*)?(?:[eE][+\-]?\d+)?)?/);
|
2016-12-07 20:41:05 +01:00
|
|
|
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 (expressionAllowed(stream, state, 1)) {
|
|
|
|
readRegexp(stream);
|
2019-01-25 21:43:01 +01:00
|
|
|
stream.match(/^\b(([gimyus])(?![gimyus]*\2))+\b/);
|
2016-12-07 20:41:05 +01:00
|
|
|
return ret("regexp", "string-2");
|
|
|
|
} else {
|
2019-01-25 21:43:01 +01:00
|
|
|
stream.eat("=");
|
2016-12-07 20:41:05 +01:00
|
|
|
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)) {
|
2019-01-25 21:43:01 +01:00
|
|
|
if (ch != ">" || !state.lexical || state.lexical.type != ">") {
|
|
|
|
if (stream.eat("=")) {
|
|
|
|
if (ch == "!" || ch == "=") stream.eat("=")
|
|
|
|
} else if (/[<>*+\-]/.test(ch)) {
|
|
|
|
stream.eat(ch)
|
|
|
|
if (ch == ">") stream.eat(ch)
|
|
|
|
}
|
|
|
|
}
|
2016-12-07 20:41:05 +01:00
|
|
|
return ret("operator", "operator", stream.current());
|
|
|
|
} else if (wordRE.test(ch)) {
|
|
|
|
stream.eatWhile(wordRE);
|
2019-01-25 21:43:01 +01:00
|
|
|
var word = stream.current()
|
|
|
|
if (state.lastType != ".") {
|
|
|
|
if (keywords.propertyIsEnumerable(word)) {
|
|
|
|
var kw = keywords[word]
|
|
|
|
return ret(kw.type, kw.style, word)
|
|
|
|
}
|
|
|
|
if (word == "async" && stream.match(/^(\s|\/\*.*?\*\/)*[\[\(\w]/, false))
|
|
|
|
return ret("async", "keyword", word)
|
|
|
|
}
|
|
|
|
return ret("variable", "variable", word)
|
2016-12-07 20:41:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (isTS) { // Try to skip TypeScript return type declarations after the arguments
|
|
|
|
var m = /:\s*(?:\w+(?:<[^>]*>|\[\])?|\{[^}]*\})\s*$/.exec(stream.string.slice(stream.start, arrow))
|
|
|
|
if (m) arrow = m.index
|
|
|
|
}
|
|
|
|
|
|
|
|
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) { if (ch == "(") sawSomething = true; 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;
|
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
function inList(name, list) {
|
|
|
|
for (var v = list; v; v = v.next) if (v.name == name) return true
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-07 20:41:05 +01:00
|
|
|
function register(varname) {
|
|
|
|
var state = cx.state;
|
|
|
|
cx.marked = "def";
|
|
|
|
if (state.context) {
|
2019-01-25 21:43:01 +01:00
|
|
|
if (state.lexical.info == "var" && state.context && state.context.block) {
|
|
|
|
// FIXME function decls are also not block scoped
|
|
|
|
var newContext = registerVarScoped(varname, state.context)
|
|
|
|
if (newContext != null) {
|
|
|
|
state.context = newContext
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if (!inList(varname, state.localVars)) {
|
|
|
|
state.localVars = new Var(varname, state.localVars)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.register = register;
|
|
|
|
// Fall through means this is global
|
|
|
|
if (parserConfig.globalVars && !inList(varname, state.globalVars))
|
|
|
|
state.globalVars = new Var(varname, state.globalVars)
|
|
|
|
}
|
|
|
|
function registerVarScoped(varname, context) {
|
|
|
|
if (!context) {
|
|
|
|
return null
|
|
|
|
} else if (context.block) {
|
|
|
|
var inner = registerVarScoped(varname, context.prev)
|
|
|
|
if (!inner) return null
|
|
|
|
if (inner == context.prev) return context
|
|
|
|
return new Context(inner, context.vars, true)
|
|
|
|
} else if (inList(varname, context.vars)) {
|
|
|
|
return context
|
2016-12-07 20:41:05 +01:00
|
|
|
} else {
|
2019-01-25 21:43:01 +01:00
|
|
|
return new Context(context.prev, new Var(varname, context.vars), false)
|
2016-12-07 20:41:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 21:43:01 +01:00
|
|
|
function isModifier(name) {
|
|
|
|
return name == "public" || name == "private" || name == "protected" || name == "abstract" || name == "readonly"
|
|
|
|
}
|
|
|
|
|
2016-12-07 20:41:05 +01:00
|
|
|
// Combinators
|
|
|
|
|
2019-01-25 21:43:01 +01:00
|
|
|
function Context(prev, vars, block) { this.prev = prev; this.vars = vars; this.block = block }
|
|
|
|
function Var(name, next) { this.name = name; this.next = next }
|
|
|
|
|
|
|
|
var defaultVars = new Var("this", new Var("arguments", null))
|
2016-12-07 20:41:05 +01:00
|
|
|
function pushcontext() {
|
2019-01-25 21:43:01 +01:00
|
|
|
cx.state.context = new Context(cx.state.context, cx.state.localVars, false)
|
|
|
|
cx.state.localVars = defaultVars
|
|
|
|
}
|
|
|
|
function pushblockcontext() {
|
|
|
|
cx.state.context = new Context(cx.state.context, cx.state.localVars, true)
|
|
|
|
cx.state.localVars = null
|
2016-12-07 20:41:05 +01:00
|
|
|
}
|
|
|
|
function popcontext() {
|
2019-01-25 21:43:01 +01:00
|
|
|
cx.state.localVars = cx.state.context.vars
|
|
|
|
cx.state.context = cx.state.context.prev
|
2016-12-07 20:41:05 +01:00
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
popcontext.lex = true
|
2016-12-07 20:41:05 +01:00
|
|
|
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();
|
2019-01-25 21:43:01 +01:00
|
|
|
else if (wanted == ";" || type == "}" || type == ")" || type == "]") return pass();
|
2016-12-07 20:41:05 +01:00
|
|
|
else return cont(exp);
|
|
|
|
};
|
|
|
|
return exp;
|
|
|
|
}
|
|
|
|
|
|
|
|
function statement(type, value) {
|
2019-01-25 21:43:01 +01:00
|
|
|
if (type == "var") return cont(pushlex("vardef", value), vardef, expect(";"), poplex);
|
2016-12-07 20:41:05 +01:00
|
|
|
if (type == "keyword a") return cont(pushlex("form"), parenExpr, statement, poplex);
|
|
|
|
if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
|
2019-01-25 21:43:01 +01:00
|
|
|
if (type == "keyword d") return cx.stream.match(/^\s*$/, false) ? cont() : cont(pushlex("stat"), maybeexpression, expect(";"), poplex);
|
|
|
|
if (type == "debugger") return cont(expect(";"));
|
|
|
|
if (type == "{") return cont(pushlex("}"), pushblockcontext, block, poplex, popcontext);
|
2016-12-07 20:41:05 +01:00
|
|
|
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"), parenExpr, statement, poplex, maybeelse);
|
|
|
|
}
|
|
|
|
if (type == "function") return cont(functiondef);
|
|
|
|
if (type == "for") return cont(pushlex("form"), forspec, statement, poplex);
|
2019-01-25 21:43:01 +01:00
|
|
|
if (type == "class" || (isTS && value == "interface")) {
|
|
|
|
cx.marked = "keyword"
|
|
|
|
return cont(pushlex("form", type == "class" ? type : value), className, poplex)
|
|
|
|
}
|
|
|
|
if (type == "variable") {
|
|
|
|
if (isTS && value == "declare") {
|
|
|
|
cx.marked = "keyword"
|
|
|
|
return cont(statement)
|
|
|
|
} else if (isTS && (value == "module" || value == "enum" || value == "type") && cx.stream.match(/^\s*\w/, false)) {
|
|
|
|
cx.marked = "keyword"
|
|
|
|
if (value == "enum") return cont(enumdef);
|
|
|
|
else if (value == "type") return cont(typeexpr, expect("operator"), typeexpr, expect(";"));
|
|
|
|
else return cont(pushlex("form"), pattern, expect("{"), pushlex("}"), block, poplex, poplex)
|
|
|
|
} else if (isTS && value == "namespace") {
|
|
|
|
cx.marked = "keyword"
|
|
|
|
return cont(pushlex("form"), expression, statement, poplex)
|
|
|
|
} else if (isTS && value == "abstract") {
|
|
|
|
cx.marked = "keyword"
|
|
|
|
return cont(statement)
|
|
|
|
} else {
|
|
|
|
return cont(pushlex("stat"), maybelabel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type == "switch") return cont(pushlex("form"), parenExpr, expect("{"), pushlex("}", "switch"), pushblockcontext,
|
|
|
|
block, poplex, poplex, popcontext);
|
2016-12-07 20:41:05 +01:00
|
|
|
if (type == "case") return cont(expression, expect(":"));
|
|
|
|
if (type == "default") return cont(expect(":"));
|
2019-01-25 21:43:01 +01:00
|
|
|
if (type == "catch") return cont(pushlex("form"), pushcontext, maybeCatchBinding, statement, poplex, popcontext);
|
2016-12-07 20:41:05 +01:00
|
|
|
if (type == "export") return cont(pushlex("stat"), afterExport, poplex);
|
|
|
|
if (type == "import") return cont(pushlex("stat"), afterImport, poplex);
|
|
|
|
if (type == "async") return cont(statement)
|
2019-01-25 21:43:01 +01:00
|
|
|
if (value == "@") return cont(expression, statement)
|
2016-12-07 20:41:05 +01:00
|
|
|
return pass(pushlex("stat"), expression, expect(";"), poplex);
|
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
function maybeCatchBinding(type) {
|
|
|
|
if (type == "(") return cont(funarg, expect(")"))
|
2016-12-07 20:41:05 +01:00
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
function expression(type, value) {
|
|
|
|
return expressionInner(type, value, false);
|
|
|
|
}
|
|
|
|
function expressionNoComma(type, value) {
|
|
|
|
return expressionInner(type, value, true);
|
2016-12-07 20:41:05 +01:00
|
|
|
}
|
|
|
|
function parenExpr(type) {
|
|
|
|
if (type != "(") return pass()
|
|
|
|
return cont(pushlex(")"), expression, expect(")"), poplex)
|
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
function expressionInner(type, value, noComma) {
|
2016-12-07 20:41:05 +01:00
|
|
|
if (cx.state.fatArrowAt == cx.stream.start) {
|
|
|
|
var body = noComma ? arrowBodyNoComma : arrowBody;
|
2019-01-25 21:43:01 +01:00
|
|
|
if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, expect("=>"), body, popcontext);
|
2016-12-07 20:41:05 +01:00
|
|
|
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);
|
2019-01-25 21:43:01 +01:00
|
|
|
if (type == "class" || (isTS && value == "interface")) { cx.marked = "keyword"; return cont(pushlex("form"), classExpression, poplex); }
|
|
|
|
if (type == "keyword c" || type == "async") return cont(noComma ? expressionNoComma : expression);
|
2016-12-07 20:41:05 +01:00
|
|
|
if (type == "(") return cont(pushlex(")"), maybeexpression, 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);
|
|
|
|
if (type == "new") return cont(maybeTarget(noComma));
|
2019-01-25 21:43:01 +01:00
|
|
|
if (type == "import") return cont(expression);
|
2016-12-07 20:41:05 +01:00
|
|
|
return cont();
|
|
|
|
}
|
|
|
|
function maybeexpression(type) {
|
|
|
|
if (type.match(/[;\}\)\],]/)) return pass();
|
|
|
|
return pass(expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
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") {
|
2019-01-25 21:43:01 +01:00
|
|
|
if (/\+\+|--/.test(value) || isTS && value == "!") return cont(me);
|
|
|
|
if (isTS && value == "<" && cx.stream.match(/^([^>]|<.*?>)*>\s*\(/, false))
|
|
|
|
return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, me);
|
2016-12-07 20:41:05 +01:00
|
|
|
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);
|
2019-01-25 21:43:01 +01:00
|
|
|
if (isTS && value == "as") { cx.marked = "keyword"; return cont(typeexpr, me) }
|
|
|
|
if (type == "regexp") {
|
|
|
|
cx.state.lastType = cx.marked = "operator"
|
|
|
|
cx.stream.backUp(cx.stream.pos - cx.stream.start - 1)
|
|
|
|
return cont(expr)
|
|
|
|
}
|
2016-12-07 20:41:05 +01:00
|
|
|
}
|
|
|
|
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 maybeTarget(noComma) {
|
|
|
|
return function(type) {
|
|
|
|
if (type == ".") return cont(noComma ? targetNoComma : target);
|
2019-01-25 21:43:01 +01:00
|
|
|
else if (type == "variable" && isTS) return cont(maybeTypeArgs, noComma ? maybeoperatorNoComma : maybeoperatorComma)
|
2016-12-07 20:41:05 +01:00
|
|
|
else return pass(noComma ? expressionNoComma : expression);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function target(_, value) {
|
|
|
|
if (value == "target") { cx.marked = "keyword"; return cont(maybeoperatorComma); }
|
|
|
|
}
|
|
|
|
function targetNoComma(_, value) {
|
|
|
|
if (value == "target") { cx.marked = "keyword"; return cont(maybeoperatorNoComma); }
|
|
|
|
}
|
|
|
|
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 == "async") {
|
|
|
|
cx.marked = "property";
|
|
|
|
return cont(objprop);
|
|
|
|
} else if (type == "variable" || cx.style == "keyword") {
|
|
|
|
cx.marked = "property";
|
|
|
|
if (value == "get" || value == "set") return cont(getterSetter);
|
2019-01-25 21:43:01 +01:00
|
|
|
var m // Work around fat-arrow-detection complication for detecting typescript typed arrow params
|
|
|
|
if (isTS && cx.state.fatArrowAt == cx.stream.start && (m = cx.stream.match(/^\s*:\s*/, false)))
|
|
|
|
cx.state.fatArrowAt = cx.stream.pos + m[0].length
|
2016-12-07 20:41:05 +01:00
|
|
|
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);
|
2019-01-25 21:43:01 +01:00
|
|
|
} else if (isTS && isModifier(value)) {
|
|
|
|
cx.marked = "keyword"
|
2016-12-07 20:41:05 +01:00
|
|
|
return cont(objprop)
|
|
|
|
} else if (type == "[") {
|
2019-01-25 21:43:01 +01:00
|
|
|
return cont(expression, maybetype, expect("]"), afterprop);
|
2016-12-07 20:41:05 +01:00
|
|
|
} else if (type == "spread") {
|
2019-01-25 21:43:01 +01:00
|
|
|
return cont(expressionNoComma, afterprop);
|
|
|
|
} else if (value == "*") {
|
|
|
|
cx.marked = "keyword";
|
|
|
|
return cont(objprop);
|
2016-12-07 20:41:05 +01:00
|
|
|
} else if (type == ":") {
|
|
|
|
return pass(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);
|
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
function commasep(what, end, sep) {
|
2016-12-07 20:41:05 +01:00
|
|
|
function proceed(type, value) {
|
2019-01-25 21:43:01 +01:00
|
|
|
if (sep ? sep.indexOf(type) > -1 : type == ",") {
|
2016-12-07 20:41:05 +01:00
|
|
|
var lex = cx.state.lexical;
|
|
|
|
if (lex.info == "call") lex.pos = (lex.pos || 0) + 1;
|
|
|
|
return cont(function(type, value) {
|
|
|
|
if (type == end || value == end) return pass()
|
|
|
|
return pass(what)
|
|
|
|
}, proceed);
|
|
|
|
}
|
|
|
|
if (type == end || value == end) return cont();
|
2019-01-25 21:43:01 +01:00
|
|
|
if (sep && sep.indexOf(";") > -1) return pass(what)
|
2016-12-07 20:41:05 +01:00
|
|
|
return cont(expect(end));
|
|
|
|
}
|
|
|
|
return function(type, value) {
|
|
|
|
if (type == end || value == 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, value) {
|
|
|
|
if (isTS) {
|
|
|
|
if (type == ":") return cont(typeexpr);
|
|
|
|
if (value == "?") return cont(maybetype);
|
|
|
|
}
|
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
function mayberettype(type) {
|
|
|
|
if (isTS && type == ":") {
|
|
|
|
if (cx.stream.match(/^\s*\w+\s+is\b/, false)) return cont(expression, isKW, typeexpr)
|
|
|
|
else return cont(typeexpr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function isKW(_, value) {
|
|
|
|
if (value == "is") {
|
|
|
|
cx.marked = "keyword"
|
|
|
|
return cont()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function typeexpr(type, value) {
|
|
|
|
if (value == "keyof" || value == "typeof") {
|
|
|
|
cx.marked = "keyword"
|
|
|
|
return cont(value == "keyof" ? typeexpr : expressionNoComma)
|
|
|
|
}
|
|
|
|
if (type == "variable" || value == "void") {
|
|
|
|
cx.marked = "type"
|
|
|
|
return cont(afterType)
|
|
|
|
}
|
|
|
|
if (type == "string" || type == "number" || type == "atom") return cont(afterType);
|
|
|
|
if (type == "[") return cont(pushlex("]"), commasep(typeexpr, "]", ","), poplex, afterType)
|
|
|
|
if (type == "{") return cont(pushlex("}"), commasep(typeprop, "}", ",;"), poplex, afterType)
|
2016-12-07 20:41:05 +01:00
|
|
|
if (type == "(") return cont(commasep(typearg, ")"), maybeReturnType)
|
2019-01-25 21:43:01 +01:00
|
|
|
if (type == "<") return cont(commasep(typeexpr, ">"), typeexpr)
|
2016-12-07 20:41:05 +01:00
|
|
|
}
|
|
|
|
function maybeReturnType(type) {
|
|
|
|
if (type == "=>") return cont(typeexpr)
|
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
function typeprop(type, value) {
|
2016-12-07 20:41:05 +01:00
|
|
|
if (type == "variable" || cx.style == "keyword") {
|
|
|
|
cx.marked = "property"
|
|
|
|
return cont(typeprop)
|
2019-01-25 21:43:01 +01:00
|
|
|
} else if (value == "?") {
|
|
|
|
return cont(typeprop)
|
2016-12-07 20:41:05 +01:00
|
|
|
} else if (type == ":") {
|
|
|
|
return cont(typeexpr)
|
2019-01-25 21:43:01 +01:00
|
|
|
} else if (type == "[") {
|
|
|
|
return cont(expression, maybetype, expect("]"), typeprop)
|
|
|
|
} else if (type == "(") {
|
|
|
|
return cont(pushlex(")"), commasep(funarg, ")"), poplex, typeprop)
|
2016-12-07 20:41:05 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
function typearg(type, value) {
|
|
|
|
if (type == "variable" && cx.stream.match(/^\s*[?:]/, false) || value == "?") return cont(typearg)
|
|
|
|
if (type == ":") return cont(typeexpr)
|
|
|
|
return pass(typeexpr)
|
2016-12-07 20:41:05 +01:00
|
|
|
}
|
|
|
|
function afterType(type, value) {
|
2019-01-25 21:43:01 +01:00
|
|
|
if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, afterType)
|
|
|
|
if (value == "|" || type == "." || value == "&") return cont(typeexpr)
|
2016-12-07 20:41:05 +01:00
|
|
|
if (type == "[") return cont(expect("]"), afterType)
|
2019-01-25 21:43:01 +01:00
|
|
|
if (value == "extends" || value == "implements") { cx.marked = "keyword"; return cont(typeexpr) }
|
|
|
|
}
|
|
|
|
function maybeTypeArgs(_, value) {
|
|
|
|
if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, afterType)
|
|
|
|
}
|
|
|
|
function typeparam() {
|
|
|
|
return pass(typeexpr, maybeTypeDefault)
|
2016-12-07 20:41:05 +01:00
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
function maybeTypeDefault(_, value) {
|
|
|
|
if (value == "=") return cont(typeexpr)
|
|
|
|
}
|
|
|
|
function vardef(_, value) {
|
|
|
|
if (value == "enum") {cx.marked = "keyword"; return cont(enumdef)}
|
2016-12-07 20:41:05 +01:00
|
|
|
return pass(pattern, maybetype, maybeAssign, vardefCont);
|
|
|
|
}
|
|
|
|
function pattern(type, value) {
|
2019-01-25 21:43:01 +01:00
|
|
|
if (isTS && isModifier(value)) { cx.marked = "keyword"; return cont(pattern) }
|
2016-12-07 20:41:05 +01:00
|
|
|
if (type == "variable") { register(value); return cont(); }
|
|
|
|
if (type == "spread") return cont(pattern);
|
2019-01-25 21:43:01 +01:00
|
|
|
if (type == "[") return contCommasep(eltpattern, "]");
|
2016-12-07 20:41:05 +01:00
|
|
|
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";
|
|
|
|
if (type == "spread") return cont(pattern);
|
|
|
|
if (type == "}") return pass();
|
2019-01-25 21:43:01 +01:00
|
|
|
if (type == "[") return cont(expression, expect(']'), expect(':'), proppattern);
|
2016-12-07 20:41:05 +01:00
|
|
|
return cont(expect(":"), pattern, maybeAssign);
|
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
function eltpattern() {
|
|
|
|
return pass(pattern, maybeAssign)
|
|
|
|
}
|
2016-12-07 20:41:05 +01:00
|
|
|
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);
|
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
function forspec(type, value) {
|
|
|
|
if (value == "await") return cont(forspec);
|
2016-12-07 20:41:05 +01:00
|
|
|
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);}
|
2019-01-25 21:43:01 +01:00
|
|
|
if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, mayberettype, statement, popcontext);
|
|
|
|
if (isTS && value == "<") return cont(pushlex(">"), commasep(typeparam, ">"), poplex, functiondef)
|
|
|
|
}
|
|
|
|
function functiondecl(type, value) {
|
|
|
|
if (value == "*") {cx.marked = "keyword"; return cont(functiondecl);}
|
|
|
|
if (type == "variable") {register(value); return cont(functiondecl);}
|
|
|
|
if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, mayberettype, popcontext);
|
|
|
|
if (isTS && value == "<") return cont(pushlex(">"), commasep(typeparam, ">"), poplex, functiondecl)
|
2016-12-07 20:41:05 +01:00
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
function funarg(type, value) {
|
|
|
|
if (value == "@") cont(expression, funarg)
|
2016-12-07 20:41:05 +01:00
|
|
|
if (type == "spread") return cont(funarg);
|
2019-01-25 21:43:01 +01:00
|
|
|
if (isTS && isModifier(value)) { cx.marked = "keyword"; return cont(funarg); }
|
2016-12-07 20:41:05 +01:00
|
|
|
return pass(pattern, maybetype, maybeAssign);
|
|
|
|
}
|
|
|
|
function classExpression(type, value) {
|
|
|
|
// Class expressions may have an optional name.
|
|
|
|
if (type == "variable") return className(type, value);
|
|
|
|
return classNameAfter(type, value);
|
|
|
|
}
|
|
|
|
function className(type, value) {
|
|
|
|
if (type == "variable") {register(value); return cont(classNameAfter);}
|
|
|
|
}
|
|
|
|
function classNameAfter(type, value) {
|
2019-01-25 21:43:01 +01:00
|
|
|
if (value == "<") return cont(pushlex(">"), commasep(typeparam, ">"), poplex, classNameAfter)
|
|
|
|
if (value == "extends" || value == "implements" || (isTS && type == ",")) {
|
|
|
|
if (value == "implements") cx.marked = "keyword";
|
|
|
|
return cont(isTS ? typeexpr : expression, classNameAfter);
|
|
|
|
}
|
2016-12-07 20:41:05 +01:00
|
|
|
if (type == "{") return cont(pushlex("}"), classBody, poplex);
|
|
|
|
}
|
|
|
|
function classBody(type, value) {
|
2019-01-25 21:43:01 +01:00
|
|
|
if (type == "async" ||
|
|
|
|
(type == "variable" &&
|
|
|
|
(value == "static" || value == "get" || value == "set" || (isTS && isModifier(value))) &&
|
|
|
|
cx.stream.match(/^\s+[\w$\xa1-\uffff]/, false))) {
|
|
|
|
cx.marked = "keyword";
|
|
|
|
return cont(classBody);
|
|
|
|
}
|
2016-12-07 20:41:05 +01:00
|
|
|
if (type == "variable" || cx.style == "keyword") {
|
|
|
|
cx.marked = "property";
|
|
|
|
return cont(isTS ? classfield : functiondef, classBody);
|
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
if (type == "[")
|
|
|
|
return cont(expression, maybetype, expect("]"), isTS ? classfield : functiondef, classBody)
|
2016-12-07 20:41:05 +01:00
|
|
|
if (value == "*") {
|
|
|
|
cx.marked = "keyword";
|
|
|
|
return cont(classBody);
|
|
|
|
}
|
|
|
|
if (type == ";") return cont(classBody);
|
|
|
|
if (type == "}") return cont();
|
2019-01-25 21:43:01 +01:00
|
|
|
if (value == "@") return cont(expression, classBody)
|
2016-12-07 20:41:05 +01:00
|
|
|
}
|
|
|
|
function classfield(type, value) {
|
|
|
|
if (value == "?") return cont(classfield)
|
|
|
|
if (type == ":") return cont(typeexpr, maybeAssign)
|
2019-01-25 21:43:01 +01:00
|
|
|
if (value == "=") return cont(expressionNoComma)
|
|
|
|
var context = cx.state.lexical.prev, isInterface = context && context.info == "interface"
|
|
|
|
return pass(isInterface ? functiondecl : functiondef)
|
2016-12-07 20:41:05 +01:00
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
function afterExport(type, value) {
|
2016-12-07 20:41:05 +01:00
|
|
|
if (value == "*") { cx.marked = "keyword"; return cont(maybeFrom, expect(";")); }
|
|
|
|
if (value == "default") { cx.marked = "keyword"; return cont(expression, expect(";")); }
|
2019-01-25 21:43:01 +01:00
|
|
|
if (type == "{") return cont(commasep(exportField, "}"), maybeFrom, expect(";"));
|
2016-12-07 20:41:05 +01:00
|
|
|
return pass(statement);
|
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
function exportField(type, value) {
|
|
|
|
if (value == "as") { cx.marked = "keyword"; return cont(expect("variable")); }
|
|
|
|
if (type == "variable") return pass(expressionNoComma, exportField);
|
|
|
|
}
|
2016-12-07 20:41:05 +01:00
|
|
|
function afterImport(type) {
|
|
|
|
if (type == "string") return cont();
|
2019-01-25 21:43:01 +01:00
|
|
|
if (type == "(") return pass(expression);
|
|
|
|
return pass(importSpec, maybeMoreImports, maybeFrom);
|
2016-12-07 20:41:05 +01:00
|
|
|
}
|
|
|
|
function importSpec(type, value) {
|
|
|
|
if (type == "{") return contCommasep(importSpec, "}");
|
|
|
|
if (type == "variable") register(value);
|
|
|
|
if (value == "*") cx.marked = "keyword";
|
|
|
|
return cont(maybeAs);
|
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
function maybeMoreImports(type) {
|
|
|
|
if (type == ",") return cont(importSpec, maybeMoreImports)
|
|
|
|
}
|
2016-12-07 20:41:05 +01:00
|
|
|
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(commasep(expressionNoComma, "]"));
|
|
|
|
}
|
2019-01-25 21:43:01 +01:00
|
|
|
function enumdef() {
|
|
|
|
return pass(pushlex("form"), pattern, expect("{"), pushlex("}"), commasep(enummember, "}"), poplex, poplex)
|
|
|
|
}
|
|
|
|
function enummember() {
|
|
|
|
return pass(pattern, maybeAssign);
|
|
|
|
}
|
2016-12-07 20:41:05 +01:00
|
|
|
|
|
|
|
function isContinuedStatement(state, textAfter) {
|
|
|
|
return state.lastType == "operator" || state.lastType == "," ||
|
|
|
|
isOperatorChar.test(textAfter.charAt(0)) ||
|
|
|
|
/[,.]/.test(textAfter.charAt(0));
|
|
|
|
}
|
|
|
|
|
2019-01-25 21:43:01 +01:00
|
|
|
function expressionAllowed(stream, state, backUp) {
|
|
|
|
return state.tokenize == tokenBase &&
|
|
|
|
/^(?:operator|sof|keyword [bcd]|case|new|export|default|spread|[\[{}\(,;:]|=>)$/.test(state.lastType) ||
|
|
|
|
(state.lastType == "quasi" && /\{\s*$/.test(stream.string.slice(0, stream.pos - (backUp || 0))))
|
|
|
|
}
|
|
|
|
|
2016-12-07 20:41:05 +01:00
|
|
|
// Interface
|
|
|
|
|
|
|
|
return {
|
|
|
|
startState: function(basecolumn) {
|
|
|
|
var state = {
|
|
|
|
tokenize: tokenBase,
|
|
|
|
lastType: "sof",
|
|
|
|
cc: [],
|
|
|
|
lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
|
|
|
|
localVars: parserConfig.localVars,
|
2019-01-25 21:43:01 +01:00
|
|
|
context: parserConfig.localVars && new Context(null, null, false),
|
2016-12-07 20:41:05 +01:00
|
|
|
indented: basecolumn || 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, top
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
while ((lexical.type == "stat" || lexical.type == "form") &&
|
|
|
|
(firstChar == "}" || ((top = state.cc[state.cc.length - 1]) &&
|
|
|
|
(top == maybeoperatorComma || top == maybeoperatorNoComma) &&
|
|
|
|
!/^[,\.=+\-*:?[\(]/.test(textAfter))))
|
|
|
|
lexical = lexical.prev;
|
|
|
|
if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat")
|
|
|
|
lexical = lexical.prev;
|
|
|
|
var type = lexical.type, closing = firstChar == type;
|
|
|
|
|
2019-01-25 21:43:01 +01:00
|
|
|
if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? lexical.info.length + 1 : 0);
|
2016-12-07 20:41:05 +01:00
|
|
|
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 : "*/",
|
2019-01-25 21:43:01 +01:00
|
|
|
blockCommentContinue: jsonMode ? null : " * ",
|
2016-12-07 20:41:05 +01:00
|
|
|
lineComment: jsonMode ? null : "//",
|
|
|
|
fold: "brace",
|
|
|
|
closeBrackets: "()[]{}''\"\"``",
|
|
|
|
|
2016-12-13 22:59:47 +01:00
|
|
|
helperType: jsonMode ? "json" : "javascript",
|
2016-12-07 20:41:05 +01:00
|
|
|
jsonldMode: jsonldMode,
|
|
|
|
jsonMode: jsonMode,
|
|
|
|
|
|
|
|
expressionAllowed: expressionAllowed,
|
2019-01-25 21:43:01 +01:00
|
|
|
|
2016-12-07 20:41:05 +01:00
|
|
|
skipExpression: function(state) {
|
|
|
|
var top = state.cc[state.cc.length - 1]
|
|
|
|
if (top == expression || top == expressionNoComma) state.cc.pop()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-12-13 22:59:47 +01:00
|
|
|
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 });
|
2016-12-07 20:41:05 +01:00
|
|
|
|
|
|
|
});
|