use crate::{
error::{Error, ErrorDisplay},
parser::*,
};
#[derive(Clone)]
pub struct PDebug<P> {
inner: P,
label: &'static str,
}
pub fn debug<P>(inner: P, label: &'static str) -> PDebug<P> {
PDebug { inner, label }
}
impl<'a, K, O, P> ParserCore<'a, K, O> for PDebug<P>
where
K: PartialEq + Clone + ErrorDisplay + 'a,
O: Clone + 'a,
P: Parser<'a, K, O>,
{
fn parse(&self, i: PInput<'a, K>) -> Result<PSuccess<'a, K, O>, Error<'a, K>> {
match self.inner.parse(i) {
Ok(psuccess) => {
println!("Successfully parsed with {}", self.label);
Ok(psuccess)
}
Err(Error { kind, span, state }) => {
println!(
"Failed {} with msg: {} at position span ({}, {})",
self.label,
kind.last().unwrap(),
span.0,
span.1
);
Err(Error { kind, span, state })
}
}
}
}
impl<'a, K, O, Inner> Parser<'a, K, O> for PDebug<Inner>
where
K: PartialEq + Clone + ErrorDisplay + 'a,
O: Clone + 'a,
Inner: Parser<'a, K, O>,
{
}