Merge pull request #3629 from rtfeldman/when-binop

Allow `when` after infix operators
This commit is contained in:
Richard Feldman 2022-07-25 08:59:54 -04:00 committed by GitHub
commit 10001876bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 9 deletions

View file

@ -189,7 +189,7 @@ fn record_field_access<'a>() -> impl Parser<'a, &'a str, EExpr<'a>> {
/// In some contexts we want to parse the `_` as an expression, so it can then be turned into a
/// pattern later
fn parse_loc_term_or_underscore_or_if<'a>(
fn parse_loc_term_or_underscore_or_conditional<'a>(
min_indent: u32,
options: ExprParseOptions,
arena: &'a Bump,
@ -198,6 +198,10 @@ fn parse_loc_term_or_underscore_or_if<'a>(
one_of!(
loc_expr_in_parens_etc_help(min_indent),
loc!(specialize(EExpr::If, if_expr_help(min_indent, options))),
loc!(specialize(
EExpr::When,
when::expr_help(min_indent, options)
)),
loc!(specialize(EExpr::Str, string_literal_help())),
loc!(specialize(EExpr::SingleQuote, single_quote_literal_help())),
loc!(specialize(EExpr::Number, positive_number_literal_help())),
@ -304,7 +308,9 @@ fn loc_possibly_negative_or_negated_term<'a>(
Expr::UnaryOp(arena.alloc(loc_expr), Loc::at(loc_op.region, UnaryOp::Not))
}
)),
|arena, state| { parse_loc_term_or_underscore_or_if(min_indent, options, arena, state) }
|arena, state| {
parse_loc_term_or_underscore_or_conditional(min_indent, options, arena, state)
}
]
}

View file

@ -0,0 +1,54 @@
BinOps(
[
(
@0-1 Num(
"1",
),
@2-3 Plus,
),
],
@8-53 SpaceBefore(
When(
@13-16 Tag(
"Foo",
),
[
WhenBranch {
patterns: [
@28-31 SpaceBefore(
Tag(
"Foo",
),
[
Newline,
],
),
],
value: @35-36 Num(
"2",
),
guard: None,
},
WhenBranch {
patterns: [
@45-48 SpaceBefore(
Tag(
"Bar",
),
[
Newline,
],
),
],
value: @52-53 Num(
"3",
),
guard: None,
},
],
),
[
Newline,
],
),
)

View file

@ -0,0 +1,4 @@
1 +
when Foo is
Foo -> 2
Bar -> 3

View file

@ -229,6 +229,7 @@ mod test_parse {
pass/parse_as_ann.expr,
pass/pattern_with_space_in_parens.expr, // https://github.com/rtfeldman/roc/issues/929
pass/plus_if.expr,
pass/plus_when.expr,
pass/pos_inf_float.expr,
pass/positive_float.expr,
pass/positive_int.expr,

View file

@ -358,7 +358,7 @@ fn to_expr_report<'a>(
Node::WhenCondition | Node::WhenBranch | Node::WhenIfGuard => (
pos,
alloc.concat([
alloc.text("an "),
alloc.text("a "),
alloc.keyword("when"),
alloc.text(" expression"),
]),

View file

@ -4809,14 +4809,10 @@ mod test_reporting {
@r###"
MISSING EXPRESSION tmp/pattern_binds_keyword/Test.roc
I am partway through parsing a definition, but I got stuck here:
I am partway through parsing a `when` expression, but I got stuck here:
1 app "test" provides [main] to "./platform"
2
3 main =
4 when Just 4 is
5 Just when ->
^
^
I was expecting to see an expression like 42 or "hello".
"###