@willbooster/web-tree-sitter 0.0.0-placeholder.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +248 -1
- package/debug/web-tree-sitter.cjs +4754 -0
- package/debug/web-tree-sitter.cjs.map +7 -0
- package/debug/web-tree-sitter.js +4694 -0
- package/debug/web-tree-sitter.js.map +7 -0
- package/debug/web-tree-sitter.wasm +0 -0
- package/debug/web-tree-sitter.wasm.map +57 -0
- package/package.json +112 -2
- package/web-tree-sitter.cjs +4154 -0
- package/web-tree-sitter.cjs.map +7 -0
- package/web-tree-sitter.d.cts +1060 -0
- package/web-tree-sitter.d.cts.map +61 -0
- package/web-tree-sitter.d.ts +1060 -0
- package/web-tree-sitter.d.ts.map +61 -0
- package/web-tree-sitter.js +4094 -0
- package/web-tree-sitter.js.map +7 -0
- package/web-tree-sitter.wasm +0 -0
- package/web-tree-sitter.wasm.map +55 -0
- package/web-tree-sitter.web.js +4126 -0
- package/web-tree-sitter.web.js.map +7 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Max Brunsfeld
|
|
4
|
+
Copyright (c) 2026 WillBooster Inc.
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,250 @@
|
|
|
1
1
|
# @willbooster/web-tree-sitter
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[![npmjs.com badge]][npmjs.com]
|
|
4
|
+
|
|
5
|
+
[npmjs.com]: https://www.npmjs.org/package/@willbooster/web-tree-sitter
|
|
6
|
+
[npmjs.com badge]: https://img.shields.io/npm/v/@willbooster/web-tree-sitter.svg?color=%23BF4A4A
|
|
7
|
+
|
|
8
|
+
WebAssembly bindings to WillBooster's fork of the [Tree-sitter](https://github.com/tree-sitter/tree-sitter) parsing library.
|
|
9
|
+
The package runs in Node.js, Bun, browsers, and Cloudflare Workers.
|
|
10
|
+
|
|
11
|
+
## Setup
|
|
12
|
+
|
|
13
|
+
In Node.js and Bun, import the package and initialize it; it reads `web-tree-sitter.wasm` from the package:
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { Parser } from '@willbooster/web-tree-sitter';
|
|
17
|
+
await Parser.init();
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
In browsers, bundlers select the `browser` export, which has no Node.js-specific code. The library fetches
|
|
21
|
+
`web-tree-sitter.wasm` next to the bundled script by default; pass `locateFile` when your bundler serves it elsewhere:
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
import { Parser } from '@willbooster/web-tree-sitter';
|
|
25
|
+
import wasmUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url'; // Vite
|
|
26
|
+
|
|
27
|
+
await Parser.init({ locateFile: () => wasmUrl });
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
In Cloudflare Workers, which do not allow compiling Wasm at run time, import the `.wasm` files as modules and pass
|
|
31
|
+
them to `Parser.init` and `Language.load`:
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
35
|
+
import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
|
|
36
|
+
import javascript from './tree-sitter-javascript.wasm';
|
|
37
|
+
|
|
38
|
+
await Parser.init({ wasmModule: runtime });
|
|
39
|
+
const JavaScript = await Language.load(javascript);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
To use the debug version of the library in Node.js, import `@willbooster/web-tree-sitter/debug` instead. It loads the
|
|
43
|
+
debug versions of the `.js` and `.wasm` files, which include debug symbols and assertions.
|
|
44
|
+
|
|
45
|
+
### Basic Usage
|
|
46
|
+
|
|
47
|
+
First, create a parser:
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
const parser = new Parser();
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Then assign a language to the parser. Tree-sitter languages are packaged as individual `.wasm` files (more on this below):
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
const { Language } = require('@willbooster/web-tree-sitter');
|
|
57
|
+
const JavaScript = await Language.load('/path/to/tree-sitter-javascript.wasm');
|
|
58
|
+
parser.setLanguage(JavaScript);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Now you can parse source code:
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
const sourceCode = 'let x = 1; console.log(x);';
|
|
65
|
+
const tree = parser.parse(sourceCode);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
and inspect the syntax tree.
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
console.log(tree.rootNode.toString());
|
|
72
|
+
|
|
73
|
+
// (program
|
|
74
|
+
// (lexical_declaration
|
|
75
|
+
// (variable_declarator (identifier) (number)))
|
|
76
|
+
// (expression_statement
|
|
77
|
+
// (call_expression
|
|
78
|
+
// (member_expression (identifier) (property_identifier))
|
|
79
|
+
// (arguments (identifier)))))
|
|
80
|
+
|
|
81
|
+
const callExpression = tree.rootNode.child(1).firstChild;
|
|
82
|
+
console.log(callExpression);
|
|
83
|
+
|
|
84
|
+
// { type: 'call_expression',
|
|
85
|
+
// startPosition: {row: 0, column: 16},
|
|
86
|
+
// endPosition: {row: 0, column: 30},
|
|
87
|
+
// startIndex: 0,
|
|
88
|
+
// endIndex: 30 }
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Editing
|
|
92
|
+
|
|
93
|
+
If your source code *changes*, you can update the syntax tree. This will take less time than the first parse.
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
// Replace 'let' with 'const'
|
|
97
|
+
const newSourceCode = 'const x = 1; console.log(x);';
|
|
98
|
+
|
|
99
|
+
tree.edit({
|
|
100
|
+
startIndex: 0,
|
|
101
|
+
oldEndIndex: 3,
|
|
102
|
+
newEndIndex: 5,
|
|
103
|
+
startPosition: {row: 0, column: 0},
|
|
104
|
+
oldEndPosition: {row: 0, column: 3},
|
|
105
|
+
newEndPosition: {row: 0, column: 5},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const newTree = parser.parse(newSourceCode, tree);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Parsing Text From a Custom Data Structure
|
|
112
|
+
|
|
113
|
+
If your text is stored in a data structure other than a single string, you can parse it by supplying a callback to `parse`
|
|
114
|
+
instead of a string:
|
|
115
|
+
|
|
116
|
+
```javascript
|
|
117
|
+
const sourceLines = [
|
|
118
|
+
'let x = 1;',
|
|
119
|
+
'console.log(x);'
|
|
120
|
+
];
|
|
121
|
+
|
|
122
|
+
const tree = parser.parse((index, position) => {
|
|
123
|
+
let line = sourceLines[position.row];
|
|
124
|
+
if (line) return line.slice(position.column);
|
|
125
|
+
});
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Getting the `.wasm` language files
|
|
129
|
+
|
|
130
|
+
There are several options on how to get the `.wasm` files for the languages you want to parse.
|
|
131
|
+
|
|
132
|
+
#### From npmjs.com
|
|
133
|
+
|
|
134
|
+
The recommended way is to just install the package from npm. For example, to parse JavaScript, you can install the `tree-sitter-javascript`
|
|
135
|
+
package:
|
|
136
|
+
|
|
137
|
+
```sh
|
|
138
|
+
npm install tree-sitter-javascript
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Then you can find the `.wasm` file in the `node_modules/tree-sitter-javascript` directory.
|
|
142
|
+
|
|
143
|
+
#### From GitHub
|
|
144
|
+
|
|
145
|
+
You can also download the `.wasm` files from GitHub releases, so long as the repository uses our reusable workflow to publish
|
|
146
|
+
them.
|
|
147
|
+
For example, you can download the JavaScript `.wasm` file from the tree-sitter-javascript [releases page][gh release js].
|
|
148
|
+
|
|
149
|
+
#### Generating `.wasm` files
|
|
150
|
+
|
|
151
|
+
You can also generate the `.wasm` file for your desired grammar. Shown below is an example of how to generate the `.wasm`
|
|
152
|
+
file for the JavaScript grammar.
|
|
153
|
+
|
|
154
|
+
> [!NOTE]
|
|
155
|
+
> Since v0.26.1, `tree-sitter build --wasm` uses [wasi-sdk][] and will automatically download it on first use.
|
|
156
|
+
> No additional tools need to be installed.
|
|
157
|
+
|
|
158
|
+
First install `tree-sitter-cli`, and the tree-sitter language for which to generate `.wasm`
|
|
159
|
+
(`tree-sitter-javascript` in this example):
|
|
160
|
+
|
|
161
|
+
```sh
|
|
162
|
+
npm install --save-dev tree-sitter-cli tree-sitter-javascript
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Then just use tree-sitter cli tool to generate the `.wasm`.
|
|
166
|
+
|
|
167
|
+
```sh
|
|
168
|
+
npx tree-sitter build --wasm node_modules/tree-sitter-javascript
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
If everything is fine, file `tree-sitter-javascript.wasm` should be generated in current directory.
|
|
172
|
+
|
|
173
|
+
### Wasm compatibility
|
|
174
|
+
|
|
175
|
+
`web-tree-sitter` supports the same parser ABI versions as the corresponding tree-sitter library:
|
|
176
|
+
|
|
177
|
+
| web-tree-sitter version | Min parser ABI version | Max parser ABI version |
|
|
178
|
+
|-------------------------|------------------------|------------------------|
|
|
179
|
+
| 0.24.x | 13 | 14 |
|
|
180
|
+
| >= 0.25.0 | 13 | 15 |
|
|
181
|
+
|
|
182
|
+
> [!WARNING]
|
|
183
|
+
> Some prebuilt `.wasm` files use an older dynamic-linking format that newer versions of `web-tree-sitter` cannot
|
|
184
|
+
> load, even if their parser ABI is supported. Rebuild these files using a current tree-sitter CLI.
|
|
185
|
+
|
|
186
|
+
### Running .wasm in Node.js
|
|
187
|
+
|
|
188
|
+
Notice that executing `.wasm` files in Node.js is considerably slower than running [Node.js bindings][node bindings].
|
|
189
|
+
However, this could be useful for testing purposes:
|
|
190
|
+
|
|
191
|
+
```javascript
|
|
192
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
193
|
+
|
|
194
|
+
await Parser.init();
|
|
195
|
+
const parser = new Parser();
|
|
196
|
+
parser.setLanguage(await Language.load('tree-sitter-javascript.wasm'));
|
|
197
|
+
const tree = parser.parse('let x = 1;');
|
|
198
|
+
console.log(tree.rootNode.toString());
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Loading a pre-compiled WebAssembly module
|
|
202
|
+
|
|
203
|
+
Some environments, such as Cloudflare Workers and Vercel Edge Functions, import
|
|
204
|
+
`.wasm` files as `WebAssembly.Module` objects. `Language.load` accepts those modules, and `Language.loadSync` loads them
|
|
205
|
+
synchronously:
|
|
206
|
+
|
|
207
|
+
```javascript
|
|
208
|
+
import treeSitterJavaScript from 'tree-sitter-javascript.wasm';
|
|
209
|
+
// treeSitterJavaScript is of type `WebAssembly.Module`
|
|
210
|
+
const JavaScript = Language.loadSync(treeSitterJavaScript);
|
|
211
|
+
parser.setLanguage(JavaScript);
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### Running .wasm in browser
|
|
215
|
+
|
|
216
|
+
`@willbooster/web-tree-sitter` can run in the browser, but there are some common pitfalls.
|
|
217
|
+
|
|
218
|
+
#### Loading the .wasm file
|
|
219
|
+
|
|
220
|
+
`@willbooster/web-tree-sitter` needs to load the `web-tree-sitter.wasm` file. By default, it assumes that this file is available in the
|
|
221
|
+
same path as the JavaScript code. Therefore, if the code is being served from `http://localhost:3000/bundle.js`, then
|
|
222
|
+
the Wasm file should be at `http://localhost:3000/web-tree-sitter.wasm`.
|
|
223
|
+
|
|
224
|
+
For server side frameworks like NextJS, this can be tricky as pages are often served from a path such as
|
|
225
|
+
`http://localhost:3000/_next/static/chunks/pages/index.js`. The loader will therefore look for the Wasm file at
|
|
226
|
+
`http://localhost:3000/_next/static/chunks/pages/web-tree-sitter.wasm`. The solution is to pass a `locateFile` function in
|
|
227
|
+
the `moduleOptions` argument to `Parser.init()`:
|
|
228
|
+
|
|
229
|
+
```javascript
|
|
230
|
+
await Parser.init({
|
|
231
|
+
locateFile(scriptName: string, scriptDirectory: string) {
|
|
232
|
+
return scriptName;
|
|
233
|
+
},
|
|
234
|
+
});
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
`locateFile` takes in two parameters, `scriptName`, i.e. the Wasm file name, and `scriptDirectory`, i.e. the directory
|
|
238
|
+
where the loader expects the script to be. It returns the path where the loader will look for the Wasm file. In the NextJS
|
|
239
|
+
case, we want to return just the `scriptName` so that the loader will look at `http://localhost:3000/web-tree-sitter.wasm`
|
|
240
|
+
and not `http://localhost:3000/_next/static/chunks/pages/web-tree-sitter.wasm`.
|
|
241
|
+
|
|
242
|
+
For more information on the module options you can pass in, see the [emscripten documentation][emscripten-module-options].
|
|
243
|
+
|
|
244
|
+
[docker]: https://www.docker.com
|
|
245
|
+
[emscripten]: https://emscripten.org
|
|
246
|
+
[emscripten-module-options]: https://emscripten.org/docs/api_reference/module.html#affecting-execution
|
|
247
|
+
[gh release js]: https://github.com/tree-sitter/tree-sitter-javascript/releases/latest
|
|
248
|
+
[node bindings]: https://github.com/tree-sitter/node-tree-sitter
|
|
249
|
+
[podman]: https://podman.io
|
|
250
|
+
[wasi-sdk]: https://github.com/WebAssembly/wasi-sdk
|