then method

This commit is contained in:
Greg Shuflin 2022-10-16 01:10:48 -07:00
parent 981b5c08d0
commit 00d866f2a1

View File

@ -18,6 +18,18 @@ trait Parser<I, O, E> {
{ {
BoxedParser::new(map(self, map_fn)) BoxedParser::new(map(self, map_fn))
} }
fn then<'a, P, O2>(self, next_parser: P) -> BoxedParser<'a, I, (O, O2), E>
where
Self: Sized + 'a,
I: 'a,
O: 'a,
O2: 'a,
E: 'a,
P: Parser<I, O2, E> + 'a,
{
BoxedParser::new(seq(self, next_parser))
}
} }
struct BoxedParser<'a, I, O, E> { struct BoxedParser<'a, I, O, E> {
@ -198,6 +210,9 @@ mod tests {
fn test_seq() { fn test_seq() {
let p = seq(identifier, seq(literal(" "), literal("ruts"))); let p = seq(identifier, seq(literal(" "), literal("ruts")));
assert_matches!(p.parse("fort1 ruts"), Ok((r, "")) if r.0 == "fort1" && r.1 == (" ", "ruts") ); assert_matches!(p.parse("fort1 ruts"), Ok((r, "")) if r.0 == "fort1" && r.1 == (" ", "ruts") );
let p = identifier.then(literal(" ")).then(literal("ruts"));
assert_matches!(p.parse("fort1 ruts"), Ok((r, "")) if r.0.0 == "fort1" && r.0.1== " " && r.1 == "ruts");
} }
#[test] #[test]