Skip to content

Commit 4cfbaed

Browse files
committed
add switch pattern matching changes
1 parent 0b3f9cf commit 4cfbaed

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

grammar.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,13 @@ module.exports = grammar({
482482
),
483483

484484
switch_label: $ => choice(
485-
seq('case', choice($.pattern, commaSep1($.expression))),
485+
seq('case',
486+
choice(
487+
$.pattern,
488+
commaSep1($.expression)
489+
),
490+
optional($.guard)
491+
),
486492
'default'
487493
),
488494

@@ -498,6 +504,8 @@ module.exports = grammar({
498504
choice($.identifier, $._reserved_identifier)
499505
),
500506

507+
guard: $ => seq('when', $.expression),
508+
501509
// Statements
502510

503511
statement: $ => choice(

test/corpus/expressions.txt

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,3 +1676,98 @@ STR."We can have \{many(of)} \{them.in} a \{row}";
16761676
(string_fragment)
16771677
(string_interpolation
16781678
(identifier))))))
1679+
1680+
================================================================================
1681+
switch with qualified enum constant
1682+
================================================================================
1683+
1684+
switch (c) {
1685+
case Coin.HEADS -> {
1686+
}
1687+
case Coin.TAILS -> {
1688+
}
1689+
}
1690+
1691+
--------------------------------------------------------------------------------
1692+
1693+
(program
1694+
(switch_expression
1695+
(parenthesized_expression
1696+
(identifier))
1697+
(switch_block
1698+
(switch_rule
1699+
(switch_label
1700+
(field_access
1701+
(identifier)
1702+
(identifier)))
1703+
(block))
1704+
(switch_rule
1705+
(switch_label
1706+
(field_access
1707+
(identifier)
1708+
(identifier)))
1709+
(block)))))
1710+
1711+
================================================================================
1712+
switch with guarded case
1713+
================================================================================
1714+
1715+
switch (c) {
1716+
case 1 when true -> {
1717+
}
1718+
case 2 when f("hi") == "foo" -> {
1719+
}
1720+
}
1721+
1722+
--------------------------------------------------------------------------------
1723+
1724+
(program
1725+
(switch_expression
1726+
(parenthesized_expression
1727+
(identifier))
1728+
(switch_block
1729+
(switch_rule
1730+
(switch_label
1731+
(decimal_integer_literal)
1732+
(guard
1733+
(true)))
1734+
(block))
1735+
(switch_rule
1736+
(switch_label
1737+
(decimal_integer_literal)
1738+
(guard
1739+
(binary_expression
1740+
(method_invocation
1741+
(identifier)
1742+
(argument_list
1743+
(string_literal
1744+
(string_fragment))))
1745+
(string_literal
1746+
(string_fragment)))))
1747+
(block)))))
1748+
1749+
================================================================================
1750+
switch with complex type
1751+
================================================================================
1752+
1753+
switch (c) {
1754+
case int[] a -> {
1755+
}
1756+
}
1757+
1758+
--------------------------------------------------------------------------------
1759+
1760+
(program
1761+
(switch_expression
1762+
(parenthesized_expression
1763+
(identifier))
1764+
(switch_block
1765+
(switch_rule
1766+
(switch_label
1767+
(pattern
1768+
(type_pattern
1769+
(array_type
1770+
(integral_type)
1771+
(dimensions))
1772+
(identifier))))
1773+
(block)))))

0 commit comments

Comments
 (0)