-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
104 lines (91 loc) · 2.52 KB
/
node.go
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
package treesittergo
import (
"context"
"fmt"
)
type Node struct {
t Treesitter
n uint64
}
func newNode(t Treesitter, n uint64) Node {
return Node{t, n}
}
func (t Treesitter) allocateNode(ctx context.Context) (uint64, error) {
// allocate tsnode 24 bytes
nodePtr, err := t.malloc.Call(ctx, uint64(24))
if err != nil {
return 0, fmt.Errorf("allocating node: %w", err)
}
return nodePtr[0], nil
}
func (n Node) Kind(ctx context.Context) (string, error) {
nodeTypeStrPtr, err := n.t.nodeType.Call(ctx, n.n)
if err != nil {
return "", fmt.Errorf("getting node type: %w", err)
}
return n.t.readString(ctx, nodeTypeStrPtr[0])
}
func (n Node) Child(ctx context.Context, index uint64) (Node, error) {
nodePtr, err := n.t.allocateNode(ctx)
if err != nil {
return Node{}, err
}
_, err = n.t.nodeChild.Call(ctx, nodePtr, n.n, index)
if err != nil {
return Node{}, fmt.Errorf("getting node child: %w", err)
}
return newNode(n.t, nodePtr), nil
}
func (n Node) NamedChild(ctx context.Context, index uint64) (Node, error) {
nodePtr, err := n.t.allocateNode(ctx)
if err != nil {
return Node{}, err
}
_, err = n.t.nodeNamedChild.Call(ctx, nodePtr, n.n, index)
if err != nil {
return Node{}, fmt.Errorf("getting node child: %w", err)
}
return newNode(n.t, nodePtr), nil
}
func (n Node) IsError(ctx context.Context) (bool, error) {
res, err := n.t.nodeIsError.Call(ctx, n.n)
if err != nil {
return false, fmt.Errorf("getting node is error: %w", err)
}
return res[0] == 1, nil
}
func (n Node) StartByte(ctx context.Context) (uint64, error) {
res, err := n.t.nodeStartByte.Call(ctx, n.n)
if err != nil {
return 0, fmt.Errorf("getting node start byte: %w", err)
}
return res[0], nil
}
func (n Node) EndByte(ctx context.Context) (uint64, error) {
res, err := n.t.nodeEndByte.Call(ctx, n.n)
if err != nil {
return 0, fmt.Errorf("getting node end byte: %w", err)
}
return res[0], nil
}
func (n Node) ChildCount(ctx context.Context) (uint64, error) {
res, err := n.t.nodeChildCount.Call(ctx, n.n)
if err != nil {
return 0, fmt.Errorf("getting node child count: %w", err)
}
return res[0], nil
}
func (n Node) NamedChildCount(ctx context.Context) (uint64, error) {
res, err := n.t.nodeChildCount.Call(ctx, n.n)
if err != nil {
return 0, fmt.Errorf("getting node child count: %w", err)
}
return res[0], nil
}
func (n Node) String(ctx context.Context) (string, error) {
strPtr, err := n.t.nodeString.Call(ctx, n.n)
if err != nil {
return "", fmt.Errorf("getting node string: %w", err)
}
return n.t.readString(ctx, strPtr[0])
}