oak-ocaml 0.0.0

OCaml functional programming language parser with support for modern OCaml features and type system.
Documentation

Oak OCaml Parser

Crates.io Documentation

High-performance incremental OCaml parser for the oak ecosystem with flexible configuration, optimized for functional programming language parsing and type system analysis.

🎯 Overview

Oak OCaml is a robust parser for OCaml, designed to handle complete OCaml syntax including modern features. Built on the solid foundation of oak-core, it provides both high-level convenience and detailed AST generation for functional programming language parsing and type system analysis.

✨ Features

  • Complete OCaml Syntax: Supports all OCaml 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_ocaml::{Parser, OCamlLanguage, SourceText};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let parser = Parser::new();
    let source = SourceText::new(r#"
let rec factorial n =
  if n <= 1 then 1
  else n * factorial (n - 1)

let () =
  let result = factorial 5 in
  Printf.printf "Factorial of 5 is %d\n" result
    "#);
    
    let result = parser.parse(&source);
    println!("Parsed OCaml successfully.");
    Ok(())
}

📋 Parsing Examples

Function Parsing

use oak_ocaml::{Parser, OCamlLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new(r#"
let rec map f = function
  | [] -> []
  | h :: t -> f h :: map f t
"#);

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

Module Parsing

use oak_ocaml::{Parser, OCamlLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new(r#"
module Stack = struct
  type 'a t = 'a list
  
  let empty = []
  let push x s = x :: s
  let pop = function
    | [] -> failwith "Empty stack"
    | h :: t -> (h, t)
end
"#);

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

Pattern Matching Parsing

use oak_ocaml::{Parser, OCamlLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new(r#"
type expr = 
  | Const of int
  | Add of expr * expr
  | Mul of expr * expr

let rec eval = function
  | Const n -> n
  | Add (e1, e2) -> eval e1 + eval e2
  | Mul (e1, e2) -> eval e1 * eval e2
"#);

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

🔧 Advanced Features

Token-Level Parsing

use oak_ocaml::{Parser, OCamlLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new("let x = 42");
let result = parser.parse(&source);
println!("Token parsing completed.");

Error Handling

use oak_ocaml::{Parser, OCamlLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new(r#"
let broken_function x =
  if x > 0 then
    x + 1
  else
    x + 2
  // Missing 'in' part in let binding
"#);

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:

  • OCamlSource: Root container for OCaml source files
  • Module: OCaml module definitions and structures
  • Expression: Various expression types including functions and literals
  • Pattern: Pattern matching expressions
  • Type: Type definitions and annotations
  • Declaration: Value and type declarations
  • Signature: Module signatures and interfaces

📊 Performance

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

  • IDE Support: Language server protocol compatibility for OCaml
  • Static Analysis: Type checking and code analysis tools
  • Code Generation: Generating code from OCaml AST
  • Documentation: Extracting documentation from OCaml source
  • Refactoring: Automated code refactoring tools

📚 Examples

Check out the examples directory for comprehensive examples:

  • Complete OCaml module parsing
  • Functional programming patterns analysis
  • Type system validation
  • Integration with development workflows

🤝 Contributing

Contributions are welcome!

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