From 8a13b5d120f0071c07d8961a2fe7c9e9f0900476 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 3 Mar 2016 10:16:10 -0800 Subject: [PATCH] Rename StackPopResult -> StackSlice --- spec/runtime/stack_spec.cc | 318 ++++++++++++++++--------------------- src/runtime/parser.c | 120 +++++++------- src/runtime/stack.c | 82 +++++----- src/runtime/stack.h | 8 +- 4 files changed, 238 insertions(+), 290 deletions(-) diff --git a/spec/runtime/stack_spec.cc b/spec/runtime/stack_spec.cc index 2f8dbf43..d2796484 100644 --- a/spec/runtime/stack_spec.cc +++ b/spec/runtime/stack_spec.cc @@ -38,23 +38,23 @@ int tree_selection_spy_callback(void *data, TSTree *left, TSTree *right) { return 1; } -void free_pop_results(StackPopResultArray *pop_results) { - for (size_t i = 0; i < pop_results->size; i++) { - StackPopResult pop_result = pop_results->contents[i]; +void free_slice_array(StackSliceArray *slices) { + for (size_t i = 0; i < slices->size; i++) { + StackSlice slice = slices->contents[i]; bool matches_prior_trees = false; for (size_t j = 0; j < i; j++) { - StackPopResult prior_result = pop_results->contents[j]; - if (pop_result.trees.contents == prior_result.trees.contents) { + StackSlice prior_slice = slices->contents[j]; + if (slice.trees.contents == prior_slice.trees.contents) { matches_prior_trees = true; break; } } if (!matches_prior_trees) { - for (size_t j = 0; j < pop_result.trees.size; j++) - ts_tree_release(pop_result.trees.contents[j]); - array_delete(&pop_result.trees); + for (size_t j = 0; j < slice.trees.size; j++) + ts_tree_release(slice.trees.contents[j]); + array_delete(&slice.trees); } } } @@ -99,27 +99,21 @@ describe("Stack", [&]() { AssertThat(ts_stack_head_count(stack), Equals(1)); AssertThat(ts_stack_head(stack, 0), Equals(nullptr)); - /* - * . <--0-- A* - */ + // . <──0── A* ts_stack_push(stack, 0, trees[0], stateA); const StackEntry *entry1 = ts_stack_head(stack, 0); AssertThat(*entry1, Equals({stateA, tree_len})); AssertThat(ts_stack_entry_next_count(entry1), Equals(1)); AssertThat(ts_stack_entry_next(entry1, 0), Equals(nullptr)); - /* - * . <--0-- A <--1-- B* - */ + // . <──0── A <──1── B* ts_stack_push(stack, 0, trees[1], stateB); const StackEntry *entry2 = ts_stack_head(stack, 0); AssertThat(*entry2, Equals({stateB, tree_len * 2})); AssertThat(ts_stack_entry_next_count(entry2), Equals(1)); AssertThat(ts_stack_entry_next(entry2, 0), Equals(entry1)); - /* - * . <--0-- A <--1-- B <--2-- C* - */ + // . <──0── A <──1── B <──2── C* ts_stack_push(stack, 0, trees[2], stateC); const StackEntry *entry3 = ts_stack_head(stack, 0); AssertThat(*entry3, Equals({stateC, tree_len * 3})); @@ -130,122 +124,104 @@ describe("Stack", [&]() { describe("popping nodes from the stack", [&]() { before_each([&]() { - /* - * . <--0-- A <--1-- B <--2-- C* - */ + // . <──0── A <──1── B <──2── C* ts_stack_push(stack, 0, trees[0], stateA); ts_stack_push(stack, 0, trees[1], stateB); ts_stack_push(stack, 0, trees[2], stateC); }); it("removes the given number of nodes from the stack", [&]() { - /* - * . <--0-- A* - */ - StackPopResultArray results = ts_stack_pop(stack, 0, 2, false); - AssertThat(results.size, Equals(1)); + // . <──0── A* + StackSliceArray slices = ts_stack_pop(stack, 0, 2, false); + AssertThat(slices.size, Equals(1)); - StackPopResult result = results.contents[0]; - AssertThat(result.trees, Equals(vector({ trees[1], trees[2] }))); + StackSlice slice = slices.contents[0]; + AssertThat(slice.trees, Equals(vector({ trees[1], trees[2] }))); AssertThat(*ts_stack_head(stack, 0), Equals({stateA, tree_len})); - free_pop_results(&results); + free_slice_array(&slices); - /* - * . - */ - results = ts_stack_pop(stack, 0, 1, false); - AssertThat(results.size, Equals(1)); + // .* + slices = ts_stack_pop(stack, 0, 1, false); + AssertThat(slices.size, Equals(1)); - result = results.contents[0]; - AssertThat(result.trees, Equals(vector({ trees[0] }))); + slice = slices.contents[0]; + AssertThat(slice.trees, Equals(vector({ trees[0] }))); AssertThat(ts_stack_head(stack, 0), Equals(nullptr)); - free_pop_results(&results); + free_slice_array(&slices); }); it("does not count 'extra' trees toward the count", [&]() { trees[1]->extra = true; - /* - * . - */ - StackPopResultArray results = ts_stack_pop(stack, 0, 2, false); - AssertThat(results.size, Equals(1)); + // .* + StackSliceArray slices = ts_stack_pop(stack, 0, 2, false); + AssertThat(slices.size, Equals(1)); - StackPopResult result = results.contents[0]; - AssertThat(result.trees, Equals(vector({ trees[0], trees[1], trees[2] }))); + StackSlice slice = slices.contents[0]; + AssertThat(slice.trees, Equals(vector({ trees[0], trees[1], trees[2] }))); AssertThat(ts_stack_head(stack, 0), Equals(nullptr)); - free_pop_results(&results); + free_slice_array(&slices); }); it("pops the entire stack when given a negative count", [&]() { - /* - * . - */ - StackPopResultArray results = ts_stack_pop(stack, 0, -1, false); - AssertThat(results.size, Equals(1)); + // .* + StackSliceArray slices = ts_stack_pop(stack, 0, -1, false); + AssertThat(slices.size, Equals(1)); - StackPopResult result = results.contents[0]; - AssertThat(result.trees, Equals(vector({ trees[0], trees[1], trees[2] }))); + StackSlice slice = slices.contents[0]; + AssertThat(slice.trees, Equals(vector({ trees[0], trees[1], trees[2] }))); - free_pop_results(&results); + free_slice_array(&slices); }); it("stops immediately when removing an error", [&]() { trees[2]->symbol = ts_builtin_sym_error; - StackPopResultArray results = ts_stack_pop(stack, 0, 2, false); - AssertThat(results.size, Equals(1)); + StackSliceArray slices = ts_stack_pop(stack, 0, 2, false); + AssertThat(slices.size, Equals(1)); - StackPopResult result = results.contents[0]; - AssertThat(result.trees, Equals(vector({ trees[2] }))); + StackSlice slice = slices.contents[0]; + AssertThat(slice.trees, Equals(vector({ trees[2] }))); AssertThat(ts_stack_top_state(stack, 0), Equals(stateB)); - free_pop_results(&results); + free_slice_array(&slices); }); }); describe("splitting the stack", [&]() { it("creates a new independent head with the same entries", [&]() { - /* - * . <--0-- A <--1-- B <--2-- C* - */ + // . <──0── A <──1── B <──2── C* ts_stack_push(stack, 0, trees[0], stateA); ts_stack_push(stack, 0, trees[1], stateB); ts_stack_push(stack, 0, trees[2], stateC); - /* - * . <--0-- A <--1-- B <--2-- C* - * ↑ - * `-* - */ + // . <──0── A <──1── B <──2── C* + // ↑ + // └─* int new_index = ts_stack_split(stack, 0); AssertThat(ts_stack_head_count(stack), Equals(2)); AssertThat(new_index, Equals(1)); AssertThat(ts_stack_top_state(stack, 1), Equals(stateC)); - /* - * . <--0-- A <--1-- B <--2-- C <--3-- D* - * ↑ - * `-* - */ + // . <──0── A <──1── B <──2── C <──3── D* + // ↑ + // └─* ts_stack_push(stack, 0, trees[3], stateD); - StackPopResultArray pop_results = ts_stack_pop(stack, 1, 1, false); + StackSliceArray slices = ts_stack_pop(stack, 1, 1, false); AssertThat(ts_stack_head_count(stack), Equals(2)); AssertThat(*ts_stack_head(stack, 0), Equals({stateD, tree_len * 4})); AssertThat(*ts_stack_head(stack, 1), Equals({stateB, tree_len * 2})); - AssertThat(pop_results.size, Equals(1)); - StackPopResult pop_result = pop_results.contents[0]; - AssertThat(pop_result.trees.size, Equals(1)); - free_pop_results(&pop_results); + AssertThat(slices.size, Equals(1)); + StackSlice slice = slices.contents[0]; + AssertThat(slice.trees.size, Equals(1)); + free_slice_array(&slices); - /* - * . <--0-- A <--1-- B <--2-- C <--3-- D* - * ↑ - * `---4--- E <--5-- F* - */ + // . <──0── A <──1── B <──2── C <──3── D* + // ↑ + // └───4─── E <──5── F* ts_stack_push(stack, 1, trees[4], stateE); ts_stack_push(stack, 1, trees[5], stateF); @@ -257,11 +233,9 @@ describe("Stack", [&]() { describe("pushing the same state onto two different heads of the stack", [&]() { before_each([&]() { - /* - * . <--0-- A <--1-- B <--2-- C <--3-- D* - * ↑ - * `---4--- E <--5-- F* - */ + // . <──0── A <──1── B <──2── C <──3── D* + // ↑ + // └───4─── E <──5── F* ts_stack_push(stack, 0, trees[0], stateA); ts_stack_push(stack, 0, trees[1], stateB); ts_stack_split(stack, 0); @@ -276,11 +250,9 @@ describe("Stack", [&]() { }); it("merges the heads", [&]() { - /* - * . <--0-- A <--1-- B <--2-- C <--3-- D <--6-- G* - * ^ | - * `---4--- E <--5-- F <--7---' - */ + // . <──0── A <──1── B <──2── C <──3── D <──6── G* + // ↑ | + // └───4─── E <──5── F <──7───┘ AssertThat(ts_stack_push(stack, 0, trees[6], stateG), Equals(StackPushResultContinued)); AssertThat(ts_stack_push(stack, 1, trees[7], stateG), Equals(StackPushResultMerged)); @@ -294,20 +266,16 @@ describe("Stack", [&]() { describe("when the merged nodes share a successor", [&]() { it("recursively merges the successor nodes", [&]() { - /* - * . <--0-- A <--1-- B <--2-- C <--3-- D <--6-- G <--7--H* - * ↑ - * `---4--- E <--5-- F <--8-- G* - */ + // . <──0── A <──1── B <──2── C <──3── D <──6── G <──7──H* + // ↑ + // └───4─── E <──5── F <──8── G* AssertThat(ts_stack_push(stack, 0, trees[6], stateG), Equals(StackPushResultContinued)); AssertThat(ts_stack_push(stack, 0, trees[7], stateH), Equals(StackPushResultContinued)); AssertThat(ts_stack_push(stack, 1, trees[6], stateG), Equals(StackPushResultContinued)); - /* - * . <--0-- A <--1-- B <--2-- C <--3-- D <--6-- G <--7--H* - * ↑ | - * `---4--- E <--5-- F <--8---' - */ + // . <──0── A <──1── B <──2── C <──3── D <──6── G <──7──H* + // ↑ | + // └───4─── E <──5── F <──8───┘ AssertThat(ts_stack_push(stack, 1, trees[7], stateH), Equals(StackPushResultMerged)); AssertThat(ts_stack_head_count(stack), Equals(1)); @@ -327,11 +295,9 @@ describe("Stack", [&]() { ts_tree_retain(trees[3]); TSTree *parent = ts_tree_make_node(5, 2, tree_array({ trees[2], trees[3] }), metadata); - /* - * . <--2-- B <--3-- C - * ^ | - * `--------5--------' - */ + // . <──2── B <──3── C + // ↑ | + // └────────5────────┘ ts_stack_clear(stack); ts_stack_split(stack, 0); AssertThat(ts_stack_push(stack, 0, parent, stateC), Equals(StackPushResultContinued)); @@ -353,11 +319,9 @@ describe("Stack", [&]() { describe("popping from a stack head that has been merged", [&]() { before_each([&]() { - /* - * . <--0-- A <--1-- B <--2-- C <--3-- D <--4-- E* - * ^ | - * `---5--- F <--6-- G <--7---' - */ + // . <──0── A <──1── B <──2── C <──3── D <──4── E* + // ↑ | + // └───5─── F <──6── G <──7───┘ ts_stack_push(stack, 0, trees[0], stateA); ts_stack_push(stack, 0, trees[1], stateB); ts_stack_split(stack, 0); @@ -375,58 +339,52 @@ describe("Stack", [&]() { describe("when there are two paths that lead to two different heads", [&]() { it("returns an entry for each revealed head", [&]() { - /* - * . <--0-- A <--1-- B <--2-- C* - * ^ - * `---5--- F* - */ - StackPopResultArray results = ts_stack_pop(stack, 0, 2, false); + // . <──0── A <──1── B <──2── C* + // ↑ + // └───5─── F* + StackSliceArray slices = ts_stack_pop(stack, 0, 2, false); - AssertThat(results.size, Equals(2)); - StackPopResult result1 = results.contents[0]; - AssertThat(result1.head_index, Equals(0)); + AssertThat(slices.size, Equals(2)); + StackSlice slice1 = slices.contents[0]; + AssertThat(slice1.head_index, Equals(0)); AssertThat(ts_stack_top_state(stack, 0), Equals(stateC)); - AssertThat(result1.trees, Equals(vector({ trees[3], trees[4] }))); + AssertThat(slice1.trees, Equals(vector({ trees[3], trees[4] }))); - StackPopResult result2 = results.contents[1]; - AssertThat(result2.head_index, Equals(1)); + StackSlice slice2 = slices.contents[1]; + AssertThat(slice2.head_index, Equals(1)); AssertThat(ts_stack_top_state(stack, 1), Equals(stateF)); - AssertThat(result2.trees, Equals(vector({ trees[6], trees[7] }))); + AssertThat(slice2.trees, Equals(vector({ trees[6], trees[7] }))); AssertThat(ts_stack_head_count(stack), Equals(2)); AssertThat(*ts_stack_head(stack, 0), Equals({stateC, tree_len * 3})); AssertThat(*ts_stack_head(stack, 1), Equals({stateF, tree_len * 3})); - free_pop_results(&results); + free_slice_array(&slices); }); }); describe("when there is one path, leading to one head", [&]() { it("returns a single entry", [&]() { - /* - * . <--0-- A <--1-- B <--2-- C <--3-- D <--4-- E <--8--H* - * ^ | - * `---5--- F <--6-- G <--7---' - */ + // . <──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], stateH), Equals(StackPushResultContinued)); AssertThat(ts_stack_head_count(stack), Equals(1)); AssertThat(ts_stack_top_state(stack, 0), Equals(stateH)); - /* - * . <--0-- A <--1-- B <--2-- C <--3-- D <--4-- E* - * ^ | - * `---5--- F <--6-- G <--7---' - */ - StackPopResultArray results = ts_stack_pop(stack, 0, 1, false); - AssertThat(results.size, Equals(1)); - StackPopResult result1 = results.contents[0]; - AssertThat(result1.head_index, Equals(0)); - AssertThat(result1.trees, Equals(vector({ trees[8] }))); + // . <──0── A <──1── B <──2── C <──3── D <──4── E* + // ↑ | + // └───5─── F <──6── G <──7───┘ + StackSliceArray slices = ts_stack_pop(stack, 0, 1, false); + AssertThat(slices.size, Equals(1)); + StackSlice slice1 = slices.contents[0]; + AssertThat(slice1.head_index, Equals(0)); + AssertThat(slice1.trees, Equals(vector({ trees[8] }))); AssertThat(ts_stack_head_count(stack), Equals(1)); AssertThat(ts_stack_top_state(stack, 0), Equals(stateE)); - free_pop_results(&results); + free_slice_array(&slices); }); }); @@ -435,19 +393,17 @@ describe("Stack", [&]() { it("returns one entry for that head, with the first path of trees", [&]() { tree_selection_spy.tree_to_return = trees[2]; - /* - * . <--0-- A <--1-- B* - */ - StackPopResultArray results = ts_stack_pop(stack, 0, 3, false); + // . <──0── A <──1── B* + StackSliceArray slices = ts_stack_pop(stack, 0, 3, false); AssertThat(ts_stack_head_count(stack), Equals(1)); AssertThat(*ts_stack_head(stack, 0), Equals({stateB, tree_len * 2})); - AssertThat(results.size, Equals(1)); - StackPopResult result1 = results.contents[0]; - AssertThat(result1.head_index, Equals(0)); - AssertThat(result1.trees, Equals(vector({ trees[2], trees[3], trees[4] }))); + AssertThat(slices.size, Equals(1)); + StackSlice slice1 = slices.contents[0]; + AssertThat(slice1.head_index, Equals(0)); + AssertThat(slice1.trees, Equals(vector({ trees[2], trees[3], trees[4] }))); - free_pop_results(&results); + free_slice_array(&slices); }); }); @@ -455,19 +411,17 @@ describe("Stack", [&]() { it("returns one entry for that head, with the second path of trees", [&]() { tree_selection_spy.tree_to_return = trees[4]; - /* - * . <--0-- A <--1-- B* - */ - StackPopResultArray results = ts_stack_pop(stack, 0, 3, false); + // . <──0── A <──1── B* + StackSliceArray slices = ts_stack_pop(stack, 0, 3, false); AssertThat(ts_stack_head_count(stack), Equals(1)); AssertThat(*ts_stack_head(stack, 0), Equals({stateB, tree_len * 2})); - AssertThat(results.size, Equals(1)); - StackPopResult result1 = results.contents[0]; - AssertThat(result1.head_index, Equals(0)); - AssertThat(result1.trees, Equals(vector({ trees[5], trees[6], trees[7] }))) + AssertThat(slices.size, Equals(1)); + StackSlice slice1 = slices.contents[0]; + AssertThat(slice1.head_index, Equals(0)); + AssertThat(slice1.trees, Equals(vector({ trees[5], trees[6], trees[7] }))) - free_pop_results(&results); + free_slice_array(&slices); }); }); }); @@ -475,13 +429,11 @@ describe("Stack", [&]() { describe("popping from a stack head that has been 3-way merged", [&]() { before_each([&]() { - /* - * . <--0-- A <--1-- B <--2-- C <--3-- D <--10-- I - * ^ | - * `---4--- E <--5-- F <--6---' - * | | - * `---7--- G <--8-- H <--9---' - */ + // . <──0── A <──1── B <──2── C <──3── D <──10── I + // ↑ | + // ├───4─── E <──5── F <──6───┤ + // | | + // └───7─── G <──8── H <──9───┘ ts_stack_clear(stack); ts_stack_push(stack, 0, trees[0], stateA); ts_stack_split(stack, 0); @@ -503,34 +455,32 @@ describe("Stack", [&]() { describe("when there are three different paths that lead to three different heads", [&]() { it("returns three entries with different arrays of trees", [&]() { - /* - * . <--0-- A <--1-- B <--2-- C* - * ^ - * `---4--- E <--5-- F* - * | - * `---7--- G <--8-- H* - */ - StackPopResultArray results = ts_stack_pop(stack, 0, 2, false); + // . <──0── A <──1── B <──2── C* + // ↑ + // ├───4─── E <──5── F* + // | + // └───7─── G <──8── H* + StackSliceArray slices = ts_stack_pop(stack, 0, 2, false); AssertThat(ts_stack_head_count(stack), Equals(3)); - AssertThat(results.size, Equals(3)); + AssertThat(slices.size, Equals(3)); - StackPopResult result1 = results.contents[0]; + StackSlice slice1 = slices.contents[0]; AssertThat(ts_stack_top_state(stack, 0), Equals(stateC)); - AssertThat(result1.head_index, Equals(0)); - AssertThat(result1.trees, Equals(vector({ trees[3], trees[10] }))) + AssertThat(slice1.head_index, Equals(0)); + AssertThat(slice1.trees, Equals(vector({ trees[3], trees[10] }))) - StackPopResult result2 = results.contents[1]; + StackSlice slice2 = slices.contents[1]; AssertThat(ts_stack_top_state(stack, 1), Equals(stateF)); - AssertThat(result2.head_index, Equals(1)); - AssertThat(result2.trees, Equals(vector({ trees[6], trees[10] }))) + AssertThat(slice2.head_index, Equals(1)); + AssertThat(slice2.trees, Equals(vector({ trees[6], trees[10] }))) - StackPopResult result3 = results.contents[2]; + StackSlice slice3 = slices.contents[2]; AssertThat(ts_stack_top_state(stack, 2), Equals(stateH)); - AssertThat(result3.head_index, Equals(2)); - AssertThat(result3.trees, Equals(vector({ trees[9], trees[10] }))) + AssertThat(slice3.head_index, Equals(2)); + AssertThat(slice3.trees, Equals(vector({ trees[9], trees[10] }))) - free_pop_results(&results); + free_slice_array(&slices); }); }); }); diff --git a/src/runtime/parser.c b/src/runtime/parser.c index 21e827ba..57087141 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -67,16 +67,16 @@ typedef enum { * Private */ -static StackPopResult ts_parser__pop_one(TSParser *self, int head_index, int count) { - StackPopResultArray results = ts_stack_pop(self->stack, head_index, count, true); - assert(results.size > 0); - assert(results.contents[0].head_index == head_index); - for (size_t i = 1; i < results.size; i++) { - ts_tree_array_clear(&results.contents[i].trees); - array_delete(&results.contents[i].trees); - ts_stack_remove_head(self->stack, results.contents[i].head_index); +static StackSlice ts_parser__pop_one(TSParser *self, int head_index, int count) { + StackSliceArray slices = ts_stack_pop(self->stack, head_index, count, true); + assert(slices.size > 0); + assert(slices.contents[0].head_index == head_index); + for (size_t i = 1; i < slices.size; i++) { + ts_tree_array_clear(&slices.contents[i].trees); + array_delete(&slices.contents[i].trees); + ts_stack_remove_head(self->stack, slices.contents[i].head_index); } - return results.contents[0]; + return slices.contents[0]; } static ParseActionResult ts_parser__breakdown_top_of_stack(TSParser *self, @@ -84,21 +84,21 @@ static ParseActionResult ts_parser__breakdown_top_of_stack(TSParser *self, TSTree *last_child = NULL; do { - StackPopResultArray pop_results = ts_stack_pop(self->stack, head, 1, false); - if (!pop_results.size) + StackSliceArray slices = ts_stack_pop(self->stack, head, 1, false); + if (!slices.size) return FailedToUpdateStackHead; - assert(pop_results.size > 0); + assert(slices.size > 0); /* * Since only one entry (not counting extra trees) is being popped from the * stack, there should only be one possible array of removed trees. */ - for (size_t i = 0; i < pop_results.size; i++) { - StackPopResult pop_result = pop_results.contents[i]; - TreeArray removed_trees = pop_result.trees; + for (size_t i = 0; i < slices.size; i++) { + StackSlice slice = slices.contents[i]; + TreeArray removed_trees = slice.trees; TSTree *parent = *array_front(&removed_trees); - int head_index = pop_result.head_index; + int head_index = slice.head_index; LOG("breakdown_pop sym:%s, size:%lu", SYM_NAME(parent->symbol), ts_tree_total_size(parent).chars); @@ -121,8 +121,8 @@ static ParseActionResult ts_parser__breakdown_top_of_stack(TSParser *self, goto error; } - for (size_t j = 1, count = pop_result.trees.size; j < count; j++) { - TSTree *tree = pop_result.trees.contents[j]; + 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, state); if (last_push == StackPushResultFailed) goto error; @@ -349,28 +349,28 @@ static ReduceResult ts_parser__reduce(TSParser *self, int head, TSSymbol symbol, int child_count, bool extra, bool fragile) { array_clear(&self->reduce_parents); TSSymbolMetadata metadata = ts_language_symbol_metadata(self->language, symbol); - StackPopResultArray pop_results = + StackSliceArray slices = ts_stack_pop(self->stack, head, child_count, false); - if (!pop_results.size) + if (!slices.size) return ReduceFailed; - if (pop_results.contents[0].trees.size && - pop_results.contents[0].trees.contents[0]->symbol == ts_builtin_sym_error) { + if (slices.contents[0].trees.size && + slices.contents[0].trees.contents[0]->symbol == ts_builtin_sym_error) { if (self->partial_pop.size) { ts_tree_array_clear(&self->partial_pop); array_delete(&self->partial_pop); } - self->partial_pop = pop_results.contents[0].trees; - for (size_t i = 1; i < pop_results.size; i++) { - ts_tree_array_clear(&pop_results.contents[i].trees); - array_delete(&pop_results.contents[i].trees); + self->partial_pop = slices.contents[0].trees; + for (size_t i = 1; i < slices.size; i++) { + ts_tree_array_clear(&slices.contents[i].trees); + array_delete(&slices.contents[i].trees); } return ReduceStoppedAtError; } size_t removed_heads = 0; - for (size_t i = 0; i < pop_results.size; i++) { - StackPopResult pop_result = pop_results.contents[i]; + for (size_t i = 0; i < slices.size; i++) { + StackSlice slice = slices.contents[i]; /* * If the same set of trees led to a previous stack head, reuse the parent @@ -379,27 +379,27 @@ static ReduceResult ts_parser__reduce(TSParser *self, int head, TSSymbol symbol, */ TSTree *parent = NULL; size_t trailing_extra_count = 0; - for (size_t j = pop_result.trees.size - 1; j + 1 > 0; j--) { - if (pop_result.trees.contents[j]->extra) + for (size_t j = slice.trees.size - 1; j + 1 > 0; j--) { + if (slice.trees.contents[j]->extra) trailing_extra_count++; else break; } - size_t popped_child_count = pop_result.trees.size - trailing_extra_count; + size_t popped_child_count = slice.trees.size - trailing_extra_count; parent = ts_tree_make_node(symbol, popped_child_count, - pop_result.trees.contents, metadata); + slice.trees.contents, metadata); if (!parent) { - for (size_t i = 0; i < pop_result.trees.size; i++) - ts_tree_release(pop_result.trees.contents[i]); - array_delete(&pop_result.trees); + for (size_t i = 0; i < slice.trees.size; i++) + ts_tree_release(slice.trees.contents[i]); + array_delete(&slice.trees); goto error; } if (!array_push(&self->reduce_parents, parent)) goto error; - int new_head = pop_result.head_index - removed_heads; + int new_head = slice.head_index - removed_heads; if (i > 0) { if (symbol == ts_builtin_sym_error) { @@ -463,8 +463,8 @@ static ReduceResult ts_parser__reduce(TSParser *self, int head, TSSymbol symbol, if (trailing_extra_count > 0) { for (size_t j = 0; j < trailing_extra_count; j++) { - size_t index = pop_result.trees.size - trailing_extra_count + j; - TSTree *tree = pop_result.trees.contents[index]; + size_t index = slice.trees.size - trailing_extra_count + j; + TSTree *tree = slice.trees.contents[index]; switch (ts_stack_push(self->stack, new_head, tree, state)) { case StackPushResultFailed: return ReduceFailed; @@ -492,7 +492,7 @@ static ReduceResult ts_parser__reduce(TSParser *self, int head, TSSymbol symbol, ts_tree_release(*parent); } - if (removed_heads < pop_results.size) + if (removed_heads < slices.size) return ReduceSucceeded; else return ReduceMerged; @@ -520,7 +520,7 @@ static ParseActionResult ts_parser__handle_error(TSParser *self, int head, } if (next_lookahead->symbol == ts_builtin_sym_end) { - StackPopResult result = ts_parser__pop_one(self, head, -1); + StackSlice result = ts_parser__pop_one(self, head, -1); TSTree *parent = ts_tree_make_node(ts_builtin_sym_start, result.trees.size, result.trees.contents, ts_language_symbol_metadata(self->language, ts_builtin_sym_start)); ts_stack_push(self->stack, head, parent, 0); @@ -641,26 +641,26 @@ static ParseActionResult ts_parser__repair_error(TSParser *self, int head_index, // Pop any trees that were skipped. Make a new extra error node that contains // them and the error leaf node. - StackPopResult pop_result = ts_parser__pop_one(self, head_index, best_repair.skipped_subtree_count); - array_push(&pop_result.trees, *array_front(&self->partial_pop)); + StackSlice slice = ts_parser__pop_one(self, head_index, best_repair.skipped_subtree_count); + array_push(&slice.trees, *array_front(&self->partial_pop)); TSTree *error = ts_tree_make_node( ts_builtin_sym_error, - pop_result.trees.size, - pop_result.trees.contents, + slice.trees.size, + slice.trees.contents, ts_language_symbol_metadata(self->language, ts_builtin_sym_error)); // Pop any additional trees that are needed for the chosen reduce action. Make // a new wrapper node of the chosen symbol that contains them, the error node, // and the trees that were popped above the error node. - pop_result = ts_parser__pop_one(self, head_index, best_repair.depth - (error->child_count - 1)); - array_push(&pop_result.trees, error); + slice = ts_parser__pop_one(self, head_index, best_repair.depth - (error->child_count - 1)); + array_push(&slice.trees, error); for (size_t i = 1; i < self->partial_pop.size; i++) - array_push(&pop_result.trees, self->partial_pop.contents[i]); + array_push(&slice.trees, self->partial_pop.contents[i]); array_delete(&self->partial_pop); TSTree *parent = ts_tree_make_node( best_repair.symbol, - pop_result.trees.size, - pop_result.trees.contents, + slice.trees.size, + slice.trees.contents, ts_language_symbol_metadata(self->language, best_repair.symbol)); StackPushResult push_result = ts_stack_push(self->stack, head_index, parent, best_repair.next_state); @@ -700,13 +700,13 @@ static ParseActionResult ts_parser__start(TSParser *self, TSInput input, } static ParseActionResult ts_parser__accept(TSParser *self, int head) { - StackPopResultArray pop_results = ts_stack_pop(self->stack, head, -1, true); - if (!pop_results.size) + StackSliceArray slices = ts_stack_pop(self->stack, head, -1, true); + if (!slices.size) goto error; - for (size_t j = 0; j < pop_results.size; j++) { - StackPopResult pop_result = pop_results.contents[j]; - TreeArray trees = pop_result.trees; + for (size_t j = 0; j < slices.size; j++) { + StackSlice slice = slices.contents[j]; + TreeArray trees = slice.trees; for (size_t i = trees.size - 1; i + 1 > 0; i--) { if (!trees.contents[i]->extra) { @@ -717,7 +717,7 @@ static ParseActionResult ts_parser__accept(TSParser *self, int head) { if (!trees.size) array_delete(&trees); - ts_parser__remove_head(self, pop_result.head_index); + ts_parser__remove_head(self, slice.head_index); int comparison = ts_parser__select_tree(self, self->finished_tree, root); if (comparison > 0) { ts_tree_release(self->finished_tree); @@ -734,11 +734,11 @@ static ParseActionResult ts_parser__accept(TSParser *self, int head) { return RemovedStackHead; error: - if (pop_results.size) { - StackPopResult pop_result = *array_front(&pop_results); - for (size_t i = 0; i < pop_result.trees.size; i++) - ts_tree_release(pop_result.trees.contents[i]); - array_delete(&pop_result.trees); + if (slices.size) { + StackSlice slice = *array_front(&slices); + for (size_t i = 0; i < slice.trees.size; i++) + ts_tree_release(slice.trees.contents[i]); + array_delete(&slice.trees); } return FailedToUpdateStackHead; } diff --git a/src/runtime/stack.c b/src/runtime/stack.c index a633e6a4..15af2354 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -37,7 +37,7 @@ typedef Array(StackNode *) StackNodeArray; struct Stack { Array(StackNode *) heads; - StackPopResultArray pop_results; + StackSliceArray slices; Array(PopPath) pop_paths; StackNodeArray node_pool; void *tree_selection_payload; @@ -58,7 +58,7 @@ Stack *ts_stack_new() { goto error; array_init(&self->heads); - array_init(&self->pop_results); + array_init(&self->slices); array_init(&self->pop_paths); array_init(&self->node_pool); self->tree_selection_payload = NULL; @@ -67,7 +67,7 @@ Stack *ts_stack_new() { if (!array_grow(&self->heads, 4)) goto error; - if (!array_grow(&self->pop_results, 4)) + if (!array_grow(&self->slices, 4)) goto error; if (!array_grow(&self->pop_paths, 4)) @@ -84,8 +84,8 @@ error: if (self) { if (self->heads.contents) array_delete(&self->heads); - if (self->pop_results.contents) - array_delete(&self->pop_results); + if (self->slices.contents) + array_delete(&self->slices); if (self->pop_paths.contents) array_delete(&self->pop_paths); if (self->node_pool.contents) @@ -181,22 +181,20 @@ static StackNode *stack_node_new(StackNode *next, TSTree *tree, TSStateId state, return node; } -static void ts_stack__clear_pop_result(Stack *self, StackPopResult *result) { - for (size_t i = 0; i < result->trees.size; i++) - ts_tree_release(result->trees.contents[i]); - array_delete(&result->trees); +static void ts_stack_slice__clear(StackSlice *slice) { + ts_tree_array_clear(&slice->trees); + array_delete(&slice->trees); } -static void ts_stack__add_alternative_pop_result(Stack *self, - StackPopResult *result, - StackPopResult *new_result) { +static void ts_stack__merge_slice(Stack *self, StackSlice *slice, + StackSlice *new_slice) { bool should_update = false; - if (result->trees.size < new_result->trees.size) { + if (slice->trees.size < new_slice->trees.size) { should_update = true; - } else if (result->trees.size == new_result->trees.size) { - for (size_t i = 0; i < result->trees.size; i++) { - TSTree *tree = result->trees.contents[i]; - TSTree *new_tree = new_result->trees.contents[i]; + } else if (slice->trees.size == new_slice->trees.size) { + for (size_t i = 0; i < slice->trees.size; i++) { + TSTree *tree = slice->trees.contents[i]; + TSTree *new_tree = new_slice->trees.contents[i]; int comparison = self->tree_selection_function( self->tree_selection_payload, tree, new_tree); if (comparison < 0) { @@ -209,11 +207,11 @@ static void ts_stack__add_alternative_pop_result(Stack *self, } if (should_update) { - ts_stack__clear_pop_result(self, result); - result->trees = new_result->trees; - result->trees.size = new_result->trees.size; + ts_stack_slice__clear(slice); + slice->trees = new_slice->trees; + slice->trees.size = new_slice->trees.size; } else { - ts_stack__clear_pop_result(self, new_result); + ts_stack_slice__clear(new_slice); } } @@ -306,9 +304,9 @@ int ts_stack_split(Stack *self, int head_index) { return ts_stack__add_head(self, head); } -StackPopResultArray ts_stack_pop(Stack *self, int head_index, int child_count, +StackSliceArray ts_stack_pop(Stack *self, int head_index, int child_count, bool count_extra) { - array_clear(&self->pop_results); + array_clear(&self->slices); array_clear(&self->pop_paths); StackNode *previous_head = *array_get(&self->heads, head_index); @@ -384,47 +382,47 @@ StackPopResultArray ts_stack_pop(Stack *self, int head_index, int child_count, if (!path->is_shared) array_reverse(&path->trees); - StackPopResult result = { + StackSlice slice = { .trees = path->trees, .head_index = -1, }; if (i == 0) { stack_node_retain(path->node); self->heads.contents[head_index] = path->node; - result.head_index = head_index; + slice.head_index = head_index; } else { - result.head_index = ts_stack__find_head(self, path->node); - if (result.head_index == -1) { - result.head_index = ts_stack__add_head(self, path->node); - if (result.head_index == -1) + slice.head_index = ts_stack__find_head(self, path->node); + if (slice.head_index == -1) { + slice.head_index = ts_stack__add_head(self, path->node); + if (slice.head_index == -1) goto error; } else { - bool merged_result = false; - for (size_t j = 0; j < self->pop_results.size; j++) { - StackPopResult *prior_result = &self->pop_results.contents[j]; - if (prior_result->head_index == result.head_index) { - ts_stack__add_alternative_pop_result(self, prior_result, &result); - merged_result = true; + 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) { + ts_stack__merge_slice(self, prior_result, &slice); + merged = true; break; } } - if (merged_result) + if (merged) continue; } } - if (!array_push(&self->pop_results, result)) + if (!array_push(&self->slices, slice)) goto error; } stack_node_release(previous_head, &self->node_pool); - return self->pop_results; + return self->slices; error: array_delete(&initial_path.trees); - StackPopResultArray result; - array_init(&result); - return result; + StackSliceArray slices; + array_init(&slices); + return slices; } void ts_stack_shrink(Stack *self, int head_index, int count) { @@ -455,7 +453,7 @@ void ts_stack_set_tree_selection_callback(Stack *self, void *payload, void ts_stack_delete(Stack *self) { if (self->pop_paths.contents) - array_delete(&self->pop_results); + array_delete(&self->slices); if (self->pop_paths.contents) array_delete(&self->pop_paths); ts_stack_clear(self); diff --git a/src/runtime/stack.h b/src/runtime/stack.h index 58415891..3d675775 100644 --- a/src/runtime/stack.h +++ b/src/runtime/stack.h @@ -19,7 +19,7 @@ typedef struct { typedef struct { TreeArray trees; int head_index; -} StackPopResult; +} StackSlice; typedef enum { StackPushResultFailed, @@ -27,7 +27,7 @@ typedef enum { StackPushResultContinued, } StackPushResult; -typedef Array(StackPopResult) StackPopResultArray; +typedef Array(StackSlice) StackSliceArray; typedef int (*TreeSelectionFunction)(void *, TSTree *, TSTree *); @@ -85,8 +85,8 @@ StackPushResult ts_stack_push(Stack *, int head_index, TSTree *, TSStateId); * which had previously been merged. It returns a struct that indicates the * index of each revealed head and the trees removed from that head. */ -StackPopResultArray ts_stack_pop(Stack *, int head_index, int count, - bool count_extra); +StackSliceArray ts_stack_pop(Stack *, int head_index, int count, + bool count_extra); /* * Remove the given number of entries from the given head of the stack.