1#ifndef TREE_SITTER_PARSER_H_
2#define TREE_SITTER_PARSER_H_
3
4#ifdef __cplusplus
5extern "C" {
6#endif
7
8#include <stdbool.h>
9#include <stdint.h>
10#include <stdlib.h>
11
12#define ts_builtin_sym_error ((TSSymbol)-1)
13#define ts_builtin_sym_end 0
14#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
15
16#ifndef TREE_SITTER_API_H_
17typedef uint16_t TSSymbol;
18typedef uint16_t TSFieldId;
19typedef struct TSLanguage TSLanguage;
20#endif
21
22typedef struct {
23 TSFieldId field_id;
24 uint8_t child_index;
25 bool inherited;
26} TSFieldMapEntry;
27
28typedef struct {
29 uint16_t index;
30 uint16_t length;
31} TSFieldMapSlice;
32
33typedef uint16_t TSStateId;
34
35typedef struct {
36 bool visible : 1;
37 bool named : 1;
38 bool supertype: 1;
39} TSSymbolMetadata;
40
41typedef struct TSLexer TSLexer;
42
43struct TSLexer {
44 int32_t lookahead;
45 TSSymbol result_symbol;
46 void (*advance)(TSLexer *, bool);
47 void (*mark_end)(TSLexer *);
48 uint32_t (*get_column)(TSLexer *);
49 bool (*is_at_included_range_start)(const TSLexer *);
50 bool (*eof)(const TSLexer *);
51};
52
53typedef enum {
54 TSParseActionTypeShift,
55 TSParseActionTypeReduce,
56 TSParseActionTypeAccept,
57 TSParseActionTypeRecover,
58} TSParseActionType;
59
60typedef struct {
61 union {
62 struct {
63 TSStateId state;
64 bool extra : 1;
65 bool repetition : 1;
66 } shift;
67 struct {
68 TSSymbol symbol;
69 int16_t dynamic_precedence;
70 uint8_t child_count;
71 uint8_t production_id;
72 } reduce;
73 } params;
74 TSParseActionType type : 4;
75} TSParseAction;
76
77typedef struct {
78 uint16_t lex_state;
79 uint16_t external_lex_state;
80} TSLexMode;
81
82typedef union {
83 TSParseAction action;
84 struct {
85 uint8_t count;
86 bool reusable : 1;
87 } entry;
88} TSParseActionEntry;
89
90struct TSLanguage {
91 uint32_t version;
92 uint32_t symbol_count;
93 uint32_t alias_count;
94 uint32_t token_count;
95 uint32_t external_token_count;
96 const char **symbol_names;
97 const TSSymbolMetadata *symbol_metadata;
98 const uint16_t *parse_table;
99 const TSParseActionEntry *parse_actions;
100 const TSLexMode *lex_modes;
101 const TSSymbol *alias_sequences;
102 uint16_t max_alias_sequence_length;
103 bool (*lex_fn)(TSLexer *, TSStateId);
104 bool (*keyword_lex_fn)(TSLexer *, TSStateId);
105 TSSymbol keyword_capture_token;
106 struct {
107 const bool *states;
108 const TSSymbol *symbol_map;
109 void *(*create)(void);
110 void (*destroy)(void *);
111 bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
112 unsigned (*serialize)(void *, char *);
113 void (*deserialize)(void *, const char *, unsigned);
114 } external_scanner;
115 uint32_t field_count;
116 const TSFieldMapSlice *field_map_slices;
117 const TSFieldMapEntry *field_map_entries;
118 const char **field_names;
119 uint32_t large_state_count;
120 const uint16_t *small_parse_table;
121 const uint32_t *small_parse_table_map;
122 const TSSymbol *public_symbol_map;
123 const uint16_t *alias_map;
124 uint32_t state_count;
125};
126
127/*
128 * Lexer Macros
129 */
130
131#define START_LEXER() \
132 bool result = false; \
133 bool skip = false; \
134 bool eof = false; \
135 int32_t lookahead; \
136 goto start; \
137 next_state: \
138 lexer->advance(lexer, skip); \
139 start: \
140 skip = false; \
141 lookahead = lexer->lookahead;
142
143#define ADVANCE(state_value) \
144 { \
145 state = state_value; \
146 goto next_state; \
147 }
148
149#define SKIP(state_value) \
150 { \
151 skip = true; \
152 state = state_value; \
153 goto next_state; \
154 }
155
156#define ACCEPT_TOKEN(symbol_value) \
157 result = true; \
158 lexer->result_symbol = symbol_value; \
159 lexer->mark_end(lexer);
160
161#define END_STATE() return result;
162
163/*
164 * Parse Table Macros
165 */
166
167#define SMALL_STATE(id) id - LARGE_STATE_COUNT
168
169#define STATE(id) id
170
171#define ACTIONS(id) id
172
173#define SHIFT(state_value) \
174 { \
175 { \
176 .params = { \
177 .shift = { \
178 .state = state_value \
179 } \
180 }, \
181 .type = TSParseActionTypeShift \
182 } \
183 }
184
185#define SHIFT_REPEAT(state_value) \
186 { \
187 { \
188 .params = { \
189 .shift = { \
190 .state = state_value, \
191 .repetition = true \
192 } \
193 }, \
194 .type = TSParseActionTypeShift \
195 } \
196 }
197
198#define RECOVER() \
199 { \
200 { .type = TSParseActionTypeRecover } \
201 }
202
203#define SHIFT_EXTRA() \
204 { \
205 { \
206 .params = { \
207 .shift = { \
208 .extra = true \
209 } \
210 }, \
211 .type = TSParseActionTypeShift \
212 } \
213 }
214
215#define REDUCE(symbol_val, child_count_val, ...) \
216 { \
217 { \
218 .params = { \
219 .reduce = { \
220 .symbol = symbol_val, \
221 .child_count = child_count_val, \
222 __VA_ARGS__ \
223 }, \
224 }, \
225 .type = TSParseActionTypeReduce \
226 } \
227 }
228
229#define ACCEPT_INPUT() \
230 { \
231 { .type = TSParseActionTypeAccept } \
232 }
233
234#ifdef __cplusplus
235}
236#endif
237
238#endif // TREE_SITTER_PARSER_H_