Standardize BinaryOp and UnaryOp nodes

These were previously called "BinaryExpr" and "Unary"; besides being
inconsistent, it's also not correct to say "binary expression" or "unary
expression", as it's the operators that have arities, not the
expression. Adjust the naming of the variants accordingly.
This commit is contained in:
Nikhil Benesch 2019-06-05 12:21:38 -04:00
parent b379480b7a
commit 9e33cea9b8
No known key found for this signature in database
GPG key ID: F7386C5DEADABA7F
3 changed files with 58 additions and 58 deletions

View file

@ -89,12 +89,17 @@ pub enum ASTNode {
low: Box<ASTNode>,
high: Box<ASTNode>,
},
/// Binary expression e.g. `1 + 1` or `foo > bar`
SQLBinaryExpr {
/// Binary operation e.g. `1 + 1` or `foo > bar`
SQLBinaryOp {
left: Box<ASTNode>,
op: SQLOperator,
right: Box<ASTNode>,
},
/// Unary operation e.g. `NOT foo`
SQLUnaryOp {
op: SQLOperator,
expr: Box<ASTNode>,
},
/// CAST an expression to a different data type e.g. `CAST(foo AS VARCHAR(123))`
SQLCast {
expr: Box<ASTNode>,
@ -111,11 +116,6 @@ pub enum ASTNode {
},
/// Nested expression e.g. `(foo > bar)` or `(1)`
SQLNested(Box<ASTNode>),
/// Unary expression
SQLUnary {
operator: SQLOperator,
expr: Box<ASTNode>,
},
/// SQLValue
SQLValue(Value),
/// Scalar function call e.g. `LEFT(foo, 5)`
@ -179,12 +179,15 @@ impl ToString for ASTNode {
low.to_string(),
high.to_string()
),
ASTNode::SQLBinaryExpr { left, op, right } => format!(
ASTNode::SQLBinaryOp { left, op, right } => format!(
"{} {} {}",
left.as_ref().to_string(),
op.to_string(),
right.as_ref().to_string()
),
ASTNode::SQLUnaryOp { op, expr } => {
format!("{} {}", op.to_string(), expr.as_ref().to_string())
}
ASTNode::SQLCast { expr, data_type } => format!(
"CAST({} AS {})",
expr.as_ref().to_string(),
@ -199,9 +202,6 @@ impl ToString for ASTNode {
collation.to_string()
),
ASTNode::SQLNested(ast) => format!("({})", ast.as_ref().to_string()),
ASTNode::SQLUnary { operator, expr } => {
format!("{} {}", operator.to_string(), expr.as_ref().to_string())
}
ASTNode::SQLValue(v) => v.to_string(),
ASTNode::SQLFunction(f) => f.to_string(),
ASTNode::SQLCase {

View file

@ -183,8 +183,8 @@ impl Parser {
"EXISTS" => self.parse_exists_expression(),
"EXTRACT" => self.parse_extract_expression(),
"INTERVAL" => self.parse_literal_interval(),
"NOT" => Ok(ASTNode::SQLUnary {
operator: SQLOperator::Not,
"NOT" => Ok(ASTNode::SQLUnaryOp {
op: SQLOperator::Not,
expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
}),
"TIME" => Ok(ASTNode::SQLValue(Value::Time(self.parse_literal_string()?))),
@ -224,13 +224,13 @@ impl Parser {
}, // End of Token::SQLWord
Token::Mult => Ok(ASTNode::SQLWildcard),
tok @ Token::Minus | tok @ Token::Plus => {
let operator = if tok == Token::Plus {
let op = if tok == Token::Plus {
SQLOperator::Plus
} else {
SQLOperator::Minus
};
Ok(ASTNode::SQLUnary {
operator,
Ok(ASTNode::SQLUnaryOp {
op,
expr: Box::new(self.parse_subexpr(Self::PLUS_MINUS_PREC)?),
})
}
@ -541,7 +541,7 @@ impl Parser {
};
if let Some(op) = regular_binary_operator {
Ok(ASTNode::SQLBinaryExpr {
Ok(ASTNode::SQLBinaryOp {
left: Box::new(expr),
op,
right: Box::new(self.parse_subexpr(precedence)?),