mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-09-10 07:56:20 +00:00
Introduce a peek_nth_token method
This will be used in a future commit, where looking ahead by two tokens is important.
This commit is contained in:
parent
202464a06a
commit
f55e3d5305
1 changed files with 22 additions and 22 deletions
|
@ -561,10 +561,28 @@ impl Parser {
|
||||||
|
|
||||||
/// Return first non-whitespace token that has not yet been processed
|
/// Return first non-whitespace token that has not yet been processed
|
||||||
pub fn peek_token(&self) -> Option<Token> {
|
pub fn peek_token(&self) -> Option<Token> {
|
||||||
if let Some(n) = self.til_non_whitespace() {
|
self.peek_nth_token(0)
|
||||||
self.token_at(n)
|
}
|
||||||
} else {
|
|
||||||
None
|
/// Return nth non-whitespace token that has not yet been processed
|
||||||
|
pub fn peek_nth_token(&self, mut n: usize) -> Option<Token> {
|
||||||
|
let mut index = self.index;
|
||||||
|
loop {
|
||||||
|
match self.token_at(index) {
|
||||||
|
Some(Token::Whitespace(_)) => {
|
||||||
|
index += 1;
|
||||||
|
}
|
||||||
|
Some(token) => {
|
||||||
|
if n == 0 {
|
||||||
|
return Some(token);
|
||||||
|
}
|
||||||
|
index += 1;
|
||||||
|
n -= 1;
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -582,24 +600,6 @@ impl Parser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// get the index for non whitepsace token
|
|
||||||
fn til_non_whitespace(&self) -> Option<usize> {
|
|
||||||
let mut index = self.index;
|
|
||||||
loop {
|
|
||||||
match self.token_at(index) {
|
|
||||||
Some(Token::Whitespace(_)) => {
|
|
||||||
index += 1;
|
|
||||||
}
|
|
||||||
Some(_) => {
|
|
||||||
return Some(index);
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// see the token at this index
|
/// see the token at this index
|
||||||
fn token_at(&self, n: usize) -> Option<Token> {
|
fn token_at(&self, n: usize) -> Option<Token> {
|
||||||
if let Some(token) = self.tokens.get(n) {
|
if let Some(token) = self.tokens.get(n) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue