Skip to main content

oni_comb_parser/primitive/
one_of.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 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 = ParseError;
22  type Output = I::Token;
23
24  #[inline]
25  fn parse_next(&mut self, input: &mut I) -> PResult<I::Token, ParseError> {
26    let pos = input.offset();
27    match input.peek_token() {
28      Some(t) if self.set.contains(&t) => {
29        input.next_token();
30        Ok(t)
31      }
32      _ => Err(Fail::Backtrack(ParseError::expected_description(pos, "one of set"))),
33    }
34  }
35}