tree-sitter/src/runtime/node.c

280 lines
8.6 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
TSNode ts_node_make(const TSTree *tree, TSLength offset, size_t row) {
return (TSNode){.data = tree, .offset = offset, .row = row };
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(), 0);
2015-08-16 10:54:02 -07:00
}
2015-10-14 21:52:13 -07:00
static inline const TSTree *ts_node__tree(TSNode self) {
return self.data;
2014-07-17 23:29:11 -07:00
}
2015-10-14 21:52:13 -07:00
static inline TSLength ts_node__offset(TSNode self) {
return self.offset;
}
static inline bool ts_tree__is_relevant(const TSTree *tree, bool include_anonymous) {
return include_anonymous ? tree->options.visible : tree->options.named;
}
static inline size_t ts_tree__relevant_child_count(const TSTree *tree,
bool include_anonymous) {
return include_anonymous ? tree->visible_child_count : tree->named_child_count;
}
2015-10-14 21:52:13 -07:00
static inline TSNode ts_node__child(TSNode self, size_t child_index,
bool include_anonymous) {
2015-10-14 21:52:13 -07:00
const TSTree *tree = ts_node__tree(self);
TSLength position = ts_node__offset(self);
size_t offset_row = self.row;
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 (ts_tree__is_relevant(child, include_anonymous)) {
2015-08-14 16:34:12 -07:00
if (index == child_index)
return ts_node_make(child, position, offset_row);
2015-08-14 16:34:12 -07:00
index++;
} else {
size_t grandchild_index = child_index - index;
size_t grandchild_count =
ts_tree__relevant_child_count(child, include_anonymous);
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));
offset_row += child->padding_point.row + child->size_point.row;
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 self, bool include_anonymous) {
2015-10-14 21:52:13 -07:00
const TSTree *tree = ts_node__tree(self);
TSLength offset = ts_node__offset(self);
size_t offset_row = self.row;
do {
size_t index = tree->context.index;
offset = ts_length_sub(offset, tree->context.offset);
offset_row -= tree->context.offset_point.row;
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_offset = ts_length_add(offset, child->context.offset);
size_t child_offset_row = offset_row + child->context.offset_point.row;
if (ts_tree__is_relevant(child, include_anonymous))
return ts_node_make(child, child_offset, child_offset_row);
size_t grandchild_count = ts_tree__relevant_child_count(child, include_anonymous);
if (grandchild_count > 0)
return ts_node__child(ts_node_make(child, child_offset, child_offset_row),
grandchild_count - 1, include_anonymous);
}
} while (!ts_tree_is_visible(tree));
return ts_node__null();
}
static inline TSNode ts_node__next_sibling(TSNode self, bool include_anonymous) {
2015-10-14 21:52:13 -07:00
const TSTree *tree = ts_node__tree(self);
TSLength offset = ts_node__offset(self);
size_t offset_row = self.row;
do {
size_t index = tree->context.index;
offset = ts_length_sub(offset, tree->context.offset);
offset_row -= tree->context.offset_point.row;
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_offset = ts_length_add(offset, child->context.offset);
size_t child_offset_row = offset_row + child->context.offset_point.row;
if (ts_tree__is_relevant(child, include_anonymous))
return ts_node_make(child, child_offset, child_offset_row);
size_t grandchild_count = ts_tree__relevant_child_count(child, include_anonymous);
if (grandchild_count > 0)
return ts_node__child(ts_node_make(child, child_offset, child_offset_row), 0,
include_anonymous);
}
} while (!ts_tree_is_visible(tree));
return ts_node__null();
}
static inline TSNode ts_node__descendant_for_range(TSNode self, size_t min,
size_t max,
bool include_anonymous) {
2015-10-14 21:52:13 -07:00
const TSTree *tree = ts_node__tree(self), *last_visible_tree = tree;
TSLength offset = ts_node__offset(self), last_visible_position = offset;
size_t offset_row = self.row, last_visible_row = offset_row;
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 (offset.chars + child->padding.chars > min)
break;
if (offset.chars + child->padding.chars + child->size.chars > max) {
2015-08-16 10:27:26 -07:00
tree = child;
if (ts_tree__is_relevant(child, include_anonymous)) {
2015-08-16 10:27:26 -07:00
last_visible_tree = tree;
last_visible_position = offset;
last_visible_row = offset_row;
2015-08-16 10:27:26 -07:00
}
did_descend = true;
2015-08-16 10:27:26 -07:00
break;
}
offset = ts_length_add(offset, ts_tree_total_size(child));
offset_row += child->padding_point.row + child->size_point.row;
}
2014-07-20 20:27:33 -07:00
}
2015-08-14 16:34:12 -07:00
return ts_node_make(last_visible_tree, last_visible_position, last_visible_row);
2014-07-17 23:29:11 -07:00
}
2014-08-25 09:31:27 -07:00
/*
* Public
*/
size_t ts_node_start_char(TSNode self) {
return ts_node__offset(self).chars + ts_node__tree(self)->padding.chars;
}
size_t ts_node_end_char(TSNode self) {
return ts_node_start_char(self) + ts_node__tree(self)->size.chars;
}
size_t ts_node_start_byte(TSNode self) {
return ts_node__offset(self).bytes + ts_node__tree(self)->padding.bytes;
}
size_t ts_node_end_byte(TSNode self) {
return ts_node_start_byte(self) + ts_node__tree(self)->size.bytes;
}
2015-11-18 16:47:27 -08:00
TSPoint ts_node_size_point(TSNode self) {
2015-11-18 16:45:10 -08:00
return ts_node__tree(self)->size_point;
}
TSPoint ts_node_start_point(TSNode self) {
2015-12-02 16:45:12 -05:00
const TSTree *tree = ts_node__tree(self);
return ts_point_make(self.row + tree->padding_point.row, ts_tree_offset_column(tree));
}
TSPoint ts_node_end_point(TSNode self) {
return ts_point_add(ts_node_start_point(self), ts_node_size_point(self));
}
2015-10-14 21:52:13 -07:00
TSSymbol ts_node_symbol(TSNode self) {
return ts_node__tree(self)->symbol;
}
2015-10-14 21:52:13 -07:00
const char *ts_node_name(TSNode self, const TSDocument *document) {
return document->parser.language->symbol_names[ts_node__tree(self)->symbol];
}
2015-10-14 21:52:13 -07:00
const char *ts_node_string(TSNode self, const TSDocument *document) {
return ts_tree_string(ts_node__tree(self),
document->parser.language->symbol_names, false);
}
2015-10-14 21:52:13 -07:00
bool ts_node_eq(TSNode self, TSNode other) {
return ts_tree_eq(ts_node__tree(self), ts_node__tree(other)) &&
2015-12-02 16:45:20 -05:00
ts_length_eq(ts_node__offset(self), ts_node__offset(other)) &&
self.row == other.row;
}
2015-10-14 21:52:13 -07:00
bool ts_node_is_named(TSNode self) {
return ts_node__tree(self)->options.named;
}
2015-10-14 21:52:13 -07:00
bool ts_node_has_changes(TSNode self) {
return ts_node__tree(self)->options.has_changes;
}
2015-10-14 21:52:13 -07:00
TSNode ts_node_parent(TSNode self) {
const TSTree *tree = ts_node__tree(self);
TSLength offset = ts_node__offset(self);
size_t offset_row = self.row;
do {
offset = ts_length_sub(offset, tree->context.offset);
offset_row -= tree->context.offset_point.row;
2015-09-09 12:43:35 -07:00
tree = tree->context.parent;
if (!tree)
return ts_node__null();
} while (!ts_tree_is_visible(tree));
return ts_node_make(tree, offset, offset_row);
}
2015-10-14 21:52:13 -07:00
TSNode ts_node_child(TSNode self, size_t child_index) {
return ts_node__child(self, child_index, true);
}
2015-10-14 21:52:13 -07:00
TSNode ts_node_named_child(TSNode self, size_t child_index) {
return ts_node__child(self, child_index, false);
}
2015-10-14 21:52:13 -07:00
size_t ts_node_child_count(TSNode self) {
return ts_node__tree(self)->visible_child_count;
}
2015-10-14 21:52:13 -07:00
size_t ts_node_named_child_count(TSNode self) {
return ts_node__tree(self)->named_child_count;
}
2015-10-14 21:52:13 -07:00
TSNode ts_node_next_sibling(TSNode self) {
return ts_node__next_sibling(self, true);
}
2015-10-14 21:52:13 -07:00
TSNode ts_node_next_named_sibling(TSNode self) {
return ts_node__next_sibling(self, false);
}
2015-10-14 21:52:13 -07:00
TSNode ts_node_prev_sibling(TSNode self) {
return ts_node__prev_sibling(self, true);
}
2015-10-14 21:52:13 -07:00
TSNode ts_node_prev_named_sibling(TSNode self) {
return ts_node__prev_sibling(self, false);
}
TSNode ts_node_descendant_for_range(TSNode self, size_t min, size_t max) {
return ts_node__descendant_for_range(self, min, max, true);
}
TSNode ts_node_named_descendant_for_range(TSNode self, size_t min, size_t max) {
return ts_node__descendant_for_range(self, min, max, false);
2014-08-25 09:31:27 -07:00
}