Skip to content

Commit 0763f52

Browse files
committed
extract parse_comma_separated0
1 parent 21cdef3 commit 0763f52

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/parser/mod.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2939,7 +2939,7 @@ impl<'a> Parser<'a> {
29392939
Expr::InList {
29402940
expr: Box::new(expr),
29412941
list: if self.dialect.supports_in_empty_list() {
2942-
self.parse_comma_separated0(Parser::parse_expr)?
2942+
self.parse_comma_separated0(Parser::parse_expr, self.options.trailing_commas, Token::RParen)?
29432943
} else {
29442944
self.parse_comma_separated(Parser::parse_expr)?
29452945
},
@@ -3481,17 +3481,18 @@ impl<'a> Parser<'a> {
34813481
}
34823482

34833483
/// Parse a comma-separated list of 0+ items accepted by `F`
3484-
pub fn parse_comma_separated0<T, F>(&mut self, f: F) -> Result<Vec<T>, ParserError>
3484+
/// - [trailing_commas]: support trailing_commas or not
3485+
/// - [end_token]: expected end token for the closure (e.g. [Token::RParen], [Token::RBrace] ...)
3486+
pub fn parse_comma_separated0<T, F>(&mut self, f: F, trailing_commas: bool, end_token: Token) -> Result<Vec<T>, ParserError>
34853487
where
34863488
F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
34873489
{
3488-
// ()
3489-
if matches!(self.peek_token().token, Token::RParen) {
3490+
if self.peek_token().token == end_token {
34903491
return Ok(vec![]);
34913492
}
3492-
// (,)
3493-
if self.options.trailing_commas
3494-
&& matches!(self.peek_tokens(), [Token::Comma, Token::RParen])
3493+
3494+
if trailing_commas
3495+
&& self.peek_tokens() == [Token::Comma, end_token]
34953496
{
34963497
let _ = self.consume_token(&Token::Comma);
34973498
return Ok(vec![]);
@@ -4061,7 +4062,7 @@ impl<'a> Parser<'a> {
40614062
})
40624063
};
40634064
self.expect_token(&Token::LParen)?;
4064-
let args = self.parse_comma_separated0(parse_function_param)?;
4065+
let args = self.parse_comma_separated0(parse_function_param, self.options.trailing_commas, Token::RParen)?;
40654066
self.expect_token(&Token::RParen)?;
40664067

40674068
let return_type = if self.parse_keyword(Keyword::RETURNS) {
@@ -10715,7 +10716,7 @@ impl<'a> Parser<'a> {
1071510716
}
1071610717

1071710718
if self.consume_token(&Token::LParen) {
10718-
let interpolations = self.parse_comma_separated0(|p| p.parse_interpolation())?;
10719+
let interpolations = self.parse_comma_separated0(|p| p.parse_interpolation(), self.options.trailing_commas, Token::RParen)?;
1071910720
self.expect_token(&Token::RParen)?;
1072010721
// INTERPOLATE () and INTERPOLATE ( ... ) variants
1072110722
return Ok(Some(Interpolate {

0 commit comments

Comments
 (0)