tree-sitter-julia icon indicating copy to clipboard operation
tree-sitter-julia copied to clipboard

Question: expand `for` and `struct` grammar with headers?

Open simonmandlik opened this issue 8 months ago • 3 comments

Did you check existing issues?

  • [X] I have read all the tree-sitter docs if it relates to using the parser
  • [X] I have searched the existing issues of tree-sitter-julia

Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)

No response

Describe the bug

This is not a bug, but more like a question/feature request.

I'm trying to update / fix julia queries for neovim and I'm having a very hard time with for loops and also structs.

This python example

for a in range(10):
    pass

is parsed as follows:

(module ; [0, 0] - [2, 0]
  (for_statement ; [0, 0] - [1, 8]
    left: (identifier) ; [0, 4] - [0, 5]
    right: (call ; [0, 9] - [0, 18]
      function: (identifier) ; [0, 9] - [0, 14]
      arguments: (argument_list ; [0, 14] - [0, 18]
        (integer))) ; [0, 15] - [0, 17]
    body: (block ; [1, 4] - [1, 8]
      (pass_statement)))) ; [1, 4] - [1, 8]

and this julia example

for a in 1:10, b in 1:10
    print(a)
end

is parsed as

(source_file ; [0, 0] - [3, 0]
  (for_statement ; [0, 0] - [2, 3]
    (for_binding ; [0, 4] - [0, 13]
      (identifier) ; [0, 4] - [0, 5]
      (range_expression ; [0, 9] - [0, 13]
        (integer_literal) ; [0, 9] - [0, 10]
        (integer_literal))) ; [0, 11] - [0, 13]
    (for_binding ; [0, 15] - [0, 24]
      (identifier) ; [0, 15] - [0, 16]
      (range_expression ; [0, 20] - [0, 24]
        (integer_literal) ; [0, 20] - [0, 21]
        (integer_literal))) ; [0, 22] - [0, 24]
    (call_expression ; [1, 4] - [1, 12]
      (identifier) ; [1, 4] - [1, 9]
      (argument_list ; [1, 9] - [1, 12]
        (identifier))))) ; [1, 10] - [1, 11]

Because the two for_binding nodes are not grouped together in any way and are siblings of the call_expression, I couldn't write any query that would correctly select the loop "header" (regardless of the number of variables iterated over), and neither any query that would select the body without the "header". This might be due to the fact that I'm no expert in TS queries, but for Python such queries are really simple.

Similar situation is with struct definitions:

struct A{B, C} <: D
    x
    y
end

is parsed as

(source_file ; [0, 0] - [4, 0]
  (struct_definition ; [0, 0] - [3, 3]
    name: (identifier) ; [0, 7] - [0, 8]
    (type_parameter_list ; [0, 8] - [0, 14]
      (identifier) ; [0, 9] - [0, 10]
      (identifier)) ; [0, 12] - [0, 13]
    (type_clause ; [0, 15] - [0, 19]
      (operator) ; [0, 15] - [0, 17]
      (identifier)) ; [0, 18] - [0, 19]
    (identifier) ; [1, 4] - [1, 5]
    (identifier))) ; [2, 4] - [2, 5]

Again, struct header nodes type_parameter_list and type_clause are siblings of the struct body.

Is there a reason not to group struct and loop "headers" together similarly to how python is parsed?

simonmandlik avatar Jun 21 '24 20:06 simonmandlik