one_of combinator

This commit is contained in:
Greg Shuflin 2022-10-17 00:37:55 -07:00
parent dd9c05ccd1
commit 8b010811e3

View File

@ -21,6 +21,18 @@ pub fn any_char(input: &str) -> ParseResult<&str, char, &str> {
} }
} }
pub fn one_of<'a>(items: &'static str) -> impl Parser<&'a str, &'a str, &'a str> {
move |input: &'a str| {
if let Some(ch) = input.chars().nth(0) {
if items.contains(ch) {
let (first, rest) = input.split_at(1);
return Ok((first, rest));
}
}
Err(input)
}
}
pub fn pred<P, F, I, O>(parser: P, pred_fn: F) -> impl Parser<I, O, I> pub fn pred<P, F, I, O>(parser: P, pred_fn: F) -> impl Parser<I, O, I>
where where
P: Parser<I, O, I>, P: Parser<I, O, I>,