Revert "tamer: parse::TransitionResult: Move common Transition into Result"

This reverts commit bf5da75096.
main
Mike Gerwitz 2022-03-25 09:17:25 -04:00
parent bf5da75096
commit 2e98a69d15
4 changed files with 28 additions and 52 deletions

View File

@ -169,16 +169,16 @@ impl<S: ParseState> Transition<S> {
///
/// This allows [`ParseState::parse_token`] to emit a parsed object and
/// corresponds to [`ParseStatus::Object`].
pub fn with(self, obj: S::Object) -> TransitionResult<S> {
Ok((self, ParseStatus::Object(obj)))
pub fn with(self, obj: S::Object) -> (Self, ParseStateResult<S>) {
(self, Ok(ParseStatus::Object(obj)))
}
/// A state transition indicating that more data is needed before an
/// object can be emitted.
///
/// This corresponds to [`ParseStatus::Incomplete`].
pub fn incomplete(self) -> TransitionResult<S> {
Ok((self, ParseStatus::Incomplete))
pub fn incomplete(self) -> (Self, ParseStateResult<S>) {
(self, Ok(ParseStatus::Incomplete))
}
/// A dead state transition.
@ -186,16 +186,16 @@ impl<S: ParseState> Transition<S> {
/// This corresponds to [`ParseStatus::Dead`],
/// and a calling parser should use the provided [`Token`] as
/// lookahead.
pub fn dead(self, tok: S::Token) -> TransitionResult<S> {
Ok((self, ParseStatus::Dead(tok)))
pub fn dead(self, tok: S::Token) -> (Self, ParseStateResult<S>) {
(self, Ok(ParseStatus::Dead(tok)))
}
/// A transition with corresponding error.
///
/// This indicates a parsing failure.
/// The state ought to be suitable for error recovery.
pub fn err<E: Into<S::Error>>(self, err: E) -> TransitionResult<S> {
Err((self, err.into()))
pub fn err<E: Into<S::Error>>(self, err: E) -> (Self, ParseStateResult<S>) {
(self, Err(err.into()))
}
}
@ -204,16 +204,7 @@ impl<S: ParseState> Transition<S> {
/// Conceptually,
/// imagine the act of a state transition producing data.
/// See [`Transition`] for convenience methods for producing this tuple.
///
/// See also [`result_tup0_invert`] if this common [`Transition`] in the
/// inner tuple is inconvenient to work with.
pub type TransitionResult<S> = Result<
(
Transition<S>,
ParseStatus<<S as ParseState>::Token, <S as ParseState>::Object>,
),
(Transition<S>, <S as ParseState>::Error),
>;
pub type TransitionResult<S> = (Transition<S>, ParseStateResult<S>);
/// A streaming parser defined by a [`ParseState`] with exclusive
/// mutable access to an underlying [`TokenStream`].
@ -236,20 +227,6 @@ pub struct Parser<S: ParseState, I: TokenStream<S::Token>> {
last_span: Option<Span>,
}
/// Invert a [`Result`] containing a tuple for both [`Ok`] and [`Err`]
/// variants where first index of the tuple is a common `C`.
///
/// The result is a tuple with the common type in the first index,
/// and the second index containing the remaining [`Result`] data.
pub fn result_tup0_invert<C, T, E>(
result: Result<(C, T), (C, E)>,
) -> (C, Result<T, E>) {
match result {
Ok((c, t)) => (c, Ok(t)),
Err((c, e)) => (c, Err(e)),
}
}
impl<S: ParseState, I: TokenStream<S::Token>> Parser<S, I> {
/// Indicate that no further parsing will take place using this parser,
/// and [`drop`] it.
@ -299,7 +276,7 @@ impl<S: ParseState, I: TokenStream<S::Token>> Parser<S, I> {
let result;
(Transition(self.state), result) =
result_tup0_invert(take(&mut self.state).parse_token(tok));
take(&mut self.state).parse_token(tok);
use ParseStatus::*;
match result {

View File

@ -121,7 +121,7 @@ mod test {
use super::*;
use crate::{
convert::ExpectInto,
parse::{result_tup0_invert, ParseStatus, Parsed},
parse::{ParseStatus, Parsed},
sym::GlobalSymbolIntern,
};
@ -137,12 +137,12 @@ mod test {
// There is no state that we can transition to,
// and we're in an empty accepting state.
assert_eq!(
// Make sure we're in the same state we started in so that
// we know we can accommodate recovery token(s).
Ok((
(
// Make sure we're in the same state we started in so that
// we know we can accommodate recovery token(s).
Transition(AttrParseState::default()),
ParseStatus::Dead(tok.clone())
)),
Ok(ParseStatus::Dead(tok.clone()))
),
sut.parse_token(tok)
);
}
@ -175,12 +175,12 @@ mod test {
// This token indicates that we're expecting a value to come next in
// the token stream.
let (Transition(sut), result) =
result_tup0_invert(sut.parse_token(XirToken::AttrName(attr, S)));
sut.parse_token(XirToken::AttrName(attr, S));
assert_eq!(result, Ok(ParseStatus::Incomplete));
// But we provide something else unexpected.
let (Transition(sut), result) =
result_tup0_invert(sut.parse_token(XirToken::Close(None, S2)));
sut.parse_token(XirToken::Close(None, S2));
assert_eq!(
result,
Err(AttrParseError::AttrValueExpected(
@ -200,9 +200,8 @@ mod test {
// Rather than checking for that state,
// let's actually attempt a recovery.
let recover = "value".intern();
let (Transition(sut), result) = result_tup0_invert(
sut.parse_token(XirToken::AttrValue(recover, S2)),
);
let (Transition(sut), result) =
sut.parse_token(XirToken::AttrValue(recover, S2));
assert_eq!(
result,
Ok(ParseStatus::Object(Attr::new(attr, recover, (S, S2)))),

View File

@ -218,14 +218,14 @@ where
(NodeExpected(stack), tok) => Self::parse_node(stack, tok),
(AttrExpected(stack, sa), tok) => match sa.parse_token(tok) {
Ok((Transition(sa), Incomplete)) => {
(Transition(sa), Ok(Incomplete)) => {
Transition(AttrExpected(stack, sa)).incomplete()
}
Ok((Transition(sa), Obj(attr))) => {
(Transition(sa), Ok(Obj(attr))) => {
Transition(AttrExpected(stack, sa)).with(Object::Attr(attr))
}
Ok((_, Dead(lookahead))) => Self::parse_node(stack, lookahead),
Err((Transition(sa), x)) => {
(_, Ok(Dead(lookahead))) => Self::parse_node(stack, lookahead),
(Transition(sa), Err(x)) => {
Transition(AttrExpected(stack, sa)).err(x)
}
},

View File

@ -542,18 +542,18 @@ impl<SA: StackAttrParseState> ParseState for Stack<SA> {
(AttrState(estack, attrs, sa), tok) => {
use ParseStatus::*;
match sa.parse_token(tok) {
Ok((Transition(sa), Incomplete)) => {
(Transition(sa), Ok(Incomplete)) => {
Transition(AttrState(estack, attrs, sa)).incomplete()
}
Ok((Transition(sa), Object(attr))) => {
(Transition(sa), Ok(Object(attr))) => {
Transition(AttrState(estack, attrs.push(attr), sa))
.incomplete()
}
Ok((_, Dead(lookahead))) => {
(_, Ok(Dead(lookahead))) => {
BuddingElement(estack.consume_attrs(attrs))
.parse_token(lookahead)
}
Err((Transition(sa), x)) => {
(Transition(sa), Err(x)) => {
Transition(AttrState(estack, attrs, sa)).err(x.into())
}
}