Skip to content

Commit 5d6600e

Browse files
committed
Python: Fix generator expression locations
Our logic for detecting the first and last item in a generator expression was faulty, sometimes matching comments as well. Because attributes (like `_location_start`) can only be written once, this caused `tree-sitter-graph` to get unhappy. To fix this, we now require the first item to be an `expression`, and the last one to be either a `for_in_clause` or an `if_clause`. Crucially, `comment` is neither of these, and this prevents the unfortunate overlap.
1 parent ef60b73 commit 5d6600e

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

python/extractor/tests/parser/comprehensions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,10 @@
6565
d for e in f if g # comment
6666
# comment
6767
] # comment
68+
69+
# Generator expression with comments
70+
(# comment
71+
alpha # comment
72+
for beta in gamma # comment
73+
# comment
74+
)

python/extractor/tsg-python/python.tsg

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@
404404

405405
;;; GeneratorExp
406406

407-
(generator_expression . "(" . (comment)* . (_) @start (_) @end . (comment)* . ")" .) @generatorexp
407+
(generator_expression . "(" . (comment)* . (expression) @start [(for_in_clause) (if_clause)] @end . (comment)* . ")" .) @generatorexp
408408
{
409409
attr (@generatorexp.node) _location_start = (location-start @start)
410410
attr (@generatorexp.node) _location_end = (location-end @end)
@@ -416,13 +416,13 @@
416416
attr (@if.node) _location_end = (location-end @expr)
417417
}
418418

419-
(generator_expression . "(" . (comment)* . (_) @start (for_in_clause) @child (_) @end . (comment)* . ")" .) @genexpr
419+
(generator_expression . "(" . (comment)* . (expression) @start (for_in_clause) @child [(for_in_clause) (if_clause)] @end . (comment)* . ")" .) @genexpr
420420
{
421421
attr (@child.node) _location_start = (location-start @start)
422422
attr (@child.node) _location_end = (location-end @end)
423423
}
424424

425-
(generator_expression . "(" . (comment)* . (_) @start (for_in_clause) @end . (comment)* . ")" .) @genexpr
425+
(generator_expression . "(" . (comment)* . (expression) @start (for_in_clause) @end . (comment)* . ")" .) @genexpr
426426
{
427427
attr (@end.node) _location_start = (location-start @start)
428428
attr (@end.node) _location_end = (location-end @end)
@@ -863,7 +863,7 @@
863863
; information for the entire generator expression (yes, it is a wide parameter!) and so we must recreate the logic for
864864
; setting this location information correctly.
865865

866-
(generator_expression . "(" . (comment)* . (_) @start (_) @end . (comment)* . ")" .) @genexpr
866+
(generator_expression . "(" . (comment)* . (expression) @start [(for_in_clause) (if_clause)] @end . (comment)* . ")" .) @genexpr
867867
{
868868
; Synthesize the `genexpr` function
869869
let @genexpr.fun = (ast-node @genexpr "Function")

0 commit comments

Comments
 (0)