Comments (5)
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.
The PR mentioned has been merged, so I believe it is now possible to "get tree node parent".
from py-tree-sitter.
From a quick search:
py-tree-sitter/tree_sitter/binding.c
Lines 220 to 231 in 868e667
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.
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.
FYI, #35 is a PR that includes code for parent
.
from py-tree-sitter.
Related Issues (20)
- Query doesn't work correctly when using read_callable HOT 11
- Miss distutils in Windows HOT 1
- Documentation is required.
- Is it possible to understand if an object is an instance of a certain class that may be defined in another file?
- How to use tree-sitter in Windows 10? HOT 6
- Special case not managed by the parser, when an expression is split without a backslash and the second line is dedented. HOT 4
- Example mismatch HOT 1
- Captures are not grouped HOT 3
- How to sync node after code edit? HOT 1
- README.md needs updates
- Tree-sitter Fails with Core Dump on Processing Large Input Code File HOT 4
- query failing in py-tree-sitter but compiling in tree-sitter playground HOT 1
- a __main__.py to build `vendor/tree-sitter-X` from command line HOT 1
- [bug] Cannot build a library in a directory containing two parsers. HOT 5
- The Python parser appears to be parsing comments and strings with unmatched parentheses as code HOT 2
- py-tree-sitter is 10x slower than lezer-parser HOT 9
- FileNotFoundError: [Errno 2] No such file or directory: 'tree-sitter-java/src/parser.c' HOT 2
- UTF-16 encoding support is wanted HOT 1
- Can't install tree-sitter-python HOT 1
- [tree-sitter-python] `No matching distribution found for tree-sitter-python` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from py-tree-sitter.