Provides .NET bindings for the tree-sitter parsing library. Also includes the native tree-sitter parsing library and a complete set of native language parsing libraries.
- .NET bindings for the tree-sitter parsing library.
- Includes native libraries for the tree-sitter parsing library and language grammars.
- Includes 28+ language grammars.
- Work with all .NET languages such as C#, F#, and VB.NET.
- Work with Windows (x86, x64, arm64), Linux (x86, x64, arm, arm64), and macOS (x64, arm64).
- Support for predicates queries.
- Passes the WebAssembly bindings test suite.
Parsing source code:
using TreeSitter;
using var language = new Language("JavaScript");
using var parser = new Parser(language);
using var tree = parser.Parse("console.log('Hello World');")!;
Console.WriteLine($"Root node: {tree.RootNode}");Expected output:
Root node: (program (expression_statement (call_expression function: (member_expression object: (identifier) property: (property_identifier)) arguments: (arguments (string (string_fragment))))))
Running queries:
using TreeSitter;
using var language = new Language("JavaScript");
using var parser = new Parser(language);
using var tree = parser.Parse("function one() { function two() {} }")!;
using var query = new Query(language, "(function_declaration name: (identifier) @fn)");
foreach (var capture in query.Execute(tree.RootNode).Captures)
{
Console.WriteLine($"Found function: {capture.Node.Text}");
}Expected output:
Found function: one
Found function: two
To install the .NET bindings for tree-sitter, add the NuGet package TreeSitter.DotNet to your .NET project.
The NuGet package TreeSitter.DotNet consists of three components:
- The tree-sitter .NET bindings (TreeSitter.dll)
- The native tree-sitter parsing library (tree-sitter.[dll/so])
- A number of native language parser DLLs (e.g. tree-sitter-c.[dll/so])
The NuGet package includes all DLLs and shared objects for the supported platforms and languages.
The NuGet package included pre-built native libraries for the following runtime identifier (RIDs):
- win-x64
- win-x86
- win-arm64
- linux-x64
- linux-x86
- linux-arm
- linux-arm64
- osx-x64
- osx-arm64
and the following projects:
- Tree-sitter parsing library
- Agda grammar
- Bash grammar
- C grammar
- C++ grammar
- C# grammar
- CSS grammar
- Embedded template languages like ERB, EJS grammar
- Go grammar
- Haskell grammar
- HTML grammar
- Java grammar
- JavaScript grammar
- JSDoc grammar
- JSON grammar
- Julia grammar
- OCaml grammar
- PHP grammar
- Python grammar
- CodeQL grammar
- Rust grammar
- Razor grammar
- Ruby grammar
- Rust grammar
- Scala grammar
- Swift grammar
- TOML grammar
- Tree-sitter query grammar
- TypeScript grammar
- SystemVerilog grammar
To use your own language grammar, you need to first build the native language parser library for your grammer. Ensure that the native library is built for the platform and processor architecture you are targeting.
You can then load the native library using the Language(string library, string function) constructor.
Place the native library in your application's output folder and call the Language constructor with the library file name
and the exported function name to load.
Typically, the library name is of the form tree-sitter-<language>.dll and the exported function name is of the form tree_sitter_<language>,
where <language> is the lower-case name of the programming language.
For example, to create a Language instance for the native C language parser library named tree-sitter-c.dll, you can call:
var language = new Language("tree-sitter-c.dll", "tree_sitter_c");which is equivalent to:
var language = new Language("C");The tree-sitter .NET bindings package consists of two projects:
- The tree-sitter .NET bindings
- The native tree-sitter libraries
If you want to build the tree-sitter .NET bindings, you need to build the native tree-sitter libraries first.
Building the native tree-sitter libraries is currently supported only for Windows x64 and Linux x64.
To build the tree-sitter libraries for Windows open a Developer Command Prompt for VS 2022 and run the following commands:
cd tree-sitter-native
msbuild tree-sitter-native.sln /p:Configuration=Release /p:Platform=x64
cd ..\src
dotnet build --runtime win-x64 --configuration ReleaseThe native tree-sitter libraries will be placed in the folder /build/runtimes/win-x64/native
and automatically copied to your .NET bin folder when you build TreeSitter.csproj.
To build the tree-sitter libraries for Linux open a shell and run the following commands:
cd tree-sitter-native
make
cd ../src
dotnet build --runtime linux-x64 --configuration ReleaseThe native tree-sitter libraries will be built in the folder /build/runtimes/linux-x64/native
and automatically copied to your .NET bin folder when you build TreeSitter.csproj.
.NET bindings for tree-sitter is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.