Remove TreeSelectionCallback struct

Just make a typedef for the function type
This commit is contained in:
Max Brunsfeld 2015-12-17 12:08:06 -08:00
parent e6f933a21f
commit 7fbb628c78
4 changed files with 23 additions and 22 deletions

View file

@ -42,13 +42,13 @@ describe("Stack", [&]() {
TSLength tree_len = {2, 3, 0, 3};
TSSymbolMetadata metadata = {true, true, true, true};
before_each([&]() {
before_each([&]() {
stack = ts_stack_new();
ts_stack_set_tree_selection_callback(stack, {
ts_stack_set_tree_selection_callback(stack,
&tree_selection_spy,
tree_selection_spy_callback
});
);
for (size_t i = 0; i < tree_count; i++)
trees[i] = ts_tree_make_leaf(i, ts_length_zero(), tree_len, {});

View file

@ -455,9 +455,8 @@ static void ts_parser__start(TSParser *self, TSInput input,
ts_lexer_set_input(&self->lexer, input);
ts_stack_clear(self->stack);
ts_stack_set_tree_selection_callback(self->stack, (TreeSelectionCallback){
self, ts_parser__select_tree,
});
ts_stack_set_tree_selection_callback(self->stack, self,
ts_parser__select_tree);
LookaheadState lookahead_state = {
.reusable_subtree = previous_tree,
@ -504,7 +503,7 @@ static TSTree *ts_parser__finish(TSParser *self, int finished_stack_head) {
* lookahead symbol is consumed.
*/
static bool ts_parser__consume_lookahead(TSParser *self, int head,
TSTree *lookahead) {
TSTree *lookahead) {
for (;;) {
TSStateId state = ts_stack_top_state(self->stack, head);
const TSParseAction *next_action =

View file

@ -22,7 +22,8 @@ struct Stack {
int head_capacity;
Vector pop_results;
Vector pop_paths;
TreeSelectionCallback tree_selection_callback;
void *tree_selection_payload;
TreeSelectionFunction tree_selection_function;
};
typedef struct {
@ -46,7 +47,8 @@ Stack *ts_stack_new() {
.heads = calloc(INITIAL_HEAD_CAPACITY, sizeof(StackNode *)),
.head_count = 1,
.head_capacity = INITIAL_HEAD_CAPACITY,
.tree_selection_callback = {NULL, ts_stack__default_tree_selection},
.tree_selection_payload = NULL,
.tree_selection_function = ts_stack__default_tree_selection,
.pop_results = vector_new(sizeof(StackPopResult), 4),
.pop_paths = vector_new(sizeof(PopPath), 4),
};
@ -152,8 +154,8 @@ static void ts_stack__add_node_successor(Stack *self, StackNode *node,
if (successor->entry.state == new_successor->entry.state) {
if (successor->entry.tree != new_successor->entry.tree) {
successor->entry.tree = self->tree_selection_callback.callback(
self->tree_selection_callback.data, successor->entry.tree,
successor->entry.tree = self->tree_selection_function(
self->tree_selection_payload, successor->entry.tree,
new_successor->entry.tree);
ts_tree_retain(successor->entry.tree);
}
@ -207,8 +209,8 @@ static bool ts_stack__merge_head(Stack *self, int head_index, TSStateId state,
if (head->entry.state == state &&
ts_length_eq(head->entry.position, position)) {
if (head->entry.tree != tree) {
head->entry.tree = self->tree_selection_callback.callback(
self->tree_selection_callback.data, head->entry.tree, tree);
head->entry.tree = self->tree_selection_function(
self->tree_selection_payload, head->entry.tree, tree);
ts_tree_retain(head->entry.tree);
}
ts_stack__add_node_successor(self, head, self->heads[head_index]);
@ -237,8 +239,8 @@ bool ts_stack_push(Stack *self, int head_index, TSStateId state, TSTree *tree) {
void ts_stack_add_alternative(Stack *self, int head_index, TSTree *tree) {
assert(head_index < self->head_count);
StackEntry *entry = &self->heads[head_index]->entry;
entry->tree = self->tree_selection_callback.callback(
self->tree_selection_callback.data, entry->tree, tree);
entry->tree = self->tree_selection_function(self->tree_selection_payload,
entry->tree, tree);
}
int ts_stack_split(Stack *self, int head_index) {
@ -356,6 +358,8 @@ void ts_stack_clear(Stack *self) {
self->heads[0] = NULL;
}
void ts_stack_set_tree_selection_callback(Stack *self, TreeSelectionCallback callback) {
self->tree_selection_callback = callback;
void ts_stack_set_tree_selection_callback(Stack *self, void *payload,
TreeSelectionFunction function) {
self->tree_selection_payload = payload;
self->tree_selection_function = function;
}

View file

@ -22,10 +22,7 @@ typedef struct {
int head_index;
} StackPopResult;
typedef struct {
void *data;
TSTree *(*callback)(void *, TSTree *, TSTree *);
} TreeSelectionCallback;
typedef TSTree *(*TreeSelectionFunction)(void *, TSTree *, TSTree *);
/*
* Create a parse stack.
@ -117,7 +114,8 @@ void ts_stack_remove_head(Stack *, int head);
*/
void ts_stack_clear(Stack *);
void ts_stack_set_tree_selection_callback(Stack *, TreeSelectionCallback);
void ts_stack_set_tree_selection_callback(Stack *, void *,
TreeSelectionFunction);
#ifdef __cplusplus
}