tree-sitter / tree-sitter-haskell

Haskell grammar for tree-sitter.
MIT License
157 stars 37 forks source link

Incorrect parse for function with where-clause and comments #109

Open wenkokke opened 10 months ago

wenkokke commented 10 months ago

When parsing a function with a where-clause, tree-sitter-haskell groups comments after the where-clause into the decls node, rather than making them top-level comment nodes. For instance, the following code:

foo = bar
  where
    bar = 1
--   πŸŽ‰ `foo = bar` and `bar = 1`
--   πŸ‘€ `foo = bar where bar = 1`
--   πŸš€ `foo = bar where bar = 1` and `bar = 1`

bap :: Int -> Int
bap | 1 == 1, 2 == 2 = x
--   πŸŽ‰ `1 == 1` and `2 == 2`
--   πŸ‘€ `1 == 1, 2 == 2`

Parses to the following tree:

(haskell
  (function
    name: (variable)
    "="
    rhs: (exp_name
      (variable)
    )
    (where)
    (decls
      (function
        name: (variable)
        "="
        rhs: (exp_literal
          (integer)
        )
      )
      (comment)
      (comment)
      (comment)
    )
  )
  (signature
    name: (variable)
    type: "::"
    type: (fun
      (type_name
        (type)
      )
      "->"
      (type_name
        (type)
      )
    )
  )
  (function
    name: (variable)
    (guard_equation
      (guards
        "|"
        (guard
          (exp_infix
            (exp_literal
              (integer)
            )
            (operator)
            (exp_literal
              (integer)
            )
          )
        )
        (comma)
        (guard
          (exp_infix
            (exp_literal
              (integer)
            )
            (operator)
            (exp_literal
              (integer)
            )
          )
        )
      )
      "="
      (exp_name
        (variable)
      )
    )
  )
  (comment)
  (comment)
)
Nisarg1112 commented 7 months ago

Hey @wenkokke @tek Have you guys done any fixes for this issue?

tek commented 7 months ago

@Nisarg1112 partially, in #120, but there's some work to be done before merging that PR and some follow-up work for fine-tuning comments.

Nisarg1112 commented 7 months ago

@tek Any estimate by when that can be fully done and get merged?

tek commented 7 months ago

@Nisarg1112 optimistically I would say two weeks, but if you're a dependent library author you can help speed up the process by giving feedback on discourse πŸ˜„

sakthii25 commented 4 months ago

Hello @tek,

I also faced the same problem when parsing a function with a where clause. Tree-sitter Haskell groups comments into the function. Have you fixed this issue? If not, is there a temporary solution or workaround until it is resolved?

Nisarg1112 commented 4 months ago

@sakthii25 Maybe you can traverse back the tree-sitter nodes and use regex to identify the comment blocks at the end of the function and then create a new tree?

sakthii25 commented 4 months ago

@sakthii25 Maybe you can traverse back the tree-sitter nodes and use regex to identify the comment blocks at the end of the function and then create a new tree?

For now, I’m just using the comment from the last node's function, as you suggested. However, I don’t think this is an ideal solution. Is there any other approach to resolve this issue, or do you know when it might be fixed

Nisarg1112 commented 4 months ago

Yes, I agree that it's not ideal solution rather it's just a work-around. Unfortunately, I don't have visibility over when this would be fixed

tek commented 4 months ago

if this is still broken you're likely using a pre-#120 version. If that's not the case, please post some example code

sakthii25 commented 4 months ago

the version of tree-sitter is Version: 0.21.3 the version of tree-sitter-languages is Version: 1.10.2 i use python are you asking about these versions @tek ?

tek commented 4 months ago

According to https://pypi.org/project/tree-sitter-languages/, the latest release is from February, so it definitely won't contain the fix for this