1#ifndef TREE_SITTER_PARSE_STACK_H_
2#define TREE_SITTER_PARSE_STACK_H_
3
4#ifdef __cplusplus
5extern "C" {
6#endif
7
8#include "./array.h"
9#include "./subtree.h"
10#include "./error_costs.h"
11#include <stdio.h>
12
13typedef struct Stack Stack;
14
15typedef unsigned StackVersion;
16#define STACK_VERSION_NONE ((StackVersion)-1)
17
18typedef struct {
19 SubtreeArray subtrees;
20 StackVersion version;
21} StackSlice;
22typedef Array(StackSlice) StackSliceArray;
23
24typedef struct {
25 Length position;
26 unsigned depth;
27 TSStateId state;
28} StackSummaryEntry;
29typedef Array(StackSummaryEntry) StackSummary;
30
31// Create a stack.
32Stack *ts_stack_new(SubtreePool *);
33
34// Release the memory reserved for a given stack.
35void ts_stack_delete(Stack *);
36
37// Get the stack's current number of versions.
38uint32_t ts_stack_version_count(const Stack *);
39
40// Get the state at the top of the given version of the stack. If the stack is
41// empty, this returns the initial state, 0.
42TSStateId ts_stack_state(const Stack *, StackVersion);
43
44// Get the last external token associated with a given version of the stack.
45Subtree ts_stack_last_external_token(const Stack *, StackVersion);
46
47// Set the last external token associated with a given version of the stack.
48void ts_stack_set_last_external_token(Stack *, StackVersion, Subtree );
49
50// Get the position of the given version of the stack within the document.
51Length ts_stack_position(const Stack *, StackVersion);
52
53// Push a tree and state onto the given version of the stack.
54//
55// This transfers ownership of the tree to the Stack. Callers that
56// need to retain ownership of the tree for their own purposes should
57// first retain the tree.
58void ts_stack_push(Stack *, StackVersion, Subtree , bool, TSStateId);
59
60// Pop the given number of entries from the given version of the stack. This
61// operation can increase the number of stack versions by revealing multiple
62// versions which had previously been merged. It returns an array that
63// specifies the index of each revealed version and the trees that were
64// removed from that version.
65StackSliceArray ts_stack_pop_count(Stack *, StackVersion, uint32_t count);
66
67// Remove an error at the top of the given version of the stack.
68SubtreeArray ts_stack_pop_error(Stack *, StackVersion);
69
70// Remove any pending trees from the top of the given version of the stack.
71StackSliceArray ts_stack_pop_pending(Stack *, StackVersion);
72
73// Remove any all trees from the given version of the stack.
74StackSliceArray ts_stack_pop_all(Stack *, StackVersion);
75
76// Get the maximum number of tree nodes reachable from this version of the stack
77// since the last error was detected.
78unsigned ts_stack_node_count_since_error(const Stack *, StackVersion);
79
80int ts_stack_dynamic_precedence(Stack *, StackVersion);
81
82bool ts_stack_has_advanced_since_error(const Stack *, StackVersion);
83
84// Compute a summary of all the parse states near the top of the given
85// version of the stack and store the summary for later retrieval.
86void ts_stack_record_summary(Stack *, StackVersion, unsigned max_depth);
87
88// Retrieve a summary of all the parse states near the top of the
89// given version of the stack.
90StackSummary *ts_stack_get_summary(Stack *, StackVersion);
91
92// Get the total cost of all errors on the given version of the stack.
93unsigned ts_stack_error_cost(const Stack *, StackVersion version);
94
95// Merge the given two stack versions if possible, returning true
96// if they were successfully merged and false otherwise.
97bool ts_stack_merge(Stack *, StackVersion, StackVersion);
98
99// Determine whether the given two stack versions can be merged.
100bool ts_stack_can_merge(Stack *, StackVersion, StackVersion);
101
102Subtree ts_stack_resume(Stack *, StackVersion);
103
104void ts_stack_pause(Stack *, StackVersion, Subtree);
105
106void ts_stack_halt(Stack *, StackVersion);
107
108bool ts_stack_is_active(const Stack *, StackVersion);
109
110bool ts_stack_is_paused(const Stack *, StackVersion);
111
112bool ts_stack_is_halted(const Stack *, StackVersion);
113
114void ts_stack_renumber_version(Stack *, StackVersion, StackVersion);
115
116void ts_stack_swap_versions(Stack *, StackVersion, StackVersion);
117
118StackVersion ts_stack_copy_version(Stack *, StackVersion);
119
120// Remove the given version from the stack.
121void ts_stack_remove_version(Stack *, StackVersion);
122
123void ts_stack_clear(Stack *);
124
125bool ts_stack_print_dot_graph(Stack *, const TSLanguage *, FILE *);
126
127typedef void (*StackIterateCallback)(void *, TSStateId, uint32_t);
128
129#ifdef __cplusplus
130}
131#endif
132
133#endif // TREE_SITTER_PARSE_STACK_H_