Merge pull request #18702 from github/tausbn/python-allow-comments-in-subscripts

Python: Allow comments in subscripts
This commit is contained in:
yoff
2025-02-06 23:31:29 +01:00
committed by GitHub
7 changed files with 44353 additions and 45734 deletions

View File

@@ -0,0 +1,21 @@
a[b]
c[d,e]
c1[d1,]
# And now with many comments
e[
# comment1
f
# comment2
]
g[
# comment3
h,
# comment4
i
# comment5
]

View File

@@ -22,7 +22,7 @@
(assignment !type) @assign
{ let @assign.node = (ast-node @assign "Assign") }
[ (expression_list) (tuple) (tuple_pattern) (pattern_list) ] @tuple
[ (expression_list) (tuple) (tuple_pattern) (pattern_list) (index_expression_list) ] @tuple
{ let @tuple.node = (ast-node @tuple "Tuple") }
(list_pattern) @list
@@ -2543,66 +2543,16 @@
(subscript
value: (_) @value
subscript: (_) @index
) @subscript
{
attr (@subscript.node) value = @value.node
attr (@value.node) ctx = "load"
}
; Single subscript
(subscript
value: (_)
.
subscript: (_) @index
.
) @subscript
{
attr (@subscript.node) index = @index.node
attr (@index.node) ctx = "load"
}
; For expressions of the form `a[b, c]` we must explicitly synthesize an internal tuple node
; We do this and also hook it up:
(subscript
value: (_)
.
subscript: (_) @first
.
subscript: (_)
) @subscript
{
let @subscript.tuple = (ast-node @first "Tuple")
attr (@subscript.tuple) ctx = "load"
attr (@subscript.node) index = @subscript.tuple
edge @subscript.tuple -> @first.node
attr (@subscript.tuple -> @first.node) elts = (named-child-index @first)
attr (@first.node) ctx = "load"
}
(subscript
value: (_)
.
subscript: (_)
subscript: (_) @elt
) @subscript
{
edge @subscript.tuple -> @elt.node
attr (@subscript.tuple -> @elt.node) elts = (named-child-index @elt)
attr (@elt.node) ctx = "load"
}
; Set the end position correctly
(subscript
value: (_)
.
subscript: (_)
subscript: (_) @last
.
) @subscript
{
attr (@subscript.tuple) _location_end = (location-end @last)
}
@@ -3448,9 +3398,12 @@
; Left hand side of an assignment such as `[foo, bar] = ...`
(list_pattern element: (_) @elt) @parent
; An unadorned tuple (such as in `x = y, z`)
; An unadorned tuple such as in `x = y, z`
(expression_list element: (_) @elt) @parent
; An index containing multiple indices such as in `x[y, z]`
(index_expression_list element: (_) @elt) @parent
; A regular tuple such as `(x, y, z)`
(tuple element: (_) @elt) @parent
@@ -3486,6 +3439,7 @@
(pattern_list element: (_) @elt)
(list_pattern element: (_) @elt)
(expression_list element: (_) @elt)
(index_expression_list element: (_) @elt)
(parenthesized_expression inner: (_) @elt)
(set element: (_) @elt)
(match_sequence_pattern (_) @elt)

View File

@@ -929,11 +929,18 @@ module.exports = grammar({
field('attribute', $.identifier)
)),
_index_expression: $ => choice(
$.list_splat,
$.expression,
$.slice
),
index_expression_list: $ => open_sequence(field('element', $._index_expression)),
subscript: $ => prec(PREC.call, seq(
field('value', $.primary_expression),
'[',
commaSep1(field('subscript', choice($.list_splat, $.expression, $.slice))),
optional(','),
field('subscript', choice($._index_expression, $.index_expression_list)),
']'
)),

View File

@@ -5045,6 +5045,86 @@
]
}
},
"_index_expression": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "list_splat"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "SYMBOL",
"name": "slice"
}
]
},
"index_expression_list": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "element",
"content": {
"type": "SYMBOL",
"name": "_index_expression"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SEQ",
"members": [
{
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "FIELD",
"name": "element",
"content": {
"type": "SYMBOL",
"name": "_index_expression"
}
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
}
]
}
]
}
},
"subscript": {
"type": "PREC",
"value": 21,
@@ -5064,75 +5144,21 @@
"value": "["
},
{
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "subscript",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "list_splat"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "SYMBOL",
"name": "slice"
}
]
"type": "FIELD",
"name": "subscript",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_index_expression"
},
{
"type": "SYMBOL",
"name": "index_expression_list"
}
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "FIELD",
"name": "subscript",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "list_splat"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "SYMBOL",
"name": "slice"
}
]
}
}
]
}
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
]
}
},
{
"type": "STRING",

View File

@@ -1829,6 +1829,30 @@
}
}
},
{
"type": "index_expression_list",
"named": true,
"fields": {
"element": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "list_splat",
"named": true
},
{
"type": "slice",
"named": true
}
]
}
}
},
{
"type": "interpolation",
"named": true,
@@ -3200,13 +3224,17 @@
"named": true,
"fields": {
"subscript": {
"multiple": true,
"multiple": false,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "index_expression_list",
"named": true
},
{
"type": "list_splat",
"named": true

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,5 @@
---
category: fix
---
- Fixed a bug in the extractor where a comment inside a subscript could sometimes cause the AST to be missing nodes.