Skip to content

Commit

Permalink
Add PartialEq
Browse files Browse the repository at this point in the history
  • Loading branch information
ankrgyl committed Dec 1, 2022
1 parent 25a1701 commit 414f261
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
55 changes: 27 additions & 28 deletions src/parser.rs
Expand Up @@ -154,7 +154,7 @@ impl<'a> Parser<'a> {
expecting_statement_delimiter = false;
}

if parser.peek_token().token == Token::EOF {
if parser.peek_token() == Token::EOF {
break;
}
if expecting_statement_delimiter {
Expand Down Expand Up @@ -515,12 +515,12 @@ impl<'a> Parser<'a> {
Keyword::INTERVAL => self.parse_interval(),
Keyword::LISTAGG => self.parse_listagg_expr(),
// Treat ARRAY[1,2,3] as an array [1,2,3], otherwise try as subquery or a function call
Keyword::ARRAY if self.peek_token().token == Token::LBracket => {
Keyword::ARRAY if self.peek_token() == Token::LBracket => {
self.expect_token(&Token::LBracket)?;
self.parse_array_expr(true)
}
Keyword::ARRAY
if self.peek_token().token == Token::LParen
if self.peek_token() == Token::LParen
&& !dialect_of!(self is ClickHouseDialect) =>
{
self.expect_token(&Token::LParen)?;
Expand Down Expand Up @@ -1574,30 +1574,30 @@ impl<'a> Parser<'a> {
// Can only happen if `get_next_precedence` got out of sync with this function
_ => parser_err!(format!("No infix parser for token {:?}", tok.token)),
}
} else if Token::DoubleColon == tok.token {
} else if Token::DoubleColon == tok {
self.parse_pg_cast(expr)
} else if Token::ExclamationMark == tok.token {
} else if Token::ExclamationMark == tok {
// PostgreSQL factorial operation
Ok(Expr::UnaryOp {
op: UnaryOperator::PGPostfixFactorial,
expr: Box::new(expr),
})
} else if Token::LBracket == tok.token {
} else if Token::LBracket == tok {
if dialect_of!(self is PostgreSqlDialect | GenericDialect) {
// parse index
return self.parse_array_index(expr);
}
self.parse_map_access(expr)
} else if Token::Colon == tok.token {
} else if Token::Colon == tok {
Ok(Expr::JsonAccess {
left: Box::new(expr),
operator: JsonOperator::Colon,
right: Box::new(Expr::Value(self.parse_value()?)),
})
} else if Token::Arrow == tok.token
|| Token::LongArrow == tok.token
|| Token::HashArrow == tok.token
|| Token::HashLongArrow == tok.token
} else if Token::Arrow == tok
|| Token::LongArrow == tok
|| Token::HashArrow == tok
|| Token::HashLongArrow == tok
{
let operator = match tok.token {
Token::Arrow => JsonOperator::Arrow,
Expand Down Expand Up @@ -1962,7 +1962,7 @@ impl<'a> Parser<'a> {
/// Consume the next token if it matches the expected token, otherwise return false
#[must_use]
pub fn consume_token(&mut self, expected: &Token) -> bool {
if self.peek_token().token == *expected {
if self.peek_token() == *expected {
self.next_token();
true
} else {
Expand Down Expand Up @@ -2135,14 +2135,14 @@ impl<'a> Parser<'a> {
table_flag = Some(self.parse_object_name()?);
if self.parse_keyword(Keyword::TABLE) {
let table_name = self.parse_object_name()?;
if self.peek_token().token != Token::EOF {
if self.peek_token() != Token::EOF {
if let Token::Word(word) = self.peek_token().token {
if word.keyword == Keyword::OPTIONS {
options = self.parse_options(Keyword::OPTIONS)?
}
};

if self.peek_token().token != Token::EOF {
if self.peek_token() != Token::EOF {
let (a, q) = self.parse_as_query()?;
has_as = a;
query = Some(q);
Expand All @@ -2165,7 +2165,7 @@ impl<'a> Parser<'a> {
})
}
} else {
if self.peek_token().token == Token::EOF {
if self.peek_token() == Token::EOF {
self.prev_token();
}
self.expected("a `TABLE` keyword", self.peek_token())
Expand Down Expand Up @@ -3792,7 +3792,7 @@ impl<'a> Parser<'a> {
let next_token = self.next_token();
match next_token.token {
Token::Word(Word { value, keyword, .. }) if keyword == Keyword::NoKeyword => {
if self.peek_token().token == Token::LParen {
if self.peek_token() == Token::LParen {
return self.parse_function(ObjectName(vec![Ident::new(value)]));
}
Ok(Expr::Value(Value::SingleQuotedString(value)))
Expand Down Expand Up @@ -5849,8 +5849,7 @@ impl<'a> Parser<'a> {
pub fn parse_merge_clauses(&mut self) -> Result<Vec<MergeClause>, ParserError> {
let mut clauses: Vec<MergeClause> = vec![];
loop {
if self.peek_token().token == Token::EOF || self.peek_token().token == Token::SemiColon
{
if self.peek_token() == Token::EOF || self.peek_token() == Token::SemiColon {
break;
}
self.expect_keyword(Keyword::WHEN)?;
Expand Down Expand Up @@ -6069,19 +6068,19 @@ mod tests {
fn test_prev_index() {
let sql = "SELECT version";
all_dialects().run_parser_method(sql, |parser| {
assert_eq!(parser.peek_token().token, Token::make_keyword("SELECT"));
assert_eq!(parser.next_token().token, Token::make_keyword("SELECT"));
assert_eq!(parser.peek_token(), Token::make_keyword("SELECT"));
assert_eq!(parser.next_token(), Token::make_keyword("SELECT"));
parser.prev_token();
assert_eq!(parser.next_token().token, Token::make_keyword("SELECT"));
assert_eq!(parser.next_token().token, Token::make_word("version", None));
assert_eq!(parser.next_token(), Token::make_keyword("SELECT"));
assert_eq!(parser.next_token(), Token::make_word("version", None));
parser.prev_token();
assert_eq!(parser.peek_token().token, Token::make_word("version", None));
assert_eq!(parser.next_token().token, Token::make_word("version", None));
assert_eq!(parser.peek_token().token, Token::EOF);
assert_eq!(parser.peek_token(), Token::make_word("version", None));
assert_eq!(parser.next_token(), Token::make_word("version", None));
assert_eq!(parser.peek_token(), Token::EOF);
parser.prev_token();
assert_eq!(parser.next_token().token, Token::make_word("version", None));
assert_eq!(parser.next_token().token, Token::EOF);
assert_eq!(parser.next_token().token, Token::EOF);
assert_eq!(parser.next_token(), Token::make_word("version", None));
assert_eq!(parser.next_token(), Token::EOF);
assert_eq!(parser.next_token(), Token::EOF);
parser.prev_token();
});
}
Expand Down
6 changes: 6 additions & 0 deletions src/tokenizer.rs
Expand Up @@ -335,6 +335,12 @@ impl PartialEq<Token> for TokenWithLocation {
}
}

impl PartialEq<TokenWithLocation> for Token {
fn eq(&self, other: &TokenWithLocation) -> bool {
self == &other.token
}
}

impl fmt::Display for TokenWithLocation {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.token.fmt(f)
Expand Down

0 comments on commit 414f261

Please sign in to comment.