tree-sitter/src/runtime/stack.h

129 lines
3.2 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"
2016-02-17 20:41:29 -08:00
#include "runtime/array.h"
#include "runtime/tree.h"
#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 {
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
typedef Array(StackSlice) StackSliceArray;
typedef enum {
StackPushFailed,
StackPushMerged,
StackPushContinued,
} StackPushResult;
typedef struct {
enum {
StackPopFailed,
StackPopStoppedAtError,
StackPopSucceeded,
} status;
StackSliceArray slices;
} StackPopResult;
typedef enum {
StackIterateContinue,
StackIterateAbort,
StackIteratePop,
} StackIterateAction;
typedef StackIterateAction (*StackIterateCallback)(void *, TSStateId state,
size_t tree_count,
bool is_done,
bool is_pending);
typedef int (*TreeSelectionFunction)(void *, TSTree *tree1, TSTree *tree2);
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
*/
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
/*
* 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
/*
* 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-06-03 09:44:13 -07: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
*/
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.
*/
StackPopResult ts_stack_pop_count(Stack *, int head_index, int count);
StackPopResult ts_stack_pop_until(Stack *, int head_index, StackIterateCallback,
void *);
StackPopResult ts_stack_pop_pending(Stack *, int head_index);
/*
* 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
/*
* Remove the given head from the stack.
*/
2016-02-25 21:46:13 -08:00
void ts_stack_remove_head(Stack *, int head_index);
/*
* Remove all entries from the stack.
*/
2015-09-18 18:04:52 -07:00
void ts_stack_clear(Stack *);
void ts_stack_set_tree_selection_callback(Stack *, void *,
TreeSelectionFunction);
int ts_stack_print_dot_graph(Stack *, const char **, FILE *);
2015-05-25 20:21:13 -07:00
#ifdef __cplusplus
}
#endif
#endif // RUNTIME_PARSE_STACK_H_