Skip to content
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

Replace tuple implementation macros with typle #614

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 18 additions & 6 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ spin = { version = "0.9", features = ["once"], default-features = false, optiona
lexical = { version = "6.1.1", default-features = false, features = ["parse-integers", "parse-floats", "format"], optional = true }
either = { version = "1.8.1", optional = true }
serde = { version = "1.0", default-features = false, optional = true, features = ["derive"] }
typle = "0.9.6"
unicode-ident = "1.0.10"

[dev-dependencies]
Expand Down
205 changes: 107 additions & 98 deletions src/pratt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
//! );
//! ```

use typle::typle;

use super::*;

trait Operator<'a, I, O, E>
Expand Down Expand Up @@ -404,117 +406,124 @@ pub struct Pratt<Atom, Ops> {
pub(crate) ops: Ops,
}

macro_rules! impl_pratt_for_tuple {
() => {};
($head:ident $($X:ident)*) => {
impl_pratt_for_tuple!($($X)*);
impl_pratt_for_tuple!(~ $head $($X)*);
};
(~ $($X:ident)+) => {
#[allow(unused_variables, non_snake_case)]
impl<'a, Atom, $($X),*> Pratt<Atom, ($($X,)*)> {
#[inline]
fn pratt_go<M: Mode, I, O, E>(&self, inp: &mut InputRef<'a, '_, I, E>, min_power: u32) -> PResult<M, O>
where
I: Input<'a>,
E: ParserExtra<'a, I>,
Atom: Parser<'a, I, O, E>,
$($X: Operator<'a, I, O, E>),*
{
let pre_expr = inp.save();
let mut lhs = 'choice: {
let ($($X,)*) = &self.ops;

// Prefix unary operators
$(
if $X::IS_PREFIX {
match $X.op_parser().go::<M>(inp) {
Ok(op) => {
match recursive::recurse(|| self.pratt_go::<M, _, _, _>(inp, $X.associativity().left_power())) {
Ok(rhs) => break 'choice M::combine(op, rhs, |op, rhs| {
$X.fold_prefix(op, rhs, &mut MapExtra::new(pre_expr.offset(), inp))
}),
Err(()) => inp.rewind(pre_expr),
}
},
#[typle(Tuple for 1..=26)]
impl<'a, Atom, T: Tuple> Pratt<Atom, T> {
#[inline]
fn pratt_go<M: Mode, I, O, E>(
&self,
inp: &mut InputRef<'a, '_, I, E>,
min_power: u32,
) -> PResult<M, O>
where
I: Input<'a>,
E: ParserExtra<'a, I>,
Atom: Parser<'a, I, O, E>,
T<_>: Operator<'a, I, O, E>,
{
let pre_expr = inp.save();
let mut lhs = 'choice: {
// Prefix unary operators
for typle_index!(i) in 0..T::LEN {
if T::<{ i }>::IS_PREFIX {
let t = &self.ops[[i]];
match t.op_parser().go::<M>(inp) {
Ok(op) => {
match recursive::recurse(|| {
self.pratt_go::<M, _, _, _>(inp, t.associativity().left_power())
}) {
Ok(rhs) => {
break 'choice M::combine(op, rhs, |op, rhs| {
t.fold_prefix(
op,
rhs,
&mut MapExtra::new(pre_expr.offset(), inp),
)
})
}
Err(()) => inp.rewind(pre_expr),
}
}
)*

self.atom.go::<M>(inp)?
};

loop {
let ($($X,)*) = &self.ops;

let pre_op = inp.save();

// Postfix unary operators
$(
let assoc = $X.associativity();
if $X::IS_POSTFIX && assoc.right_power() >= min_power {
match $X.op_parser().go::<M>(inp) {
Ok(op) => {
lhs = M::combine(lhs, op, |lhs, op| {
$X.fold_postfix(lhs, op, &mut MapExtra::new(pre_expr.offset(), inp))
});
continue
},
Err(()) => inp.rewind(pre_op),
}
Err(()) => inp.rewind(pre_expr),
}
}
}

self.atom.go::<M>(inp)?
};

'start: loop {
let pre_op = inp.save();

// Postfix unary operators
for typle_index!(i) in 0..T::LEN {
let t = &self.ops[[i]];
let assoc = t.associativity();
if T::<{ i }>::IS_POSTFIX && assoc.right_power() >= min_power {
match t.op_parser().go::<M>(inp) {
Ok(op) => {
lhs = M::combine(lhs, op, |lhs, op| {
t.fold_postfix(lhs, op, &mut MapExtra::new(pre_expr.offset(), inp))
});
continue 'start;
}
)*

// Infix binary operators
$(
let assoc = $X.associativity();
if $X::IS_INFIX && assoc.left_power() >= min_power {
match $X.op_parser().go::<M>(inp) {
Ok(op) => match recursive::recurse(|| self.pratt_go::<M, _, _, _>(inp, assoc.right_power())) {
Ok(rhs) => {
lhs = M::combine(
M::combine(lhs, rhs, |lhs, rhs| (lhs, rhs)),
Err(()) => inp.rewind(pre_op),
}
}
}

// Infix binary operators
for typle_index!(i) in 0..T::LEN {
let t = &self.ops[[i]];
let assoc = t.associativity();
if T::<{ i }>::IS_INFIX && assoc.left_power() >= min_power {
match t.op_parser().go::<M>(inp) {
Ok(op) => match recursive::recurse(|| {
self.pratt_go::<M, _, _, _>(inp, assoc.right_power())
}) {
Ok(rhs) => {
lhs = M::combine(
M::combine(lhs, rhs, |lhs, rhs| (lhs, rhs)),
op,
|(lhs, rhs), op| {
t.fold_infix(
lhs,
op,
|(lhs, rhs), op| {
$X.fold_infix(lhs, op, rhs, &mut MapExtra::new(pre_expr.offset(), inp))
},
);
continue
rhs,
&mut MapExtra::new(pre_expr.offset(), inp),
)
},
Err(()) => inp.rewind(pre_op),
},
Err(()) => inp.rewind(pre_op),
);
continue 'start;
}
}
)*

inp.rewind(pre_op);
break;
Err(()) => inp.rewind(pre_op),
},
Err(()) => inp.rewind(pre_op),
}
}

Ok(lhs)
}
}

#[allow(unused_variables, non_snake_case)]
impl<'a, I, O, E, Atom, $($X),*> ParserSealed<'a, I, O, E> for Pratt<Atom, ($($X,)*)>
where
I: Input<'a>,
E: ParserExtra<'a, I>,
Atom: Parser<'a, I, O, E>,
$($X: Operator<'a, I, O, E>),*
{
fn go<M: Mode>(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult<M, O> {
self.pratt_go::<M, _, _, _>(inp, 0)
}

go_extra!(O);
inp.rewind(pre_op);
break;
}
};

Ok(lhs)
}
}

impl_pratt_for_tuple!(A_ B_ C_ D_ E_ F_ G_ H_ I_ J_ K_ L_ M_ N_ O_ P_ Q_ R_ S_ T_ U_ V_ W_ X_ Y_ Z_);
#[typle(Tuple for 1..=26)]
impl<'a, I, O, E, Atom, T: Tuple> ParserSealed<'a, I, O, E> for Pratt<Atom, T>
where
I: Input<'a>,
E: ParserExtra<'a, I>,
Atom: Parser<'a, I, O, E>,
T<_>: Operator<'a, I, O, E>,
{
fn go<M: Mode>(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult<M, O> {
self.pratt_go::<M, _, _, _>(inp, 0)
}

go_extra!(O);
}

#[cfg(test)]
mod tests {
Expand Down
Loading