tree-sitter-blazor 0.1.2

Tree-sitter grammar for Blazor/Razor syntax
Documentation

tree-sitter-blazor

Tree-sitter grammar for Blazor/Razor syntax (.razor and .cshtml files).

Overview

This crate provides a tree-sitter grammar for parsing Blazor and Razor template syntax, enabling code analysis, syntax highlighting, and tooling support for ASP.NET Blazor applications.

Features

  • Parse .razor and .cshtml files
  • Support for Blazor component syntax
  • HTML and C# code mixing
  • Directive parsing (@page, @code, @inject, etc.)
  • Component parameters and events

Usage

Add this to your Cargo.toml:

[dependencies]
tree-sitter-blazor = "0.1"
tree-sitter = "0.20"

Example

use tree_sitter::{Parser, Language};

extern "C" { fn tree_sitter_blazor() -> Language; }

fn main() {
    let mut parser = Parser::new();
    let language = unsafe { tree_sitter_blazor() };
    parser.set_language(&language).expect("Error loading Blazor grammar");
    
    let source_code = r#"
        @page "/counter"
        <h1>Counter</h1>
        <button @onclick="IncrementCount">Click me</button>
    "#;
    
    let tree = parser.parse(source_code, None).unwrap();
    println!("{}", tree.root_node().to_sexp());
}

Development

This grammar is part of the Coraline code graph indexer project.

Building

cargo build --release

Testing

cargo test

Grammar Definition

The grammar is defined in grammar.js and follows the tree-sitter grammar format. See the tree-sitter documentation for more information on creating and modifying grammars.

License

MIT - See LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or pull request on GitHub.