GithubHelp home page GithubHelp logo

Comments (5)

issue-label-bot avatar issue-label-bot commented on May 19, 2024 1

Issue-Label Bot is automatically applying the label question to this issue, with a confidence of 0.89. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

from py-tree-sitter.

sogaiu avatar sogaiu commented on May 19, 2024 1

The PR mentioned has been merged, so I believe it is now possible to "get tree node parent".

from py-tree-sitter.

sogaiu avatar sogaiu commented on May 19, 2024

From a quick search:

static PyGetSetDef node_accessors[] = {
{"type", (getter)node_get_type, NULL, "The node's type", NULL},
{"is_named", (getter)node_get_is_named, NULL, "Is this a named node", NULL},
{"has_changes", (getter)node_get_has_changes, NULL, "Does this node have text changes since it was parsed", NULL},
{"has_error", (getter)node_get_has_error, NULL, "Does this node contain any errors", NULL},
{"start_byte", (getter)node_get_start_byte, NULL, "The node's start byte", NULL},
{"end_byte", (getter)node_get_end_byte, NULL, "The node's end byte", NULL},
{"start_point", (getter)node_get_start_point, NULL, "The node's start point", NULL},
{"end_point", (getter)node_get_end_point, NULL, "The node's end point", NULL},
{"children", (getter)node_get_children, NULL, "The node's children", NULL},
{NULL}
};

it doesn't look exposed via basic functionality, though it looks possible via a cursor:

cursor = tree.walk()
cursor.goto_first_child()
cursor.goto_parent()
cursor.node

https://github.com/tree-sitter/py-tree-sitter#walking-syntax-trees

On a side note, node-tree-sitter seems to have it: https://github.com/tree-sitter/node-tree-sitter/blob/c40d598b4e3d587c0be26ac6a780d91815ff731a/tree-sitter.d.ts#L52

Perhaps it's not too hard to make this available?

from py-tree-sitter.

sogaiu avatar sogaiu commented on May 19, 2024

I don't know how proper the following is, but in limited testing, it seems to work here:

diff --git a/tree_sitter/binding.c b/tree_sitter/binding.c
index 2b7b953..674a3f0 100644
--- a/tree_sitter/binding.c
+++ b/tree_sitter/binding.c
@@ -185,6 +185,14 @@ static PyObject *node_get_children(Node *self, void *payload) {
   return result;
 }
 
+static PyObject *node_get_parent(Node *self, void *payload) {
+  TSNode parent = ts_node_parent(self->node);
+  if (ts_node_is_null(parent)) {
+    Py_RETURN_NONE;
+  }
+  return node_new_internal(parent, self->tree);
+}
+
 static PyMethodDef node_methods[] = {
   {
     .ml_name = "walk",
@@ -227,6 +235,7 @@ static PyGetSetDef node_accessors[] = {
   {"start_point", (getter)node_get_start_point, NULL, "The node's start point", NULL},
   {"end_point", (getter)node_get_end_point, NULL, "The node's end point", NULL},
   {"children", (getter)node_get_children, NULL, "The node's children", NULL},
+  {"parent", (getter)node_get_parent, NULL, "The node's parent", NULL},
   {NULL}
 };

Basically it just adds a node_get_parent function and a corresponding parent to node_methods[].

To test this, save the diff to a file (e.g. parent.patch), then in the py-tree-sitter directory:

git apply parent.patch

To verify the patch applied correctly run:

git diff

That should produce output similar to the content of the patch.

Then to build a customized py-tree-sitter that should be testable within the source directory:

python setup.py build_ext --inplace

Here I tested the following code (it's likely you'll need to adjust it to use the appropriate language, the content of source, etc.):

from tree_sitter import Language, Parser

CLJ_LANGUAGE = Language('build/my-languages.so', 'clojure')

parser = Parser()
parser.set_language(CLJ_LANGUAGE)

source = bytes("^:a [:x]", "utf8")

tree = parser.parse(source)

root_node = tree.root_node

vector = root_node.children[0]

print(vector.parent)

from py-tree-sitter.

sogaiu avatar sogaiu commented on May 19, 2024

FYI, #35 is a PR that includes code for parent.

from py-tree-sitter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.