Skip to main content

parse

Function parse 

Source
pub fn parse(
    src: &str,
    pool: &ExprPool,
    symbols: &mut HashMap<String, ExprId>,
) -> Result<ExprId, ParseError>
Expand description

Parse a mathematical expression string into an ExprId.

Uses a Pratt (top-down operator precedence) recursive-descent parser. The grammar supports integer/float literals, identifiers, arithmetic operators (+, -, *, /, ^, **), unary -/+, parentheses, and a fixed set of mathematical functions: sin, cos, tan, sinh, cosh, tanh, asin, acos, atan, atan2, exp, log, sqrt, abs, sign, floor, ceil, round, erf, erfc, gamma.

symbols maps identifier names to pre-existing ExprIds. Identifiers not in the map are interned as new Domain::Real symbols and added to the map so they are reused within the same call.

§Errors

Returns ParseError (E-PARSE-001 lexical, E-PARSE-002 syntactic, E-PARSE-003 unknown function) on failure, with a byte-offset span.

§Example

use alkahest_cas::{ExprPool, parse};
use alkahest_cas::kernel::Domain;
use std::collections::HashMap;

let pool = ExprPool::new();
let x = pool.symbol("x", Domain::Real);
let mut syms = HashMap::from([("x".to_owned(), x)]);
let e = parse("sin(x)^2 + cos(x)^2", &pool, &mut syms).unwrap();