willbooster-tree-sitter-c-sharp 38.2.1

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

Syntax trees

Every is test uses is_pattern_expression. A name that could also be a constant, whether simple, generic, or dotted (x is T, x is List<int>, x is System.String), is a constant_pattern; other types (x is int, x is T?, x is T[], x is global::T) are a type_pattern. A conditional_access_expression holds its receiver and the member or element binding after ?; a member access, invocation, or element access that follows applies to that conditional_access_expression. The contextual type names nint and nuint use identifier nodes.

Support Scope

This fork maintains only full parsing, optimized for binary size and parse speed: valid code as found in real-world projects yields a correct tree, rare valid syntax whose support would noticeably grow the parser may instead yield ERROR or MISSING nodes, and valid code never yields an incorrect tree without them. Any input parses without crashing, hanging, or taking more than linear time. Invalid input may parse without ERROR or MISSING nodes. Incremental parsing is not supported, and no queries (highlights, locals, injections, tags) are shipped. Node types, field names, and the trees that error recovery produces may change; such changes are released as new major versions.

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. The parser uses WillBooster ABI 0x5742000e, which upstream Tree-sitter runtimes and pre-23.1.0 WillBooster runtimes reject. Require @willbooster/web-tree-sitter 26.3.0 through 28.x. The package tests use 28.0.0. Install the runtime alongside the grammar when using the Wasm parser; it remains an optional peer for consumers that only use the grammar source.

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 cSharpUrl from '@willbooster/tree-sitter-c-sharp/tree-sitter-c_sharp.wasm?url';
import { Language, Parser } from '@willbooster/web-tree-sitter';
import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.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 cSharp from '@willbooster/tree-sitter-c-sharp/tree-sitter-c_sharp.wasm';
import { Language, Parser } from '@willbooster/web-tree-sitter';
import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';

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

The package also ships the node types in src/node-types.json. When extending the source grammar, use grammar.js and the files under src/ from the same repository release tag. Their external tokens and serialized scanner state must match.

In Rust, depend on the crate and on willbooster-tree-sitter, the full-parse runtime this package is tested and fuzzed with. ABI 0x5742000e rejects upstream's tree-sitter crate and pre-23.1.0 WillBooster runtimes. Require willbooster-tree-sitter 26.3.0 through 28.x:

cargo add willbooster-tree-sitter@28.0.0 --rename tree-sitter
cargo add willbooster-tree-sitter-c-sharp --rename tree-sitter-c-sharp
let mut parser = tree_sitter::Parser::new();
parser.set_language(&tree_sitter_c_sharp::LANGUAGE.into())?;

Development

mise install
bun install --frozen-lockfile
bun playwright install chromium
bun run build/ci
bun run verify
bun run test
cargo test

References