tree-sitter / tree-sitter-julia

Julia grammar for Tree-sitter
MIT License
93 stars 32 forks source link

Fail to parse: ternary expression #50

Closed barucden closed 1 year ago

barucden commented 2 years ago

Hi, I am using tree-sitter-julia via the neovim tree-sitter plugin. I stumbled upon a piece of valid Julia code that neovim fails to highlight correctly. I am not sure why (hence the title), but I could get the following MWE:

function f()
    (a == nothing ? true : a == 1) && (true) && true
end

g() = 1

Running tree-sitter parse produces

(ERROR [0, 0] - [5, 0]
  (identifier [0, 9] - [0, 10])
  (parameter_list [0, 10] - [0, 12])
  (binary_expression [1, 5] - [1, 17]
    (identifier [1, 5] - [1, 6])
    (operator [1, 7] - [1, 9])
    (identifier [1, 10] - [1, 17]))
  (ERROR [1, 20] - [2, 3]
    (binary_expression [1, 20] - [1, 52]
      (binary_expression [1, 20] - [1, 44]
        (binary_expression [1, 20] - [1, 33]
          (range_expression [1, 20] - [1, 28]
            (true [1, 20] - [1, 24])
            (identifier [1, 27] - [1, 28]))
          (operator [1, 29] - [1, 31])
          (integer_literal [1, 32] - [1, 33]))
        (ERROR [1, 33] - [1, 34])
        (operator [1, 35] - [1, 37])
        (parenthesized_expression [1, 38] - [1, 44]
          (true [1, 39] - [1, 43])))
      (operator [1, 45] - [1, 47])
      (true [1, 48] - [1, 52])))
  (call_expression [4, 0] - [4, 3]
    (identifier [4, 0] - [4, 1])
    (argument_list [4, 1] - [4, 3])))

EDIT: I further reduced the MWE, and the problem seems to be the ternary expression:

a == nothing ? true : a == 1

Parsed tree:

(source_file [0, 0] - [2, 0]
  (binary_expression [0, 0] - [0, 12]
    (identifier [0, 0] - [0, 1])
    (operator [0, 2] - [0, 4])
    (identifier [0, 5] - [0, 12]))
  (ERROR [0, 13] - [0, 28]
    (binary_expression [0, 15] - [0, 28]
      (range_expression [0, 15] - [0, 23]
        (true [0, 15] - [0, 19])
        (identifier [0, 22] - [0, 23]))
      (operator [0, 24] - [0, 26])
      (integer_literal [0, 27] - [0, 28]))))

However, adding parentheses

a == nothing ? true : (a == 1)

fixes the problem

(source_file [0, 0] - [2, 0]
  (ternary_expression [0, 0] - [0, 30]
    (binary_expression [0, 0] - [0, 12]
      (identifier [0, 0] - [0, 1])
      (operator [0, 2] - [0, 4])
      (identifier [0, 5] - [0, 12]))
    (true [0, 15] - [0, 19])
    (parenthesized_expression [0, 22] - [0, 30]
      (binary_expression [0, 23] - [0, 29]
        (identifier [0, 23] - [0, 24])
        (operator [0, 25] - [0, 27])
        (integer_literal [0, 28] - [0, 29])))))
barucden commented 1 year ago

This is working now.

savq commented 1 year ago

@barucden are you sure this is now working? I still have this error.

barucden commented 1 year ago

You are right. I accidentally checked the working version a == nothing ? true : (a == 1). The original issue is still there.