Merge pull request #19895 from github/tausbn/python-fix-match-as-identifier

Python: Allow use of `match` as an identifier
This commit is contained in:
Taus
2025-06-30 16:24:23 +02:00
committed by GitHub
8 changed files with 48890 additions and 47125 deletions

View File

@@ -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
]

View File

@@ -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.

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),

View File

@@ -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": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "expression_list"
},
"named": true,
"value": "tuple"
}
]
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "cases",
"content": {
"type": "SYMBOL",
"name": "cases"
}
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "cases",
"content": {
"type": "SYMBOL",
"name": "cases"
}
}
]
]
}
},
"cases": {
"type": "REPEAT1",
@@ -6675,6 +6679,10 @@
[
"with_item",
"_collection_elements"
],
[
"match_statement",
"primary_expression"
]
],
"precedences": [],

File diff suppressed because it is too large Load Diff

View File

@@ -14,6 +14,7 @@ extern "C" {
#include <string.h>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4101)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
@@ -278,7 +279,7 @@ static inline void _array__splice(Array *self, size_t element_size,
#define _compare_int(a, b) ((int)*(a) - (int)(b))
#ifdef _MSC_VER
#pragma warning(default : 4101)
#pragma warning(pop)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif

View File

@@ -123,6 +123,7 @@ struct TSLanguage {
unsigned (*serialize)(void *, char *);
void (*deserialize)(void *, const char *, unsigned);
} external_scanner;
const TSStateId *primary_state_ids;
};
/*

View File

@@ -0,0 +1,5 @@
---
category: fix
---
- The Python parser is now able to correctly parse expressions such as `match[1]` and `match()` where `match` is not used as a keyword.