@@ -1853,17 +1853,9 @@ impl<'a> Parser<'a> {
1853
1853
/// Parses an array expression `[ex1, ex2, ..]`
1854
1854
/// if `named` is `true`, came from an expression like `ARRAY[ex1, ex2]`
1855
1855
pub fn parse_array_expr ( & mut self , named : bool ) -> Result < Expr , ParserError > {
1856
- if self . peek_token ( ) . token == Token :: RBracket {
1857
- let _ = self . next_token ( ) ; // consume ]
1858
- Ok ( Expr :: Array ( Array {
1859
- elem : vec ! [ ] ,
1860
- named,
1861
- } ) )
1862
- } else {
1863
- let exprs = self . parse_comma_separated ( Parser :: parse_expr) ?;
1864
- self . expect_token ( & Token :: RBracket ) ?;
1865
- Ok ( Expr :: Array ( Array { elem : exprs, named } ) )
1866
- }
1856
+ let exprs = self . parse_comma_separated0 ( Parser :: parse_expr, Token :: RBracket ) ?;
1857
+ self . expect_token ( & Token :: RBracket ) ?;
1858
+ Ok ( Expr :: Array ( Array { elem : exprs, named } ) )
1867
1859
}
1868
1860
1869
1861
pub fn parse_listagg_on_overflow ( & mut self ) -> Result < Option < ListAggOnOverflow > , ParserError > {
@@ -2356,11 +2348,8 @@ impl<'a> Parser<'a> {
2356
2348
/// [map]: https://duckdb.org/docs/sql/data_types/map.html#creating-maps
2357
2349
fn parse_duckdb_map_literal ( & mut self ) -> Result < Expr , ParserError > {
2358
2350
self . expect_token ( & Token :: LBrace ) ?;
2359
-
2360
- let fields = self . parse_comma_separated ( Self :: parse_duckdb_map_field) ?;
2361
-
2351
+ let fields = self . parse_comma_separated0 ( Self :: parse_duckdb_map_field, Token :: RBrace ) ?;
2362
2352
self . expect_token ( & Token :: RBrace ) ?;
2363
-
2364
2353
Ok ( Expr :: Map ( Map { entries : fields } ) )
2365
2354
}
2366
2355
@@ -2942,7 +2931,7 @@ impl<'a> Parser<'a> {
2942
2931
Expr :: InList {
2943
2932
expr : Box :: new ( expr) ,
2944
2933
list : if self . dialect . supports_in_empty_list ( ) {
2945
- self . parse_comma_separated0 ( Parser :: parse_expr) ?
2934
+ self . parse_comma_separated0 ( Parser :: parse_expr, Token :: RParen ) ?
2946
2935
} else {
2947
2936
self . parse_comma_separated ( Parser :: parse_expr) ?
2948
2937
} ,
@@ -3493,18 +3482,20 @@ impl<'a> Parser<'a> {
3493
3482
}
3494
3483
3495
3484
/// Parse a comma-separated list of 0+ items accepted by `F`
3496
- pub fn parse_comma_separated0 < T , F > ( & mut self , f : F ) -> Result < Vec < T > , ParserError >
3485
+ /// * `end_token` - expected end token for the closure (e.g. [Token::RParen], [Token::RBrace] ...)
3486
+ pub fn parse_comma_separated0 < T , F > (
3487
+ & mut self ,
3488
+ f : F ,
3489
+ end_token : Token ,
3490
+ ) -> Result < Vec < T > , ParserError >
3497
3491
where
3498
3492
F : FnMut ( & mut Parser < ' a > ) -> Result < T , ParserError > ,
3499
3493
{
3500
- // ()
3501
- if matches ! ( self . peek_token( ) . token, Token :: RParen ) {
3494
+ if self . peek_token ( ) . token == end_token {
3502
3495
return Ok ( vec ! [ ] ) ;
3503
3496
}
3504
- // (,)
3505
- if self . options . trailing_commas
3506
- && matches ! ( self . peek_tokens( ) , [ Token :: Comma , Token :: RParen ] )
3507
- {
3497
+
3498
+ if self . options . trailing_commas && self . peek_tokens ( ) == [ Token :: Comma , end_token] {
3508
3499
let _ = self . consume_token ( & Token :: Comma ) ;
3509
3500
return Ok ( vec ! [ ] ) ;
3510
3501
}
@@ -4073,7 +4064,7 @@ impl<'a> Parser<'a> {
4073
4064
} )
4074
4065
} ;
4075
4066
self . expect_token ( & Token :: LParen ) ?;
4076
- let args = self . parse_comma_separated0 ( parse_function_param) ?;
4067
+ let args = self . parse_comma_separated0 ( parse_function_param, Token :: RParen ) ?;
4077
4068
self . expect_token ( & Token :: RParen ) ?;
4078
4069
4079
4070
let return_type = if self . parse_keyword ( Keyword :: RETURNS ) {
@@ -10742,7 +10733,8 @@ impl<'a> Parser<'a> {
10742
10733
}
10743
10734
10744
10735
if self . consume_token ( & Token :: LParen ) {
10745
- let interpolations = self . parse_comma_separated0 ( |p| p. parse_interpolation ( ) ) ?;
10736
+ let interpolations =
10737
+ self . parse_comma_separated0 ( |p| p. parse_interpolation ( ) , Token :: RParen ) ?;
10746
10738
self . expect_token ( & Token :: RParen ) ?;
10747
10739
// INTERPOLATE () and INTERPOLATE ( ... ) variants
10748
10740
return Ok ( Some ( Interpolate {
0 commit comments