@@ -34,6 +34,12 @@ macro_rules! parser_err {
34
34
} ;
35
35
}
36
36
37
+ /// The parser state
38
+ pub struct Marker {
39
+ /// position in the token stream (`parser.index`)
40
+ index : usize ,
41
+ }
42
+
37
43
#[ derive( PartialEq ) ]
38
44
pub enum IsOptional {
39
45
Optional ,
@@ -742,6 +748,14 @@ impl Parser {
742
748
}
743
749
}
744
750
751
+ pub fn start ( & mut self ) -> Marker {
752
+ Marker { index : self . index }
753
+ }
754
+
755
+ pub fn reset ( & mut self , m : Marker ) {
756
+ self . index = m. index ;
757
+ }
758
+
745
759
/// Return the first non-whitespace token that has not yet been processed
746
760
/// (or None if reached end-of-file)
747
761
pub fn peek_token ( & self ) -> Option < Token > {
@@ -827,12 +841,10 @@ impl Parser {
827
841
/// Look for an expected sequence of keywords and consume them if they exist
828
842
#[ must_use]
829
843
pub fn parse_keywords ( & mut self , keywords : Vec < & ' static str > ) -> bool {
830
- let index = self . index ;
844
+ let checkpoint = self . start ( ) ;
831
845
for keyword in keywords {
832
846
if !self . parse_keyword ( & keyword) {
833
- //println!("parse_keywords aborting .. did not find {}", keyword);
834
- // reset index and return immediately
835
- self . index = index;
847
+ self . reset ( checkpoint) ;
836
848
return false ;
837
849
}
838
850
}
@@ -1920,7 +1932,7 @@ impl Parser {
1920
1932
}
1921
1933
1922
1934
if self . consume_token ( & Token :: LParen ) {
1923
- let index = self . index ;
1935
+ let checkpoint = self . start ( ) ;
1924
1936
// A left paren introduces either a derived table (i.e., a subquery)
1925
1937
// or a nested join. It's nearly impossible to determine ahead of
1926
1938
// time which it is... so we just try to parse both.
@@ -1948,7 +1960,7 @@ impl Parser {
1948
1960
// the '(' we've recently consumed does not start a derived table
1949
1961
// (cases 1, 2, or 4). Ignore the error and back up to where we
1950
1962
// were before - right after the opening '('.
1951
- self . index = index ;
1963
+ self . reset ( checkpoint ) ;
1952
1964
1953
1965
// Inside the parentheses we expect to find a table factor
1954
1966
// followed by some joins or another level of nesting.
0 commit comments