tree-sitter/src/runtime/node.c

234 lines
6.9 KiB
C
Raw Normal View History

2014-10-03 16:06:08 -07:00
#include <stdbool.h>
2014-07-17 23:29:11 -07:00
#include "runtime/node.h"
#include "runtime/length.h"
2014-07-17 23:29:11 -07:00
#include "runtime/tree.h"
#include "runtime/document.h"
2014-07-17 23:29:11 -07:00
2015-08-16 10:51:34 -07:00
TSNode ts_node_make(const TSTree *tree, TSLength offset) {
return (TSNode){.data = tree, .offset = offset };
2014-07-17 23:29:11 -07:00
}
/*
* Private
*/
2015-08-16 10:51:34 -07:00
2015-08-16 10:54:02 -07:00
static inline TSNode ts_node__null() {
return ts_node_make(NULL, ts_length_zero());
}
static inline const TSTree *ts_node__tree(TSNode this) {
return this.data;
2014-07-17 23:29:11 -07:00
}
static inline TSLength ts_node__offset(TSNode this) {
return this.offset;
}
static inline TSNode ts_node__child(TSNode this, size_t child_index,
TSNodeType type) {
2015-08-16 10:51:34 -07:00
const TSTree *tree = ts_node__tree(this);
TSLength position = ts_node__offset(this);
2015-08-16 10:27:26 -07:00
2015-08-14 16:34:12 -07:00
bool did_descend = true;
while (did_descend) {
did_descend = false;
2015-08-14 16:11:37 -07:00
2015-08-14 16:34:12 -07:00
size_t index = 0;
for (size_t i = 0; i < tree->child_count; i++) {
TSTree *child = tree->children[i];
if (child->options.type >= type) {
2015-08-14 16:34:12 -07:00
if (index == child_index)
return ts_node_make(child, position);
index++;
} else {
size_t grandchild_index = child_index - index;
size_t grandchild_count = (type == TSNodeTypeNamed)
? child->named_child_count
: child->visible_child_count;
if (grandchild_index < grandchild_count) {
2015-08-14 16:34:12 -07:00
did_descend = true;
2015-08-16 10:27:26 -07:00
tree = child;
2015-08-14 16:34:12 -07:00
child_index = grandchild_index;
2015-08-16 10:27:26 -07:00
break;
2015-08-14 16:34:12 -07:00
}
index += grandchild_count;
2015-08-14 16:34:12 -07:00
}
position = ts_length_add(position, ts_tree_total_size(child));
2015-08-14 16:11:37 -07:00
}
}
2015-08-16 10:54:02 -07:00
return ts_node__null();
2014-07-17 23:29:11 -07:00
}
static inline TSNode ts_node__prev_sibling(TSNode this, TSNodeType type) {
const TSTree *tree = ts_node__tree(this);
TSLength position = ts_node__offset(this);
do {
size_t index = tree->context.index;
position = ts_length_sub(position, tree->context.offset);
tree = tree->context.parent;
if (!tree)
break;
for (size_t i = index - 1; i + 1 > 0; i--) {
const TSTree *child = tree->children[i];
TSLength child_position = ts_length_add(position, child->context.offset);
if (child->options.type >= type)
return ts_node_make(child, child_position);
size_t grandchild_count = (type == TSNodeTypeNamed)
? child->named_child_count
: child->visible_child_count;
if (grandchild_count > 0)
return ts_node__child(ts_node_make(child, child_position),
grandchild_count - 1, type);
}
} while (!ts_tree_is_visible(tree));
return ts_node__null();
}
static inline TSNode ts_node__next_sibling(TSNode this, TSNodeType type) {
const TSTree *tree = ts_node__tree(this);
TSLength position = ts_node__offset(this);
do {
size_t index = tree->context.index;
position = ts_length_sub(position, tree->context.offset);
tree = tree->context.parent;
if (!tree)
break;
for (size_t i = index + 1; i < tree->child_count; i++) {
const TSTree *child = tree->children[i];
TSLength child_position = ts_length_add(position, child->context.offset);
if (child->options.type >= type)
return ts_node_make(child, child_position);
size_t grandchild_count = (type == TSNodeTypeNamed)
? child->named_child_count
: child->visible_child_count;
if (grandchild_count > 0)
return ts_node__child(ts_node_make(child, child_position), 0, type);
}
} while (!ts_tree_is_visible(tree));
return ts_node__null();
}
static inline TSNode ts_node__descendent_for_range(TSNode this, size_t min,
size_t max, TSNodeType type) {
2015-08-16 10:51:34 -07:00
const TSTree *tree = ts_node__tree(this), *last_visible_tree = tree;
TSLength position = ts_node__offset(this), last_visible_position = position;
2015-08-16 10:27:26 -07:00
bool did_descend = true;
while (did_descend) {
did_descend = false;
2015-08-14 16:34:12 -07:00
2015-08-14 16:11:37 -07:00
for (size_t i = 0; i < tree->child_count; i++) {
const TSTree *child = tree->children[i];
if (position.chars + child->padding.chars > min)
break;
2015-08-14 16:11:37 -07:00
if (position.chars + child->padding.chars + child->size.chars > max) {
2015-08-16 10:27:26 -07:00
tree = child;
if (child->options.type >= type) {
2015-08-16 10:27:26 -07:00
last_visible_tree = tree;
last_visible_position = position;
}
did_descend = true;
2015-08-16 10:27:26 -07:00
break;
}
2015-08-14 16:11:37 -07:00
position = ts_length_add(position, ts_tree_total_size(child));
}
2014-07-20 20:27:33 -07:00
}
2015-08-14 16:34:12 -07:00
2015-08-16 10:27:26 -07:00
return ts_node_make(last_visible_tree, last_visible_position);
2014-07-17 23:29:11 -07:00
}
2014-08-25 09:31:27 -07:00
/*
* Public
*/
TSLength ts_node_pos(TSNode this) {
return ts_length_add(ts_node__offset(this), ts_node__tree(this)->padding);
}
TSLength ts_node_size(TSNode this) {
return ts_node__tree(this)->size;
}
TSSymbol ts_node_symbol(TSNode this) {
return ts_node__tree(this)->symbol;
}
const char *ts_node_name(TSNode this, const TSDocument *document) {
return document->parser.language->symbol_names[ts_node__tree(this)->symbol];
}
const char *ts_node_string(TSNode this, const TSDocument *document) {
return ts_tree_string(ts_node__tree(this),
document->parser.language->symbol_names);
}
bool ts_node_eq(TSNode this, TSNode other) {
return ts_tree_eq(ts_node__tree(this), ts_node__tree(other)) &&
ts_length_eq(ts_node__offset(this), ts_node__offset(other));
}
bool ts_node_is_named(TSNode this) {
return ts_node__tree(this)->options.type == TSNodeTypeNamed;
}
TSNode ts_node_parent(TSNode this) {
const TSTree *tree = ts_node__tree(this);
TSLength position = ts_node__offset(this);
do {
2015-09-09 12:43:35 -07:00
position = ts_length_sub(position, tree->context.offset);
tree = tree->context.parent;
if (!tree)
return ts_node__null();
} while (!ts_tree_is_visible(tree));
return ts_node_make(tree, position);
}
TSNode ts_node_child(TSNode this, size_t child_index) {
return ts_node__child(this, child_index, TSNodeTypeAnonymous);
}
TSNode ts_node_named_child(TSNode this, size_t child_index) {
return ts_node__child(this, child_index, TSNodeTypeNamed);
}
size_t ts_node_child_count(TSNode this) {
return ts_node__tree(this)->visible_child_count;
}
size_t ts_node_named_child_count(TSNode this) {
return ts_node__tree(this)->named_child_count;
}
TSNode ts_node_next_sibling(TSNode this) {
return ts_node__next_sibling(this, TSNodeTypeAnonymous);
}
TSNode ts_node_next_named_sibling(TSNode this) {
return ts_node__next_sibling(this, TSNodeTypeNamed);
}
TSNode ts_node_prev_sibling(TSNode this) {
return ts_node__prev_sibling(this, TSNodeTypeAnonymous);
}
TSNode ts_node_prev_named_sibling(TSNode this) {
return ts_node__prev_sibling(this, TSNodeTypeNamed);
}
TSNode ts_node_descendent_for_range(TSNode this, size_t min, size_t max) {
return ts_node__descendent_for_range(this, min, max, TSNodeTypeAnonymous);
}
TSNode ts_node_named_descendent_for_range(TSNode this, size_t min, size_t max) {
return ts_node__descendent_for_range(this, min, max, TSNodeTypeNamed);
2014-08-25 09:31:27 -07:00
}