RtkSharp.TreeSitter.Trimmed

Size-trimmed redistribution of TreeSitter.DotNet 1.3.0 (MIT, upstream commit 8cae484bc033dac6e492ed15166877f3d784850f). Managed assembly and build props are untouched; native grammar libraries are pruned to only the set RtkSharp registers: c, cpp, go, java, python, ruby, rust, bash, typescript. See third-party/treesitter-dotnet-trimmed/README.md for provenance and how to regenerate this package.


Keywords
bindings, csharp, dotnet, parser, tree-sitter, treesitter, trimmed, c-sharp
License
MIT
Install
Install-Package RtkSharp.TreeSitter.Trimmed -Version 1.3.0-rtksharp.1

Documentation

.NET bindings for tree-sitter parsing library

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.

Key Features

  • .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.

How to Use

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

Installation

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:

  1. The tree-sitter .NET bindings (TreeSitter.dll)
  2. The native tree-sitter parsing library (tree-sitter.[dll/so])
  3. 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.

Included runtime libraries

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:

Using your own language grammars

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");

Development

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.

Building .NET bindings for Windows

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 Release

The 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.

Building .NET bindings for Linux

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 Release

The 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.

Additional Documentation

Feedback & Contributing

.NET bindings for tree-sitter is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.