Rework API completely
This commit is contained in:
parent
33f7643040
commit
e75ecd1bb1
31 changed files with 841 additions and 1075 deletions
|
|
@ -34,7 +34,7 @@ typedef struct {
|
|||
static Iterator iterator_new(TSTreeCursor *cursor, Subtree *tree, const TSLanguage *language) {
|
||||
array_clear(&cursor->stack);
|
||||
array_push(&cursor->stack, ((TreeCursorEntry){
|
||||
.tree = tree,
|
||||
.subtree = tree,
|
||||
.position = length_zero(),
|
||||
.child_index = 0,
|
||||
.structural_child_index = 0,
|
||||
|
|
@ -56,25 +56,25 @@ Length iterator_start_position(Iterator *self) {
|
|||
if (self->in_padding) {
|
||||
return entry.position;
|
||||
} else {
|
||||
return length_add(entry.position, entry.tree->padding);
|
||||
return length_add(entry.position, entry.subtree->padding);
|
||||
}
|
||||
}
|
||||
|
||||
Length iterator_end_position(Iterator *self) {
|
||||
TreeCursorEntry entry = *array_back(&self->cursor.stack);
|
||||
Length result = length_add(entry.position, entry.tree->padding);
|
||||
Length result = length_add(entry.position, entry.subtree->padding);
|
||||
if (self->in_padding) {
|
||||
return result;
|
||||
} else {
|
||||
return length_add(result, entry.tree->size);
|
||||
return length_add(result, entry.subtree->size);
|
||||
}
|
||||
}
|
||||
|
||||
static bool iterator_tree_is_visible(const Iterator *self) {
|
||||
TreeCursorEntry entry = *array_back(&self->cursor.stack);
|
||||
if (entry.tree->visible) return true;
|
||||
if (entry.subtree->visible) return true;
|
||||
if (self->cursor.stack.size > 1) {
|
||||
Subtree *parent = self->cursor.stack.contents[self->cursor.stack.size - 2].tree;
|
||||
Subtree *parent = self->cursor.stack.contents[self->cursor.stack.size - 2].subtree;
|
||||
const TSSymbol *alias_sequence = ts_language_alias_sequence(self->language, parent->alias_sequence_id);
|
||||
return alias_sequence && alias_sequence[entry.structural_child_index] != 0;
|
||||
}
|
||||
|
|
@ -94,7 +94,7 @@ static void iterator_get_visible_state(const Iterator *self, Subtree **tree,
|
|||
TreeCursorEntry entry = self->cursor.stack.contents[i];
|
||||
|
||||
if (i > 0) {
|
||||
Subtree *parent = self->cursor.stack.contents[i - 1].tree;
|
||||
Subtree *parent = self->cursor.stack.contents[i - 1].subtree;
|
||||
const TSSymbol *alias_sequence = ts_language_alias_sequence(
|
||||
self->language,
|
||||
parent->alias_sequence_id
|
||||
|
|
@ -104,8 +104,8 @@ static void iterator_get_visible_state(const Iterator *self, Subtree **tree,
|
|||
}
|
||||
}
|
||||
|
||||
if (entry.tree->visible || *alias_symbol) {
|
||||
*tree = entry.tree;
|
||||
if (entry.subtree->visible || *alias_symbol) {
|
||||
*tree = entry.subtree;
|
||||
*start_byte = entry.position.bytes;
|
||||
break;
|
||||
}
|
||||
|
|
@ -128,14 +128,14 @@ static bool iterator_descend(Iterator *self, uint32_t goal_position) {
|
|||
TreeCursorEntry entry = *array_back(&self->cursor.stack);
|
||||
Length position = entry.position;
|
||||
uint32_t structural_child_index = 0;
|
||||
for (uint32_t i = 0; i < entry.tree->children.size; i++) {
|
||||
Subtree *child = entry.tree->children.contents[i];
|
||||
for (uint32_t i = 0; i < entry.subtree->children.size; i++) {
|
||||
Subtree *child = entry.subtree->children.contents[i];
|
||||
Length child_left = length_add(position, child->padding);
|
||||
Length child_right = length_add(child_left, child->size);
|
||||
|
||||
if (child_right.bytes > goal_position) {
|
||||
array_push(&self->cursor.stack, ((TreeCursorEntry){
|
||||
.tree = child,
|
||||
.subtree = child,
|
||||
.position = position,
|
||||
.child_index = i,
|
||||
.structural_child_index = structural_child_index,
|
||||
|
|
@ -178,16 +178,16 @@ static void iterator_advance(Iterator *self) {
|
|||
TreeCursorEntry entry = array_pop(&self->cursor.stack);
|
||||
if (iterator_done(self)) return;
|
||||
|
||||
Subtree *parent = array_back(&self->cursor.stack)->tree;
|
||||
Subtree *parent = array_back(&self->cursor.stack)->subtree;
|
||||
uint32_t child_index = entry.child_index + 1;
|
||||
if (parent->children.size > child_index) {
|
||||
Length position = length_add(entry.position, ts_subtree_total_size(entry.tree));
|
||||
Length position = length_add(entry.position, ts_subtree_total_size(entry.subtree));
|
||||
uint32_t structural_child_index = entry.structural_child_index;
|
||||
if (!entry.tree->extra) structural_child_index++;
|
||||
if (!entry.subtree->extra) structural_child_index++;
|
||||
Subtree *next_child = parent->children.contents[child_index];
|
||||
|
||||
array_push(&self->cursor.stack, ((TreeCursorEntry){
|
||||
.tree = next_child,
|
||||
.subtree = next_child,
|
||||
.position = position,
|
||||
.child_index = child_index,
|
||||
.structural_child_index = structural_child_index,
|
||||
|
|
@ -250,7 +250,7 @@ static inline void iterator_print_state(Iterator *self) {
|
|||
TreeCursorEntry entry = *array_back(&self->cursor.stack);
|
||||
TSPoint start = iterator_start_position(self).extent;
|
||||
TSPoint end = iterator_end_position(self).extent;
|
||||
const char *name = ts_language_symbol_name(self->language, entry.tree->symbol);
|
||||
const char *name = ts_language_symbol_name(self->language, entry.subtree->symbol);
|
||||
printf(
|
||||
"(%-25s %s\t depth:%u [%u, %u] - [%u, %u])",
|
||||
name, self->in_padding ? "(p)" : " ",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue