Skip to main content

oni_comb_parser/primitive/
satisfy.rs

1use core::marker::PhantomData;
2
3use crate::error::{ExpectError, Expected};
4use crate::fail::{Fail, PResult};
5use crate::input::Input;
6use crate::parser::Parser;
7
8pub struct Satisfy<F, I: Input>(F, PhantomData<fn(&mut I)>);
9
10pub fn satisfy<I: Input, F: FnMut(I::Token) -> bool>(f: F) -> Satisfy<F, I> {
11  Satisfy(f, PhantomData)
12}
13
14impl<I: Input, F> Parser<I> for Satisfy<F, I>
15where
16  F: FnMut(I::Token) -> bool,
17{
18  type Error = I::Error;
19  type Output = I::Token;
20
21  #[inline]
22  fn parse_next(&mut self, input: &mut I) -> PResult<I::Token, I::Error> {
23    let pos = input.offset();
24    let cp = input.checkpoint();
25    match input.next_token() {
26      Some(t) if (self.0)(t) => Ok(t),
27      Some(_) => {
28        input.reset(cp);
29        Err(Fail::Backtrack(I::Error::from_expected(
30          pos,
31          Expected::Description("satisfy"),
32        )))
33      }
34      None => Err(Fail::Backtrack(I::Error::from_expected(
35        pos,
36        Expected::Description("satisfy"),
37      ))),
38    }
39  }
40}