Skip to content

Commit f55e3d5

Browse files
committed
Introduce a peek_nth_token method
This will be used in a future commit, where looking ahead by two tokens is important.
1 parent 202464a commit f55e3d5

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/sqlparser.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -561,40 +561,40 @@ impl Parser {
561561

562562
/// Return first non-whitespace token that has not yet been processed
563563
pub fn peek_token(&self) -> Option<Token> {
564-
if let Some(n) = self.til_non_whitespace() {
565-
self.token_at(n)
566-
} else {
567-
None
568-
}
564+
self.peek_nth_token(0)
569565
}
570566

571-
/// Get the next token skipping whitespace and increment the token index
572-
pub fn next_token(&mut self) -> Option<Token> {
567+
/// Return nth non-whitespace token that has not yet been processed
568+
pub fn peek_nth_token(&self, mut n: usize) -> Option<Token> {
569+
let mut index = self.index;
573570
loop {
574-
match self.next_token_no_skip() {
571+
match self.token_at(index) {
575572
Some(Token::Whitespace(_)) => {
576-
continue;
573+
index += 1;
577574
}
578-
token => {
579-
return token;
575+
Some(token) => {
576+
if n == 0 {
577+
return Some(token);
578+
}
579+
index += 1;
580+
n -= 1;
581+
}
582+
None => {
583+
return None;
580584
}
581585
}
582586
}
583587
}
584588

585-
/// get the index for non whitepsace token
586-
fn til_non_whitespace(&self) -> Option<usize> {
587-
let mut index = self.index;
589+
/// Get the next token skipping whitespace and increment the token index
590+
pub fn next_token(&mut self) -> Option<Token> {
588591
loop {
589-
match self.token_at(index) {
592+
match self.next_token_no_skip() {
590593
Some(Token::Whitespace(_)) => {
591-
index += 1;
592-
}
593-
Some(_) => {
594-
return Some(index);
594+
continue;
595595
}
596-
None => {
597-
return None;
596+
token => {
597+
return token;
598598
}
599599
}
600600
}

0 commit comments

Comments
 (0)