use std::{borrow::Cow, marker::PhantomData};
use crate::{
error::{Error, ErrorKind, TokenPattern},
prelude::{PInput, PSuccess, Parser, ParserCore},
};
#[derive(Clone)]
pub struct PUntilEnd<P, O> {
inner: P,
_marker: PhantomData<O>,
}
pub fn puntil_end<P, O>(inner: P) -> PUntilEnd<P, O> {
PUntilEnd {
inner,
_marker: PhantomData,
}
}
impl<'a, P, K, O> ParserCore<'a, K, O> for PUntilEnd<P, O>
where
K: PartialEq + Clone + 'a,
O: Clone + 'a,
P: Parser<'a, K, O>,
{
fn parse(&self, i: PInput<'a, K>) -> Result<PSuccess<'a, K, O>, Error<'a, K>> {
let PSuccess { val, rest } = self.inner.parse(i)?;
if rest.loc == rest.tokens.len() {
Ok(PSuccess { val, rest })
} else {
Err(Error {
kind: vec![ErrorKind::Unexpected {
expected: vec![TokenPattern::String(Cow::Borrowed("End of file"))],
found: TokenPattern::Tokens(Cow::Borrowed(&rest.tokens[rest.loc..])),
}],
span: (rest.loc, rest.loc + 1),
state: rest,
})
}
}
}
impl<'a, P, K, O> Parser<'a, K, O> for PUntilEnd<P, O>
where
K: PartialEq + Clone + 'a,
O: Clone + 'a,
P: Parser<'a, K, O>,
{
}