finish reorganisation of babylon

This commit is contained in:
Sebastian McKenzie
2015-07-25 08:03:39 +01:00
parent 7e7ad89724
commit 2598299e64
20 changed files with 748 additions and 776 deletions

View File

@@ -0,0 +1,141 @@
/**
* Based on the comment attachment algorithm used in espree and estraverse.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import Parser from "./index";
function last(stack) {
return stack[stack.length - 1];
}
const pp = Parser.prototype;
pp.addComment = function (comment) {
this.state.trailingComments.push(comment);
this.state.leadingComments.push(comment);
};
pp.processComment = function (node) {
if (node.type === "Program" && node.body.length > 0) return;
var stack = this.state.bottomRightStack;
var lastChild, trailingComments, i;
if (this.state.trailingComments.length > 0) {
// If the first comment in trailingComments comes after the
// current node, then we're good - all comments in the array will
// come after the node and so it's safe to add then as official
// trailingComments.
if (this.state.trailingComments[0].start >= node.end) {
trailingComments = this.state.trailingComments;
this.state.trailingComments = [];
} else {
// Otherwise, if the first comment doesn't come after the
// current node, that means we have a mix of leading and trailing
// comments in the array and that leadingComments contains the
// same items as trailingComments. Reset trailingComments to
// zero items and we'll handle this by evaluating leadingComments
// later.
this.state.trailingComments.length = 0;
}
} else {
var lastInStack = last(stack);
if (stack.length > 0 && lastInStack.trailingComments && lastInStack.trailingComments[0].start >= node.end) {
trailingComments = lastInStack.trailingComments;
lastInStack.trailingComments = null;
}
}
// Eating the stack.
while (stack.length > 0 && last(stack).start >= node.start) {
lastChild = stack.pop();
}
if (lastChild) {
if (lastChild.leadingComments) {
if (last(lastChild.leadingComments).end <= node.start) {
node.leadingComments = lastChild.leadingComments;
lastChild.leadingComments = null;
} else {
// A leading comment for an anonymous class had been stolen by its first MethodDefinition,
// so this takes back the leading comment.
// See Also: https://github.com/eslint/espree/issues/158
for (i = lastChild.leadingComments.length - 2; i >= 0; --i) {
if (lastChild.leadingComments[i].end <= node.start) {
node.leadingComments = lastChild.leadingComments.splice(0, i + 1);
break;
}
}
}
}
} else if (this.state.leadingComments.length > 0) {
if (last(this.state.leadingComments).end <= node.start) {
node.leadingComments = this.state.leadingComments;
this.state.leadingComments = [];
} else {
// https://github.com/eslint/espree/issues/2
//
// In special cases, such as return (without a value) and
// debugger, all comments will end up as leadingComments and
// will otherwise be eliminated. This this step runs when the
// bottomRightStack is empty and there are comments left
// in leadingComments.
//
// This loop figures out the stopping point between the actual
// leading and trailing comments by finding the location of the
// first comment that comes after the given node.
for (i = 0; i < this.state.leadingComments.length; i++) {
if (this.state.leadingComments[i].end > node.start) {
break;
}
}
// Split the array based on the location of the first comment
// that comes after the node. Keep in mind that this could
// result in an empty array, and if so, the array must be
// deleted.
node.leadingComments = this.state.leadingComments.slice(0, i);
if (node.leadingComments.length === 0) {
node.leadingComments = null;
}
// Similarly, trailing comments are attached later. The variable
// must be reset to null if there are no trailing comments.
trailingComments = this.state.leadingComments.slice(i);
if (trailingComments.length === 0) {
trailingComments = null;
}
}
}
if (trailingComments) {
if (trailingComments.length && trailingComments[0].start >= node.start && last(trailingComments).end <= node.end) {
node.innerComments = trailingComments;
} else {
node.trailingComments = trailingComments;
}
}
stack.push(node);
};

View File

@@ -0,0 +1,836 @@
// A recursive descent parser operates by defining functions for all
// syntactic elements, and recursively calling those, each function
// advancing the input stream and returning an AST node. Precedence
// of constructs (for example, the fact that `!x[1]` means `!(x[1])`
// instead of `(!x)[1]` is handled by the fact that the parser
// function that parses unary prefix operators is called first, and
// in turn calls the function that parses `[]` subscripts — that
// way, it'll receive the node for `x[1]` already parsed, and wraps
// *that* in the unary operator node.
//
// Acorn uses an [operator precedence parser][opp] to handle binary
// operator precedence, because it is much more compact than using
// the technique outlined above, which uses different, nesting
// functions to specify precedence, for all of the ten binary
// precedence levels that JavaScript defines.
//
// [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser
import { types as tt } from "../tokenizer/types";
import Parser from "./index";
import { reservedWords } from "../util/identifier";
const pp = Parser.prototype;
// Check if property name clashes with already added.
// Object/class getters and setters are not allowed to clash —
// either with each other or with an init property — and in
// strict mode, init properties are also not allowed to be repeated.
pp.checkPropClash = function (prop, propHash) {
if (prop.computed || prop.method || prop.shorthand) return;
let key = prop.key, name;
switch (key.type) {
case "Identifier": name = key.name; break;
case "Literal": name = String(key.value); break;
default: return;
}
let kind = prop.kind;
if (name === "__proto__" && kind === "init") {
if (propHash.proto) this.raise(key.start, "Redefinition of __proto__ property");
propHash.proto = true;
}
};
// ### Expression parsing
// These nest, from the most general expression type at the top to
// 'atomic', nondivisible expression types at the bottom. Most of
// the functions will simply let the function (s) below them parse,
// and, *if* the syntactic construct they handle is present, wrap
// the AST node that the inner parser gave them in another node.
// Parse a full expression. The optional arguments are used to
// forbid the `in` operator (in for loops initalization expressions)
// and provide reference for storing '=' operator inside shorthand
// property assignment in contexts where both object expression
// and object pattern might appear (so it's possible to raise
// delayed syntax error at correct position).
pp.parseExpression = function (noIn, refShorthandDefaultPos) {
let startPos = this.state.start, startLoc = this.state.startLoc;
let expr = this.parseMaybeAssign(noIn, refShorthandDefaultPos);
if (this.state.type === tt.comma) {
let node = this.startNodeAt(startPos, startLoc);
node.expressions = [expr];
while (this.eat(tt.comma)) {
node.expressions.push(this.parseMaybeAssign(noIn, refShorthandDefaultPos));
}
this.toReferencedList(node.expressions);
return this.finishNode(node, "SequenceExpression");
}
return expr;
};
// Parse an assignment expression. This includes applications of
// operators like `+=`.
pp.parseMaybeAssign = function (noIn, refShorthandDefaultPos, afterLeftParse) {
if (this.state.type === tt._yield && this.state.inGenerator) {
return this.parseYield();
}
let failOnShorthandAssign;
if (!refShorthandDefaultPos) {
refShorthandDefaultPos = {start: 0};
failOnShorthandAssign = true;
} else {
failOnShorthandAssign = false;
}
let startPos = this.state.start, startLoc = this.state.startLoc;
if (this.state.type === tt.parenL || this.state.type === tt.name) {
this.state.potentialArrowAt = this.state.start;
}
let left = this.parseMaybeConditional(noIn, refShorthandDefaultPos);
if (afterLeftParse) left = afterLeftParse.call(this, left, startPos, startLoc);
if (this.state.type.isAssign) {
let node = this.startNodeAt(startPos, startLoc);
node.operator = this.state.value;
node.left = this.state.type === tt.eq ? this.toAssignable(left) : left;
refShorthandDefaultPos.start = 0; // reset because shorthand default was used correctly
this.checkLVal(left);
if (left.parenthesizedExpression) {
let errorMsg;
if (left.type === "ObjectPattern") {
errorMsg = "`({a}) = 0` use `({a} = 0)`";
} else if (left.type === "ArrayPattern") {
errorMsg = "`([a]) = 0` use `([a] = 0)`";
}
if (errorMsg) {
this.raise(left.start, `You're trying to assign to a parenthesized expression, eg. instead of ${errorMsg}`);
}
}
this.next();
node.right = this.parseMaybeAssign(noIn);
return this.finishNode(node, "AssignmentExpression");
} else if (failOnShorthandAssign && refShorthandDefaultPos.start) {
this.unexpected(refShorthandDefaultPos.start);
}
return left;
};
// Parse a ternary conditional (`?:`) operator.
pp.parseMaybeConditional = function (noIn, refShorthandDefaultPos) {
let startPos = this.state.start, startLoc = this.state.startLoc;
let expr = this.parseExprOps(noIn, refShorthandDefaultPos);
if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
if (this.eat(tt.question)) {
let node = this.startNodeAt(startPos, startLoc);
node.test = expr;
node.consequent = this.parseMaybeAssign();
this.expect(tt.colon);
node.alternate = this.parseMaybeAssign(noIn);
return this.finishNode(node, "ConditionalExpression");
}
return expr;
};
// Start the precedence parser.
pp.parseExprOps = function (noIn, refShorthandDefaultPos) {
let startPos = this.state.start, startLoc = this.state.startLoc;
let expr = this.parseMaybeUnary(refShorthandDefaultPos);
if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
return this.parseExprOp(expr, startPos, startLoc, -1, noIn);
};
// Parse binary operators with the operator precedence parsing
// algorithm. `left` is the left-hand side of the operator.
// `minPrec` provides context that allows the function to stop and
// defer further parser to one of its callers when it encounters an
// operator that has a lower precedence than the set it is parsing.
pp.parseExprOp = function(left, leftStartPos, leftStartLoc, minPrec, noIn) {
let prec = this.state.type.binop;
if (prec != null && (!noIn || this.state.type !== tt._in)) {
if (prec > minPrec) {
let node = this.startNodeAt(leftStartPos, leftStartLoc);
node.left = left;
node.operator = this.state.value;
let op = this.state.type;
this.next();
let startPos = this.state.start, startLoc = this.state.startLoc;
node.right = this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec, noIn);
this.finishNode(node, (op === tt.logicalOR || op === tt.logicalAND) ? "LogicalExpression" : "BinaryExpression");
return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn);
}
}
return left;
};
// Parse unary operators, both prefix and postfix.
pp.parseMaybeUnary = function (refShorthandDefaultPos) {
if (this.state.type.prefix) {
let node = this.startNode(), update = this.state.type === tt.incDec;
node.operator = this.state.value;
node.prefix = true;
this.next();
node.argument = this.parseMaybeUnary();
if (refShorthandDefaultPos && refShorthandDefaultPos.start) this.unexpected(refShorthandDefaultPos.start);
if (update) {
this.checkLVal(node.argument);
} else if (this.strict && node.operator === "delete" && node.argument.type === "Identifier") {
this.raise(node.start, "Deleting local variable in strict mode");
}
return this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
}
let startPos = this.state.start, startLoc = this.state.startLoc;
let expr = this.parseExprSubscripts(refShorthandDefaultPos);
if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
while (this.state.type.postfix && !this.canInsertSemicolon()) {
let node = this.startNodeAt(startPos, startLoc);
node.operator = this.state.value;
node.prefix = false;
node.argument = expr;
this.checkLVal(expr);
this.next();
expr = this.finishNode(node, "UpdateExpression");
}
return expr;
};
// Parse call, dot, and `[]`-subscript expressions.
pp.parseExprSubscripts = function (refShorthandDefaultPos) {
let startPos = this.state.start, startLoc = this.state.startLoc;
let expr = this.parseExprAtom(refShorthandDefaultPos);
if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
return expr;
} else {
return this.parseSubscripts(expr, startPos, startLoc);
}
};
pp.parseSubscripts = function(base, startPos, startLoc, noCalls) {
for (;;) {
if (!noCalls && this.eat(tt.doubleColon)) {
let node = this.startNodeAt(startPos, startLoc);
node.object = base;
node.callee = this.parseNoCallExpr();
return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
} else if (this.eat(tt.dot)) {
let node = this.startNodeAt(startPos, startLoc);
node.object = base;
node.property = this.parseIdent(true);
node.computed = false;
base = this.finishNode(node, "MemberExpression");
} else if (this.eat(tt.bracketL)) {
let node = this.startNodeAt(startPos, startLoc);
node.object = base;
node.property = this.parseExpression();
node.computed = true;
this.expect(tt.bracketR);
base = this.finishNode(node, "MemberExpression");
} else if (!noCalls && this.state.type === tt.parenL) {
let possibleAsync = false;
if (base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon()) possibleAsync = true;
this.next();
let node = this.startNodeAt(startPos, startLoc);
node.callee = base;
node.arguments = this.parseExprList(tt.parenR, this.options.features["es7.trailingFunctionCommas"]);
base = this.finishNode(node, "CallExpression");
if (possibleAsync && (this.state.type === tt.colon || this.state.type === tt.arrow)) {
base = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
} else {
this.toReferencedList(node.arguments);
}
} else if (this.state.type === tt.backQuote) {
let node = this.startNodeAt(startPos, startLoc);
node.tag = base;
node.quasi = this.parseTemplate();
base = this.finishNode(node, "TaggedTemplateExpression");
} else {
return base;
}
}
};
pp.parseAsyncArrowFromCallExpression = function (node, call) {
if (!this.options.features["es7.asyncFunctions"]) this.unexpected();
this.expect(tt.arrow);
return this.parseArrowExpression(node, call.arguments, true);
};
// Parse a no-call expression (like argument of `new` or `::` operators).
pp.parseNoCallExpr = function () {
let startPos = this.state.start, startLoc = this.state.startLoc;
return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
};
// Parse an atomic expression — either a single token that is an
// expression, an expression started by a keyword like `function` or
// `new`, or an expression wrapped in punctuation like `()`, `[]`,
// or `{}`.
pp.parseExprAtom = function (refShorthandDefaultPos) {
let node, canBeArrow = this.state.potentialArrowAt === this.state.start;
switch (this.state.type) {
case tt._super:
if (!this.state.inFunction)
this.raise(this.state.start, "'super' outside of function or class");
case tt._this:
let type = this.state.type === tt._this ? "ThisExpression" : "Super";
node = this.startNode();
this.next();
return this.finishNode(node, type);
case tt._yield:
if (this.state.inGenerator) this.unexpected();
case tt._do:
if (this.options.features["es7.doExpressions"]) {
let node = this.startNode();
this.next();
var oldInFunction = this.state.inFunction;
var oldLabels = this.state.labels;
this.state.labels = [];
this.state.inFunction = false;
node.body = this.parseBlock();
this.state.inFunction = oldInFunction;
this.state.labels = oldLabels;
return this.finishNode(node, "DoExpression");
}
case tt.name:
node = this.startNode();
let id = this.parseIdent(true);
//
if (this.options.features["es7.asyncFunctions"]) {
if (id.name === "await") {
if (this.inAsync) return this.parseAwait(node);
} else if (id.name === "async" && this.state.type === tt._function && !this.canInsertSemicolon()) {
this.next();
return this.parseFunction(node, false, false, true);
} else if (canBeArrow && id.name === "async" && this.state.type === tt.name) {
var params = [this.parseIdent()];
this.expect(tt.arrow);
// var foo = bar => {};
return this.parseArrowExpression(node, params, true);
}
}
if (canBeArrow && !this.canInsertSemicolon() && this.eat(tt.arrow)) {
return this.parseArrowExpression(node, [id]);
}
return id;
case tt.regexp:
let value = this.state.value;
node = this.parseLiteral(value.value);
node.regex = {pattern: value.pattern, flags: value.flags};
return node;
case tt.num: case tt.string:
return this.parseLiteral(this.state.value);
case tt._null: case tt._true: case tt._false:
node = this.startNode();
node.rawValue = node.value = this.state.type === tt._null ? null : this.state.type === tt._true;
node.raw = this.state.type.keyword;
this.next();
return this.finishNode(node, "Literal");
case tt.parenL:
return this.parseParenAndDistinguishExpression(null, null, canBeArrow);
case tt.bracketL:
node = this.startNode();
this.next();
// check whether this is array comprehension or regular array
if (this.options.features["es7.comprehensions"] && this.state.type === tt._for) {
return this.parseComprehension(node, false);
}
node.elements = this.parseExprList(tt.bracketR, true, true, refShorthandDefaultPos);
this.toReferencedList(node.elements);
return this.finishNode(node, "ArrayExpression");
case tt.braceL:
return this.parseObj(false, refShorthandDefaultPos);
case tt._function:
node = this.startNode();
this.next();
return this.parseFunction(node, false);
case tt.at:
this.parseDecorators();
case tt._class:
node = this.startNode();
this.takeDecorators(node);
return this.parseClass(node, false);
case tt._new:
return this.parseNew();
case tt.backQuote:
return this.parseTemplate();
case tt.doubleColon:
node = this.startNode();
this.next();
node.object = null;
let callee = node.callee = this.parseNoCallExpr();
if (callee.type === "MemberExpression") {
return this.finishNode(node, "BindExpression");
} else {
this.raise(callee.start, "Binding should be performed on object property.");
}
default:
this.unexpected();
}
};
pp.parseLiteral = function (value) {
let node = this.startNode();
node.rawValue = node.value = value;
node.raw = this.input.slice(this.state.start, this.state.end);
this.next();
return this.finishNode(node, "Literal");
};
pp.parseParenExpression = function () {
this.expect(tt.parenL);
let val = this.parseExpression();
this.expect(tt.parenR);
return val;
};
pp.parseParenAndDistinguishExpression = function (startPos, startLoc, canBeArrow, isAsync) {
startPos = startPos || this.state.start;
startLoc = startLoc || this.state.startLoc;
let val;
this.next();
if (this.options.features["es7.comprehensions"] && this.state.type === tt._for) {
return this.parseComprehension(this.startNodeAt(startPos, startLoc), true);
}
let innerStartPos = this.state.start, innerStartLoc = this.state.startLoc;
let exprList = [], first = true;
let refShorthandDefaultPos = {start: 0}, spreadStart, innerParenStart, optionalCommaStart;
while (this.state.type !== tt.parenR) {
if (first) {
first = false;
} else {
this.expect(tt.comma);
if (this.state.type === tt.parenR && this.options.features["es7.trailingFunctionCommas"]) {
optionalCommaStart = this.state.start;
break;
}
}
if (this.state.type === tt.ellipsis) {
let spreadNodeStartPos = this.state.start, spreadNodeStartLoc = this.state.startLoc;
spreadStart = this.state.start;
exprList.push(this.parseParenItem(this.parseRest(), spreadNodeStartLoc, spreadNodeStartPos));
break;
} else {
if (this.state.type === tt.parenL && !innerParenStart) {
innerParenStart = this.state.start;
}
exprList.push(this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem));
}
}
let innerEndPos = this.state.start;
let innerEndLoc = this.state.startLoc;
this.expect(tt.parenR);
if (canBeArrow && !this.canInsertSemicolon() && this.eat(tt.arrow)) {
if (innerParenStart) this.unexpected(innerParenStart);
return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), exprList, isAsync);
}
if (!exprList.length) {
if (isAsync) {
return;
} else {
this.unexpected(this.state.lastTokStart);
}
}
if (optionalCommaStart) this.unexpected(optionalCommaStart);
if (spreadStart) this.unexpected(spreadStart);
if (refShorthandDefaultPos.start) this.unexpected(refShorthandDefaultPos.start);
if (exprList.length > 1) {
val = this.startNodeAt(innerStartPos, innerStartLoc);
val.expressions = exprList;
this.toReferencedList(val.expressions);
this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc);
} else {
val = exprList[0];
}
val.parenthesizedExpression = true;
return val;
};
pp.parseParenItem = function (node) {
return node;
};
// New's precedence is slightly tricky. It must allow its argument
// to be a `[]` or dot subscript expression, but not a call — at
// least, not without wrapping it in parentheses. Thus, it uses the
pp.parseNew = function () {
let node = this.startNode();
let meta = this.parseIdent(true);
if (this.eat(tt.dot)) {
node.meta = meta;
node.property = this.parseIdent(true);
if (node.property.name !== "target") {
this.raise(node.property.start, "The only valid meta property for new is new.target");
}
return this.finishNode(node, "MetaProperty");
}
node.callee = this.parseNoCallExpr();
if (this.eat(tt.parenL)) {
node.arguments = this.parseExprList(tt.parenR, this.options.features["es7.trailingFunctionCommas"]);
this.toReferencedList(node.arguments);
} else {
node.arguments = [];
}
return this.finishNode(node, "NewExpression");
};
// Parse template expression.
pp.parseTemplateElement = function () {
let elem = this.startNode();
elem.value = {
raw: this.input.slice(this.state.start, this.state.end).replace(/\r\n?/g, "\n"),
cooked: this.state.value
};
this.next();
elem.tail = this.state.type === tt.backQuote;
return this.finishNode(elem, "TemplateElement");
};
pp.parseTemplate = function () {
let node = this.startNode();
this.next();
node.expressions = [];
let curElt = this.parseTemplateElement();
node.quasis = [curElt];
while (!curElt.tail) {
this.expect(tt.dollarBraceL);
node.expressions.push(this.parseExpression());
this.expect(tt.braceR);
node.quasis.push(curElt = this.parseTemplateElement());
}
this.next();
return this.finishNode(node, "TemplateLiteral");
};
// Parse an object literal or binding pattern.
pp.parseObj = function (isPattern, refShorthandDefaultPos) {
let node = this.startNode(), first = true, propHash = Object.create(null);
node.properties = [];
let decorators = [];
this.next();
while (!this.eat(tt.braceR)) {
if (first) {
first = false;
} else {
this.expect(tt.comma);
if (this.afterTrailingComma(tt.braceR)) break;
}
while (this.state.type === tt.at) {
decorators.push(this.parseDecorator());
}
let prop = this.startNode(), isGenerator = false, isAsync = false, startPos, startLoc;
if (decorators.length) {
prop.decorators = decorators;
decorators = [];
}
if (this.options.features["es7.objectRestSpread"] && this.state.type === tt.ellipsis) {
prop = this.parseSpread();
prop.type = "SpreadProperty";
node.properties.push(prop);
continue;
}
prop.method = false;
prop.shorthand = false;
if (isPattern || refShorthandDefaultPos) {
startPos = this.state.start;
startLoc = this.state.startLoc;
}
if (!isPattern)
isGenerator = this.eat(tt.star);
if (this.options.features["es7.asyncFunctions"] && this.isContextual("async")) {
if (isGenerator || isPattern) this.unexpected();
var asyncId = this.parseIdent();
if (this.state.type === tt.colon || this.state.type === tt.parenL) {
prop.key = asyncId;
} else {
isAsync = true;
this.parsePropertyName(prop);
}
} else {
this.parsePropertyName(prop);
}
this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos);
this.checkPropClash(prop, propHash);
node.properties.push(this.finishNode(prop, "Property"));
}
if (decorators.length) {
this.raise(this.state.start, "You have trailing decorators with no property");
}
return this.finishNode(node, isPattern ? "ObjectPattern" : "ObjectExpression");
};
pp.parseObjPropValue = function (prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos) {
if (this.eat(tt.colon)) {
prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssign(false, refShorthandDefaultPos);
prop.kind = "init";
} else if (this.state.type === tt.parenL) {
if (isPattern) this.unexpected();
prop.kind = "init";
prop.method = true;
prop.value = this.parseMethod(isGenerator, isAsync);
} else if (!prop.computed && prop.key.type === "Identifier" && (prop.key.name === "get" || prop.key.name === "set") && (this.state.type !== tt.comma && this.state.type !== tt.braceR)) {
if (isGenerator || isAsync || isPattern) this.unexpected();
prop.kind = prop.key.name;
this.parsePropertyName(prop);
prop.value = this.parseMethod(false);
let paramCount = prop.kind === "get" ? 0 : 1;
if (prop.value.params.length !== paramCount) {
let start = prop.value.start;
if (prop.kind === "get")
this.raise(start, "getter should have no params");
else
this.raise(start, "setter should have exactly one param");
}
} else if (!prop.computed && prop.key.type === "Identifier") {
prop.kind = "init";
if (isPattern) {
if (this.isKeyword(prop.key.name) ||
(this.strict && (reservedWords.strictBind(prop.key.name) || reservedWords.strict(prop.key.name))) ||
(!this.options.allowReserved && this.isReservedWord(prop.key.name)))
this.raise(prop.key.start, "Binding " + prop.key.name);
prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
} else if (this.state.type === tt.eq && refShorthandDefaultPos) {
if (!refShorthandDefaultPos.start)
refShorthandDefaultPos.start = this.state.start;
prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
} else {
prop.value = prop.key.__clone();
}
prop.shorthand = true;
} else {
this.unexpected();
}
};
pp.parsePropertyName = function (prop) {
if (this.eat(tt.bracketL)) {
prop.computed = true;
prop.key = this.parseMaybeAssign();
this.expect(tt.bracketR);
return prop.key;
} else {
prop.computed = false;
return prop.key = (this.state.type === tt.num || this.state.type === tt.string) ? this.parseExprAtom() : this.parseIdent(true);
}
};
// Initialize empty function node.
pp.initFunction = function (node, isAsync) {
node.id = null;
node.generator = false;
node.expression = false;
if (this.options.features["es7.asyncFunctions"]) {
node.async = !!isAsync;
}
};
// Parse object or class method.
pp.parseMethod = function (isGenerator, isAsync) {
let node = this.startNode();
this.initFunction(node, isAsync);
this.expect(tt.parenL);
node.params = this.parseBindingList(tt.parenR, false, this.options.features["es7.trailingFunctionCommas"]);
node.generator = isGenerator;
this.parseFunctionBody(node);
return this.finishNode(node, "FunctionExpression");
};
// Parse arrow function expression with given parameters.
pp.parseArrowExpression = function (node, params, isAsync) {
this.initFunction(node, isAsync);
node.params = this.toAssignableList(params, true);
this.parseFunctionBody(node, true);
return this.finishNode(node, "ArrowFunctionExpression");
};
// Parse function body and check parameters.
pp.parseFunctionBody = function (node, allowExpression) {
let isExpression = allowExpression && this.state.type !== tt.braceL;
var oldInAsync = this.inAsync;
this.inAsync = node.async;
if (isExpression) {
node.body = this.parseMaybeAssign();
node.expression = true;
} else {
// Start a new scope with regard to labels and the `inFunction`
// flag (restore them to their old value afterwards).
let oldInFunc = this.state.inFunction, oldInGen = this.state.inGenerator, oldLabels = this.state.labels;
this.state.inFunction = true; this.state.inGenerator = node.generator; this.state.labels = [];
node.body = this.parseBlock(true);
node.expression = false;
this.state.inFunction = oldInFunc; this.state.inGenerator = oldInGen; this.state.labels = oldLabels;
}
this.inAsync = oldInAsync;
// If this is a strict mode function, verify that argument names
// are not repeated, and it does not try to bind the words `eval`
// or `arguments`.
if (this.strict || !isExpression && node.body.body.length && this.isUseStrict(node.body.body[0])) {
let nameHash = Object.create(null), oldStrict = this.strict;
this.strict = true;
if (node.id) {
this.checkLVal(node.id, true);
}
for (let param of (node.params: Array)) {
this.checkLVal(param, true, nameHash);
}
this.strict = oldStrict;
}
};
// Parses a comma-separated list of expressions, and returns them as
// an array. `close` is the token type that ends the list, and
// `allowEmpty` can be turned on to allow subsequent commas with
// nothing in between them to be parsed as `null` (which is needed
// for array literals).
pp.parseExprList = function (close, allowTrailingComma, allowEmpty, refShorthandDefaultPos) {
let elts = [], first = true;
while (!this.eat(close)) {
if (first) {
first = false;
} else {
this.expect(tt.comma);
if (allowTrailingComma && this.afterTrailingComma(close)) break;
}
elts.push(this.parseExprListItem(allowEmpty, refShorthandDefaultPos));
}
return elts;
};
pp.parseExprListItem = function (allowEmpty, refShorthandDefaultPos) {
let elt;
if (allowEmpty && this.state.type === tt.comma) {
elt = null;
} else if (this.state.type === tt.ellipsis) {
elt = this.parseSpread(refShorthandDefaultPos);
} else {
elt = this.parseMaybeAssign(false, refShorthandDefaultPos);
}
return elt;
};
// Parse the next token as an identifier. If `liberal` is true (used
// when parsing properties), it will also convert keywords into
// identifiers.
pp.parseIdent = function (liberal) {
let node = this.startNode();
if (this.state.type === tt.name) {
if (!liberal &&
((!this.options.allowReserved && this.isReservedWord(this.state.value)) ||
(this.strict && reservedWords.strict(this.state.value))))
this.raise(this.state.start, "The keyword '" + this.state.value + "' is reserved");
node.name = this.state.value;
} else if (liberal && this.state.type.keyword) {
node.name = this.state.type.keyword;
} else {
this.unexpected();
}
this.next();
return this.finishNode(node, "Identifier");
};
// Parses await expression inside async function.
pp.parseAwait = function (node) {
if (this.eat(tt.semi) || this.canInsertSemicolon()) {
this.unexpected();
}
node.all = this.eat(tt.star);
node.argument = this.parseMaybeUnary();
return this.finishNode(node, "AwaitExpression");
};
// Parses yield expression inside generator.
pp.parseYield = function () {
let node = this.startNode();
this.next();
if (this.state.type === tt.semi || this.canInsertSemicolon() || (this.state.type !== tt.star && !this.state.type.startsExpr)) {
node.delegate = false;
node.argument = null;
} else {
node.delegate = this.eat(tt.star);
node.argument = this.parseMaybeAssign();
}
return this.finishNode(node, "YieldExpression");
};
// Parses array and generator comprehensions.
pp.parseComprehension = function (node, isGenerator) {
node.blocks = [];
while (this.state.type === tt._for) {
let block = this.startNode();
this.next();
this.expect(tt.parenL);
block.left = this.parseBindingAtom();
this.checkLVal(block.left, true);
this.expectContextual("of");
block.right = this.parseExpression();
this.expect(tt.parenR);
node.blocks.push(this.finishNode(block, "ComprehensionBlock"));
}
node.filter = this.eat(tt._if) ? this.parseParenExpression() : null;
node.body = this.parseExpression();
this.expect(isGenerator ? tt.parenR : tt.bracketR);
node.generator = isGenerator;
return this.finishNode(node, "ComprehensionExpression");
};

View File

@@ -0,0 +1,47 @@
import { reservedWords, isKeyword } from "../util/identifier";
import { getOptions } from "../options";
import Tokenizer from "../tokenizer";
// Registered plugins
export const plugins = {};
export default class Parser extends Tokenizer {
constructor(options, input) {
super(input);
this.options = getOptions(options);
this.isKeyword = isKeyword;
this.isReservedWord = reservedWords[6];
this.input = input;
this.loadPlugins(this.options.plugins);
// Figure out if it's a module code.
this.inModule = this.options.sourceType === "module";
this.strict = this.options.strictMode === false ? false : this.inModule;
// If enabled, skip leading hashbang line.
if (this.state.pos === 0 && this.input[0] === "#" && this.input[1] === "!") {
this.skipLineComment(2);
}
}
extend(name, f) {
this[name] = f(this[name]);
}
loadPlugins(plugins) {
for (let name in plugins) {
let plugin = exports.plugins[name];
if (!plugin) throw new Error(`Plugin '${name}' not found`);
plugin(this, plugins[name]);
}
}
parse() {
let file = this.startNode();
let program = this.startNode();
this.nextToken();
return this.parseTopLevel(file, program);
}
}

View File

@@ -0,0 +1,19 @@
import { getLineInfo } from "../util/location";
import Parser from "./index";
const pp = Parser.prototype;
// This function is used to raise exceptions on parse errors. It
// takes an offset integer (into the current `input`) to indicate
// the location of the error, attaches the position to the end
// of the error message, and then raises a `SyntaxError` with that
// message.
pp.raise = function (pos, message) {
let loc = getLineInfo(this.input, pos);
message += ` (${loc.line}:${loc.column})`;
let err = new SyntaxError(message);
err.pos = pos;
err.loc = loc;
throw err;
};

View File

@@ -0,0 +1,211 @@
import { types as tt } from "../tokenizer/types";
import Parser from "./index";
import { reservedWords } from "../util/identifier";
const pp = Parser.prototype;
// Convert existing expression atom to assignable pattern
// if possible.
pp.toAssignable = function (node, isBinding) {
if (node) {
switch (node.type) {
case "Identifier":
case "ObjectPattern":
case "ArrayPattern":
case "AssignmentPattern":
break;
case "ObjectExpression":
node.type = "ObjectPattern";
for (let prop of (node.properties: Array)) {
if (prop.type === "SpreadProperty") continue;
if (prop.kind !== "init") this.raise(prop.key.start, "Object pattern can't contain getter or setter");
this.toAssignable(prop.value, isBinding);
}
break;
case "ArrayExpression":
node.type = "ArrayPattern";
this.toAssignableList(node.elements, isBinding);
break;
case "AssignmentExpression":
if (node.operator === "=") {
node.type = "AssignmentPattern";
delete node.operator;
} else {
this.raise(node.left.end, "Only '=' operator can be used for specifying default value.");
}
break;
case "MemberExpression":
if (!isBinding) break;
default:
this.raise(node.start, "Assigning to rvalue");
}
}
return node;
};
// Convert list of expression atoms to binding list.
pp.toAssignableList = function (exprList, isBinding) {
let end = exprList.length;
if (end) {
let last = exprList[end - 1];
if (last && last.type === "RestElement") {
--end;
} else if (last && last.type === "SpreadElement") {
last.type = "RestElement";
let arg = last.argument;
this.toAssignable(arg, isBinding);
if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern") {
this.unexpected(arg.start);
}
--end;
}
}
for (let i = 0; i < end; i++) {
let elt = exprList[i];
if (elt) this.toAssignable(elt, isBinding);
}
return exprList;
};
// Convert list of expression atoms to a list of
pp.toReferencedList = function (exprList) {
return exprList;
};
// Parses spread element.
pp.parseSpread = function (refShorthandDefaultPos) {
let node = this.startNode();
this.next();
node.argument = this.parseMaybeAssign(refShorthandDefaultPos);
return this.finishNode(node, "SpreadElement");
};
pp.parseRest = function () {
let node = this.startNode();
this.next();
node.argument = this.state.type === tt.name || this.state.type === tt.bracketL ? this.parseBindingAtom() : this.unexpected();
return this.finishNode(node, "RestElement");
};
// Parses lvalue (assignable) atom.
pp.parseBindingAtom = function () {
switch (this.state.type) {
case tt.name:
return this.parseIdent();
case tt.bracketL:
let node = this.startNode();
this.next();
node.elements = this.parseBindingList(tt.bracketR, true, true);
return this.finishNode(node, "ArrayPattern");
case tt.braceL:
return this.parseObj(true);
default:
this.unexpected();
}
};
pp.parseBindingList = function (close, allowEmpty, allowTrailingComma) {
var elts = [], first = true;
while (!this.eat(close)) {
if (first) first = false;
else this.expect(tt.comma);
if (allowEmpty && this.state.type === tt.comma) {
elts.push(null);
} else if (allowTrailingComma && this.afterTrailingComma(close)) {
break;
} else if (this.state.type === tt.ellipsis) {
elts.push(this.parseAssignableListItemTypes(this.parseRest()));
this.expect(close);
break;
} else {
var left = this.parseMaybeDefault();
this.parseAssignableListItemTypes(left);
elts.push(this.parseMaybeDefault(null, null, left));
}
}
return elts;
};
pp.parseAssignableListItemTypes = function (param) {
return param;
};
// Parses assignment pattern around given atom if possible.
pp.parseMaybeDefault = function (startPos, startLoc, left) {
startLoc = startLoc || this.state.startLoc;
startPos = startPos || this.state.start;
left = left || this.parseBindingAtom();
if (!this.eat(tt.eq)) return left;
let node = this.startNodeAt(startPos, startLoc);
node.operator = "=";
node.left = left;
node.right = this.parseMaybeAssign();
return this.finishNode(node, "AssignmentPattern");
};
// Verify that a node is an lval — something that can be assigned
// to.
pp.checkLVal = function (expr, isBinding, checkClashes) {
switch (expr.type) {
case "Identifier":
if (this.strict && (reservedWords.strictBind(expr.name) || reservedWords.strict(expr.name)))
this.raise(expr.start, (isBinding ? "Binding " : "Assigning to ") + expr.name + " in strict mode");
if (checkClashes) {
if (checkClashes[expr.name]) {
this.raise(expr.start, "Argument name clash in strict mode");
} else {
checkClashes[expr.name] = true;
}
}
break;
case "MemberExpression":
if (isBinding) this.raise(expr.start, (isBinding ? "Binding" : "Assigning to") + " member expression");
break;
case "ObjectPattern":
for (let prop of (expr.properties: Array)) {
if (prop.type === "Property") prop = prop.value;
this.checkLVal(prop, isBinding, checkClashes);
}
break;
case "ArrayPattern":
for (let elem of (expr.elements: Array)) {
if (elem) this.checkLVal(elem, isBinding, checkClashes);
}
break;
case "AssignmentPattern":
this.checkLVal(expr.left, isBinding, checkClashes);
break;
case "SpreadProperty":
case "RestElement":
this.checkLVal(expr.argument, isBinding, checkClashes);
break;
case "ParenthesizedExpression":
this.checkLVal(expr.expression, isBinding, checkClashes);
break;
default:
this.raise(expr.start, (isBinding ? "Binding" : "Assigning to") + " rvalue");
}
};

View File

@@ -0,0 +1,52 @@
import Parser from "./index";
import { SourceLocation } from "../util/location";
// Start an AST node, attaching a start offset.
const pp = Parser.prototype;
export class Node {
constructor(parser, pos, loc) {
this.type = "";
this.start = pos;
this.end = 0;
if (parser) {
this.loc = new SourceLocation(loc);
}
}
__clone() {
var node2 = new Node;
for (var key in this) node2[key] = this[key];
return node2;
}
}
pp.startNode = function () {
return new Node(this, this.state.start, this.state.startLoc);
};
pp.startNodeAt = function (pos, loc) {
return new Node(this, pos, loc);
};
function finishNodeAt(node, type, pos, loc) {
node.type = type;
node.end = pos;
node.loc.end = loc;
this.processComment(node);
return node;
}
// Finish an AST node, adding `type` and `end` properties.
pp.finishNode = function (node, type) {
return finishNodeAt.call(this, node, type, this.state.lastTokEnd, this.state.lastTokEndLoc);
};
// Finish node at given position
pp.finishNodeAt = function (node, type, pos, loc) {
return finishNodeAt.call(this, node, type, pos, loc);
};

View File

@@ -0,0 +1,807 @@
import { types as tt } from "../tokenizer/types";
import Parser from "./index";
import { lineBreak } from "../util/whitespace";
const pp = Parser.prototype;
// ### Statement parsing
// Parse a program. Initializes the parser, reads any number of
// statements, and wraps them in a Program node. Optionally takes a
// `program` argument. If present, the statements will be appended
// to its body instead of creating a new node.
pp.parseTopLevel = function (file, program) {
program.sourceType = this.options.sourceType;
program.body = [];
let first = true;
while (this.state.type !== tt.eof) {
let stmt = this.parseStatement(true, true);
program.body.push(stmt);
if (first) {
if (this.isUseStrict(stmt)) this.setStrict(true);
first = false;
}
}
this.next();
file.program = this.finishNode(program, "Program");
file.comments = this.state.comments;
file.tokens = this.state.tokens;
return this.finishNode(file, "File");
};
const loopLabel = {kind: "loop"}, switchLabel = {kind: "switch"};
// Parse a single statement.
//
// If expecting a statement and finding a slash operator, parse a
// regular expression literal. This is to handle cases like
// `if (foo) /blah/.exec(foo)`, where looking at the previous token
// does not help.
pp.parseStatement = function (declaration, topLevel) {
if (this.state.type === tt.at) {
this.parseDecorators(true);
}
let starttype = this.state.type, node = this.startNode();
// Most types of statements are recognized by the keyword they
// start with. Many are trivial to parse, some require a bit of
// complexity.
switch (starttype) {
case tt._break: case tt._continue: return this.parseBreakContinueStatement(node, starttype.keyword);
case tt._debugger: return this.parseDebuggerStatement(node);
case tt._do: return this.parseDoStatement(node);
case tt._for: return this.parseForStatement(node);
case tt._function:
if (!declaration) this.unexpected();
return this.parseFunctionStatement(node);
case tt._class:
if (!declaration) this.unexpected();
this.takeDecorators(node);
return this.parseClass(node, true);
case tt._if: return this.parseIfStatement(node);
case tt._return: return this.parseReturnStatement(node);
case tt._switch: return this.parseSwitchStatement(node);
case tt._throw: return this.parseThrowStatement(node);
case tt._try: return this.parseTryStatement(node);
case tt._let: case tt._const: if (!declaration) this.unexpected(); // NOTE: falls through to _var
case tt._var: return this.parseVarStatement(node, starttype);
case tt._while: return this.parseWhileStatement(node);
case tt._with: return this.parseWithStatement(node);
case tt.braceL: return this.parseBlock();
case tt.semi: return this.parseEmptyStatement(node);
case tt._export:
case tt._import:
if (!this.options.allowImportExportEverywhere) {
if (!topLevel)
this.raise(this.state.start, "'import' and 'export' may only appear at the top level");
if (!this.inModule)
this.raise(this.state.start, "'import' and 'export' may appear only with 'sourceType: module'");
}
return starttype === tt._import ? this.parseImport(node) : this.parseExport(node);
case tt.name:
if (this.options.features["es7.asyncFunctions"] && this.state.value === "async") {
// peek ahead and see if next token is a function
var state = this.state.clone();
this.next();
if (this.state.type === tt._function && !this.canInsertSemicolon()) {
this.expect(tt._function);
return this.parseFunction(node, true, false, true);
} else {
this.state = state;
}
}
// If the statement does not start with a statement keyword or a
// brace, it's an ExpressionStatement or LabeledStatement. We
// simply start parsing an expression, and afterwards, if the
// next token is a colon and the expression was a simple
// Identifier node, we switch to interpreting it as a label.
default:
let maybeName = this.state.value, expr = this.parseExpression();
if (starttype === tt.name && expr.type === "Identifier" && this.eat(tt.colon)) {
return this.parseLabeledStatement(node, maybeName, expr);
} else {
return this.parseExpressionStatement(node, expr);
}
}
};
pp.takeDecorators = function (node) {
if (this.state.decorators.length) {
node.decorators = this.state.decorators;
this.state.decorators = [];
}
};
pp.parseDecorators = function (allowExport) {
while (this.state.type === tt.at) {
this.state.decorators.push(this.parseDecorator());
}
if (allowExport && this.state.type === tt._export) {
return;
}
if (this.state.type !== tt._class) {
this.raise(this.state.start, "Leading decorators must be attached to a class declaration");
}
};
pp.parseDecorator = function () {
if (!this.options.features["es7.decorators"]) {
this.unexpected();
}
let node = this.startNode();
this.next();
node.expression = this.parseMaybeAssign();
return this.finishNode(node, "Decorator");
};
pp.parseBreakContinueStatement = function (node, keyword) {
let isBreak = keyword === "break";
this.next();
if (this.eat(tt.semi) || this.insertSemicolon()) {
node.label = null;
} else if (this.state.type !== tt.name) {
this.unexpected();
} else {
node.label = this.parseIdent();
this.semicolon();
}
// Verify that there is an actual destination to break or
// continue to.
for (var i = 0; i < this.state.labels.length; ++i) {
let lab = this.state.labels[i];
if (node.label == null || lab.name === node.label.name) {
if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
if (node.label && isBreak) break;
}
}
if (i === this.state.labels.length) this.raise(node.start, "Unsyntactic " + keyword);
return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
};
pp.parseDebuggerStatement = function (node) {
this.next();
this.semicolon();
return this.finishNode(node, "DebuggerStatement");
};
pp.parseDoStatement = function (node) {
this.next();
this.state.labels.push(loopLabel);
node.body = this.parseStatement(false);
this.state.labels.pop();
this.expect(tt._while);
node.test = this.parseParenExpression();
this.eat(tt.semi);
return this.finishNode(node, "DoWhileStatement");
};
// Disambiguating between a `for` and a `for`/`in` or `for`/`of`
// loop is non-trivial. Basically, we have to parse the init `var`
// statement or expression, disallowing the `in` operator (see
// the second parameter to `parseExpression`), and then check
// whether the next token is `in` or `of`. When there is no init
// part (semicolon immediately after the opening parenthesis), it
// is a regular `for` loop.
pp.parseForStatement = function (node) {
this.next();
this.state.labels.push(loopLabel);
this.expect(tt.parenL);
if (this.state.type === tt.semi) {
return this.parseFor(node, null);
}
if (this.state.type === tt._var || this.state.type === tt._let || this.state.type === tt._const) {
let init = this.startNode(), varKind = this.state.type;
this.next();
this.parseVar(init, true, varKind);
this.finishNode(init, "VariableDeclaration");
if ((this.state.type === tt._in || this.isContextual("of")) && init.declarations.length === 1 &&
!(varKind !== tt._var && init.declarations[0].init))
return this.parseForIn(node, init);
return this.parseFor(node, init);
}
let refShorthandDefaultPos = {start: 0};
let init = this.parseExpression(true, refShorthandDefaultPos);
if (this.state.type === tt._in || this.isContextual("of")) {
this.toAssignable(init);
this.checkLVal(init);
return this.parseForIn(node, init);
} else if (refShorthandDefaultPos.start) {
this.unexpected(refShorthandDefaultPos.start);
}
return this.parseFor(node, init);
};
pp.parseFunctionStatement = function (node) {
this.next();
return this.parseFunction(node, true);
};
pp.parseIfStatement = function (node) {
this.next();
node.test = this.parseParenExpression();
node.consequent = this.parseStatement(false);
node.alternate = this.eat(tt._else) ? this.parseStatement(false) : null;
return this.finishNode(node, "IfStatement");
};
pp.parseReturnStatement = function (node) {
if (!this.state.inFunction && !this.options.allowReturnOutsideFunction) {
this.raise(this.state.start, "'return' outside of function");
}
this.next();
// In `return` (and `break`/`continue`), the keywords with
// optional arguments, we eagerly look for a semicolon or the
// possibility to insert one.
if (this.eat(tt.semi) || this.insertSemicolon()) {
node.argument = null;
} else {
node.argument = this.parseExpression();
this.semicolon();
}
return this.finishNode(node, "ReturnStatement");
};
pp.parseSwitchStatement = function (node) {
this.next();
node.discriminant = this.parseParenExpression();
node.cases = [];
this.expect(tt.braceL);
this.state.labels.push(switchLabel);
// Statements under must be grouped (by label) in SwitchCase
// nodes. `cur` is used to keep the node that we are currently
// adding statements to.
for (var cur, sawDefault; this.state.type !== tt.braceR; ) {
if (this.state.type === tt._case || this.state.type === tt._default) {
let isCase = this.state.type === tt._case;
if (cur) this.finishNode(cur, "SwitchCase");
node.cases.push(cur = this.startNode());
cur.consequent = [];
this.next();
if (isCase) {
cur.test = this.parseExpression();
} else {
if (sawDefault) this.raise(this.state.lastTokStart, "Multiple default clauses");
sawDefault = true;
cur.test = null;
}
this.expect(tt.colon);
} else {
if (!cur) this.unexpected();
cur.consequent.push(this.parseStatement(true));
}
}
if (cur) this.finishNode(cur, "SwitchCase");
this.next(); // Closing brace
this.state.labels.pop();
return this.finishNode(node, "SwitchStatement");
};
pp.parseThrowStatement = function (node) {
this.next();
if (lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start)))
this.raise(this.state.lastTokEnd, "Illegal newline after throw");
node.argument = this.parseExpression();
this.semicolon();
return this.finishNode(node, "ThrowStatement");
};
// Reused empty array added for node fields that are always empty.
var empty = [];
pp.parseTryStatement = function (node) {
this.next();
node.block = this.parseBlock();
node.handler = null;
if (this.state.type === tt._catch) {
let clause = this.startNode();
this.next();
this.expect(tt.parenL);
clause.param = this.parseBindingAtom();
this.checkLVal(clause.param, true);
this.expect(tt.parenR);
clause.body = this.parseBlock();
node.handler = this.finishNode(clause, "CatchClause");
}
node.guardedHandlers = empty;
node.finalizer = this.eat(tt._finally) ? this.parseBlock() : null;
if (!node.handler && !node.finalizer) {
this.raise(node.start, "Missing catch or finally clause");
}
return this.finishNode(node, "TryStatement");
};
pp.parseVarStatement = function (node, kind) {
this.next();
this.parseVar(node, false, kind);
this.semicolon();
return this.finishNode(node, "VariableDeclaration");
};
pp.parseWhileStatement = function (node) {
this.next();
node.test = this.parseParenExpression();
this.state.labels.push(loopLabel);
node.body = this.parseStatement(false);
this.state.labels.pop();
return this.finishNode(node, "WhileStatement");
};
pp.parseWithStatement = function (node) {
if (this.strict) this.raise(this.state.start, "'with' in strict mode");
this.next();
node.object = this.parseParenExpression();
node.body = this.parseStatement(false);
return this.finishNode(node, "WithStatement");
};
pp.parseEmptyStatement = function (node) {
this.next();
return this.finishNode(node, "EmptyStatement");
};
pp.parseLabeledStatement = function (node, maybeName, expr) {
for (let label of (this.state.labels: Array)){
if (label.name === maybeName) {
this.raise(expr.start, `Label '${maybeName}' is already declared`);
}
}
let kind = this.state.type.isLoop ? "loop" : this.state.type === tt._switch ? "switch" : null;
for (let i = this.state.labels.length - 1; i >= 0; i--) {
let label = this.state.labels[i];
if (label.statementStart === node.start) {
label.statementStart = this.state.start;
label.kind = kind;
} else {
break;
}
}
this.state.labels.push({name: maybeName, kind: kind, statementStart: this.state.start});
node.body = this.parseStatement(true);
this.state.labels.pop();
node.label = expr;
return this.finishNode(node, "LabeledStatement");
};
pp.parseExpressionStatement = function (node, expr) {
node.expression = expr;
this.semicolon();
return this.finishNode(node, "ExpressionStatement");
};
// Parse a semicolon-enclosed block of statements, handling `"use
// strict"` declarations when `allowStrict` is true (used for
// function bodies).
pp.parseBlock = function (allowStrict) {
let node = this.startNode(), first = true, oldStrict;
node.body = [];
this.expect(tt.braceL);
while (!this.eat(tt.braceR)) {
let stmt = this.parseStatement(true);
node.body.push(stmt);
if (first && allowStrict && this.isUseStrict(stmt)) {
oldStrict = this.strict;
this.setStrict(this.strict = true);
}
first = false;
}
if (oldStrict === false) this.setStrict(false);
return this.finishNode(node, "BlockStatement");
};
// Parse a regular `for` loop. The disambiguation code in
// `parseStatement` will already have parsed the init statement or
// expression.
pp.parseFor = function (node, init) {
node.init = init;
this.expect(tt.semi);
node.test = this.state.type === tt.semi ? null : this.parseExpression();
this.expect(tt.semi);
node.update = this.state.type === tt.parenR ? null : this.parseExpression();
this.expect(tt.parenR);
node.body = this.parseStatement(false);
this.state.labels.pop();
return this.finishNode(node, "ForStatement");
};
// Parse a `for`/`in` and `for`/`of` loop, which are almost
// same from parser's perspective.
pp.parseForIn = function (node, init) {
let type = this.state.type === tt._in ? "ForInStatement" : "ForOfStatement";
this.next();
node.left = init;
node.right = this.parseExpression();
this.expect(tt.parenR);
node.body = this.parseStatement(false);
this.state.labels.pop();
return this.finishNode(node, type);
};
// Parse a list of variable declarations.
pp.parseVar = function (node, isFor, kind) {
node.declarations = [];
node.kind = kind.keyword;
for (;;) {
let decl = this.startNode();
this.parseVarHead(decl);
if (this.eat(tt.eq)) {
decl.init = this.parseMaybeAssign(isFor);
} else if (kind === tt._const && !(this.state.type === tt._in || this.isContextual("of"))) {
this.unexpected();
} else if (decl.id.type !== "Identifier" && !(isFor && (this.state.type === tt._in || this.isContextual("of")))) {
this.raise(this.state.lastTokEnd, "Complex binding patterns require an initialization value");
} else {
decl.init = null;
}
node.declarations.push(this.finishNode(decl, "VariableDeclarator"));
if (!this.eat(tt.comma)) break;
}
return node;
};
pp.parseVarHead = function (decl) {
decl.id = this.parseBindingAtom();
this.checkLVal(decl.id, true);
};
// Parse a function declaration or literal (depending on the
// `isStatement` parameter).
pp.parseFunction = function (node, isStatement, allowExpressionBody, isAsync) {
this.initFunction(node, isAsync);
node.generator = this.eat(tt.star);
if (isStatement || this.state.type === tt.name) {
node.id = this.parseIdent();
}
this.parseFunctionParams(node);
this.parseFunctionBody(node, allowExpressionBody);
return this.finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
};
pp.parseFunctionParams = function (node) {
this.expect(tt.parenL);
node.params = this.parseBindingList(tt.parenR, false, this.options.features["es7.trailingFunctionCommas"]);
};
// Parse a class declaration or literal (depending on the
// `isStatement` parameter).
pp.parseClass = function (node, isStatement) {
this.next();
this.parseClassId(node, isStatement);
this.parseClassSuper(node);
var classBody = this.startNode();
let hadConstructor = false;
classBody.body = [];
this.expect(tt.braceL);
let decorators = [];
while (!this.eat(tt.braceR)) {
if (this.eat(tt.semi)) continue;
if (this.state.type === tt.at) {
decorators.push(this.parseDecorator());
continue;
}
var method = this.startNode();
if (decorators.length) {
method.decorators = decorators;
decorators = [];
}
let isMaybeStatic = this.state.type === tt.name && this.state.value === "static";
var isGenerator = this.eat(tt.star), isAsync = false;
this.parsePropertyName(method);
method.static = isMaybeStatic && this.state.type !== tt.parenL;
if (method.static) {
if (isGenerator) this.unexpected();
isGenerator = this.eat(tt.star);
this.parsePropertyName(method);
}
if (!isGenerator && method.key.type === "Identifier" && !method.computed && this.isClassProperty()) {
classBody.body.push(this.parseClassProperty(method));
continue;
}
if (this.options.features["es7.asyncFunctions"] && this.state.type !== tt.parenL &&
!method.computed && method.key.type === "Identifier" && method.key.name === "async") {
isAsync = true;
this.parsePropertyName(method);
}
let isGetSet = false;
method.kind = "method";
if (!method.computed) {
let {key} = method;
if (!isAsync && !isGenerator && key.type === "Identifier" && this.state.type !== tt.parenL && (key.name === "get" || key.name === "set")) {
isGetSet = true;
method.kind = key.name;
key = this.parsePropertyName(method);
}
if (!method.static && (key.type === "Identifier" && key.name === "constructor" ||
key.type === "Literal" && key.value === "constructor")) {
if (hadConstructor) this.raise(key.start, "Duplicate constructor in the same class");
if (isGetSet) this.raise(key.start, "Constructor can't have get/set modifier");
if (isGenerator) this.raise(key.start, "Constructor can't be a generator");
if (isAsync) this.raise(key.start, "Constructor can't be an async function");
method.kind = "constructor";
hadConstructor = true;
}
}
if (method.kind === "constructor" && method.decorators) {
this.raise(method.start, "You can't attach decorators to a class constructor");
}
this.parseClassMethod(classBody, method, isGenerator, isAsync);
if (isGetSet) {
let paramCount = method.kind === "get" ? 0 : 1;
if (method.value.params.length !== paramCount) {
let start = method.value.start;
if (method.kind === "get") {
this.raise(start, "getter should have no params");
} else {
this.raise(start, "setter should have exactly one param");
}
}
}
}
if (decorators.length) {
this.raise(this.state.start, "You have trailing decorators with no method");
}
node.body = this.finishNode(classBody, "ClassBody");
return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
};
pp.isClassProperty = function () {
return this.state.type === tt.eq || (this.state.type === tt.semi || this.canInsertSemicolon());
};
pp.parseClassProperty = function (node) {
if (this.state.type === tt.eq) {
if (!this.options.features["es7.classProperties"]) this.unexpected();
this.next();
node.value = this.parseMaybeAssign();
} else {
node.value = null;
}
this.semicolon();
return this.finishNode(node, "ClassProperty");
};
pp.parseClassMethod = function (classBody, method, isGenerator, isAsync) {
method.value = this.parseMethod(isGenerator, isAsync);
classBody.body.push(this.finishNode(method, "MethodDefinition"));
};
pp.parseClassId = function (node, isStatement) {
node.id = this.state.type === tt.name ? this.parseIdent() : isStatement ? this.unexpected() : null;
};
pp.parseClassSuper = function (node) {
node.superClass = this.eat(tt._extends) ? this.parseExprSubscripts() : null;
};
// Parses module export declaration.
pp.parseExport = function (node) {
this.next();
// export * from '...'
if (this.state.type === tt.star) {
let specifier = this.startNode();
this.next();
if (this.options.features["es7.exportExtensions"] && this.eatContextual("as")) {
specifier.exported = this.parseIdent();
node.specifiers = [this.finishNode(specifier, "ExportNamespaceSpecifier")];
this.parseExportSpecifiersMaybe(node);
this.parseExportFrom(node);
} else {
this.parseExportFrom(node);
return this.finishNode(node, "ExportAllDeclaration");
}
} else if (this.options.features["es7.exportExtensions"] && this.isExportDefaultSpecifier()) {
let specifier = this.startNode();
specifier.exported = this.parseIdent(true);
node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
if (this.state.type === tt.comma && this.lookahead().type === tt.star) {
this.expect(tt.comma);
let specifier = this.startNode();
this.expect(tt.star);
this.expectContextual("as");
specifier.exported = this.parseIdent();
node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
} else {
this.parseExportSpecifiersMaybe(node);
}
this.parseExportFrom(node);
} else if (this.eat(tt._default)) { // export default ...
let expr = this.parseMaybeAssign();
let needsSemi = true;
if (expr.type === "FunctionExpression" || expr.type === "ClassExpression") {
needsSemi = false;
if (expr.id) {
expr.type = expr.type === "FunctionExpression" ? "FunctionDeclaration" : "ClassDeclaration";
}
}
node.declaration = expr;
if (needsSemi) this.semicolon();
this.checkExport(node);
return this.finishNode(node, "ExportDefaultDeclaration");
} else if (this.state.type.keyword || this.shouldParseExportDeclaration()) {
node.declaration = this.parseStatement(true);
node.specifiers = [];
node.source = null;
} else { // export { x, y as z } [from '...']
node.declaration = null;
node.specifiers = this.parseExportSpecifiers();
if (this.eatContextual("from")) {
node.source = this.state.type === tt.string ? this.parseExprAtom() : this.unexpected();
} else {
node.source = null;
}
this.semicolon();
}
this.checkExport(node);
return this.finishNode(node, "ExportNamedDeclaration");
};
pp.isExportDefaultSpecifier = function () {
if (this.state.type === tt.name) {
return this.state.value !== "type" && this.state.value !== "async";
}
if (this.state.type !== tt._default) {
return false;
}
var lookahead = this.lookahead();
return lookahead.type === tt.comma || (lookahead.type === tt.name && lookahead.value === "from");
};
pp.parseExportSpecifiersMaybe = function (node) {
if (this.eat(tt.comma)) {
node.specifiers = node.specifiers.concat(this.parseExportSpecifiers());
}
};
pp.parseExportFrom = function (node) {
this.expectContextual("from");
node.source = this.state.type === tt.string ? this.parseExprAtom() : this.unexpected();
this.semicolon();
this.checkExport(node);
};
pp.shouldParseExportDeclaration = function () {
return this.options.features["es7.asyncFunctions"] && this.isContextual("async");
};
pp.checkExport = function (node) {
if (this.state.decorators.length) {
var isClass = node.declaration && (node.declaration.type === "ClassDeclaration" || node.declaration.type === "ClassExpression");
if (!node.declaration || !isClass) {
this.raise(node.start, "You can only use decorators on an export when exporting a class");
}
this.takeDecorators(node.declaration);
}
};
// Parses a comma-separated list of module exports.
pp.parseExportSpecifiers = function () {
let nodes = [], first = true;
// export { x, y as z } [from '...']
this.expect(tt.braceL);
while (!this.eat(tt.braceR)) {
if (first) {
first = false;
} else {
this.expect(tt.comma);
if (this.afterTrailingComma(tt.braceR)) break;
}
let node = this.startNode();
node.local = this.parseIdent(this.state.type === tt._default);
node.exported = this.eatContextual("as") ? this.parseIdent(true) : node.local.__clone();
nodes.push(this.finishNode(node, "ExportSpecifier"));
}
return nodes;
};
// Parses import declaration.
pp.parseImport = function (node) {
this.next();
// import '...'
if (this.state.type === tt.string) {
node.specifiers = [];
node.source = this.parseExprAtom();
} else {
node.specifiers = [];
this.parseImportSpecifiers(node);
this.expectContextual("from");
node.source = this.state.type === tt.string ? this.parseExprAtom() : this.unexpected();
}
this.semicolon();
return this.finishNode(node, "ImportDeclaration");
};
// Parses a comma-separated list of module imports.
pp.parseImportSpecifiers = function (node) {
var first = true;
if (this.state.type === tt.name) {
// import defaultObj, { x, y as z } from '...'
var startPos = this.state.start, startLoc = this.state.startLoc;
node.specifiers.push(this.parseImportSpecifierDefault(this.parseIdent(), startPos, startLoc));
if (!this.eat(tt.comma)) return;
}
if (this.state.type === tt.star) {
let specifier = this.startNode();
this.next();
this.expectContextual("as");
specifier.local = this.parseIdent();
this.checkLVal(specifier.local, true);
node.specifiers.push(this.finishNode(specifier, "ImportNamespaceSpecifier"));
return;
}
this.expect(tt.braceL);
while (!this.eat(tt.braceR)) {
if (first) {
first = false;
} else {
this.expect(tt.comma);
if (this.afterTrailingComma(tt.braceR)) break;
}
let specifier = this.startNode();
specifier.imported = this.parseIdent(true);
specifier.local = this.eatContextual("as") ? this.parseIdent() : specifier.imported.__clone();
this.checkLVal(specifier.local, true);
node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
}
};
pp.parseImportSpecifierDefault = function (id, startPos, startLoc) {
var node = this.startNodeAt(startPos, startLoc);
node.local = id;
this.checkLVal(node.local, true);
return this.finishNode(node, "ImportDefaultSpecifier");
};

View File

@@ -0,0 +1,100 @@
import { types as tt } from "../tokenizer/types";
import Parser from "./index";
import { lineBreak } from "../util/whitespace";
const pp = Parser.prototype;
// ## Parser utilities
// Test whether a statement node is the string literal `"use strict"`.
pp.isUseStrict = function (stmt) {
return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && stmt.expression.raw.slice(1, -1) === "use strict";
};
// Predicate that tests whether the next token is of the given
// type, and if yes, consumes it as a side effect.
pp.eat = function (type) {
if (this.state.type === type) {
this.next();
return true;
} else {
return false;
}
};
// TODO
pp.isRelational = function (op) {
return this.state.type === tt.relational && this.state.value === op;
};
// TODO
pp.expectRelational = function (op) {
if (this.isRelational(op)) {
this.next();
} else {
this.unexpected();
}
};
// Tests whether parsed token is a contextual keyword.
pp.isContextual = function (name) {
return this.state.type === tt.name && this.state.value === name;
};
// Consumes contextual keyword if possible.
pp.eatContextual = function (name) {
return this.state.value === name && this.eat(tt.name);
};
// Asserts that following token is given contextual keyword.
pp.expectContextual = function (name) {
if (!this.eatContextual(name)) this.unexpected();
};
// Test whether a semicolon can be inserted at the current position.
pp.canInsertSemicolon = function () {
return this.state.type === tt.eof ||
this.state.type === tt.braceR ||
lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
};
pp.insertSemicolon = function () {
if (this.canInsertSemicolon()) {
return true;
}
};
// Consume a semicolon, or, failing that, see if we are allowed to
// pretend that there is a semicolon at this position.
pp.semicolon = function () {
if (!this.eat(tt.semi) && !this.insertSemicolon()) this.unexpected();
};
pp.afterTrailingComma = function (tokType) {
if (this.state.type === tokType) {
this.next();
return true;
}
};
// Expect a token of a given type. If found, consume it, otherwise,
// raise an unexpected token error.
pp.expect = function (type) {
return this.eat(type) || this.unexpected();
};
// Raise an unexpected token error.
pp.unexpected = function (pos) {
this.raise(pos != null ? pos : this.state.start, "Unexpected token");
};