-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
234 lines (191 loc) · 6.06 KB
/
tree.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
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
package sitter
// #include "sitter.h"
import "C"
import (
"fmt"
"os"
"runtime"
"sync"
"unsafe"
)
// BaseTree is needed as we use cache for nodes on normal tree object.
// It prevent run of SetFinalizer as it introduces cycle we can workaround it using
// separate object for details see: https://github.com/golang/go/issues/7358#issuecomment-66091558
type BaseTree struct {
c *C.TSTree
once sync.Once
}
// Tree represents the syntax tree of an entire source code file
// Note: Tree instances are not thread safe;
// you must copy a tree if you want to use it on multiple threads simultaneously.
type Tree struct {
*BaseTree
// p is a pointer to a Parser that produced the Tree. Only used to keep Parser alive.
// Otherwise Parser may be GC'ed (and deleted by the finalizer) while some Tree
// objects are still in use.
p *Parser
cache map[uintptr]*Node
}
// Point represents one location in the input.
type Point struct {
Row uint32
Column uint32
}
// Range represents a range in the input.
type Range struct {
StartPoint Point
EndPoint Point
StartByte uint32
EndByte uint32
}
// InputEdit represents one edit in the input.
type InputEdit struct {
StartIndex uint32
OldEndIndex uint32
NewEndIndex uint32
StartPoint Point
OldEndPoint Point
NewEndPoint Point
}
func (i InputEdit) c() *C.TSInputEdit {
return &C.TSInputEdit{
start_byte: C.uint(i.StartIndex),
old_end_byte: C.uint(i.OldEndIndex),
new_end_byte: C.uint(i.NewEndIndex),
start_point: i.StartPoint.c(),
old_end_point: i.OldEndPoint.c(),
new_end_point: i.NewEndPoint.c(),
}
}
func (p Point) c() C.TSPoint {
return C.TSPoint{row: C.uint32_t(p.Row), column: C.uint32_t(p.Column)}
}
func mkPoint(p C.TSPoint) Point {
return Point{Row: uint32(p.row), Column: uint32(p.column)}
}
func (r Range) c() C.TSRange {
return C.TSRange{
start_point: r.StartPoint.c(),
end_point: r.EndPoint.c(),
start_byte: C.uint32_t(r.StartByte),
end_byte: C.uint32_t(r.EndByte),
}
}
func mkRange(r C.TSRange) Range {
return Range{
StartPoint: mkPoint(r.start_point),
EndPoint: mkPoint(r.end_point),
StartByte: uint32(r.start_byte),
EndByte: uint32(r.end_byte),
}
}
func mkRanges(p *C.TSRange, count C.uint32_t) (out []Range) {
out = make([]Range, count)
for i, r := range unsafe.Slice(p, int(count)) {
out[i] = mkRange(r)
}
return
}
// newTree creates a new tree object from a C pointer. The function will set a finalizer for the object,
// thus no free is needed for it.
func (p *Parser) newTree(c *C.TSTree) *Tree {
base := &BaseTree{c: c}
runtime.SetFinalizer(base, (*BaseTree).close)
return &Tree{p: p, BaseTree: base, cache: map[uintptr]*Node{}}
}
// Copy creates a shallow copy of the syntax tree. This is very fast.
//
// You need to copy a syntax tree in order to use it on more than one thread at
// a time, as syntax trees are not thread safe.
func (t *Tree) Copy() *Tree {
return t.p.newTree(C.ts_tree_copy(t.c))
}
// close should be called to ensure that all the memory used by the tree is freed.
//
// As the constructor in go-tree-sitter would set this func call through runtime.SetFinalizer,
// parser.close() will be called by Go's garbage collector and users need not call this manually.
func (t *BaseTree) close() {
t.once.Do(func() { C.ts_tree_delete(t.c) })
}
// RootNode returns root node of the syntax tree.
func (t *Tree) RootNode() *Node {
nn := C.ts_tree_root_node(t.c)
return t.cachedNode(nn)
}
// RootNodeWithOffset returns the root node of the syntax tree, but with its position
// shifted forward by the given offset.
func (t *Tree) RootNodeWithOffset(ofs uint32, extent Point) *Node {
nn := C.ts_tree_root_node_with_offset(t.c, C.uint32_t(ofs), extent.c())
return t.cachedNode(nn)
}
// Language returns the language that was used to parse the syntax tree.
func (t *Tree) Language() *Language {
return NewLanguage(unsafe.Pointer(C.ts_tree_language(t.c)))
}
// IncludedRanges returns the array of included ranges that was used to parse the syntax tree.
//
// The returned pointer must be freed by the caller.
func (t *Tree) IncludedRanges() []Range {
count := C.uint(0)
p := C.ts_tree_included_ranges(t.c, &count)
defer freeTSRangeArray(p, count)
return mkRanges(p, count)
}
// Edit the syntax tree to keep it in sync with source code that has been edited.
//
// You MUST describe the edit both in terms of byte offsets and in terms of
// (row, column) coordinates.
func (t *Tree) Edit(i InputEdit) {
C.ts_tree_edit(t.c, i.c())
}
// GetChangedRanges compares an old edited syntax tree to a new syntax tree
// representing the same document, returning an array of ranges whose
// syntactic structure has changed.
//
// For this to work correctly, the old syntax tree must have been edited such
// that its ranges match up to the new tree. Generally, you'll want to call
// this function right after calling one of the [`ts_parser_parse`] functions.
// You need to pass the old tree that was passed to parse, as well as the new
// tree that was returned from that function.
//
// The returned array is allocated using `malloc` and the caller is responsible
// for freeing it using `free`. The length of the array will be written to the
// given `length` pointer.
func (t *Tree) GetChangedRanges(other *Tree) []Range {
count := C.uint(0)
p := C.ts_tree_get_changed_ranges(t.c, other.c, &count)
defer freeTSRangeArray(p, count)
return mkRanges(p, count)
}
// PrintDotGraph writes a DOT graph describing the syntax tree to the given file.
func (t *Tree) PrintDotGraph(name string) (err error) {
f, err := os.Create(name)
if err != nil {
return
}
C.ts_tree_print_dot_graph(t.c, C.int(f.Fd()))
if err = f.Close(); err != nil {
err = fmt.Errorf("cannot save dot file: %w", err)
}
return
}
func (t *Tree) cachedNode(ptr C.TSNode) (n *Node) {
if ptr.id == nil {
return
}
var ok bool
p := uintptr(ptr.id)
if n, ok = t.cache[p]; ok {
return
}
n = &Node{c: ptr, t: t}
t.cache[p] = n
return
}
func freeTSRangeArray(p *C.struct_TSRange, count C.uint) {
pp := unsafe.Pointer(p)
for ; count > 0; count-- {
C.free(pp)
pp = unsafe.Add(pp, C.sizeof_struct_TSRange)
}
}