@willbooster/tree-sitter-c 0.0.0-placeholder.0 → 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 +22 -0
- package/README.md +110 -1
- package/grammar.js +1276 -0
- package/package.json +63 -2
- package/queries/highlights.scm +81 -0
- package/queries/tags.scm +9 -0
- package/src/node-types.json +4619 -0
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014 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,112 @@
|
|
|
1
1
|
# @willbooster/tree-sitter-c
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@willbooster/tree-sitter-c)
|
|
4
|
+
[](https://www.npmjs.com/package/@willbooster/tree-sitter-c)
|
|
5
|
+
[](https://github.com/WillBooster/tree-sitter-c/actions/workflows/test.yml)
|
|
6
|
+
[](https://github.com/WillBooster/tree-sitter-c/actions/workflows/test-rust.yml)
|
|
7
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
8
|
+
[](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
|
|
9
|
+
[](https://crates.io/crates/willbooster-tree-sitter-c)
|
|
10
|
+
|
|
11
|
+
C grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
|
|
12
|
+
[tree-sitter/tree-sitter-c](https://github.com/tree-sitter/tree-sitter-c). We are grateful
|
|
13
|
+
to its authors and contributors. This is not an official release of that project.
|
|
14
|
+
|
|
15
|
+
This fork fixes parsing bugs and raises conformance with the ISO C standard
|
|
16
|
+
([C23 working draft N3220](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf)).
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
The npm package ships `tree-sitter-c.wasm` for
|
|
21
|
+
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter), which runs in Node.js,
|
|
22
|
+
Bun, browsers, and Cloudflare Workers. 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-c/tree-sitter-c.wasm'));
|
|
31
|
+
parser.setLanguage(await Language.load(wasmPath));
|
|
32
|
+
const tree = parser.parse('int main(void) { return 0; }\n');
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
In browsers, serve both `.wasm` files and load them by URL (the example uses Vite's `?url` imports):
|
|
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 cUrl from '@willbooster/tree-sitter-c/tree-sitter-c.wasm?url';
|
|
41
|
+
|
|
42
|
+
await Parser.init({ locateFile: () => runtimeUrl });
|
|
43
|
+
const parser = new Parser();
|
|
44
|
+
parser.setLanguage(await Language.load(cUrl));
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Cloudflare Workers do not allow compiling Wasm at run time, so import both `.wasm` files as modules, with or without
|
|
48
|
+
Node.js compatibility:
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
52
|
+
import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
|
|
53
|
+
import c from '@willbooster/tree-sitter-c/tree-sitter-c.wasm';
|
|
54
|
+
|
|
55
|
+
await Parser.init({ wasmModule: runtime });
|
|
56
|
+
const parser = new Parser();
|
|
57
|
+
parser.setLanguage(await Language.load(c));
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The package also ships `grammar.js`, the queries in `queries/`, and the node types in `src/node-types.json` for grammars
|
|
61
|
+
extending C (such as C++).
|
|
62
|
+
|
|
63
|
+
In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-c):
|
|
64
|
+
|
|
65
|
+
```toml
|
|
66
|
+
[dependencies]
|
|
67
|
+
tree-sitter = "0.27"
|
|
68
|
+
tree-sitter-c = { package = "willbooster-tree-sitter-c", version = "1" }
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```rust
|
|
72
|
+
let mut parser = tree_sitter::Parser::new();
|
|
73
|
+
parser.set_language(&tree_sitter_c::LANGUAGE.into())?;
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Development
|
|
77
|
+
|
|
78
|
+
```sh
|
|
79
|
+
mise install
|
|
80
|
+
bun install --frozen-lockfile
|
|
81
|
+
bun run test/ci-setup # installs Chromium for the browser test
|
|
82
|
+
bun run build/ci
|
|
83
|
+
bun run test
|
|
84
|
+
script/parse-examples
|
|
85
|
+
cargo test
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`bun run test` runs:
|
|
89
|
+
|
|
90
|
+
- the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK);
|
|
91
|
+
- an incremental-parsing check (`test/unit/incremental.test.ts`): `tree-sitter fuzz` edits each corpus case at random,
|
|
92
|
+
reparses it, undoes the edits, and reparses again. `TREE_SITTER_SEED`, `TREE_SITTER_ITERATIONS`, and
|
|
93
|
+
`TREE_SITTER_EDITS` run other or more edits;
|
|
94
|
+
- a check that the real-world C files in `examples/`, the checked-in ones and those of the cloned repositories, fail to
|
|
95
|
+
parse exactly as listed in `script/known-failures.txt`. Many of the listed files use preprocessor conditionals or
|
|
96
|
+
macros that tree-sitter parses without expanding them. The first run clones the repositories. The example
|
|
97
|
+
repositories are pinned to commits in `script/parse-examples`. After a grammar change or a moved pin alters that
|
|
98
|
+
list, `script/parse-examples` rewrites it; review its diff before committing;
|
|
99
|
+
- a performance check (`test/unit/performance.test.ts`) that recovering from an error on each of 10,000 lines takes
|
|
100
|
+
linear time, since consumers parse files while they are being edited. It loads the Wasm build through
|
|
101
|
+
@willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after regenerating the parser;
|
|
102
|
+
- tests that load the Wasm build with @willbooster/web-tree-sitter in Chromium (`test/e2e/browser.test.ts`) and in
|
|
103
|
+
Cloudflare Workers with and without Node.js compatibility (`test/e2e/workers.test.ts`).
|
|
104
|
+
|
|
105
|
+
CI also runs these tests on Linux arm64 and macOS, where the Rust binding compiles the parser natively, and fuzzes the
|
|
106
|
+
parser with libFuzzer and sanitizers (`.github/workflows/robustness.yml`).
|
|
107
|
+
|
|
108
|
+
### References
|
|
109
|
+
|
|
110
|
+
- The grammar was originally adapted from [this C99 grammar](http://slps.github.io/zoo/c/iso-9899-tc3.html).
|
|
111
|
+
- [C23 working draft N3220](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf) and
|
|
112
|
+
[C11 working draft N1570](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf).
|