tree-sitter/src/runtime/stack.h

118 lines
2.7 KiB
C
Raw Normal View History

2015-05-25 20:21:13 -07:00
#ifndef RUNTIME_PARSE_STACK_H_
#define RUNTIME_PARSE_STACK_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "tree_sitter/parser.h"
2015-11-20 00:01:53 -08:00
#include "runtime/vector.h"
2015-05-25 20:21:13 -07:00
2015-09-18 18:04:52 -07:00
typedef struct Stack Stack;
2015-05-25 20:21:13 -07:00
2015-06-03 09:44:13 -07:00
typedef struct {
2015-05-25 20:21:13 -07:00
TSTree *tree;
TSStateId state;
2015-09-18 18:04:52 -07:00
} StackEntry;
2015-05-25 20:21:13 -07:00
2015-06-03 09:44:13 -07:00
typedef struct {
TSTree **trees;
2015-11-20 00:01:53 -08:00
size_t tree_count;
int head_index;
2015-09-18 18:04:52 -07:00
} StackPopResult;
2015-06-03 09:44:13 -07:00
typedef struct {
void *data;
2015-07-27 18:29:48 -07:00
TSTree *(*callback)(void *data, TSTree *, TSTree *);
} TreeSelectionCallback;
2015-06-03 09:44:13 -07:00
/*
2015-08-16 19:53:34 -07:00
* Create a parse stack.
2015-06-03 09:44:13 -07:00
*/
2015-09-18 18:04:52 -07:00
Stack *ts_stack_new(TreeSelectionCallback);
2015-06-03 09:44:13 -07:00
/*
* Release any resources reserved by a parse stack.
*/
2015-09-18 18:04:52 -07:00
void ts_stack_delete(Stack *);
2015-05-25 20:21:13 -07:00
2015-06-03 09:44:13 -07:00
/*
* Get the stack's current number of heads.
*/
2015-09-18 18:04:52 -07:00
int ts_stack_head_count(const Stack *);
2015-05-25 20:21:13 -07:00
2015-06-03 09:44:13 -07:00
/*
* Get the state at given head of the stack. If the stack is empty, this
* returns the initial state (0).
2015-06-03 09:44:13 -07:00
*/
2015-09-18 18:04:52 -07:00
TSStateId ts_stack_top_state(const Stack *, int head);
2015-06-03 09:44:13 -07:00
/*
* Get the tree at given head of the stack. If the stack is empty, this
* returns NULL.
*/
2015-09-18 18:04:52 -07:00
TSTree *ts_stack_top_tree(const Stack *, int head);
/*
* Get the entry at the given head of the stack.
*/
2015-09-18 18:04:52 -07:00
StackEntry *ts_stack_head(Stack *, int head);
/*
* Get the number of successors for the parse stack entry.
2015-06-03 09:44:13 -07:00
*/
2015-09-18 18:04:52 -07:00
int ts_stack_entry_next_count(const StackEntry *);
2015-06-03 09:44:13 -07:00
/*
* Get the given successor for the parse stack entry.
2015-06-03 09:44:13 -07:00
*/
2015-09-18 18:04:52 -07:00
StackEntry *ts_stack_entry_next(const StackEntry *, int);
2015-06-03 09:44:13 -07:00
/*
* 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.
*/
2015-09-18 18:04:52 -07:00
bool ts_stack_push(Stack *, int head, TSStateId, TSTree *);
2015-06-03 09:44:13 -07:00
/*
* Add an alternative tree for the given head of the stack.
*/
2015-09-18 18:04:52 -07:00
void ts_stack_add_alternative(Stack *, int head, TSTree *);
2015-06-03 09:44:13 -07:00
/*
* Pop the given number of entries from the given head of the stack. This
* operation can increase the number of stack heads by revealing multiple heads
* which had previously been merged. It returns a struct that indicates the
* index of each revealed head and the trees removed from that head.
*/
2015-11-20 00:01:53 -08:00
Vector ts_stack_pop(Stack *, int head, int count, bool count_extra);
/*
* Remove the given number of entries from the given head of the stack.
*/
2015-09-18 18:04:52 -07:00
void ts_stack_shrink(Stack *, int head, int count);
2015-06-03 09:44:13 -07:00
/*
* 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.
*/
2015-09-18 18:04:52 -07:00
int ts_stack_split(Stack *, int head);
2015-05-25 20:21:13 -07:00
/*
* Remove the given head from the stack.
*/
2015-09-18 18:04:52 -07:00
void ts_stack_remove_head(Stack *, int head);
/*
* Remove all entries from the stack.
*/
2015-09-18 18:04:52 -07:00
void ts_stack_clear(Stack *);
2015-05-25 20:21:13 -07:00
#ifdef __cplusplus
}
#endif
#endif // RUNTIME_PARSE_STACK_H_