Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: allow parenthesizing in two type locations #2388

Merged
merged 2 commits into from Aug 21, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions crates/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,10 +942,11 @@ fn type_expression() -> impl NoirParser<UnresolvedTypeExpression> {
expression_with_precedence(
Precedence::lowest_type_precedence(),
nothing(),
jfecher marked this conversation as resolved.
Show resolved Hide resolved
expr,
expr.clone(),
true,
false,
)
.or(parenthesized(expr))
jfecher marked this conversation as resolved.
Show resolved Hide resolved
})
.labelled(ParsingRuleLabel::TypeExpression)
.try_map(UnresolvedTypeExpression::from_expr)
Expand Down Expand Up @@ -1056,13 +1057,13 @@ where
P2: ExprParser + 'a,
{
if precedence == Precedence::Highest {
if is_type_expression {
type_expression_term(expr_parser).boxed().labelled(ParsingRuleLabel::Term)
let expr_parser = if is_type_expression {
type_expression_term(expr_parser).boxed()
} else {
term(expr_parser, expr_no_constructors, allow_constructors)
.boxed()
.labelled(ParsingRuleLabel::Term)
}
term(expr_parser, expr_no_constructors, allow_constructors).boxed()
};

expr_parser.clone().or(parenthesized(expr_parser)).boxed().labelled(ParsingRuleLabel::Term)
jfecher marked this conversation as resolved.
Show resolved Hide resolved
} else {
let next_precedence =
if is_type_expression { precedence.next_type_precedence() } else { precedence.next() };
Expand Down Expand Up @@ -1645,6 +1646,11 @@ mod test {
);
}

#[test]
fn parse_type_expression() {
parse_all(type_expression(), vec!["(123)", "123", "((1 + 1))", "(1 + (1))"]);
}

#[test]
fn parse_array_sugar() {
let valid = vec!["[0;7]", "[(1, 2); 4]", "[0;Four]", "[2;1+3-a]"];
Expand Down