-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreesitter.go
134 lines (113 loc) · 4.27 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package treesittergo
import (
"context"
_ "embed"
"errors"
"fmt"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/imports/emscripten"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)
//go:embed ts-combined-sql.wasm
var tsWasm []byte
type Treesitter struct {
m api.Module
malloc api.Function
free api.Function
strlen api.Function
parserNew api.Function
parserParseString api.Function
parserDelete api.Function
parserSetLanguage api.Function
languageName api.Function
languageVersion api.Function
treeRootNode api.Function
queryNew api.Function
queryCursorNew api.Function
queryCusorExec api.Function
queryCursorNextMatch api.Function
queryCaptureNameForID api.Function
nodeString api.Function
nodeChildCount api.Function
nodeNamedChildCount api.Function
nodeChild api.Function
nodeNamedChild api.Function
nodeType api.Function
nodeEndByte api.Function
nodeStartByte api.Function
nodeIsError api.Function
languageSQL api.Function
}
func New(ctx context.Context) (Treesitter, error) {
r := wazero.NewRuntime(ctx)
wasi_snapshot_preview1.MustInstantiate(ctx, r)
compiled, err := r.CompileModule(ctx, tsWasm)
if err != nil {
return Treesitter{}, fmt.Errorf("compiling wasm module: %w", err)
}
_, err = emscripten.InstantiateForModule(ctx, r, compiled)
if err != nil {
return Treesitter{}, fmt.Errorf("instantiating emscripten module: %w", err)
}
mod, err := r.InstantiateModule(ctx, compiled, wazero.NewModuleConfig())
if err != nil {
return Treesitter{}, fmt.Errorf("instantiating module: %w", err)
}
return Treesitter{
m: mod,
malloc: mod.ExportedFunction("malloc"),
free: mod.ExportedFunction("free"),
strlen: mod.ExportedFunction("strlen"),
parserNew: mod.ExportedFunction("ts_parser_new"),
parserParseString: mod.ExportedFunction("ts_parser_parse_string"),
parserSetLanguage: mod.ExportedFunction("ts_parser_set_language"),
parserDelete: mod.ExportedFunction("ts_parser_delete"),
queryNew: mod.ExportedFunction("ts_query_new"),
queryCursorNew: mod.ExportedFunction("ts_query_cursor_new"),
queryCusorExec: mod.ExportedFunction("ts_query_cursor_exec"),
queryCursorNextMatch: mod.ExportedFunction("ts_query_cursor_next_match"),
queryCaptureNameForID: mod.ExportedFunction("ts_query_capture_name_for_id"),
languageName: mod.ExportedFunction("ts_language_name"),
languageVersion: mod.ExportedFunction("ts_language_version"),
treeRootNode: mod.ExportedFunction("ts_tree_root_node"),
nodeString: mod.ExportedFunction("ts_node_string"),
nodeChildCount: mod.ExportedFunction("ts_node_child_count"),
nodeNamedChildCount: mod.ExportedFunction("ts_node_named_child_count"),
nodeChild: mod.ExportedFunction("ts_node_child"),
nodeNamedChild: mod.ExportedFunction("ts_node_named_child"),
nodeType: mod.ExportedFunction("ts_node_type"),
nodeStartByte: mod.ExportedFunction("ts_node_start_byte"),
nodeEndByte: mod.ExportedFunction("ts_node_end_byte"),
nodeIsError: mod.ExportedFunction("ts_node_is_error"),
languageSQL: mod.ExportedFunction("tree_sitter_sql"),
}, nil
}
func (t Treesitter) allocateString(
ctx context.Context,
str string,
) (ptr uint64, size uint64, free func(), err error) {
strByte := []byte(str)
strSize := uint64(len(strByte))
strPtr, err := t.malloc.Call(ctx, strSize)
if err != nil {
return 0, 0, nil, fmt.Errorf("allocating string: %w", err)
}
if !t.m.Memory().Write(uint32(strPtr[0]), strByte) {
return 0, 0, nil, fmt.Errorf("writing string: %w", err)
}
return strPtr[0], strSize, func() {
t.free.Call(context.Background(), strPtr[0])
}, nil
}
func (t Treesitter) readString(ctx context.Context, ptr uint64) (string, error) {
strSize, err := t.strlen.Call(ctx, ptr)
if err != nil {
return "", fmt.Errorf("getting string length: %w", err)
}
strBytes, ok := t.m.Memory().Read(uint32(ptr), uint32(strSize[0]))
if !ok {
return "", errors.New("error reading string")
}
return string(strBytes), nil
}