Skip to content

Commit

Permalink
Where clauses go on switch patterns, not cases
Browse files Browse the repository at this point in the history
The grammar erroneously had it that a switch _case_ would get a single
`where` clause, disallowing something like:

```
case .foo(x) where x.count() == 0, .bar(x):
```

Allowing `where`-clauses per pattern is strictly more flexible.
  • Loading branch information
alex-pinkus committed Nov 28, 2021
1 parent fa831a7 commit c2bbbc5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion corpus/statements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ Weird switch statements

switch something {
case let .pattern2(_, bound): fallthrough
case .ident where someCondition: fallthrough
case .ident where someCondition, ident2: fallthrough
@unknown default: return "Goodbye"
}

Expand All @@ -231,6 +231,7 @@ case let .isExecutable(path?):
(switch_entry
(switch_pattern (simple_identifier))
(simple_identifier)
(switch_pattern (simple_identifier))
(statements (simple_identifier)))
(switch_entry
(modifiers (attribute (user_type (type_identifier))))
Expand Down
10 changes: 8 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,10 +765,16 @@ module.exports = grammar({
seq(
optional($.modifiers),
choice(
seq("case", $.switch_pattern, repeat(seq(",", $.switch_pattern))),
seq(
"case",
seq(
$.switch_pattern,
optional(seq($._where_keyword, $._expression))
),
repeat(seq(",", $.switch_pattern))
),
$.default_keyword
),
optional(seq($._where_keyword, $._expression)),
":",
$.statements,
optional("fallthrough")
Expand Down

0 comments on commit c2bbbc5

Please sign in to comment.