-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathparser.ts
More file actions
61 lines (50 loc) · 1.7 KB
/
parser.ts
File metadata and controls
61 lines (50 loc) · 1.7 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
/**
* Parser service using Web Tree Sitter to parse Python code.
*/
import { Language, Parser as TreeSitterParser } from "web-tree-sitter"
export class Parser {
private parser: TreeSitterParser | null = null
/**
* Initialize the parser with Wasm binaries.
* @param wasmBinaries.core - The web-tree-sitter.wasm binary
* @param wasmBinaries.python - The tree-sitter-python.wasm binary
*/
async init(wasmBinaries: { core: Uint8Array; python: Uint8Array }) {
if (this.parser) {
return
}
// Pre-compile the Wasm module from the binary
const wasmModule = await WebAssembly.compile(
wasmBinaries.core as Uint8Array<ArrayBuffer>,
)
// Use instantiateWasm to provide custom Wasm instantiation.
// This bypasses tree-sitter's default URL-based loading which relies
// on import.meta.url - unavailable in bundled or non-ESM environments.
await TreeSitterParser.init({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
instantiateWasm(imports: any, successCallback: any) {
WebAssembly.instantiate(wasmModule, imports).then(
(instance: WebAssembly.Instance) => {
successCallback(instance, wasmModule)
},
)
return {}
},
})
const parser = new TreeSitterParser()
// Load Python language from Wasm binary
const pythonLanguage = await Language.load(wasmBinaries.python)
parser.setLanguage(pythonLanguage)
this.parser = parser
}
parse(code: string) {
if (!this.parser) {
throw new Error("ParserService not initialized. Call init() first.")
}
return this.parser.parse(code)
}
dispose() {
this.parser?.delete()
this.parser = null
}
}