tamer: parse::state: Begin to tame delegation methods

These delegation methods have been a pain in my ass for quite some time, and
their lack of generalization makes the introduction of new delegation
methods (in the general sense, not necessarily trait methods) very tedious
and prone to inconsistencies.

I'm going to progressively refactor them in separate commits so it's clear
what I'm doing, primarily for future me to reference if need be.

DEV-13156
main
Mike Gerwitz 2022-11-15 22:16:30 -05:00
parent fc425ff1d5
commit a17e53258b
2 changed files with 32 additions and 15 deletions

View File

@ -302,20 +302,9 @@ where
TransitionData::Dead(Lookahead(lookahead)) => {
dead().incomplete().with_lookahead(lookahead)
}
TransitionData::Result(result, lookahead) => TransitionResult(
into(newst).into_super(),
TransitionData::Result(
match result {
Ok(Incomplete) => Ok(Incomplete),
Ok(Obj(obj)) => Ok(Obj(obj.into())),
// First convert the error into `SP::Error`,
// and then `SP::Super::Error`
// (which will be the same type if SP is closed).
Err(e) => Err(e.into().into()),
},
lookahead,
),
),
TransitionData::Result(..) => {
TransitionResult(into(newst).into_super(), data.inner_into())
}
}
}

View File

@ -20,7 +20,8 @@
//! State transitions for parser automata.
use super::{
ClosedParseState, ParseState, ParseStateResult, ParseStatus, Token,
ClosedParseState, ParseState, ParseStateResult, ParseStatus,
StitchableParseState, Token,
};
use std::{
convert::Infallible,
@ -306,6 +307,33 @@ impl<S: ParseState> TransitionData<S> {
Dead(la) => Dead(la),
}
}
/// Transform inner types using [`Into`] such that they are compatible
/// with the superstate of `SB`.
pub fn inner_into<SB: ParseState>(
self,
) -> TransitionData<<SB as ParseState>::Super>
where
S: StitchableParseState<SB>,
{
use ParseStatus::{Incomplete, Object};
use TransitionData::*;
match self {
Dead(la) => Dead(la),
Result(result, la) => Result(
match result {
Ok(Incomplete) => Ok(Incomplete),
Ok(Object(obj)) => Ok(Object(obj.into())),
// First convert the error into `SB::Error`,
// and then `SP::Super::Error`
// (which will be the same type if SB is closed).
Err(e) => Err(e.into().into()),
},
la,
),
}
}
}
/// A verb denoting a state transition.