Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returned parent node doesn't contain given node as child #274

Open
mathrick opened this issue Mar 6, 2024 · 1 comment
Open

Returned parent node doesn't contain given node as child #274

mathrick opened this issue Mar 6, 2024 · 1 comment

Comments

@mathrick
Copy link

mathrick commented Mar 6, 2024

(let* ((parser (tsc-make-parser))
       (parsed (progn
                 (tsc-set-language parser (tree-sitter-require 'python))
                 (tsc-root-node (tsc-parse-string parser "def dupa(foo):")))))
  (message "parsed is %s" parsed)
  (tsc-traverse-mapc (lambda (node)
                       (let ((parent (tsc-get-parent node)))
                         (when (and parent
                                    (zerop (tsc-count-children parent)))
                           (error "Our parent has zero children!? %s" (tsc-node-to-sexp node)))))
                     parsed))

This results in:

tsc-traverse-mapc: Our parent has zero children!? ("_newline")

For some reason, the node returns the trailing ":" as its parent, but it is actually its sibling in the traversal order (ie. (tsc-node-eq node (tsc-get-next-sibling parent)) => t).

@ubolonton
Copy link
Collaborator

Seems like a bug in the underlying C API of tsc-get-parent. I could reproduce this with the Python binding:

>>> from tree_sitter import *
>>> py = Language("python.dylib", "python")
>>> p = Parser()
>>> p.set_language(py)
>>> tree = p.parse("def dupa(foo):".encode('utf-8')); tree
<tree_sitter.Tree object at 0x1027d2410>
>>>
>>> fd = tree.root_node.child(0); fd
<Node type=function_definition, start_point=(0, 0), end_point=(0, 14)>
>>>
>>> blk = fd.child(4); blk
<Node type=block, start_point=(0, 14), end_point=(0, 14)>
>>>
>>> blk.parent
<Node type=":", start_point=(0, 13), end_point=(0, 14)>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants