@willbooster/tree-sitter-zig 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 ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Amaan Qureshi <amaanq12@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,142 @@
1
1
  # @willbooster/tree-sitter-zig
2
2
 
3
- This version is a placeholder that reserves the package name so that releases can be published from GitHub Actions with trusted publishing. Use 1.0.0 or later.
3
+ [![npm version](https://img.shields.io/npm/v/@willbooster/tree-sitter-zig.svg)](https://www.npmjs.com/package/@willbooster/tree-sitter-zig)
4
+ [![license](https://img.shields.io/npm/l/@willbooster/tree-sitter-zig.svg)](https://www.npmjs.com/package/@willbooster/tree-sitter-zig)
5
+ [![Test](https://github.com/WillBooster/tree-sitter-zig/actions/workflows/test.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-zig/actions/workflows/test.yml)
6
+ [![Test rust](https://github.com/WillBooster/tree-sitter-zig/actions/workflows/test-rust.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-zig/actions/workflows/test-rust.yml)
7
+ [![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)
8
+ [![wbfy](https://img.shields.io/badge/wbfy-20.36.11-1e90ff.svg)](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
9
+ [![crates.io](https://img.shields.io/crates/v/willbooster-tree-sitter-zig.svg)](https://crates.io/crates/willbooster-tree-sitter-zig)
10
+
11
+ Zig grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
12
+ [tree-sitter-grammars/tree-sitter-zig](https://github.com/tree-sitter-grammars/tree-sitter-zig). We are grateful
13
+ to its authors and contributors. This is not an official release of that project.
14
+
15
+ This fork keeps the grammar of upstream's version 1.1.2 and generates the parser with the
16
+ [WillBooster/tree-sitter](https://github.com/WillBooster/tree-sitter) generator. The parser carries that fork's own ABI
17
+ number (`0x57420001`) and loads only with that fork's runtime, version 2.0.0 or later: upstream's tree-sitter runtime
18
+ rejects it. It parses as the parser in upstream's 1.1.2 packages does, with two exceptions:
19
+
20
+ - In the ranges a `for` loop iterates over, a range end whose first token can both start an expression and continue
21
+ the one before `..`, for example `(`, `[`, `.`, `*`, `&`, `-`, or `!` (as in `for (0..(n)) |i| {}` or
22
+ `for (0..&x) |i| {}`), is the right operand of a `range_expression`. Upstream's packaged parser, generated by
23
+ tree-sitter CLI 0.24, lets that token continue the expression instead, on a range without an end: a call, an index,
24
+ a field access, a binary expression, and so on. tree-sitter CLI 0.27 generates this fork's reading from the same
25
+ grammar. Every other range end reads the same in both, whether its first token can only start an expression (`n`,
26
+ `~x`) or only continue one (`+`, `orelse`), and so do ranges outside `for` loops.
27
+ - A NUL character inside a token such as a string or a comment is part of that token, where upstream's lexer reports
28
+ an error.
29
+
30
+ ## Usage
31
+
32
+ The npm package ships `tree-sitter-zig.wasm` for
33
+ [@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 2.0.0 or later, which runs in Node.js, Bun, browsers, and Cloudflare Workers. In Node.js and Bun, load the grammar from its path:
34
+
35
+ ```js
36
+ import { fileURLToPath } from 'node:url';
37
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
38
+
39
+ await Parser.init();
40
+ const parser = new Parser();
41
+ const wasmPath = fileURLToPath(import.meta.resolve('@willbooster/tree-sitter-zig/tree-sitter-zig.wasm'));
42
+ parser.setLanguage(await Language.load(wasmPath));
43
+ const tree = parser.parse('pub fn main() void {}\n');
44
+ ```
45
+
46
+ In browsers, serve both `.wasm` files and load them by URL (the example uses Vite's `?url` imports):
47
+
48
+ ```js
49
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
50
+ import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
51
+ import zigUrl from '@willbooster/tree-sitter-zig/tree-sitter-zig.wasm?url';
52
+
53
+ await Parser.init({ locateFile: () => runtimeUrl });
54
+ const parser = new Parser();
55
+ parser.setLanguage(await Language.load(zigUrl));
56
+ ```
57
+
58
+ Cloudflare Workers do not allow compiling Wasm at run time, so import both `.wasm` files as modules, with or without
59
+ Node.js compatibility:
60
+
61
+ ```js
62
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
63
+ import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
64
+ import zig from '@willbooster/tree-sitter-zig/tree-sitter-zig.wasm';
65
+
66
+ await Parser.init({ wasmModule: runtime });
67
+ const parser = new Parser();
68
+ parser.setLanguage(await Language.load(zig));
69
+ ```
70
+
71
+ The package also ships `grammar.js`, the queries in `queries/`, and the node types in `src/node-types.json`.
72
+
73
+ In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-zig) and on
74
+ [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
75
+ fuzzed with:
76
+
77
+ ```toml
78
+ [dependencies]
79
+ tree-sitter = { package = "willbooster-tree-sitter", version = "2" }
80
+ tree-sitter-zig = { package = "willbooster-tree-sitter-zig", version = "1" }
81
+ ```
82
+
83
+ ```rust
84
+ let mut parser = tree_sitter::Parser::new();
85
+ parser.set_language(&tree_sitter_zig::LANGUAGE.into())?;
86
+ ```
87
+
88
+ ## Development
89
+
90
+ ```sh
91
+ mise install
92
+ bun install --frozen-lockfile
93
+ bun run test/ci-setup # installs Chromium for the browser test
94
+ bun run build/ci
95
+ bun run test
96
+ script/parse-examples
97
+ cargo test
98
+ ```
99
+
100
+ The scripts and tests generate, build, test, and parse with `script/tree-sitter`, the tree-sitter CLI of the
101
+ WillBooster/tree-sitter runtime version locked in `Cargo.lock`, since the generator and the runtime of upstream's CLI are
102
+ not the ones this package ships with. Its first run downloads that CLI from the runtime's GitHub Release, or builds it
103
+ 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 commands through it as
104
+ well (e.g. `script/tree-sitter parse file.zig`).
105
+
106
+ `bun run generate` records a fresh generation profile from the applicable `test/corpus` cases, then generates
107
+ compact parser tables. It would also profile files tracked in `examples/`, but this repository tracks none: upstream
108
+ ships neither examples nor a corpus, so the cases in `test/corpus` were written for this fork, with the trees upstream's
109
+ packaged parser gives them, except the NUL case and the `for` range case, which pin this parser's reading. The parser also embeds metadata from `tree-sitter.json`.
110
+ After changing a grammar, `tree-sitter.json`, or a corpus case, regenerate and commit `src/`.
111
+ Profiles in `.tmp/generation-profiles/` are temporary and must not be committed. `bun run build-wasm`, `bun run build/ci`,
112
+ and the release build regenerate the parsers before compiling them.
113
+
114
+ `bun run test` runs:
115
+
116
+ - the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK);
117
+ - an incremental-parsing check (`test/unit/incremental.test.ts`): `script/fuzz-corpus` runs `tree-sitter fuzz`, which
118
+ edits each corpus case at random, reparses it, undoes the edits, and reparses again. `TREE_SITTER_SEED`,
119
+ `TREE_SITTER_ITERATIONS`, and `TREE_SITTER_EDITS` run other or more edits;
120
+ - a check that the real-world Zig files in `examples/` fail to parse exactly as listed in
121
+ `script/known-failures.txt`. The first run clones the repositories. The example repositories are pinned to commits in
122
+ `script/parse-examples`. After a grammar change or a moved pin alters that list, `script/parse-examples` rewrites it;
123
+ review its diff before committing;
124
+ - a performance check (`test/unit/performance.test.ts`) that recovering from an error on each line takes linear
125
+ time, since consumers parse files while they are being edited. It loads the Wasm build through
126
+ @willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after regenerating the parser;
127
+ - a check (`test/unit/runtimeVersion.test.ts`) that `@willbooster/web-tree-sitter` in `package.json` and
128
+ `willbooster-tree-sitter` in `Cargo.lock` are the same version, since the Wasm tests run on the former and the Rust
129
+ tests, the fuzzer, and the CLI on the latter;
130
+ - tests that load the Wasm build with @willbooster/web-tree-sitter in Chromium (`test/e2e/browser.test.ts`) and in
131
+ Cloudflare Workers with and without Node.js compatibility (`test/e2e/workers.test.ts`).
132
+
133
+ The tests and `script/parse-examples` compile the parser into `.tmp/tree-sitter-lib` rather than the CLI's cache shared
134
+ by every checkout; `script/fuzz-corpus` builds a parser of its own in `.tmp/fuzz` for each run and deletes it
135
+ afterwards.
136
+
137
+ CI also runs these tests on Linux arm64 and macOS, where the CLI compiles the parser natively, and fuzzes the parser
138
+ with libFuzzer and sanitizers (`.github/workflows/robustness.yml`).
139
+
140
+ ### References
141
+
142
+ - [Zig Grammar](https://github.com/ziglang/zig-spec/blob/master/grammar/grammar.peg)