tree-sitter/src/runtime/stack.h

148 lines
3.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
2016-02-17 20:41:29 -08:00
#include "runtime/array.h"
#include "runtime/tree.h"
#include "runtime/error_costs.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
typedef unsigned StackVersion;
#define STACK_VERSION_NONE ((StackVersion)-1)
2016-04-04 12:25:57 -07:00
2015-06-03 09:44:13 -07:00
typedef struct {
TreeArray trees;
2016-04-04 12:25:57 -07:00
StackVersion version;
2016-03-03 10:16:10 -08:00
} StackSlice;
2015-06-03 09:44:13 -07:00
typedef Array(StackSlice) StackSliceArray;
typedef struct {
StackSliceArray slices;
} StackPopResult;
typedef unsigned StackIterateAction;
enum {
StackIterateNone,
StackIterateStop = 1,
StackIteratePop = 2,
};
typedef struct {
Length position;
unsigned depth;
TSStateId state;
} StackSummaryEntry;
typedef Array(StackSummaryEntry) StackSummary;
typedef StackIterateAction (*StackIterateCallback)(void *, TSStateId state,
const TreeArray *trees,
uint32_t tree_count);
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(TreePool *);
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
/*
2016-04-04 12:25:57 -07:00
* Get the stack's current number of versions.
2015-06-03 09:44:13 -07:00
*/
uint32_t ts_stack_version_count(const Stack *);
2015-05-25 20:21:13 -07:00
2015-06-03 09:44:13 -07:00
/*
2016-04-04 12:25:57 -07:00
* Get the state at the top of the given version of the stack. If the stack is
* empty, this returns the initial state (0).
2015-06-03 09:44:13 -07:00
*/
2016-04-04 12:25:57 -07:00
TSStateId ts_stack_top_state(const Stack *, StackVersion);
2015-06-03 09:44:13 -07:00
unsigned ts_stack_push_count(const Stack *, StackVersion);
void ts_stack_decrease_push_count(Stack *, StackVersion, unsigned);
Tree *ts_stack_last_external_token(const Stack *, StackVersion);
void ts_stack_set_last_external_token(Stack *, StackVersion, Tree *);
/*
2016-04-04 12:25:57 -07:00
* Get the position at the top of the given version of the stack. If the stack
* is empty, this returns zero.
*/
Length ts_stack_top_position(const Stack *, StackVersion);
2015-06-03 09:44:13 -07:00
/*
2017-06-29 10:43:20 -07:00
* Push a tree and state onto the given head of the stack.
2015-06-03 09:44:13 -07:00
*/
2017-06-29 10:43:20 -07:00
void ts_stack_push(Stack *, StackVersion, Tree *, bool, TSStateId);
2015-06-03 09:44:13 -07:00
/*
2016-04-04 12:25:57 -07:00
* Pop the given number of entries from the given version of the stack. This
* operation can increase the number of stack versions by revealing multiple
* versions which had previously been merged. It returns a struct that
* indicates the index of each revealed version and the trees removed from that
* version.
2015-06-03 09:44:13 -07:00
*/
StackPopResult ts_stack_pop_count(Stack *, StackVersion, uint32_t count);
StackPopResult ts_stack_iterate(Stack *, StackVersion, StackIterateCallback, void *);
StackPopResult ts_stack_pop_error(Stack *, StackVersion);
2016-04-04 12:25:57 -07:00
StackPopResult ts_stack_pop_pending(Stack *, StackVersion);
StackPopResult ts_stack_pop_all(Stack *, StackVersion);
unsigned ts_stack_depth_since_error(Stack *, StackVersion);
int ts_stack_dynamic_precedence(Stack *, StackVersion);
void ts_stack_record_summary(Stack *, StackVersion, unsigned max_depth);
StackSummary *ts_stack_get_summary(Stack *, StackVersion);
unsigned ts_stack_error_cost(const Stack *, StackVersion version);
bool ts_stack_merge(Stack *, StackVersion, StackVersion);
bool ts_stack_can_merge(Stack *, StackVersion, StackVersion);
void ts_stack_force_merge(Stack *, StackVersion, StackVersion);
void ts_stack_halt(Stack *, StackVersion);
bool ts_stack_is_halted(Stack *, StackVersion);
void ts_stack_renumber_version(Stack *, StackVersion, StackVersion);
2015-05-25 20:21:13 -07:00
void ts_stack_swap_versions(Stack *, StackVersion, StackVersion);
2016-11-14 17:25:55 -08:00
StackVersion ts_stack_copy_version(Stack *, StackVersion);
/*
2016-04-04 12:25:57 -07:00
* Remove the given version from the stack.
*/
2016-04-04 12:25:57 -07:00
void ts_stack_remove_version(Stack *, StackVersion);
/*
* Remove all entries from the stack.
*/
2015-09-18 18:04:52 -07:00
void ts_stack_clear(Stack *);
bool 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_