Rename ParseStack -> Stack

This commit is contained in:
Max Brunsfeld 2015-09-18 18:04:52 -07:00
parent b3d883e128
commit 6254f45c1b
6 changed files with 218 additions and 226 deletions

View file

@ -104,7 +104,7 @@
'src/runtime/document.c',
'src/runtime/lexer.c',
'src/runtime/node.c',
'src/runtime/parse_stack.c',
'src/runtime/stack.c',
'src/runtime/parser.c',
'src/runtime/string_input.c',
'src/runtime/tree.c',

View file

@ -1,5 +1,5 @@
#include "runtime/runtime_spec_helper.h"
#include "runtime/parse_stack.h"
#include "runtime/stack.h"
#include "runtime/tree.h"
#include "runtime/length.h"
@ -29,14 +29,14 @@ TSTree * tree_selection_spy_callback(void *data, TSTree *left, TSTree *right) {
START_TEST
describe("ParseStack", [&]() {
ParseStack *stack;
describe("Stack", [&]() {
Stack *stack;
const size_t tree_count = 8;
TSTree *trees[tree_count];
TreeSelectionSpy tree_selection_spy{0, NULL, {NULL, NULL}};
before_each([&]() {
stack = ts_parse_stack_new({
stack = ts_stack_new({
&tree_selection_spy,
tree_selection_spy_callback
});
@ -47,92 +47,92 @@ describe("ParseStack", [&]() {
});
after_each([&]() {
ts_parse_stack_delete(stack);
ts_stack_delete(stack);
for (size_t i = 0; i < tree_count; i++)
ts_tree_release(trees[i]);
});
describe("pushing entries to the stack", [&]() {
it("adds entries to the stack", [&]() {
AssertThat(ts_parse_stack_head_count(stack), Equals(1));
AssertThat(ts_parse_stack_head(stack, 0), Equals<const ParseStackEntry *>(nullptr));
AssertThat(ts_stack_head_count(stack), Equals(1));
AssertThat(ts_stack_head(stack, 0), Equals<const StackEntry *>(nullptr));
/*
* A0.
*/
ts_parse_stack_push(stack, 0, stateA, trees[0]);
const ParseStackEntry *entry1 = ts_parse_stack_head(stack, 0);
AssertThat(*entry1, Equals<ParseStackEntry>({trees[0], stateA}));
AssertThat(ts_parse_stack_entry_next_count(entry1), Equals(1));
AssertThat(ts_parse_stack_entry_next(entry1, 0), Equals<const ParseStackEntry *>(nullptr));
ts_stack_push(stack, 0, stateA, trees[0]);
const StackEntry *entry1 = ts_stack_head(stack, 0);
AssertThat(*entry1, Equals<StackEntry>({trees[0], stateA}));
AssertThat(ts_stack_entry_next_count(entry1), Equals(1));
AssertThat(ts_stack_entry_next(entry1, 0), Equals<const StackEntry *>(nullptr));
/*
* A0__B1.
*/
ts_parse_stack_push(stack, 0, stateB, trees[1]);
const ParseStackEntry *entry2 = ts_parse_stack_head(stack, 0);
AssertThat(*entry2, Equals<ParseStackEntry>({trees[1], stateB}));
AssertThat(ts_parse_stack_entry_next_count(entry2), Equals(1));
AssertThat(ts_parse_stack_entry_next(entry2, 0), Equals(entry1));
ts_stack_push(stack, 0, stateB, trees[1]);
const StackEntry *entry2 = ts_stack_head(stack, 0);
AssertThat(*entry2, Equals<StackEntry>({trees[1], stateB}));
AssertThat(ts_stack_entry_next_count(entry2), Equals(1));
AssertThat(ts_stack_entry_next(entry2, 0), Equals(entry1));
/*
* A0__B1__C2.
*/
ts_parse_stack_push(stack, 0, stateC, trees[2]);
const ParseStackEntry *entry3 = ts_parse_stack_head(stack, 0);
AssertThat(*entry3, Equals<ParseStackEntry>({trees[2], stateC}));
AssertThat(ts_parse_stack_entry_next_count(entry3), Equals(1));
AssertThat(ts_parse_stack_entry_next(entry3, 0), Equals(entry2));
ts_stack_push(stack, 0, stateC, trees[2]);
const StackEntry *entry3 = ts_stack_head(stack, 0);
AssertThat(*entry3, Equals<StackEntry>({trees[2], stateC}));
AssertThat(ts_stack_entry_next_count(entry3), Equals(1));
AssertThat(ts_stack_entry_next(entry3, 0), Equals(entry2));
});
});
describe("popping nodes from the stack", [&]() {
ParseStackPopResultList pop;
StackPopResultList pop;
before_each([&]() {
/*
* A0__B1__C2.
*/
ts_parse_stack_push(stack, 0, stateA, trees[0]);
ts_parse_stack_push(stack, 0, stateB, trees[1]);
ts_parse_stack_push(stack, 0, stateC, trees[2]);
ts_stack_push(stack, 0, stateA, trees[0]);
ts_stack_push(stack, 0, stateB, trees[1]);
ts_stack_push(stack, 0, stateC, trees[2]);
});
it("removes the given number of nodes from the stack", [&]() {
/*
* A0.
*/
pop = ts_parse_stack_pop(stack, 0, 2, false);
pop = ts_stack_pop(stack, 0, 2, false);
AssertThat(pop.size, Equals(1));
AssertThat(pop.contents[0].tree_count, Equals(2));
AssertThat(pop.contents[0].trees[0], Equals(trees[1]));
AssertThat(pop.contents[0].trees[1], Equals(trees[2]));
AssertThat(*ts_parse_stack_head(stack, 0), Equals<ParseStackEntry>({trees[0], stateA}));
AssertThat(*ts_stack_head(stack, 0), Equals<StackEntry>({trees[0], stateA}));
/*
* .
*/
pop = ts_parse_stack_pop(stack, 0, 1, false);
pop = ts_stack_pop(stack, 0, 1, false);
AssertThat(pop.size, Equals(1));
AssertThat(pop.contents[0].tree_count, Equals(1));
AssertThat(pop.contents[0].trees[0], Equals(trees[0]));
AssertThat(ts_parse_stack_head(stack, 0), Equals<const ParseStackEntry *>(nullptr));
AssertThat(ts_stack_head(stack, 0), Equals<const StackEntry *>(nullptr));
});
it("does not count 'extra' trees toward the count", [&]() {
ts_tree_set_extra(trees[1]);
pop = ts_parse_stack_pop(stack, 0, 2, false);
pop = ts_stack_pop(stack, 0, 2, false);
AssertThat(pop.size, Equals(1));
AssertThat(pop.contents[0].tree_count, Equals(3));
AssertThat(pop.contents[0].trees[0], Equals(trees[0]));
AssertThat(pop.contents[0].trees[1], Equals(trees[1]));
AssertThat(pop.contents[0].trees[2], Equals(trees[2]));
AssertThat(ts_parse_stack_head(stack, 0), Equals<const ParseStackEntry *>(nullptr));
AssertThat(ts_stack_head(stack, 0), Equals<const StackEntry *>(nullptr));
});
it("pops the entire stack when given a negative count", [&]() {
pop = ts_parse_stack_pop(stack, 0, -1, false);
pop = ts_stack_pop(stack, 0, -1, false);
AssertThat(pop.size, Equals(1));
AssertThat(pop.contents[0].tree_count, Equals(3));
@ -147,35 +147,35 @@ describe("ParseStack", [&]() {
/*
* A0__B1__C2.
*/
ts_parse_stack_push(stack, 0, stateA, trees[0]);
ts_parse_stack_push(stack, 0, stateB, trees[1]);
ts_parse_stack_push(stack, 0, stateC, trees[2]);
ts_stack_push(stack, 0, stateA, trees[0]);
ts_stack_push(stack, 0, stateB, trees[1]);
ts_stack_push(stack, 0, stateC, trees[2]);
int new_index = ts_parse_stack_split(stack, 0);
AssertThat(ts_parse_stack_head_count(stack), Equals(2));
int new_index = ts_stack_split(stack, 0);
AssertThat(ts_stack_head_count(stack), Equals(2));
AssertThat(new_index, Equals(1));
/*
* A0__B1__C2__D3.
* \.
*/
ts_parse_stack_push(stack, 0, stateD, trees[3]);
ts_parse_stack_pop(stack, 1, 1, false);
ts_stack_push(stack, 0, stateD, trees[3]);
ts_stack_pop(stack, 1, 1, false);
AssertThat(ts_parse_stack_head_count(stack), Equals(2));
AssertThat(*ts_parse_stack_head(stack, 0), Equals<ParseStackEntry>({trees[3], stateD}));
AssertThat(*ts_parse_stack_head(stack, 1), Equals<ParseStackEntry>({trees[1], stateB}));
AssertThat(ts_stack_head_count(stack), Equals(2));
AssertThat(*ts_stack_head(stack, 0), Equals<StackEntry>({trees[3], stateD}));
AssertThat(*ts_stack_head(stack, 1), Equals<StackEntry>({trees[1], stateB}));
/*
* A0__B1__C2__D3.
* \__E4__F3.
*/
ts_parse_stack_push(stack, 1, stateE, trees[4]);
ts_parse_stack_push(stack, 1, stateF, trees[3]);
ts_stack_push(stack, 1, stateE, trees[4]);
ts_stack_push(stack, 1, stateF, trees[3]);
AssertThat(ts_parse_stack_head_count(stack), Equals(2));
AssertThat(*ts_parse_stack_head(stack, 0), Equals<ParseStackEntry>({trees[3], stateD}));
AssertThat(*ts_parse_stack_head(stack, 1), Equals<ParseStackEntry>({trees[3], stateF}));
AssertThat(ts_stack_head_count(stack), Equals(2));
AssertThat(*ts_stack_head(stack, 0), Equals<StackEntry>({trees[3], stateD}));
AssertThat(*ts_stack_head(stack, 1), Equals<StackEntry>({trees[3], stateF}));
});
});
@ -185,17 +185,17 @@ describe("ParseStack", [&]() {
* A0__B1__C2__D3.
* \__E4__F5.
*/
ts_parse_stack_push(stack, 0, stateA, trees[0]);
ts_parse_stack_push(stack, 0, stateB, trees[1]);
ts_parse_stack_split(stack, 0);
ts_parse_stack_push(stack, 0, stateC, trees[2]);
ts_parse_stack_push(stack, 0, stateD, trees[3]);
ts_parse_stack_push(stack, 1, stateE, trees[4]);
ts_parse_stack_push(stack, 1, stateF, trees[5]);
ts_stack_push(stack, 0, stateA, trees[0]);
ts_stack_push(stack, 0, stateB, trees[1]);
ts_stack_split(stack, 0);
ts_stack_push(stack, 0, stateC, trees[2]);
ts_stack_push(stack, 0, stateD, trees[3]);
ts_stack_push(stack, 1, stateE, trees[4]);
ts_stack_push(stack, 1, stateF, trees[5]);
AssertThat(ts_parse_stack_head_count(stack), Equals(2));
AssertThat(*ts_parse_stack_head(stack, 0), Equals<ParseStackEntry>({trees[3], stateD}));
AssertThat(*ts_parse_stack_head(stack, 1), Equals<ParseStackEntry>({trees[5], stateF}));
AssertThat(ts_stack_head_count(stack), Equals(2));
AssertThat(*ts_stack_head(stack, 0), Equals<StackEntry>({trees[3], stateD}));
AssertThat(*ts_stack_head(stack, 1), Equals<StackEntry>({trees[5], stateF}));
});
describe("when the trees are identical", [&]() {
@ -204,17 +204,17 @@ describe("ParseStack", [&]() {
* A0__B1__C2__D3__G6.
* \__E4__F5__/
*/
bool merged = ts_parse_stack_push(stack, 0, stateG, trees[6]);
bool merged = ts_stack_push(stack, 0, stateG, trees[6]);
AssertThat(merged, IsFalse());
merged = ts_parse_stack_push(stack, 1, stateG, trees[6]);
merged = ts_stack_push(stack, 1, stateG, trees[6]);
AssertThat(merged, IsTrue());
AssertThat(ts_parse_stack_head_count(stack), Equals(1));
const ParseStackEntry *entry1 = ts_parse_stack_head(stack, 0);
AssertThat(*entry1, Equals<ParseStackEntry>({trees[6], stateG}));
AssertThat(ts_parse_stack_entry_next_count(entry1), Equals(2));
AssertThat(*ts_parse_stack_entry_next(entry1, 0), Equals<ParseStackEntry>({trees[3], stateD}));
AssertThat(*ts_parse_stack_entry_next(entry1, 1), Equals<ParseStackEntry>({trees[5], stateF}));
AssertThat(ts_stack_head_count(stack), Equals(1));
const StackEntry *entry1 = ts_stack_head(stack, 0);
AssertThat(*entry1, Equals<StackEntry>({trees[6], stateG}));
AssertThat(ts_stack_entry_next_count(entry1), Equals(2));
AssertThat(*ts_stack_entry_next(entry1, 0), Equals<StackEntry>({trees[3], stateD}));
AssertThat(*ts_stack_entry_next(entry1, 1), Equals<StackEntry>({trees[5], stateF}));
});
});
@ -229,16 +229,16 @@ describe("ParseStack", [&]() {
* A0__B1__C2__D3__G(6|7)
* \__E4__F5____/
*/
bool merged = ts_parse_stack_push(stack, 0, stateG, trees[6]);
bool merged = ts_stack_push(stack, 0, stateG, trees[6]);
AssertThat(merged, IsFalse());
merged = ts_parse_stack_push(stack, 1, stateG, trees[7]);
merged = ts_stack_push(stack, 1, stateG, trees[7]);
AssertThat(merged, IsTrue());
AssertThat(ts_parse_stack_head_count(stack), Equals(1));
AssertThat(ts_stack_head_count(stack), Equals(1));
AssertThat(tree_selection_spy.call_count, Equals(1));
AssertThat(tree_selection_spy.arguments[0], Equals(trees[6]));
AssertThat(tree_selection_spy.arguments[1], Equals(trees[7]));
AssertThat(*ts_parse_stack_head(stack, 0), Equals<ParseStackEntry>({
AssertThat(*ts_stack_head(stack, 0), Equals<StackEntry>({
trees[7],
stateG
}));
@ -251,28 +251,28 @@ describe("ParseStack", [&]() {
* A0__B1__C2__D3__G6__H7.
* \__E4__F5__G6.
*/
bool merged = ts_parse_stack_push(stack, 0, stateG, trees[6]);
bool merged = ts_stack_push(stack, 0, stateG, trees[6]);
AssertThat(merged, IsFalse());
merged = ts_parse_stack_push(stack, 0, stateH, trees[7]);
merged = ts_stack_push(stack, 0, stateH, trees[7]);
AssertThat(merged, IsFalse());
merged = ts_parse_stack_push(stack, 1, stateG, trees[6]);
merged = ts_stack_push(stack, 1, stateG, trees[6]);
AssertThat(merged, IsFalse());
/*
* A0__B1__C2__D3__G6__H7.
* \__E4__F5_/
*/
merged = ts_parse_stack_push(stack, 1, stateH, trees[7]);
merged = ts_stack_push(stack, 1, stateH, trees[7]);
AssertThat(merged, IsTrue());
AssertThat(ts_parse_stack_head_count(stack), Equals(1));
ParseStackEntry *head = ts_parse_stack_head(stack, 0);
AssertThat(*head, Equals<ParseStackEntry>({trees[7], stateH}))
AssertThat(ts_parse_stack_entry_next_count(head), Equals(1));
AssertThat(ts_stack_head_count(stack), Equals(1));
StackEntry *head = ts_stack_head(stack, 0);
AssertThat(*head, Equals<StackEntry>({trees[7], stateH}))
AssertThat(ts_stack_entry_next_count(head), Equals(1));
ParseStackEntry *next = ts_parse_stack_entry_next(head, 0);
AssertThat(*next, Equals<ParseStackEntry>({trees[6], stateG}))
AssertThat(ts_parse_stack_entry_next_count(next), Equals(2));
StackEntry *next = ts_stack_entry_next(head, 0);
AssertThat(*next, Equals<StackEntry>({trees[6], stateG}))
AssertThat(ts_stack_entry_next_count(next), Equals(2));
});
});
});
@ -283,15 +283,15 @@ describe("ParseStack", [&]() {
* A0__B1__C2__D3__G6.
* \__E4__F5__/
*/
ts_parse_stack_push(stack, 0, stateA, trees[0]);
ts_parse_stack_push(stack, 0, stateB, trees[1]);
ts_parse_stack_split(stack, 0);
ts_parse_stack_push(stack, 0, stateC, trees[2]);
ts_parse_stack_push(stack, 0, stateD, trees[3]);
ts_parse_stack_push(stack, 0, stateG, trees[6]);
ts_parse_stack_push(stack, 1, stateE, trees[4]);
ts_parse_stack_push(stack, 1, stateF, trees[5]);
ts_parse_stack_push(stack, 1, stateG, trees[6]);
ts_stack_push(stack, 0, stateA, trees[0]);
ts_stack_push(stack, 0, stateB, trees[1]);
ts_stack_split(stack, 0);
ts_stack_push(stack, 0, stateC, trees[2]);
ts_stack_push(stack, 0, stateD, trees[3]);
ts_stack_push(stack, 0, stateG, trees[6]);
ts_stack_push(stack, 1, stateE, trees[4]);
ts_stack_push(stack, 1, stateF, trees[5]);
ts_stack_push(stack, 1, stateG, trees[6]);
});
describe("when there are two paths that lead to two different heads", [&]() {
@ -300,24 +300,24 @@ describe("ParseStack", [&]() {
* A0__B1__C2.
* \__E4.
*/
ParseStackPopResultList pop = ts_parse_stack_pop(stack, 0, 2, false);
StackPopResultList pop = ts_stack_pop(stack, 0, 2, false);
AssertThat(pop.size, Equals(2));
ParseStackPopResult pop1 = pop.contents[0];
StackPopResult pop1 = pop.contents[0];
AssertThat(pop1.index, Equals(0));
AssertThat(pop1.tree_count, Equals(2));
AssertThat(pop1.trees[0], Equals(trees[3]));
AssertThat(pop1.trees[1], Equals(trees[6]));
ParseStackPopResult pop2 = pop.contents[1];
StackPopResult pop2 = pop.contents[1];
AssertThat(pop2.index, Equals(1));
AssertThat(pop2.tree_count, Equals(2));
AssertThat(pop2.trees[0], Equals(trees[5]));
AssertThat(pop2.trees[1], Equals(trees[6]));
AssertThat(ts_parse_stack_head_count(stack), Equals(2));
AssertThat(*ts_parse_stack_head(stack, 0), Equals<ParseStackEntry>({trees[2], stateC}));
AssertThat(*ts_parse_stack_head(stack, 1), Equals<ParseStackEntry>({trees[4], stateE}));
AssertThat(ts_stack_head_count(stack), Equals(2));
AssertThat(*ts_stack_head(stack, 0), Equals<StackEntry>({trees[2], stateC}));
AssertThat(*ts_stack_head(stack, 1), Equals<StackEntry>({trees[4], stateE}));
});
});
@ -327,18 +327,18 @@ describe("ParseStack", [&]() {
* A0__B1__C2__D3__G6__H7.
* \__E4__F5__/
*/
bool merged = ts_parse_stack_push(stack, 0, stateH, trees[7]);
bool merged = ts_stack_push(stack, 0, stateH, trees[7]);
AssertThat(merged, IsFalse());
AssertThat(ts_parse_stack_head_count(stack), Equals(1));
AssertThat(ts_stack_head_count(stack), Equals(1));
/*
* A0__B1__C2__D3__G6.
* \__E4__F5__/
*/
ParseStackPopResultList pop = ts_parse_stack_pop(stack, 0, 1, false);
StackPopResultList pop = ts_stack_pop(stack, 0, 1, false);
AssertThat(pop.size, Equals(1));
AssertThat(ts_parse_stack_head_count(stack), Equals(1));
AssertThat(ts_stack_head_count(stack), Equals(1));
});
});
@ -348,8 +348,8 @@ describe("ParseStack", [&]() {
* A0__B1__C2__D3.
* \__E4__F5.
*/
ParseStackPopResultList pop = ts_parse_stack_pop(stack, 0, 1, false);
AssertThat(ts_parse_stack_head_count(stack), Equals(2));
StackPopResultList pop = ts_stack_pop(stack, 0, 1, false);
AssertThat(ts_stack_head_count(stack), Equals(2));
AssertThat(pop.size, Equals(2));
AssertThat(pop.contents[0].index, Equals(0));
@ -363,9 +363,9 @@ describe("ParseStack", [&]() {
/*
* A0__B1.
*/
ParseStackPopResultList pop = ts_parse_stack_pop(stack, 0, 3, false);
AssertThat(ts_parse_stack_head_count(stack), Equals(1));
AssertThat(*ts_parse_stack_head(stack, 0), Equals<ParseStackEntry>({trees[1], stateB}));
StackPopResultList pop = ts_stack_pop(stack, 0, 3, false);
AssertThat(ts_stack_head_count(stack), Equals(1));
AssertThat(*ts_stack_head(stack, 0), Equals<StackEntry>({trees[1], stateB}));
AssertThat(pop.size, Equals(2));
AssertThat(pop.contents[0].tree_count, Equals(3));
@ -381,10 +381,10 @@ describe("ParseStack", [&]() {
END_TEST
bool operator==(const ParseStackEntry &left, const ParseStackEntry &right) {
bool operator==(const StackEntry &left, const StackEntry &right) {
return left.state == right.state && ts_tree_eq(left.tree, right.tree);
}
std::ostream &operator<<(std::ostream &stream, const ParseStackEntry &entry) {
std::ostream &operator<<(std::ostream &stream, const StackEntry &entry) {
return stream << "{" << entry.state << ", " << entry.tree << "}";
}

View file

@ -104,7 +104,7 @@ static void ts_parser__get_next_lookahead(TSParser *parser, bool error_mode) {
continue;
}
TSStateId top_state = ts_parse_stack_top_state(parser->stack, 0);
TSStateId top_state = ts_stack_top_state(parser->stack, 0);
TSSymbol symbol = parser->reusable_subtree->symbol;
if (ts_language__action(parser->language, top_state, symbol).type ==
TSParseActionTypeError) {
@ -125,7 +125,7 @@ static void ts_parser__get_next_lookahead(TSParser *parser, bool error_mode) {
TSStateId lex_state =
error_mode
? ts_lex_state_error
: parser->language->lex_states[ts_parse_stack_top_state(parser->stack, 0)];
: parser->language->lex_states[ts_stack_top_state(parser->stack, 0)];
parser->lookahead = parser->language->lex_fn(&parser->lexer, lex_state);
}
@ -135,7 +135,7 @@ static void ts_parser__get_next_lookahead(TSParser *parser, bool error_mode) {
*/
static void ts_parser__shift(TSParser *parser, int head, TSStateId parse_state) {
ts_parse_stack_push(parser->stack, head, parse_state, parser->lookahead);
ts_stack_push(parser->stack, head, parse_state, parser->lookahead);
}
static void ts_parser__shift_extra(TSParser *parser, int head, TSStateId state) {
@ -147,15 +147,15 @@ static TSTree *ts_parser__reduce(TSParser *parser, int head, TSSymbol symbol,
size_t child_count, bool extra,
bool count_extra) {
TSNodeType node_type = parser->language->node_types[symbol];
ParseStackPopResultList pop_results =
ts_parse_stack_pop(parser->stack, head, child_count, count_extra);
StackPopResultList pop_results =
ts_stack_pop(parser->stack, head, child_count, count_extra);
TSTree *parent = NULL;
TSTree **last_children = NULL;
int last_index = -1;
for (int i = 0; i < pop_results.size; i++) {
ParseStackPopResult pop_result = pop_results.contents[i];
StackPopResult pop_result = pop_results.contents[i];
if (pop_result.trees != last_children) {
parent = ts_tree_make_node(symbol, pop_result.tree_count,
@ -163,10 +163,9 @@ static TSTree *ts_parser__reduce(TSParser *parser, int head, TSSymbol symbol,
}
if (pop_result.index == last_index) {
ts_parse_stack_add_alternative(parser->stack, pop_result.index, parent);
ts_stack_add_alternative(parser->stack, pop_result.index, parent);
} else {
TSStateId top_state =
ts_parse_stack_top_state(parser->stack, pop_result.index);
TSStateId top_state = ts_stack_top_state(parser->stack, pop_result.index);
TSStateId state;
if (extra) {
@ -177,7 +176,7 @@ static TSTree *ts_parser__reduce(TSParser *parser, int head, TSSymbol symbol,
.data.to_state;
}
ts_parse_stack_push(parser->stack, pop_result.index, state, parent);
ts_stack_push(parser->stack, pop_result.index, state, parent);
}
last_index = pop_result.index;
@ -207,7 +206,7 @@ static void ts_parser__reduce_error(TSParser *parser, int head,
static bool ts_parser__handle_error(TSParser *parser, int head) {
size_t error_token_count = 1;
ParseStackEntry *entry_before_error = ts_parse_stack_head(parser->stack, head);
StackEntry *entry_before_error = ts_stack_head(parser->stack, head);
for (;;) {
@ -216,8 +215,8 @@ static bool ts_parser__handle_error(TSParser *parser, int head) {
* expected and the current lookahead token is expected afterwards.
*/
int i = -1;
for (ParseStackEntry *entry = entry_before_error; entry != NULL;
entry = ts_parse_stack_entry_next(entry, head), i++) {
for (StackEntry *entry = entry_before_error; entry != NULL;
entry = ts_stack_entry_next(entry, head), i++) {
TSStateId stack_state = entry->state;
TSParseAction action_on_error = ts_language__action(
parser->language, stack_state, ts_builtin_sym_error);
@ -241,8 +240,7 @@ static bool ts_parser__handle_error(TSParser *parser, int head) {
* current lookahead token, advance to the next token.
*/
DEBUG("skip token:%s", SYM_NAME(parser->lookahead->symbol));
ts_parser__shift(parser, head,
ts_parse_stack_top_state(parser->stack, head));
ts_parser__shift(parser, head, ts_stack_top_state(parser->stack, head));
ts_parser__get_next_lookahead(parser, true);
error_token_count++;
@ -261,7 +259,7 @@ static void ts_parser__start(TSParser *parser, TSInput input) {
parser->lexer.input = input;
ts_lexer_reset(&parser->lexer, ts_length_zero());
parser->previous_tree = ts_parse_stack_top_tree(parser->stack, 0);
parser->previous_tree = ts_stack_top_tree(parser->stack, 0);
if (parser->previous_tree) {
DEBUG("parse_after_edit");
ts_tree_retain(parser->previous_tree);
@ -272,19 +270,19 @@ static void ts_parser__start(TSParser *parser, TSInput input) {
parser->reusable_subtree_pos = 0;
parser->lookahead = NULL;
parser->is_verifying = false;
ts_parse_stack_clear(parser->stack);
ts_stack_clear(parser->stack);
}
static TSTree *ts_parser__finish(TSParser *parser) {
ParseStackPopResult pop_result =
ts_parse_stack_pop(parser->stack, 0, -1, true).contents[0];
StackPopResult pop_result =
ts_stack_pop(parser->stack, 0, -1, true).contents[0];
TSTree **trees = pop_result.trees;
size_t extra_count = pop_result.tree_count - 1;
TSTree *root = trees[extra_count];
ts_tree_prepend_children(root, extra_count, trees);
ts_parse_stack_push(parser->stack, 0, 0, root);
ts_stack_push(parser->stack, 0, 0, root);
return root;
}
@ -296,7 +294,7 @@ typedef enum {
} ParserNextResult;
static ParserNextResult ts_parser__next(TSParser *parser, int head_to_advance) {
TSStateId state = ts_parse_stack_top_state(parser->stack, head_to_advance);
TSStateId state = ts_stack_top_state(parser->stack, head_to_advance);
const TSParseAction *next_action =
ts_language__actions(parser->language, state, parser->lookahead->symbol);
int head, next_head = head_to_advance;
@ -311,7 +309,7 @@ static ParserNextResult ts_parser__next(TSParser *parser, int head_to_advance) {
if (next_action->type == 0) {
next_action = NULL;
} else {
next_head = ts_parse_stack_split(parser->stack, head);
next_head = ts_stack_split(parser->stack, head);
DEBUG("split head:%d, created:%d", head, next_head);
}
@ -325,14 +323,14 @@ static ParserNextResult ts_parser__next(TSParser *parser, int head_to_advance) {
switch (action.type) {
case TSParseActionTypeError:
DEBUG("error_sym");
if (ts_parse_stack_head_count(parser->stack) == 1) {
if (ts_stack_head_count(parser->stack) == 1) {
if (ts_parser__handle_error(parser, head))
break;
else
return ParserNextResultFinished;
} else {
DEBUG("bail head:%d", head);
ts_parse_stack_remove_head(parser->stack, head);
ts_stack_remove_head(parser->stack, head);
return ParserNextResultRemoved;
}
@ -387,7 +385,7 @@ static TSTree *ts_parser__select_tree(void *data, TSTree *left, TSTree *right) {
TSParser ts_parser_make() {
return (TSParser){
.lexer = ts_lexer_make(),
.stack = ts_parse_stack_new((TreeSelectionCallback){
.stack = ts_stack_new((TreeSelectionCallback){
NULL, ts_parser__select_tree,
}),
.lookahead = NULL,
@ -396,7 +394,7 @@ TSParser ts_parser_make() {
}
void ts_parser_destroy(TSParser *parser) {
ts_parse_stack_delete(parser->stack);
ts_stack_delete(parser->stack);
if (parser->lookahead)
ts_tree_release(parser->lookahead);
}
@ -416,10 +414,10 @@ TSTree *ts_parser_parse(TSParser *parser, TSInput input) {
ts_parser__get_next_lookahead(parser, false);
DEBUG("lookahead sym:%s", SYM_NAME(parser->lookahead->symbol));
DEBUG("head_count: %d", ts_parse_stack_head_count(parser->stack));
DEBUG("head_count: %d", ts_stack_head_count(parser->stack));
int head = 0;
while (head < ts_parse_stack_head_count(parser->stack)) {
while (head < ts_stack_head_count(parser->stack)) {
bool removed = false, advanced = false;
while (!(advanced || removed)) {

View file

@ -5,11 +5,11 @@
extern "C" {
#endif
#include "runtime/parse_stack.h"
#include "runtime/stack.h"
typedef struct {
TSLexer lexer;
ParseStack *stack;
Stack *stack;
TSTree *lookahead;
TSTree *previous_tree;
TSTree *reusable_subtree;

View file

@ -1,7 +1,7 @@
#include "tree_sitter/parser.h"
#include "runtime/tree.h"
#include "runtime/tree_vector.h"
#include "runtime/parse_stack.h"
#include "runtime/stack.h"
#include "runtime/length.h"
#include <assert.h>
@ -9,18 +9,18 @@
#define INITIAL_HEAD_CAPACITY 3
#define STARTING_TREE_CAPACITY 10
typedef struct ParseStackNode {
ParseStackEntry entry;
struct ParseStackNode *successors[MAX_POP_PATH_COUNT];
typedef struct StackNode {
StackEntry entry;
struct StackNode *successors[MAX_POP_PATH_COUNT];
short unsigned int successor_count;
short unsigned int ref_count;
} ParseStackNode;
} StackNode;
struct ParseStack {
ParseStackNode **heads;
struct Stack {
StackNode **heads;
int head_count;
int head_capacity;
ParseStackPopResult last_pop_results[MAX_POP_PATH_COUNT];
StackPopResult last_pop_results[MAX_POP_PATH_COUNT];
TreeSelectionCallback tree_selection_callback;
};
@ -28,10 +28,10 @@ struct ParseStack {
* Section: Stack lifecycle
*/
ParseStack *ts_parse_stack_new(TreeSelectionCallback tree_selection_callback) {
ParseStack *this = malloc(sizeof(ParseStack));
*this = (ParseStack){
.heads = calloc(INITIAL_HEAD_CAPACITY, sizeof(ParseStackNode *)),
Stack *ts_stack_new(TreeSelectionCallback tree_selection_callback) {
Stack *this = malloc(sizeof(Stack));
*this = (Stack){
.heads = calloc(INITIAL_HEAD_CAPACITY, sizeof(StackNode *)),
.head_count = 1,
.head_capacity = INITIAL_HEAD_CAPACITY,
.tree_selection_callback = tree_selection_callback,
@ -39,7 +39,7 @@ ParseStack *ts_parse_stack_new(TreeSelectionCallback tree_selection_callback) {
return this;
}
void ts_parse_stack_delete(ParseStack *this) {
void ts_stack_delete(Stack *this) {
free(this->heads);
free(this);
}
@ -48,46 +48,46 @@ void ts_parse_stack_delete(ParseStack *this) {
* Section: Reading from the stack
*/
TSStateId ts_parse_stack_top_state(const ParseStack *this, int head) {
ParseStackEntry *entry = ts_parse_stack_head((ParseStack *)this, head);
TSStateId ts_stack_top_state(const Stack *this, int head) {
StackEntry *entry = ts_stack_head((Stack *)this, head);
return entry ? entry->state : 0;
}
TSTree *ts_parse_stack_top_tree(const ParseStack *this, int head) {
ParseStackEntry *entry = ts_parse_stack_head((ParseStack *)this, head);
TSTree *ts_stack_top_tree(const Stack *this, int head) {
StackEntry *entry = ts_stack_head((Stack *)this, head);
return entry ? entry->tree : NULL;
}
ParseStackEntry *ts_parse_stack_head(ParseStack *this, int head) {
StackEntry *ts_stack_head(Stack *this, int head) {
assert(head < this->head_count);
ParseStackNode *node = this->heads[head];
StackNode *node = this->heads[head];
return node ? &node->entry : NULL;
}
int ts_parse_stack_head_count(const ParseStack *this) {
int ts_stack_head_count(const Stack *this) {
return this->head_count;
}
int ts_parse_stack_entry_next_count(const ParseStackEntry *entry) {
return ((const ParseStackNode *)entry)->successor_count;
int ts_stack_entry_next_count(const StackEntry *entry) {
return ((const StackNode *)entry)->successor_count;
}
ParseStackEntry *ts_parse_stack_entry_next(const ParseStackEntry *entry, int i) {
return &((const ParseStackNode *)entry)->successors[i]->entry;
StackEntry *ts_stack_entry_next(const StackEntry *entry, int i) {
return &((const StackNode *)entry)->successors[i]->entry;
}
/*
* Section: Manipulating nodes (Private)
*/
static void stack_node_retain(ParseStackNode *this) {
static void stack_node_retain(StackNode *this) {
if (!this)
return;
assert(this->ref_count != 0);
this->ref_count++;
}
static bool stack_node_release(ParseStackNode *this) {
static bool stack_node_release(StackNode *this) {
if (!this)
return false;
assert(this->ref_count != 0);
@ -103,12 +103,11 @@ static bool stack_node_release(ParseStackNode *this) {
}
}
static ParseStackNode *stack_node_new(ParseStackNode *next, TSStateId state,
TSTree *tree) {
ParseStackNode *this = malloc(sizeof(ParseStackNode));
static StackNode *stack_node_new(StackNode *next, TSStateId state, TSTree *tree) {
StackNode *this = malloc(sizeof(StackNode));
ts_tree_retain(tree);
stack_node_retain(next);
*this = (ParseStackNode){
*this = (StackNode){
.ref_count = 1,
.successor_count = 1,
.successors = { next, NULL, NULL },
@ -120,11 +119,10 @@ static ParseStackNode *stack_node_new(ParseStackNode *next, TSStateId state,
return this;
}
static void ts_parse_stack__add_node_successor(ParseStack *this,
ParseStackNode *node,
ParseStackNode *new_successor) {
static void ts_stack__add_node_successor(Stack *this, StackNode *node,
StackNode *new_successor) {
for (int i = 0; i < node->successor_count; i++) {
ParseStackNode *successor = node->successors[i];
StackNode *successor = node->successors[i];
if (successor == new_successor)
return;
if (successor->entry.state == new_successor->entry.state) {
@ -133,8 +131,8 @@ static void ts_parse_stack__add_node_successor(ParseStack *this,
this->tree_selection_callback.data, successor->entry.tree,
new_successor->entry.tree);
for (int j = 0; j < new_successor->successor_count; j++)
ts_parse_stack__add_node_successor(this, successor,
new_successor->successors[j]);
ts_stack__add_node_successor(this, successor,
new_successor->successors[j]);
return;
}
}
@ -148,11 +146,11 @@ static void ts_parse_stack__add_node_successor(ParseStack *this,
* Section: Mutating the stack (Private)
*/
static int ts_parse_stack__add_head(ParseStack *this, ParseStackNode *node) {
static int ts_stack__add_head(Stack *this, StackNode *node) {
if (this->head_count == this->head_capacity) {
this->head_capacity += 3;
this->heads =
realloc(this->heads, this->head_capacity * sizeof(ParseStackNode *));
realloc(this->heads, this->head_capacity * sizeof(StackNode *));
}
int new_index = this->head_count++;
this->heads[new_index] = node;
@ -160,16 +158,15 @@ static int ts_parse_stack__add_head(ParseStack *this, ParseStackNode *node) {
return new_index;
}
static int ts_parse_stack__find_or_add_head(ParseStack *this,
ParseStackNode *node) {
static int ts_stack__find_or_add_head(Stack *this, StackNode *node) {
for (int i = 0; i < this->head_count; i++)
if (this->heads[i] == node) {
return i;
}
return ts_parse_stack__add_head(this, node);
return ts_stack__add_head(this, node);
}
void ts_parse_stack_remove_head(ParseStack *this, int head_index) {
void ts_stack_remove_head(Stack *this, int head_index) {
stack_node_release(this->heads[head_index]);
for (int i = head_index; i < this->head_count - 1; i++) {
this->heads[head_index] = this->heads[head_index + 1];
@ -177,17 +174,17 @@ void ts_parse_stack_remove_head(ParseStack *this, int head_index) {
this->head_count--;
}
static bool ts_parse_stack__merge_head(ParseStack *this, int head_index,
TSStateId state, TSTree *tree) {
static bool ts_stack__merge_head(Stack *this, int head_index, TSStateId state,
TSTree *tree) {
for (int i = 0; i < head_index; i++) {
ParseStackNode *head = this->heads[i];
StackNode *head = this->heads[i];
if (head->entry.state == state) {
if (head->entry.tree != tree) {
head->entry.tree = this->tree_selection_callback.callback(
this->tree_selection_callback.data, head->entry.tree, tree);
}
ts_parse_stack__add_node_successor(this, head, this->heads[head_index]);
ts_parse_stack_remove_head(this, head_index);
ts_stack__add_node_successor(this, head, this->heads[head_index]);
ts_stack_remove_head(this, head_index);
return true;
}
}
@ -198,36 +195,34 @@ static bool ts_parse_stack__merge_head(ParseStack *this, int head_index,
* Section: Mutating the stack (Public)
*/
bool ts_parse_stack_push(ParseStack *this, int head_index, TSStateId state,
TSTree *tree) {
bool ts_stack_push(Stack *this, int head_index, TSStateId state, TSTree *tree) {
assert(head_index < this->head_count);
if (ts_parse_stack__merge_head(this, head_index, state, tree))
if (ts_stack__merge_head(this, head_index, state, tree))
return true;
this->heads[head_index] = stack_node_new(this->heads[head_index], state, tree);
return false;
}
void ts_parse_stack_add_alternative(ParseStack *this, int head_index,
TSTree *tree) {
void ts_stack_add_alternative(Stack *this, int head_index, TSTree *tree) {
assert(head_index < this->head_count);
ParseStackEntry *entry = &this->heads[head_index]->entry;
StackEntry *entry = &this->heads[head_index]->entry;
entry->tree = this->tree_selection_callback.callback(
this->tree_selection_callback.data, entry->tree, tree);
}
int ts_parse_stack_split(ParseStack *this, int head_index) {
int ts_stack_split(Stack *this, int head_index) {
assert(head_index < this->head_count);
return ts_parse_stack__add_head(this, this->heads[head_index]);
return ts_stack__add_head(this, this->heads[head_index]);
}
ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index,
int child_count, bool count_extra) {
ParseStackNode *previous_head = this->heads[head_index];
StackPopResultList ts_stack_pop(Stack *this, int head_index, int child_count,
bool count_extra) {
StackNode *previous_head = this->heads[head_index];
int path_count = 1;
int capacity = (child_count == -1) ? STARTING_TREE_CAPACITY : child_count;
size_t tree_counts_by_path[MAX_POP_PATH_COUNT] = { child_count };
ParseStackNode *nodes_by_path[MAX_POP_PATH_COUNT] = { previous_head };
StackNode *nodes_by_path[MAX_POP_PATH_COUNT] = { previous_head };
TreeVector trees_by_path[MAX_POP_PATH_COUNT] = { tree_vector_new(capacity) };
bool is_shared_by_path[MAX_POP_PATH_COUNT] = { false };
@ -240,7 +235,7 @@ ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index,
all_paths_done = true;
int current_path_count = path_count;
for (int path = 0; path < current_path_count; path++) {
ParseStackNode *node = nodes_by_path[path];
StackNode *node = nodes_by_path[path];
if (!node || (trees_by_path[path].size == tree_counts_by_path[path]))
continue;
all_paths_done = false;
@ -288,10 +283,10 @@ ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index,
this->heads[head_index] = nodes_by_path[path];
index = head_index;
} else {
index = ts_parse_stack__find_or_add_head(this, nodes_by_path[path]);
index = ts_stack__find_or_add_head(this, nodes_by_path[path]);
}
this->last_pop_results[path] = (ParseStackPopResult){
this->last_pop_results[path] = (StackPopResult){
.index = index,
.tree_count = trees_by_path[path].size,
.trees = trees_by_path[path].contents,
@ -299,14 +294,14 @@ ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index,
}
stack_node_release(previous_head);
return (ParseStackPopResultList){
return (StackPopResultList){
.size = path_count, .contents = this->last_pop_results,
};
}
void ts_parse_stack_shrink(ParseStack *this, int head_index, int count) {
ParseStackNode *head = this->heads[head_index];
ParseStackNode *new_head = head;
void ts_stack_shrink(Stack *this, int head_index, int count) {
StackNode *head = this->heads[head_index];
StackNode *new_head = head;
for (int i = 0; i < count; i++) {
if (new_head->successor_count == 0)
break;
@ -317,7 +312,7 @@ void ts_parse_stack_shrink(ParseStack *this, int head_index, int count) {
this->heads[head_index] = new_head;
}
void ts_parse_stack_clear(ParseStack *this) {
void ts_stack_clear(Stack *this) {
for (int i = 0; i < this->head_count; i++)
stack_node_release(this->heads[i]);
this->head_count = 1;

View file

@ -7,23 +7,23 @@ extern "C" {
#include "tree_sitter/parser.h"
typedef struct ParseStack ParseStack;
typedef struct Stack Stack;
typedef struct {
TSTree *tree;
TSStateId state;
} ParseStackEntry;
} StackEntry;
typedef struct {
int index;
int tree_count;
TSTree **trees;
} ParseStackPopResult;
} StackPopResult;
typedef struct {
int size;
ParseStackPopResult *contents;
} ParseStackPopResultList;
StackPopResult *contents;
} StackPopResultList;
typedef struct {
void *data;
@ -33,56 +33,56 @@ typedef struct {
/*
* Create a parse stack.
*/
ParseStack *ts_parse_stack_new(TreeSelectionCallback);
Stack *ts_stack_new(TreeSelectionCallback);
/*
* Release any resources reserved by a parse stack.
*/
void ts_parse_stack_delete(ParseStack *);
void ts_stack_delete(Stack *);
/*
* Get the stack's current number of heads.
*/
int ts_parse_stack_head_count(const ParseStack *);
int ts_stack_head_count(const Stack *);
/*
* Get the state at given head of the stack. If the stack is empty, this
* returns the initial state (0).
*/
TSStateId ts_parse_stack_top_state(const ParseStack *, int head);
TSStateId ts_stack_top_state(const Stack *, int head);
/*
* Get the tree at given head of the stack. If the stack is empty, this
* returns NULL.
*/
TSTree *ts_parse_stack_top_tree(const ParseStack *, int head);
TSTree *ts_stack_top_tree(const Stack *, int head);
/*
* Get the entry at the given head of the stack.
*/
ParseStackEntry *ts_parse_stack_head(ParseStack *, int head);
StackEntry *ts_stack_head(Stack *, int head);
/*
* Get the number of successors for the parse stack entry.
*/
int ts_parse_stack_entry_next_count(const ParseStackEntry *);
int ts_stack_entry_next_count(const StackEntry *);
/*
* Get the given successor for the parse stack entry.
*/
ParseStackEntry *ts_parse_stack_entry_next(const ParseStackEntry *, int);
StackEntry *ts_stack_entry_next(const StackEntry *, int);
/*
* Push a (tree, state) pair onto the given head of the stack. Returns
* a boolean indicating whether the stack head was merged with an
* existing head.
*/
bool ts_parse_stack_push(ParseStack *, int head, TSStateId, TSTree *);
bool ts_stack_push(Stack *, int head, TSStateId, TSTree *);
/*
* Add an alternative tree for the given head of the stack.
*/
void ts_parse_stack_add_alternative(ParseStack *, int head, TSTree *);
void ts_stack_add_alternative(Stack *, int head, TSTree *);
/*
* Pop the given number of entries from the given head of the stack. This
@ -90,30 +90,29 @@ void ts_parse_stack_add_alternative(ParseStack *, int head, TSTree *);
* which had previously been merged. It returns a struct that indicates the
* index of each revealed head and the trees removed from that head.
*/
ParseStackPopResultList ts_parse_stack_pop(ParseStack *, int head, int count,
bool count_extra);
StackPopResultList ts_stack_pop(Stack *, int head, int count, bool count_extra);
/*
* Remove the given number of entries from the given head of the stack.
*/
void ts_parse_stack_shrink(ParseStack *, int head, int count);
void ts_stack_shrink(Stack *, int head, int count);
/*
* Split the given stack head into two heads, so that the stack can be
* transformed from its current state in multiple alternative ways. Returns
* the index of the newly-created head.
*/
int ts_parse_stack_split(ParseStack *, int head);
int ts_stack_split(Stack *, int head);
/*
* Remove the given head from the stack.
*/
void ts_parse_stack_remove_head(ParseStack *, int head);
void ts_stack_remove_head(Stack *, int head);
/*
* Remove all entries from the stack.
*/
void ts_parse_stack_clear(ParseStack *);
void ts_stack_clear(Stack *);
#ifdef __cplusplus
}