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"
|
2016-04-02 22:18:44 -07:00
|
|
|
#include <stdio.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 {
|
2016-02-21 22:31:04 -08:00
|
|
|
TreeArray trees;
|
2015-11-20 00:01:53 -08:00
|
|
|
int head_index;
|
2016-03-03 10:16:10 -08:00
|
|
|
} StackSlice;
|
2015-06-03 09:44:13 -07:00
|
|
|
|
2016-03-03 10:20:05 -08:00
|
|
|
typedef Array(StackSlice) StackSliceArray;
|
|
|
|
|
|
2016-01-19 18:07:24 -08:00
|
|
|
typedef enum {
|
2016-03-03 10:20:05 -08:00
|
|
|
StackPushFailed,
|
|
|
|
|
StackPushMerged,
|
|
|
|
|
StackPushContinued,
|
2016-01-19 18:07:24 -08:00
|
|
|
} StackPushResult;
|
|
|
|
|
|
2016-03-03 11:05:37 -08:00
|
|
|
typedef struct {
|
|
|
|
|
enum {
|
|
|
|
|
StackPopFailed,
|
|
|
|
|
StackPopStoppedAtError,
|
|
|
|
|
StackPopSucceeded,
|
|
|
|
|
} status;
|
|
|
|
|
StackSliceArray slices;
|
|
|
|
|
} StackPopResult;
|
|
|
|
|
|
2016-03-07 16:03:23 -08:00
|
|
|
typedef enum {
|
|
|
|
|
StackIterateContinue,
|
|
|
|
|
StackIterateAbort,
|
|
|
|
|
StackIteratePop,
|
|
|
|
|
} StackIterateAction;
|
|
|
|
|
|
|
|
|
|
typedef StackIterateAction (*StackIterateCallback)(void *, TSStateId state,
|
2016-03-10 11:51:38 -08:00
|
|
|
size_t tree_count,
|
2016-03-31 12:03:07 -07:00
|
|
|
bool is_done,
|
|
|
|
|
bool is_pending);
|
2016-03-07 16:03:23 -08:00
|
|
|
|
|
|
|
|
typedef int (*TreeSelectionFunction)(void *, TSTree *tree1, TSTree *tree2);
|
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-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-03-31 12:03:07 -07:00
|
|
|
StackPushResult ts_stack_push(Stack *, int head_index, TSTree *, bool,
|
|
|
|
|
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-03-07 16:03:23 -08:00
|
|
|
StackPopResult ts_stack_pop_count(Stack *, int head_index, int count);
|
|
|
|
|
|
|
|
|
|
StackPopResult ts_stack_pop_until(Stack *, int head_index, StackIterateCallback,
|
|
|
|
|
void *);
|
2015-06-18 15:04:03 -07:00
|
|
|
|
2016-03-31 12:03:07 -07:00
|
|
|
StackPopResult ts_stack_pop_pending(Stack *, int head_index);
|
|
|
|
|
|
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-04-02 22:18:44 -07:00
|
|
|
int ts_stack_print_dot_graph(Stack *, const char **, FILE *);
|
2016-02-23 00:08:55 -08:00
|
|
|
|
2015-05-25 20:21:13 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif // RUNTIME_PARSE_STACK_H_
|