@@ -40,6 +40,7 @@ macro_rules! parser_err {
40
40
}
41
41
42
42
/// The parser state
43
+ #[ derive( Clone ) ]
43
44
pub struct Marker {
44
45
/// position in the token stream (`parser.index`)
45
46
index : usize ,
@@ -238,6 +239,7 @@ impl Parser {
238
239
/// the `precedence` is 0 (representing the lowest binding power).
239
240
pub fn parse_subexpr ( & mut self , precedence : u8 ) -> Result < Expr , ParserError > {
240
241
debug ! ( "parsing expr" ) ;
242
+ let m = self . start ( ) ;
241
243
let mut expr = self . parse_prefix ( ) ?;
242
244
debug ! ( "prefix: {:?}" , expr) ;
243
245
loop {
@@ -259,13 +261,14 @@ impl Parser {
259
261
// | |< current token (returned by `peek_token()`;
260
262
// `precedence` has `next_precedence`)
261
263
//
262
- expr = self . parse_infix ( expr, next_precedence) ?;
264
+ expr = self . parse_infix ( m . clone ( ) , expr, next_precedence) ?;
263
265
}
264
266
Ok ( expr)
265
267
}
266
268
267
269
/// Parse an expression prefix
268
270
pub fn parse_prefix ( & mut self ) -> Result < Expr , ParserError > {
271
+ let m = self . start ( ) ;
269
272
let tok = self
270
273
. next_token ( )
271
274
. ok_or_else ( || ParserError :: ParserError ( "Unexpected EOF" . to_string ( ) ) ) ?;
@@ -341,11 +344,14 @@ impl Parser {
341
344
Token :: LParen => {
342
345
let expr = if self . parse_keyword ( "SELECT" ) || self . parse_keyword ( "WITH" ) {
343
346
self . prev_token ( ) ;
344
- Expr :: Subquery ( Box :: new ( self . parse_query ( ) ?) )
347
+ let expr = Expr :: Subquery ( Box :: new ( self . parse_query ( ) ?) ) ;
348
+ self . expect_token ( & Token :: RParen ) ?;
349
+ ret ! ( expr => via self . complete( m, SK :: EXPR_SUBQUERY , ..) )
345
350
} else {
346
- Expr :: Nested ( Box :: new ( self . parse_expr ( ) ?) )
351
+ let expr = Expr :: Nested ( Box :: new ( self . parse_expr ( ) ?) ) ;
352
+ self . expect_token ( & Token :: RParen ) ?;
353
+ ret ! ( expr => via self . complete( m, SK :: EXPR_NESTED , ..) )
347
354
} ;
348
- self . expect_token ( & Token :: RParen ) ?;
349
355
Ok ( expr)
350
356
}
351
357
unexpected => self . expected ( "an expression" , Some ( unexpected) ) ,
@@ -651,7 +657,12 @@ impl Parser {
651
657
}
652
658
653
659
/// Parse an operator following an expression
654
- pub fn parse_infix ( & mut self , expr : Expr , precedence : u8 ) -> Result < Expr , ParserError > {
660
+ pub fn parse_infix (
661
+ & mut self ,
662
+ m : Marker ,
663
+ expr : Expr ,
664
+ precedence : u8 ,
665
+ ) -> Result < Expr , ParserError > {
655
666
debug ! ( "parsing infix" ) ;
656
667
let tok = self . next_token ( ) . unwrap ( ) ; // safe as EOF's precedence is the lowest
657
668
@@ -688,11 +699,12 @@ impl Parser {
688
699
} ;
689
700
690
701
if let Some ( op) = regular_binary_operator {
691
- Ok ( Expr :: BinaryOp {
692
- left : Box :: new ( expr) ,
693
- op,
694
- right : Box :: new ( self . parse_subexpr ( precedence) ?) ,
695
- } )
702
+ ret ! ( Ok ( Expr :: BinaryOp {
703
+ left: Box :: new( expr) ,
704
+ op,
705
+ right: Box :: new( self . parse_subexpr( precedence) ?) ,
706
+ } )
707
+ => via self . complete( m, SK :: BIN_EXPR , ..) ) // TBD OTHER
696
708
} else if let Token :: Word ( ref k) = tok {
697
709
match k. keyword . as_ref ( ) {
698
710
"IS" => {
@@ -1669,21 +1681,26 @@ impl Parser {
1669
1681
/// Parse a possibly qualified, possibly quoted identifier, e.g.
1670
1682
/// `foo` or `myschema."table"`
1671
1683
pub fn parse_object_name ( & mut self ) -> Result < ObjectName , ParserError > {
1684
+ let m = self . start ( ) ;
1672
1685
let mut idents = vec ! [ ] ;
1673
1686
loop {
1674
1687
idents. push ( self . parse_identifier ( ) ?) ;
1675
1688
if !self . consume_token ( & Token :: Period ) {
1676
1689
break ;
1677
1690
}
1678
1691
}
1679
- Ok ( ObjectName ( idents) )
1692
+ ret ! ( Ok ( ObjectName ( idents) )
1693
+ => via self . complete( m, SK :: OBJECT_NAME , ..) )
1680
1694
}
1681
1695
1682
1696
/// Parse a simple one-word identifier (possibly quoted, possibly a keyword)
1683
1697
pub fn parse_identifier ( & mut self ) -> Result < Ident , ParserError > {
1698
+ let m = self . start ( ) ;
1684
1699
match self . next_token ( ) {
1685
- Some ( Token :: Word ( w) ) => Ok ( w. to_ident ( ) ) ,
1686
- unexpected => self . expected ( "identifier" , unexpected) ,
1700
+ Some ( Token :: Word ( w) ) => ret ! ( Ok ( w. to_ident( ) )
1701
+ => via self . complete( m, SK :: IDENT , ..) ) ,
1702
+ unexpected => ret ! ( self . expected( "identifier" , unexpected)
1703
+ => via self . complete( m, SK :: ERR , ..) ) ,
1687
1704
}
1688
1705
}
1689
1706
@@ -1750,47 +1767,44 @@ impl Parser {
1750
1767
/// by `ORDER BY`. Unlike some other parse_... methods, this one doesn't
1751
1768
/// expect the initial keyword to be already consumed
1752
1769
pub fn parse_query ( & mut self ) -> Result < Query , ParserError > {
1753
- let ctes = if self . parse_keyword ( "WITH" ) {
1770
+ let m = self . start ( ) ;
1771
+ let ctes = if let Some ( m) = self . start_if ( |p| p. parse_keyword ( "WITH" ) ) {
1754
1772
// TODO: optional RECURSIVE
1755
- self . parse_comma_separated ( Parser :: parse_cte) ?
1773
+ ret ! ( self . parse_comma_separated( Parser :: parse_cte) ?
1774
+ => via self . complete( m, SK :: CTES , ..) )
1756
1775
} else {
1757
1776
vec ! [ ]
1758
1777
} ;
1759
1778
1760
1779
let body = self . parse_query_body ( 0 ) ?;
1761
1780
1762
- let order_by = if self . parse_keywords ( vec ! [ "ORDER" , "BY" ] ) {
1763
- self . parse_comma_separated ( Parser :: parse_order_by_expr) ?
1781
+ let order_by = if let Some ( m) = self . start_if ( |p| p. parse_keywords ( vec ! [ "ORDER" , "BY" ] ) ) {
1782
+ ret ! ( self . parse_comma_separated( Parser :: parse_order_by_expr) ?
1783
+ => via self . complete( m, SK :: ORDER_BY , ..) )
1764
1784
} else {
1765
1785
vec ! [ ]
1766
1786
} ;
1767
1787
1768
- let limit = if self . parse_keyword ( "LIMIT" ) {
1769
- self . parse_limit ( ) ?
1788
+ let limit = if let Some ( _m ) = self . start_if ( |p| p . parse_keyword ( "LIMIT" ) ) {
1789
+ self . parse_limit ( ) ? // TBD
1770
1790
} else {
1771
1791
None
1772
1792
} ;
1773
1793
1774
- let offset = if self . parse_keyword ( "OFFSET" ) {
1775
- Some ( self . parse_offset ( ) ?)
1794
+ let offset = if let Some ( _m ) = self . start_if ( |p| p . parse_keyword ( "OFFSET" ) ) {
1795
+ Some ( self . parse_offset ( ) ?) // TBD
1776
1796
} else {
1777
1797
None
1778
1798
} ;
1779
1799
1780
- let fetch = if self . parse_keyword ( "FETCH" ) {
1781
- Some ( self . parse_fetch ( ) ?)
1800
+ let fetch = if let Some ( _m ) = self . start_if ( |p| p . parse_keyword ( "FETCH" ) ) {
1801
+ Some ( self . parse_fetch ( ) ?) // TBD
1782
1802
} else {
1783
1803
None
1784
1804
} ;
1785
1805
1786
- Ok ( Query {
1787
- ctes,
1788
- body,
1789
- limit,
1790
- order_by,
1791
- offset,
1792
- fetch,
1793
- } )
1806
+ ret ! ( Ok ( Query { ctes, body, limit, order_by, offset, fetch } )
1807
+ => via self . complete( m, SK :: QUERY , ..) )
1794
1808
}
1795
1809
1796
1810
/// Parse a CTE (`alias [( col1, col2, ... )] AS (subquery)`)
@@ -1817,15 +1831,16 @@ impl Parser {
1817
1831
fn parse_query_body ( & mut self , precedence : u8 ) -> Result < SetExpr , ParserError > {
1818
1832
// We parse the expression using a Pratt parser, as in `parse_expr()`.
1819
1833
// Start by parsing a restricted SELECT or a `(subquery)`:
1820
- let mut expr = if self . parse_keyword ( "SELECT" ) {
1821
- SetExpr :: Select ( Box :: new ( self . parse_select ( ) ?) )
1834
+ let mut expr = if let Some ( m) = self . start_if ( |parser| parser. parse_keyword ( "SELECT" ) ) {
1835
+ ret ! ( SetExpr :: Select ( Box :: new( self . parse_select( ) ?) )
1836
+ => via self . complete( m, SK :: SELECT , ..) )
1822
1837
} else if self . consume_token ( & Token :: LParen ) {
1823
1838
// CTEs are not allowed here, but the parser currently accepts them
1824
1839
let subquery = self . parse_query ( ) ?;
1825
1840
self . expect_token ( & Token :: RParen ) ?;
1826
- SetExpr :: Query ( Box :: new ( subquery) )
1841
+ SetExpr :: Query ( Box :: new ( subquery) ) // TBD
1827
1842
} else if self . parse_keyword ( "VALUES" ) {
1828
- SetExpr :: Values ( self . parse_values ( ) ?)
1843
+ SetExpr :: Values ( self . parse_values ( ) ?) // TBD
1829
1844
} else {
1830
1845
return self . expected (
1831
1846
"SELECT, VALUES, or a subquery in the query body" ,
@@ -1850,6 +1865,7 @@ impl Parser {
1850
1865
}
1851
1866
self . next_token ( ) ; // skip past the set operator
1852
1867
expr = SetExpr :: SetOperation {
1868
+ // TBD ret!
1853
1869
left : Box :: new ( expr) ,
1854
1870
op : op. unwrap ( ) ,
1855
1871
all : self . parse_keyword ( "ALL" ) ,
@@ -1880,21 +1896,24 @@ impl Parser {
1880
1896
None
1881
1897
} ;
1882
1898
1883
- let projection = self . parse_comma_separated ( Parser :: parse_select_item) ?;
1899
+ let m = self . start ( ) ;
1900
+ let projection = ret ! ( self . parse_comma_separated( Parser :: parse_select_item) ?
1901
+ => via self . complete( m, SK :: PROJECTION , ..) ) ;
1884
1902
1885
1903
// Note that for keywords to be properly handled here, they need to be
1886
1904
// added to `RESERVED_FOR_COLUMN_ALIAS` / `RESERVED_FOR_TABLE_ALIAS`,
1887
1905
// otherwise they may be parsed as an alias as part of the `projection`
1888
1906
// or `from`.
1889
1907
1890
- let from = if self . parse_keyword ( "FROM" ) {
1891
- self . parse_comma_separated ( Parser :: parse_table_and_joins) ?
1908
+ let from = if let Some ( m) = self . start_if ( |parser| parser. parse_keyword ( "FROM" ) ) {
1909
+ ret ! ( self . parse_comma_separated( Parser :: parse_table_and_joins) ?
1910
+ => via self . complete( m, SK :: FROM , ..) )
1892
1911
} else {
1893
1912
vec ! [ ]
1894
1913
} ;
1895
1914
1896
- let selection = if self . parse_keyword ( "WHERE" ) {
1897
- Some ( self . parse_expr ( ) ?)
1915
+ let selection = if let Some ( m ) = self . start_if ( |parser| parser . parse_keyword ( "WHERE" ) ) {
1916
+ ret ! ( Some ( self . parse_expr( ) ?) => via self . complete ( m , SK :: WHERE , .. ) )
1898
1917
} else {
1899
1918
None
1900
1919
} ;
@@ -2162,12 +2181,14 @@ impl Parser {
2162
2181
fn parse_join_constraint ( & mut self , natural : bool ) -> Result < JoinConstraint , ParserError > {
2163
2182
if natural {
2164
2183
Ok ( JoinConstraint :: Natural )
2165
- } else if self . parse_keyword ( "ON" ) {
2184
+ } else if let Some ( m ) = self . start_if ( |parser| parser . parse_keyword ( "ON" ) ) {
2166
2185
let constraint = self . parse_expr ( ) ?;
2167
- Ok ( JoinConstraint :: On ( constraint) )
2168
- } else if self . parse_keyword ( "USING" ) {
2186
+ ret ! ( Ok ( JoinConstraint :: On ( constraint) )
2187
+ => via self . complete( m, SK :: JoinConstraint__On , ..) )
2188
+ } else if let Some ( m) = self . start_if ( |parser| parser. parse_keyword ( "USING" ) ) {
2169
2189
let columns = self . parse_parenthesized_column_list ( Mandatory ) ?;
2170
- Ok ( JoinConstraint :: Using ( columns) )
2190
+ ret ! ( Ok ( JoinConstraint :: Using ( columns) )
2191
+ => via self . complete( m, SK :: JoinConstraint__Using , ..) )
2171
2192
} else {
2172
2193
self . expected ( "ON, or USING after JOIN" , self . peek_token ( ) )
2173
2194
}
@@ -2222,17 +2243,22 @@ impl Parser {
2222
2243
2223
2244
/// Parse a comma-delimited list of projections after SELECT
2224
2245
pub fn parse_select_item ( & mut self ) -> Result < SelectItem , ParserError > {
2246
+ let m = self . start ( ) ;
2225
2247
let expr = self . parse_expr ( ) ?;
2226
2248
if let Expr :: Wildcard = expr {
2227
- Ok ( SelectItem :: Wildcard )
2249
+ ret ! ( Ok ( SelectItem :: Wildcard )
2250
+ => via self . complete( m, SK :: SELECT_ITEM_WILDCARD , ..) )
2228
2251
} else if let Expr :: QualifiedWildcard ( prefix) = expr {
2229
- Ok ( SelectItem :: QualifiedWildcard ( ObjectName ( prefix) ) )
2252
+ ret ! ( Ok ( SelectItem :: QualifiedWildcard ( ObjectName ( prefix) ) )
2253
+ => via self . complete( m, SK :: SELECT_ITEM_QWILDCARD , ..) )
2230
2254
} else {
2231
2255
// `expr` is a regular SQL expression and can be followed by an alias
2232
2256
if let Some ( alias) = self . parse_optional_alias ( keywords:: RESERVED_FOR_COLUMN_ALIAS ) ? {
2233
- Ok ( SelectItem :: ExprWithAlias { expr, alias } )
2257
+ ret ! ( Ok ( SelectItem :: ExprWithAlias { expr, alias } )
2258
+ => via self . complete( m, SK :: SELECT_ITEM_EXPR_WITH_ALIAS , ..) )
2234
2259
} else {
2235
- Ok ( SelectItem :: UnnamedExpr ( expr) )
2260
+ ret ! ( Ok ( SelectItem :: UnnamedExpr ( expr) )
2261
+ => via self . complete( m, SK :: SELECT_ITEM_UNNAMED , ..) )
2236
2262
}
2237
2263
}
2238
2264
}
0 commit comments