chore: move if_branch to helper parser

This commit is contained in:
rvcas 2024-06-11 12:49:19 -04:00 committed by Lucas
parent d99c014bf7
commit b2c42febaf
1 changed files with 15 additions and 20 deletions

View File

@ -13,29 +13,11 @@ pub fn parser<'a>(
expression: Recursive<'a, Token, UntypedExpr, ParseError>, expression: Recursive<'a, Token, UntypedExpr, ParseError>,
) -> impl Parser<Token, UntypedExpr, Error = ParseError> + 'a { ) -> impl Parser<Token, UntypedExpr, Error = ParseError> + 'a {
just(Token::If) just(Token::If)
.ignore_then( .ignore_then(if_branch(sequence.clone(), expression.clone()))
expression
.clone()
.then(block(sequence.clone()))
.map_with_span(|(condition, body), span| ast::IfBranch {
condition,
body,
location: span,
}),
)
.then( .then(
just(Token::Else) just(Token::Else)
.ignore_then(just(Token::If)) .ignore_then(just(Token::If))
.ignore_then( .ignore_then(if_branch(sequence.clone(), expression))
expression
.clone()
.then(block(sequence.clone()))
.map_with_span(|(condition, body), span| ast::IfBranch {
condition,
body,
location: span,
}),
)
.repeated(), .repeated(),
) )
.then_ignore(just(Token::Else)) .then_ignore(just(Token::Else))
@ -53,6 +35,19 @@ pub fn parser<'a>(
}) })
} }
fn if_branch<'a>(
sequence: Recursive<'a, Token, UntypedExpr, ParseError>,
expression: Recursive<'a, Token, UntypedExpr, ParseError>,
) -> impl Parser<Token, ast::UntypedIfBranch, Error = ParseError> + 'a {
expression
.then(block(sequence))
.map_with_span(|(condition, body), span| ast::IfBranch {
condition,
body,
location: span,
})
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::assert_expr; use crate::assert_expr;