Clean up Stack API

* Remove StackPopResult
* Rename top_state() -> state()
* Rename top_position() -> position()
* Improve docs
This commit is contained in:
Max Brunsfeld 2018-03-29 17:37:54 -07:00
parent ee995c3d6b
commit 5520983144
4 changed files with 169 additions and 180 deletions

View file

@ -76,15 +76,14 @@ static bool parser__breakdown_top_of_stack(Parser *self, StackVersion version) {
bool pending = false;
do {
StackPopResult pop = ts_stack_pop_pending(self->stack, version);
if (!pop.slices.size)
break;
StackSliceArray pop = ts_stack_pop_pending(self->stack, version);
if (!pop.size) break;
did_break_down = true;
pending = false;
for (uint32_t i = 0; i < pop.slices.size; i++) {
StackSlice slice = pop.slices.contents[i];
TSStateId state = ts_stack_top_state(self->stack, slice.version);
for (uint32_t i = 0; i < pop.size; i++) {
StackSlice slice = pop.contents[i];
TSStateId state = ts_stack_state(self->stack, slice.version);
Tree *parent = *array_front(&slice.trees);
for (uint32_t j = 0; j < parent->child_count; j++) {
@ -106,13 +105,12 @@ static bool parser__breakdown_top_of_stack(Parser *self, StackVersion version) {
ts_stack_push(self->stack, slice.version, tree, false, state);
}
LOG("breakdown_top_of_stack tree:%s", SYM_NAME(parent->symbol));
LOG_STACK();
ts_stack_decrease_push_count(self->stack, slice.version, parent->child_count + 1);
ts_tree_release(&self->tree_pool, parent);
array_delete(&slice.trees);
LOG("breakdown_top_of_stack tree:%s", SYM_NAME(parent->symbol));
LOG_STACK();
}
} while (pending);
@ -177,7 +175,7 @@ static bool parser__better_version_exists(Parser *self, StackVersion version,
bool is_in_error, unsigned cost) {
if (self->finished_tree && self->finished_tree->error_cost <= cost) return true;
Length position = ts_stack_top_position(self->stack, version);
Length position = ts_stack_position(self->stack, version);
ErrorStatus status = {
.cost = cost,
.is_in_error = is_in_error,
@ -188,10 +186,10 @@ static bool parser__better_version_exists(Parser *self, StackVersion version,
for (StackVersion i = 0, n = ts_stack_version_count(self->stack); i < n; i++) {
if (i == version ||
ts_stack_is_halted(self->stack, i) ||
ts_stack_top_position(self->stack, i).bytes < position.bytes) continue;
ts_stack_position(self->stack, i).bytes < position.bytes) continue;
ErrorStatus status_i = {
.cost = ts_stack_error_cost(self->stack, i),
.is_in_error = ts_stack_top_state(self->stack, i) == ERROR_STATE,
.is_in_error = ts_stack_state(self->stack, i) == ERROR_STATE,
.dynamic_precedence = ts_stack_dynamic_precedence(self->stack, i),
.push_count = ts_stack_push_count(self->stack, i)
};
@ -222,7 +220,7 @@ static unsigned parser__condense_stack(Parser *self) {
.cost = ts_stack_error_cost(self->stack, i),
.push_count = ts_stack_push_count(self->stack, i),
.dynamic_precedence = ts_stack_dynamic_precedence(self->stack, i),
.is_in_error = ts_stack_top_state(self->stack, i) == ERROR_STATE,
.is_in_error = ts_stack_state(self->stack, i) == ERROR_STATE,
};
if (!status_i.is_in_error && status_i.cost < min_error_cost) {
min_error_cost = status_i.cost;
@ -233,7 +231,7 @@ static unsigned parser__condense_stack(Parser *self) {
.cost = ts_stack_error_cost(self->stack, j),
.push_count = ts_stack_push_count(self->stack, j),
.dynamic_precedence = ts_stack_dynamic_precedence(self->stack, j),
.is_in_error = ts_stack_top_state(self->stack, j) == ERROR_STATE,
.is_in_error = ts_stack_state(self->stack, j) == ERROR_STATE,
};
bool can_merge = ts_stack_can_merge(self->stack, j, i);
@ -298,7 +296,7 @@ static void parser__restore_external_scanner(Parser *self, Tree *external_token)
}
static Tree *parser__lex(Parser *self, StackVersion version, TSStateId parse_state) {
Length start_position = ts_stack_top_position(self->stack, version);
Length start_position = ts_stack_position(self->stack, version);
Tree *external_token = ts_stack_last_external_token(self->stack, version);
TSLexMode lex_mode = self->language->lex_modes[parse_state];
const bool *valid_external_tokens = ts_language_enabled_external_tokens(
@ -492,7 +490,7 @@ static bool parser__can_reuse_first_leaf(Parser *self, TSStateId state, Tree *tr
static Tree *parser__get_lookahead(Parser *self, StackVersion version, TSStateId *state,
ReusableNode *reusable_node, TableEntry *table_entry) {
Length position = ts_stack_top_position(self->stack, version);
Length position = ts_stack_position(self->stack, version);
Tree *last_external_token = ts_stack_last_external_token(self->stack, version);
Tree *result;
@ -532,7 +530,7 @@ static Tree *parser__get_lookahead(Parser *self, StackVersion version, TSStateId
if (!reusable_node_breakdown(reusable_node)) {
reusable_node_pop(reusable_node);
parser__breakdown_top_of_stack(self, version);
*state = ts_stack_top_state(self->stack, version);
*state = ts_stack_state(self->stack, version);
}
continue;
}
@ -652,15 +650,15 @@ static bool parser__replace_children(Parser *self, Tree *tree, Tree **children,
}
}
static StackPopResult parser__reduce(Parser *self, StackVersion version, TSSymbol symbol,
static StackSliceArray parser__reduce(Parser *self, StackVersion version, TSSymbol symbol,
uint32_t count, int dynamic_precedence,
uint16_t alias_sequence_id, bool fragile) {
uint32_t initial_version_count = ts_stack_version_count(self->stack);
StackPopResult pop = ts_stack_pop_count(self->stack, version, count);
StackSliceArray pop = ts_stack_pop_count(self->stack, version, count);
for (uint32_t i = 0; i < pop.slices.size; i++) {
StackSlice slice = pop.slices.contents[i];
for (uint32_t i = 0; i < pop.size; i++) {
StackSlice slice = pop.contents[i];
// Extra tokens on top of the stack should not be included in this new parent
// node. They will be re-pushed onto the stack after the parent node is
@ -678,8 +676,8 @@ static StackPopResult parser__reduce(Parser *self, StackVersion version, TSSymbo
// into one, because they all diverged from a common state. In that case,
// choose one of the arrays of trees to be the parent node's children, and
// delete the rest of the tree arrays.
while (i + 1 < pop.slices.size) {
StackSlice next_slice = pop.slices.contents[i + 1];
while (i + 1 < pop.size) {
StackSlice next_slice = pop.contents[i + 1];
if (next_slice.version != slice.version) break;
i++;
@ -699,9 +697,9 @@ static StackPopResult parser__reduce(Parser *self, StackVersion version, TSSymbo
parent->dynamic_precedence += dynamic_precedence;
parent->alias_sequence_id = alias_sequence_id;
TSStateId state = ts_stack_top_state(self->stack, slice.version);
TSStateId state = ts_stack_state(self->stack, slice.version);
TSStateId next_state = ts_language_next_state(self->language, state, symbol);
if (fragile || self->in_ambiguity || pop.slices.size > 1 || initial_version_count > 1) {
if (fragile || self->in_ambiguity || pop.size > 1 || initial_version_count > 1) {
parent->fragile_left = true;
parent->fragile_right = true;
parent->parse_state = TS_TREE_STATE_NONE;
@ -755,10 +753,10 @@ static void parser__accept(Parser *self, StackVersion version,
assert(lookahead->symbol == ts_builtin_sym_end);
ts_tree_retain(lookahead);
ts_stack_push(self->stack, version, lookahead, false, 1);
StackPopResult pop = ts_stack_pop_all(self->stack, version);
StackSliceArray pop = ts_stack_pop_all(self->stack, version);
for (uint32_t i = 0; i < pop.slices.size; i++) {
StackSlice slice = pop.slices.contents[i];
for (uint32_t i = 0; i < pop.size; i++) {
StackSlice slice = pop.contents[i];
TreeArray trees = slice.trees;
Tree *root = NULL;
@ -792,7 +790,7 @@ static void parser__accept(Parser *self, StackVersion version,
}
}
ts_stack_remove_version(self->stack, pop.slices.contents[0].version);
ts_stack_remove_version(self->stack, pop.contents[0].version);
ts_stack_halt(self->stack, version);
}
@ -803,7 +801,7 @@ static bool parser__do_all_potential_reductions(Parser *self, StackVersion start
uint32_t version_count = ts_stack_version_count(self->stack);
if (version >= version_count) break;
TSStateId state = ts_stack_top_state(self->stack, version);
TSStateId state = ts_stack_state(self->stack, version);
bool has_shift_action = false;
array_clear(&self->reduce_actions);
@ -898,7 +896,7 @@ static void parser__handle_error(Parser *self, StackVersion version, TSSymbol lo
bool did_insert_missing_token = false;
for (StackVersion v = version; v < version_count;) {
if (!did_insert_missing_token) {
TSStateId state = ts_stack_top_state(self->stack, v);
TSStateId state = ts_stack_state(self->stack, v);
for (TSSymbol missing_symbol = 1;
missing_symbol < self->language->token_count;
missing_symbol++) {
@ -952,7 +950,7 @@ static void parser__halt_parse(Parser *self) {
ts_lexer_advance_to_end(&self->lexer);
Length remaining_length = length_sub(
self->lexer.current_position,
ts_stack_top_position(self->stack, 0)
ts_stack_position(self->stack, 0)
);
Tree *filler_node = ts_tree_make_error(&self->tree_pool, remaining_length, length_zero(), 0, self->language);
@ -970,28 +968,28 @@ static void parser__halt_parse(Parser *self) {
static bool parser__recover_to_state(Parser *self, StackVersion version, unsigned depth,
TSStateId goal_state) {
StackPopResult pop = ts_stack_pop_count(self->stack, version, depth);
StackSliceArray pop = ts_stack_pop_count(self->stack, version, depth);
StackVersion previous_version = STACK_VERSION_NONE;
for (unsigned i = 0; i < pop.slices.size; i++) {
StackSlice slice = pop.slices.contents[i];
for (unsigned i = 0; i < pop.size; i++) {
StackSlice slice = pop.contents[i];
if (slice.version == previous_version) {
ts_tree_array_delete(&self->tree_pool, &slice.trees);
array_erase(&pop.slices, i--);
array_erase(&pop, i--);
continue;
}
if (ts_stack_top_state(self->stack, slice.version) != goal_state) {
if (ts_stack_state(self->stack, slice.version) != goal_state) {
ts_stack_halt(self->stack, slice.version);
ts_tree_array_delete(&self->tree_pool, &slice.trees);
array_erase(&pop.slices, i--);
array_erase(&pop, i--);
continue;
}
StackPopResult error_pop = ts_stack_pop_error(self->stack, slice.version);
if (error_pop.slices.size > 0) {
StackSlice error_slice = error_pop.slices.contents[0];
StackSliceArray error_pop = ts_stack_pop_error(self->stack, slice.version);
if (error_pop.size > 0) {
StackSlice error_slice = error_pop.contents[0];
array_push_all(&error_slice.trees, &slice.trees);
array_delete(&slice.trees);
slice.trees = error_slice.trees;
@ -1023,7 +1021,7 @@ static bool parser__recover_to_state(Parser *self, StackVersion version, unsigne
static void parser__recover(Parser *self, StackVersion version, Tree *lookahead) {
bool did_recover = false;
unsigned previous_version_count = ts_stack_version_count(self->stack);
Length position = ts_stack_top_position(self->stack, version);
Length position = ts_stack_position(self->stack, version);
StackSummary *summary = ts_stack_get_summary(self->stack, version);
for (unsigned i = 0; i < summary->size; i++) {
@ -1088,7 +1086,7 @@ static void parser__recover(Parser *self, StackVersion version, Tree *lookahead)
}
static void parser__advance(Parser *self, StackVersion version, ReusableNode *reusable_node) {
TSStateId state = ts_stack_top_state(self->stack, version);
TSStateId state = ts_stack_state(self->stack, version);
TableEntry table_entry;
Tree *lookahead = parser__get_lookahead(self, version, &state, reusable_node, &table_entry);
@ -1124,12 +1122,12 @@ static void parser__advance(Parser *self, StackVersion version, ReusableNode *re
case TSParseActionTypeReduce: {
bool is_fragile = table_entry.action_count > 1;
LOG("reduce sym:%s, child_count:%u", SYM_NAME(action.params.symbol), action.params.child_count);
StackPopResult reduction = parser__reduce(
StackSliceArray reduction = parser__reduce(
self, version, action.params.symbol, action.params.child_count,
action.params.dynamic_precedence, action.params.alias_sequence_id,
is_fragile
);
StackSlice slice = *array_front(&reduction.slices);
StackSlice slice = *array_front(&reduction);
last_reduction_version = slice.version;
break;
}
@ -1168,12 +1166,12 @@ static void parser__advance(Parser *self, StackVersion version, ReusableNode *re
return;
} else if (lookahead->size.bytes == 0) {
ts_tree_release(&self->tree_pool, lookahead);
state = ts_stack_top_state(self->stack, version);
state = ts_stack_state(self->stack, version);
lookahead = parser__get_lookahead(self, version, &state, reusable_node, &table_entry);
}
}
state = ts_stack_top_state(self->stack, version);
state = ts_stack_state(self->stack, version);
ts_language_table_entry(self->language, state, lookahead->first_leaf.symbol, &table_entry);
}
}
@ -1222,7 +1220,7 @@ Tree *parser_parse(Parser *self, TSInput input, Tree *old_tree, bool halt_on_err
reusable_node = self->reusable_node;
while (!ts_stack_is_halted(self->stack, version)) {
position = ts_stack_top_position(self->stack, version).bytes;
position = ts_stack_position(self->stack, version).bytes;
if (position > last_position || (version > 0 && position == last_position)) {
last_position = position;
break;
@ -1230,9 +1228,9 @@ Tree *parser_parse(Parser *self, TSInput input, Tree *old_tree, bool halt_on_err
LOG("process version:%d, version_count:%u, state:%d, row:%u, col:%u",
version, ts_stack_version_count(self->stack),
ts_stack_top_state(self->stack, version),
ts_stack_top_position(self->stack, version).extent.row,
ts_stack_top_position(self->stack, version).extent.column);
ts_stack_state(self->stack, version),
ts_stack_position(self->stack, version).extent.row,
ts_stack_position(self->stack, version).extent.column);
parser__advance(self, version, &reusable_node);
LOG_STACK();

View file

@ -232,9 +232,9 @@ static void ts_stack__add_slice(Stack *self, StackVersion original_version, Stac
array_push(&self->slices, slice);
}
inline StackPopResult stack__iter(Stack *self, StackVersion version,
StackIterateInternalCallback callback, void *payload,
bool include_trees) {
inline StackSliceArray stack__iter(Stack *self, StackVersion version,
StackIterateInternalCallback callback, void *payload,
bool include_trees) {
array_clear(&self->slices);
array_clear(&self->iterators);
@ -315,7 +315,7 @@ inline StackPopResult stack__iter(Stack *self, StackVersion version,
}
}
return (StackPopResult){self->slices};
return self->slices;
}
Stack *ts_stack_new(TreePool *tree_pool) {
@ -360,11 +360,11 @@ uint32_t ts_stack_version_count(const Stack *self) {
return self->heads.size;
}
TSStateId ts_stack_top_state(const Stack *self, StackVersion version) {
TSStateId ts_stack_state(const Stack *self, StackVersion version) {
return array_get(&self->heads, version)->node->state;
}
Length ts_stack_top_position(const Stack *self, StackVersion version) {
Length ts_stack_position(const Stack *self, StackVersion version) {
return array_get(&self->heads, version)->node->position;
}
@ -409,8 +409,8 @@ inline StackIterateAction iterate_callback(void *payload, const Iterator *iterat
return session->callback(session->payload, iterator->node->state, &iterator->trees, iterator->tree_count);
}
StackPopResult ts_stack_iterate(Stack *self, StackVersion version,
StackIterateCallback callback, void *payload) {
StackSliceArray ts_stack_iterate(Stack *self, StackVersion version,
StackIterateCallback callback, void *payload) {
StackIterateSession session = {payload, callback};
return stack__iter(self, version, iterate_callback, &session, true);
}
@ -424,7 +424,7 @@ inline StackIterateAction pop_count_callback(void *payload, const Iterator *iter
}
}
StackPopResult ts_stack_pop_count(Stack *self, StackVersion version, uint32_t count) {
StackSliceArray ts_stack_pop_count(Stack *self, StackVersion version, uint32_t count) {
return stack__iter(self, version, pop_count_callback, &count, true);
}
@ -440,11 +440,11 @@ inline StackIterateAction pop_pending_callback(void *payload, const Iterator *it
}
}
StackPopResult ts_stack_pop_pending(Stack *self, StackVersion version) {
StackPopResult pop = stack__iter(self, version, pop_pending_callback, NULL, true);
if (pop.slices.size > 0) {
ts_stack_renumber_version(self, pop.slices.contents[0].version, version);
pop.slices.contents[0].version = version;
StackSliceArray ts_stack_pop_pending(Stack *self, StackVersion version) {
StackSliceArray pop = stack__iter(self, version, pop_pending_callback, NULL, true);
if (pop.size > 0) {
ts_stack_renumber_version(self, pop.contents[0].version, version);
pop.contents[0].version = version;
}
return pop;
}
@ -463,7 +463,7 @@ inline StackIterateAction pop_error_callback(void *payload, const Iterator *iter
}
}
StackPopResult ts_stack_pop_error(Stack *self, StackVersion version) {
StackSliceArray ts_stack_pop_error(Stack *self, StackVersion version) {
StackNode *node = array_get(&self->heads, version)->node;
for (unsigned i = 0; i < node->link_count; i++) {
if (node->links[i].tree && node->links[i].tree->symbol == ts_builtin_sym_error) {
@ -471,14 +471,14 @@ StackPopResult ts_stack_pop_error(Stack *self, StackVersion version) {
return stack__iter(self, version, pop_error_callback, &found_error, true);
}
}
return (StackPopResult){.slices = array_new()};
return (StackSliceArray){.size = 0};
}
inline StackIterateAction pop_all_callback(void *payload, const Iterator *iterator) {
return iterator->node->link_count == 0 ? StackIteratePop : StackIterateNone;
}
StackPopResult ts_stack_pop_all(Stack *self, StackVersion version) {
StackSliceArray ts_stack_pop_all(Stack *self, StackVersion version) {
return stack__iter(self, version, pop_all_callback, NULL, true);
}

View file

@ -19,12 +19,14 @@ typedef struct {
TreeArray trees;
StackVersion version;
} StackSlice;
typedef Array(StackSlice) StackSliceArray;
typedef struct {
StackSliceArray slices;
} StackPopResult;
Length position;
unsigned depth;
TSStateId state;
} StackSummaryEntry;
typedef Array(StackSummaryEntry) StackSummary;
typedef unsigned StackIterateAction;
enum {
@ -33,81 +35,74 @@ enum {
StackIteratePop = 2,
};
typedef struct {
Length position;
unsigned depth;
TSStateId state;
} StackSummaryEntry;
typedef Array(StackSummaryEntry) StackSummary;
typedef StackIterateAction (*StackIterateCallback)(void *, TSStateId state,
const TreeArray *trees,
uint32_t tree_count);
/*
* Create a parse stack.
*/
// Create a stack.
Stack *ts_stack_new(TreePool *);
/*
* Release any resources reserved by a parse stack.
*/
// Release the memory reserved for a given stack.
void ts_stack_delete(Stack *);
/*
* Get the stack's current number of versions.
*/
// Get the stack's current number of versions.
uint32_t ts_stack_version_count(const Stack *);
/*
* 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 *, StackVersion);
// 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_state(const Stack *, StackVersion);
// Get the number of trees that have been pushed to a given version of
// the stack.
unsigned ts_stack_push_count(const Stack *, StackVersion);
// In the event that trees were permanently removed from some version
// of the stack, decrease the version's push count to account for the
// removal.
void ts_stack_decrease_push_count(Stack *, StackVersion, unsigned);
// Get the last external token associated with a given version of the stack.
Tree *ts_stack_last_external_token(const Stack *, StackVersion);
// Set the last external token associated with a given version of the stack.
void ts_stack_set_last_external_token(Stack *, StackVersion, Tree *);
/*
* Get the position at the top of the given version of the stack. If the stack
* is empty, this returns zero.
*/
Length ts_stack_top_position(const Stack *, StackVersion);
// Get the position of the given version of the stack within the document.
Length ts_stack_position(const Stack *, StackVersion);
/*
* Push a tree and state onto the given head of the stack.
*/
// Push a tree and state onto the given version of the stack.
//
// This transfers ownership of the tree to the Stack. Callers that
// need to retain ownership of the tree for their own purposes should
// first retain the tree.
void ts_stack_push(Stack *, StackVersion, Tree *, bool, TSStateId);
/*
* 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 *, StackVersion, uint32_t count);
// 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 an array that
// specifies the index of each revealed version and the trees that were
// removed from that version.
StackSliceArray ts_stack_pop_count(Stack *, StackVersion, uint32_t count);
StackPopResult ts_stack_iterate(Stack *, StackVersion, StackIterateCallback, void *);
// Remove an error at the top of the given version of the stack.
StackSliceArray ts_stack_pop_error(Stack *, StackVersion);
StackPopResult ts_stack_pop_error(Stack *, StackVersion);
// Remove any pending trees from the top of the given version of the stack.
StackSliceArray ts_stack_pop_pending(Stack *, StackVersion);
StackPopResult ts_stack_pop_pending(Stack *, StackVersion);
StackPopResult ts_stack_pop_all(Stack *, StackVersion);
// Remove any all trees from the given version of the stack.
StackSliceArray ts_stack_pop_all(Stack *, StackVersion);
unsigned ts_stack_depth_since_error(Stack *, StackVersion);
int ts_stack_dynamic_precedence(Stack *, StackVersion);
// Compute a summary of all the parse states near the top of the given
// version of the stack and store the summary for later retrieval.
void ts_stack_record_summary(Stack *, StackVersion, unsigned max_depth);
// Retrieve a summary of all the parse states near the top of the
// given version of the stack.
StackSummary *ts_stack_get_summary(Stack *, StackVersion);
unsigned ts_stack_error_cost(const Stack *, StackVersion version);
@ -128,17 +123,13 @@ void ts_stack_swap_versions(Stack *, StackVersion, StackVersion);
StackVersion ts_stack_copy_version(Stack *, StackVersion);
/*
* Remove the given version from the stack.
*/
// Remove the given version from the stack.
void ts_stack_remove_version(Stack *, StackVersion);
/*
* Remove all entries from the stack.
*/
void ts_stack_clear(Stack *);
bool ts_stack_print_dot_graph(Stack *, const char **, FILE *);
StackSliceArray ts_stack_iterate(Stack *, StackVersion, StackIterateCallback, void *);
#ifdef __cplusplus
}

View file

@ -108,23 +108,23 @@ describe("Stack", [&]() {
describe("push(version, tree, is_pending, state)", [&]() {
it("adds entries to the given version of the stack", [&]() {
AssertThat(ts_stack_version_count(stack), Equals<size_t>(1));
AssertThat(ts_stack_top_state(stack, 0), Equals(1));
AssertThat(ts_stack_top_position(stack, 0), Equals(length_zero()));
AssertThat(ts_stack_state(stack, 0), Equals(1));
AssertThat(ts_stack_position(stack, 0), Equals(length_zero()));
// . <──0── A*
push(0, trees[0], stateA);
AssertThat(ts_stack_top_state(stack, 0), Equals(stateA));
AssertThat(ts_stack_top_position(stack, 0), Equals(tree_len));
AssertThat(ts_stack_state(stack, 0), Equals(stateA));
AssertThat(ts_stack_position(stack, 0), Equals(tree_len));
// . <──0── A <──1── B*
push(0, trees[1], stateB);
AssertThat(ts_stack_top_state(stack, 0), Equals(stateB));
AssertThat(ts_stack_top_position(stack, 0), Equals(tree_len * 2));
AssertThat(ts_stack_state(stack, 0), Equals(stateB));
AssertThat(ts_stack_position(stack, 0), Equals(tree_len * 2));
// . <──0── A <──1── B <──2── C*
push(0, trees[2], stateC);
AssertThat(ts_stack_top_state(stack, 0), Equals(stateC));
AssertThat(ts_stack_top_position(stack, 0), Equals(tree_len * 3));
AssertThat(ts_stack_state(stack, 0), Equals(stateC));
AssertThat(ts_stack_position(stack, 0), Equals(tree_len * 3));
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
{stateC, 0},
@ -256,16 +256,16 @@ describe("Stack", [&]() {
// . <──0── A <──1── B <──2── C*
// ↑
// └─*
StackPopResult pop = ts_stack_pop_count(stack, 0, 2);
AssertThat(pop.slices.size, Equals<size_t>(1));
StackSliceArray pop = ts_stack_pop_count(stack, 0, 2);
AssertThat(pop.size, Equals<size_t>(1));
AssertThat(ts_stack_version_count(stack), Equals<size_t>(2));
StackSlice slice = pop.slices.contents[0];
StackSlice slice = pop.contents[0];
AssertThat(slice.version, Equals<StackVersion>(1));
AssertThat(slice.trees, Equals(vector<Tree *>({ trees[1], trees[2] })));
AssertThat(ts_stack_top_state(stack, 1), Equals(stateA));
AssertThat(ts_stack_state(stack, 1), Equals(stateA));
free_slice_array(&pool,&pop.slices);
free_slice_array(&pool,&pop);
});
it("does not count 'extra' trees toward the given count", [&]() {
@ -274,14 +274,14 @@ describe("Stack", [&]() {
// . <──0── A <──1── B <──2── C*
// ↑
// └─*
StackPopResult pop = ts_stack_pop_count(stack, 0, 2);
AssertThat(pop.slices.size, Equals<size_t>(1));
StackSliceArray pop = ts_stack_pop_count(stack, 0, 2);
AssertThat(pop.size, Equals<size_t>(1));
StackSlice slice = pop.slices.contents[0];
StackSlice slice = pop.contents[0];
AssertThat(slice.trees, Equals(vector<Tree *>({ trees[0], trees[1], trees[2] })));
AssertThat(ts_stack_top_state(stack, 1), Equals(1));
AssertThat(ts_stack_state(stack, 1), Equals(1));
free_slice_array(&pool,&pop.slices);
free_slice_array(&pool,&pop);
});
describe("when the version has been merged", [&]() {
@ -290,8 +290,8 @@ describe("Stack", [&]() {
// ↑ |
// └───4─── E <──5── F <──6───┘
push(0, trees[3], stateD);
StackPopResult pop = ts_stack_pop_count(stack, 0, 3);
free_slice_array(&pool,&pop.slices);
StackSliceArray pop = ts_stack_pop_count(stack, 0, 3);
free_slice_array(&pool,&pop);
push(1, trees[4], stateE);
push(1, trees[5], stateF);
push(1, trees[6], stateD);
@ -318,14 +318,14 @@ describe("Stack", [&]() {
// | └*
// |
// └───4─── E*
StackPopResult pop = ts_stack_pop_count(stack, 0, 3);
AssertThat(pop.slices.size, Equals<size_t>(2));
StackSliceArray pop = ts_stack_pop_count(stack, 0, 3);
AssertThat(pop.size, Equals<size_t>(2));
StackSlice slice1 = pop.slices.contents[0];
StackSlice slice1 = pop.contents[0];
AssertThat(slice1.version, Equals<StackVersion>(1));
AssertThat(slice1.trees, Equals(vector<Tree *>({ trees[2], trees[3], trees[10] })));
StackSlice slice2 = pop.slices.contents[1];
StackSlice slice2 = pop.contents[1];
AssertThat(slice2.version, Equals<StackVersion>(2));
AssertThat(slice2.trees, Equals(vector<Tree *>({ trees[5], trees[6], trees[10] })));
@ -351,7 +351,7 @@ describe("Stack", [&]() {
{1, 2},
})));
free_slice_array(&pool,&pop.slices);
free_slice_array(&pool,&pop);
});
});
@ -362,18 +362,18 @@ describe("Stack", [&]() {
// └───5─── F <──6── G <──7───┘
// |
// └*
StackPopResult pop = ts_stack_pop_count(stack, 0, 1);
AssertThat(pop.slices.size, Equals<size_t>(1));
StackSliceArray pop = ts_stack_pop_count(stack, 0, 1);
AssertThat(pop.size, Equals<size_t>(1));
StackSlice slice1 = pop.slices.contents[0];
StackSlice slice1 = pop.contents[0];
AssertThat(slice1.version, Equals<StackVersion>(1));
AssertThat(slice1.trees, Equals(vector<Tree *>({ trees[10] })));
AssertThat(ts_stack_version_count(stack), Equals<size_t>(2));
AssertThat(ts_stack_top_state(stack, 0), Equals(stateI));
AssertThat(ts_stack_top_state(stack, 1), Equals(stateD));
AssertThat(ts_stack_state(stack, 0), Equals(stateI));
AssertThat(ts_stack_state(stack, 1), Equals(stateD));
free_slice_array(&pool,&pop.slices);
free_slice_array(&pool,&pop);
});
});
@ -384,22 +384,22 @@ describe("Stack", [&]() {
// ├───4─── E <──5── F <──6───┘
// |
// └*
StackPopResult pop = ts_stack_pop_count(stack, 0, 4);
AssertThat(pop.slices.size, Equals<size_t>(2));
StackSliceArray pop = ts_stack_pop_count(stack, 0, 4);
AssertThat(pop.size, Equals<size_t>(2));
StackSlice slice1 = pop.slices.contents[0];
StackSlice slice1 = pop.contents[0];
AssertThat(slice1.version, Equals<StackVersion>(1));
AssertThat(slice1.trees, Equals(vector<Tree *>({ trees[1], trees[2], trees[3], trees[10] })));
StackSlice slice2 = pop.slices.contents[1];
StackSlice slice2 = pop.contents[1];
AssertThat(slice2.version, Equals<StackVersion>(1));
AssertThat(slice2.trees, Equals(vector<Tree *>({ trees[4], trees[5], trees[6], trees[10] })));
AssertThat(ts_stack_version_count(stack), Equals<size_t>(2));
AssertThat(ts_stack_top_state(stack, 0), Equals(stateI));
AssertThat(ts_stack_top_state(stack, 1), Equals(stateA));
AssertThat(ts_stack_state(stack, 0), Equals(stateI));
AssertThat(ts_stack_state(stack, 1), Equals(stateA));
free_slice_array(&pool,&pop.slices);
free_slice_array(&pool,&pop);
});
});
@ -410,8 +410,8 @@ describe("Stack", [&]() {
// ├───4─── E <──5── F <──6───┘
// | |
// └───7─── G <──8── H <──9───┘
StackPopResult pop = ts_stack_pop_count(stack, 0, 4);
free_slice_array(&pool,&pop.slices);
StackSliceArray pop = ts_stack_pop_count(stack, 0, 4);
free_slice_array(&pool,&pop);
push(1, trees[7], stateG);
push(1, trees[8], stateH);
push(1, trees[9], stateD);
@ -440,27 +440,27 @@ describe("Stack", [&]() {
// |
// └───7─── G <──8── H*
pop = ts_stack_pop_count(stack, 0, 2);
AssertThat(pop.slices.size, Equals<size_t>(3));
AssertThat(pop.size, Equals<size_t>(3));
StackSlice slice1 = pop.slices.contents[0];
StackSlice slice1 = pop.contents[0];
AssertThat(slice1.version, Equals<StackVersion>(1));
AssertThat(slice1.trees, Equals(vector<Tree *>({ trees[3], trees[10] })));
StackSlice slice2 = pop.slices.contents[1];
StackSlice slice2 = pop.contents[1];
AssertThat(slice2.version, Equals<StackVersion>(2));
AssertThat(slice2.trees, Equals(vector<Tree *>({ trees[6], trees[10] })));
StackSlice slice3 = pop.slices.contents[2];
StackSlice slice3 = pop.contents[2];
AssertThat(slice3.version, Equals<StackVersion>(3));
AssertThat(slice3.trees, Equals(vector<Tree *>({ trees[9], trees[10] })));
AssertThat(ts_stack_version_count(stack), Equals<size_t>(4));
AssertThat(ts_stack_top_state(stack, 0), Equals(stateI));
AssertThat(ts_stack_top_state(stack, 1), Equals(stateC));
AssertThat(ts_stack_top_state(stack, 2), Equals(stateF));
AssertThat(ts_stack_top_state(stack, 3), Equals(stateH));
AssertThat(ts_stack_state(stack, 0), Equals(stateI));
AssertThat(ts_stack_state(stack, 1), Equals(stateC));
AssertThat(ts_stack_state(stack, 2), Equals(stateF));
AssertThat(ts_stack_state(stack, 3), Equals(stateH));
free_slice_array(&pool,&pop.slices);
free_slice_array(&pool,&pop);
});
});
});
@ -475,15 +475,15 @@ describe("Stack", [&]() {
ts_stack_push(stack, 0, trees[1], true, stateB);
ts_tree_retain(trees[1]);
StackPopResult pop = ts_stack_pop_pending(stack, 0);
AssertThat(pop.slices.size, Equals<size_t>(1));
StackSliceArray pop = ts_stack_pop_pending(stack, 0);
AssertThat(pop.size, Equals<size_t>(1));
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
{stateA, 0},
{1, 1},
})));
free_slice_array(&pool,&pop.slices);
free_slice_array(&pool,&pop);
});
it("skips entries whose trees are extra", [&]() {
@ -496,24 +496,24 @@ describe("Stack", [&]() {
push(0, trees[2], stateB);
push(0, trees[3], stateB);
StackPopResult pop = ts_stack_pop_pending(stack, 0);
AssertThat(pop.slices.size, Equals<size_t>(1));
StackSliceArray pop = ts_stack_pop_pending(stack, 0);
AssertThat(pop.size, Equals<size_t>(1));
AssertThat(pop.slices.contents[0].trees, Equals(vector<Tree *>({ trees[1], trees[2], trees[3] })));
AssertThat(pop.contents[0].trees, Equals(vector<Tree *>({ trees[1], trees[2], trees[3] })));
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
{stateA, 0},
{1, 1},
})));
free_slice_array(&pool,&pop.slices);
free_slice_array(&pool,&pop);
});
it("does nothing if the top node was not pushed in pending mode", [&]() {
push(0, trees[1], stateB);
StackPopResult pop = ts_stack_pop_pending(stack, 0);
AssertThat(pop.slices.size, Equals<size_t>(0));
StackSliceArray pop = ts_stack_pop_pending(stack, 0);
AssertThat(pop.size, Equals<size_t>(0));
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
{stateB, 0},
@ -521,7 +521,7 @@ describe("Stack", [&]() {
{1, 2},
})));
free_slice_array(&pool,&pop.slices);
free_slice_array(&pool,&pop);
});
});