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"
|
2016-02-17 20:41:29 -08:00
|
|
|
#include "runtime/array.h"
|
2016-02-22 09:23:25 -08:00
|
|
|
#include "runtime/tree.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
|
|
|
TSStateId state;
|
2015-12-02 07:53:15 -08:00
|
|
|
TSLength position;
|
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 {
|
2016-02-21 22:31:04 -08:00
|
|
|
TreeArray trees;
|
2015-11-20 00:01:53 -08:00
|
|
|
int head_index;
|
2015-09-18 18:04:52 -07:00
|
|
|
} StackPopResult;
|
2015-06-03 09:44:13 -07:00
|
|
|
|
2016-01-19 18:07:24 -08:00
|
|
|
typedef enum {
|
|
|
|
|
StackPushResultFailed,
|
|
|
|
|
StackPushResultMerged,
|
|
|
|
|
StackPushResultContinued,
|
|
|
|
|
} StackPushResult;
|
|
|
|
|
|
2016-02-17 20:41:29 -08:00
|
|
|
typedef Array(StackPopResult) StackPopResultArray;
|
2016-02-17 14:45:00 -08:00
|
|
|
|
2016-02-08 12:08:15 -08:00
|
|
|
typedef int (*TreeSelectionFunction)(void *, TSTree *, TSTree *);
|
2015-07-15 09:21:53 -07:00
|
|
|
|
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-12-08 12:20:50 -08:00
|
|
|
Stack *ts_stack_new();
|
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
|
|
|
/*
|
2015-06-18 15:04:03 -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
|
|
|
*/
|
2016-02-25 21:46:13 -08:00
|
|
|
TSStateId ts_stack_top_state(const Stack *, int head_index);
|
2015-06-03 09:44:13 -07:00
|
|
|
|
2015-12-02 07:53:15 -08:00
|
|
|
/*
|
|
|
|
|
* Get the position of the given head of the stack. If the stack is empty, this
|
|
|
|
|
* returns {0, 0}.
|
|
|
|
|
*/
|
2016-02-25 21:46:13 -08:00
|
|
|
TSLength ts_stack_top_position(const Stack *, int head_index);
|
2015-12-02 07:53:15 -08:00
|
|
|
|
2015-06-18 15:04:03 -07:00
|
|
|
/*
|
|
|
|
|
* Get the entry at the given head of the stack.
|
|
|
|
|
*/
|
2016-02-25 21:46:13 -08:00
|
|
|
StackEntry *ts_stack_head(Stack *, int head_index);
|
2015-06-18 15:04:03 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 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
|
|
|
|
|
|
|
|
/*
|
2015-06-18 15:04:03 -07:00
|
|
|
* Get the given successor for the parse stack entry.
|
2015-06-03 09:44:13 -07:00
|
|
|
*/
|
2016-02-25 21:46:13 -08:00
|
|
|
StackEntry *ts_stack_entry_next(const StackEntry *, int head_index);
|
2015-06-03 09:44:13 -07:00
|
|
|
|
|
|
|
|
/*
|
2016-01-19 18:07:24 -08:00
|
|
|
* Push a (tree, state) pair onto the given head of the stack. This could cause
|
|
|
|
|
* the head to merge with an existing head.
|
2015-06-03 09:44:13 -07:00
|
|
|
*/
|
2016-02-25 21:46:13 -08:00
|
|
|
StackPushResult ts_stack_push(Stack *, int head_index, TSTree *, TSStateId);
|
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.
|
|
|
|
|
*/
|
2016-02-25 21:46:13 -08:00
|
|
|
StackPopResultArray ts_stack_pop(Stack *, int head_index, int count,
|
|
|
|
|
bool count_extra);
|
2015-06-18 15:04:03 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Remove the given number of entries from the given head of the stack.
|
|
|
|
|
*/
|
2016-02-25 21:46:13 -08:00
|
|
|
void ts_stack_shrink(Stack *, int head_index, 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.
|
|
|
|
|
*/
|
2016-02-25 21:46:13 -08:00
|
|
|
int ts_stack_split(Stack *, int head_index);
|
2015-05-25 20:21:13 -07:00
|
|
|
|
2015-07-08 17:34:21 -07:00
|
|
|
/*
|
|
|
|
|
* Remove the given head from the stack.
|
|
|
|
|
*/
|
2016-02-25 21:46:13 -08:00
|
|
|
void ts_stack_remove_head(Stack *, int head_index);
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2015-06-18 15:04:03 -07:00
|
|
|
/*
|
|
|
|
|
* Remove all entries from the stack.
|
|
|
|
|
*/
|
2015-09-18 18:04:52 -07:00
|
|
|
void ts_stack_clear(Stack *);
|
2015-06-18 15:04:03 -07:00
|
|
|
|
2015-12-17 12:08:06 -08:00
|
|
|
void ts_stack_set_tree_selection_callback(Stack *, void *,
|
|
|
|
|
TreeSelectionFunction);
|
2015-12-08 12:20:50 -08:00
|
|
|
|
2016-02-23 00:08:55 -08:00
|
|
|
char *ts_stack_dot_graph(Stack *, const char **);
|
|
|
|
|
|
2015-05-25 20:21:13 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif // RUNTIME_PARSE_STACK_H_
|