Store AST nodes' non-hidden children

This commit is contained in:
Max Brunsfeld 2014-03-24 00:36:47 -07:00
parent 95188d84b6
commit 3a7c4bb5b1
6 changed files with 77 additions and 57 deletions

View file

@ -58,25 +58,49 @@ size_t ts_stack_right_position(const ts_stack *stack) {
return result;
}
ts_tree * ts_stack_reduce(ts_stack *stack, ts_symbol symbol, int child_count, const int *collapse_flags) {
size_t new_stack_size = stack->size - child_count;
ts_tree * ts_stack_reduce(ts_stack *stack, ts_symbol symbol, int immediate_child_count, const int *collapse_flags) {
size_t new_stack_size = stack->size - immediate_child_count;
size_t size = 0, offset = 0;
ts_tree **children = malloc(child_count * sizeof(ts_tree *));
for (int i = 0; i < child_count; i++) {
int child_count = 0;
for (int i = 0; i < immediate_child_count; i++) {
ts_tree *child = stack->entries[new_stack_size + i].node;
ts_tree_hide(child, collapse_flags[i]);
if (i == 0) {
offset = ts_tree_offset(child);
size = ts_tree_size(child);
} else {
size += ts_tree_total_size(child);
size += ts_tree_offset(child) + ts_tree_size(child);
}
children[i] = child;
if (collapse_flags[i]) {
size_t grandchild_count;
ts_tree_children(child, &grandchild_count);
child_count += grandchild_count;
} else {
child_count++;
}
}
ts_tree *lookahead = ts_tree_make_node(symbol, child_count, children, size, offset);
size_t child_index = 0;
ts_tree **children = malloc((child_count + immediate_child_count) * sizeof(ts_tree *));
ts_tree **immediate_children = children + child_count;
for (int i = 0; i < immediate_child_count; i++) {
ts_tree *child = stack->entries[new_stack_size + i].node;
immediate_children[i] = child;
if (collapse_flags[i]) {
size_t grandchild_count;
ts_tree ** grandchildren = ts_tree_children(child, &grandchild_count);
memcpy(children + child_index, grandchildren, (grandchild_count * sizeof(ts_tree *)));
child_index += grandchild_count;
} else {
children[child_index] = child;
child_index++;
}
}
ts_tree *lookahead = ts_tree_make_node(symbol, child_count, immediate_child_count, children, size, offset);
ts_stack_shrink(stack, new_stack_size);
return lookahead;
}