Support unary + / -

This commit is contained in:
Nickolay Ponomarev 2019-02-11 02:30:07 +03:00
parent 786b1cf18a
commit 54c9ca8619
2 changed files with 30 additions and 1 deletions

View file

@ -174,6 +174,7 @@ impl Parser {
expr: Box::new(self.parse_subexpr(p)?),
})
}
// another SQLWord:
_ => match self.peek_token() {
Some(Token::LParen) => self.parse_function(w.as_sql_ident()),
Some(Token::Period) => {
@ -201,8 +202,15 @@ impl Parser {
}
_ => Ok(ASTNode::SQLIdentifier(w.as_sql_ident())),
},
},
}, // End of Token::SQLWord
Token::Mult => Ok(ASTNode::SQLWildcard),
tok @ Token::Minus | tok @ Token::Plus => {
let p = self.get_precedence(&tok)?;
Ok(ASTNode::SQLUnary {
operator: self.to_sql_operator(&tok)?,
expr: Box::new(self.parse_subexpr(p)?),
})
}
Token::Number(_)
| Token::SingleQuotedString(_)
| Token::NationalStringLiteral(_) => {

View file

@ -174,6 +174,27 @@ fn parse_compound_expr_2() {
);
}
#[test]
fn parse_unary_math() {
use self::ASTNode::*;
use self::SQLOperator::*;
let sql = "- a + - b";
assert_eq!(
SQLBinaryExpr {
left: Box::new(SQLUnary {
operator: Minus,
expr: Box::new(SQLIdentifier("a".to_string())),
}),
op: Plus,
right: Box::new(SQLUnary {
operator: Minus,
expr: Box::new(SQLIdentifier("b".to_string())),
}),
},
verified_expr(sql)
);
}
#[test]
fn parse_is_null() {
use self::ASTNode::*;