macro_rules! sequence {
($first:tt $(> $tail:tt)+ => $map:expr) => { ... };
($first:tt $(> $tail:tt)+) => { ... };
(@chain $head:tt > $($tail:tt)+) => { ... };
(@chain $last:tt) => { ... };
}Expand description
Macro for ergonomic parsing in sequence.
This macro recursively combines multiple parser or literals
into a sequene parser using pseq. Optionally one can use =>
along with a closure to map the parser result into a useful result.
See select! for details about passing parser versus literals.
ยงExamples
use cypress::prelude::*;
let input = "1+2".into_input();
#[derive(PartialEq, Debug)]
enum Expr {
Num(u8),
Add(Box<Expr>, Box<Expr>)
};
let parser = sequence!(
(pnum()) > '+' > (pnum()) => |(a, (_, b))| Expr::Add(Box::new(Expr::Num(a)), Box::new(Expr::Num(b)))
);
match parser.parse(input) {
Ok(PSuccess {val, rest: _ }) => assert_eq!(val, Expr::Add(Box::new(Expr::Num(b'1')), Box::new(Expr::Num(b'2')))),
Err(_) => assert!(false),
};