Skip to content

Commit

Permalink
Improved pratt parser type inference by removing fold overloading
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Barretto committed Oct 22, 2024
1 parent 115d433 commit 69065aa
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 183 deletions.
30 changes: 9 additions & 21 deletions examples/mini_ml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,29 +105,17 @@ fn parser<'src>(
))
.pratt((
// Multiply
infix(
left(10),
just(Token::Asterisk),
|x, _, y, e: &mut MapExtra<'src, '_, _, _>| {
(Expr::Mul(Box::new(x), Box::new(y)), e.span())
},
),
infix(left(10), just(Token::Asterisk), |x, _, y, e| {
(Expr::Mul(Box::new(x), Box::new(y)), e.span())
}),
// Add
infix(
left(9),
just(Token::Plus),
|x, _, y, e: &mut MapExtra<'src, '_, _, _>| {
(Expr::Add(Box::new(x), Box::new(y)), e.span())
},
),
infix(left(9), just(Token::Plus), |x, _, y, e| {
(Expr::Add(Box::new(x), Box::new(y)), e.span())
}),
// Calls
infix(
left(1),
empty(),
|x, _, y, e: &mut MapExtra<'src, '_, _, _>| {
(Expr::Call(Box::new(x), Box::new(y)), e.span())
},
),
infix(left(1), empty(), |x, _, y, e| {
(Expr::Call(Box::new(x), Box::new(y)), e.span())
}),
))
})
}
Expand Down
11 changes: 5 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2130,7 +2130,6 @@ pub trait Parser<'a, I: Input<'a>, O, E: ParserExtra<'a, I> = extra::Default>:
/// ```
/// # use chumsky::prelude::*;
/// use chumsky::pratt::*;
/// use std::ops::{Neg, Mul, Div, Add, Sub};
///
/// let int = text::int::<_, _, extra::Err<Rich<char>>>(10)
/// .from_str()
Expand All @@ -2140,11 +2139,11 @@ pub trait Parser<'a, I: Input<'a>, O, E: ParserExtra<'a, I> = extra::Default>:
/// let op = |c| just(c).padded();
///
/// let expr = int.pratt((
/// prefix(2, op('-'), i64::neg),
/// infix(left(1), op('*'), i64::mul),
/// infix(left(1), op('/'), i64::div),
/// infix(left(0), op('+'), i64::add),
/// infix(left(0), op('-'), i64::sub),
/// prefix(2, op('-'), |_, x: i64, _| -x),
/// infix(left(1), op('*'), |x, _, y, _| x * y),
/// infix(left(1), op('/'), |x, _, y, _| x / y),
/// infix(left(0), op('+'), |x, _, y, _| x + y),
/// infix(left(0), op('-'), |x, _, y, _| x - y),
/// ));
///
/// // Pratt parsing can handle unary operators...
Expand Down
Loading

0 comments on commit 69065aa

Please sign in to comment.