Skip to main content

oni_comb_parser/primitive/
take_while1.rs

1use core::marker::PhantomData;
2
3use crate::error::ParseError;
4use crate::fail::{Fail, PResult};
5use crate::input::Input;
6use crate::parser::Parser;
7
8pub struct TakeWhile1<F, I: Input>(F, PhantomData<fn(&mut I)>);
9
10pub fn take_while1<I: Input, F: FnMut(I::Token) -> bool>(f: F) -> TakeWhile1<F, I> {
11  TakeWhile1(f, PhantomData)
12}
13
14impl<I: Input, F> Parser<I> for TakeWhile1<F, I>
15where
16  F: FnMut(I::Token) -> bool,
17{
18  type Error = ParseError;
19  type Output = I::Slice;
20
21  #[inline]
22  fn parse_next(&mut self, input: &mut I) -> PResult<I::Slice, ParseError> {
23    let pos = input.offset();
24    let cp = input.checkpoint();
25    while let Some(t) = input.peek_token() {
26      if (self.0)(t) {
27        input.next_token();
28      } else {
29        break;
30      }
31    }
32    if input.checkpoint() == cp {
33      return Err(Fail::Backtrack(ParseError::expected_description(
34        pos,
35        "at least one matching token",
36      )));
37    }
38    Ok(input.slice_since(cp))
39  }
40}