use crate::{
error::{Error, ErrorKind},
parser::*,
};
#[derive(Clone)]
pub struct PMany1<P> {
p: P,
}
pub fn pmany1<P>(p: P) -> PMany1<P> {
PMany1 { p }
}
impl<'a, K, O, P> ParserCore<'a, K, Vec<O>> for PMany1<P>
where
K: PartialEq + Clone + 'a,
O: Clone + 'a,
P: Parser<'a, K, O>,
{
fn parse(&self, i: PInput<'a, K>) -> Result<PSuccess<'a, K, Vec<O>>, Error<'a, K>> {
let mut vals: Vec<O> = vec![];
let mut input = i;
while let Ok(PSuccess { val, rest }) = self.p.parse(input.clone()) {
vals.push(val);
input = rest;
}
if vals.is_empty() {
Err(Error {
kind: vec![ErrorKind::Custom(
"Expected at least one instance for `many1` but found none.",
)],
span: (input.loc, input.loc + 1),
state: input,
})
} else {
Ok(PSuccess {
val: vals,
rest: input,
})
}
}
}
impl<'a, K, O, P> Parser<'a, K, Vec<O>> for PMany1<P>
where
K: PartialEq + Clone + 'a,
O: Clone + 'a,
P: Parser<'a, K, O>,
{
}