Pass reference to parser in stack's tree selection callback
This commit is contained in:
parent
4abb415113
commit
10286f307f
4 changed files with 21 additions and 8 deletions
|
|
@ -43,7 +43,9 @@ describe("Stack", [&]() {
|
|||
TSSymbolMetadata metadata = {true, true, true};
|
||||
|
||||
before_each([&]() {
|
||||
stack = ts_stack_new({
|
||||
stack = ts_stack_new();
|
||||
|
||||
ts_stack_set_tree_selection_callback(stack, {
|
||||
&tree_selection_spy,
|
||||
tree_selection_spy_callback
|
||||
});
|
||||
|
|
|
|||
|
|
@ -433,6 +433,9 @@ 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,
|
||||
});
|
||||
|
||||
LookaheadState lookahead_state = {
|
||||
.reusable_subtree = previous_tree,
|
||||
|
|
@ -582,9 +585,7 @@ static ConsumeResult ts_parser__consume_lookahead(TSParser *self, int head,
|
|||
TSParser ts_parser_make() {
|
||||
return (TSParser){
|
||||
.lexer = ts_lexer_make(),
|
||||
.stack = ts_stack_new((TreeSelectionCallback){
|
||||
NULL, ts_parser__select_tree,
|
||||
}),
|
||||
.stack = ts_stack_new(),
|
||||
.lookahead_states = vector_new(sizeof(LookaheadState), 4),
|
||||
.reduce_parents = vector_new(sizeof(TSTree *), 4),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -36,13 +36,17 @@ typedef struct {
|
|||
* Section: Stack lifecycle
|
||||
*/
|
||||
|
||||
Stack *ts_stack_new(TreeSelectionCallback tree_selection_callback) {
|
||||
static TSTree *ts_stack__default_tree_selection(void *p, TSTree *t1, TSTree *t2) {
|
||||
return t1;
|
||||
}
|
||||
|
||||
Stack *ts_stack_new() {
|
||||
Stack *self = malloc(sizeof(Stack));
|
||||
*self = (Stack){
|
||||
.heads = calloc(INITIAL_HEAD_CAPACITY, sizeof(StackNode *)),
|
||||
.head_count = 1,
|
||||
.head_capacity = INITIAL_HEAD_CAPACITY,
|
||||
.tree_selection_callback = tree_selection_callback,
|
||||
.tree_selection_callback = {NULL, ts_stack__default_tree_selection},
|
||||
.pop_results = vector_new(sizeof(StackPopResult), 4),
|
||||
.pop_paths = vector_new(sizeof(PopPath), 4),
|
||||
};
|
||||
|
|
@ -351,3 +355,7 @@ void ts_stack_clear(Stack *self) {
|
|||
self->head_count = 1;
|
||||
self->heads[0] = NULL;
|
||||
}
|
||||
|
||||
void ts_stack_set_tree_selection_callback(Stack *self, TreeSelectionCallback callback) {
|
||||
self->tree_selection_callback = callback;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,13 +24,13 @@ typedef struct {
|
|||
|
||||
typedef struct {
|
||||
void *data;
|
||||
TSTree *(*callback)(void *data, TSTree *, TSTree *);
|
||||
TSTree *(*callback)(void *, TSTree *, TSTree *);
|
||||
} TreeSelectionCallback;
|
||||
|
||||
/*
|
||||
* Create a parse stack.
|
||||
*/
|
||||
Stack *ts_stack_new(TreeSelectionCallback);
|
||||
Stack *ts_stack_new();
|
||||
|
||||
/*
|
||||
* Release any resources reserved by a parse stack.
|
||||
|
|
@ -117,6 +117,8 @@ void ts_stack_remove_head(Stack *, int head);
|
|||
*/
|
||||
void ts_stack_clear(Stack *);
|
||||
|
||||
void ts_stack_set_tree_selection_callback(Stack *, TreeSelectionCallback);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue