oak-csharp 0.0.0

High-performance incremental C# parser for the oak ecosystem with flexible configuration, supporting modern .NET features and managed programming.
Documentation

Oak C# Parser

Crates.io Documentation

High-performance incremental C# parser for the oak ecosystem with flexible configuration, optimized for code analysis and compilation.

🎯 Overview

Oak C# is a robust parser for C#, designed to handle complete C# syntax including modern features. Built on the solid foundation of oak-core, it provides both high-level convenience and detailed AST generation for static analysis and code generation.

✨ Features

  • Complete C# Syntax: Supports all C# features including modern specifications
  • Full AST Generation: Generates comprehensive Abstract Syntax Trees
  • Lexer Support: Built-in tokenization with proper span information
  • Error Recovery: Graceful handling of syntax errors with detailed diagnostics

🚀 Quick Start

Basic example:

use oak_csharp::{Parser, CSharpLanguage, SourceText};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let parser = Parser::new();
    let source = SourceText::new(r#"
using System;

class Program {
    static void Main() {
        Console.WriteLine("Hello, C#!");
    }
}
    "#);
    
    let result = parser.parse(&source);
    println!("Parsed C# successfully.");
    Ok(())
}

📋 Parsing Examples

Class Parsing

use oak_csharp::{Parser, CSharpLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new(r#"
public class Calculator {
    public int Add(int a, int b) {
        return a + b;
    }
    
    public int Subtract(int a, int b) {
        return a - b;
    }
}
"#);

let result = parser.parse(&source);
println!("Class parsed successfully.");

Interface Parsing

use oak_csharp::{Parser, CSharpLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new(r#"
public interface IDrawable {
    void Draw();
    double Area { get; }
}

public class Circle : IDrawable {
    public double Radius { get; set; }
    
    public void Draw() {
        Console.WriteLine($"Drawing circle with radius {Radius}");
    }
    
    public double Area => Math.PI * Radius * Radius;
}
"#);

let result = parser.parse(&source);
println!("Interface parsed successfully.");

🔧 Advanced Features

Token-Level Parsing

use oak_csharp::{Parser, CSharpLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new("int x = 42;");
let result = parser.parse(&source);
// Token information is available in the parse result

Error Handling

use oak_csharp::{Parser, CSharpLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new(r#"
public class BrokenClass {
    public void Method() {
        Console.WriteLine("Hello")
    // Missing closing brace
"#);

let result = parser.parse(&source);
if let Err(e) = result.result {
    println!("Parse error: {:?}", e);
}

🏗️ AST Structure

The parser generates a comprehensive AST with the following main structures:

  • CompilationUnit: Root container for C# programs
  • ClassDeclaration: C# class definitions
  • InterfaceDeclaration: Interface definitions
  • MethodDeclaration: Method declarations
  • PropertyDeclaration: Property declarations
  • Statement: Various statement types
  • Expression: Various expression types

📊 Performance

  • Streaming: Parse large C# files without loading entirely into memory
  • Incremental: Re-parse only changed sections
  • Memory Efficient: Smart AST node allocation
  • Fast Recovery: Quick error recovery for better IDE integration

🔗 Integration

Oak C# integrates seamlessly with:

  • Static Analysis: Code quality and security analysis
  • Code Generation: Generating code from C# AST
  • IDE Support: Language server protocol compatibility
  • Refactoring: Automated code refactoring
  • Documentation: Generating documentation from C# code

📚 Examples

Check out the examples directory for comprehensive examples:

  • Complete C# program parsing
  • Class and interface analysis
  • Code transformation
  • Integration with development workflows

🤝 Contributing

Contributions are welcome!

Please feel free to submit pull requests at the project repository or open issues.