Skip to content

Commit

Permalink
fix: parse block statement independently of block expression in state…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
asterite committed Jul 30, 2024
1 parent f069bc2 commit 5f885b2
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 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()),
block_statement(statement.clone()),
for_loop(expr_no_constructors.clone(), statement.clone()),
break_statement(),
continue_statement(),
Expand Down Expand Up @@ -932,6 +933,15 @@ where
})
}

fn block_statement<'a, S>(statement: S) -> impl NoirParser<StatementKind> + 'a
where
S: NoirParser<StatementKind> + 'a,
{
block(statement).map_with_span(|block, span| {
StatementKind::Expression(Expression::new(ExpressionKind::Block(block), span))
})
}

fn for_loop<'a, P, S>(expr_no_constructors: P, statement: S) -> impl NoirParser<StatementKind> + 'a
where
P: ExprParser + 'a,
Expand Down Expand Up @@ -1629,4 +1639,16 @@ mod test {
let failing = vec!["quote {}}", "quote a", "quote { { { } } } }"];
parse_all_failing(quote(), failing);
}

#[test]
fn test_parses_block_statement_not_infix_expression() {
let src = r#"
{
{}
-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 5f885b2

Please sign in to comment.