This commit is contained in:
AF 2023-07-29 18:32:11 +00:00
parent b7e3beacb6
commit 5dff836c58
3 changed files with 54 additions and 2 deletions

View File

@ -33,7 +33,7 @@ pub use self::hashing::{Hash, HASH_SIZE, HASH_ZEROS};
pub use self::inctx::InCtx;
pub use self::inlining::{Inlining, InliningExt, InliningResultExt};
pub use self::modes::{
FactoryProxy, ParseMode, RegularFactory, RegularMode, WithMode, WithParseMode,
FactoryProxy, Mode, ParseMode, RegularFactory, RegularMode, WithMode, WithParseMode,
};
pub use self::origin::{OFctr, Origin};
pub use self::point::Point;

View File

@ -2,8 +2,22 @@ use std::marker::PhantomData;
use super::*;
pub trait Mode {
type ParseResult<A, E, I>;
fn map_err<A, E0, E1, I>(
result: Self::ParseResult<A, E0, I>,
f: impl FnOnce(E0) -> E1,
) -> Self::ParseResult<A, E1, I>;
fn bind<A0, A1, E, I>(
result: Self::ParseResult<A0, E, I>,
f: impl FnOnce(A0) -> Result<A1, E>,
) -> Self::ParseResult<A1, E, I>;
}
pub trait ParseMode {
type Mode: ?Sized;
type Mode: ?Sized + Mode;
}
pub trait WithParseMode: ParseMode {
@ -49,6 +63,24 @@ where
pub struct RegularMode;
impl Mode for RegularMode {
type ParseResult<A, E, I> = Result<A, E>;
fn map_err<A, E0, E1, I>(
result: Self::ParseResult<A, E0, I>,
f: impl FnOnce(E0) -> E1,
) -> Self::ParseResult<A, E1, I> {
result.map_err(f)
}
fn bind<A0, A1, E, I>(
result: Self::ParseResult<A0, E, I>,
f: impl FnOnce(A0) -> Result<A1, E>,
) -> Self::ParseResult<A1, E, I> {
result.and_then(f)
}
}
pub trait RegularFactory<'a, Ctx: Context<'a>>:
FactoryBase<'a, Ctx> + ParseMode<Mode = RegularMode>
{

View File

@ -2,6 +2,26 @@ use super::*;
pub struct InliningMode;
impl Mode for InliningMode {
type ParseResult<A, E, I> = Result<(A, I), E>;
fn map_err<A, E0, E1, I>(
result: Self::ParseResult<A, E0, I>,
f: impl FnOnce(E0) -> E1,
) -> Self::ParseResult<A, E1, I> {
result.map_err(f)
}
fn bind<A0, A1, E, I>(
result: Self::ParseResult<A0, E, I>,
f: impl FnOnce(A0) -> Result<A1, E>,
) -> Self::ParseResult<A1, E, I> {
let (a0, i) = result?;
let a1 = f(a0)?;
Ok((a1, i))
}
}
impl<'a, Ctx: Context<'a>, F: InlineableFactory<'a, Ctx>> FactoryProxy<'a, Ctx>
for WithMode<F, InliningMode>
{