Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Dec 9, 2019
1 parent 92e7f20 commit 93a4212
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions acorn/src/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,11 @@ pp.parseMaybeConditional = function(noIn, refDestructuringErrors) {

// Start the precedence parser.

const BOTH_ALLOWED = 0
const ONLY_LOGICAL = 1
const ONLY_COALESCE = 2

pp.parseExprOps = function(noIn, refDestructuringErrors) {
let startPos = this.start, startLoc = this.startLoc
let expr = this.parseMaybeUnary(refDestructuringErrors, false)
if (this.checkExpressionErrors(refDestructuringErrors)) return expr
return expr.start === startPos && expr.type === "ArrowFunctionExpression" ? expr : this.parseExprOp(expr, startPos, startLoc, -1, noIn, BOTH_ALLOWED)
return expr.start === startPos && expr.type === "ArrowFunctionExpression" ? expr : this.parseExprOp(expr, startPos, startLoc, -1, noIn)
}

// Parse binary operators with the operator precedence parsing
Expand All @@ -182,24 +178,24 @@ pp.parseExprOps = function(noIn, refDestructuringErrors) {
// 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, shortCircuitOps) {
pp.parseExprOp = function(left, leftStartPos, leftStartLoc, minPrec, noIn) {
let prec = this.type.binop
if (prec != null && (!noIn || this.type !== tt._in)) {
if (prec != null && (!noIn || this.type !== tt._in) && prec > minPrec) {
let logical = this.type === tt.logicalOR || this.type === tt.logicalAND
let coalesce = this.type === tt.coalesce
if (shortCircuitOps === ONLY_LOGICAL && coalesce || shortCircuitOps === ONLY_COALESCE && logical) {
let op = this.value
this.next()
let startPos = this.start
let startLoc = this.startLoc
let rightBase = this.parseMaybeUnary(null, false)
if (logical && this.type === tt.coalesce ||
coalesce && (this.type === tt.logicalAND || this.type === tt.logicalOR)
) {
this.raiseRecoverable(this.start, "Logical expressions and coalesce expressions cannot be mixed. Wrap either by parentheses")
}
shortCircuitOps = shortCircuitOps || (logical ? ONLY_LOGICAL : coalesce ? ONLY_COALESCE : BOTH_ALLOWED)

if (prec > minPrec) {
let op = this.value
this.next()
let startPos = this.start, startLoc = this.startLoc
let right = this.parseExprOp(this.parseMaybeUnary(null, false), startPos, startLoc, prec, noIn, shortCircuitOps)
let node = this.buildBinary(leftStartPos, leftStartLoc, left, right, op, logical || coalesce)
return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn, shortCircuitOps)
}
let right = this.parseExprOp(rightBase, startPos, startLoc, prec, noIn)
let node = this.buildBinary(leftStartPos, leftStartLoc, left, right, op, logical || coalesce)
return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn)
}
return left
}
Expand Down

0 comments on commit 93a4212

Please sign in to comment.