use crate::types::*;
#[inline]
pub fn delimited<'a, Lhs, LhsRes, Par, ParRes, Rhs, RhsRes>(
mut prefix: Lhs,
mut parser: Par,
mut suffix: Rhs,
) -> impl Parser<'a, ParRes>
where
Lhs: Parser<'a, LhsRes>,
Par: Parser<'a, ParRes>,
Rhs: Parser<'a, RhsRes>,
{
move |ctx| {
prefix
.parse(ctx)
.and_then(|(ctx, _)| parser.parse(ctx))
.and_then(|(ctx, res)| match suffix.parse(ctx) {
Ok((ctx, _)) => Ok(ctx.with_data(res)),
Err(error) => Err(error),
})
.map_error(|error| SourceError::Ranges(vec![error]))
}
}
#[inline]
pub fn left_delimited<'a, Lhs, LhsRes, Par, ParRes>(
mut left: Lhs,
mut parser: Par,
) -> impl Parser<'a, ParRes>
where
Lhs: Parser<'a, LhsRes>,
Par: Parser<'a, ParRes>,
{
move |ctx| {
left.parse(ctx)
.and_then(|ctx| parser.parse(ctx))
.map_error(|error| SourceError::Ranges(vec![error]))
}
}
#[inline]
pub fn right_delimited<'a, Par, ParRes, Rhs, RhsRes>(
mut parser: Par,
mut right: Rhs,
) -> impl Parser<'a, ParRes>
where
Par: Parser<'a, ParRes>,
Rhs: Parser<'a, RhsRes>,
{
move |ctx| {
parser
.parse(ctx)
.and_then(|(ctx, res)| match right.parse(ctx) {
Ok((ctx, _)) => Ok(ctx.with_data(res)),
Err(error) => Err(error),
})
.map_error(|error| SourceError::Ranges(vec![error]))
}
}
#[inline]
pub fn separated<'a, Lhs, LhsRes, Sep, SepRes, Rhs, RhsRes>(
mut left: Lhs,
mut separator: Sep,
mut right: Rhs,
) -> impl Parser<'a, (LhsRes, RhsRes)>
where
Lhs: Parser<'a, LhsRes>,
Sep: Parser<'a, SepRes>,
Rhs: Parser<'a, RhsRes>,
{
move |ctx| {
left.parse(ctx)
.and_then(|(ctx, bef_res)| {
separator.parse(ctx).and_then(|ctx| {
right
.parse(ctx)
.and_then(|(ctx, aft_res)| Ok(ctx.with_data((bef_res, aft_res))))
})
})
.map_error(|error| SourceError::Ranges(vec![error]))
}
}