@willbooster/tree-sitter-c-sharp
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,#endifblocks - 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:
varandawaitcannot 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 from 'node:url';
import from '@willbooster/web-tree-sitter';
await ;
const parser = ;
const wasmPath = ;
parser.;
const tree = parser.;
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 from '@willbooster/web-tree-sitter';
import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
await ;
const parser = ;
parser.;
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 from '@willbooster/web-tree-sitter';
import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
await ;
const parser = ;
parser.;
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:
let mut parser = new;
parser.set_language?;
Development
- Parser generation and validation: read before changing grammar inputs, runtime versions, or examples.
References
- C# language specification
- Official C# 8 Draft Language Spec provides chapters that formally define the language grammar.
- Roslyn C# language grammar export
- SharpLab (web-based syntax tree playground based on Roslyn)