Repository navigation
Expand file tree
/
Copy pathnode.go
More file actions
1202 lines (1068 loc) · 30.1 KB
/
Copy pathnode.go
File metadata and controls
1202 lines (1068 loc) · 30.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package transit
import "iter"
// This file ports lib/src/node.c, and the type TSNode of
// lib/include/tree_sitter/api.h.
//
// A method whose C function can return a null node returns the node and a
// bool, which is false for the null node (D56). The zero Node is the null
// node. ts_node_is_null has no exported form, and it is the method isNull,
// because a lookup returns a bool with the node.
//
// Two nodes compare with ==, which also compares the position of the node.
// Equal is ts_node_eq, which compares only the tree and the node, so a node
// that Node.Edit moved is Equal to the node before the edit, and not == to
// it (D68).
//
// The exported API counts with int (D25). A function of this file that
// needs the width of C converts the int back to a uint32.
//
// The methods at the end of the file come from the Rust binding, and they
// port no C function.
// Node is a node of a tree. It is a small value, and two nodes compare with
// ==, which also compares their positions. Equal compares them as C does
// (D68). The zero Node is no node.
//
// Node is TSNode. context holds the start byte, the start row, the start
// column and the alias of the node, as in C. id is the address of the
// subtree of the node: the root of the tree, or a child in the children of
// its parent.
type Node struct {
context [4]uint32
id *subtree
tree *Tree
}
// nodeChildIterator is NodeChildIterator.
type nodeChildIterator struct {
parent subtree
tree *Tree
position length
childIndex uint32
structuralChildIndex uint32
aliasSequence []uint16
}
// TSNode - constructors
// makeNode is ts_node_new. The name newNode is ts_subtree_new_node.
func makeNode(tree *Tree, subtree *subtree, position length, alias Symbol) Node {
return Node{
context: [4]uint32{position.bytes, position.extent.row, position.extent.column, uint32(alias)},
id: subtree,
tree: tree,
}
}
// nullNode is ts_node__null.
func nullNode() Node {
return makeNode(nil, nil, lengthZero(), 0)
}
// TSNode - accessors
// StartByte returns the byte offset where the node starts.
//
// StartByte is ts_node_start_byte.
func (n Node) StartByte() int {
return int(n.context[0])
}
// StartPoint returns the point where the node starts.
//
// StartPoint is ts_node_start_point.
func (n Node) StartPoint() Point {
return point{n.context[1], n.context[2]}.public()
}
// alias is ts_node__alias.
func (n Node) alias() Symbol {
return Symbol(n.context[3])
}
// subtree is ts_node__subtree.
func (n Node) subtree() subtree {
return *n.id
}
// NodeChildIterator
// iterateChildren is ts_node_iterate_children.
func (n Node) iterateChildren() nodeChildIterator {
subtree := n.subtree()
if subtree.childCount() == 0 {
return nodeChildIterator{tree: n.tree, position: lengthZero()}
}
aliasSequence := n.tree.language.aliasSequence(uint32(subtree.ptr.productionID))
return nodeChildIterator{
tree: n.tree,
parent: subtree,
position: length{uint32(n.StartByte()), n.StartPoint().internal()},
childIndex: 0,
structuralChildIndex: 0,
aliasSequence: aliasSequence,
}
}
// done is ts_node_child_iterator_done.
func (it *nodeChildIterator) done() bool {
return it.childIndex == it.parent.childCount()
}
// next is ts_node_child_iterator_next. The C function writes the child to an
// out parameter, and the Go function returns it.
func (it *nodeChildIterator) next() (Node, bool) {
if it.parent.ptr == nil || it.done() {
return Node{}, false
}
child := &it.parent.ptr.children[it.childIndex]
aliasSymbol := Symbol(0)
if !child.extra() {
if it.aliasSequence != nil {
aliasSymbol = Symbol(it.aliasSequence[it.structuralChildIndex])
}
it.structuralChildIndex++
}
if it.childIndex > 0 {
it.position = it.position.add(child.padding())
}
result := makeNode(
it.tree,
child,
it.position,
aliasSymbol,
)
it.position = it.position.add(child.size())
it.childIndex++
return result, true
}
// TSNode - private
// isRelevant is ts_node__is_relevant.
func (n Node) isRelevant(includeAnonymous bool) bool {
tree := n.subtree()
if includeAnonymous {
return tree.visible() || n.alias() != 0
}
alias := n.alias()
if alias != 0 {
return n.tree.language.symbolMetadata(alias).Named
}
return tree.visible() && tree.named()
}
// relevantChildCount is ts_node__relevant_child_count.
func (n Node) relevantChildCount(includeAnonymous bool) uint32 {
tree := n.subtree()
if tree.childCount() > 0 {
if includeAnonymous {
return tree.ptr.visibleChildCount
}
return tree.ptr.namedChildCount
}
return 0
}
// child is ts_node__child.
func (n Node) child(childIndex uint32, includeAnonymous bool) Node {
result := n
didDescend := true
for didDescend {
didDescend = false
index := uint32(0)
iterator := result.iterateChildren()
for {
child, ok := iterator.next()
if !ok {
break
}
if child.isRelevant(includeAnonymous) {
if index == childIndex {
return child
}
index++
} else {
grandchildIndex := childIndex - index
grandchildCount := child.relevantChildCount(includeAnonymous)
if grandchildIndex < grandchildCount {
didDescend = true
result = child
childIndex = grandchildIndex
break
}
index += grandchildCount
}
}
}
return nullNode()
}
// hasTrailingEmptyDescendant is ts_subtree_has_trailing_empty_descendant.
func (s subtree) hasTrailingEmptyDescendant(other subtree) bool {
for i := s.childCount() - 1; i+1 > 0; i-- {
child := s.ptr.children[i]
if child.totalBytes() > 0 {
break
}
if child.ptr == other.ptr || child.hasTrailingEmptyDescendant(other) {
return true
}
}
return false
}
// prevSibling is ts_node__prev_sibling.
func (n Node) prevSibling(includeAnonymous bool) Node {
self := n
selfSubtree := self.subtree()
selfIsEmpty := selfSubtree.totalBytes() == 0
targetEndByte := uint32(self.EndByte())
node, _ := self.Parent()
earlierNode := nullNode()
earlierNodeIsRelevant := false
for !node.isNull() {
earlierChild := nullNode()
earlierChildIsRelevant := false
foundChildContainingTarget := false
var child Node
iterator := node.iterateChildren()
for {
next, ok := iterator.next()
if !ok {
break
}
child = next
if child.id == self.id {
break
}
if iterator.position.bytes > targetEndByte {
foundChildContainingTarget = true
break
}
if iterator.position.bytes == targetEndByte &&
(!selfIsEmpty ||
child.subtree().hasTrailingEmptyDescendant(selfSubtree)) {
foundChildContainingTarget = true
break
}
if child.isRelevant(includeAnonymous) {
earlierChild = child
earlierChildIsRelevant = true
} else if child.relevantChildCount(includeAnonymous) > 0 {
earlierChild = child
earlierChildIsRelevant = false
}
}
switch {
case foundChildContainingTarget:
if !earlierChild.isNull() {
earlierNode = earlierChild
earlierNodeIsRelevant = earlierChildIsRelevant
}
node = child
case earlierChildIsRelevant:
return earlierChild
case !earlierChild.isNull():
node = earlierChild
case earlierNodeIsRelevant:
return earlierNode
default:
node = earlierNode
earlierNode = nullNode()
earlierNodeIsRelevant = false
}
}
return nullNode()
}
// nextSibling is ts_node__next_sibling.
func (n Node) nextSibling(includeAnonymous bool) Node {
self := n
targetEndByte := uint32(self.EndByte())
node, _ := self.Parent()
laterNode := nullNode()
laterNodeIsRelevant := false
for !node.isNull() {
laterChild := nullNode()
laterChildIsRelevant := false
childContainingTarget := nullNode()
iterator := node.iterateChildren()
for {
child, ok := iterator.next()
if !ok {
break
}
if iterator.position.bytes <= targetEndByte {
continue
}
startByte := uint32(self.StartByte())
childStartByte := uint32(child.StartByte())
isEmpty := startByte == targetEndByte
var containsTarget bool
if isEmpty {
containsTarget = childStartByte < startByte
} else {
containsTarget = childStartByte <= startByte
}
switch {
case containsTarget:
if child.subtree().ptr != self.subtree().ptr {
childContainingTarget = child
}
continue
case child.isRelevant(includeAnonymous):
laterChild = child
laterChildIsRelevant = true
case child.relevantChildCount(includeAnonymous) > 0:
laterChild = child
laterChildIsRelevant = false
default:
continue
}
break
}
switch {
case !childContainingTarget.isNull():
if !laterChild.isNull() {
laterNode = laterChild
laterNodeIsRelevant = laterChildIsRelevant
}
node = childContainingTarget
case laterChildIsRelevant:
return laterChild
case !laterChild.isNull():
node = laterChild
case laterNodeIsRelevant:
return laterNode
default:
node = laterNode
}
}
return nullNode()
}
// firstChildForByte is ts_node__first_child_for_byte. The goto of C is a
// loop.
//
// The C function compares the index of the iterator in the parent with the
// child count of the child, and not with the child count of the parent. The
// Go function does the same.
func (n Node) firstChildForByte(goal uint32, includeAnonymous bool) Node {
node := n
didDescend := true
var lastIterator nodeChildIterator
hasLastIterator := false
for didDescend {
didDescend = false
iterator := node.iterateChildren()
for {
for {
child, ok := iterator.next()
if !ok {
break
}
if uint32(child.EndByte()) <= goal {
continue
}
if child.isRelevant(includeAnonymous) {
return child
}
if child.ChildCount() > 0 {
if iterator.childIndex < child.subtree().childCount() {
lastIterator = iterator
hasLastIterator = true
}
didDescend = true
node = child
break
}
}
if !didDescend && hasLastIterator {
iterator = lastIterator
hasLastIterator = false
continue
}
break
}
}
return nullNode()
}
// descendantForByteRange is ts_node__descendant_for_byte_range.
func (n Node) descendantForByteRange(rangeStart, rangeEnd uint32, includeAnonymous bool) Node {
if rangeStart > rangeEnd {
return nullNode()
}
node := n
lastVisibleNode := n
didDescend := true
for didDescend {
didDescend = false
iterator := node.iterateChildren()
for {
child, ok := iterator.next()
if !ok {
break
}
nodeEnd := iterator.position.bytes
// The end of this node must extend far enough forward to touch
// the end of the range
if nodeEnd < rangeEnd {
continue
}
// ...and exceed the start of the range, unless the node itself is
// empty, in which case it must at least be equal to the start of the range.
isEmpty := uint32(child.StartByte()) == nodeEnd
if isEmpty && nodeEnd < rangeStart || !isEmpty && nodeEnd <= rangeStart {
continue
}
// The start of this node must extend far enough backward to
// touch the start of the range.
if rangeStart < uint32(child.StartByte()) {
break
}
node = child
if node.isRelevant(includeAnonymous) {
lastVisibleNode = node
}
didDescend = true
break
}
}
return lastVisibleNode
}
// descendantForPointRange is ts_node__descendant_for_point_range.
func (n Node) descendantForPointRange(rangeStart, rangeEnd point, includeAnonymous bool) Node {
if rangeStart.gt(rangeEnd) {
return nullNode()
}
node := n
lastVisibleNode := n
didDescend := true
for didDescend {
didDescend = false
iterator := node.iterateChildren()
for {
child, ok := iterator.next()
if !ok {
break
}
nodeEnd := iterator.position.extent
// The end of this node must extend far enough forward to touch
// the end of the range
if nodeEnd.lt(rangeEnd) {
continue
}
// ...and exceed the start of the range, unless the node itself is
// empty, in which case it must at least be equal to the start of the range.
isEmpty := child.StartPoint().internal().eq(nodeEnd)
if isEmpty && nodeEnd.lt(rangeStart) || !isEmpty && nodeEnd.lte(rangeStart) {
continue
}
// The start of this node must extend far enough backward to
// touch the start of the range.
if rangeStart.lt(child.StartPoint().internal()) {
break
}
node = child
if node.isRelevant(includeAnonymous) {
lastVisibleNode = node
}
didDescend = true
break
}
}
return lastVisibleNode
}
// TSNode - public
// EndByte returns the byte offset where the node ends.
//
// EndByte is ts_node_end_byte.
func (n Node) EndByte() int {
return int(uint32(n.StartByte()) + n.subtree().size().bytes)
}
// EndPoint returns the point where the node ends.
//
// EndPoint is ts_node_end_point.
func (n Node) EndPoint() Point {
return n.StartPoint().internal().add(n.subtree().size().extent).public()
}
// KindID returns the symbol of the node, or of its alias.
//
// KindID is ts_node_symbol.
func (n Node) KindID() Symbol {
symbol := n.alias()
if symbol == 0 {
symbol = n.subtree().symbol()
}
return n.tree.language.publicSymbol(symbol)
}
// Kind returns the name of the symbol of the node, or of its alias.
//
// Kind is ts_node_type.
func (n Node) Kind() string {
symbol := n.alias()
if symbol == 0 {
symbol = n.subtree().symbol()
}
return n.tree.language.SymbolName(symbol)
}
// Language returns the language of the tree of the node.
//
// Language is ts_node_language.
func (n Node) Language() *Language {
return n.tree.Language()
}
// GrammarID returns the symbol of the node in the grammar, before any alias.
//
// GrammarID is ts_node_grammar_symbol.
func (n Node) GrammarID() Symbol {
return n.subtree().symbol()
}
// GrammarKind returns the name of the symbol of the node in the grammar,
// before any alias.
//
// GrammarKind is ts_node_grammar_type.
func (n Node) GrammarKind() string {
symbol := n.subtree().symbol()
return n.tree.language.SymbolName(symbol)
}
// String returns the node and its named descendants as an S-expression.
//
// String is ts_node_string.
func (n Node) String() string {
aliasSymbol := n.alias()
return n.subtree().string(
aliasSymbol,
n.tree.language.symbolMetadata(aliasSymbol).Visible,
n.tree.language,
false,
)
}
// Equal reports whether n and other are the same node of the same tree. It
// does not compare the positions of the nodes, so a node that Edit moved is
// Equal to the node before the edit, as in C, and == tells them apart.
//
// Equal is ts_node_eq.
func (n Node) Equal(other Node) bool {
return n.tree == other.tree && n.id == other.id
}
// isNull is ts_node_is_null.
func (n Node) isNull() bool {
return n.id == nil
}
// IsExtra reports whether the node is an extra, such as a comment.
//
// IsExtra is ts_node_is_extra.
func (n Node) IsExtra() bool {
return n.subtree().extra()
}
// IsNamed reports whether the node is named, or has a named alias.
//
// IsNamed is ts_node_is_named.
func (n Node) IsNamed() bool {
alias := n.alias()
if alias != 0 {
return n.tree.language.symbolMetadata(alias).Named
}
return n.subtree().named()
}
// IsMissing reports whether the parser inserted the node to recover from an
// error.
//
// IsMissing is ts_node_is_missing.
func (n Node) IsMissing() bool {
return n.subtree().missing()
}
// HasChanges reports whether an edit changed the node.
//
// HasChanges is ts_node_has_changes.
func (n Node) HasChanges() bool {
return n.subtree().hasChanges()
}
// HasError reports whether the node is an error or holds one.
//
// HasError is ts_node_has_error.
func (n Node) HasError() bool {
return n.subtree().errorCost() > 0
}
// IsError reports whether the node is an ERROR node.
//
// IsError is ts_node_is_error.
func (n Node) IsError() bool {
symbol := n.KindID()
return symbol == builtinSymError
}
// DescendantCount returns the number of the node and its descendants.
//
// DescendantCount is ts_node_descendant_count.
func (n Node) DescendantCount() int {
return int(n.subtree().visibleDescendantCount() + 1)
}
// ParseState returns the parse state of the node.
//
// ParseState is ts_node_parse_state.
func (n Node) ParseState() StateID {
return n.subtree().parseState()
}
// NextParseState returns the parse state after the node.
//
// NextParseState is ts_node_next_parse_state.
func (n Node) NextParseState() StateID {
language := n.tree.language
state := n.ParseState()
if state == tsTreeStateNone {
return tsTreeStateNone
}
symbol := n.GrammarID()
return language.NextState(state, symbol)
}
// Parent returns the parent of the node. It returns false for the root.
//
// Parent is ts_node_parent.
func (n Node) Parent() (Node, bool) {
self := n
node := self.tree.RootNode()
if node.id == self.id {
return nullNode(), false
}
for {
nextNode, _ := node.ChildWithDescendant(self)
if nextNode.id == self.id || nextNode.isNull() {
break
}
node = nextNode
}
return node, !node.isNull()
}
// ChildWithDescendant returns the child of the node that holds descendant.
// The result can be descendant itself.
//
// ChildWithDescendant is ts_node_child_with_descendant.
func (n Node) ChildWithDescendant(descendant Node) (Node, bool) {
self := n
startByte := uint32(descendant.StartByte())
endByte := uint32(descendant.EndByte())
isEmpty := startByte == endByte
for {
iterator := self.iterateChildren()
for {
child, ok := iterator.next()
if !ok || uint32(child.StartByte()) > startByte {
return nullNode(), false
}
self = child
if self.id == descendant.id {
return self, true
}
// If the descendant is empty, and the end byte is within `self`,
// we check whether `self` contains it or not.
if isEmpty && iterator.position.bytes >= endByte && self.ChildCount() > 0 {
child, ok := self.ChildWithDescendant(descendant)
// If the child is not null, return self if it's relevant, else return the child
if ok {
if self.isRelevant(true) {
return self, true
}
return child, true
}
}
var inRange bool
if isEmpty {
inRange = iterator.position.bytes <= endByte
} else {
inRange = iterator.position.bytes < endByte
}
if !inRange && self.ChildCount() != 0 {
break
}
}
if self.isRelevant(true) {
break
}
}
return self, true
}
// Child returns the child of the node at an index, where the index counts
// the named and the anonymous children.
//
// Child is ts_node_child.
func (n Node) Child(i int) (Node, bool) {
result := n.child(uint32(i), true)
return result, !result.isNull()
}
// NamedChild returns the named child of the node at an index, where the
// index counts only the named children.
//
// NamedChild is ts_node_named_child.
func (n Node) NamedChild(i int) (Node, bool) {
result := n.child(uint32(i), false)
return result, !result.isNull()
}
// ChildByFieldID returns the first child of the node with a field.
//
// ChildByFieldID is ts_node_child_by_field_id. The goto of C is a labeled
// continue.
func (n Node) ChildByFieldID(fieldID FieldID) (Node, bool) {
self := n
recur:
for {
if fieldID == 0 || self.ChildCount() == 0 {
return nullNode(), false
}
fieldMap := self.tree.language.fieldMap(uint32(self.subtree().ptr.productionID))
if len(fieldMap) == 0 {
return nullNode(), false
}
// The field mappings are sorted by their field id. Scan all
// the mappings to find the ones for the given field id.
for FieldID(fieldMap[0].FieldID) < fieldID {
fieldMap = fieldMap[1:]
if len(fieldMap) == 0 {
return nullNode(), false
}
}
for FieldID(fieldMap[len(fieldMap)-1].FieldID) > fieldID {
fieldMap = fieldMap[:len(fieldMap)-1]
if len(fieldMap) == 0 {
return nullNode(), false
}
}
iterator := self.iterateChildren()
for {
child, ok := iterator.next()
if !ok {
break
}
if !child.subtree().extra() {
index := iterator.structuralChildIndex - 1
if index < uint32(fieldMap[0].ChildIndex) {
continue
}
// Hidden nodes' fields are "inherited" by their visible parent.
switch {
case fieldMap[0].Inherited:
// If this is the *last* possible child node for this field,
// then perform a tail call to avoid recursion.
if len(fieldMap) == 1 {
self = child
continue recur
}
// Otherwise, descend into this child, but if it doesn't contain
// the field, continue searching subsequent children.
result, ok := child.ChildByFieldID(fieldID)
if ok {
return result, true
}
fieldMap = fieldMap[1:]
if len(fieldMap) == 0 {
return nullNode(), false
}
case child.isRelevant(true):
return child, true
// If the field refers to a hidden node with visible children,
// return the first visible child.
case child.ChildCount() > 0:
return child.Child(0)
// Otherwise, continue searching subsequent children.
default:
fieldMap = fieldMap[1:]
if len(fieldMap) == 0 {
return nullNode(), false
}
}
}
}
return nullNode(), false
}
}
// fieldNameFromLanguage is ts_node__field_name_from_language. It returns ""
// where the C function returns NULL.
func (n Node) fieldNameFromLanguage(structuralChildIndex uint32) string {
fieldMap := n.tree.language.fieldMap(uint32(n.subtree().ptr.productionID))
for _, m := range fieldMap {
if !m.Inherited && uint32(m.ChildIndex) == structuralChildIndex {
return n.tree.language.tables.FieldNames[m.FieldID]
}
}
return ""
}
// FieldNameForChild returns the field name of the child of the node at an
// index, or "" when the child has no field.
//
// FieldNameForChild is ts_node_field_name_for_child.
func (n Node) FieldNameForChild(i int) string {
result := n
childIndex := uint32(i)
didDescend := true
inheritedFieldName := ""
for didDescend {
didDescend = false
index := uint32(0)
iterator := result.iterateChildren()
for {
child, ok := iterator.next()
if !ok {
break
}
if child.isRelevant(true) {
if index == childIndex {
if child.IsExtra() {
return ""
}
fieldName := result.fieldNameFromLanguage(iterator.structuralChildIndex - 1)
if fieldName != "" {
return fieldName
}
return inheritedFieldName
}
index++
} else {
grandchildIndex := childIndex - index
grandchildCount := child.relevantChildCount(true)
if grandchildIndex < grandchildCount {
fieldName := result.fieldNameFromLanguage(iterator.structuralChildIndex - 1)
if fieldName != "" {
inheritedFieldName = fieldName
}
didDescend = true
result = child
childIndex = grandchildIndex
break
}
index += grandchildCount
}
}
}
return ""
}
// FieldNameForNamedChild returns the field name of the named child of the
// node at an index, or "" when the child has no field.
//
// FieldNameForNamedChild is ts_node_field_name_for_named_child.
func (n Node) FieldNameForNamedChild(i int) string {
result := n
namedChildIndex := uint32(i)
didDescend := true
inheritedFieldName := ""
for didDescend {
didDescend = false
index := uint32(0)
iterator := result.iterateChildren()
for {
child, ok := iterator.next()
if !ok {
break
}
if child.isRelevant(false) {
if index == namedChildIndex {
if child.IsExtra() {
return ""
}
fieldName := result.fieldNameFromLanguage(iterator.structuralChildIndex - 1)
if fieldName != "" {
return fieldName
}
return inheritedFieldName
}
index++
} else {
namedGrandchildIndex := namedChildIndex - index
grandchildCount := child.relevantChildCount(false)
if namedGrandchildIndex < grandchildCount {
fieldName := result.fieldNameFromLanguage(iterator.structuralChildIndex - 1)
if fieldName != "" {
inheritedFieldName = fieldName
}
didDescend = true
result = child
namedChildIndex = namedGrandchildIndex
break
}
index += grandchildCount
}
}
}
return ""
}
// ChildByFieldName returns the first child of the node with a field.
//
// ChildByFieldName is ts_node_child_by_field_name. The C function looks up
// the field with ts_language_field_id_for_name, which returns 0 for a name
// that the language does not have. The field 0 finds no child.
func (n Node) ChildByFieldName(name string) (Node, bool) {
fieldID, _ := n.tree.language.FieldForName(name)
return n.ChildByFieldID(fieldID)
}
// ChildCount returns the number of the named and the anonymous children of
// the node.
//
// ChildCount is ts_node_child_count.
func (n Node) ChildCount() int {
tree := n.subtree()
if tree.childCount() > 0 {
return int(tree.ptr.visibleChildCount)
}
return 0
}