@willbooster/tree-sitter-php 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) 2017 Josh Vera, GitHub
4
+ Copyright (c) 2019 Max Brunsfeld, Amaan Qureshi, Christian Frøystad, Caleb White
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,139 @@
1
- # Temporary Holding Version
1
+ # @willbooster/tree-sitter-php
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-php/actions/workflows/test.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-php/actions/workflows/test.yml)
4
+ [![Test rust](https://github.com/WillBooster/tree-sitter-php/actions/workflows/test-rust.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-php/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.11-1e90ff.svg)](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
7
+ [![crates.io](https://img.shields.io/crates/v/willbooster-tree-sitter-php.svg)](https://crates.io/crates/willbooster-tree-sitter-php)
8
+
9
+ PHP grammars for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
10
+ [tree-sitter/tree-sitter-php](https://github.com/tree-sitter/tree-sitter-php). We are grateful
11
+ to its authors and contributors. This is not an official release of that project.
12
+
13
+ This fork keeps the grammars and the queries of upstream's version 0.24.2. Its external scanner adapts to the generated
14
+ array API and fixes incremental heredoc parsing. It generates the parsers with the
15
+ [WillBooster/tree-sitter](https://github.com/WillBooster/tree-sitter) generator. The parsers carry that fork's own ABI
16
+ number, so they load only with its runtime (`willbooster-tree-sitter`, `@willbooster/web-tree-sitter`), version 2.0.0
17
+ or later, and that runtime loads no parser that upstream's generator wrote.
18
+
19
+ It holds two grammars, as upstream does: `php` parses PHP embedded in text, which starts in text mode and enters PHP at
20
+ an opening tag such as `<?php`, and `php_only` parses PHP code on its own, where the opening tag is optional.
21
+
22
+ ## Usage
23
+
24
+ The npm package ships `tree-sitter-php.wasm` and `tree-sitter-php_only.wasm` for
25
+ [@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:
26
+
27
+ ```js
28
+ import { fileURLToPath } from 'node:url';
29
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
30
+
31
+ await Parser.init();
32
+ const parser = new Parser();
33
+ const wasmPath = fileURLToPath(import.meta.resolve('@willbooster/tree-sitter-php/tree-sitter-php.wasm'));
34
+ parser.setLanguage(await Language.load(wasmPath));
35
+ const tree = parser.parse('<?php\necho "Hello, World!";\n');
36
+ ```
37
+
38
+ In browsers, serve both `.wasm` files and load them by URL (the example uses Vite's `?url` imports):
39
+
40
+ ```js
41
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
42
+ import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
43
+ import phpUrl from '@willbooster/tree-sitter-php/tree-sitter-php.wasm?url';
44
+
45
+ await Parser.init({ locateFile: () => runtimeUrl });
46
+ const parser = new Parser();
47
+ parser.setLanguage(await Language.load(phpUrl));
48
+ ```
49
+
50
+ Cloudflare Workers do not allow compiling Wasm at run time, so import both `.wasm` files as modules, with or without
51
+ Node.js compatibility:
52
+
53
+ ```js
54
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
55
+ import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
56
+ import php from '@willbooster/tree-sitter-php/tree-sitter-php.wasm';
57
+
58
+ await Parser.init({ wasmModule: runtime });
59
+ const parser = new Parser();
60
+ parser.setLanguage(await Language.load(php));
61
+ ```
62
+
63
+ The package also ships the grammar definitions (`common/define-grammar.js`, `php/grammar.js`, and
64
+ `php_only/grammar.js`), the queries in `queries/`, and the node types in `php/src/node-types.json` and
65
+ `php_only/src/node-types.json`.
66
+
67
+ In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-php) and on
68
+ [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
69
+ fuzzed with:
70
+
71
+ ```toml
72
+ [dependencies]
73
+ tree-sitter = { package = "willbooster-tree-sitter", version = "2" }
74
+ tree-sitter-php = { package = "willbooster-tree-sitter-php", version = "1" }
75
+ ```
76
+
77
+ ```rust
78
+ let mut parser = tree_sitter::Parser::new();
79
+ parser.set_language(&tree_sitter_php::LANGUAGE_PHP.into())?;
80
+ ```
81
+
82
+ `tree_sitter_php::LANGUAGE_PHP_ONLY` is the `php_only` grammar.
83
+
84
+ ## Development
85
+
86
+ ```sh
87
+ mise install
88
+ bun install --frozen-lockfile
89
+ bun run test/ci-setup # installs Chromium for the browser test
90
+ bun run build/ci
91
+ bun run test
92
+ script/parse-examples
93
+ cargo test
94
+ ```
95
+
96
+ The scripts and tests generate, build, test, and parse with `script/tree-sitter`, the tree-sitter CLI of the
97
+ WillBooster/tree-sitter runtime version locked in `Cargo.lock`, since the generator and the runtime of upstream's CLI are
98
+ not the ones this package ships with. Its first run downloads that CLI from the runtime's GitHub Release, or builds it
99
+ 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
100
+ well (e.g. `script/tree-sitter parse --grammar-path php file.php`).
101
+
102
+ `bun run generate` records a fresh generation profile from the applicable `test/corpus` cases and Git-tracked
103
+ files in `examples/` for each grammar, then generates compact parser tables. The parsers also embed metadata from `tree-sitter.json`. After changing a grammar,
104
+ `tree-sitter.json`, a corpus case, or a tracked example,
105
+ regenerate and commit `php/src/` and `php_only/src/`. Stage added or removed examples with `git add -A examples` before generation so the profile uses the intended file list.
106
+ Profiles in `.tmp/generation-profiles/` are temporary and must not be committed. `bun run build-wasm`, `bun run build/ci`,
107
+ and the release build regenerate the parsers before compiling them.
108
+
109
+ `bun run test` runs:
110
+
111
+ - the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK).
112
+ Its cases belong to the `php` grammar unless they are marked `:language(php_only)`;
113
+ - an incremental-parsing check (`test/unit/incremental.test.ts`): `script/fuzz-corpus` runs `tree-sitter fuzz` for a
114
+ grammar, which edits each of its corpus cases at random, reparses it, undoes the edits, and reparses again. `TREE_SITTER_SEED`,
115
+ `TREE_SITTER_ITERATIONS`, and `TREE_SITTER_EDITS` run other or more edits. It runs every corpus case with both grammars,
116
+ except the cases `script/fuzz-corpus` excludes;
117
+ - a check that the real-world PHP files in `examples/`, those of the cloned repositories, fail to parse with the `php`
118
+ grammar exactly as listed in `script/known-failures.txt`. The first run clones the repositories. The example repositories are pinned to commits in
119
+ `script/parse-examples`. After a grammar change or a moved pin alters that list, `script/parse-examples` rewrites it;
120
+ review its diff before committing;
121
+ - a performance check (`test/unit/performance.test.ts`) that recovering from an error on each line takes linear
122
+ time with both grammars, since consumers parse files while they are being edited. It loads the Wasm builds through
123
+ @willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after regenerating the parsers;
124
+ - a check (`test/unit/runtimeVersion.test.ts`) that `@willbooster/web-tree-sitter` in `package.json` and
125
+ `willbooster-tree-sitter` in `Cargo.lock` are the same version, since the Wasm tests run on the former and the Rust
126
+ tests, the fuzzer, and the CLI on the latter;
127
+ - tests that load the Wasm builds with @willbooster/web-tree-sitter in Chromium (`test/e2e/browser.test.ts`) and in
128
+ Cloudflare Workers with and without Node.js compatibility (`test/e2e/workers.test.ts`).
129
+
130
+ The tests compile the parsers into `.tmp/tree-sitter-lib` rather than the CLI's cache shared by every checkout;
131
+ `script/fuzz-corpus` and `script/parse-examples` build a parser of their own in `.tmp/` for each run and delete it
132
+ afterwards.
133
+
134
+ CI also runs these tests on Linux arm64 and macOS, where the CLI compiles the parsers and the scanner natively, and
135
+ fuzzes both parsers with libFuzzer and sanitizers (`.github/workflows/robustness.yml`).
136
+
137
+ ### References
138
+
139
+ - [PHP Language Reference](https://www.php.net/manual/en/langref.php)