Skip to content

Commit

Permalink
fix: parse if statement independently of if expression in statement
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jul 30, 2024
1 parent 5f885b2 commit 9a954fd
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ where
assertion::assertion_eq(expr_parser.clone()),
declaration(expr_parser.clone()),
assignment(expr_parser.clone()),
if_statement(expr_no_constructors.clone(), statement.clone()),
block_statement(statement.clone()),
for_loop(expr_no_constructors.clone(), statement.clone()),
break_statement(),
Expand Down Expand Up @@ -933,6 +934,19 @@ where
})
}

fn if_statement<'a, P, S>(
expr_no_constructors: P,
statement: S,
) -> impl NoirParser<StatementKind> + 'a
where
P: ExprParser + 'a,
S: NoirParser<StatementKind> + 'a,
{
if_expr(expr_no_constructors, statement).map_with_span(|expression_kind, span| {
StatementKind::Expression(Expression::new(expression_kind, span))
})
}

fn block_statement<'a, S>(statement: S) -> impl NoirParser<StatementKind> + 'a
where
S: NoirParser<StatementKind> + 'a,
Expand Down Expand Up @@ -1651,4 +1665,16 @@ mod test {
let block_expr = block_expr.expect("Failed to parse module");
assert_eq!(block_expr.statements.len(), 2);
}

#[test]
fn test_parses_if_statement_not_infix_expression() {
let src = r#"
{
if 1 { 2 } else { 3 }
-1
}"#;
let (block_expr, _) = parse_recover(block(fresh_statement()), src);
let block_expr = block_expr.expect("Failed to parse module");
assert_eq!(block_expr.statements.len(), 2);
}
}

0 comments on commit 9a954fd

Please sign in to comment.