Skip to content

Move the math expression parser from Pest to Chumsky and add more features #2685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 43 additions & 57 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion frontend/wasm/src/editor_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,6 @@ pub fn evaluate_math_expression(expression: &str) -> Option<f64> {
let value = math_parser::evaluate(expression)
.inspect_err(|err| error!("Math parser error on \"{expression}\": {err}"))
.ok()?
.0
.inspect_err(|err| error!("Math evaluate error on \"{expression}\": {err} "))
.ok()?;
let Some(real) = value.as_real() else {
Expand Down
3 changes: 1 addition & 2 deletions libraries/math-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ description = "Parser for Graphite style mathematics expressions"
license = "MIT OR Apache-2.0"

[dependencies]
pest = "2.7"
pest_derive = "2.7.11"
thiserror = "2.0"
lazy_static = "1.5"
num-complex = "0.4"
chumsky = { version = "0.10", default-features = false, features = ["std"] }

[dev-dependencies]
criterion = "0.5"
Expand Down
2 changes: 1 addition & 1 deletion libraries/math-parser/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ macro_rules! generate_benchmarks {

fn evaluation_bench(c: &mut Criterion) {
$(
let expr = ast::Node::try_parse_from_str($input).unwrap().0;
let expr = ast::Node::try_parse_from_str($input).unwrap();
let context = EvalContext::default();

c.bench_function(concat!("eval ", $input), |b| {
Expand Down
8 changes: 7 additions & 1 deletion libraries/math-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Unit {
}
}

#[derive(Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
pub enum Literal {
Float(f64),
Complex(Complex),
Expand All @@ -56,6 +56,11 @@ pub enum BinaryOp {
Mul,
Div,
Pow,
Leq,
Lt,
Geq,
Gt,
Eq,
}

#[derive(Debug, PartialEq, Clone, Copy)]
Expand All @@ -72,4 +77,5 @@ pub enum Node {
FnCall { name: String, expr: Vec<Node> },
BinOp { lhs: Box<Node>, op: BinaryOp, rhs: Box<Node> },
UnaryOp { expr: Box<Node>, op: UnaryOp },
Conditional { condition: Box<Node>, if_block: Box<Node>, else_block: Box<Node> },
}
Loading