From a17e53258b295891e162929b3909f8fc18c4f0ed Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 15 Nov 2022 22:16:30 -0500 Subject: [PATCH] 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 --- tamer/src/parse/state.rs | 17 +++------------- tamer/src/parse/state/transition.rs | 30 ++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/tamer/src/parse/state.rs b/tamer/src/parse/state.rs index b0e10f22..e6668d7c 100644 --- a/tamer/src/parse/state.rs +++ b/tamer/src/parse/state.rs @@ -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()) + } } } diff --git a/tamer/src/parse/state/transition.rs b/tamer/src/parse/state/transition.rs index 1af68ea0..b8950dca 100644 --- a/tamer/src/parse/state/transition.rs +++ b/tamer/src/parse/state/transition.rs @@ -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 TransitionData { Dead(la) => Dead(la), } } + + /// Transform inner types using [`Into`] such that they are compatible + /// with the superstate of `SB`. + pub fn inner_into( + self, + ) -> TransitionData<::Super> + where + S: StitchableParseState, + { + 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.