tree-sitter/src/runtime/node.c

64 lines
1.7 KiB
C
Raw Normal View History

2014-07-17 23:29:11 -07:00
#include "runtime/node.h"
#include "tree_sitter/parser.h"
#include "runtime/tree.h"
2014-07-18 13:08:14 -07:00
TSNode * ts_node_make(const TSTree *tree, TSNode *parent, size_t start_position, TSParserConfig *config) {
2014-07-17 23:29:11 -07:00
if (parent) ts_node_retain(parent);
TSNode *result = malloc(sizeof(TSNode));
*result = (TSNode) {
.ref_count = 1,
.parent = parent,
.content = tree,
2014-07-18 13:08:14 -07:00
.start_position = start_position,
2014-07-17 23:29:11 -07:00
.config = config
};
return result;
}
2014-07-18 13:08:14 -07:00
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);
}
2014-07-17 23:29:11 -07:00
void ts_node_retain(TSNode *node) {
node->ref_count++;
}
void ts_node_release(TSNode *node) {
node->ref_count--;
if (node->ref_count == 0) {
if (node->parent) ts_node_release(node->parent);
free(node);
}
}
size_t ts_node_pos(const TSNode *node) {
2014-07-18 13:08:14 -07:00
return node->start_position + node->content->offset;
2014-07-17 23:29:11 -07:00
}
size_t ts_node_size(const TSNode *node) {
return node->content->size;
}
const char * ts_node_name(const TSNode *node) {
return node->config->symbol_names[node->content->symbol];
}
const char * ts_node_string(const TSNode *node) {
return ts_tree_string(node->content, node->config->symbol_names);
}
2014-07-18 13:08:14 -07:00
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)
2014-07-17 23:29:11 -07:00
return NULL;
2014-07-18 13:08:14 -07:00
size_t position = parent->start_position + children[index].position;
return ts_node_make(children[index].tree, parent, position, parent->config);
2014-07-17 23:29:11 -07:00
}
TSNode * ts_node_leaf_at_pos(TSNode *parent, size_t child_index) {
return NULL;
}