Debian Codegraph @

Xtree-house/bindings/vendor/src/subtree.h

raw
1 #ifndef TREE_SITTER_SUBTREE_H_
2 #define TREE_SITTER_SUBTREE_H_
3
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7
8 #include <limits.h>
9 #include <stdbool.h>
10 #include <stdio.h>
11 #include "./length.h"
12 #include "./array.h"
13 #include "./error_costs.h"
14 #include "./host.h"
15 #include "tree_sitter/api.h"
16 #include "./parser.h"
17
18 #define TS_TREE_STATE_NONE USHRT_MAX
19 #define NULL_SUBTREE ((Subtree) {.ptr = NULL})
20
21 // The serialized state of an external scanner.
22 //
23 // Every time an external token subtree is created after a call to an
24 // external scanner, the scanner's `serialize` function is called to
25 // retrieve a serialized copy of its state. The bytes are then copied
26 // onto the subtree itself so that the scanner's state can later be
27 // restored using its `deserialize` function.
28 //
29 // Small byte arrays are stored inline, and long ones are allocated
30 // separately on the heap.
31 typedef struct {
32 union {
33 char *long_data;
34 char short_data[24];
35 };
36 uint32_t length;
37 } ExternalScannerState;
38
39 // A compact representation of a subtree.
40 //
41 // This representation is used for small leaf nodes that are not
42 // errors, and were not created by an external scanner.
43 //
44 // The idea behind the layout of this struct is that the `is_inline`
45 // bit will fall exactly into the same location as the least significant
46 // bit of the pointer in `Subtree` or `MutableSubtree`, respectively.
47 // Because of alignment, for any valid pointer this will be 0, giving
48 // us the opportunity to make use of this bit to signify whether to use
49 // the pointer or the inline struct.
50 typedef struct SubtreeInlineData SubtreeInlineData;
51
52 #define SUBTREE_BITS \
53 bool visible : 1; \
54 bool named : 1; \
55 bool extra : 1; \
56 bool has_changes : 1; \
57 bool is_missing : 1; \
58 bool is_keyword : 1;
59
60 #define SUBTREE_SIZE \
61 uint8_t padding_columns; \
62 uint8_t padding_rows : 4; \
63 uint8_t lookahead_bytes : 4; \
64 uint8_t padding_bytes; \
65 uint8_t size_bytes;
66
67 #if TS_BIG_ENDIAN
68 #if TS_PTR_SIZE == 32
69
70 struct SubtreeInlineData {
71 uint16_t parse_state;
72 uint8_t symbol;
73 SUBTREE_BITS
74 bool unused : 1;
75 bool is_inline : 1;
76 SUBTREE_SIZE
77 };
78
79 #else
80
81 struct SubtreeInlineData {
82 SUBTREE_SIZE
83 uint16_t parse_state;
84 uint8_t symbol;
85 SUBTREE_BITS
86 bool unused : 1;
87 bool is_inline : 1;
88 };
89
90 #endif
91 #else
92
93 struct SubtreeInlineData {
94 bool is_inline : 1;
95 SUBTREE_BITS
96 uint8_t symbol;
97 uint16_t parse_state;
98 SUBTREE_SIZE
99 };
100
101 #endif
102
103 #undef SUBTREE_BITS
104 #undef SUBTREE_SIZE
105
106 // A heap-allocated representation of a subtree.
107 //
108 // This representation is used for parent nodes, external tokens,
109 // errors, and other leaf nodes whose data is too large to fit into
110 // the inline representation.
111 typedef struct {
112 volatile uint32_t ref_count;
113 Length padding;
114 Length size;
115 uint32_t lookahead_bytes;
116 uint32_t error_cost;
117 uint32_t child_count;
118 TSSymbol symbol;
119 TSStateId parse_state;
120
121 bool visible : 1;
122 bool named : 1;
123 bool extra : 1;
124 bool fragile_left : 1;
125 bool fragile_right : 1;
126 bool has_changes : 1;
127 bool has_external_tokens : 1;
128 bool has_external_scanner_state_change : 1;
129 bool depends_on_column: 1;
130 bool is_missing : 1;
131 bool is_keyword : 1;
132
133 union {
134 // Non-terminal subtrees (`child_count > 0`)
135 struct {
136 uint32_t visible_child_count;
137 uint32_t named_child_count;
138 uint32_t visible_descendant_count;
139 int32_t dynamic_precedence;
140 uint16_t repeat_depth;
141 uint16_t production_id;
142 struct {
143 TSSymbol symbol;
144 TSStateId parse_state;
145 } first_leaf;
146 };
147
148 // External terminal subtrees (`child_count == 0 && has_external_tokens`)
149 ExternalScannerState external_scanner_state;
150
151 // Error terminal subtrees (`child_count == 0 && symbol == ts_builtin_sym_error`)
152 int32_t lookahead_char;
153 };
154 } SubtreeHeapData;
155
156 // The fundamental building block of a syntax tree.
157 typedef union {
158 SubtreeInlineData data;
159 const SubtreeHeapData *ptr;
160 } Subtree;
161
162 // Like Subtree, but mutable.
163 typedef union {
164 SubtreeInlineData data;
165 SubtreeHeapData *ptr;
166 } MutableSubtree;
167
168 typedef Array(Subtree) SubtreeArray;
169 typedef Array(MutableSubtree) MutableSubtreeArray;
170
171 typedef struct {
172 MutableSubtreeArray free_trees;
173 MutableSubtreeArray tree_stack;
174 } SubtreePool;
175
176 void ts_external_scanner_state_init(ExternalScannerState *self, const char *data, unsigned length);
177 const char *ts_external_scanner_state_data(const ExternalScannerState *self);
178 bool ts_external_scanner_state_eq(const ExternalScannerState *self, const char *buffer, unsigned length);
179 void ts_external_scanner_state_delete(ExternalScannerState *self);
180
181 void ts_subtree_array_copy(SubtreeArray self, SubtreeArray *dest);
182 void ts_subtree_array_clear(SubtreePool *pool, SubtreeArray *self);
183 void ts_subtree_array_delete(SubtreePool *pool, SubtreeArray *self);
184 void ts_subtree_array_remove_trailing_extras(SubtreeArray *self, SubtreeArray *destination);
185 void ts_subtree_array_reverse(SubtreeArray *self);
186
187 SubtreePool ts_subtree_pool_new(uint32_t capacity);
188 void ts_subtree_pool_delete(SubtreePool *self);
189
190 Subtree ts_subtree_new_leaf(
191 SubtreePool *pool, TSSymbol symbol, Length padding, Length size,
192 uint32_t lookahead_bytes, TSStateId parse_state,
193 bool has_external_tokens, bool depends_on_column,
194 bool is_keyword, const TSLanguage *language
195 );
196 Subtree ts_subtree_new_error(
197 SubtreePool *pool, int32_t lookahead_char, Length padding, Length size,
198 uint32_t bytes_scanned, TSStateId parse_state, const TSLanguage *language
199 );
200 MutableSubtree ts_subtree_new_node(
201 TSSymbol symbol,
202 SubtreeArray *chiildren,
203 unsigned production_id,
204 const TSLanguage *language
205 );
206 Subtree ts_subtree_new_error_node(
207 SubtreeArray *children,
208 bool extra,
209 const TSLanguage * language
210 );
211 Subtree ts_subtree_new_missing_leaf(
212 SubtreePool *pool,
213 TSSymbol symbol,
214 Length padding,
215 uint32_t lookahead_bytes,
216 const TSLanguage *language
217 );
218 MutableSubtree ts_subtree_make_mut(SubtreePool *pool, Subtree self);
219 void ts_subtree_retain(Subtree self);
220 void ts_subtree_release(SubtreePool *pool, Subtree self);
221 int ts_subtree_compare(Subtree left, Subtree right, SubtreePool *pool);
222 void ts_subtree_set_symbol(MutableSubtree *self, TSSymbol symbol, const TSLanguage *language);
223 void ts_subtree_compress(MutableSubtree self, unsigned count, const TSLanguage *language, MutableSubtreeArray *stack);
224 void ts_subtree_summarize_children(MutableSubtree self, const TSLanguage *language);
225 Subtree ts_subtree_edit(Subtree self, const TSInputEdit *edit, SubtreePool *pool);
226 char *ts_subtree_string(Subtree self, TSSymbol alias_symbol, bool alias_is_named, const TSLanguage *language, bool include_all);
227 void ts_subtree_print_dot_graph(Subtree self, const TSLanguage *language, FILE *f);
228 Subtree ts_subtree_last_external_token(Subtree tree);
229 const ExternalScannerState *ts_subtree_external_scanner_state(Subtree self);
230 bool ts_subtree_external_scanner_state_eq(Subtree self, Subtree other);
231
232 #define SUBTREE_GET(self, name) ((self).data.is_inline ? (self).data.name : (self).ptr->name)
233
234 static inline TSSymbol ts_subtree_symbol(Subtree self) { return SUBTREE_GET(self, symbol); }
235 static inline bool ts_subtree_visible(Subtree self) { return SUBTREE_GET(self, visible); }
236 static inline bool ts_subtree_named(Subtree self) { return SUBTREE_GET(self, named); }
237 static inline bool ts_subtree_extra(Subtree self) { return SUBTREE_GET(self, extra); }
238 static inline bool ts_subtree_has_changes(Subtree self) { return SUBTREE_GET(self, has_changes); }
239 static inline bool ts_subtree_missing(Subtree self) { return SUBTREE_GET(self, is_missing); }
240 static inline bool ts_subtree_is_keyword(Subtree self) { return SUBTREE_GET(self, is_keyword); }
241 static inline TSStateId ts_subtree_parse_state(Subtree self) { return SUBTREE_GET(self, parse_state); }
242 static inline uint32_t ts_subtree_lookahead_bytes(Subtree self) { return SUBTREE_GET(self, lookahead_bytes); }
243
244 #undef SUBTREE_GET
245
246 // Get the size needed to store a heap-allocated subtree with the given
247 // number of children.
248 static inline size_t ts_subtree_alloc_size(uint32_t child_count) {
249 return child_count * sizeof(Subtree) + sizeof(SubtreeHeapData);
250 }
251
252 // Get a subtree's children, which are allocated immediately before the
253 // tree's own heap data.
254 #define ts_subtree_children(self) \
255 ((self).data.is_inline ? (Subtree *)NULL : (Subtree *)((self).ptr) - (self).ptr->child_count)
256
257 static inline void ts_subtree_set_extra(MutableSubtree *self, bool is_extra) {
258 if (self->data.is_inline) {
259 self->data.extra = is_extra;
260 } else {
261 self->ptr->extra = is_extra;
262 }
263 }
264
265 static inline TSSymbol ts_subtree_leaf_symbol(Subtree self) {
266 if (self.data.is_inline) return self.data.symbol;
267 if (self.ptr->child_count == 0) return self.ptr->symbol;
268 return self.ptr->first_leaf.symbol;
269 }
270
271 static inline TSStateId ts_subtree_leaf_parse_state(Subtree self) {
272 if (self.data.is_inline) return self.data.parse_state;
273 if (self.ptr->child_count == 0) return self.ptr->parse_state;
274 return self.ptr->first_leaf.parse_state;
275 }
276
277 static inline Length ts_subtree_padding(Subtree self) {
278 if (self.data.is_inline) {
279 Length result = {self.data.padding_bytes, {self.data.padding_rows, self.data.padding_columns}};
280 return result;
281 } else {
282 return self.ptr->padding;
283 }
284 }
285
286 static inline Length ts_subtree_size(Subtree self) {
287 if (self.data.is_inline) {
288 Length result = {self.data.size_bytes, {0, self.data.size_bytes}};
289 return result;
290 } else {
291 return self.ptr->size;
292 }
293 }
294
295 static inline Length ts_subtree_total_size(Subtree self) {
296 return length_add(ts_subtree_padding(self), ts_subtree_size(self));
297 }
298
299 static inline uint32_t ts_subtree_total_bytes(Subtree self) {
300 return ts_subtree_total_size(self).bytes;
301 }
302
303 static inline uint32_t ts_subtree_child_count(Subtree self) {
304 return self.data.is_inline ? 0 : self.ptr->child_count;
305 }
306
307 static inline uint32_t ts_subtree_repeat_depth(Subtree self) {
308 return self.data.is_inline ? 0 : self.ptr->repeat_depth;
309 }
310
311 static inline uint32_t ts_subtree_is_repetition(Subtree self) {
312 return self.data.is_inline
313 ? 0
314 : !self.ptr->named && !self.ptr->visible && self.ptr->child_count != 0;
315 }
316
317 static inline uint32_t ts_subtree_visible_descendant_count(Subtree self) {
318 return (self.data.is_inline || self.ptr->child_count == 0)
319 ? 0
320 : self.ptr->visible_descendant_count;
321 }
322
323 static inline uint32_t ts_subtree_visible_child_count(Subtree self) {
324 if (ts_subtree_child_count(self) > 0) {
325 return self.ptr->visible_child_count;
326 } else {
327 return 0;
328 }
329 }
330
331 static inline uint32_t ts_subtree_error_cost(Subtree self) {
332 if (ts_subtree_missing(self)) {
333 return ERROR_COST_PER_MISSING_TREE + ERROR_COST_PER_RECOVERY;
334 } else {
335 return self.data.is_inline ? 0 : self.ptr->error_cost;
336 }
337 }
338
339 static inline int32_t ts_subtree_dynamic_precedence(Subtree self) {
340 return (self.data.is_inline || self.ptr->child_count == 0) ? 0 : self.ptr->dynamic_precedence;
341 }
342
343 static inline uint16_t ts_subtree_production_id(Subtree self) {
344 if (ts_subtree_child_count(self) > 0) {
345 return self.ptr->production_id;
346 } else {
347 return 0;
348 }
349 }
350
351 static inline bool ts_subtree_fragile_left(Subtree self) {
352 return self.data.is_inline ? false : self.ptr->fragile_left;
353 }
354
355 static inline bool ts_subtree_fragile_right(Subtree self) {
356 return self.data.is_inline ? false : self.ptr->fragile_right;
357 }
358
359 static inline bool ts_subtree_has_external_tokens(Subtree self) {
360 return self.data.is_inline ? false : self.ptr->has_external_tokens;
361 }
362
363 static inline bool ts_subtree_has_external_scanner_state_change(Subtree self) {
364 return self.data.is_inline ? false : self.ptr->has_external_scanner_state_change;
365 }
366
367 static inline bool ts_subtree_depends_on_column(Subtree self) {
368 return self.data.is_inline ? false : self.ptr->depends_on_column;
369 }
370
371 static inline bool ts_subtree_is_fragile(Subtree self) {
372 return self.data.is_inline ? false : (self.ptr->fragile_left || self.ptr->fragile_right);
373 }
374
375 static inline bool ts_subtree_is_error(Subtree self) {
376 return ts_subtree_symbol(self) == ts_builtin_sym_error;
377 }
378
379 static inline bool ts_subtree_is_eof(Subtree self) {
380 return ts_subtree_symbol(self) == ts_builtin_sym_end;
381 }
382
383 static inline Subtree ts_subtree_from_mut(MutableSubtree self) {
384 Subtree result;
385 result.data = self.data;
386 return result;
387 }
388
389 static inline MutableSubtree ts_subtree_to_mut_unsafe(Subtree self) {
390 MutableSubtree result;
391 result.data = self.data;
392 return result;
393 }
394
395 #ifdef __cplusplus
396 }
397 #endif
398
399 #endif // TREE_SITTER_SUBTREE_H_