Skip to content

Python: Allow use of match as an identifier #19895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions python/extractor/tests/parser/soft_keywords_new.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
Module: [1, 0] - [21, 0]
body: [
Expr: [1, 0] - [1, 8]
value:
Subscript: [1, 0] - [1, 8]
value:
Name: [1, 0] - [1, 5]
variable: Variable('match', None)
ctx: Load
index:
Num: [1, 6] - [1, 7]
n: 1
text: '1'
ctx: Load
Assign: [2, 0] - [2, 12]
targets: [
Subscript: [2, 0] - [2, 8]
value:
Name: [2, 0] - [2, 5]
variable: Variable('match', None)
ctx: Load
index:
Num: [2, 6] - [2, 7]
n: 2
text: '2'
ctx: Store
]
value:
Num: [2, 11] - [2, 12]
n: 3
text: '3'
Assign: [4, 0] - [4, 13]
targets: [
Attribute: [4, 0] - [4, 9]
value:
Name: [4, 0] - [4, 5]
variable: Variable('match', None)
ctx: Load
attr: 'foo'
ctx: Store
]
value:
Num: [4, 12] - [4, 13]
n: 4
text: '4'
Expr: [6, 0] - [6, 7]
value:
Call: [6, 0] - [6, 7]
func:
Name: [6, 0] - [6, 5]
variable: Variable('match', None)
ctx: Load
positional_args: []
named_args: []
AnnAssign: [8, 0] - [8, 15]
value: None
annotation:
Name: [8, 11] - [8, 15]
variable: Variable('case', None)
ctx: Load
target:
Subscript: [8, 0] - [8, 8]
value:
Name: [8, 0] - [8, 5]
variable: Variable('match', None)
ctx: Load
index:
Num: [8, 6] - [8, 7]
n: 5
text: '5'
ctx: Store
Match: [12, 0] - [14, 12]
subject:
List: [12, 6] - [12, 9]
elts: [
Num: [12, 7] - [12, 8]
n: 6
text: '6'
]
ctx: Load
cases: [
Case: [13, 4] - [14, 12]
pattern:
MatchLiteralPattern: [13, 9] - [13, 10]
literal:
Num: [13, 9] - [13, 10]
n: 7
text: '7'
guard: None
body: [
Pass: [14, 8] - [14, 12]
]
]
Print: [17, 0] - [17, 19]
dest:
Num: [17, 9] - [17, 10]
n: 8
text: '8'
values: [
Str: [17, 12] - [17, 19]
s: 'hello'
prefix: '"'
implicitly_concatenated_parts: None
]
nl: True
Expr: [18, 0] - [18, 19]
value:
Tuple: [18, 0] - [18, 19]
elts: [
BinOp: [18, 0] - [18, 10]
left:
Name: [18, 0] - [18, 5]
variable: Variable('pront', None)
ctx: Load
op: RShift
right:
Num: [18, 9] - [18, 10]
n: 9
text: '9'
Str: [18, 12] - [18, 19]
s: 'world'
prefix: '"'
implicitly_concatenated_parts: None
]
ctx: Load
Expr: [20, 0] - [20, 10]
value:
Await: [20, 0] - [20, 10]
value:
List: [20, 6] - [20, 10]
elts: [
Num: [20, 7] - [20, 9]
n: 10
text: '10'
]
ctx: Load
]
20 changes: 20 additions & 0 deletions python/extractor/tests/parser/soft_keywords_new.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
match[1]
match[2] = 3

match.foo = 4

match()

match[5] : case


# match used "properly"
match [6]:
case 7:
pass


print >> 8, "hello" # Python 2-style print
pront >> 9, "world" # How this would be interpreted in Python 3

await [10] # In Python 2 this would be an indexing operation, but it's more likely to be an await.
5 changes: 3 additions & 2 deletions python/extractor/tsg-python/tsp/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = grammar({
[$.tuple, $.tuple_pattern],
[$.list, $.list_pattern],
[$.with_item, $._collection_elements],
[$.match_statement, $.primary_expression],
],

supertypes: $ => [
Expand Down Expand Up @@ -349,7 +350,7 @@ module.exports = grammar({
))
)),

match_statement: $ => seq(
match_statement: $ => prec(-3, seq(
'match',
field('subject',
choice(
Expand All @@ -359,7 +360,7 @@ module.exports = grammar({
),
':',
field('cases', $.cases)
),
)),

cases: $ => repeat1($.case_block),

Expand Down
84 changes: 46 additions & 38 deletions python/extractor/tsg-python/tsp/src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1407,47 +1407,51 @@
}
},
"match_statement": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "match"
},
{
"type": "FIELD",
"name": "subject",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "ALIAS",
"content": {
"type": "PREC",
"value": -3,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "match"
},
{
"type": "FIELD",
"name": "subject",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "expression_list"
"name": "expression"
},
"named": true,
"value": "tuple"
}
]
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "cases",
"content": {
"type": "SYMBOL",
"name": "cases"
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "expression_list"
},
"named": true,
"value": "tuple"
}
]
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "cases",
"content": {
"type": "SYMBOL",
"name": "cases"
}
}
}
]
]
}
},
"cases": {
"type": "REPEAT1",
Expand Down Expand Up @@ -6675,6 +6679,10 @@
[
"with_item",
"_collection_elements"
],
[
"match_statement",
"primary_expression"
]
],
"precedences": [],
Expand Down
Loading