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-07-31 15:47:48 -07:00
|
|
|
TSNode ts_node_make(const TSTree *tree, TSLength position) {
|
|
|
|
|
return (TSNode){.data = tree, .position = position };
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
static inline const TSTree *get_tree(TSNode this) {
|
|
|
|
|
return this.data;
|
2014-07-18 13:08:14 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
TSLength ts_node_pos(TSNode this) {
|
|
|
|
|
return this.position;
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
TSLength ts_node_size(TSNode this) {
|
|
|
|
|
return get_tree(this)->size;
|
2015-07-31 15:47:48 -07:00
|
|
|
}
|
2014-07-17 23:29:11 -07:00
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
bool ts_node_eq(TSNode this, TSNode other) {
|
|
|
|
|
return ts_tree_eq(get_tree(this), get_tree(other)) &&
|
|
|
|
|
ts_length_eq(this.position, other.position);
|
2015-07-31 15:47:48 -07:00
|
|
|
}
|
2014-07-17 23:29:11 -07:00
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
const char *ts_node_name(TSNode this, const TSDocument *document) {
|
|
|
|
|
return document->parser.language->symbol_names[get_tree(this)->symbol];
|
2014-07-18 13:24:03 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
const char *ts_node_string(TSNode this, const TSDocument *document) {
|
|
|
|
|
return ts_tree_string(get_tree(this), document->parser.language->symbol_names);
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-31 15:47:48 -07:00
|
|
|
typedef struct {
|
2015-08-12 13:41:22 -07:00
|
|
|
TSNode node;
|
2015-07-31 15:47:48 -07:00
|
|
|
size_t index;
|
2015-08-12 13:41:22 -07:00
|
|
|
} NodeWithIndex;
|
2015-07-31 15:47:48 -07:00
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
static inline NodeWithIndex ts_node_parent_with_index(TSNode this) {
|
|
|
|
|
const TSTree *tree = get_tree(this);
|
2015-07-31 15:47:48 -07:00
|
|
|
size_t index = 0;
|
2015-08-12 13:41:22 -07:00
|
|
|
TSLength position = ts_length_sub(this.position, tree->padding);
|
2015-07-31 15:47:48 -07:00
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
TSTree *parent = tree->parent;
|
|
|
|
|
if (!parent)
|
2015-08-12 13:41:22 -07:00
|
|
|
return (NodeWithIndex){ ts_node_null(), 0 };
|
2015-07-31 15:47:48 -07:00
|
|
|
|
|
|
|
|
for (size_t i = 0; i < parent->child_count; i++) {
|
|
|
|
|
TSTree *child = parent->children[i];
|
|
|
|
|
if (child == tree)
|
|
|
|
|
break;
|
|
|
|
|
if (ts_tree_is_visible(child)) {
|
|
|
|
|
index += 1;
|
|
|
|
|
} else {
|
|
|
|
|
size_t child_count;
|
|
|
|
|
ts_tree_visible_children(child, &child_count);
|
|
|
|
|
index += child_count;
|
|
|
|
|
}
|
|
|
|
|
position = ts_length_sub(position, ts_tree_total_size(child));
|
|
|
|
|
}
|
|
|
|
|
tree = parent;
|
|
|
|
|
} while (!ts_tree_is_visible(tree));
|
|
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
return (NodeWithIndex){
|
2015-07-31 15:47:48 -07:00
|
|
|
ts_node_make(tree, ts_length_add(position, tree->padding)), index
|
|
|
|
|
};
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
TSNode ts_node_parent(TSNode this) {
|
|
|
|
|
return ts_node_parent_with_index(this).node;
|
2015-07-31 15:47:48 -07:00
|
|
|
}
|
2014-07-18 13:24:03 -07:00
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
TSNode ts_node_prev_sibling(TSNode this) {
|
|
|
|
|
NodeWithIndex parent = ts_node_parent_with_index(this);
|
|
|
|
|
if (parent.node.data && parent.index > 0)
|
|
|
|
|
return ts_node_child(parent.node, parent.index - 1);
|
2014-08-25 11:28:09 -07:00
|
|
|
else
|
2015-07-31 15:47:48 -07:00
|
|
|
return ts_node_null();
|
2014-07-18 13:24:03 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
TSNode ts_node_next_sibling(TSNode this) {
|
|
|
|
|
NodeWithIndex parent = ts_node_parent_with_index(this);
|
|
|
|
|
if (parent.node.data)
|
|
|
|
|
return ts_node_child(parent.node, parent.index + 1);
|
2014-08-25 11:28:09 -07:00
|
|
|
else
|
2015-07-31 15:47:48 -07:00
|
|
|
return ts_node_null();
|
2014-07-18 13:24:03 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
size_t ts_node_child_count(TSNode this) {
|
2014-07-20 20:27:33 -07:00
|
|
|
size_t result;
|
2015-08-12 13:41:22 -07:00
|
|
|
ts_tree_visible_children(get_tree(this), &result);
|
2014-07-20 20:27:33 -07:00
|
|
|
return result;
|
2014-07-20 17:57:20 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
TSNode ts_node_child(TSNode this, size_t i) {
|
2014-08-28 13:22:06 -07:00
|
|
|
size_t count;
|
2015-08-12 13:41:22 -07:00
|
|
|
TSTreeChild *children = ts_tree_visible_children(get_tree(this), &count);
|
2014-08-28 13:22:06 -07:00
|
|
|
if (i >= count)
|
2015-07-31 15:47:48 -07:00
|
|
|
return ts_node_null();
|
2015-08-12 13:41:22 -07:00
|
|
|
TSLength position = ts_length_add(this.position, children[i].offset);
|
2015-07-31 15:47:48 -07:00
|
|
|
return ts_node_make(children[i].tree, position);
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
TSNode ts_node_find_for_range(TSNode this, size_t min, size_t max) {
|
|
|
|
|
TSNode node = this;
|
2015-07-31 15:47:48 -07:00
|
|
|
bool did_descend = true;
|
|
|
|
|
while (did_descend) {
|
|
|
|
|
did_descend = false;
|
|
|
|
|
size_t count;
|
2015-08-12 13:41:22 -07:00
|
|
|
TSTreeChild *children = ts_tree_visible_children(get_tree(node), &count);
|
2015-07-31 15:47:48 -07:00
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
|
TSTreeChild child = children[i];
|
2015-08-12 13:41:22 -07:00
|
|
|
TSLength position = ts_length_add(node.position, child.offset);
|
2015-07-31 15:47:48 -07:00
|
|
|
if (position.chars > min)
|
|
|
|
|
break;
|
|
|
|
|
if (position.chars + child.tree->size.chars > max) {
|
2015-08-12 13:41:22 -07:00
|
|
|
node = ts_node_make(child.tree, position);
|
2015-07-31 15:47:48 -07:00
|
|
|
did_descend = true;
|
|
|
|
|
}
|
2014-07-18 18:52:08 -07:00
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2015-08-12 13:41:22 -07:00
|
|
|
return node;
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
2014-08-25 09:31:27 -07:00
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
TSNode ts_node_find_for_pos(TSNode this, size_t position) {
|
|
|
|
|
return ts_node_find_for_range(this, position, position);
|
2014-08-25 09:31:27 -07:00
|
|
|
}
|