mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
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:
@@ -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),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user