TypeScriptParser provides C# bindings for the Tree-sitter parsing library, specifically optimized for TypeScript language parsing. Through P/Invoke calls, C# developers can directly use the powerful Tree-sitter parsing functionality in the .NET environment.
- TypeScriptParser: Main library project containing complete Tree-sitter C# bindings and TypeScript parser
- TypeScriptParser.Tests: xUnit test project ensuring parser correctness
- Complete Tree-sitter API C# bindings
- Dedicated TypeScript language support
- Syntax tree traversal and querying
- Memory-safe resource management
- Support for incremental parsing and syntax tree editing
- .NET 9.0 or higher
- Windows/Linux/macOS (cross-platform support)
- x64 architecture
Pull the required submodules:
git submodule update --init --recursive
-
Build native dependency libraries:
cd tree-sitter make # Linux/macOS # or use nmake on Windows cd ..
-
Build the .NET project:
dotnet build
using TreeSitter.TypeScript; // Create parser instance using var parser = new TypeScriptParser(); // Parse TypeScript code string sourceCode = "const message: string = 'Hello, World!';"; using var tree = parser.ParseString(sourceCode); // Get root node var rootNode = tree.root_node(); // Create cursor for tree traversal using var cursor = parser.CreateCursor(tree);
using var parser = new TypeScriptParser(); string code = @" function greet(name: string): string { return `Hello, ${name}!`; } "; using var tree = parser.ParseString(code); using var cursor = parser.CreateCursor(tree); // Traverse syntax tree while (cursor.goto_first_child()) { var node = cursor.current_node(); Console.WriteLine($"Node: {node.type()}, Text: {node.text(code)}"); }
ParseString(string sourceCode): Parse string and return syntax treeCreateCursor(TSTree tree): Create syntax tree cursorLanguage: Get TypeScript language objectIsAvailable: Check if parser is available
TSTree: Syntax tree objectTSNode: Syntax tree nodeTSCursor: Syntax tree cursorTSLanguage: Language definitionTSParser: Underlying parser
Run unit tests:
dotnet testTests cover core parser functionality:
- Basic parsing functionality
- Error handling
- Resource management
- Syntax tree traversal
- Incremental parsing: Support for incremental updates to existing syntax trees
- Memory efficiency: Uses reference counting and automatic garbage collection
- High-speed parsing: Based on LR parsing algorithm
- Error recovery: Able to recover from syntax errors and continue parsing
This project is licensed under the MIT License. See the LICENSE file for details.
Issues and Pull Requests are welcome! Before contributing, please ensure:
- All tests run and pass
- Follow existing code style
- Add appropriate test cases for new features
- Language: C# (.NET 9.0)
- Test Framework: xUnit
- Native Interop: P/Invoke
- Package Management: NuGet
- Build System: MSBuild
For more information, visit the Tree-sitter official documentation