Skip to main content

oni_comb_parser/primitive/
take_while0.rs

1use core::marker::PhantomData;
2
3use crate::fail::PResult;
4use crate::input::Input;
5use crate::parser::Parser;
6
7pub struct TakeWhile0<F, I: Input>(F, PhantomData<fn(&mut I)>);
8
9pub fn take_while0<I: Input, F: FnMut(I::Token) -> bool>(f: F) -> TakeWhile0<F, I> {
10  TakeWhile0(f, PhantomData)
11}
12
13impl<I: Input, F> Parser<I> for TakeWhile0<F, I>
14where
15  F: FnMut(I::Token) -> bool,
16{
17  type Error = I::Error;
18  type Output = I::Slice;
19
20  #[inline]
21  fn parse_next(&mut self, input: &mut I) -> PResult<I::Slice, I::Error> {
22    let cp = input.checkpoint();
23    loop {
24      let item_cp = input.checkpoint();
25      match input.next_token() {
26        Some(t) if (self.0)(t) => {}
27        Some(_) => {
28          input.reset(item_cp);
29          break;
30        }
31        None => break,
32      }
33    }
34    Ok(input.slice_since(cp))
35  }
36}