pub struct Parser<'a> { /* private fields */ }
👎Deprecated: Use
compile()
and Environment
insteadExpand description
Main struct for parsing and evaluating expr programs
Example:
use expr::{Context, Parser};
let ctx = Context::from_iter([("foo", 1), ("bar", 2)]);
let p = Parser::new();
assert_eq!(p.eval("foo + bar", &ctx).unwrap().to_string(), "3");
Implementations§
Source§impl<'a> Parser<'a>
impl<'a> Parser<'a>
Sourcepub fn add_function<F>(&mut self, name: &str, f: F)
pub fn add_function<F>(&mut self, name: &str, f: F)
Add a function for expr programs to call
Example:
use std::collections::HashMap;
use expr::{Context, Parser, Value};
let mut p = Parser::new();
let ctx = Context::default();
p.add_function("add", |c| {
let mut sum = 0;
for arg in c.args {
if let Value::Number(n) = arg {
sum += n;
} else {
panic!("Invalid argument: {arg:?}");
}
}
Ok(sum.into())
});
assert_eq!(p.eval("add(1, 2, 3)", &ctx).unwrap().to_string(), "6");
Sourcepub fn run(&self, program: Program, ctx: &Context) -> Result<Value>
pub fn run(&self, program: Program, ctx: &Context) -> Result<Value>
Run a compiled expr program
Sourcepub fn eval(&self, code: &str, ctx: &Context) -> Result<Value>
pub fn eval(&self, code: &str, ctx: &Context) -> Result<Value>
Compile and run an expr program in one step
Example:
use std::collections::HashMap;
use expr::{Context, Parser};
let p = Parser::default();
let ctx = Context::default();
assert_eq!(p.eval("1 + 2", &ctx).unwrap().to_string(), "3");
pub fn eval_expr(&self, ctx: &Context, node: Node) -> Result<Value>
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Parser<'a>
impl<'a> !RefUnwindSafe for Parser<'a>
impl<'a> Send for Parser<'a>
impl<'a> Sync for Parser<'a>
impl<'a> Unpin for Parser<'a>
impl<'a> !UnwindSafe for Parser<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more