Skip to main content

oni_comb_parser/primitive/
one_of.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 OneOf<'s, I: Input> {
9  set: &'s [I::Token],
10  _marker: PhantomData<fn(&mut I)>,
11}
12
13pub fn one_of<'s, I: Input>(set: &'s [I::Token]) -> OneOf<'s, I> {
14  OneOf {
15    set,
16    _marker: PhantomData,
17  }
18}
19
20impl<'s, I: Input> Parser<I> for OneOf<'s, I> {
21  type Error = I::Error;
22  type Output = I::Token;
23
24  #[inline]
25  fn parse_next(&mut self, input: &mut I) -> PResult<I::Token, I::Error> {
26    let pos = input.offset();
27    let cp = input.checkpoint();
28    match input.next_token() {
29      Some(t) if self.set.contains(&t) => Ok(t),
30      Some(_) => {
31        input.reset(cp);
32        Err(Fail::Backtrack(I::Error::from_expected(
33          pos,
34          Expected::Description("one of set"),
35        )))
36      }
37      None => Err(Fail::Backtrack(I::Error::from_expected(
38        pos,
39        Expected::Description("one of set"),
40      ))),
41    }
42  }
43}