diff --git a/spec/runtime/stack_spec.cc b/spec/runtime/stack_spec.cc index fe11acd3..19cc7254 100644 --- a/spec/runtime/stack_spec.cc +++ b/spec/runtime/stack_spec.cc @@ -70,11 +70,11 @@ struct StackEntry { size_t depth; }; -vector get_stack_entries(Stack *stack, int head_index) { +vector get_stack_entries(Stack *stack, StackVersion version) { vector result; ts_stack_pop_until( stack, - head_index, + version, [](void *payload, TSStateId state, size_t tree_count, bool is_done, bool is_pending) { auto entries = static_cast *>(payload); StackEntry entry = {state, tree_count}; @@ -122,7 +122,7 @@ describe("Stack", [&]() { describe("pushing entries to the stack", [&]() { it("adds entries to the stack", [&]() { - AssertThat(ts_stack_head_count(stack), Equals(1)); + AssertThat(ts_stack_version_count(stack), Equals(1)); AssertThat(ts_stack_top_state(stack, 0), Equals(0)); AssertThat(ts_stack_top_position(stack, 0), Equals(ts_length_zero())); @@ -217,12 +217,12 @@ describe("Stack", [&]() { StackPopResult pop_result = ts_stack_pop_count(stack, 0, 3); AssertThat(pop_result.status, Equals(StackPopResult::StackPopStoppedAtError)); - AssertThat(ts_stack_head_count(stack), Equals(1)); + AssertThat(ts_stack_version_count(stack), Equals(1)); AssertThat(ts_stack_top_state(stack, 0), Equals(ts_parse_state_error)); AssertThat(pop_result.slices.size, Equals(1)); StackSlice slice = pop_result.slices.contents[0]; - AssertThat(slice.head_index, Equals(0)); + AssertThat(slice.version, Equals(0)); AssertThat(slice.trees, Equals(vector({ trees[4] }))); free_slice_array(&pop_result.slices); @@ -268,7 +268,7 @@ describe("Stack", [&]() { }); describe("splitting the stack", [&]() { - it("creates a new independent head with the same entries", [&]() { + it("creates a new independent version with the same entries", [&]() { // . <──0── A <──1── B <──2── C* ts_stack_push(stack, 0, trees[0], false, stateA); ts_stack_push(stack, 0, trees[1], false, stateB); @@ -278,7 +278,7 @@ describe("Stack", [&]() { // ↑ // └─* int new_index = ts_stack_split(stack, 0); - AssertThat(ts_stack_head_count(stack), Equals(2)); + AssertThat(ts_stack_version_count(stack), Equals(2)); AssertThat(new_index, Equals(1)); AssertThat(ts_stack_top_state(stack, 1), Equals(stateC)); @@ -288,7 +288,7 @@ describe("Stack", [&]() { ts_stack_push(stack, 0, trees[3], false, stateD); StackPopResult pop_result = ts_stack_pop_count(stack, 1, 1); - AssertThat(ts_stack_head_count(stack), Equals(2)); + AssertThat(ts_stack_version_count(stack), Equals(2)); AssertThat(ts_stack_top_state(stack, 0), Equals(stateD)); AssertThat(ts_stack_top_position(stack, 0), Equals(tree_len * 4)); AssertThat(ts_stack_top_state(stack, 1), Equals(stateB)); @@ -305,7 +305,7 @@ describe("Stack", [&]() { ts_stack_push(stack, 1, trees[4], false, stateE); ts_stack_push(stack, 1, trees[5], false, stateF); - AssertThat(ts_stack_head_count(stack), Equals(2)); + AssertThat(ts_stack_version_count(stack), Equals(2)); AssertThat(ts_stack_top_state(stack, 0), Equals(stateD)); AssertThat(ts_stack_top_position(stack, 0), Equals(tree_len * 4)); AssertThat(ts_stack_top_state(stack, 1), Equals(stateF)); @@ -313,7 +313,7 @@ describe("Stack", [&]() { }); }); - describe("pushing the same state onto two different heads of the stack", [&]() { + describe("pushing the same state onto two different versions of the stack", [&]() { before_each([&]() { // . <──0── A <──1── B <──2── C <──3── D* // ↑ @@ -326,7 +326,7 @@ describe("Stack", [&]() { ts_stack_push(stack, 1, trees[4], false, stateE); ts_stack_push(stack, 1, trees[5], false, stateF); - AssertThat(ts_stack_head_count(stack), Equals(2)); + AssertThat(ts_stack_version_count(stack), Equals(2)); AssertThat(get_stack_entries(stack, 0), Equals(vector({ {stateD, 0}, {stateC, 1}, @@ -343,14 +343,14 @@ describe("Stack", [&]() { }))); }); - it("merges the heads", [&]() { + it("merges the versions", [&]() { // . <──0── A <──1── B <──2── C <──3── D <──6── G* // ↑ | // └───4─── E <──5── F <──7───┘ AssertThat(ts_stack_push(stack, 0, trees[6], false, stateG), Equals(StackPushContinued)); AssertThat(ts_stack_push(stack, 1, trees[7], false, stateG), Equals(StackPushMerged)); - AssertThat(ts_stack_head_count(stack), Equals(1)); + AssertThat(ts_stack_version_count(stack), Equals(1)); AssertThat(get_stack_entries(stack, 0), Equals(vector({ {stateG, 0}, {stateD, 1}, @@ -377,7 +377,7 @@ describe("Stack", [&]() { // └───4─── E <──5── F <──8───┘ AssertThat(ts_stack_push(stack, 1, trees[7], false, stateH), Equals(StackPushMerged)); - AssertThat(ts_stack_head_count(stack), Equals(1)); + AssertThat(ts_stack_version_count(stack), Equals(1)); AssertThat(get_stack_entries(stack, 0), Equals(vector({ {stateH, 0}, {stateG, 1}, @@ -392,7 +392,7 @@ describe("Stack", [&]() { }); }); - describe("when the first head is only one node deep", [&]() { + describe("when the first version is only one node deep", [&]() { it("creates a node with one null successor and one non-null successor", [&]() { ts_tree_retain(trees[2]); ts_tree_retain(trees[3]); @@ -407,7 +407,7 @@ describe("Stack", [&]() { AssertThat(ts_stack_push(stack, 1, trees[2], false, stateB), Equals(StackPushContinued)); AssertThat(ts_stack_push(stack, 1, trees[3], false, stateC), Equals(StackPushMerged)); - AssertThat(ts_stack_head_count(stack), Equals(1)); + AssertThat(ts_stack_version_count(stack), Equals(1)); AssertThat(ts_stack_top_state(stack, 0), Equals(stateC)); AssertThat(get_stack_entries(stack, 0), Equals(vector({ {stateC, 0}, @@ -421,7 +421,7 @@ describe("Stack", [&]() { }); }); - describe("popping from a stack head that has been merged", [&]() { + describe("popping from a stack version that has been merged", [&]() { before_each([&]() { // . <──0── A <──1── B <──2── C <──3── D <──4── E* // ↑ | @@ -436,7 +436,7 @@ describe("Stack", [&]() { ts_stack_push(stack, 1, trees[6], false, stateG); ts_stack_push(stack, 1, trees[7], false, stateE); - AssertThat(ts_stack_head_count(stack), Equals(1)); + AssertThat(ts_stack_version_count(stack), Equals(1)); AssertThat(get_stack_entries(stack, 0), Equals(vector({ {stateE, 0}, {stateD, 1}, @@ -449,8 +449,8 @@ describe("Stack", [&]() { }))); }); - describe("when there are two paths that lead to two different heads", [&]() { - it("returns an entry for each revealed head", [&]() { + describe("when there are two paths that lead to two different versions", [&]() { + it("returns an entry for each revealed version", [&]() { // . <──0── A <──1── B <──2── C* // ↑ // └───5─── F* @@ -458,16 +458,16 @@ describe("Stack", [&]() { AssertThat(pop_result.slices.size, Equals(2)); StackSlice slice1 = pop_result.slices.contents[0]; - AssertThat(slice1.head_index, Equals(0)); + AssertThat(slice1.version, Equals(0)); AssertThat(ts_stack_top_state(stack, 0), Equals(stateC)); AssertThat(slice1.trees, Equals(vector({ trees[3], trees[4] }))); StackSlice slice2 = pop_result.slices.contents[1]; - AssertThat(slice2.head_index, Equals(1)); + AssertThat(slice2.version, Equals(1)); AssertThat(ts_stack_top_state(stack, 1), Equals(stateF)); AssertThat(slice2.trees, Equals(vector({ trees[6], trees[7] }))); - AssertThat(ts_stack_head_count(stack), Equals(2)); + AssertThat(ts_stack_version_count(stack), Equals(2)); AssertThat(get_stack_entries(stack, 0), Equals(vector({ {stateC, 0}, {stateB, 1}, @@ -485,13 +485,13 @@ describe("Stack", [&]() { }); }); - describe("when there is one path, leading to one head", [&]() { + describe("when there is one path, leading to one version", [&]() { it("returns a single entry", [&]() { // . <──0── A <──1── B <──2── C <──3── D <──4── E <──8──H* // ↑ | // └───5─── F <──6── G <──7───┘ AssertThat(ts_stack_push(stack, 0, trees[8], false, stateH), Equals(StackPushContinued)); - AssertThat(ts_stack_head_count(stack), Equals(1)); + AssertThat(ts_stack_version_count(stack), Equals(1)); AssertThat(ts_stack_top_state(stack, 0), Equals(stateH)); // . <──0── A <──1── B <──2── C <──3── D <──4── E* @@ -500,30 +500,30 @@ describe("Stack", [&]() { StackPopResult pop_result = ts_stack_pop_count(stack, 0, 1); AssertThat(pop_result.slices.size, Equals(1)); StackSlice slice1 = pop_result.slices.contents[0]; - AssertThat(slice1.head_index, Equals(0)); + AssertThat(slice1.version, Equals(0)); AssertThat(slice1.trees, Equals(vector({ trees[8] }))); - AssertThat(ts_stack_head_count(stack), Equals(1)); + AssertThat(ts_stack_version_count(stack), Equals(1)); AssertThat(ts_stack_top_state(stack, 0), Equals(stateE)); free_slice_array(&pop_result.slices); }); }); - describe("when there are two paths that converge at the same head", [&]() { + describe("when there are two paths that converge at the same version", [&]() { describe("when the first path is preferred by the callback", [&]() { - it("returns one entry for that head, with the first path of trees", [&]() { + it("returns one entry for that version, with the first path of trees", [&]() { tree_selection_spy.tree_to_return = trees[2]; // . <──0── A <──1── B* StackPopResult pop_result = ts_stack_pop_count(stack, 0, 3); - AssertThat(ts_stack_head_count(stack), Equals(1)); + AssertThat(ts_stack_version_count(stack), Equals(1)); AssertThat(ts_stack_top_state(stack, 0), Equals(stateB)); AssertThat(ts_stack_top_position(stack, 0), Equals(tree_len * 2)); AssertThat(pop_result.slices.size, Equals(1)); StackSlice slice1 = pop_result.slices.contents[0]; - AssertThat(slice1.head_index, Equals(0)); + AssertThat(slice1.version, Equals(0)); AssertThat(slice1.trees, Equals(vector({ trees[2], trees[3], trees[4] }))); free_slice_array(&pop_result.slices); @@ -531,18 +531,18 @@ describe("Stack", [&]() { }); describe("when the second path is preferred by the callback", [&]() { - it("returns one entry for that head, with the second path of trees", [&]() { + it("returns one entry for that version, with the second path of trees", [&]() { tree_selection_spy.tree_to_return = trees[4]; // . <──0── A <──1── B* StackPopResult pop_result = ts_stack_pop_count(stack, 0, 3); - AssertThat(ts_stack_head_count(stack), Equals(1)); + AssertThat(ts_stack_version_count(stack), Equals(1)); AssertThat(ts_stack_top_state(stack, 0), Equals(stateB)); AssertThat(ts_stack_top_position(stack, 0), Equals(tree_len * 2)); AssertThat(pop_result.slices.size, Equals(1)); StackSlice slice1 = pop_result.slices.contents[0]; - AssertThat(slice1.head_index, Equals(0)); + AssertThat(slice1.version, Equals(0)); AssertThat(slice1.trees, Equals(vector({ trees[5], trees[6], trees[7] }))) free_slice_array(&pop_result.slices); @@ -551,7 +551,7 @@ describe("Stack", [&]() { }); }); - describe("popping from a stack head that has been 3-way merged", [&]() { + describe("popping from a stack version that has been 3-way merged", [&]() { before_each([&]() { // . <──0── A <──1── B <──2── C <──3── D <──10── I // ↑ | @@ -573,7 +573,7 @@ describe("Stack", [&]() { ts_stack_push(stack, 1, trees[9], false, stateD); ts_stack_push(stack, 0, trees[10], false, stateI); - AssertThat(ts_stack_head_count(stack), Equals(1)); + AssertThat(ts_stack_version_count(stack), Equals(1)); AssertThat(get_stack_entries(stack, 0), Equals(vector({ {stateI, 0}, {stateD, 1}, @@ -588,7 +588,7 @@ describe("Stack", [&]() { }))); }); - describe("when there are three different paths that lead to three different heads", [&]() { + describe("when there are three different paths that lead to three different versions", [&]() { it("returns three entries with different arrays of trees", [&]() { // . <──0── A <──1── B <──2── C* // ↑ @@ -596,23 +596,23 @@ describe("Stack", [&]() { // | // └───7─── G <──8── H* StackPopResult pop_result = ts_stack_pop_count(stack, 0, 2); - AssertThat(ts_stack_head_count(stack), Equals(3)); + AssertThat(ts_stack_version_count(stack), Equals(3)); AssertThat(pop_result.slices.size, Equals(3)); StackSlice slice1 = pop_result.slices.contents[0]; AssertThat(ts_stack_top_state(stack, 0), Equals(stateC)); - AssertThat(slice1.head_index, Equals(0)); + AssertThat(slice1.version, Equals(0)); AssertThat(slice1.trees, Equals(vector({ trees[3], trees[10] }))) StackSlice slice2 = pop_result.slices.contents[1]; AssertThat(ts_stack_top_state(stack, 1), Equals(stateF)); - AssertThat(slice2.head_index, Equals(1)); + AssertThat(slice2.version, Equals(1)); AssertThat(slice2.trees, Equals(vector({ trees[6], trees[10] }))) StackSlice slice3 = pop_result.slices.contents[2]; AssertThat(ts_stack_top_state(stack, 2), Equals(stateH)); - AssertThat(slice3.head_index, Equals(2)); + AssertThat(slice3.version, Equals(2)); AssertThat(slice3.trees, Equals(vector({ trees[9], trees[10] }))) free_slice_array(&pop_result.slices); diff --git a/src/runtime/node.c b/src/runtime/node.c index 79eeee63..16981f15 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -275,14 +275,15 @@ static size_t ts_tree__write_to_string(const TSTree *self, return cursor - string; } -static char *ts_node__string(TSNode self, const TSDocument *document, bool include_all) { +static char *ts_node__string(TSNode self, const TSDocument *document, + bool include_all) { static char SCRATCH[1]; const TSTree *tree = ts_node__tree(self); const TSLanguage *language = document->parser.language; size_t size = ts_tree__write_to_string(tree, language, SCRATCH, 0, true, include_all) + 1; char *result = ts_malloc(size * sizeof(char)); - ts_tree__write_to_string(tree, language, result , size, true, include_all); + ts_tree__write_to_string(tree, language, result, size, true, include_all); return result; } diff --git a/src/runtime/parser.c b/src/runtime/parser.c index 99502c79..58db4207 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -26,10 +26,11 @@ fprintf(stderr, "\"\n}\n\n"); \ } -#define LOG_STACK() \ - if (self->print_debugging_graphs) { \ - ts_stack_print_dot_graph(self->stack, self->language->symbol_names, stderr); \ - fputs("\n\n", stderr); \ +#define LOG_STACK() \ + if (self->print_debugging_graphs) { \ + ts_stack_print_dot_graph(self->stack, self->language->symbol_names, \ + stderr); \ + fputs("\n\n", stderr); \ } #define SYM_NAME(symbol) ts_language_symbol_name(self->language, symbol) @@ -87,13 +88,13 @@ typedef enum { } BreakdownResult; static BreakdownResult ts_parser__breakdown_top_of_stack(TSParser *self, - int head) { + StackVersion version) { TSTree *last_child = NULL; bool did_break_down = false; bool is_still_pending = false; do { - StackPopResult pop = ts_stack_pop_pending(self->stack, head); + StackPopResult pop = ts_stack_pop_pending(self->stack, version); if (!pop.status) goto error; if (!pop.slices.size) @@ -105,12 +106,11 @@ static BreakdownResult ts_parser__breakdown_top_of_stack(TSParser *self, StackSlice slice = pop.slices.contents[i]; TreeArray removed_trees = slice.trees; TSTree *parent = *array_front(&removed_trees); - int head_index = slice.head_index; LOG("breakdown_pop sym:%s, size:%lu", SYM_NAME(parent->symbol), ts_tree_total_size(parent).chars); StackPushResult last_push = StackPushContinued; - TSStateId state = ts_stack_top_state(self->stack, head_index); + TSStateId state = ts_stack_top_state(self->stack, slice.version); for (size_t j = 0; j < parent->child_count; j++) { last_child = parent->children[j]; is_still_pending = last_child->child_count > 0; @@ -124,9 +124,10 @@ static BreakdownResult ts_parser__breakdown_top_of_stack(TSParser *self, state = action.data.to_state; } + LOG("breakdown_push sym:%s, size:%lu", SYM_NAME(last_child->symbol), ts_tree_total_size(last_child).chars); - last_push = ts_stack_push(self->stack, head_index, last_child, + last_push = ts_stack_push(self->stack, slice.version, last_child, is_still_pending, state); if (last_push == StackPushFailed) goto error; @@ -134,7 +135,7 @@ static BreakdownResult ts_parser__breakdown_top_of_stack(TSParser *self, for (size_t j = 1, count = slice.trees.size; j < count; j++) { TSTree *tree = slice.trees.contents[j]; - last_push = ts_stack_push(self->stack, head_index, tree, false, state); + last_push = ts_stack_push(self->stack, slice.version, tree, false, state); if (last_push == StackPushFailed) goto error; } @@ -185,15 +186,16 @@ static void ts_parser__breakdown_reusable_node(ReusableNode *reusable_node) { } while (ts_tree_is_fragile(reusable_node->tree)); } -static bool ts_parser__can_reuse(TSParser *self, int head, TSTree *tree) { +static bool ts_parser__can_reuse(TSParser *self, StackVersion version, + TSTree *tree) { if (tree->symbol == ts_builtin_sym_error) return false; if (ts_tree_is_fragile(tree) && - tree->parse_state != ts_stack_top_state(self->stack, head)) + tree->parse_state != ts_stack_top_state(self->stack, version)) return false; - TSStateId top_state = ts_stack_top_state(self->stack, head); + TSStateId top_state = ts_stack_top_state(self->stack, version); if (tree->lex_state != TS_TREE_STATE_INDEPENDENT && tree->lex_state != self->language->lex_states[top_state]) @@ -210,9 +212,10 @@ static bool ts_parser__can_reuse(TSParser *self, int head, TSTree *tree) { return true; } -static TSTree *ts_parser__get_next_lookahead(TSParser *self, int head, +static TSTree *ts_parser__get_next_lookahead(TSParser *self, + StackVersion version, ReusableNode *reusable_node) { - TSLength position = ts_stack_top_position(self->stack, head); + TSLength position = ts_stack_top_position(self->stack, version); while (reusable_node->tree) { if (reusable_node->char_index > position.chars) { @@ -227,14 +230,14 @@ static TSTree *ts_parser__get_next_lookahead(TSParser *self, int head, if (reusable_node->tree->has_changes) { if (reusable_node->tree->child_count == 0) - ts_parser__breakdown_top_of_stack(self, head); + ts_parser__breakdown_top_of_stack(self, version); LOG("breakdown_changed sym:%s", SYM_NAME(reusable_node->tree->symbol)); ts_parser__breakdown_reusable_node(reusable_node); continue; } - if (!ts_parser__can_reuse(self, head, reusable_node->tree)) { + if (!ts_parser__can_reuse(self, version, reusable_node->tree)) { LOG("breakdown_unreusable sym:%s", SYM_NAME(reusable_node->tree->symbol)); ts_parser__breakdown_reusable_node(reusable_node); continue; @@ -250,7 +253,7 @@ static TSTree *ts_parser__get_next_lookahead(TSParser *self, int head, } ts_lexer_reset(&self->lexer, position); - TSStateId parse_state = ts_stack_top_state(self->stack, head); + TSStateId parse_state = ts_stack_top_state(self->stack, version); TSStateId lex_state = self->language->lex_states[parse_state]; LOG("lex state:%d", lex_state); return self->language->lex_fn(&self->lexer, lex_state, false); @@ -278,45 +281,47 @@ static int ts_parser__select_tree(void *data, TSTree *left, TSTree *right) { return comparison; } -static ParseActionResult ts_parser__shift(TSParser *self, int head, +static ParseActionResult ts_parser__shift(TSParser *self, StackVersion version, TSStateId parse_state, TSTree *lookahead) { bool is_pending = lookahead->child_count > 0; switch ( - ts_stack_push(self->stack, head, lookahead, is_pending, parse_state)) { + ts_stack_push(self->stack, version, lookahead, is_pending, parse_state)) { case StackPushFailed: return ParseActionFailed; case StackPushMerged: - LOG("merge head:%d", head); + LOG("merge version:%d", version); return ParseActionRemoved; default: return ParseActionUpdated; } } -static ParseActionResult ts_parser__shift_extra(TSParser *self, int head, +static ParseActionResult ts_parser__shift_extra(TSParser *self, + StackVersion version, TSStateId state, TSTree *lookahead) { TSSymbolMetadata metadata = self->language->symbol_metadata[lookahead->symbol]; - if (metadata.structural && ts_stack_head_count(self->stack) > 1) { + if (metadata.structural && ts_stack_version_count(self->stack) > 1) { TSTree *copy = ts_tree_make_copy(lookahead); if (!copy) return ParseActionFailed; copy->extra = true; - ParseActionResult result = ts_parser__shift(self, head, state, copy); + ParseActionResult result = ts_parser__shift(self, version, state, copy); ts_tree_release(copy); return result; } else { lookahead->extra = true; - return ts_parser__shift(self, head, state, lookahead); + return ts_parser__shift(self, version, state, lookahead); } } -static ReduceResult ts_parser__reduce(TSParser *self, int head, TSSymbol symbol, - int child_count, bool extra, bool fragile) { +static ReduceResult ts_parser__reduce(TSParser *self, StackVersion version, + TSSymbol symbol, int child_count, + bool extra, bool fragile) { TSSymbolMetadata metadata = ts_language_symbol_metadata(self->language, symbol); - StackPopResult pop = ts_stack_pop_count(self->stack, head, child_count); + StackPopResult pop = ts_stack_pop_count(self->stack, version, child_count); if (!pop.slices.size) goto error; if (pop.status == StackPopStoppedAtError) @@ -324,7 +329,7 @@ static ReduceResult ts_parser__reduce(TSParser *self, int head, TSSymbol symbol, .status = ReduceStoppedAtError, .partial_slice = pop.slices.contents[0], }; - size_t removed_heads = 0; + size_t removed_versions = 0; for (size_t i = 0; i < pop.slices.size; i++) { StackSlice slice = pop.slices.contents[i]; @@ -345,22 +350,22 @@ static ReduceResult ts_parser__reduce(TSParser *self, int head, TSSymbol symbol, goto error; } - if (fragile || self->is_split || ts_stack_head_count(self->stack) > 1) { + if (fragile || self->is_split || ts_stack_version_count(self->stack) > 1) { parent->fragile_left = true; parent->fragile_right = true; parent->parse_state = TS_TREE_STATE_ERROR; } - int new_head = slice.head_index - removed_heads; + int new_version = slice.version - removed_versions; if (i > 0) { if (symbol == ts_builtin_sym_error) { - removed_heads++; - ts_stack_remove_head(self->stack, new_head); + removed_versions++; + ts_stack_remove_version(self->stack, new_version); continue; } - LOG("split_during_reduce new_head:%d", new_head); + LOG("split_during_reduce new_version:%d", new_version); } /* @@ -369,7 +374,7 @@ static ReduceResult ts_parser__reduce(TSParser *self, int head, TSSymbol symbol, * new parent symbol. */ TSStateId state; - TSStateId top_state = ts_stack_top_state(self->stack, new_head); + TSStateId top_state = ts_stack_top_state(self->stack, new_version); if (parent->parse_state != TS_TREE_STATE_ERROR) parent->parse_state = top_state; if (extra) { @@ -387,14 +392,14 @@ static ReduceResult ts_parser__reduce(TSParser *self, int head, TSSymbol symbol, } StackPushResult push = - ts_stack_push(self->stack, new_head, parent, false, state); + ts_stack_push(self->stack, new_version, parent, false, state); ts_tree_release(parent); switch (push) { case StackPushFailed: goto error; case StackPushMerged: - LOG("merge_during_reduce head:%d", new_head); - removed_heads++; + LOG("merge_during_reduce version:%d", new_version); + removed_versions++; continue; case StackPushContinued: break; @@ -405,13 +410,13 @@ static ReduceResult ts_parser__reduce(TSParser *self, int head, TSSymbol symbol, size_t index = slice.trees.size - trailing_extra_count + j; TSTree *tree = slice.trees.contents[index]; StackPushResult push = - ts_stack_push(self->stack, new_head, tree, false, state); + ts_stack_push(self->stack, new_version, tree, false, state); ts_tree_release(tree); switch (push) { case StackPushFailed: goto error; case StackPushMerged: - removed_heads++; + removed_versions++; break; case StackPushContinued: break; @@ -420,7 +425,7 @@ static ReduceResult ts_parser__reduce(TSParser *self, int head, TSSymbol symbol, } } - if (removed_heads < pop.slices.size) + if (removed_versions < pop.slices.size) return (ReduceResult){.status = ReduceSucceeded }; else return (ReduceResult){.status = ReduceMerged }; @@ -471,7 +476,7 @@ static StackIterateAction ts_parser__error_repair_callback(void *payload, return StackIterateContinue; } -static RepairResult ts_parser__repair_error(TSParser *self, int head_index, +static RepairResult ts_parser__repair_error(TSParser *self, StackVersion version, StackSlice partial_slice, TSTree *lookahead, const TSParseAction *actions, @@ -492,14 +497,14 @@ static RepairResult ts_parser__repair_error(TSParser *self, int head_index, for (size_t i = 0; i < action_count; i++) if (actions[i].type == TSParseActionTypeReduce && actions[i].data.child_count > session.count_above_error) - array_push(&self->error_repairs, - ((ErrorRepair){ - .action = actions[i], .in_progress_state_count = 0, - .essential_tree_count = 0, - })); + array_push(&self->error_repairs, ((ErrorRepair){ + .action = actions[i], + .in_progress_state_count = 0, + .essential_tree_count = 0, + })); StackPopResult result = ts_stack_pop_until( - self->stack, head_index, ts_parser__error_repair_callback, &session); + self->stack, version, ts_parser__error_repair_callback, &session); if (!result.slices.size) { ts_tree_array_delete(&partial_slice.trees); return RepairNoneFound; @@ -538,7 +543,7 @@ static RepairResult ts_parser__repair_error(TSParser *self, int head_index, ts_language_symbol_metadata(self->language, symbol)); StackPushResult push_result = - ts_stack_push(self->stack, head_index, parent, false, repair.next_state); + ts_stack_push(self->stack, version, parent, false, repair.next_state); ts_tree_release(parent); switch (push_result) { case StackPushFailed: @@ -570,8 +575,8 @@ static ParseActionResult ts_parser__start(TSParser *self, TSInput input, return ParseActionUpdated; } -static ParseActionResult ts_parser__accept(TSParser *self, int head) { - TreeArray trees = ts_stack_pop_all(self->stack, head); +static ParseActionResult ts_parser__accept(TSParser *self, StackVersion version) { + TreeArray trees = ts_stack_pop_all(self->stack, version); if (!trees.contents) goto error; @@ -584,7 +589,7 @@ static ParseActionResult ts_parser__accept(TSParser *self, int head) { if (!trees.size) array_delete(&trees); - ts_stack_remove_head(self->stack, head); + ts_stack_remove_version(self->stack, version); int comparison = ts_parser__select_tree(self, self->finished_tree, root); if (comparison > 0) { ts_tree_release(self->finished_tree); @@ -604,7 +609,8 @@ error: return ParseActionFailed; } -static ParseActionResult ts_parser__handle_error(TSParser *self, int head, +static ParseActionResult ts_parser__handle_error(TSParser *self, + StackVersion version, TSTree *invalid_tree) { TreeArray invalid_trees = array_new(); TSTree *next_token = self->language->lex_fn(&self->lexer, 0, true); @@ -616,23 +622,23 @@ static ParseActionResult ts_parser__handle_error(TSParser *self, int head, if (next_token->symbol == ts_builtin_sym_end) { LOG_ACTION("fail_to_recover"); TSTree *error = ts_tree_make_error_node(&invalid_trees); - if (!ts_stack_push(self->stack, head, error, false, 0)) + if (!ts_stack_push(self->stack, version, error, false, 0)) goto error; - TreeArray trees = ts_stack_pop_all(self->stack, head); + TreeArray trees = ts_stack_pop_all(self->stack, version); if (!trees.contents) goto error; TSTree *parent = ts_tree_make_node( ts_builtin_sym_start, trees.size, trees.contents, ts_language_symbol_metadata(self->language, ts_builtin_sym_start)); - if (!ts_stack_push(self->stack, head, parent, false, 0)) + if (!ts_stack_push(self->stack, version, parent, false, 0)) goto error; ts_tree_release(parent); ts_tree_release(error); ts_tree_release(next_token); - return ts_parser__accept(self, head); + return ts_parser__accept(self, version); } TSLength position = self->lexer.current_position; @@ -652,9 +658,10 @@ static ParseActionResult ts_parser__handle_error(TSParser *self, int head, TSTree *error = ts_tree_make_error_node(&invalid_trees); if (!error) goto error; - if (!ts_stack_push(self->stack, head, error, false, ts_parse_state_error)) + if (!ts_stack_push(self->stack, version, error, false, + ts_parse_state_error)) goto error; - if (!ts_stack_push(self->stack, head, next_token, false, next_state)) + if (!ts_stack_push(self->stack, version, next_token, false, next_state)) goto error; ts_tree_release(error); @@ -674,10 +681,11 @@ error: return ParseActionFailed; } -static ParseActionResult ts_parser__consume_lookahead(TSParser *self, int head, +static ParseActionResult ts_parser__consume_lookahead(TSParser *self, + StackVersion version, TSTree *lookahead) { for (;;) { - TSStateId state = ts_stack_top_state(self->stack, head); + TSStateId state = ts_stack_top_state(self->stack, version); size_t action_count; const TSParseAction *actions = ts_language_actions( self->language, state, lookahead->symbol, &action_count); @@ -686,7 +694,7 @@ static ParseActionResult ts_parser__consume_lookahead(TSParser *self, int head, * If there are multiple actions for the current state and lookahead symbol, * split the stack so that each one can be performed. If there is a `SHIFT` * action, it will always appear *last* in the list of actions. Perform it - * on the original stack head and return. + * on the original stack version and return. */ bool repaired_error = false; size_t child_count_above_error = -1; @@ -697,23 +705,24 @@ static ParseActionResult ts_parser__consume_lookahead(TSParser *self, int head, action.type == TSParseActionTypeReduce && action.data.child_count > child_count_above_error; - int current_head; + StackVersion current_version; if (i == action_count - 1) { if (should_skip) action.type = TSParseActionTypeError; - current_head = head; + current_version = version; } else { if (should_skip) continue; - current_head = ts_stack_split(self->stack, head); - LOG_ACTION("split_action from_head:%d, new_head:%d", head, current_head); + current_version = ts_stack_split(self->stack, version); + LOG_ACTION("split_action from_version:%d, new_version:%d", version, + current_version); } LOG_STACK(); switch (action.type) { case TSParseActionTypeError: { - switch (ts_parser__breakdown_top_of_stack(self, current_head)) { + switch (ts_parser__breakdown_top_of_stack(self, current_version)) { case BreakdownFailed: return ParseActionFailed; case BreakdownPerformed: @@ -722,12 +731,12 @@ static ParseActionResult ts_parser__consume_lookahead(TSParser *self, int head, break; } - if (ts_stack_head_count(self->stack) == 1) { + if (ts_stack_version_count(self->stack) == 1) { LOG_ACTION("handle_error %s", SYM_NAME(lookahead->symbol)); - return ts_parser__handle_error(self, current_head, lookahead); + return ts_parser__handle_error(self, current_version, lookahead); } else { - LOG_ACTION("bail current_head:%d", current_head); - ts_stack_remove_head(self->stack, current_head); + LOG_ACTION("bail current_version:%d", current_version); + ts_stack_remove_version(self->stack, current_version); return ParseActionRemoved; } } @@ -735,30 +744,31 @@ static ParseActionResult ts_parser__consume_lookahead(TSParser *self, int head, case TSParseActionTypeShift: if (action.extra) { LOG_ACTION("shift_extra"); - return ts_parser__shift_extra(self, current_head, state, lookahead); + return ts_parser__shift_extra(self, current_version, state, + lookahead); } else { LOG_ACTION("shift state:%u", action.data.to_state); TSStateId state = action.data.to_state; - return ts_parser__shift(self, current_head, state, lookahead); + return ts_parser__shift(self, current_version, state, lookahead); } case TSParseActionTypeReduce: if (action.extra) { LOG_ACTION("reduce_extra sym:%s", SYM_NAME(action.data.symbol)); - ts_parser__reduce(self, current_head, action.data.symbol, 1, true, - false); + ts_parser__reduce(self, current_version, action.data.symbol, 1, + true, false); } else { LOG_ACTION("reduce sym:%s, child_count:%u, fragile:%s", SYM_NAME(action.data.symbol), action.data.child_count, BOOL_STRING(action.fragile)); ReduceResult result = - ts_parser__reduce(self, current_head, action.data.symbol, + ts_parser__reduce(self, current_version, action.data.symbol, action.data.child_count, false, action.fragile); switch (result.status) { case ReduceFailed: return ParseActionFailed; case ReduceMerged: - if (current_head == head) + if (current_version == version) return ParseActionRemoved; break; case ReduceSucceeded: @@ -770,28 +780,28 @@ static ParseActionResult ts_parser__consume_lookahead(TSParser *self, int head, if (!result.partial_slice.trees.contents[j]->extra) child_count_above_error++; - LOG_ACTION("repair head:%d", current_head); - switch (ts_parser__repair_error(self, current_head, + LOG_ACTION("repair version:%d", current_version); + switch (ts_parser__repair_error(self, current_version, result.partial_slice, lookahead, actions, action_count)) { case RepairFailed: return ParseActionFailed; case RepairNoneFound: LOG_ACTION("repair_failed"); - if (ts_stack_head_count(self->stack) == 1) { + if (ts_stack_version_count(self->stack) == 1) { LOG_ACTION("handle_error %s", SYM_NAME(lookahead->symbol)); - return ts_parser__handle_error(self, current_head, + return ts_parser__handle_error(self, current_version, lookahead); } else { - LOG_ACTION("bail head:%d, count:%d", current_head, - ts_stack_head_count(self->stack)); - ts_stack_remove_head(self->stack, current_head); - if (current_head == head) + LOG_ACTION("bail version:%d, count:%d", current_version, + ts_stack_version_count(self->stack)); + ts_stack_remove_version(self->stack, current_version); + if (current_version == version) return ParseActionRemoved; break; } case RepairMerged: - if (current_head == head) + if (current_version == version) return ParseActionRemoved; break; default: @@ -804,7 +814,7 @@ static ParseActionResult ts_parser__consume_lookahead(TSParser *self, int head, case TSParseActionTypeAccept: LOG_ACTION("accept"); - return ts_parser__accept(self, current_head); + return ts_parser__accept(self, current_version); } } } @@ -860,35 +870,37 @@ TSTree *ts_parser_parse(TSParser *self, TSInput input, TSTree *previous_tree) { TSTree *lookahead = NULL; size_t last_position, position = 0; - self->is_split = ts_stack_head_count(self->stack) > 1; + self->is_split = ts_stack_version_count(self->stack) > 1; - for (int head = 0; head < ts_stack_head_count(self->stack);) { + for (StackVersion version = 0; + version < ts_stack_version_count(self->stack);) { ReusableNode reusable_node = current_reusable_node; for (bool removed = false; !removed;) { last_position = position; - size_t new_position = ts_stack_top_position(self->stack, head).chars; + size_t new_position = ts_stack_top_position(self->stack, version).chars; if (new_position > max_position) { max_position = new_position; next_reusable_node = reusable_node; - head++; + version++; break; - } else if (new_position == max_position && head > 0) { - head++; + } else if (new_position == max_position && version > 0) { + version++; break; } position = new_position; - LOG_ACTION("process head:%d, head_count:%d, state:%d, pos:%lu", head, - ts_stack_head_count(self->stack), - ts_stack_top_state(self->stack, head), position); + LOG_ACTION("process version:%d, head_count:%d, state:%d, pos:%lu", + version, ts_stack_version_count(self->stack), + ts_stack_top_state(self->stack, version), position); if (!lookahead || (position != last_position) || - !ts_parser__can_reuse(self, head, lookahead)) { + !ts_parser__can_reuse(self, version, lookahead)) { ts_tree_release(lookahead); - lookahead = ts_parser__get_next_lookahead(self, head, &reusable_node); + lookahead = + ts_parser__get_next_lookahead(self, version, &reusable_node); if (!lookahead) return NULL; } @@ -896,7 +908,7 @@ TSTree *ts_parser_parse(TSParser *self, TSInput input, TSTree *previous_tree) { LOG_ACTION("lookahead sym:%s, size:%lu", SYM_NAME(lookahead->symbol), ts_tree_total_chars(lookahead)); - switch (ts_parser__consume_lookahead(self, head, lookahead)) { + switch (ts_parser__consume_lookahead(self, version, lookahead)) { case ParseActionFailed: ts_tree_release(lookahead); goto error; @@ -911,7 +923,7 @@ TSTree *ts_parser_parse(TSParser *self, TSInput input, TSTree *previous_tree) { ts_tree_release(lookahead); - if (ts_stack_head_count(self->stack) == 0) { + if (ts_stack_version_count(self->stack) == 0) { ts_stack_clear(self->stack); ts_tree_assign_parents(self->finished_tree); return self->finished_tree; diff --git a/src/runtime/stack.c b/src/runtime/stack.c index 48ad5f64..76079cbf 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -8,7 +8,6 @@ #include #define MAX_SUCCESSOR_COUNT 8 -#define INITIAL_HEAD_CAPACITY 3 #define STARTING_TREE_CAPACITY 10 #define MAX_NODE_POOL_SIZE 50 @@ -203,15 +202,15 @@ error: * Section: Reading from the stack */ -TSStateId ts_stack_top_state(const Stack *self, int head_index) { - return (*array_get(&self->heads, head_index))->state; +TSStateId ts_stack_top_state(const Stack *self, StackVersion version) { + return (*array_get(&self->heads, version))->state; } -TSLength ts_stack_top_position(const Stack *self, int head_index) { - return (*array_get(&self->heads, head_index))->position; +TSLength ts_stack_top_position(const Stack *self, StackVersion version) { + return (*array_get(&self->heads, version))->position; } -int ts_stack_head_count(const Stack *self) { +int ts_stack_version_count(const Stack *self) { return self->heads.size; } @@ -248,72 +247,72 @@ static void ts_stack__merge_slice(Stack *self, StackSlice *slice, * Section: Mutating the stack (Private) */ -static int ts_stack__add_head(Stack *self, StackNode *node) { +static StackVersion ts_stack__add_version(Stack *self, StackNode *node) { if (array_push(&self->heads, node)) { stack_node_retain(node); - return self->heads.size - 1; + return (StackVersion)(self->heads.size - 1); } else { return -1; } } -static int ts_stack__index_of_head(Stack *self, StackNode *node) { - for (size_t i = 0; i < self->heads.size; i++) { +static StackVersion ts_stack__find_version(Stack *self, StackNode *node) { + for (size_t i = 0; i < self->heads.size; i++) if (self->heads.contents[i] == node) - return i; - } + return (StackVersion)i; return -1; } -void ts_stack_remove_head(Stack *self, int head_index) { - StackNode *node = *array_get(&self->heads, head_index); +void ts_stack_remove_version(Stack *self, StackVersion version) { + StackNode *node = *array_get(&self->heads, version); stack_node_release(node, &self->node_pool); - array_erase(&self->heads, head_index); + array_erase(&self->heads, version); } /* * Section: Mutating the stack (Public) */ -StackPushResult ts_stack_push(Stack *self, int head_index, TSTree *tree, +StackPushResult ts_stack_push(Stack *self, StackVersion version, TSTree *tree, bool is_pending, TSStateId state) { - StackNode *current_head = *array_get(&self->heads, head_index); + StackNode *current_version = *array_get(&self->heads, version); TSLength position = - ts_length_add(current_head->position, ts_tree_total_size(tree)); + ts_length_add(current_version->position, ts_tree_total_size(tree)); - for (int i = 0; i < head_index; i++) { + for (int i = 0; i < version; i++) { StackNode *prior_node = self->heads.contents[i]; if (prior_node->state == state && prior_node->position.chars == position.chars) { - stack_node_add_successor(prior_node, tree, is_pending, current_head); - ts_stack_remove_head(self, head_index); + stack_node_add_successor(prior_node, tree, is_pending, current_version); + ts_stack_remove_version(self, version); return StackPushMerged; } } - StackNode *new_head = stack_node_new(current_head, tree, is_pending, state, - position, &self->node_pool); - if (!new_head) + StackNode *new_version = stack_node_new(current_version, tree, is_pending, + state, position, &self->node_pool); + if (!new_version) return StackPushFailed; - stack_node_release(current_head, &self->node_pool); - self->heads.contents[head_index] = new_head; + stack_node_release(current_version, &self->node_pool); + self->heads.contents[version] = new_version; return StackPushContinued; } -int ts_stack_split(Stack *self, int head_index) { - StackNode *head = self->heads.contents[head_index]; - return ts_stack__add_head(self, head); +StackVersion ts_stack_split(Stack *self, StackVersion version) { + StackNode *head = self->heads.contents[version]; + return ts_stack__add_version(self, head); } -static inline ALWAYS_INLINE StackPopResult stack__pop( - Stack *self, int head_index, StackIterateCallback callback, void *payload) { +static inline ALWAYS_INLINE StackPopResult + stack__pop(Stack *self, StackVersion version, StackIterateCallback callback, + void *payload) { array_clear(&self->slices); array_clear(&self->pop_paths); - StackNode *initial_head = *array_get(&self->heads, head_index); + StackNode *initial_version = *array_get(&self->heads, version); PopPath pop_path = { - .node = initial_head, + .node = initial_version, .trees = array_new(), .essential_tree_count = 0, .is_done = false, @@ -372,7 +371,8 @@ static inline ALWAYS_INLINE StackPopResult stack__pop( next_path->node = successor.node; if (!array_push(&next_path->trees, successor.tree)) goto error; - if (!successor.tree->extra && successor.tree->symbol != ts_builtin_sym_error) + if (!successor.tree->extra && + successor.tree->symbol != ts_builtin_sym_error) next_path->essential_tree_count++; if (!successor.is_pending) next_path->is_pending = false; @@ -386,23 +386,23 @@ static inline ALWAYS_INLINE StackPopResult stack__pop( if (!path->is_done) continue; - StackSlice slice = {.trees = path->trees, .head_index = -1 }; + StackSlice slice = {.trees = path->trees, .version = -1 }; array_reverse(&slice.trees); if (i == 0) { stack_node_retain(path->node); - self->heads.contents[head_index] = path->node; - slice.head_index = head_index; + self->heads.contents[version] = path->node; + slice.version = version; } else { - slice.head_index = ts_stack__index_of_head(self, path->node); - if (slice.head_index == -1) { - if ((slice.head_index = ts_stack__add_head(self, path->node)) == -1) + slice.version = ts_stack__find_version(self, path->node); + if (slice.version == -1) { + if ((slice.version = ts_stack__add_version(self, path->node)) == -1) goto error; } else { bool merged = false; for (size_t j = 0; j < self->slices.size; j++) { StackSlice *prior_result = &self->slices.contents[j]; - if (prior_result->head_index == slice.head_index) { + if (prior_result->version == slice.version) { ts_stack__merge_slice(self, prior_result, &slice); merged = true; break; @@ -418,19 +418,19 @@ static inline ALWAYS_INLINE StackPopResult stack__pop( } if (self->slices.size) - stack_node_release(initial_head, &self->node_pool); - return (StackPopResult){.status = StackPopSucceeded, .slices = self->slices}; + stack_node_release(initial_version, &self->node_pool); + return (StackPopResult){.status = StackPopSucceeded, .slices = self->slices }; error: for (size_t i = 0; i < self->pop_paths.size; i++) array_delete(&self->pop_paths.contents[i].trees); array_clear(&self->slices); - return (StackPopResult){.status = StackPopFailed}; + return (StackPopResult){.status = StackPopFailed }; } -StackPopResult ts_stack_pop_until(Stack *self, int head_index, +StackPopResult ts_stack_pop_until(Stack *self, StackVersion version, StackIterateCallback callback, void *payload) { - return stack__pop(self, head_index, callback, payload); + return stack__pop(self, version, callback, payload); } static inline ALWAYS_INLINE StackIterateAction @@ -448,11 +448,12 @@ static inline ALWAYS_INLINE StackIterateAction return StackIterateContinue; } -StackPopResult ts_stack_pop_count(Stack *self, int head_index, size_t count) { - StackPopSession session = { .goal_tree_count = count, .found_error = false }; +StackPopResult ts_stack_pop_count(Stack *self, StackVersion version, + size_t count) { + StackPopSession session = {.goal_tree_count = count, .found_error = false }; StackPopResult pop = - stack__pop(self, head_index, stack__pop_count_callback, &session); + stack__pop(self, version, stack__pop_count_callback, &session); if (pop.status && session.found_error) { pop.status = StackPopStoppedAtError; @@ -460,7 +461,7 @@ StackPopResult ts_stack_pop_count(Stack *self, int head_index, size_t count) { while (pop.slices.size > 1) { StackSlice slice = array_pop(&pop.slices); ts_tree_array_delete(&slice.trees); - ts_stack_remove_head(self, slice.head_index); + ts_stack_remove_version(self, slice.version); } } @@ -476,23 +477,22 @@ static inline ALWAYS_INLINE StackIterateAction return StackIterateContinue; } -StackPopResult ts_stack_pop_pending(Stack *self, int head_index) { - return stack__pop(self, head_index, stack__pop_pending_callback, NULL); +StackPopResult ts_stack_pop_pending(Stack *self, StackVersion version) { + return stack__pop(self, version, stack__pop_pending_callback, NULL); } - static inline ALWAYS_INLINE StackIterateAction stack__pop_all_callback(void *payload, TSStateId state, size_t tree_count, - bool is_done, bool is_pending) { + bool is_done, bool is_pending) { return is_done ? StackIteratePop : StackIterateContinue; } -TreeArray ts_stack_pop_all(Stack *self, int head_index) { - StackPopResult pop = stack__pop(self, head_index, stack__pop_all_callback, NULL); +TreeArray ts_stack_pop_all(Stack *self, StackVersion version) { + StackPopResult pop = stack__pop(self, version, stack__pop_all_callback, NULL); if (pop.status != StackPopSucceeded) return (TreeArray)array_new(); assert(pop.slices.size == 1); - assert(pop.slices.contents[0].head_index == head_index); + assert(pop.slices.contents[0].version == version); return pop.slices.contents[0].trees; } @@ -573,7 +573,8 @@ int ts_stack_print_dot_graph(Stack *self, const char **symbol_names, FILE *f) { for (int j = 0; j < node->successor_count; j++) { StackLink successor = node->successors[j]; fprintf(f, "node_%p -> node_%p [", node, successor.node); - if (successor.is_pending) fprintf(f, "style=dashed "); + if (successor.is_pending) + fprintf(f, "style=dashed "); fprintf(f, "label=\""); if (successor.tree->symbol == ts_builtin_sym_error) { diff --git a/src/runtime/stack.h b/src/runtime/stack.h index 24f86455..99ccdca2 100644 --- a/src/runtime/stack.h +++ b/src/runtime/stack.h @@ -12,9 +12,11 @@ extern "C" { typedef struct Stack Stack; +typedef int StackVersion; + typedef struct { TreeArray trees; - int head_index; + StackVersion version; } StackSlice; typedef Array(StackSlice) StackSliceArray; @@ -58,55 +60,55 @@ Stack *ts_stack_new(); void ts_stack_delete(Stack *); /* - * Get the stack's current number of heads. + * Get the stack's current number of versions. */ -int ts_stack_head_count(const Stack *); +int ts_stack_version_count(const Stack *); /* - * Get the state at given head of the stack. If the stack is empty, this - * returns the initial state (0). + * Get the state at the top of the given version of the stack. If the stack is + * empty, this returns the initial state (0). */ -TSStateId ts_stack_top_state(const Stack *, int head_index); +TSStateId ts_stack_top_state(const Stack *, StackVersion); /* - * Get the position of the given head of the stack. If the stack is empty, this - * returns {0, 0}. + * Get the position at the top of the given version of the stack. If the stack + * is empty, this returns zero. */ -TSLength ts_stack_top_position(const Stack *, int head_index); +TSLength ts_stack_top_position(const Stack *, StackVersion); /* - * Push a (tree, state) pair onto the given head of the stack. This could cause - * the head to merge with an existing head. + * Push a tree and state onto the given head of the stack. This could cause + * the version to merge with an existing version. */ -StackPushResult ts_stack_push(Stack *, int head_index, TSTree *, bool, - TSStateId); +StackPushResult ts_stack_push(Stack *, StackVersion, TSTree *, bool, TSStateId); /* - * Pop the given number of entries from the given head of the stack. This - * operation can increase the number of stack heads by revealing multiple heads - * which had previously been merged. It returns a struct that indicates the - * index of each revealed head and the trees removed from that head. + * Pop the given number of entries from the given version of the stack. This + * operation can increase the number of stack versions by revealing multiple + * versions which had previously been merged. It returns a struct that + * indicates the index of each revealed version and the trees removed from that + * version. */ -StackPopResult ts_stack_pop_count(Stack *, int head_index, size_t count); +StackPopResult ts_stack_pop_count(Stack *, StackVersion, size_t count); -StackPopResult ts_stack_pop_until(Stack *, int head_index, StackIterateCallback, +StackPopResult ts_stack_pop_until(Stack *, StackVersion, StackIterateCallback, void *); -StackPopResult ts_stack_pop_pending(Stack *, int head_index); +StackPopResult ts_stack_pop_pending(Stack *, StackVersion); -TreeArray ts_stack_pop_all(Stack *, int head_index); +TreeArray ts_stack_pop_all(Stack *, StackVersion); /* - * Split the given stack head into two heads, so that the stack can be + * Split the given stack head into two versions, so that the stack can be * transformed from its current state in multiple alternative ways. Returns - * the index of the newly-created head. + * the ID of the newly-created version. */ -int ts_stack_split(Stack *, int head_index); +StackVersion ts_stack_split(Stack *, StackVersion); /* - * Remove the given head from the stack. + * Remove the given version from the stack. */ -void ts_stack_remove_head(Stack *, int head_index); +void ts_stack_remove_version(Stack *, StackVersion); /* * Remove all entries from the stack. diff --git a/src/runtime/tree.c b/src/runtime/tree.c index b9b8c867..0d32dcac 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -159,10 +159,9 @@ TSTree *ts_tree_make_error_node(TreeArray *children) { } } - return ts_tree_make_node(ts_builtin_sym_error, children->size, - children->contents, (TSSymbolMetadata){ - .extra = false, .visible = true, .named = true - }); + return ts_tree_make_node( + ts_builtin_sym_error, children->size, children->contents, + (TSSymbolMetadata){.extra = false, .visible = true, .named = true }); } void ts_tree_retain(TSTree *self) {