Skip to main content

Crate oak_ruby

Crate oak_ruby 

Source
Expand description

ยง๐Ÿ› ๏ธ Ruby Parser Developer Guide

This guide is designed to help you quickly get started with developing and integrating oak-ruby.

ยง๐Ÿšฆ Quick Start

Add the dependency to your Cargo.toml:

[dependencies]
oak-ruby = { path = "..." }

ยงBasic Parsing Example

The following is a standard workflow for parsing Ruby code, including support for classes, methods, and blocks:

use oak_ruby::{RubyParser, SourceText, RubyLanguage};

fn main() {
    // 1. Prepare source code
    let code = r#"
        class Greeter
          def initialize(name)
            @name = name
          end

          def greet
            puts "Hello, #{@name}!"
          end
        end

        greeter = Greeter.new("Oak")
        greeter.greet
    "#;
    let source = SourceText::new(code);

    // 2. Initialize parser
    let config = RubyLanguage::new();
    let parser = RubyParser::new(&config);

    // 3. Execute parsing
    let result = parser.parse(&source);

    // 4. Handle results
    if result.is_success() {
        println!("Parsing successful! AST node count: {}", result.node_count());
    } else {
        eprintln!("Errors found during parsing.");
        for diag in result.diagnostics() {
            println!("[{}:{}] {}", diag.line, diag.column, diag.message);
        }
    }
}

ยง๐Ÿ” Core API Usage

ยง1. Syntax Tree Traversal

After a successful parse, you can use the built-in visitor pattern or manually traverse the Green/Red Tree to extract Ruby specific constructs like class definitions, method bodies, block expressions, and complex literals.

ยง2. Incremental Parsing

Ruby codebases (especially Rails apps) can be massive. oak-ruby supports sub-millisecond incremental updates:

// Re-parse only the modified section
let new_result = parser.reparse(&new_source, &old_result);

ยง3. DSL and Metaprogramming

The parser is designed to handle Rubyโ€™s flexible nature, providing high-fidelity trees for code that heavily uses DSLs or dynamic method calls.

ยง๐Ÿ—๏ธ Architecture Overview

  • Lexer: Tokenizes Ruby source text, including support for complex string interpolations, heredocs, and various percent literals.
  • Parser: A high-performance syntax analyzer that handles Rubyโ€™s expression-heavy syntax and complex grammar rules.
  • AST: A strongly-typed, lossless syntax tree that preserves all trivia (comments/whitespace) for refactoring and formatting tools.

ยง๐Ÿ”— Advanced Resources

  • Full Examples: Check the examples/ folder in the project root.
  • API Documentation: Run cargo doc --open for detailed type definitions.
  • Test Cases: See tests/readme.md for handling of various Ruby versions and edge cases. Ruby support for the Oak language framework.

Re-exportsยง

pub use crate::ast::RubyRoot;
pub use crate::builder::RubyBuilder;
pub use crate::language::RubyLanguage;
pub use crate::lexer::RubyLexer;
pub use crate::parser::RubyParser;
pub use crate::lsp::highlighter::RubyHighlighter;
pub use crate::lsp::RubyLanguageService;
pub use crate::lsp::formatter::RubyFormatter;
pub use crate::mcp::serve_ruby_mcp;
pub use lexer::token_type::RubyTokenType;
pub use parser::element_type::RubyElementType;

Modulesยง

ast
AST module.
builder
Builder module.
language
Type definitions module. Language configuration module.
lexer
Lexer module.
lsp
LSP module.
mcp
MCP module.
parser
Parser module.