Skip to content

Commit f75bb4b

Browse files
authored
Add parse_keyword_with_tokens for paring keyword and tokens combination (#1141)
1 parent 1cf6585 commit f75bb4b

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/parser/mod.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2791,6 +2791,31 @@ impl<'a> Parser<'a> {
27912791
}
27922792
}
27932793

2794+
/// If the current token is the `expected` keyword followed by
2795+
/// specified tokens, consume them and returns true.
2796+
/// Otherwise, no tokens are consumed and returns false.
2797+
///
2798+
/// Note that if the length of `tokens` is too long, this function will
2799+
/// not be efficient as it does a loop on the tokens with `peek_nth_token`
2800+
/// each time.
2801+
pub fn parse_keyword_with_tokens(&mut self, expected: Keyword, tokens: &[Token]) -> bool {
2802+
match self.peek_token().token {
2803+
Token::Word(w) if expected == w.keyword => {
2804+
for (idx, token) in tokens.iter().enumerate() {
2805+
if self.peek_nth_token(idx + 1).token != *token {
2806+
return false;
2807+
}
2808+
}
2809+
// consume all tokens
2810+
for _ in 0..(tokens.len() + 1) {
2811+
self.next_token();
2812+
}
2813+
true
2814+
}
2815+
_ => false,
2816+
}
2817+
}
2818+
27942819
/// If the current and subsequent tokens exactly match the `keywords`
27952820
/// sequence, consume them and returns true. Otherwise, no tokens are
27962821
/// consumed and returns false
@@ -7555,12 +7580,7 @@ impl<'a> Parser<'a> {
75557580
with_offset,
75567581
with_offset_alias,
75577582
})
7558-
} else if matches!(
7559-
self.peek_token().token, Token::Word(w)
7560-
if w.keyword == Keyword::JSON_TABLE && self.peek_nth_token(1).token == Token::LParen
7561-
) {
7562-
self.expect_keyword(Keyword::JSON_TABLE)?;
7563-
self.expect_token(&Token::LParen)?;
7583+
} else if self.parse_keyword_with_tokens(Keyword::JSON_TABLE, &[Token::LParen]) {
75647584
let json_expr = self.parse_expr()?;
75657585
self.expect_token(&Token::Comma)?;
75667586
let json_path = self.parse_value()?;

0 commit comments

Comments
 (0)