zesterer / chumsky

Write expressive, high-performance parsers with ease.
https://crates.io/crates/chumsky
MIT License
3.63k stars 155 forks source link

Pratt parsing with non-associative operators #557

Closed rmehri01 closed 1 year ago

rmehri01 commented 1 year ago

Hey there, thanks for this crate, it's really nice to work with :)

I was wondering if it's possible to parse non-associative operators with the pratt parsing feature, for example to turn x == y == z into x == y && y == z or have a parse error instead?

zesterer commented 1 year ago

Hmm, interesting one. I would say that pratt parsing probably isn't cut out for this sort of thing due to the way binding power allows the parser to 'slide' between operator levels and hence requires atoms to have a homogeneous type (so you can't, say, build up one specific operator as a list instead of a tree).

If you chose to not use pratt parsing, the following should work:

enum Expr {
    ...
    Eq(Vec<Expr>),
    ...
}

atom
    .separated_by(eq_op)
    .at_least(1)
    .collect::<Vec<_>>()
    .map(|terms| Expr::Eq(terms))
rmehri01 commented 1 year ago

Ah okay, that makes sense, thanks!