C# grammar for tree-sitter based on an export of the Roslyn grammar export with various 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
Comprehensive support for C# exists with the following exceptions:
-
async
,var
andawait
cannot be used as identifiers
- Tuples
- Discards
- Basic pattern matching
- Recursive pattern matching
- Ref locals and return
- Local function statements
- Expression bodied constructors
- Expression bodied destructors/finalizers
- Throw expressions
- Binary and
_
literal support
-
async
main method - Default literals (as
default_expression
) - Inferred tuple element names
- Generic type pattern matching
-
in
parameter modifiers -
ref readonly
method returns -
readonly struct
-
ref struct
- Non-named trailing arguments
-
_
leading binary and hex literals -
private protected
modifier - Conditional
ref
expressions
-
stackalloc
array initializers -
unmanaged
generic type parameter constraint - Attributes attached to auto property backing fields
-
out
support for parameters on initializers, constructors etc.
-
readonly
members - Default interface methods
-
switch
expressions -
switch
property patterns -
switch
tuple patterns -
static
local functions - Nullable reference types
- Null-forgiving operator
- Null-coalescing assignment
- Covariant returns
- Extending partial methods
- Extension GetEnumerator
- Function pointers
- Init properties
- Lambda discard parameters
- Local function attributes
- Module initializers
- Native integers
- Patterns
- Simple type patterns
- Relational patterns
- Logical patterns
- Records
- Skip Locals Init
- Static anonymous functions
- Target-typed conditional expression
- Target-typed new
- Top-level statements
- Official C# 6 Language Spec provides chapters that formally define the language grammar.
- Roslyn C# language grammar export
- SharpLab (web-based syntax tree playground based on Roslyn)