A Rust workspace of parsing and expression evaluation crates.
// Quick example: parse and evaluate a math expression
use shunting::{ShuntingParser, MathContext};
let expr = ShuntingParser::parse_str("sin(0.2)^2 + cos(0.2)^2").unwrap();
let result = MathContext::new().eval(&expr).unwrap();
println!("{}", result);| Crate | Description |
|---|---|
| lexers | Reusable tokenizers — numbers, identifiers, math, strings, lisp, quoted text |
| earlgrey | Earley CFG parser — ambiguous grammars, EBNF, custom ASTs, all parse trees |
| kronos | Time sequence computation — "the 3rd Monday of the month", intervals, shifts |
| fluxcap | Natural language time parsing — "next Tuesday at 3pm", "3 weeks ago" |
| shunting | Math expression evaluator — operators, functions, variables, random variables |
| lisp | Minimal Lisp interpreter — closures, special forms, REPL |
| lox | C-style scripting language — variables, functions, closures, classes |
Parse natural language time expressions:
use fluxcap::TimeMachine;
let tm = TimeMachine::new();
let results = tm.eval("next Tuesday", None).unwrap();
for r in results {
println!("{:?}", r);
}Build a grammar and parse custom syntax:
use earlgrey::{ParserBuilder, EbnfParser};
let grammar = EbnfParser::new().parse(r#"
expr := 'x' '+' 'y';
"#).unwrap();
let parser = ParserBuilder::new(grammar, "expr")
.terminal("x", |_| Some(1))
.terminal("y", |_| Some(2))
.build()
.unwrap();
for tree in parser.parse_all(&mut vec!["x", "+", "y"].into_iter()) {
println!("{:?}", tree);
}Compute time sequences:
use kronos::{TimeSeqSpec, TimeSpan};
use time::macros::datetime;
let reftime = datetime!(2024-06-01 12:00 UTC);
let mondays = TimeSeqSpec::weekday(1); // Monday
for day in mondays.future(reftime).take(3) {
println!("{:?}", day);
}Most crates ship with a runnable example or binary:
cargo run -p toxtools --bin numerica # Numerica REPL
cargo run -p toxtools --bin shunting # Math expression REPL
cargo run -p toxtools --bin fluxcap # Time expression parser
cargo run -p toxtools --bin lisp # Lisp REPL
cargo run -p lox # Lox interpreterSee each crate's README for detailed documentation.