QRegularFactory

This commit is contained in:
AF 2023-07-29 22:06:11 +00:00
parent 59a771630f
commit c9e65ca690
8 changed files with 78 additions and 26 deletions

View File

@ -34,7 +34,7 @@ pub use self::inctx::InCtx;
pub use self::inlining::{Inlining, InliningExt, InliningResultExt}; pub use self::inlining::{Inlining, InliningExt, InliningResultExt};
pub use self::modes::{ pub use self::modes::{
ExtensionResultM, ExtensionSourceM, FactoryModeParse, FactoryModeProxy, ImplMode, Mode, ExtensionResultM, ExtensionSourceM, FactoryModeParse, FactoryModeProxy, ImplMode, Mode,
ParseMode, ParseResultM, RegularFactory, RegularMode, WithMode, WithParseMode, ParseMode, ParseResultM, QRegularFactory, RegularFactory, RegularMode, WithMode, WithParseMode,
}; };
pub use self::origin::{OFctr, Origin}; pub use self::origin::{OFctr, Origin};
pub use self::point::Point; pub use self::point::Point;

View File

@ -32,6 +32,11 @@ pub trait Mode {
) -> Self::ExtensionResult<A1, E>; ) -> Self::ExtensionResult<A1, E>;
fn xseal<A, E>(result: Self::ExtensionResult<A, E>) -> Result<A, E>; fn xseal<A, E>(result: Self::ExtensionResult<A, E>) -> Result<A, E>;
fn smap<A0, A1, E>(
source: Self::ExtensionSource<A0, E>,
f: impl FnOnce(A0) -> A1,
) -> Self::ExtensionSource<A1, E>;
} }
pub trait ParseMode { pub trait ParseMode {
@ -66,7 +71,7 @@ impl<'a, Ctx: Context<'a>, F: FactoryModeParse<'a, Ctx>> FactoryParse<'a, Ctx> f
} }
} }
impl<'a, Ctx: Context<'a>, F: FactoryParse<'a, Ctx> + WithParseMode> Factory<'a, Ctx> for F impl<'a, Ctx: Context<'a>, F: FactoryParse<'a, Ctx> + ParseMode> Factory<'a, Ctx> for F
where where
F::Mtbl: MentionableTop<'a, Ctx>, F::Mtbl: MentionableTop<'a, Ctx>,
{ {
@ -174,6 +179,20 @@ impl Mode for RegularMode {
fn xseal<A, E>(result: Self::ExtensionResult<A, E>) -> Result<A, E> { fn xseal<A, E>(result: Self::ExtensionResult<A, E>) -> Result<A, E> {
result result
} }
fn smap<A0, A1, E>(
source: Self::ExtensionSource<A0, E>,
f: impl FnOnce(A0) -> A1,
) -> Self::ExtensionSource<A1, E> {
f(source)
}
}
pub trait QRegularFactory<'a, Ctx: Context<'a>>:
FactoryBase<'a, Ctx> + ImplMode<Mode = RegularMode>
{
fn qrdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self>;
fn qrextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self>;
} }
pub trait RegularFactory<'a, Ctx: Context<'a>>: pub trait RegularFactory<'a, Ctx: Context<'a>>:
@ -183,17 +202,29 @@ pub trait RegularFactory<'a, Ctx: Context<'a>>:
fn rextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self>; fn rextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self>;
} }
impl<'a, Ctx: Context<'a>, F: RegularFactory<'a, Ctx>> FactoryModeProxy<'a, Ctx> impl<'a, Ctx: Context<'a>, F: FactoryModeParse<'a, Ctx> + ParseMode<Mode = RegularMode>>
RegularFactory<'a, Ctx> for F
{
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
self.mdeserialize(inctx)
}
fn rextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
self.mextend(mentionable, tail)
}
}
impl<'a, Ctx: Context<'a>, F: QRegularFactory<'a, Ctx>> FactoryModeProxy<'a, Ctx>
for WithMode<F, RegularMode> for WithMode<F, RegularMode>
{ {
type F = F; type F = F;
fn pmdeserialize<I: InCtx<'a, Ctx>>(f: &Self::F, inctx: I) -> ParseResultM<'a, Ctx, F, I> { fn pmdeserialize<I: InCtx<'a, Ctx>>(f: &Self::F, inctx: I) -> ParseResultM<'a, Ctx, F, I> {
f.rdeserialize(inctx) f.qrdeserialize(inctx)
} }
fn pmextend(f: &F, mentionable: Mtbl<'a, Ctx, F>, tail: &[u8]) -> ExtensionResultM<'a, Ctx, F> { fn pmextend(f: &F, mentionable: Mtbl<'a, Ctx, F>, tail: &[u8]) -> ExtensionResultM<'a, Ctx, F> {
f.rextend(mentionable, tail) f.qrextend(mentionable, tail)
} }
fn pmprepare(mentionable: Mtbl<'a, Ctx, Self::F>) -> ExtensionSourceM<'a, Ctx, Self::F> { fn pmprepare(mentionable: Mtbl<'a, Ctx, Self::F>) -> ExtensionSourceM<'a, Ctx, Self::F> {

View File

@ -75,17 +75,31 @@ impl<'a, Ctx: Context<'a>, A: AtomicBase> FactoryBase<'a, Ctx> for AtomicFactory
type ParseError = A::AParseError; type ParseError = A::AParseError;
} }
impl<A: ParseMode> ImplMode for AtomicFactory<A> { impl<A: ParseMode> ParseMode for AtomicFactory<A> {
type Mode = A::Mode; type Mode = A::Mode;
} }
impl<'a, Ctx: Context<'a>, A: RegularAtomic> RegularFactory<'a, Ctx> for AtomicFactory<A> { impl<'a, Ctx: Context<'a>, A: AtomicModeParse> FactoryModeParse<'a, Ctx> for AtomicFactory<A> {
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { fn mdeserialize<I: InCtx<'a, Ctx>>(&self, inctx: I) -> ParseResultM<'a, Ctx, Self, I> {
Ok(A::ra_deserialize(inctx)?.into()) <<A as ParseMode>::Mode as Mode>::bind(A::ma_deserialize(inctx), |a| Ok(a.into()))
} }
fn rextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { fn mextend(
Ok(A::ra_extend(mentionable.atomic, tail)?.into()) &self,
mentionable: ExtensionSourceM<'a, Ctx, Self>,
tail: &[u8],
) -> ExtensionResultM<'a, Ctx, Self> {
<<A as ParseMode>::Mode as Mode>::xbind(
A::ma_extend(
<<A as ParseMode>::Mode as Mode>::smap(mentionable, |a| a.atomic),
tail,
),
|a| Ok(a.into()),
)
}
fn mprepare(mentionable: Self::Mtbl) -> ExtensionSourceM<'a, Ctx, Self> {
<<A as ParseMode>::Mode as Mode>::smap(mentionable.atomic.ma_prepare(), |a| a.into())
} }
} }

View File

@ -106,12 +106,12 @@ impl<F: ParseMode> ImplMode for StackNodeFactory<F> {
type Mode = F::Mode; type Mode = F::Mode;
} }
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx> + RegularFactory<'a, Ctx>> RegularFactory<'a, Ctx> impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx> + RegularFactory<'a, Ctx>> QRegularFactory<'a, Ctx>
for StackNodeFactory<F> for StackNodeFactory<F>
where where
F::Mtbl: MentionableTop<'a, Ctx>, F::Mtbl: MentionableTop<'a, Ctx>,
{ {
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { fn qrdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
let (rest, inctx) = self.parse_point(inctx)?; let (rest, inctx) = self.parse_point(inctx)?;
let element = self let element = self
.element_factory .element_factory
@ -120,7 +120,7 @@ where
Ok(StackNode { rest, element }) Ok(StackNode { rest, element })
} }
fn rextend(&self, mut mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { fn qrextend(&self, mut mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
mentionable.element = self mentionable.element = self
.element_factory .element_factory
.extend(mentionable.element, tail) .extend(mentionable.element, tail)
@ -182,8 +182,8 @@ where
} }
} }
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Mentionable<'a, Ctx> + Clone> impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone> ExtStackClone<'a, Ctx, A>
ExtStackClone<'a, Ctx, A> for Stack<'a, Ctx, A> for Stack<'a, Ctx, A>
where where
StackNode<'a, Ctx, A>: Mentionable<'a, Ctx, _Fctr = StackNodeFactory<A::Fctr>>, StackNode<'a, Ctx, A>: Mentionable<'a, Ctx, _Fctr = StackNodeFactory<A::Fctr>>,
StackNodeFactory<A::Fctr>: FactoryParse<'a, Ctx>, StackNodeFactory<A::Fctr>: FactoryParse<'a, Ctx>,

View File

@ -144,8 +144,8 @@ impl<F> ImplMode for NodeFactory<F> {
type Mode = RegularMode; type Mode = RegularMode;
} }
impl<'a, Ctx: Context<'a>, F: FactoryParse<'a, Ctx>> RegularFactory<'a, Ctx> for NodeFactory<F> { impl<'a, Ctx: Context<'a>, F: FactoryParse<'a, Ctx>> QRegularFactory<'a, Ctx> for NodeFactory<F> {
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { fn qrdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
let tree_factory = TreeFactory(NullableFactory::new(self.clone())); let tree_factory = TreeFactory(NullableFactory::new(self.clone()));
let (l, inctx) = tree_factory.ideserialize(inctx)?; let (l, inctx) = tree_factory.ideserialize(inctx)?;
let (r, inctx) = tree_factory.ideserialize(inctx)?; let (r, inctx) = tree_factory.ideserialize(inctx)?;
@ -153,7 +153,7 @@ impl<'a, Ctx: Context<'a>, F: FactoryParse<'a, Ctx>> RegularFactory<'a, Ctx> for
Ok(Node { l, r, key }) Ok(Node { l, r, key })
} }
fn rextend(&self, mut mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { fn qrextend(&self, mut mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
mentionable.key = self mentionable.key = self
.0 .0
.extend(mentionable.key, tail) .extend(mentionable.key, tail)

View File

@ -46,6 +46,13 @@ impl Mode for InliningMode {
fn xseal<A, E>(result: Self::ExtensionResult<A, E>) -> Result<A, E> { fn xseal<A, E>(result: Self::ExtensionResult<A, E>) -> Result<A, E> {
Err(result) Err(result)
} }
fn smap<A0, A1, E>(
source: Self::ExtensionSource<A0, E>,
_f: impl FnOnce(A0) -> A1,
) -> Self::ExtensionSource<A1, E> {
source
}
} }
impl<'a, Ctx: Context<'a>, F: InliningFactory<'a, Ctx>> FactoryModeProxy<'a, Ctx> impl<'a, Ctx: Context<'a>, F: InliningFactory<'a, Ctx>> FactoryModeProxy<'a, Ctx>

View File

@ -140,16 +140,16 @@ impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> FactoryBase<'a, Ctx>
type ParseError = SP::ParseError; type ParseError = SP::ParseError;
} }
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> ParseMode for StaticPairFactory<'a, Ctx, SP> { impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> ImplMode for StaticPairFactory<'a, Ctx, SP> {
type Mode = <SP::FB as ParseMode>::Mode; type Mode = <SP::FB as ParseMode>::Mode;
} }
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> RegularFactory<'a, Ctx> impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> QRegularFactory<'a, Ctx>
for StaticPairFactory<'a, Ctx, SP> for StaticPairFactory<'a, Ctx, SP>
where where
SP::FB: RegularFactory<'a, Ctx>, SP::FB: RegularFactory<'a, Ctx>,
{ {
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { fn qrdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
let (fa, fb) = SP::factories(&self.factory_data); let (fa, fb) = SP::factories(&self.factory_data);
let (a, inctx) = fa let (a, inctx) = fa
.ideserialize(inctx) .ideserialize(inctx)
@ -162,7 +162,7 @@ where
}) })
} }
fn rextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { fn qrextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
let (_, fb) = SP::factories(&self.factory_data); let (_, fb) = SP::factories(&self.factory_data);
let (a, b) = mentionable.pair.into_elements(); let (a, b) = mentionable.pair.into_elements();
match fb.rextend(b, tail) { match fb.rextend(b, tail) {

View File

@ -99,12 +99,12 @@ impl<'a, Ctx: Context<'a>> ImplMode for TypelessFactory<'a, Ctx> {
type Mode = RegularMode; type Mode = RegularMode;
} }
impl<'a, Ctx: Context<'a>> RegularFactory<'a, Ctx> for TypelessFactory<'a, Ctx> { impl<'a, Ctx: Context<'a>> QRegularFactory<'a, Ctx> for TypelessFactory<'a, Ctx> {
fn rdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> { fn qrdeserialize(&self, inctx: impl InCtx<'a, Ctx>) -> ParseResult<'a, Ctx, Self> {
self.t_deserialize.de(inctx.demote()).map_err(TypelessError) self.t_deserialize.de(inctx.demote()).map_err(TypelessError)
} }
fn rextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> { fn qrextend(&self, mentionable: Self::Mtbl, tail: &[u8]) -> ParseResult<'a, Ctx, Self> {
self.t_extend.xt(mentionable, tail) self.t_extend.xt(mentionable, tail)
} }
} }