diff --git a/include/tree_sitter/parser.h b/include/tree_sitter/parser.h index d25becb4..63cdd2d1 100644 --- a/include/tree_sitter/parser.h +++ b/include/tree_sitter/parser.h @@ -283,7 +283,7 @@ static size_t ts_lr_parser_breakdown_stack(ts_lr_parser *parser, ts_input_edit * if (!node) break; position = ts_stack_right_position(stack); - ts_tree **children = ts_tree_children(node, &child_count); + ts_tree **children = ts_tree_immediate_children(node, &child_count); if (position <= edit->position && !children) break; stack->size--; diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index 97568ad4..aa007e6e 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -13,7 +13,7 @@ static const ts_symbol ts_builtin_sym_end = -2; typedef struct ts_tree ts_tree; ts_tree * ts_tree_make_leaf(ts_symbol symbol, size_t size, size_t offset); -ts_tree * ts_tree_make_node(ts_symbol symbol, size_t child_count, ts_tree **children, size_t size, size_t offset); +ts_tree * ts_tree_make_node(ts_symbol symbol, size_t child_count, size_t immediate_child_count, ts_tree **children, size_t size, size_t offset); ts_tree * ts_tree_make_error(char lookahead_char, size_t expected_input_count, const ts_symbol *expected_inputs, size_t size, size_t offset); void ts_tree_retain(ts_tree *tree); void ts_tree_release(ts_tree *tree); @@ -21,11 +21,11 @@ int ts_tree_equals(const ts_tree *tree1, const ts_tree *tree2); char * ts_tree_string(const ts_tree *tree, const char **names); char * ts_tree_error_string(const ts_tree *tree, const char **names); ts_tree ** ts_tree_children(const ts_tree *tree, size_t *count); +ts_tree ** ts_tree_immediate_children(const ts_tree *tree, size_t *count); size_t ts_tree_size(const ts_tree *tree); size_t ts_tree_offset(const ts_tree *tree); size_t ts_tree_total_size(const ts_tree *tree); ts_symbol ts_tree_symbol(const ts_tree *tree); -void ts_tree_hide(ts_tree *tree, int hide); typedef struct { void *data; diff --git a/spec/runtime/node_position_spec.cc b/spec/runtime/node_position_spec.cc index 6155121b..463c345e 100644 --- a/spec/runtime/node_position_spec.cc +++ b/spec/runtime/node_position_spec.cc @@ -21,12 +21,12 @@ describe("tracking the positions of AST nodes", []() { const ts_tree *tree = ts_document_tree(doc); const ts_tree *array = ts_tree_children(tree, NULL)[0]; - const ts_tree *number1 = ts_tree_children(ts_tree_children(array, NULL)[1], NULL)[0]; - const ts_tree *number2 = ts_tree_children(ts_tree_children(ts_tree_children(array, NULL)[2], NULL)[1], NULL)[0]; + const ts_tree *number1 = ts_tree_children(array, NULL)[0]; + const ts_tree *number2 = ts_tree_children(array, NULL)[1]; AssertThat(ts_document_symbol_name(doc, array), Equals("array")); - AssertThat(ts_document_symbol_name(doc, number1), Equals("number")); - AssertThat(ts_document_symbol_name(doc, number2), Equals("number")); + AssertThat(ts_document_symbol_name(doc, number1), Equals("value")); + AssertThat(ts_document_symbol_name(doc, number2), Equals("value")); AssertThat(ts_tree_offset(number1), Equals(0)); AssertThat(ts_tree_size(number1), Equals(2)); diff --git a/spec/runtime/tree_spec.cc b/spec/runtime/tree_spec.cc index 1ef26c0b..91656d67 100644 --- a/spec/runtime/tree_spec.cc +++ b/spec/runtime/tree_spec.cc @@ -17,7 +17,7 @@ describe("trees", []() { before_each([&]() { tree1 = ts_tree_make_leaf(cat, 0, 0); - parent1 = ts_tree_make_node(dog, 1, tree_array({ tree1 }), 0, 0); + parent1 = ts_tree_make_node(dog, 1, 1, tree_array({ tree1, tree1 }), 0, 0); }); after_each([&]() { @@ -30,7 +30,7 @@ describe("trees", []() { ts_tree *tree2 = ts_tree_make_leaf(cat, 0, 0); AssertThat(ts_tree_equals(tree1, tree2), Equals(1)); - ts_tree *parent2 = ts_tree_make_node(dog, 1, tree_array({ tree2 }), 0, 0); + ts_tree *parent2 = ts_tree_make_node(dog, 1, 1, tree_array({ tree2, tree2 }), 0, 0); AssertThat(ts_tree_equals(parent1, parent2), Equals(1)); ts_tree_release(tree2); @@ -45,7 +45,7 @@ describe("trees", []() { it("returns false for trees with different children", [&]() { ts_tree *tree2 = ts_tree_make_leaf(pig, 0, 0); - ts_tree *parent2 = ts_tree_make_node(dog, 1, tree_array({ tree2 }), 0, 0); + ts_tree *parent2 = ts_tree_make_node(dog, 1, 1, tree_array({ tree2, tree2 }), 0, 0); AssertThat(ts_tree_equals(parent2, parent1), Equals(0)); AssertThat(ts_tree_equals(parent1, parent2), Equals(0)); ts_tree_release(tree2); diff --git a/src/runtime/stack.c b/src/runtime/stack.c index fe509cdb..81bf8aee 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -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; } diff --git a/src/runtime/tree.c b/src/runtime/tree.c index 8d28c3ad..98118621 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -7,7 +7,6 @@ struct ts_tree { size_t ref_count; size_t offset; size_t size; - int is_hidden; union { struct { size_t count; @@ -26,7 +25,6 @@ static ts_tree * ts_tree_make(ts_symbol symbol, size_t size, size_t offset) { ts_tree *result = malloc(sizeof(ts_tree)); *result = (ts_tree) { .ref_count = 1, - .is_hidden = 0, .symbol = symbol, .size = size, .offset = offset, @@ -37,15 +35,18 @@ static ts_tree * ts_tree_make(ts_symbol symbol, size_t size, size_t offset) { ts_tree * ts_tree_make_leaf(ts_symbol symbol, size_t size, size_t offset) { ts_tree *result = ts_tree_make(symbol, size, offset); result->data.children.count = 0; + result->data.children.immediate_count = 0; result->data.children.contents = NULL; return result; } -ts_tree * ts_tree_make_node(ts_symbol symbol, size_t child_count, ts_tree **children, size_t size, size_t offset) { - for (size_t i = 0; i < child_count; i++) - ts_tree_retain(children[i]); +ts_tree * ts_tree_make_node(ts_symbol symbol, size_t child_count, size_t immediate_child_count, ts_tree **children, size_t size, size_t offset) { + ts_tree **immediate_children = children + child_count; + for (size_t i = 0; i < immediate_child_count; i++) + ts_tree_retain(immediate_children[i]); ts_tree *result = ts_tree_make(symbol, size, offset); result->data.children.count = child_count; + result->data.children.immediate_count = immediate_child_count; result->data.children.contents = children; return result; } @@ -78,18 +79,23 @@ size_t ts_tree_total_size(const ts_tree *tree) { return ts_tree_offset(tree) + ts_tree_size(tree); } -void ts_tree_hide(ts_tree *tree, int hide) { - tree->is_hidden = hide; +ts_tree ** ts_tree_immediate_children(const ts_tree *tree, size_t *count) { + if (!tree || tree->symbol == ts_builtin_sym_error) { + if (count) *count = 0; + return NULL; + } + if (count) *count = tree->data.children.immediate_count; + return tree->data.children.contents + tree->data.children.count; } void ts_tree_release(ts_tree *tree) { tree->ref_count--; if (tree->ref_count == 0) { size_t count; - ts_tree **children = ts_tree_children(tree, &count); + ts_tree **children = ts_tree_immediate_children(tree, &count); for (size_t i = 0; i < count; i++) ts_tree_release(children[i]); - free(children); + free(tree->data.children.contents); free(tree); } } @@ -99,14 +105,12 @@ int ts_tree_equals(const ts_tree *node1, const ts_tree *node2) { if (node1->symbol == ts_builtin_sym_error) { // check error equality } else { - if (node1->data.children.count != node2->data.children.count) - return 0; - for (size_t i = 0; i < node1->data.children.count; i++) { - ts_tree *child1 = node1->data.children.contents[i]; - ts_tree *child2 = node2->data.children.contents[i]; - if (!ts_tree_equals(child1, child2)) - return 0; - } + size_t count1, count2; + ts_tree **children1 = ts_tree_children(node1, &count1); + ts_tree **children2 = ts_tree_children(node2, &count2); + if (count1 != count2) return 0; + for (size_t i = 0; i < count1; i++) + if (!ts_tree_equals(children1[i], children2[i])) return 0; } return 1; } @@ -120,40 +124,32 @@ ts_tree ** ts_tree_children(const ts_tree *tree, size_t *count) { return tree->data.children.contents; } -static size_t tree_write_to_string(const ts_tree *tree, const char **symbol_names, char *string, size_t limit, int is_beginning) { +static size_t tree_write_to_string(const ts_tree *tree, const char **symbol_names, char *string, size_t limit) { char *cursor = string; char **destination = (limit > 0) ? &cursor : &string; if (!tree) return snprintf(*destination, limit, "(NULL)"); + if (tree->symbol == ts_builtin_sym_error) + return snprintf(*destination, limit, "(ERROR)"); - if (!tree->is_hidden && !is_beginning) + cursor += snprintf(*destination, limit, "(%s", symbol_names[tree->symbol]); + size_t count; + ts_tree **children = ts_tree_children(tree, &count); + for (size_t i = 0; i < count; i++) { cursor += snprintf(*destination, limit, " "); - - if (tree->symbol == ts_builtin_sym_error) { - cursor += snprintf(*destination, limit, "(ERROR)"); - return cursor - string; + cursor += tree_write_to_string(children[i], symbol_names, *destination, limit); } - - if (!tree->is_hidden) - cursor += snprintf(*destination, limit, "(%s", symbol_names[tree->symbol]); - - for (size_t i = 0; i < tree->data.children.count; i++) { - ts_tree *child = tree->data.children.contents[i]; - cursor += tree_write_to_string(child, symbol_names, *destination, limit, 0); - } - - if (!tree->is_hidden) - cursor += snprintf(*destination, limit, ")"); - + cursor += snprintf(*destination, limit, ")"); + return cursor - string; } char * ts_tree_string(const ts_tree *tree, const char **symbol_names) { - static char SCRATCH_STRING[100]; - size_t size = tree_write_to_string(tree, symbol_names, SCRATCH_STRING, 0, 1) + 1; + static char SCRATCH_STRING[1]; + size_t size = tree_write_to_string(tree, symbol_names, SCRATCH_STRING, 0) + 1; char *result = malloc(size * sizeof(char)); - tree_write_to_string(tree, symbol_names, result, size, 1); + tree_write_to_string(tree, symbol_names, result, size); return result; }