@willbooster/tree-sitter-python 0.0.0-stage → 1.0.1

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 ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 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,128 @@
1
- # Temporary Holding Version
1
+ # @willbooster/tree-sitter-python
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![Test](https://github.com/WillBooster/tree-sitter-python/actions/workflows/test.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-python/actions/workflows/test.yml)
4
+ [![Test rust](https://github.com/WillBooster/tree-sitter-python/actions/workflows/test-rust.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-python/actions/workflows/test-rust.yml)
5
+ [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
6
+ [![wbfy](https://img.shields.io/badge/wbfy-20.36.6-1e90ff.svg)](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
7
+ [![crates.io](https://img.shields.io/crates/v/willbooster-tree-sitter-python.svg)](https://crates.io/crates/willbooster-tree-sitter-python)
8
+
9
+ Python grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
10
+ [tree-sitter/tree-sitter-python](https://github.com/tree-sitter/tree-sitter-python). We are grateful
11
+ to its authors and contributors. This is not an official release of that project.
12
+
13
+ This fork ships the unmodified grammar of upstream v0.25.0 as a compact ABI 16 parser, generated by the parser generator
14
+ of [WillBooster/tree-sitter](https://github.com/WillBooster/tree-sitter).
15
+
16
+ ## Usage
17
+
18
+ The npm package ships `tree-sitter-python.wasm` for
19
+ [@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter), which runs in Node.js, Bun,
20
+ browsers, and Cloudflare Workers. The compact ABI 16 parser requires runtime 1.3.0 or later.
21
+
22
+ In Node.js and Bun, load the grammar from its path:
23
+
24
+ ```js
25
+ import { fileURLToPath } from 'node:url';
26
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
27
+
28
+ await Parser.init();
29
+ const parser = new Parser();
30
+ const wasmPath = fileURLToPath(import.meta.resolve('@willbooster/tree-sitter-python/tree-sitter-python.wasm'));
31
+ parser.setLanguage(await Language.load(wasmPath));
32
+ const tree = parser.parse('def main():\n pass\n');
33
+ ```
34
+
35
+ In browsers, load the grammar from its URL. With Vite:
36
+
37
+ ```js
38
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
39
+ import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
40
+ import pythonUrl from '@willbooster/tree-sitter-python/tree-sitter-python.wasm?url';
41
+
42
+ await Parser.init({ locateFile: () => runtimeUrl });
43
+ const parser = new Parser();
44
+ parser.setLanguage(await Language.load(pythonUrl));
45
+ ```
46
+
47
+ In Cloudflare Workers, which do not allow compiling Wasm at run time, import both `.wasm` files as modules:
48
+
49
+ ```js
50
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
51
+ import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
52
+ import python from '@willbooster/tree-sitter-python/tree-sitter-python.wasm';
53
+
54
+ await Parser.init({ wasmModule: runtime });
55
+ const parser = new Parser();
56
+ parser.setLanguage(await Language.load(python));
57
+ ```
58
+
59
+ The package also ships the queries in `queries/` and the node types in `src/node-types.json`.
60
+
61
+ In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-python) and on
62
+ [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
63
+ fuzzed with. The compact ABI 16 parser requires runtime 1.3.0 or later:
64
+
65
+ ```toml
66
+ [dependencies]
67
+ tree-sitter = { package = "willbooster-tree-sitter", version = "1.3.0" }
68
+ tree-sitter-python = { package = "willbooster-tree-sitter-python", version = "1" }
69
+ ```
70
+
71
+ ```rust
72
+ let mut parser = tree_sitter::Parser::new();
73
+ parser.set_language(&tree_sitter_python::LANGUAGE.into())?;
74
+ ```
75
+
76
+ ## Development
77
+
78
+ ```sh
79
+ mise install
80
+ bun install --frozen-lockfile
81
+ bun run build/ci
82
+ bun run test
83
+ script/parse-examples
84
+ cargo test
85
+ ```
86
+
87
+ The scripts and tests generate, build, test, and parse with `script/tree-sitter`, the tree-sitter CLI of the
88
+ WillBooster/tree-sitter runtime version locked in `Cargo.lock`, since the generator and the runtime of upstream's CLI are
89
+ not the ones this package ships with. Its first run downloads that CLI from the runtime's GitHub Release, or builds it
90
+ with `cargo` (whose build runs the CMake that `mise.toml` pins) when the download fails or the release has no binary that runs here. Run other CLI
91
+ commands through it as well (e.g. `script/tree-sitter parse file.py`).
92
+
93
+ `bun run generate` records a fresh ABI 16 generation profile from the applicable `test/corpus` cases and Git-tracked
94
+ Python files in `examples/`, then generates compact parser tables. The parser also embeds metadata from `tree-sitter.json`. After changing a grammar,
95
+ `tree-sitter.json`, a corpus case, or a tracked example,
96
+ regenerate and commit `src/`. Stage added or removed examples with `git add -A examples` before generation so the profile uses the intended file list.
97
+ Profiles in `.tmp/generation-profiles/` are temporary and must not be committed. `bun run build-wasm`, `bun run build/ci`,
98
+ and the release build regenerate the parsers before compiling them.
99
+
100
+ `bun run test` runs:
101
+
102
+ - the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK);
103
+ - an incremental-parsing check (`test/unit/incremental.test.ts`): `script/fuzz-corpus` runs `tree-sitter fuzz`, which
104
+ edits each corpus case at random, reparses it, undoes the edits, and reparses again. `TREE_SITTER_SEED`,
105
+ `TREE_SITTER_ITERATIONS`, and `TREE_SITTER_EDITS` run other or more edits;
106
+ - a check that the real-world Python files in `examples/`, the checked-in ones and those of the cloned repositories,
107
+ fail to parse exactly as listed in `script/known-failures.txt`. The first run clones the repositories. The example
108
+ repositories are pinned to commits in `script/parse-examples`. After a grammar change or a moved pin alters that list,
109
+ `script/parse-examples` rewrites it; review its diff before committing;
110
+ - a performance check (`test/unit/performance.test.ts`) that recovering from an error on each line takes linear time
111
+ (ten times the lines take about ten times the CPU time, under a ceiling), since consumers parse files while they are
112
+ being edited. It loads the Wasm build through @willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after
113
+ regenerating the parser;
114
+ - a check that `package.json` and `Cargo.lock` test the same runtime version (`test/unit/runtimeVersion.test.ts`);
115
+ - checks that the Wasm build parses in Chromium (`test/unit/browser/`) and in Cloudflare Workers with and without
116
+ Node.js compatibility (`test/unit/workerd.test.ts`). Run `bun run test/ci-setup` once to install Chromium.
117
+
118
+ The tests and `script/parse-examples` compile the parser into `.tmp/tree-sitter-lib` rather than the CLI's cache shared
119
+ by every checkout; `script/fuzz-corpus` builds a per-run parser in `.tmp/fuzz` and deletes it afterwards.
120
+
121
+ CI also runs these tests on Linux arm64 and macOS, where the CLI compiles the parser and the scanner with the platform's
122
+ C compiler as the crate does for its users, and fuzzes the parser with libFuzzer and sanitizers
123
+ (`.github/workflows/robustness.yml`).
124
+
125
+ ### References
126
+
127
+ - [Python 2 Grammar](https://docs.python.org/2/reference/grammar.html)
128
+ - [Python 3 Grammar](https://docs.python.org/3/reference/grammar.html)