@@ -1315,7 +1315,9 @@ impl<'a> Parser<'a> {
1315
1315
1316
1316
let dialect = self.dialect;
1317
1317
1318
- let (next_token, next_token_index) = self.next_token_ref_with_index();
1318
+ self.advance_token();
1319
+ let next_token_index = self.get_current_index();
1320
+ let next_token = self.get_current_token();
1319
1321
let span = next_token.span;
1320
1322
let expr = match &next_token.token {
1321
1323
Token::Word(w) => {
@@ -2953,7 +2955,9 @@ impl<'a> Parser<'a> {
2953
2955
2954
2956
let dialect = self.dialect;
2955
2957
2956
- let (tok, tok_index) = self.next_token_ref_with_index();
2958
+ self.advance_token();
2959
+ let tok = self.get_current_token();
2960
+ let tok_index = self.get_current_index();
2957
2961
let span = tok.span;
2958
2962
let regular_binary_operator = match &tok.token {
2959
2963
Token::Spaceship => Some(BinaryOperator::Spaceship),
@@ -3033,7 +3037,8 @@ impl<'a> Parser<'a> {
3033
3037
// See https://www.postgresql.org/docs/current/sql-createoperator.html
3034
3038
let mut idents = vec![];
3035
3039
loop {
3036
- idents.push(self.next_token_ref().to_string());
3040
+ self.advance_token();
3041
+ idents.push(self.get_current_token().to_string());
3037
3042
if !self.consume_token(&Token::Period) {
3038
3043
break;
3039
3044
}
@@ -3480,6 +3485,8 @@ impl<'a> Parser<'a> {
3480
3485
3481
3486
/// Return the first non-whitespace token that has not yet been processed
3482
3487
/// or Token::EOF
3488
+ ///
3489
+ /// See [`Self::peek_token_ref`] to avoid the copy.
3483
3490
pub fn peek_token(&self) -> TokenWithSpan {
3484
3491
self.peek_nth_token(0)
3485
3492
}
@@ -3594,47 +3601,70 @@ impl<'a> Parser<'a> {
3594
3601
3595
3602
/// Advances to the next non-whitespace token and returns a copy.
3596
3603
///
3597
- /// See [`Self::next_token_ref`] to avoid the copy.
3604
+ /// Please use [`Self::advance_token`] and [`Self::get_current_token`] to
3605
+ /// avoid the copy.
3598
3606
pub fn next_token(&mut self) -> TokenWithSpan {
3599
- self.next_token_ref().clone()
3607
+ self.advance_token();
3608
+ self.get_current_token().clone()
3600
3609
}
3601
3610
3602
- pub fn next_token_ref(&mut self) -> &TokenWithSpan {
3603
- self.next_token_ref_with_index().0
3611
+ /// Returns the index of the current token
3612
+ ///
3613
+ /// This can be used with APIs that expect an index, such as
3614
+ /// [`Self::token_at`]
3615
+ pub fn get_current_index(&self) -> usize {
3616
+ self.index.saturating_sub(1)
3604
3617
}
3605
3618
3606
- /// Return the first non-whitespace token that has not yet been processed
3607
- /// and that tokens index and advances the tokens
3619
+ /// Return the next unprocessed token, possibly whitespace.
3620
+ pub fn next_token_no_skip(&mut self) -> Option<&TokenWithSpan> {
3621
+ self.index += 1;
3622
+ self.tokens.get(self.index - 1)
3623
+ }
3624
+
3625
+ /// Advances the current token to the next non-whitespace token
3608
3626
///
3609
- /// # Notes:
3610
- /// OK to call repeatedly after reaching EOF.
3611
- pub fn next_token_ref_with_index(&mut self) -> (&TokenWithSpan, usize) {
3627
+ /// See [`Self::get_current_token`] to get the current token after advancing
3628
+ pub fn advance_token(&mut self) {
3612
3629
loop {
3613
3630
self.index += 1;
3614
3631
match self.tokens.get(self.index - 1) {
3615
3632
Some(TokenWithSpan {
3616
3633
token: Token::Whitespace(_),
3617
3634
span: _,
3618
3635
}) => continue,
3619
- token => return (token.unwrap_or(&EOF_TOKEN), self.index - 1) ,
3636
+ _ => break ,
3620
3637
}
3621
3638
}
3622
3639
}
3623
3640
3624
3641
/// Returns a reference to the current token
3625
- pub fn current_token(&self) -> &TokenWithSpan {
3626
- self.tokens.get(self.index - 1).unwrap_or(&EOF_TOKEN)
3642
+ ///
3643
+ /// Does not advance the current token.
3644
+ pub fn get_current_token(&self) -> &TokenWithSpan {
3645
+ self.token_at(self.index.saturating_sub(1))
3627
3646
}
3628
3647
3629
- /// Return the first unprocessed token, possibly whitespace.
3630
- pub fn next_token_no_skip(&mut self) -> Option<&TokenWithSpan> {
3631
- self.index += 1;
3632
- self.tokens.get(self.index - 1)
3648
+ /// Returns a reference to the previous token
3649
+ ///
3650
+ /// Does not advance the current token.
3651
+ pub fn get_previous_token(&self) -> &TokenWithSpan {
3652
+ self.token_at(self.index.saturating_sub(2))
3633
3653
}
3634
3654
3635
- /// Push back the last one non-whitespace token. Must be called after
3636
- /// `next_token()`, otherwise might panic. OK to call after
3637
- /// `next_token()` indicates an EOF.
3655
+ /// Returns a reference to the next token
3656
+ ///
3657
+ /// Does not advance the current token.
3658
+ pub fn get_next_token(&self) -> &TokenWithSpan {
3659
+ self.token_at(self.index)
3660
+ }
3661
+
3662
+ /// Seek back the last one non-whitespace token.
3663
+ ///
3664
+ /// Must be called after `next_token()`, otherwise might panic. OK to call
3665
+ /// after `next_token()` indicates an EOF.
3666
+ ///
3667
+ // TODO rename to backup_token and deprecate prev_token?
3638
3668
pub fn prev_token(&mut self) {
3639
3669
loop {
3640
3670
assert!(self.index > 0);
@@ -3680,22 +3710,30 @@ impl<'a> Parser<'a> {
3680
3710
#[must_use]
3681
3711
pub fn parse_keyword(&mut self, expected: Keyword) -> bool {
3682
3712
if self.peek_keyword(expected) {
3683
- self.next_token_ref ();
3713
+ self.advance_token ();
3684
3714
true
3685
3715
} else {
3686
3716
false
3687
3717
}
3688
3718
}
3689
3719
3720
+ /// If the current token is the `expected` keyword, consume it and returns
3721
+ ///
3722
+ /// See [`Self::parse_keyword_token_ref`] to avoid the copy.
3690
3723
#[must_use]
3691
3724
pub fn parse_keyword_token(&mut self, expected: Keyword) -> Option<TokenWithSpan> {
3692
3725
self.parse_keyword_token_ref(expected).cloned()
3693
3726
}
3694
3727
3728
+ /// If the current token is the `expected` keyword, consume it and returns a reference to the next token.
3729
+ ///
3695
3730
#[must_use]
3696
3731
pub fn parse_keyword_token_ref(&mut self, expected: Keyword) -> Option<&TokenWithSpan> {
3697
3732
match &self.peek_token_ref().token {
3698
- Token::Word(w) if expected == w.keyword => Some(self.next_token_ref()),
3733
+ Token::Word(w) if expected == w.keyword => {
3734
+ self.advance_token();
3735
+ Some(self.get_current_token())
3736
+ }
3699
3737
_ => None,
3700
3738
}
3701
3739
}
@@ -3722,7 +3760,7 @@ impl<'a> Parser<'a> {
3722
3760
}
3723
3761
// consume all tokens
3724
3762
for _ in 0..(tokens.len() + 1) {
3725
- self.next_token_ref ();
3763
+ self.advance_token ();
3726
3764
}
3727
3765
true
3728
3766
}
@@ -3758,7 +3796,7 @@ impl<'a> Parser<'a> {
3758
3796
.iter()
3759
3797
.find(|keyword| **keyword == w.keyword)
3760
3798
.map(|keyword| {
3761
- self.next_token_ref ();
3799
+ self.advance_token ();
3762
3800
*keyword
3763
3801
})
3764
3802
}
@@ -3813,10 +3851,12 @@ impl<'a> Parser<'a> {
3813
3851
}
3814
3852
3815
3853
/// Consume the next token if it matches the expected token, otherwise return false
3854
+ ///
3855
+ /// See [Self::advance_token] to consume the token unconditionally
3816
3856
#[must_use]
3817
3857
pub fn consume_token(&mut self, expected: &Token) -> bool {
3818
3858
if self.peek_token_ref() == expected {
3819
- self.next_token_ref ();
3859
+ self.advance_token ();
3820
3860
true
3821
3861
} else {
3822
3862
false
@@ -8338,9 +8378,9 @@ impl<'a> Parser<'a> {
8338
8378
&mut self,
8339
8379
) -> Result<(DataType, MatchedTrailingBracket), ParserError> {
8340
8380
let dialect = self.dialect;
8341
- let (next_token, next_token_index) = self.next_token_ref_with_index ();
8342
- let _ = next_token; // release ref
8343
- let next_token = self.current_token ();
8381
+ self.advance_token ();
8382
+ let next_token = self.get_current_token();
8383
+ let next_token_index = self.get_current_index ();
8344
8384
8345
8385
let mut trailing_bracket: MatchedTrailingBracket = false.into();
8346
8386
let mut data = match &next_token.token {
@@ -8866,7 +8906,7 @@ impl<'a> Parser<'a> {
8866
8906
Token::EOF | Token::Eq => break,
8867
8907
_ => {}
8868
8908
}
8869
- self.next_token_ref ();
8909
+ self.advance_token ();
8870
8910
}
8871
8911
Ok(idents)
8872
8912
}
0 commit comments