From b2c42febafd2684734f8ccc945dad44d535f8558 Mon Sep 17 00:00:00 2001 From: rvcas Date: Tue, 11 Jun 2024 12:49:19 -0400 Subject: [PATCH] chore: move if_branch to helper parser --- crates/aiken-lang/src/parser/expr/if_else.rs | 35 +++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/crates/aiken-lang/src/parser/expr/if_else.rs b/crates/aiken-lang/src/parser/expr/if_else.rs index 9a90ee7d..33acfb81 100644 --- a/crates/aiken-lang/src/parser/expr/if_else.rs +++ b/crates/aiken-lang/src/parser/expr/if_else.rs @@ -13,29 +13,11 @@ pub fn parser<'a>( expression: Recursive<'a, Token, UntypedExpr, ParseError>, ) -> impl Parser + 'a { just(Token::If) - .ignore_then( - expression - .clone() - .then(block(sequence.clone())) - .map_with_span(|(condition, body), span| ast::IfBranch { - condition, - body, - location: span, - }), - ) + .ignore_then(if_branch(sequence.clone(), expression.clone())) .then( just(Token::Else) .ignore_then(just(Token::If)) - .ignore_then( - expression - .clone() - .then(block(sequence.clone())) - .map_with_span(|(condition, body), span| ast::IfBranch { - condition, - body, - location: span, - }), - ) + .ignore_then(if_branch(sequence.clone(), expression)) .repeated(), ) .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 + 'a { + expression + .then(block(sequence)) + .map_with_span(|(condition, body), span| ast::IfBranch { + condition, + body, + location: span, + }) +} + #[cfg(test)] mod tests { use crate::assert_expr;