use crate::parser::*;
use std::marker::PhantomData;
pub struct PBetween<L, P, R, A> {
l: L,
p: P,
r: R,
_marker: PhantomData<A>,
}
pub fn pbetween<L, P, R, A>(l: L, p: P, r: R) -> PBetween<L, P, R, A> {
PBetween {
l,
p,
r,
_marker: PhantomData,
}
}
impl<'a, K, O, L, P, R, A> ParserCore<'a, K, O> for PBetween<L, P, R, A>
where
K: PartialEq + Clone + 'a,
A: 'a,
O: 'a,
L: Parser<'a, K, A>,
P: Parser<'a, K, O>,
R: Parser<'a, K, A>,
{
fn parse(&self, i: PInput<'a, K>) -> Result<PSuccess<'a, K, O>, PFail<'a, K>> {
let start_loc = i.loc;
let PSuccess {
val: _,
rest: after_l,
} = self.l.parse(i)?;
let PSuccess {
val,
rest: after_main,
} = self.p.parse(after_l)?;
match self.r.parse(after_main) {
Ok(PSuccess {
val: _,
rest: after_r,
}) => Ok(PSuccess { val, rest: after_r }),
Err(PFail {
mut error,
mut span,
rest,
}) => {
error.push("Missing final token for parsing between.".to_string());
span.push((start_loc, rest.loc));
Err(PFail { error, span, rest })
}
}
}
}
impl<'a, K, O, L, P, R, A> Parser<'a, K, O> for PBetween<L, P, R, A>
where
K: PartialEq + Clone + 'a,
O: 'a,
A: 'a,
L: Parser<'a, K, A>,
P: Parser<'a, K, O>,
R: Parser<'a, K, A>,
{
}