Add functions to retrieve nodes' siblings and parents

This commit is contained in:
Max Brunsfeld 2014-07-18 13:24:03 -07:00
parent 0e11bf7271
commit 1ecafb874e
5 changed files with 69 additions and 63 deletions

View file

@ -2,12 +2,13 @@
#include "tree_sitter/parser.h"
#include "runtime/tree.h"
TSNode * ts_node_make(const TSTree *tree, TSNode *parent, size_t start_position, TSParserConfig *config) {
TSNode * ts_node_make(const TSTree *tree, TSNode *parent, size_t index, size_t start_position, TSParserConfig *config) {
if (parent) ts_node_retain(parent);
TSNode *result = malloc(sizeof(TSNode));
*result = (TSNode) {
.ref_count = 1,
.parent = parent,
.index = index,
.content = tree,
.start_position = start_position,
.config = config
@ -18,7 +19,7 @@ TSNode * ts_node_make(const TSTree *tree, TSNode *parent, size_t start_position,
TSNode * ts_node_make_root(const TSTree *tree, TSParserConfig *config) {
while (ts_tree_is_wrapper(tree))
tree = tree->children[0];
return ts_node_make(tree, NULL, 0, config);
return ts_node_make(tree, NULL, 0, 0, config);
}
void ts_node_retain(TSNode *node) {
@ -41,6 +42,10 @@ size_t ts_node_size(const TSNode *node) {
return node->content->size;
}
int ts_node_eq(const TSNode *left, const TSNode *right) {
return ts_tree_equals(left->content, right->content);
}
const char * ts_node_name(const TSNode *node) {
return node->config->symbol_names[node->content->symbol];
}
@ -49,13 +54,25 @@ const char * ts_node_string(const TSNode *node) {
return ts_tree_string(node->content, node->config->symbol_names);
}
TSNode * ts_node_parent(TSNode *child) {
return child->parent;
}
TSNode * ts_node_prev_sibling(TSNode *child) {
return ts_node_child(child->parent, child->index - 1);
}
TSNode * ts_node_next_sibling(TSNode *child) {
return ts_node_child(child->parent, child->index + 1);
}
TSNode * ts_node_child(TSNode *parent, size_t index) {
size_t child_count;
TSChildWithPosition *children = ts_tree_visible_children(parent->content, &child_count);
if (child_count <= index)
return NULL;
size_t position = parent->start_position + children[index].position;
return ts_node_make(children[index].tree, parent, position, parent->config);
return ts_node_make(children[index].tree, parent, index, position, parent->config);
}
TSNode * ts_node_leaf_at_pos(TSNode *parent, size_t child_index) {