2014-10-03 16:06:08 -07:00
|
|
|
#include <stdbool.h>
|
2014-07-17 23:29:11 -07:00
|
|
|
#include "runtime/node.h"
|
2014-09-26 16:15:07 -07:00
|
|
|
#include "runtime/length.h"
|
2014-07-17 23:29:11 -07:00
|
|
|
#include "runtime/tree.h"
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
TSNode *ts_node_make(const TSTree *tree, TSNode *parent, size_t index,
|
2014-09-26 16:15:07 -07:00
|
|
|
TSLength position, const char **names) {
|
2014-07-20 21:43:27 -07:00
|
|
|
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,
|
2014-08-28 13:22:06 -07:00
|
|
|
.position = position,
|
2014-07-20 21:43:27 -07:00
|
|
|
.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-09-02 07:41:29 -07:00
|
|
|
return ts_node_make(tree, NULL, 0, tree->padding, 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
|
|
|
}
|
|
|
|
|
|
2014-09-26 16:15:07 -07:00
|
|
|
TSLength ts_node_pos(const TSNode *node) { return node->position; }
|
2014-07-17 23:29:11 -07:00
|
|
|
|
2014-09-26 16:15:07 -07:00
|
|
|
TSLength ts_node_size(const TSNode *node) { return node->content->size; }
|
2014-07-17 23:29:11 -07:00
|
|
|
|
2014-10-03 16:06:08 -07:00
|
|
|
bool ts_node_eq(const TSNode *left, const TSNode *right) {
|
|
|
|
|
return ts_tree_eq(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-08-25 11:28:09 -07:00
|
|
|
if (child->parent)
|
|
|
|
|
return ts_node_child(child->parent, child->index - 1);
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
2014-07-18 13:24:03 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
TSNode *ts_node_next_sibling(TSNode *child) {
|
2014-08-25 11:28:09 -07:00
|
|
|
if (child->parent)
|
|
|
|
|
return ts_node_child(child->parent, child->index + 1);
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
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-08-28 13:22:06 -07:00
|
|
|
TSNode *ts_node_child(TSNode *parent, size_t i) {
|
|
|
|
|
size_t count;
|
|
|
|
|
TSTreeChild *children = ts_tree_visible_children(parent->content, &count);
|
|
|
|
|
if (i >= count)
|
2014-07-20 20:27:33 -07:00
|
|
|
return NULL;
|
2014-09-26 16:15:07 -07:00
|
|
|
TSLength pos = ts_length_add(parent->position, children[i].offset);
|
2014-08-28 13:30:02 -07:00
|
|
|
return ts_node_make(children[i].tree, parent, i, pos, 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-08-28 13:22:06 -07:00
|
|
|
size_t count;
|
|
|
|
|
TSTreeChild *children = ts_tree_visible_children(parent->content, &count);
|
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
|
TSTreeChild child = children[i];
|
2014-09-26 16:15:07 -07:00
|
|
|
TSLength pos = ts_length_add(parent->position, child.offset);
|
|
|
|
|
if (pos.chars > min)
|
2014-07-20 20:27:33 -07:00
|
|
|
break;
|
2014-09-26 16:15:07 -07:00
|
|
|
if (pos.chars + child.tree->size.chars > max) {
|
2014-08-28 13:30:02 -07:00
|
|
|
TSNode *node = ts_node_make(child.tree, parent, i, pos, 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);
|
|
|
|
|
}
|