Skip to content

Commit

Permalink
Improve parsing of for-from loops
Browse files Browse the repository at this point in the history
  • Loading branch information
chrhansk committed Dec 8, 2024
1 parent 0fe3849 commit a1c9c26
Show file tree
Hide file tree
Showing 7 changed files with 229,300 additions and 233,638 deletions.
36 changes: 29 additions & 7 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,19 +780,41 @@ module.exports = grammar(Python, {
),
),

for_statement: $ =>

for_in_loop: $ =>
seq(
optional("async"),
"for",
optional(seq(
field("left", $._left_hand_side),
choice("in", "from"),
)),
field("left", $._left_hand_side),
"in",
field("right", $._expressions),
),

for_from_relation: $ =>
choice("<", "<=", ">", ">="),

for_from_loop: $ =>
seq(
optional(
seq(
field("left", $.identifier),
"from",
),
),
field("lower", choice($.primary_expression)),
$.for_from_relation,
field("var", $.identifier),
$.for_from_relation,
field("upper", choice($.primary_expression)),
optional(seq(
"by",
$._expressions,
)),
),

for_statement: $ =>
seq(
optional("async"),
"for",
choice($.for_from_loop, $.for_in_loop),
":",
field("body", $._suite),
field("alternative", optional($.else_clause)),
Expand Down
6 changes: 3 additions & 3 deletions queries/locals.scm
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@

; Loops
; not a scope!
(for_statement
(for_in_loop
left: (pattern_list
(identifier) @local.definition))

(for_statement
(for_in_loop
left: (tuple_pattern
(identifier) @local.definition))

(for_statement
(for_in_loop
left: (identifier) @local.definition)

; not a scope!
Expand Down
197 changes: 143 additions & 54 deletions src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a1c9c26

Please sign in to comment.