oak-r 0.0.0

R statistical programming language parser with support for data analysis, statistical computing, and graphics.
Documentation

Oak R Parser

Crates.io Documentation

High-performance incremental R parser for the oak ecosystem with flexible configuration, optimized for static analysis and code generation.

🎯 Overview

Oak R is a robust parser for R, designed to handle complete R 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 R Syntax: Supports all R 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_r::{Parser, RLanguage, SourceText};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let parser = Parser::new();
    let source = SourceText::new(r#"
print("Hello, World!")
x <- c(1, 2, 3, 4, 5)
mean(x)
    "#);
    
    let result = parser.parse(&source);
    println!("Parsed R successfully.");
    Ok(())
}

📋 Parsing Examples

Function Parsing

use oak_r::{Parser, RLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new(r#"
add <- function(a, b) {
  return(a + b)
}
"#);

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

Data Structure Parsing

use oak_r::{Parser, RLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new(r#"
data <- data.frame(
  name = c("Alice", "Bob", "Charlie"),
  age = c(25, 30, 35),
  city = c("New York", "London", "Paris")
)
"#);

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

🔧 Advanced Features

Token-Level Parsing

use oak_r::{Parser, RLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new("x <- c(1, 2, 3)");
let result = parser.parse(&source);
println!("Token parsing completed.");

Error Handling

use oak_r::{Parser, RLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new(r#"
# Invalid R code example
invalid_function(
"#);

let result = parser.parse(&source);
if let Some(errors) = result.result.err() {
    println!("Parse errors found: {:?}", errors);
} else {
    println!("Parsed successfully.");
}

🏗️ AST Structure

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

  • RProgram: Root container for R programs
  • Function: R functions and methods
  • Expression: R expressions and operations
  • DataFrame: R data structures
  • Vector: R vector operations

📊 Performance

  • Streaming: Parse large R 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 R integrates seamlessly with:

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

📚 Examples

Check out the examples directory for comprehensive examples:

  • Complete R program parsing
  • Function and data 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.