@willbooster/tree-sitter-c-sharp 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 +124 -1
- package/grammar.js +1879 -0
- package/package.json +63 -2
- package/src/node-types.json +7422 -0
- package/tree-sitter-c_sharp.wasm +0 -0
- package/tree-sitter.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014-2023 Max Brunsfeld, Damien Guard, Amaan Qureshi, and contributors.
|
|
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,126 @@
|
|
|
1
1
|
# @willbooster/tree-sitter-c-sharp
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@willbooster/tree-sitter-c-sharp)
|
|
4
|
+
[](https://www.npmjs.com/package/@willbooster/tree-sitter-c-sharp)
|
|
5
|
+
[](https://github.com/WillBooster/tree-sitter-c-sharp/actions/workflows/test.yml)
|
|
6
|
+
[](https://github.com/WillBooster/tree-sitter-c-sharp/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-sharp)
|
|
10
|
+
|
|
11
|
+
C# grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
|
|
12
|
+
[tree-sitter/tree-sitter-c-sharp](https://github.com/tree-sitter/tree-sitter-c-sharp). 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 [C# language specification](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/readme).
|
|
16
|
+
|
|
17
|
+
The grammar is based upon the Roslyn grammar with changes in order to:
|
|
18
|
+
|
|
19
|
+
- Deal with differences between the parsing technologies
|
|
20
|
+
- Work around some bugs in that grammar
|
|
21
|
+
- Handle `#if`, `#else`, `#elif`, `#endif` blocks
|
|
22
|
+
- Support syntax highlighting/parsing of fragments
|
|
23
|
+
- Simplify the output tree
|
|
24
|
+
- Reduce parser state count and complexity
|
|
25
|
+
- Be in-line with tree-sitter's convention where applicable
|
|
26
|
+
|
|
27
|
+
Known gaps:
|
|
28
|
+
|
|
29
|
+
- `async`, `var` and `await` cannot be used as identifiers everywhere they are valid
|
|
30
|
+
- File-based apps preprocessor directives (`#:property`, `#:package`, `#:sdk`, `#:project`) are not yet recognized
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
The npm package ships `tree-sitter-c_sharp.wasm` for
|
|
35
|
+
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter), which runs in Node.js, Bun,
|
|
36
|
+
browsers, and Cloudflare Workers. In Node.js and Bun, load it from the package:
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
import { fileURLToPath } from 'node:url';
|
|
40
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
41
|
+
|
|
42
|
+
await Parser.init();
|
|
43
|
+
const parser = new Parser();
|
|
44
|
+
const wasmPath = fileURLToPath(import.meta.resolve('@willbooster/tree-sitter-c-sharp/tree-sitter-c_sharp.wasm'));
|
|
45
|
+
parser.setLanguage(await Language.load(wasmPath));
|
|
46
|
+
const tree = parser.parse('class Program {}\n');
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
In browsers, serve both `.wasm` files and load them by URL. With Vite:
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
53
|
+
import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
|
|
54
|
+
import cSharpUrl from '@willbooster/tree-sitter-c-sharp/tree-sitter-c_sharp.wasm?url';
|
|
55
|
+
|
|
56
|
+
await Parser.init({ locateFile: () => runtimeUrl });
|
|
57
|
+
const parser = new Parser();
|
|
58
|
+
parser.setLanguage(await Language.load(cSharpUrl));
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
In Cloudflare Workers, which do not allow compiling Wasm at run time, import both `.wasm` files as modules (Wrangler
|
|
62
|
+
compiles them at build time) and pass them to `Parser.init` and `Language.load`. This works with and without the
|
|
63
|
+
`nodejs_compat` flag:
|
|
64
|
+
|
|
65
|
+
```js
|
|
66
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
67
|
+
import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
|
|
68
|
+
import cSharp from '@willbooster/tree-sitter-c-sharp/tree-sitter-c_sharp.wasm';
|
|
69
|
+
|
|
70
|
+
await Parser.init({ wasmModule: runtime });
|
|
71
|
+
const parser = new Parser();
|
|
72
|
+
parser.setLanguage(await Language.load(cSharp));
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The package also ships the node types in `src/node-types.json`.
|
|
76
|
+
|
|
77
|
+
In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-c-sharp):
|
|
78
|
+
|
|
79
|
+
```toml
|
|
80
|
+
[dependencies]
|
|
81
|
+
tree-sitter = "0.27"
|
|
82
|
+
tree-sitter-c-sharp = { package = "willbooster-tree-sitter-c-sharp", version = "1" }
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
```rust
|
|
86
|
+
let mut parser = tree_sitter::Parser::new();
|
|
87
|
+
parser.set_language(&tree_sitter_c_sharp::LANGUAGE.into())?;
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Development
|
|
91
|
+
|
|
92
|
+
```sh
|
|
93
|
+
mise install
|
|
94
|
+
bun install --frozen-lockfile
|
|
95
|
+
bun run build/ci
|
|
96
|
+
bun run test
|
|
97
|
+
script/parse-examples
|
|
98
|
+
cargo test
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
`bun run test` runs:
|
|
102
|
+
|
|
103
|
+
- the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK);
|
|
104
|
+
- an incremental-parsing check (`test/unit/incremental.test.ts`): `tree-sitter fuzz` edits each corpus case at random,
|
|
105
|
+
reparses it, undoes the edits, and reparses again. `TREE_SITTER_SEED`, `TREE_SITTER_ITERATIONS`, and
|
|
106
|
+
`TREE_SITTER_EDITS` run other or more edits;
|
|
107
|
+
- a check that the real-world C# files cloned into `examples/` fail to parse exactly as listed in
|
|
108
|
+
`script/known-failures.txt`. The first run clones them. The example repositories are pinned to commits in
|
|
109
|
+
`script/parse-examples`. After a grammar change or a moved pin alters that list, `script/parse-examples` rewrites
|
|
110
|
+
it; review its diff before committing;
|
|
111
|
+
- a performance check (`test/unit/performance.test.ts`) that recovering from an error on each of 10,000 lines takes
|
|
112
|
+
linear time, since consumers parse files while they are being edited. It loads the Wasm build through
|
|
113
|
+
@willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after regenerating the parser;
|
|
114
|
+
- checks that the Wasm build parses C# with @willbooster/web-tree-sitter in Chromium (`test/unit/browser.test.ts`,
|
|
115
|
+
which needs Chromium installed once by `bunx playwright install chromium`) and in Cloudflare Workers with and without
|
|
116
|
+
`nodejs_compat` (`test/unit/workers.test.ts`).
|
|
117
|
+
|
|
118
|
+
CI also runs these tests on Linux arm64 and macOS, where the Rust binding compiles the parser natively, and fuzzes the parser with libFuzzer and sanitizers
|
|
119
|
+
(`.github/workflows/robustness.yml`).
|
|
120
|
+
|
|
121
|
+
### References
|
|
122
|
+
|
|
123
|
+
- [C# language specification](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/readme)
|
|
124
|
+
- [Official C# 8 Draft Language Spec](https://github.com/dotnet/csharpstandard/tree/draft-v8/standard) provides chapters that formally define the language grammar.
|
|
125
|
+
- [Roslyn C# language grammar export](https://github.com/dotnet/roslyn/blob/master/src/Compilers/CSharp/Portable/Generated/CSharp.Generated.g4)
|
|
126
|
+
- [SharpLab](https://sharplab.io) (web-based syntax tree playground based on Roslyn)
|