2017-01-05 10:06:43 -08:00
|
|
|
#include "runtime/tree.h"
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
Tree *tree;
|
|
|
|
|
uint32_t byte_index;
|
2017-08-30 17:35:12 -07:00
|
|
|
Tree *last_external_token;
|
2017-01-05 10:06:43 -08:00
|
|
|
} ReusableNode;
|
|
|
|
|
|
|
|
|
|
static inline ReusableNode reusable_node_new(Tree *tree) {
|
2017-08-08 10:47:59 -07:00
|
|
|
ReusableNode result = {tree, 0, NULL};
|
|
|
|
|
return result;
|
2017-01-05 10:06:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void reusable_node_pop(ReusableNode *self) {
|
|
|
|
|
self->byte_index += ts_tree_total_bytes(self->tree);
|
|
|
|
|
if (self->tree->has_external_tokens) {
|
2017-08-30 17:35:12 -07:00
|
|
|
self->last_external_token = ts_tree_last_external_token(self->tree);
|
2017-01-05 10:06:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (self->tree) {
|
|
|
|
|
Tree *parent = self->tree->context.parent;
|
|
|
|
|
uint32_t next_index = self->tree->context.index + 1;
|
|
|
|
|
if (parent && parent->child_count > next_index) {
|
|
|
|
|
self->tree = parent->children[next_index];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self->tree = parent;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void reusable_node_pop_leaf(ReusableNode *self) {
|
|
|
|
|
while (self->tree->child_count > 0)
|
|
|
|
|
self->tree = self->tree->children[0];
|
|
|
|
|
reusable_node_pop(self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline bool reusable_node_breakdown(ReusableNode *self) {
|
|
|
|
|
if (self->tree->child_count == 0) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
self->tree = self->tree->children[0];
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|