tree-sitter / tree-sitter-python

Python grammar for tree-sitter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dedent regression in valid ternary expression in f-string

stephen-huan opened this issue · comments

The valid expression

def function():
     return f"""
{"string1" if True else
 "string2"}"""

parses as

(module [0, 0] - [5, 0]
 (function_definition [0, 0] - [2, 23]
   name: (identifier [0, 4] - [0, 12])
   parameters: (parameters [0, 12] - [0, 14])
   (ERROR [1, 5] - [2, 23]
     string_content: (string_content [1, 16] - [2, 0])
     (string [2, 1] - [2, 10]
       string_content: (string_content [2, 2] - [2, 9]))
     (true [2, 14] - [2, 18]))
   body: (block [2, 23] - [2, 23]))
 (ERROR [3, 0] - [3, 13]
   (string [3, 0] - [3, 9]
     string_content: (string_content [3, 1] - [3, 8]))
   (ERROR [3, 9] - [3, 10])))

however, the similar expressions

def function():
     return f"""
{"string1" if True else "string2"}"""

and

f"""
{"string1" if True else
"string2"}"""

both parse correctly, implying the the problem is with the indentation.

git bisect reveals that the problem is with commit 188b6b0.
its parent 597c4bb parses correctly as

(module [0, 0] - [4, 0]
  (function_definition [0, 0] - [3, 14]
    name: (identifier [0, 4] - [0, 12])
    parameters: (parameters [0, 12] - [0, 14])
    body: (block [1, 5] - [3, 14]
      (return_statement [1, 5] - [3, 14]
        (string [1, 12] - [3, 14]
          (interpolation [2, 0] - [3, 11]
            (conditional_expression [2, 1] - [3, 10]
              (string [2, 1] - [2, 10])
              (true [2, 14] - [2, 18])
              (string [3, 1] - [3, 10]))))))))

but commit 188b6b0 parses it as

(module [0, 0] - [4, 0]
  (function_definition [0, 0] - [2, 23]
    name: (identifier [0, 4] - [0, 12])
    parameters: (parameters [0, 12] - [0, 14])
    (ERROR [1, 5] - [2, 23]
      (string [2, 1] - [2, 10])
      (true [2, 14] - [2, 18]))
    body: (block [2, 23] - [2, 23]))
  (ERROR [3, 1] - [3, 14]
    (string [3, 1] - [3, 10])
    (ERROR [3, 10] - [3, 11])))