Skip to content

Commit c5be2a3

Browse files
committed
Python: Allow comments in subscripts
Once again, the interaction between anchors and extras (specifically comments) was causing trouble. The root of the problem was the fact that in `a[b]`, we put `b` in the `index` field of the subscript node, whereas in `a[b,c]`, we additionally synthesize a `Tuple` node for `b,c` (which matches the Python AST). To fix this, we refactored the grammar slightly so as to make that tuple explicit, such that a subscript node either contains a single expression or the newly added tuple node. This greatly simplifies the logic.
1 parent 5773538 commit c5be2a3

File tree

3 files changed

+37
-55
lines changed

3 files changed

+37
-55
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
a[b]
2+
3+
c[d,e]
4+
5+
c1[d1,]
6+
7+
# And now with many comments
8+
9+
e[
10+
# comment1
11+
f
12+
# comment2
13+
]
14+
15+
g[
16+
# comment3
17+
h,
18+
# comment4
19+
i
20+
# comment5
21+
]

python/extractor/tsg-python/python.tsg

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
(assignment !type) @assign
2323
{ let @assign.node = (ast-node @assign "Assign") }
2424

25-
[ (expression_list) (tuple) (tuple_pattern) (pattern_list) ] @tuple
25+
[ (expression_list) (tuple) (tuple_pattern) (pattern_list) (index_expression_list) ] @tuple
2626
{ let @tuple.node = (ast-node @tuple "Tuple") }
2727

2828
(list_pattern) @list
@@ -2543,66 +2543,16 @@
25432543

25442544
(subscript
25452545
value: (_) @value
2546+
subscript: (_) @index
25462547
) @subscript
25472548
{
25482549
attr (@subscript.node) value = @value.node
25492550
attr (@value.node) ctx = "load"
2550-
}
25512551

2552-
; Single subscript
2553-
(subscript
2554-
value: (_)
2555-
.
2556-
subscript: (_) @index
2557-
.
2558-
) @subscript
2559-
{
25602552
attr (@subscript.node) index = @index.node
25612553
attr (@index.node) ctx = "load"
25622554
}
25632555

2564-
; For expressions of the form `a[b, c]` we must explicitly synthesize an internal tuple node
2565-
; We do this and also hook it up:
2566-
(subscript
2567-
value: (_)
2568-
.
2569-
subscript: (_) @first
2570-
.
2571-
subscript: (_)
2572-
) @subscript
2573-
{
2574-
let @subscript.tuple = (ast-node @first "Tuple")
2575-
attr (@subscript.tuple) ctx = "load"
2576-
attr (@subscript.node) index = @subscript.tuple
2577-
edge @subscript.tuple -> @first.node
2578-
attr (@subscript.tuple -> @first.node) elts = (named-child-index @first)
2579-
attr (@first.node) ctx = "load"
2580-
}
2581-
2582-
(subscript
2583-
value: (_)
2584-
.
2585-
subscript: (_)
2586-
subscript: (_) @elt
2587-
) @subscript
2588-
{
2589-
edge @subscript.tuple -> @elt.node
2590-
attr (@subscript.tuple -> @elt.node) elts = (named-child-index @elt)
2591-
attr (@elt.node) ctx = "load"
2592-
}
2593-
2594-
2595-
; Set the end position correctly
2596-
(subscript
2597-
value: (_)
2598-
.
2599-
subscript: (_)
2600-
subscript: (_) @last
2601-
.
2602-
) @subscript
2603-
{
2604-
attr (@subscript.tuple) _location_end = (location-end @last)
2605-
}
26062556

26072557

26082558

@@ -3448,9 +3398,12 @@
34483398
; Left hand side of an assignment such as `[foo, bar] = ...`
34493399
(list_pattern element: (_) @elt) @parent
34503400

3451-
; An unadorned tuple (such as in `x = y, z`)
3401+
; An unadorned tuple such as in `x = y, z`
34523402
(expression_list element: (_) @elt) @parent
34533403

3404+
; An index containing multiple indices such as in `x[y, z]`
3405+
(index_expression_list element: (_) @elt) @parent
3406+
34543407
; A regular tuple such as `(x, y, z)`
34553408
(tuple element: (_) @elt) @parent
34563409

@@ -3486,6 +3439,7 @@
34863439
(pattern_list element: (_) @elt)
34873440
(list_pattern element: (_) @elt)
34883441
(expression_list element: (_) @elt)
3442+
(index_expression_list element: (_) @elt)
34893443
(parenthesized_expression inner: (_) @elt)
34903444
(set element: (_) @elt)
34913445
(match_sequence_pattern (_) @elt)

python/extractor/tsg-python/tsp/grammar.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,11 +929,18 @@ module.exports = grammar({
929929
field('attribute', $.identifier)
930930
)),
931931

932+
_index_expression: $ => choice(
933+
$.list_splat,
934+
$.expression,
935+
$.slice
936+
),
937+
938+
index_expression_list: $ => open_sequence(field('element', $._index_expression)),
939+
932940
subscript: $ => prec(PREC.call, seq(
933941
field('value', $.primary_expression),
934942
'[',
935-
commaSep1(field('subscript', choice($.list_splat, $.expression, $.slice))),
936-
optional(','),
943+
field('subscript', choice($._index_expression, $.index_expression_list)),
937944
']'
938945
)),
939946

0 commit comments

Comments
 (0)