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"
|
2015-07-31 15:47:48 -07:00
|
|
|
#include "runtime/document.h"
|
2014-07-17 23:29:11 -07:00
|
|
|
|
2015-11-25 13:36:19 -05: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
|
|
|
}
|
|
|
|
|
|
2015-09-08 23:16:24 -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() {
|
2015-11-25 13:36:19 -05:00
|
|
|
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;
|
2015-07-31 15:47:48 -07:00
|
|
|
}
|
2014-07-18 13:24:03 -07:00
|
|
|
|
2015-11-22 13:32:20 -08:00
|
|
|
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-11-18 16:34:50 -08:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static inline TSNode ts_node__child(TSNode self, size_t child_index,
|
2015-11-22 13:32:20 -08:00
|
|
|
bool include_anonymous) {
|
2015-10-14 21:52:13 -07:00
|
|
|
const TSTree *tree = ts_node__tree(self);
|
|
|
|
|
TSLength position = ts_node__offset(self);
|
2015-11-25 13:36:19 -05:00
|
|
|
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];
|
2015-11-22 13:32:20 -08:00
|
|
|
if (ts_tree__is_relevant(child, include_anonymous)) {
|
2015-08-14 16:34:12 -07:00
|
|
|
if (index == child_index)
|
2015-11-25 13:36:19 -05:00
|
|
|
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;
|
2015-11-22 13:32:20 -08:00
|
|
|
size_t grandchild_count =
|
|
|
|
|
ts_tree__relevant_child_count(child, include_anonymous);
|
2015-09-07 21:11:37 -07:00
|
|
|
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
|
|
|
}
|
2015-09-07 21:11:37 -07:00
|
|
|
index += grandchild_count;
|
2015-08-14 16:34:12 -07:00
|
|
|
}
|
|
|
|
|
position = ts_length_add(position, ts_tree_total_size(child));
|
2015-11-25 13:36:19 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2015-11-22 13:32:20 -08: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);
|
2015-11-18 17:53:38 -08:00
|
|
|
TSLength offset = ts_node__offset(self);
|
2015-11-25 13:36:19 -05:00
|
|
|
size_t offset_row = self.row;
|
2015-09-08 21:43:37 -07:00
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
size_t index = tree->context.index;
|
2015-11-18 17:53:38 -08:00
|
|
|
offset = ts_length_sub(offset, tree->context.offset);
|
2015-11-25 13:36:19 -05:00
|
|
|
offset_row -= tree->context.offset_point.row;
|
2015-09-08 21:43:37 -07:00
|
|
|
tree = tree->context.parent;
|
|
|
|
|
if (!tree)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
for (size_t i = index - 1; i + 1 > 0; i--) {
|
|
|
|
|
const TSTree *child = tree->children[i];
|
2015-11-18 17:53:38 -08:00
|
|
|
TSLength child_offset = ts_length_add(offset, child->context.offset);
|
2015-11-25 13:36:19 -05:00
|
|
|
size_t child_offset_row = offset_row + child->context.offset_point.row;
|
2015-11-22 13:32:20 -08:00
|
|
|
if (ts_tree__is_relevant(child, include_anonymous))
|
2015-11-25 13:36:19 -05:00
|
|
|
return ts_node_make(child, child_offset, child_offset_row);
|
2015-11-22 13:32:20 -08:00
|
|
|
size_t grandchild_count = ts_tree__relevant_child_count(child, include_anonymous);
|
2015-09-08 21:43:37 -07:00
|
|
|
if (grandchild_count > 0)
|
2015-11-25 13:36:19 -05:00
|
|
|
return ts_node__child(ts_node_make(child, child_offset, child_offset_row),
|
2015-11-22 13:32:20 -08:00
|
|
|
grandchild_count - 1, include_anonymous);
|
2015-09-08 21:43:37 -07:00
|
|
|
}
|
|
|
|
|
} while (!ts_tree_is_visible(tree));
|
|
|
|
|
|
|
|
|
|
return ts_node__null();
|
2015-09-07 21:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
2015-11-22 13:32:20 -08:00
|
|
|
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);
|
2015-11-18 17:53:38 -08:00
|
|
|
TSLength offset = ts_node__offset(self);
|
2015-11-25 13:36:19 -05:00
|
|
|
size_t offset_row = self.row;
|
2015-09-08 21:43:37 -07:00
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
size_t index = tree->context.index;
|
2015-11-18 17:53:38 -08:00
|
|
|
offset = ts_length_sub(offset, tree->context.offset);
|
2015-11-25 13:36:19 -05:00
|
|
|
offset_row -= tree->context.offset_point.row;
|
2015-09-08 21:43:37 -07:00
|
|
|
tree = tree->context.parent;
|
|
|
|
|
if (!tree)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
for (size_t i = index + 1; i < tree->child_count; i++) {
|
|
|
|
|
const TSTree *child = tree->children[i];
|
2015-11-18 17:53:38 -08:00
|
|
|
TSLength child_offset = ts_length_add(offset, child->context.offset);
|
2015-11-25 13:36:19 -05:00
|
|
|
size_t child_offset_row = offset_row + child->context.offset_point.row;
|
2015-11-22 13:32:20 -08:00
|
|
|
if (ts_tree__is_relevant(child, include_anonymous))
|
2015-11-25 13:36:19 -05:00
|
|
|
return ts_node_make(child, child_offset, child_offset_row);
|
2015-11-22 13:32:20 -08:00
|
|
|
size_t grandchild_count = ts_tree__relevant_child_count(child, include_anonymous);
|
2015-09-08 21:43:37 -07:00
|
|
|
if (grandchild_count > 0)
|
2015-11-22 13:32:20 -08:00
|
|
|
return ts_node__child(ts_node_make(child, child_offset, child_offset_row), 0,
|
|
|
|
|
include_anonymous);
|
2015-09-08 21:43:37 -07:00
|
|
|
}
|
|
|
|
|
} while (!ts_tree_is_visible(tree));
|
|
|
|
|
|
|
|
|
|
return ts_node__null();
|
2015-09-07 21:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
2015-12-03 22:59:27 -08:00
|
|
|
static inline TSNode ts_node__descendant_for_range(TSNode self, size_t min,
|
2015-11-22 13:32:20 -08:00
|
|
|
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;
|
2015-11-22 13:32:20 -08:00
|
|
|
TSLength offset = ts_node__offset(self), last_visible_position = offset;
|
2015-11-25 13:36:19 -05:00
|
|
|
size_t offset_row = self.row, last_visible_row = offset_row;
|
2015-08-16 10:27:26 -07:00
|
|
|
|
2015-07-31 15:47:48 -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];
|
2015-11-22 13:32:20 -08:00
|
|
|
if (offset.chars + child->padding.chars > min)
|
2015-07-31 15:47:48 -07:00
|
|
|
break;
|
2015-11-22 13:32:20 -08:00
|
|
|
if (offset.chars + child->padding.chars + child->size.chars > max) {
|
2015-08-16 10:27:26 -07:00
|
|
|
tree = child;
|
2015-11-22 13:32:20 -08:00
|
|
|
if (ts_tree__is_relevant(child, include_anonymous)) {
|
2015-08-16 10:27:26 -07:00
|
|
|
last_visible_tree = tree;
|
2015-11-22 13:32:20 -08:00
|
|
|
last_visible_position = offset;
|
2015-11-25 13:36:19 -05:00
|
|
|
last_visible_row = offset_row;
|
2015-08-16 10:27:26 -07:00
|
|
|
}
|
2015-07-31 15:47:48 -07:00
|
|
|
did_descend = true;
|
2015-08-16 10:27:26 -07:00
|
|
|
break;
|
2015-07-31 15:47:48 -07:00
|
|
|
}
|
2015-11-22 13:32:20 -08:00
|
|
|
offset = ts_length_add(offset, ts_tree_total_size(child));
|
2015-11-25 13:36:19 -05:00
|
|
|
offset_row += child->padding_point.row + child->size_point.row;
|
2014-07-18 18:52:08 -07:00
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2015-08-14 16:34:12 -07:00
|
|
|
|
2015-11-25 13:36:19 -05: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
|
|
|
|
2015-09-08 23:16:24 -07:00
|
|
|
/*
|
|
|
|
|
* Public
|
|
|
|
|
*/
|
|
|
|
|
|
2015-12-03 22:59:27 -08:00
|
|
|
size_t ts_node_start_char(TSNode self) {
|
|
|
|
|
return ts_node__offset(self).chars + ts_node__tree(self)->padding.chars;
|
2015-09-08 21:43:37 -07:00
|
|
|
}
|
|
|
|
|
|
2015-12-03 22:59:27 -08:00
|
|
|
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-09-08 21:43:37 -07:00
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-18 17:53:38 -08:00
|
|
|
TSPoint ts_node_start_point(TSNode self) {
|
2015-12-02 16:45:12 -05:00
|
|
|
const TSTree *tree = ts_node__tree(self);
|
2015-12-02 16:41:16 -05:00
|
|
|
return ts_point_make(self.row + tree->padding_point.row, ts_tree_offset_column(tree));
|
2015-11-18 17:53:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-09-08 21:43:37 -07:00
|
|
|
}
|
|
|
|
|
|
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-09-08 21:43:37 -07:00
|
|
|
}
|
|
|
|
|
|
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),
|
2015-10-28 12:09:28 -07:00
|
|
|
document->parser.language->symbol_names, false);
|
2015-09-08 23:16:24 -07:00
|
|
|
}
|
|
|
|
|
|
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-09-08 23:16:24 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
bool ts_node_is_named(TSNode self) {
|
2015-11-22 13:32:20 -08:00
|
|
|
return ts_node__tree(self)->options.named;
|
2015-09-08 23:16:24 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
bool ts_node_has_changes(TSNode self) {
|
|
|
|
|
return ts_node__tree(self)->options.has_changes;
|
2015-09-18 23:20:06 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
TSNode ts_node_parent(TSNode self) {
|
|
|
|
|
const TSTree *tree = ts_node__tree(self);
|
2015-11-22 13:32:20 -08:00
|
|
|
TSLength offset = ts_node__offset(self);
|
2015-11-25 13:36:19 -05:00
|
|
|
size_t offset_row = self.row;
|
2015-09-08 23:16:24 -07:00
|
|
|
|
|
|
|
|
do {
|
2015-11-22 13:32:20 -08:00
|
|
|
offset = ts_length_sub(offset, tree->context.offset);
|
2015-11-25 13:36:19 -05:00
|
|
|
offset_row -= tree->context.offset_point.row;
|
2015-11-18 16:34:50 -08:00
|
|
|
|
2015-09-09 12:43:35 -07:00
|
|
|
tree = tree->context.parent;
|
|
|
|
|
if (!tree)
|
2015-09-08 23:16:24 -07:00
|
|
|
return ts_node__null();
|
|
|
|
|
} while (!ts_tree_is_visible(tree));
|
|
|
|
|
|
2015-11-22 13:32:20 -08:00
|
|
|
return ts_node_make(tree, offset, offset_row);
|
2015-09-08 23:16:24 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
TSNode ts_node_child(TSNode self, size_t child_index) {
|
2015-11-22 13:32:20 -08:00
|
|
|
return ts_node__child(self, child_index, true);
|
2015-09-08 21:43:37 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
TSNode ts_node_named_child(TSNode self, size_t child_index) {
|
2015-11-22 13:32:20 -08:00
|
|
|
return ts_node__child(self, child_index, false);
|
2015-09-08 21:43:37 -07:00
|
|
|
}
|
|
|
|
|
|
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-09-08 23:16:24 -07:00
|
|
|
}
|
|
|
|
|
|
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-09-08 21:43:37 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
TSNode ts_node_next_sibling(TSNode self) {
|
2015-11-22 13:32:20 -08:00
|
|
|
return ts_node__next_sibling(self, true);
|
2015-09-08 21:43:37 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
TSNode ts_node_next_named_sibling(TSNode self) {
|
2015-11-22 13:32:20 -08:00
|
|
|
return ts_node__next_sibling(self, false);
|
2015-09-08 23:16:24 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
TSNode ts_node_prev_sibling(TSNode self) {
|
2015-11-22 13:32:20 -08:00
|
|
|
return ts_node__prev_sibling(self, true);
|
2015-09-08 21:43:37 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
TSNode ts_node_prev_named_sibling(TSNode self) {
|
2015-11-22 13:32:20 -08:00
|
|
|
return ts_node__prev_sibling(self, false);
|
2015-09-08 21:43:37 -07:00
|
|
|
}
|
|
|
|
|
|
2015-12-03 22:59:27 -08:00
|
|
|
TSNode ts_node_descendant_for_range(TSNode self, size_t min, size_t max) {
|
|
|
|
|
return ts_node__descendant_for_range(self, min, max, true);
|
2015-09-08 21:43:37 -07:00
|
|
|
}
|
|
|
|
|
|
2015-12-03 22:59:27 -08:00
|
|
|
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
|
|
|
}
|