Debian Codegraph @

Xtree-house/bindings/vendor/src/node.c

raw
1 #include <stdbool.h>
2 #include "./point.h"
3 #include "./subtree.h"
4 #include "./tree.h"
5 #include "./language.h"
6
7 typedef struct {
8 Subtree parent;
9 const TSTree *tree;
10 Length position;
11 uint32_t child_index;
12 uint32_t structural_child_index;
13 const TSSymbol *alias_sequence;
14 } NodeChildIterator;
15
16 static inline bool ts_node__is_relevant(TSNode self, bool include_anonymous);
17
18 // TSNode - constructors
19
20 TSNode ts_node_new(
21 const TSTree *tree,
22 const Subtree *subtree,
23 Length position,
24 TSSymbol alias
25 ) {
26 return (TSNode) {
27 {position.bytes, position.extent.row, position.extent.column, alias},
28 subtree,
29 tree,
30 };
31 }
32
33 static inline TSNode ts_node__null(void) {
34 return ts_node_new(NULL, NULL, length_zero(), 0);
35 }
36
37 // TSNode - accessors
38
39 uint32_t ts_node_start_byte(TSNode self) {
40 return self.context[0];
41 }
42
43 TSPoint ts_node_start_point(TSNode self) {
44 return (TSPoint) {self.context[1], self.context[2]};
45 }
46
47 static inline uint32_t ts_node__alias(const TSNode *self) {
48 return self->context[3];
49 }
50
51 static inline Subtree ts_node__subtree(TSNode self) {
52 return *(const Subtree *)self.id;
53 }
54
55 // NodeChildIterator
56
57 static inline NodeChildIterator ts_node_iterate_children(const TSNode *node) {
58 Subtree subtree = ts_node__subtree(*node);
59 if (ts_subtree_child_count(subtree) == 0) {
60 return (NodeChildIterator) {NULL_SUBTREE, node->tree, length_zero(), 0, 0, NULL};
61 }
62 const TSSymbol *alias_sequence = ts_language_alias_sequence(
63 node->tree->language,
64 subtree.ptr->production_id
65 );
66 return (NodeChildIterator) {
67 .tree = node->tree,
68 .parent = subtree,
69 .position = {ts_node_start_byte(*node), ts_node_start_point(*node)},
70 .child_index = 0,
71 .structural_child_index = 0,
72 .alias_sequence = alias_sequence,
73 };
74 }
75
76 static inline bool ts_node_child_iterator_done(NodeChildIterator *self) {
77 return self->child_index == self->parent.ptr->child_count;
78 }
79
80 static inline bool ts_node_child_iterator_next(
81 NodeChildIterator *self,
82 TSNode *result
83 ) {
84 if (!self->parent.ptr || ts_node_child_iterator_done(self)) return false;
85 const Subtree *child = &ts_subtree_children(self->parent)[self->child_index];
86 TSSymbol alias_symbol = 0;
87 if (!ts_subtree_extra(*child)) {
88 if (self->alias_sequence) {
89 alias_symbol = self->alias_sequence[self->structural_child_index];
90 }
91 self->structural_child_index++;
92 }
93 if (self->child_index > 0) {
94 self->position = length_add(self->position, ts_subtree_padding(*child));
95 }
96 *result = ts_node_new(
97 self->tree,
98 child,
99 self->position,
100 alias_symbol
101 );
102 self->position = length_add(self->position, ts_subtree_size(*child));
103 self->child_index++;
104 return true;
105 }
106
107 // TSNode - private
108
109 static inline bool ts_node__is_relevant(TSNode self, bool include_anonymous) {
110 Subtree tree = ts_node__subtree(self);
111 if (include_anonymous) {
112 return ts_subtree_visible(tree) || ts_node__alias(&self);
113 } else {
114 TSSymbol alias = ts_node__alias(&self);
115 if (alias) {
116 return ts_language_symbol_metadata(self.tree->language, alias).named;
117 } else {
118 return ts_subtree_visible(tree) && ts_subtree_named(tree);
119 }
120 }
121 }
122
123 static inline uint32_t ts_node__relevant_child_count(
124 TSNode self,
125 bool include_anonymous
126 ) {
127 Subtree tree = ts_node__subtree(self);
128 if (ts_subtree_child_count(tree) > 0) {
129 if (include_anonymous) {
130 return tree.ptr->visible_child_count;
131 } else {
132 return tree.ptr->named_child_count;
133 }
134 } else {
135 return 0;
136 }
137 }
138
139 static inline TSNode ts_node__child(
140 TSNode self,
141 uint32_t child_index,
142 bool include_anonymous
143 ) {
144 TSNode result = self;
145 bool did_descend = true;
146
147 while (did_descend) {
148 did_descend = false;
149
150 TSNode child;
151 uint32_t index = 0;
152 NodeChildIterator iterator = ts_node_iterate_children(&result);
153 while (ts_node_child_iterator_next(&iterator, &child)) {
154 if (ts_node__is_relevant(child, include_anonymous)) {
155 if (index == child_index) {
156 return child;
157 }
158 index++;
159 } else {
160 uint32_t grandchild_index = child_index - index;
161 uint32_t grandchild_count = ts_node__relevant_child_count(child, include_anonymous);
162 if (grandchild_index < grandchild_count) {
163 did_descend = true;
164 result = child;
165 child_index = grandchild_index;
166 break;
167 }
168 index += grandchild_count;
169 }
170 }
171 }
172
173 return ts_node__null();
174 }
175
176 static bool ts_subtree_has_trailing_empty_descendant(
177 Subtree self,
178 Subtree other
179 ) {
180 for (unsigned i = ts_subtree_child_count(self) - 1; i + 1 > 0; i--) {
181 Subtree child = ts_subtree_children(self)[i];
182 if (ts_subtree_total_bytes(child) > 0) break;
183 if (child.ptr == other.ptr || ts_subtree_has_trailing_empty_descendant(child, other)) {
184 return true;
185 }
186 }
187 return false;
188 }
189
190 static inline TSNode ts_node__prev_sibling(TSNode self, bool include_anonymous) {
191 Subtree self_subtree = ts_node__subtree(self);
192 bool self_is_empty = ts_subtree_total_bytes(self_subtree) == 0;
193 uint32_t target_end_byte = ts_node_end_byte(self);
194
195 TSNode node = ts_node_parent(self);
196 TSNode earlier_node = ts_node__null();
197 bool earlier_node_is_relevant = false;
198
199 while (!ts_node_is_null(node)) {
200 TSNode earlier_child = ts_node__null();
201 bool earlier_child_is_relevant = false;
202 bool found_child_containing_target = false;
203
204 TSNode child;
205 NodeChildIterator iterator = ts_node_iterate_children(&node);
206 while (ts_node_child_iterator_next(&iterator, &child)) {
207 if (child.id == self.id) break;
208 if (iterator.position.bytes > target_end_byte) {
209 found_child_containing_target = true;
210 break;
211 }
212
213 if (iterator.position.bytes == target_end_byte &&
214 (!self_is_empty ||
215 ts_subtree_has_trailing_empty_descendant(ts_node__subtree(child), self_subtree))) {
216 found_child_containing_target = true;
217 break;
218 }
219
220 if (ts_node__is_relevant(child, include_anonymous)) {
221 earlier_child = child;
222 earlier_child_is_relevant = true;
223 } else if (ts_node__relevant_child_count(child, include_anonymous) > 0) {
224 earlier_child = child;
225 earlier_child_is_relevant = false;
226 }
227 }
228
229 if (found_child_containing_target) {
230 if (!ts_node_is_null(earlier_child)) {
231 earlier_node = earlier_child;
232 earlier_node_is_relevant = earlier_child_is_relevant;
233 }
234 node = child;
235 } else if (earlier_child_is_relevant) {
236 return earlier_child;
237 } else if (!ts_node_is_null(earlier_child)) {
238 node = earlier_child;
239 } else if (earlier_node_is_relevant) {
240 return earlier_node;
241 } else {
242 node = earlier_node;
243 earlier_node = ts_node__null();
244 earlier_node_is_relevant = false;
245 }
246 }
247
248 return ts_node__null();
249 }
250
251 static inline TSNode ts_node__next_sibling(TSNode self, bool include_anonymous) {
252 uint32_t target_end_byte = ts_node_end_byte(self);
253
254 TSNode node = ts_node_parent(self);
255 TSNode later_node = ts_node__null();
256 bool later_node_is_relevant = false;
257
258 while (!ts_node_is_null(node)) {
259 TSNode later_child = ts_node__null();
260 bool later_child_is_relevant = false;
261 TSNode child_containing_target = ts_node__null();
262
263 TSNode child;
264 NodeChildIterator iterator = ts_node_iterate_children(&node);
265 while (ts_node_child_iterator_next(&iterator, &child)) {
266 if (iterator.position.bytes <= target_end_byte) continue;
267 uint32_t start_byte = ts_node_start_byte(self);
268 uint32_t child_start_byte = ts_node_start_byte(child);
269
270 bool is_empty = start_byte == target_end_byte;
271 bool contains_target = is_empty ?
272 child_start_byte < start_byte :
273 child_start_byte <= start_byte;
274
275 if (contains_target) {
276 if (ts_node__subtree(child).ptr != ts_node__subtree(self).ptr) {
277 child_containing_target = child;
278 }
279 } else if (ts_node__is_relevant(child, include_anonymous)) {
280 later_child = child;
281 later_child_is_relevant = true;
282 break;
283 } else if (ts_node__relevant_child_count(child, include_anonymous) > 0) {
284 later_child = child;
285 later_child_is_relevant = false;
286 break;
287 }
288 }
289
290 if (!ts_node_is_null(child_containing_target)) {
291 if (!ts_node_is_null(later_child)) {
292 later_node = later_child;
293 later_node_is_relevant = later_child_is_relevant;
294 }
295 node = child_containing_target;
296 } else if (later_child_is_relevant) {
297 return later_child;
298 } else if (!ts_node_is_null(later_child)) {
299 node = later_child;
300 } else if (later_node_is_relevant) {
301 return later_node;
302 } else {
303 node = later_node;
304 }
305 }
306
307 return ts_node__null();
308 }
309
310 static inline TSNode ts_node__first_child_for_byte(
311 TSNode self,
312 uint32_t goal,
313 bool include_anonymous
314 ) {
315 TSNode node = self;
316 bool did_descend = true;
317
318 NodeChildIterator last_iterator;
319 bool has_last_iterator = false;
320
321 while (did_descend) {
322 did_descend = false;
323
324 TSNode child;
325 NodeChildIterator iterator = ts_node_iterate_children(&node);
326 loop:
327 while (ts_node_child_iterator_next(&iterator, &child)) {
328 if (ts_node_end_byte(child) > goal) {
329 if (ts_node__is_relevant(child, include_anonymous)) {
330 return child;
331 } else if (ts_node_child_count(child) > 0) {
332 if (iterator.child_index < ts_subtree_child_count(ts_node__subtree(child))) {
333 last_iterator = iterator;
334 has_last_iterator = true;
335 }
336 did_descend = true;
337 node = child;
338 break;
339 }
340 }
341 }
342
343 if (!did_descend && has_last_iterator) {
344 iterator = last_iterator;
345 has_last_iterator = false;
346 goto loop;
347 }
348 }
349
350 return ts_node__null();
351 }
352
353 static inline TSNode ts_node__descendant_for_byte_range(
354 TSNode self,
355 uint32_t range_start,
356 uint32_t range_end,
357 bool include_anonymous
358 ) {
359 if (range_start > range_end) {
360 return ts_node__null();
361 }
362 TSNode node = self;
363 TSNode last_visible_node = self;
364
365 bool did_descend = true;
366 while (did_descend) {
367 did_descend = false;
368
369 TSNode child;
370 NodeChildIterator iterator = ts_node_iterate_children(&node);
371 while (ts_node_child_iterator_next(&iterator, &child)) {
372 uint32_t node_end = iterator.position.bytes;
373
374 // The end of this node must extend far enough forward to touch
375 // the end of the range
376 if (node_end < range_end) continue;
377
378 // ...and exceed the start of the range, unless the node itself is
379 // empty, in which case it must at least be equal to the start of the range.
380 bool is_empty = ts_node_start_byte(child) == node_end;
381 if (is_empty ? node_end < range_start : node_end <= range_start) continue;
382
383 // The start of this node must extend far enough backward to
384 // touch the start of the range.
385 if (range_start < ts_node_start_byte(child)) break;
386
387 node = child;
388 if (ts_node__is_relevant(node, include_anonymous)) {
389 last_visible_node = node;
390 }
391 did_descend = true;
392 break;
393 }
394 }
395
396 return last_visible_node;
397 }
398
399 static inline TSNode ts_node__descendant_for_point_range(
400 TSNode self,
401 TSPoint range_start,
402 TSPoint range_end,
403 bool include_anonymous
404 ) {
405 if (point_gt(range_start, range_end)) {
406 return ts_node__null();
407 }
408 TSNode node = self;
409 TSNode last_visible_node = self;
410
411 bool did_descend = true;
412 while (did_descend) {
413 did_descend = false;
414
415 TSNode child;
416 NodeChildIterator iterator = ts_node_iterate_children(&node);
417 while (ts_node_child_iterator_next(&iterator, &child)) {
418 TSPoint node_end = iterator.position.extent;
419
420 // The end of this node must extend far enough forward to touch
421 // the end of the range
422 if (point_lt(node_end, range_end)) continue;
423
424 // ...and exceed the start of the range, unless the node itself is
425 // empty, in which case it must at least be equal to the start of the range.
426 bool is_empty = point_eq(ts_node_start_point(child), node_end);
427 if (is_empty ? point_lt(node_end, range_start) : point_lte(node_end, range_start)) {
428 continue;
429 }
430
431 // The start of this node must extend far enough backward to
432 // touch the start of the range.
433 if (point_lt(range_start, ts_node_start_point(child))) break;
434
435 node = child;
436 if (ts_node__is_relevant(node, include_anonymous)) {
437 last_visible_node = node;
438 }
439 did_descend = true;
440 break;
441 }
442 }
443
444 return last_visible_node;
445 }
446
447 // TSNode - public
448
449 uint32_t ts_node_end_byte(TSNode self) {
450 return ts_node_start_byte(self) + ts_subtree_size(ts_node__subtree(self)).bytes;
451 }
452
453 TSPoint ts_node_end_point(TSNode self) {
454 return point_add(ts_node_start_point(self), ts_subtree_size(ts_node__subtree(self)).extent);
455 }
456
457 TSSymbol ts_node_symbol(TSNode self) {
458 TSSymbol symbol = ts_node__alias(&self);
459 if (!symbol) symbol = ts_subtree_symbol(ts_node__subtree(self));
460 return ts_language_public_symbol(self.tree->language, symbol);
461 }
462
463 const char *ts_node_type(TSNode self) {
464 TSSymbol symbol = ts_node__alias(&self);
465 if (!symbol) symbol = ts_subtree_symbol(ts_node__subtree(self));
466 return ts_language_symbol_name(self.tree->language, symbol);
467 }
468
469 const TSLanguage *ts_node_language(TSNode self) {
470 return self.tree->language;
471 }
472
473 TSSymbol ts_node_grammar_symbol(TSNode self) {
474 return ts_subtree_symbol(ts_node__subtree(self));
475 }
476
477 const char *ts_node_grammar_type(TSNode self) {
478 TSSymbol symbol = ts_subtree_symbol(ts_node__subtree(self));
479 return ts_language_symbol_name(self.tree->language, symbol);
480 }
481
482 char *ts_node_string(TSNode self) {
483 TSSymbol alias_symbol = ts_node__alias(&self);
484 return ts_subtree_string(
485 ts_node__subtree(self),
486 alias_symbol,
487 ts_language_symbol_metadata(self.tree->language, alias_symbol).visible,
488 self.tree->language,
489 false
490 );
491 }
492
493 bool ts_node_eq(TSNode self, TSNode other) {
494 return self.tree == other.tree && self.id == other.id;
495 }
496
497 bool ts_node_is_null(TSNode self) {
498 return self.id == 0;
499 }
500
501 bool ts_node_is_extra(TSNode self) {
502 return ts_subtree_extra(ts_node__subtree(self));
503 }
504
505 bool ts_node_is_named(TSNode self) {
506 TSSymbol alias = ts_node__alias(&self);
507 return alias
508 ? ts_language_symbol_metadata(self.tree->language, alias).named
509 : ts_subtree_named(ts_node__subtree(self));
510 }
511
512 bool ts_node_is_missing(TSNode self) {
513 return ts_subtree_missing(ts_node__subtree(self));
514 }
515
516 bool ts_node_has_changes(TSNode self) {
517 return ts_subtree_has_changes(ts_node__subtree(self));
518 }
519
520 bool ts_node_has_error(TSNode self) {
521 return ts_subtree_error_cost(ts_node__subtree(self)) > 0;
522 }
523
524 bool ts_node_is_error(TSNode self) {
525 TSSymbol symbol = ts_node_symbol(self);
526 return symbol == ts_builtin_sym_error;
527 }
528
529 uint32_t ts_node_descendant_count(TSNode self) {
530 return ts_subtree_visible_descendant_count(ts_node__subtree(self)) + 1;
531 }
532
533 TSStateId ts_node_parse_state(TSNode self) {
534 return ts_subtree_parse_state(ts_node__subtree(self));
535 }
536
537 TSStateId ts_node_next_parse_state(TSNode self) {
538 const TSLanguage *language = self.tree->language;
539 uint16_t state = ts_node_parse_state(self);
540 if (state == TS_TREE_STATE_NONE) {
541 return TS_TREE_STATE_NONE;
542 }
543 uint16_t symbol = ts_node_grammar_symbol(self);
544 return ts_language_next_state(language, state, symbol);
545 }
546
547 TSNode ts_node_parent(TSNode self) {
548 TSNode node = ts_tree_root_node(self.tree);
549 if (node.id == self.id) return ts_node__null();
550
551 while (true) {
552 TSNode next_node = ts_node_child_with_descendant(node, self);
553 if (next_node.id == self.id || ts_node_is_null(next_node)) break;
554 node = next_node;
555 }
556
557 return node;
558 }
559
560 TSNode ts_node_child_with_descendant(TSNode self, TSNode descendant) {
561 uint32_t start_byte = ts_node_start_byte(descendant);
562 uint32_t end_byte = ts_node_end_byte(descendant);
563 bool is_empty = start_byte == end_byte;
564
565 do {
566 NodeChildIterator iter = ts_node_iterate_children(&self);
567 do {
568 if (
569 !ts_node_child_iterator_next(&iter, &self)
570 || ts_node_start_byte(self) > start_byte
571 ) {
572 return ts_node__null();
573 }
574 if (self.id == descendant.id) {
575 return self;
576 }
577
578 // If the descendant is empty, and the end byte is within `self`,
579 // we check whether `self` contains it or not.
580 if (is_empty && iter.position.bytes >= end_byte && ts_node_child_count(self) > 0) {
581 TSNode child = ts_node_child_with_descendant(self, descendant);
582 // If the child is not null, return self if it's relevant, else return the child
583 if (!ts_node_is_null(child)) {
584 return ts_node__is_relevant(self, true) ? self : child;
585 }
586 }
587 } while ((is_empty ? iter.position.bytes <= end_byte : iter.position.bytes < end_byte) || ts_node_child_count(self) == 0);
588 } while (!ts_node__is_relevant(self, true));
589
590 return self;
591 }
592
593 TSNode ts_node_child(TSNode self, uint32_t child_index) {
594 return ts_node__child(self, child_index, true);
595 }
596
597 TSNode ts_node_named_child(TSNode self, uint32_t child_index) {
598 return ts_node__child(self, child_index, false);
599 }
600
601 TSNode ts_node_child_by_field_id(TSNode self, TSFieldId field_id) {
602 recur:
603 if (!field_id || ts_node_child_count(self) == 0) return ts_node__null();
604
605 const TSFieldMapEntry *field_map, *field_map_end;
606 ts_language_field_map(
607 self.tree->language,
608 ts_node__subtree(self).ptr->production_id,
609 &field_map,
610 &field_map_end
611 );
612 if (field_map == field_map_end) return ts_node__null();
613
614 // The field mappings are sorted by their field id. Scan all
615 // the mappings to find the ones for the given field id.
616 while (field_map->field_id < field_id) {
617 field_map++;
618 if (field_map == field_map_end) return ts_node__null();
619 }
620 while (field_map_end[-1].field_id > field_id) {
621 field_map_end--;
622 if (field_map == field_map_end) return ts_node__null();
623 }
624
625 TSNode child;
626 NodeChildIterator iterator = ts_node_iterate_children(&self);
627 while (ts_node_child_iterator_next(&iterator, &child)) {
628 if (!ts_subtree_extra(ts_node__subtree(child))) {
629 uint32_t index = iterator.structural_child_index - 1;
630 if (index < field_map->child_index) continue;
631
632 // Hidden nodes' fields are "inherited" by their visible parent.
633 if (field_map->inherited) {
634
635 // If this is the *last* possible child node for this field,
636 // then perform a tail call to avoid recursion.
637 if (field_map + 1 == field_map_end) {
638 self = child;
639 goto recur;
640 }
641
642 // Otherwise, descend into this child, but if it doesn't contain
643 // the field, continue searching subsequent children.
644 else {
645 TSNode result = ts_node_child_by_field_id(child, field_id);
646 if (result.id) return result;
647 field_map++;
648 if (field_map == field_map_end) return ts_node__null();
649 }
650 }
651
652 else if (ts_node__is_relevant(child, true)) {
653 return child;
654 }
655
656 // If the field refers to a hidden node with visible children,
657 // return the first visible child.
658 else if (ts_node_child_count(child) > 0 ) {
659 return ts_node_child(child, 0);
660 }
661
662 // Otherwise, continue searching subsequent children.
663 else {
664 field_map++;
665 if (field_map == field_map_end) return ts_node__null();
666 }
667 }
668 }
669
670 return ts_node__null();
671 }
672
673 static inline const char *ts_node__field_name_from_language(TSNode self, uint32_t structural_child_index) {
674 const TSFieldMapEntry *field_map, *field_map_end;
675 ts_language_field_map(
676 self.tree->language,
677 ts_node__subtree(self).ptr->production_id,
678 &field_map,
679 &field_map_end
680 );
681 for (; field_map != field_map_end; field_map++) {
682 if (!field_map->inherited && field_map->child_index == structural_child_index) {
683 return self.tree->language->field_names[field_map->field_id];
684 }
685 }
686 return NULL;
687 }
688
689 const char *ts_node_field_name_for_child(TSNode self, uint32_t child_index) {
690 TSNode result = self;
691 bool did_descend = true;
692 const char *inherited_field_name = NULL;
693
694 while (did_descend) {
695 did_descend = false;
696
697 TSNode child;
698 uint32_t index = 0;
699 NodeChildIterator iterator = ts_node_iterate_children(&result);
700 while (ts_node_child_iterator_next(&iterator, &child)) {
701 if (ts_node__is_relevant(child, true)) {
702 if (index == child_index) {
703 if (ts_node_is_extra(child)) {
704 return NULL;
705 }
706 const char *field_name = ts_node__field_name_from_language(result, iterator.structural_child_index - 1);
707 if (field_name) return field_name;
708 return inherited_field_name;
709 }
710 index++;
711 } else {
712 uint32_t grandchild_index = child_index - index;
713 uint32_t grandchild_count = ts_node__relevant_child_count(child, true);
714 if (grandchild_index < grandchild_count) {
715 const char *field_name = ts_node__field_name_from_language(result, iterator.structural_child_index - 1);
716 if (field_name) inherited_field_name = field_name;
717
718 did_descend = true;
719 result = child;
720 child_index = grandchild_index;
721 break;
722 }
723 index += grandchild_count;
724 }
725 }
726 }
727
728 return NULL;
729 }
730
731 const char *ts_node_field_name_for_named_child(TSNode self, uint32_t named_child_index) {
732 TSNode result = self;
733 bool did_descend = true;
734 const char *inherited_field_name = NULL;
735
736 while (did_descend) {
737 did_descend = false;
738
739 TSNode child;
740 uint32_t index = 0;
741 NodeChildIterator iterator = ts_node_iterate_children(&result);
742 while (ts_node_child_iterator_next(&iterator, &child)) {
743 if (ts_node__is_relevant(child, false)) {
744 if (index == named_child_index) {
745 if (ts_node_is_extra(child)) {
746 return NULL;
747 }
748 const char *field_name = ts_node__field_name_from_language(result, iterator.structural_child_index - 1);
749 if (field_name) return field_name;
750 return inherited_field_name;
751 }
752 index++;
753 } else {
754 uint32_t named_grandchild_index = named_child_index - index;
755 uint32_t grandchild_count = ts_node__relevant_child_count(child, false);
756 if (named_grandchild_index < grandchild_count) {
757 const char *field_name = ts_node__field_name_from_language(result, iterator.structural_child_index - 1);
758 if (field_name) inherited_field_name = field_name;
759
760 did_descend = true;
761 result = child;
762 named_child_index = named_grandchild_index;
763 break;
764 }
765 index += grandchild_count;
766 }
767 }
768 }
769
770 return NULL;
771 }
772
773 TSNode ts_node_child_by_field_name(
774 TSNode self,
775 const char *name,
776 uint32_t name_length
777 ) {
778 TSFieldId field_id = ts_language_field_id_for_name(
779 self.tree->language,
780 name,
781 name_length
782 );
783 return ts_node_child_by_field_id(self, field_id);
784 }
785
786 uint32_t ts_node_child_count(TSNode self) {
787 Subtree tree = ts_node__subtree(self);
788 if (ts_subtree_child_count(tree) > 0) {
789 return tree.ptr->visible_child_count;
790 } else {
791 return 0;
792 }
793 }
794
795 uint32_t ts_node_named_child_count(TSNode self) {
796 Subtree tree = ts_node__subtree(self);
797 if (ts_subtree_child_count(tree) > 0) {
798 return tree.ptr->named_child_count;
799 } else {
800 return 0;
801 }
802 }
803
804 TSNode ts_node_next_sibling(TSNode self) {
805 return ts_node__next_sibling(self, true);
806 }
807
808 TSNode ts_node_next_named_sibling(TSNode self) {
809 return ts_node__next_sibling(self, false);
810 }
811
812 TSNode ts_node_prev_sibling(TSNode self) {
813 return ts_node__prev_sibling(self, true);
814 }
815
816 TSNode ts_node_prev_named_sibling(TSNode self) {
817 return ts_node__prev_sibling(self, false);
818 }
819
820 TSNode ts_node_first_child_for_byte(TSNode self, uint32_t byte) {
821 return ts_node__first_child_for_byte(self, byte, true);
822 }
823
824 TSNode ts_node_first_named_child_for_byte(TSNode self, uint32_t byte) {
825 return ts_node__first_child_for_byte(self, byte, false);
826 }
827
828 TSNode ts_node_descendant_for_byte_range(
829 TSNode self,
830 uint32_t start,
831 uint32_t end
832 ) {
833 return ts_node__descendant_for_byte_range(self, start, end, true);
834 }
835
836 TSNode ts_node_named_descendant_for_byte_range(
837 TSNode self,
838 uint32_t start,
839 uint32_t end
840 ) {
841 return ts_node__descendant_for_byte_range(self, start, end, false);
842 }
843
844 TSNode ts_node_descendant_for_point_range(
845 TSNode self,
846 TSPoint start,
847 TSPoint end
848 ) {
849 return ts_node__descendant_for_point_range(self, start, end, true);
850 }
851
852 TSNode ts_node_named_descendant_for_point_range(
853 TSNode self,
854 TSPoint start,
855 TSPoint end
856 ) {
857 return ts_node__descendant_for_point_range(self, start, end, false);
858 }
859
860 void ts_node_edit(TSNode *self, const TSInputEdit *edit) {
861 uint32_t start_byte = ts_node_start_byte(*self);
862 TSPoint start_point = ts_node_start_point(*self);
863
864 ts_point_edit(&start_point, &start_byte, edit);
865
866 self->context[0] = start_byte;
867 self->context[1] = start_point.row;
868 self->context[2] = start_point.column;
869 }