willbooster-tree-sitter-c-sharp 3.0.2

C# grammar for tree-sitter
Documentation

@willbooster/tree-sitter-c-sharp

npm version license Test Test rust semantic-release wbfy crates.io

C# grammar for tree-sitter, forked from tree-sitter/tree-sitter-c-sharp. We are grateful to its authors and contributors. This is not an official release of that project.

This fork fixes parsing bugs and raises conformance with the C# language specification.

The grammar is based upon the Roslyn grammar with changes in order to:

  • Deal with differences between the parsing technologies
  • Work around some bugs in that grammar
  • Handle #if, #else, #elif, #endif blocks
  • Support syntax highlighting/parsing of fragments
  • Simplify the output tree
  • Reduce parser state count and complexity
  • Be in-line with tree-sitter's convention where applicable

Known gaps:

  • var and await cannot be used as identifiers everywhere they are valid

Usage

The npm package ships tree-sitter-c_sharp.wasm for @willbooster/web-tree-sitter, which runs in Node.js, Bun, browsers, and Cloudflare Workers. Use runtime version 1.3.0 or later for the compact ABI 16 parser. Install the runtime alongside the grammar when using the Wasm parser; it remains an optional peer for consumers that only use the grammar source or queries.

In Node.js and Bun, load it from the package:

import { fileURLToPath } from 'node:url';
import { Language, Parser } from '@willbooster/web-tree-sitter';

await Parser.init();
const parser = new Parser();
const wasmPath = fileURLToPath(import.meta.resolve('@willbooster/tree-sitter-c-sharp/tree-sitter-c_sharp.wasm'));
parser.setLanguage(await Language.load(wasmPath));
const tree = parser.parse('class Program {}\n');

In browsers, serve both .wasm files and load them by URL. With Vite:

import { Language, Parser } from '@willbooster/web-tree-sitter';
import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
import cSharpUrl from '@willbooster/tree-sitter-c-sharp/tree-sitter-c_sharp.wasm?url';

await Parser.init({ locateFile: () => runtimeUrl });
const parser = new Parser();
parser.setLanguage(await Language.load(cSharpUrl));

In Cloudflare Workers, which do not allow compiling Wasm at run time, import both .wasm files as modules (Wrangler compiles them at build time) and pass them to Parser.init and Language.load. This works with and without the nodejs_compat flag:

import { Language, Parser } from '@willbooster/web-tree-sitter';
import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
import cSharp from '@willbooster/tree-sitter-c-sharp/tree-sitter-c_sharp.wasm';

await Parser.init({ wasmModule: runtime });
const parser = new Parser();
parser.setLanguage(await Language.load(cSharp));

The package also ships the queries in queries/ and the node types in src/node-types.json.

In Rust, depend on the crate and on willbooster-tree-sitter, the runtime this package is tested and fuzzed with. The compact ABI 16 parser requires runtime 1.3.0 or later:

[dependencies]
tree-sitter = { package = "willbooster-tree-sitter", version = "1.3.0" }
tree-sitter-c-sharp = { package = "willbooster-tree-sitter-c-sharp", version = "3" }
let mut parser = tree_sitter::Parser::new();
parser.set_language(&tree_sitter_c_sharp::LANGUAGE.into())?;

Development

mise install
bun install --frozen-lockfile
bun run build/ci
bun run test
script/parse-examples
cargo test

script/tree-sitter (also bun run tree-sitter) runs the tree-sitter CLI of WillBooster/tree-sitter at the runtime version locked in Cargo.lock, so the parser is generated, built, tested, and fuzzed with the generator and the runtime this package ships. The first run downloads that CLI from its GitHub Release into .tmp/, or builds it with cargo (with the cmake that mise.toml pins) when the download fails or the release has no binary that runs here. The tree-sitter-cli package provides only the types of the grammar DSL that grammar.js checks against; its tree-sitter binary is upstream's.

bun run generate records a fresh ABI 16 generation profile from the applicable test/corpus cases and Git-tracked files in examples/, then generates compact parser tables. The parser also embeds metadata from tree-sitter.json. After changing a grammar, tree-sitter.json, a corpus case, or a tracked example, regenerate and commit src/. Stage added or removed examples with git add -A examples before generation so the profile uses the intended file list. Profiles in .tmp/generation-profiles/ are temporary and must not be committed. bun run build-wasm, bun run build/ci, and the release build regenerate the parsers before compiling them.

The tracked examples/JsonWriter.cs and examples/JsonSerializerInternalWriter.cs are verbatim MIT-licensed snapshots from Newtonsoft.Json commit 52fa3aef1f2cadcd3a3f874251eddc98d3efbbaa, under Src/Newtonsoft.Json/ and its Serialization/ directory respectively. Refresh these training inputs independently of the clone pins in script/parse-examples so changing a test checkout does not change profile training.

bun run test runs:

  • the corpus in test/corpus, with the native build and with the Wasm build (the first run downloads the WASI SDK);
  • an incremental-parsing check (test/unit/incremental.test.ts): script/fuzz-corpus runs tree-sitter fuzz, which edits each corpus case at random, reparses it, undoes the edits, and reparses again. TREE_SITTER_SEED, TREE_SITTER_ITERATIONS, and TREE_SITTER_EDITS run other or more edits;
  • a check that the C# files under examples/ that fail to parse are exactly those listed in script/known-failures.txt. Inputs include Git-tracked generation examples and pinned clones. The first run fetches the pinned repositories listed in script/parse-examples. After adding, removing, or changing an example, changing the grammar, or moving a pin, run script/parse-examples and review the failure-list diff before committing;
  • a performance check (test/unit/performance.test.ts) that recovering from an error on each line takes linear time (ten times the lines take about ten times the CPU time, under a ceiling), since consumers parse files while they are being edited. It loads the Wasm build through @willbooster/web-tree-sitter, which bun run build/ci rebuilds after regenerating the parser;
  • a check that package.json and Cargo.lock test the same runtime version (test/unit/runtimeVersion.test.ts);
  • checks that the Wasm build parses C# with @willbooster/web-tree-sitter in Chromium (test/unit/browser.test.ts, which needs Chromium installed once by bunx playwright install chromium) and in Cloudflare Workers with and without nodejs_compat (test/unit/workers.test.ts).

The tests and script/parse-examples compile the parser into .tmp/tree-sitter-lib rather than the CLI's cache shared by every checkout; script/fuzz-corpus builds a per-run parser in .tmp/fuzz and deletes it afterwards. mise.toml sets TREE_SITTER_LIBDIR to .tmp/tree-sitter-lib as well, so other tree-sitter commands run in the checkout use this checkout's parser too.

cargo test also replays edits that tree-sitter fuzz found to break incremental parsing on runtimes without the fixes of willbooster-tree-sitter.

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 on the willbooster-tree-sitter runtime locked in Cargo.lock (.github/workflows/robustness.yml).

References