Python: Allow use of match as an identifier

This previously only worked in certain circumstances. In particular,
assignments such as `match[1] = ...` or even just `match[1]` would fail
to parse correctly.

Fixing this turned out to be less trivial than anticipated. Consider the
fact that
```
match [1]: case (...)
```
can either look the start of a `match` statement, or it could be a type
ascription, ascribing the value of `case(...)` (a call) to the item at
index 1 of `match`.

To fix this, then, we give `match` the identifier and `match` the
statement the same precendence in the grammar, and additionally also
mark a conflict between `match_statement` and `primary_expression`. This
causes the conflict to be resolved dynamically, and seems to do the
right thing in all cases.
This commit is contained in:
Taus
2025-06-26 15:33:00 +00:00
parent ec09d36667
commit e04821e9e3
3 changed files with 160 additions and 2 deletions

View File

@@ -36,6 +36,7 @@ module.exports = grammar({
[$.tuple, $.tuple_pattern],
[$.list, $.list_pattern],
[$.with_item, $._collection_elements],
[$.match_statement, $.primary_expression],
],
supertypes: $ => [
@@ -349,7 +350,7 @@ module.exports = grammar({
))
)),
match_statement: $ => seq(
match_statement: $ => prec(-3, seq(
'match',
field('subject',
choice(
@@ -359,7 +360,7 @@ module.exports = grammar({
),
':',
field('cases', $.cases)
),
)),
cases: $ => repeat1($.case_block),