-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreesitter.go
111 lines (99 loc) · 2.51 KB
/
treesitter.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
package sitter
import (
"context"
"crypto/rand"
_ "embed"
"errors"
"fmt"
"io"
"io/fs"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)
const Version = "v0.24.7"
type Context struct {
ctx context.Context
mod api.Module
vfs fs.FS
out io.Writer
}
type TreeSitter struct {
ctx context.Context
mod api.Module
vfs fs.FS
out io.Writer
fns map[string]api.Function
lang map[string]api.Function
}
func New(vfs fs.FS, out io.Writer) (*TreeSitter, error) {
err := initialize()
if err != nil {
return nil, err
}
ts := new(TreeSitter)
ts.ctx = context.Background()
cfg := wazero.NewModuleConfig().
WithName("").
WithSysNanosleep().
WithSysNanotime().
WithSysWalltime().
WithRandSource(rand.Reader)
if vfs != nil {
cfg = cfg.WithFS(vfs)
}
if out != nil {
cfg = cfg.WithStdout(out).WithStderr(out)
}
ts.mod, err = instance.runtime.InstantiateModule(ts.ctx, instance.compiled, cfg)
if err != nil {
return nil, err
}
ts.fns = make(map[string]api.Function)
for _, name := range _functions {
if fn := ts.mod.ExportedFunction(name); fn != nil {
ts.fns[name] = fn
} else {
return nil, fmt.Errorf("missing function %q", name)
}
}
ts.lang = make(map[string]api.Function)
for _, name := range _languages {
if fn := ts.mod.ExportedFunction("tree_sitter_" + name); fn != nil {
ts.lang[name] = fn
} else {
return nil, fmt.Errorf("missing language %q", name)
}
}
return ts, nil
}
func (ts *TreeSitter) call(name string, args ...uint64) ([]uint64, error) {
return ts.callCtx(ts.ctx, name, args...)
}
func (ts *TreeSitter) callCtx(ctx context.Context, name string, args ...uint64) ([]uint64, error) {
return ts.fns[name].Call(ctx, args...)
}
func (ts *TreeSitter) allocateString(str string) (ptr uint64, size uint64, free func(), err error) {
strByte := []byte(str)
strSize := uint64(len(strByte))
strPtr, err := ts.call(_malloc, strSize)
if err != nil {
return 0, 0, nil, fmt.Errorf("allocating string: %w", err)
}
if !ts.mod.Memory().Write(uint32(strPtr[0]), strByte) {
return 0, 0, nil, fmt.Errorf("writing string: %w", err)
}
return strPtr[0], strSize, func() {
ts.callCtx(context.Background(), _free, strPtr[0])
}, nil
}
func (ts *TreeSitter) readString(ptr uint64) (string, error) {
strSize, err := ts.call(_strlen, ptr)
if err != nil {
return "", fmt.Errorf("getting string length: %w", err)
}
strBytes, ok := ts.mod.Memory().Read(uint32(ptr), uint32(strSize[0]))
if !ok {
return "", errors.New("error reading string")
}
return string(strBytes), nil
}