babel/src/loose/expression.js
2015-03-20 14:41:55 +01:00

604 lines
19 KiB
JavaScript

import {LooseParser} from "./state"
import {isDummy} from "./parseutil"
import {tokTypes as tt} from ".."
const lp = LooseParser.prototype
lp.checkLVal = function(expr) {
if (!expr) return expr;
switch (expr.type) {
case "Identifier":
case "MemberExpression":
case "ObjectPattern":
case "ArrayPattern":
case "RestElement":
case "AssignmentPattern":
return expr;
default:
return this.dummyIdent();
}
};
lp.parseExpression = function(noIn) {
var start = this.storeCurrentPos();
var expr = this.parseMaybeAssign(noIn);
if (this.tok.type === tt.comma) {
var node = this.startNodeAt(start);
node.expressions = [expr];
while (this.eat(tt.comma)) node.expressions.push(this.parseMaybeAssign(noIn));
return this.finishNode(node, "SequenceExpression");
}
return expr;
};
lp.parseParenExpression = function() {
this.pushCx();
this.expect(tt.parenL);
var val = this.parseExpression();
this.popCx();
this.expect(tt.parenR);
return val;
};
lp.parseMaybeAssign = function(noIn) {
var start = this.storeCurrentPos();
var left = this.parseMaybeConditional(noIn);
if (this.tok.type.isAssign) {
var node = this.startNodeAt(start);
node.operator = this.tok.value;
node.left = this.tok.type === tt.eq ? this.toAssignable(left) : this.checkLVal(left);
this.next();
node.right = this.parseMaybeAssign(noIn);
return this.finishNode(node, "AssignmentExpression");
}
return left;
};
lp.parseMaybeConditional = function(noIn) {
var start = this.storeCurrentPos();
var expr = this.parseExprOps(noIn);
if (this.eat(tt.question)) {
var node = this.startNodeAt(start);
node.test = expr;
node.consequent = this.parseMaybeAssign();
node.alternate = this.expect(tt.colon) ? this.parseMaybeAssign(noIn) : this.dummyIdent();
return this.finishNode(node, "ConditionalExpression");
}
return expr;
};
lp.parseExprOps = function(noIn) {
var start = this.storeCurrentPos();
var indent = this.curIndent, line = this.curLineStart;
return this.parseExprOp(this.parseMaybeUnary(noIn), start, -1, noIn, indent, line);
};
lp.parseExprOp = function(left, start, minPrec, noIn, indent, line) {
if (this.curLineStart != line && this.curIndent < indent && this.tokenStartsLine()) return left;
var prec = this.tok.type.binop;
if (prec != null && (!noIn || this.tok.type !== tt._in)) {
if (prec > minPrec) {
var node = this.startNodeAt(start);
node.left = left;
node.operator = this.tok.value;
this.next();
if (this.curLineStart != line && this.curIndent < indent && this.tokenStartsLine()) {
node.right = this.dummyIdent();
} else {
var rightStart = this.storeCurrentPos();
node.right = this.parseExprOp(this.parseMaybeUnary(noIn), rightStart, prec, noIn, indent, line);
}
this.finishNode(node, /&&|\|\|/.test(node.operator) ? "LogicalExpression" : "BinaryExpression");
return this.parseExprOp(node, start, minPrec, noIn, indent, line);
}
}
return left;
};
lp.parseMaybeUnary = function(noIn) {
if (this.tok.type.prefix) {
var node = this.startNode(), update = this.tok.type === tt.incDec;
node.operator = this.tok.value;
node.prefix = true;
this.next();
node.argument = this.parseMaybeUnary(noIn);
if (update) node.argument = this.checkLVal(node.argument);
return this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
} else if (this.tok.type === tt.ellipsis) {
var node = this.startNode();
this.next();
node.argument = this.parseMaybeUnary(noIn);
return this.finishNode(node, "SpreadElement");
}
var start = this.storeCurrentPos();
var expr = this.parseExprSubscripts();
while (this.tok.type.postfix && !this.canInsertSemicolon()) {
var node = this.startNodeAt(start);
node.operator = this.tok.value;
node.prefix = false;
node.argument = this.checkLVal(expr);
this.next();
expr = this.finishNode(node, "UpdateExpression");
}
return expr;
};
lp.parseExprSubscripts = function() {
var start = this.storeCurrentPos();
return this.parseSubscripts(this.parseExprAtom(), start, false, this.curIndent, this.curLineStart);
};
lp.parseSubscripts = function(base, start, noCalls, startIndent, line) {
for (;;) {
if (this.curLineStart != line && this.curIndent <= startIndent && this.tokenStartsLine()) {
if (this.tok.type == tt.dot && this.curIndent == startIndent)
--startIndent;
else
return base;
}
if (this.eat(tt.dot)) {
var node = this.startNodeAt(start);
node.object = base;
if (this.curLineStart != line && this.curIndent <= startIndent && this.tokenStartsLine())
node.property = this.dummyIdent();
else
node.property = this.parsePropertyAccessor() || this.dummyIdent();
node.computed = false;
base = this.finishNode(node, "MemberExpression");
} else if (this.tok.type == tt.bracketL) {
this.pushCx();
this.next();
var node = this.startNodeAt(start);
node.object = base;
node.property = this.parseExpression();
node.computed = true;
this.popCx();
this.expect(tt.bracketR);
base = this.finishNode(node, "MemberExpression");
} else if (!noCalls && this.tok.type == tt.parenL) {
this.pushCx();
var node = this.startNodeAt(start);
node.callee = base;
node.arguments = this.parseExprList(tt.parenR);
base = this.finishNode(node, "CallExpression");
} else if (this.tok.type == tt.backQuote) {
var node = this.startNodeAt(start);
node.tag = base;
node.quasi = this.parseTemplate();
base = this.finishNode(node, "TaggedTemplateExpression");
} else {
return base;
}
}
};
lp.parseExprAtom = function() {
switch (this.tok.type) {
case tt._this:
case tt._super:
var type = this.tok.type === tt._this ? "ThisExpression" : "SuperExpression";
var node = this.startNode();
this.next();
return this.finishNode(node, type);
case tt.name:
var start = this.storeCurrentPos();
var id = this.parseIdent();
return this.eat(tt.arrow) ? this.parseArrowExpression(this.startNodeAt(start), [id]) : id;
case tt.regexp:
var node = this.startNode();
var val = this.tok.value;
node.regex = {pattern: val.pattern, flags: val.flags};
node.value = val.value;
node.raw = this.input.slice(this.tok.start, this.tok.end);
this.next();
return this.finishNode(node, "Literal");
case tt.num: case tt.string:
var node = this.startNode();
node.value = this.tok.value;
node.raw = this.input.slice(this.tok.start, this.tok.end);
this.next();
return this.finishNode(node, "Literal");
case tt._null: case tt._true: case tt._false:
var node = this.startNode();
node.value = this.tok.type === tt._null ? null : this.tok.type === tt._true;
node.raw = this.tok.type.keyword;
this.next();
return this.finishNode(node, "Literal");
case tt.parenL:
var start = this.storeCurrentPos();
this.next();
var val = this.parseExpression();
this.expect(tt.parenR);
if (this.eat(tt.arrow)) {
return this.parseArrowExpression(this.startNodeAt(start), val.expressions || (isDummy(val) ? [] : [val]));
}
if (this.options.preserveParens) {
var par = this.startNodeAt(start);
par.expression = val;
val = this.finishNode(par, "ParenthesizedExpression");
}
return val;
case tt.bracketL:
var node = this.startNode();
this.pushCx();
node.elements = this.parseExprList(tt.bracketR, true);
return this.finishNode(node, "ArrayExpression");
case tt.braceL:
return this.parseObj();
case tt._class:
return this.parseClass();
case tt._function:
var node = this.startNode();
this.next();
return this.parseFunction(node, false);
case tt._new:
return this.parseNew();
case tt._yield:
var node = this.startNode();
this.next();
if (this.semicolon() || this.canInsertSemicolon() || (this.tok.type != tt.star && !this.tok.type.startsExpr)) {
node.delegate = false;
node.argument = null;
} else {
node.delegate = this.eat(tt.star);
node.argument = this.parseMaybeAssign();
}
return this.finishNode(node, "YieldExpression");
case tt.backQuote:
return this.parseTemplate();
default:
return this.dummyIdent();
}
};
lp.parseNew = function() {
var node = this.startNode(), startIndent = this.curIndent, line = this.curLineStart;
this.next();
var start = this.storeCurrentPos();
node.callee = this.parseSubscripts(this.parseExprAtom(), start, true, startIndent, line);
if (this.tok.type == tt.parenL) {
this.pushCx();
node.arguments = this.parseExprList(tt.parenR);
} else {
node.arguments = [];
}
return this.finishNode(node, "NewExpression");
};
lp.parseTemplateElement = function() {
var elem = this.startNode();
elem.value = {
raw: this.input.slice(this.tok.start, this.tok.end),
cooked: this.tok.value
};
this.next();
elem.tail = this.tok.type === tt.backQuote;
return this.finishNode(elem, "TemplateElement");
};
lp.parseTemplate = function() {
var node = this.startNode();
this.next();
node.expressions = [];
var curElt = this.parseTemplateElement();
node.quasis = [curElt];
while (!curElt.tail) {
this.next();
node.expressions.push(this.parseExpression());
if (this.expect(tt.braceR)) {
curElt = this.parseTemplateElement();
} else {
curElt = this.startNode();
curElt.value = {cooked: '', raw: ''};
curElt.tail = true;
}
node.quasis.push(curElt);
}
this.expect(tt.backQuote);
return this.finishNode(node, "TemplateLiteral");
};
lp.parseObj = function() {
var node = this.startNode();
node.properties = [];
this.pushCx();
var indent = this.curIndent + 1, line = this.curLineStart;
this.eat(tt.braceL);
if (this.curIndent + 1 < indent) { indent = this.curIndent; line = this.curLineStart; }
while (!this.closes(tt.braceR, indent, line)) {
var prop = this.startNode(), isGenerator, start;
if (this.options.ecmaVersion >= 6) {
start = this.storeCurrentPos();
prop.method = false;
prop.shorthand = false;
isGenerator = this.eat(tt.star);
}
this.parsePropertyName(prop);
if (isDummy(prop.key)) { if (isDummy(this.parseMaybeAssign())) this.next(); this.eat(tt.comma); continue; }
if (this.eat(tt.colon)) {
prop.kind = "init";
prop.value = this.parseMaybeAssign();
} else if (this.options.ecmaVersion >= 6 && (this.tok.type === tt.parenL || this.tok.type === tt.braceL)) {
prop.kind = "init";
prop.method = true;
prop.value = this.parseMethod(isGenerator);
} else if (this.options.ecmaVersion >= 5 && prop.key.type === "Identifier" &&
!prop.computed && (prop.key.name === "get" || prop.key.name === "set") &&
(this.tok.type != tt.comma && this.tok.type != tt.braceR)) {
prop.kind = prop.key.name;
this.parsePropertyName(prop);
prop.value = this.parseMethod(false);
} else {
prop.kind = "init";
if (this.options.ecmaVersion >= 6) {
if (this.eat(tt.eq)) {
var assign = this.startNodeAt(start);
assign.operator = "=";
assign.left = prop.key;
assign.right = this.parseMaybeAssign();
prop.value = this.finishNode(assign, "AssignmentExpression");
} else {
prop.value = prop.key;
}
} else {
prop.value = this.dummyIdent();
}
prop.shorthand = true;
}
node.properties.push(this.finishNode(prop, "Property"));
this.eat(tt.comma);
}
this.popCx();
if (!this.eat(tt.braceR)) {
// If there is no closing brace, make the node span to the start
// of the next token (this is useful for Tern)
this.last.end = this.tok.start;
if (this.options.locations) this.last.loc.end = this.tok.loc.start;
}
return this.finishNode(node, "ObjectExpression");
};
lp.parsePropertyName = function(prop) {
if (this.options.ecmaVersion >= 6) {
if (this.eat(tt.bracketL)) {
prop.computed = true;
prop.key = this.parseExpression();
this.expect(tt.bracketR);
return;
} else {
prop.computed = false;
}
}
var key = (this.tok.type === tt.num || this.tok.type === tt.string) ? this.parseExprAtom() : this.parseIdent();
prop.key = key || this.dummyIdent();
};
lp.parsePropertyAccessor = function() {
if (this.tok.type === tt.name || this.tok.type.keyword) return this.parseIdent();
};
lp.parseIdent = function() {
var node = this.startNode();
node.name = this.tok.type === tt.name ? this.tok.value : this.tok.type.keyword;
this.next();
return this.finishNode(node, "Identifier");
};
lp.initFunction = function(node) {
node.id = null;
node.params = [];
if (this.options.ecmaVersion >= 6) {
node.generator = false;
node.expression = false;
}
};
// Convert existing expression atom to assignable pattern
// if possible.
lp.toAssignable = function(node) {
if (this.options.ecmaVersion >= 6 && node) {
switch (node.type) {
case "ObjectExpression":
node.type = "ObjectPattern";
var props = node.properties;
for (var i = 0; i < props.length; i++)
this.toAssignable(props[i].value);
break;
case "ArrayExpression":
node.type = "ArrayPattern";
this.toAssignableList(node.elements);
break;
case "SpreadElement":
node.type = "RestElement";
node.argument = this.toAssignable(node.argument);
break;
case "AssignmentExpression":
node.type = "AssignmentPattern";
break;
}
}
return this.checkLVal(node);
};
lp.toAssignableList = function(exprList) {
for (var i = 0; i < exprList.length; i++)
this.toAssignable(exprList[i]);
return exprList;
};
lp.parseFunctionParams = function(params) {
this.pushCx();
params = this.parseExprList(tt.parenR);
return this.toAssignableList(params);
};
lp.parseMethod = function(isGenerator) {
var node = this.startNode();
this.initFunction(node);
node.params = this.parseFunctionParams();
node.generator = isGenerator || false;
node.expression = this.options.ecmaVersion >= 6 && this.tok.type !== tt.braceL;
node.body = node.expression ? this.parseMaybeAssign() : this.parseBlock();
return this.finishNode(node, "FunctionExpression");
};
lp.parseArrowExpression = function(node, params) {
this.initFunction(node);
node.params = this.toAssignableList(params);
node.expression = this.tok.type !== tt.braceL;
node.body = node.expression ? this.parseMaybeAssign() : this.parseBlock();
return this.finishNode(node, "ArrowFunctionExpression");
};
lp.parseExport = function() {
var node = this.startNode();
this.next();
if (this.eat(tt.star)) {
node.source = this.eatContextual("from") ? this.parseExprAtom() : null;
return this.finishNode(node, "ExportAllDeclaration");
}
if (this.eat(tt._default)) {
var expr = this.parseMaybeAssign();
if (expr.id) {
switch (expr.type) {
case "FunctionExpression": expr.type = "FunctionDeclaration"; break;
case "ClassExpression": expr.type = "ClassDeclaration"; break;
}
}
node.declaration = expr;
this.semicolon();
return this.finishNode(node, "ExportDefaultDeclaration");
}
if (this.tok.type.keyword) {
node.declaration = this.parseStatement();
node.specifiers = [];
node.source = null;
} else {
node.declaration = null;
node.specifiers = this.parseExportSpecifierList();
node.source = this.eatContextual("from") ? this.parseExprAtom() : null;
this.semicolon();
}
return this.finishNode(node, "ExportNamedDeclaration");
};
lp.parseImport = function() {
var node = this.startNode();
this.next();
if (this.tok.type === tt.string) {
node.specifiers = [];
node.source = this.parseExprAtom();
node.kind = '';
} else {
if (this.tok.type === tt.name && this.tok.value !== "from") {
var elt = this.startNode();
elt.local = this.parseIdent();
this.finishNode(elt, "ImportDefaultSpecifier");
this.eat(tt.comma);
}
node.specifiers = this.parseImportSpecifierList();
node.source = this.eatContextual("from") ? this.parseExprAtom() : null;
if (elt) node.specifiers.unshift(elt);
}
this.semicolon();
return this.finishNode(node, "ImportDeclaration");
};
lp.parseImportSpecifierList = function() {
var elts = [];
if (this.tok.type === tt.star) {
var elt = this.startNode();
this.next();
if (this.eatContextual("as")) elt.local = this.parseIdent();
elts.push(this.finishNode(elt, "ImportNamespaceSpecifier"));
} else {
var indent = this.curIndent, line = this.curLineStart, continuedLine = this.nextLineStart;
this.pushCx();
this.eat(tt.braceL);
if (this.curLineStart > continuedLine) continuedLine = this.curLineStart;
while (!this.closes(tt.braceR, indent + (this.curLineStart <= continuedLine ? 1 : 0), line)) {
var elt = this.startNode();
if (this.eat(tt.star)) {
if (this.eatContextual("as")) elt.local = this.parseIdent();
this.finishNode(elt, "ImportNamespaceSpecifier");
} else {
if (this.isContextual("from")) break;
elt.imported = this.parseIdent();
elt.local = this.eatContextual("as") ? this.parseIdent() : elt.imported;
this.finishNode(elt, "ImportSpecifier");
}
elts.push(elt);
this.eat(tt.comma);
}
this.eat(tt.braceR);
this.popCx();
}
return elts;
};
lp.parseExportSpecifierList = function() {
var elts = [];
var indent = this.curIndent, line = this.curLineStart, continuedLine = this.nextLineStart;
this.pushCx();
this.eat(tt.braceL);
if (this.curLineStart > continuedLine) continuedLine = this.curLineStart;
while (!this.closes(tt.braceR, indent + (this.curLineStart <= continuedLine ? 1 : 0), line)) {
if (this.isContextual("from")) break;
var elt = this.startNode();
elt.local = this.parseIdent();
elt.exported = this.eatContextual("as") ? this.parseIdent() : elt.local;
this.finishNode(elt, "ExportSpecifier");
elts.push(elt);
this.eat(tt.comma);
}
this.eat(tt.braceR);
this.popCx();
return elts;
};
lp.parseExprList = function(close, allowEmpty) {
var indent = this.curIndent, line = this.curLineStart, elts = [];
this.next(); // Opening bracket
while (!this.closes(close, indent + 1, line)) {
if (this.eat(tt.comma)) {
elts.push(allowEmpty ? null : this.dummyIdent());
continue;
}
var elt = this.parseMaybeAssign();
if (isDummy(elt)) {
if (this.closes(close, indent, line)) break;
this.next();
} else {
elts.push(elt);
}
this.eat(tt.comma);
}
this.popCx();
if (!this.eat(close)) {
// If there is no closing brace, make the node span to the start
// of the next token (this is useful for Tern)
this.last.end = this.tok.start;
if (this.options.locations) this.last.loc.end = this.tok.loc.start;
}
return elts;
};