oak-wolfram

Crates.iooak-wolfram
lib.rsoak-wolfram
version0.0.1
created_at2025-10-21 13:39:07.210205+00
updated_at2026-01-23 05:37:56.8662+00
descriptionWolfram Language parser with support for Mathematica syntax and symbolic computation.
homepagehttps://github.com/ygg-lang/oaks
repositoryhttps://github.com/ygg-lang/oaks
max_upload_size
id1893809
size70,898
publisher (github:ygg-lang:publisher)

documentation

https://docs.rs/oak-wolfram

README

Oak Wolfram Parser

Crates.io Documentation

High-performance incremental Wolfram Language parser for the oak ecosystem with flexible configuration, optimized for mathematical computation and symbolic analysis.

🎯 Overview

Oak Wolfram is a robust parser for the Wolfram Language, designed to handle complete Wolfram syntax including mathematical expressions, symbolic computations, and functional programming constructs. Built on the solid foundation of oak-core, it provides both high-level convenience and detailed AST generation for mathematical analysis and code generation.

✨ Features

  • Complete Wolfram Syntax: Supports all Wolfram Language features including mathematical expressions
  • 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_core::{Parser, SourceText, parser::session::ParseSession};
use oak_wolfram::{WolframParser, WolframLanguage};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut session = ParseSession::<WolframLanguage>::default();
    let parser = WolframParser::new();
    let source = SourceText::new(r#"
        f[x_] := x^2 + 2*x + 1
        Plot[f[x], {x, -10, 10}]
    "#);
    
    let result = parser.parse(&source, &[], &mut session);
    println!("Parsed Wolfram successfully.");
    Ok(())
}

📋 Parsing Examples

Function Definition Parsing

use oak_core::{Parser, SourceText, parser::session::ParseSession};
use oak_wolfram::{WolframParser, WolframLanguage};

let mut session = ParseSession::<WolframLanguage>::default();
let parser = WolframParser::new();
let source = SourceText::new(r#"
    factorial[n_] := If[n <= 1, 1, n * factorial[n - 1]]
"#);

let result = parser.parse(&source, &[], &mut session);
println!("Function parsed successfully.");

Expression Parsing

use oak_core::{Parser, SourceText, parser::session::ParseSession};
use oak_wolfram::{WolframParser, WolframLanguage};

let mut session = ParseSession::<WolframLanguage>::default();
let parser = WolframParser::new();
let source = SourceText::new(r#"
    Integrate[Sin[x], {x, 0, Pi}]
"#);

let result = parser.parse(&source, &[], &mut session);
println!("Expression parsed successfully.");

🔧 Advanced Features

Token-Level Parsing

use oak_core::{Parser, SourceText, parser::session::ParseSession};
use oak_wolfram::{WolframParser, WolframLanguage};

let mut session = ParseSession::<WolframLanguage>::default();
let parser = WolframParser::new();
let source = SourceText::new("f[x_] := x^2");
let result = parser.parse(&source, &[], &mut session);
println!("Token parsing completed.");

Error Handling

use oak_core::{Parser, SourceText, parser::session::ParseSession};
use oak_wolfram::{WolframParser, WolframLanguage};

let mut session = ParseSession::<WolframLanguage>::default();
let parser = WolframParser::new();
let source = SourceText::new(r#"
    f[x_ := x^2 + 1
    (* Missing closing bracket *)
"#);

let result = parser.parse(&source, &[], &mut session);
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:

  • Expression: Mathematical and symbolic expressions
  • FunctionDefinition: Function definitions with patterns
  • Rule: Rewrite rules and transformations
  • List: Ordered collections of expressions
  • Symbol: Atomic symbols and identifiers

📊 Performance

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

  • Mathematical Computation: Symbolic mathematics and calculus
  • Code Generation: Generating code from Wolfram expressions
  • IDE Support: Language server protocol compatibility
  • Educational Tools: Mathematical expression parsing for learning platforms
  • Scientific Computing: Parsing and analyzing mathematical models

📚 Examples

Check out the examples directory for comprehensive examples:

  • Complete Wolfram expression parsing
  • Function and rule analysis
  • Mathematical expression transformation
  • Integration with development workflows

🤝 Contributing

Contributions are welcome!

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

Commit count: 80

cargo fmt