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-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
|
|
|
}
|
|
|
|
|
|
2015-08-16 10:51:34 -07:00
|
|
|
static inline const TSTree *ts_node__tree(TSNode this) {
|
2015-08-12 13:41:22 -07:00
|
|
|
return this.data;
|
2014-07-18 13:08:14 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-16 10:51:34 -07:00
|
|
|
static inline TSLength ts_node__offset(TSNode this) {
|
|
|
|
|
return this.offset;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
TSLength ts_node_pos(TSNode this) {
|
2015-08-16 10:51:34 -07:00
|
|
|
return ts_length_add(ts_node__offset(this), ts_node__tree(this)->padding);
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-12 13:41:22 -07:00
|
|
|
TSLength ts_node_size(TSNode this) {
|
2015-08-16 10:51:34 -07:00
|
|
|
return ts_node__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) {
|
2015-08-16 10:51:34 -07:00
|
|
|
return ts_tree_eq(ts_node__tree(this), ts_node__tree(other)) &&
|
|
|
|
|
ts_length_eq(ts_node__offset(this), ts_node__offset(other));
|
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) {
|
2015-08-16 10:51:34 -07:00
|
|
|
return document->parser.language->symbol_names[ts_node__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) {
|
2015-08-16 10:51:34 -07:00
|
|
|
return ts_tree_string(ts_node__tree(this),
|
|
|
|
|
document->parser.language->symbol_names);
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-15 23:35:20 -07:00
|
|
|
TSNode ts_node_parent(TSNode this) {
|
2015-08-16 10:51:34 -07:00
|
|
|
const TSTree *tree = ts_node__tree(this);
|
|
|
|
|
TSLength position = ts_node__offset(this);
|
2015-07-31 15:47:48 -07:00
|
|
|
|
|
|
|
|
do {
|
2015-08-15 23:35:20 -07:00
|
|
|
TSTree *parent = tree->context.parent;
|
2015-07-31 15:47:48 -07:00
|
|
|
if (!parent)
|
2015-08-15 23:35:20 -07:00
|
|
|
return ts_node_null();
|
2015-07-31 15:47:48 -07:00
|
|
|
|
2015-08-16 10:27:26 -07:00
|
|
|
for (size_t i = 0; i < tree->context.index; i++) {
|
2015-07-31 15:47:48 -07:00
|
|
|
TSTree *child = parent->children[i];
|
|
|
|
|
position = ts_length_sub(position, ts_tree_total_size(child));
|
|
|
|
|
}
|
2015-08-14 16:11:37 -07:00
|
|
|
|
2015-07-31 15:47:48 -07:00
|
|
|
tree = parent;
|
|
|
|
|
} while (!ts_tree_is_visible(tree));
|
|
|
|
|
|
2015-08-15 23:35:20 -07:00
|
|
|
return ts_node_make(tree, position);
|
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) {
|
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-15 23:35:20 -07:00
|
|
|
do {
|
|
|
|
|
TSTree *parent = tree->context.parent;
|
|
|
|
|
if (!parent)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
for (size_t i = tree->context.index - 1; i + 1 > 0; i--) {
|
|
|
|
|
const TSTree *child = parent->children[i];
|
|
|
|
|
position = ts_length_sub(position, ts_tree_total_size(child));
|
|
|
|
|
if (ts_tree_is_visible(child))
|
|
|
|
|
return ts_node_make(child, position);
|
|
|
|
|
if (child->visible_child_count > 0)
|
2015-08-16 10:27:26 -07:00
|
|
|
return ts_node_child(ts_node_make(child, position),
|
|
|
|
|
child->visible_child_count - 1);
|
2015-08-15 23:35:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tree = parent;
|
|
|
|
|
} while (!ts_tree_is_visible(tree));
|
|
|
|
|
|
|
|
|
|
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) {
|
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-15 23:35:20 -07:00
|
|
|
do {
|
|
|
|
|
TSTree *parent = tree->context.parent;
|
|
|
|
|
if (!parent)
|
|
|
|
|
break;
|
|
|
|
|
|
2015-08-16 10:27:26 -07:00
|
|
|
const TSTree *prev_child = tree;
|
2015-08-15 23:35:20 -07:00
|
|
|
for (size_t i = tree->context.index + 1; i < parent->child_count; i++) {
|
|
|
|
|
const TSTree *child = parent->children[i];
|
2015-08-16 10:27:26 -07:00
|
|
|
position = ts_length_add(position, ts_tree_total_size(prev_child));
|
2015-08-15 23:35:20 -07:00
|
|
|
if (ts_tree_is_visible(child))
|
|
|
|
|
return ts_node_make(child, position);
|
|
|
|
|
if (child->visible_child_count > 0)
|
|
|
|
|
return ts_node_child(ts_node_make(child, position), 0);
|
2015-08-16 10:27:26 -07:00
|
|
|
prev_child = child;
|
2015-08-15 23:35:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tree = parent;
|
|
|
|
|
} while (!ts_tree_is_visible(tree));
|
|
|
|
|
|
|
|
|
|
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) {
|
2015-08-16 10:51:34 -07:00
|
|
|
return ts_node__tree(this)->visible_child_count;
|
2014-07-20 17:57:20 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-14 16:11:37 -07:00
|
|
|
TSNode ts_node_child(TSNode this, size_t child_index) {
|
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 (ts_tree_is_visible(child)) {
|
|
|
|
|
if (index == child_index)
|
|
|
|
|
return ts_node_make(child, position);
|
|
|
|
|
index++;
|
|
|
|
|
} else {
|
|
|
|
|
size_t grandchild_index = child_index - index;
|
|
|
|
|
if (grandchild_index < child->visible_child_count) {
|
|
|
|
|
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 += child->visible_child_count;
|
|
|
|
|
}
|
|
|
|
|
position = ts_length_add(position, ts_tree_total_size(child));
|
2015-08-14 16:11:37 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ts_node_null();
|
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) {
|
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
|
|
|
|
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];
|
|
|
|
|
if (position.chars + child->padding.chars > min)
|
2015-07-31 15:47:48 -07:00
|
|
|
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 (ts_tree_is_visible(child)) {
|
|
|
|
|
last_visible_tree = tree;
|
|
|
|
|
last_visible_position = position;
|
|
|
|
|
}
|
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-08-14 16:11:37 -07:00
|
|
|
position = ts_length_add(position, ts_tree_total_size(child));
|
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-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
|
|
|
|
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
|
|
|
}
|