use crate::{
error::{Error, ErrorKind},
parser::*,
};
use std::marker::PhantomData;
#[derive(Clone)]
pub struct PNot<P, O> {
p: P,
_marker: PhantomData<O>,
}
pub fn pnot<P, O>(p: P) -> PNot<P, O> {
PNot {
p,
_marker: PhantomData,
}
}
impl<'a, K, O, P> ParserCore<'a, K, ()> for PNot<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, ()>, Error<'a, K>> {
let start = i.loc;
match self.p.parse(i) {
Ok(PSuccess { val: _, rest }) => Err(Error {
kind: vec![ErrorKind::Custom(
"Expected a negation of the previous parser.".to_string(),
)],
span: (start, rest.loc),
state: rest,
}),
Err(Error {
kind: _,
span: _,
state: rest,
}) => Ok(PSuccess { val: (), rest }),
}
}
}
impl<'a, K, O, P> Parser<'a, K, ()> for PNot<P, O>
where
K: PartialEq + Clone + 'a,
O: Clone + 'a,
P: Parser<'a, K, O>,
{
}