From fc4ae140ac8d3dd40868a18d44dfb4cd46711129 Mon Sep 17 00:00:00 2001 From: John Turner Date: Fri, 24 Oct 2025 00:26:51 -0400 Subject: impl Verify combinator --- src/lib.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 6f16148..686ce6f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -170,6 +170,16 @@ pub trait Parser: Sized { delimiter, } } + + fn verify(self, checker: C) -> impl Parser + where + C: Parser, + { + Verify { + parser: self, + checker, + } + } } impl Parser for F @@ -812,6 +822,33 @@ where Opt { parser } } +struct Verify { + parser: P, + checker: C, +} + +impl Parser for Verify +where + I: Input, + P: Parser, + C: Parser, +{ + type Output = P::Output; + + fn run>( + &mut self, + it: InputIter, + ) -> ParserResult { + match self.parser.run::(it.clone()) { + Ok((rest, output)) if self.checker.check(it.clone()).is_ok() => { + Ok((rest, OM::bind(|| output))) + } + Err(rest) => Err(EM::bind(|| rest)), + _ => Err(EM::bind(|| it)), + } + } +} + pub fn alpha() -> impl Parser where I: Input, -- cgit v1.2.3