2014-07-17 23:29:11 -07:00
|
|
|
#include "runtime/node.h"
|
|
|
|
|
#include "runtime/tree.h"
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
TSNode *ts_node_make(const TSTree *tree, TSNode *parent, size_t index,
|
|
|
|
|
size_t start_position, const char **names) {
|
|
|
|
|
if (parent)
|
|
|
|
|
ts_node_retain(parent);
|
2014-07-20 20:27:33 -07:00
|
|
|
TSNode *result = malloc(sizeof(TSNode));
|
2014-07-20 21:43:27 -07:00
|
|
|
*result = (TSNode) { .ref_count = 1,
|
|
|
|
|
.parent = parent,
|
|
|
|
|
.index = index,
|
|
|
|
|
.content = tree,
|
|
|
|
|
.start_position = start_position,
|
|
|
|
|
.names = names, };
|
2014-07-20 20:27:33 -07:00
|
|
|
return result;
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
TSNode *ts_node_make_root(const TSTree *tree, const char **names) {
|
2014-07-20 20:27:33 -07:00
|
|
|
while (ts_tree_is_wrapper(tree))
|
|
|
|
|
tree = tree->children[0];
|
|
|
|
|
return ts_node_make(tree, NULL, 0, 0, names);
|
2014-07-18 13:08:14 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
void ts_node_retain(TSNode *node) { node->ref_count++; }
|
2014-07-17 23:29:11 -07:00
|
|
|
|
|
|
|
|
void ts_node_release(TSNode *node) {
|
2014-07-20 20:27:33 -07:00
|
|
|
node->ref_count--;
|
|
|
|
|
if (node->ref_count == 0) {
|
2014-07-20 21:43:27 -07:00
|
|
|
if (node->parent)
|
|
|
|
|
ts_node_release(node->parent);
|
2014-07-20 20:27:33 -07:00
|
|
|
free(node);
|
|
|
|
|
}
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t ts_node_pos(const TSNode *node) {
|
2014-07-20 20:27:33 -07:00
|
|
|
return node->start_position + node->content->offset;
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
size_t ts_node_size(const TSNode *node) { return node->content->size; }
|
2014-07-17 23:29:11 -07:00
|
|
|
|
2014-07-18 13:24:03 -07:00
|
|
|
int ts_node_eq(const TSNode *left, const TSNode *right) {
|
2014-07-20 20:27:33 -07:00
|
|
|
return ts_tree_equals(left->content, right->content);
|
2014-07-18 13:24:03 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
const char *ts_node_name(const TSNode *node) {
|
2014-07-20 20:27:33 -07:00
|
|
|
return node->names[node->content->symbol];
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
const char *ts_node_string(const TSNode *node) {
|
2014-07-20 20:27:33 -07:00
|
|
|
return ts_tree_string(node->content, node->names);
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
TSNode *ts_node_parent(TSNode *child) { return child->parent; }
|
2014-07-18 13:24:03 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
TSNode *ts_node_prev_sibling(TSNode *child) {
|
2014-07-20 20:27:33 -07:00
|
|
|
return ts_node_child(child->parent, child->index - 1);
|
2014-07-18 13:24:03 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
TSNode *ts_node_next_sibling(TSNode *child) {
|
2014-07-20 20:27:33 -07:00
|
|
|
return ts_node_child(child->parent, child->index + 1);
|
2014-07-18 13:24:03 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 17:57:20 -07:00
|
|
|
size_t ts_node_child_count(const TSNode *parent) {
|
2014-07-20 20:27:33 -07:00
|
|
|
size_t result;
|
|
|
|
|
ts_tree_visible_children(parent->content, &result);
|
|
|
|
|
return result;
|
2014-07-20 17:57:20 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
TSNode *ts_node_child(TSNode *parent, size_t index) {
|
2014-07-20 20:27:33 -07:00
|
|
|
size_t child_count;
|
2014-07-20 21:43:27 -07:00
|
|
|
TSChildWithPosition *children =
|
|
|
|
|
ts_tree_visible_children(parent->content, &child_count);
|
2014-07-20 20:27:33 -07:00
|
|
|
if (child_count <= index)
|
|
|
|
|
return NULL;
|
|
|
|
|
size_t position = parent->start_position + children[index].position;
|
2014-07-20 21:43:27 -07:00
|
|
|
return ts_node_make(children[index].tree, parent, index, position,
|
|
|
|
|
parent->names);
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
2014-08-25 09:31:27 -07:00
|
|
|
TSNode *ts_node_find_for_range(TSNode *parent, size_t min, size_t max) {
|
2014-07-20 20:27:33 -07:00
|
|
|
size_t child_count;
|
2014-07-20 21:43:27 -07:00
|
|
|
TSChildWithPosition *children =
|
|
|
|
|
ts_tree_visible_children(parent->content, &child_count);
|
2014-07-20 20:27:33 -07:00
|
|
|
for (size_t i = 0; i < child_count; i++) {
|
|
|
|
|
TSChildWithPosition child = children[i];
|
|
|
|
|
size_t child_left = child.position + child.tree->offset;
|
2014-08-25 09:31:27 -07:00
|
|
|
if (child_left > min)
|
2014-07-20 20:27:33 -07:00
|
|
|
break;
|
2014-08-25 09:31:27 -07:00
|
|
|
if (child_left + child.tree->size >= max) {
|
2014-07-20 21:43:27 -07:00
|
|
|
TSNode *node =
|
|
|
|
|
ts_node_make(child.tree, parent, i, child.position, parent->names);
|
2014-08-25 09:31:27 -07:00
|
|
|
TSNode *result = ts_node_find_for_range(node, min, max);
|
2014-07-20 20:27:33 -07:00
|
|
|
ts_node_release(node);
|
|
|
|
|
return result;
|
2014-07-18 18:52:08 -07:00
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2014-07-18 18:52:08 -07:00
|
|
|
|
2014-07-20 20:27:33 -07:00
|
|
|
ts_node_retain(parent);
|
|
|
|
|
return parent;
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
2014-08-25 09:31:27 -07:00
|
|
|
|
|
|
|
|
TSNode *ts_node_find_for_pos(TSNode *parent, size_t position) {
|
|
|
|
|
return ts_node_find_for_range(parent, position, position);
|
|
|
|
|
}
|