Fix logic for inserting leading & trailing extras into root node on acceptance

This commit is contained in:
Max Brunsfeld 2016-06-26 11:57:42 -07:00
parent 9972709e43
commit 6fd3edceae
2 changed files with 27 additions and 20 deletions

View file

@ -152,6 +152,8 @@ describe("The Corpus", []() {
ts_document_edit(document, input->replace(edit_position, 0, inserted_text));
ts_document_parse(document);
expect_a_consistent_tree(ts_document_root_node(document), document);
ts_document_edit(document, input->undo());
ts_document_parse(document);
});
@ -164,6 +166,8 @@ describe("The Corpus", []() {
ts_document_edit(document, input->replace(edit_position, deletion_size, ""));
ts_document_parse(document);
expect_a_consistent_tree(ts_document_root_node(document), document);
ts_document_edit(document, input->undo());
ts_document_parse(document);
});

View file

@ -788,29 +788,32 @@ static bool ts_parser__accept(TSParser *self, StackVersion version) {
StackSlice slice = pop.slices.contents[i];
TreeArray trees = slice.trees;
for (size_t j = trees.size - 1; j + 1 > 0; j--) {
if (!trees.contents[j]->extra) {
TSTree *root = trees.contents[j];
CHECK(array_splice(&trees, j, 1, root->child_count, root->children));
ts_tree_set_children(root, trees.size, trees.contents);
if (!trees.size)
array_delete(&trees);
for (size_t k = j - 1; k + 1 > 0; k--)
if (!root->children[k]->extra)
root->error_size += root->children[j]->size.chars;
if (ts_parser__select_tree(self, self->finished_tree, root)) {
ts_tree_release(self->finished_tree);
self->finished_tree = root;
} else {
ts_tree_release(root);
TSTree *root = NULL;
if (trees.size == 1) {
root = trees.contents[0];
array_delete(&trees);
} else {
for (size_t j = trees.size - 1; j + 1 > 0; j--) {
TSTree *child = trees.contents[j];
if (!child->extra) {
root = ts_tree_make_copy(child);
root->child_count = 0;
for (size_t k = 0; k < child->child_count; k++)
ts_tree_retain(child->children[k]);
CHECK(array_splice(&trees, j, 1, child->child_count, child->children));
ts_tree_set_children(root, trees.size, trees.contents);
ts_tree_release(child);
break;
}
break;
}
}
if (ts_parser__select_tree(self, self->finished_tree, root)) {
ts_tree_release(self->finished_tree);
self->finished_tree = root;
} else {
ts_tree_release(root);
}
}
ts_stack_remove_version(self->stack, pop.slices.contents[0].version);