tamer: parse::state::TransitionResult: Make opaque

There was only one test outside of the `parse` module using these
fields.  The next commit will be introducing lookahead, and I do not want to
have to trust callers to ensure invariants are met.

DEV-7145
main
Mike Gerwitz 2022-07-05 14:12:06 -04:00
parent a16a0d9138
commit 40c68d3e1e
2 changed files with 21 additions and 18 deletions

View File

@ -381,8 +381,8 @@ mod transition {
/// See [`Transition`] for convenience methods for producing this tuple.
#[derive(Debug, PartialEq)]
pub struct TransitionResult<S: ParseState>(
pub Transition<S>,
pub ParseStateResult<S>,
pub(in super::super) Transition<S>,
pub(in super::super) ParseStateResult<S>,
);
/// Denotes a state transition.

View File

@ -147,7 +147,7 @@ mod test {
use super::*;
use crate::{
convert::ExpectInto,
parse::{EmptyContext, ParseStatus, Parsed},
parse::{EmptyContext, ParseError, ParseStatus, Parsed},
sym::GlobalSymbolIntern,
xir::test::{close_empty, open},
};
@ -196,41 +196,44 @@ mod test {
#[test]
fn parse_fails_when_attribute_value_missing_but_can_recover() {
let attr = "bad".unwrap_into();
let recover = "value".intern();
let sut = AttrParseState::default();
let toks = vec![
XirToken::AttrName(attr, S),
close_empty(S2),
XirToken::AttrValue(recover, S2),
];
let mut sut = AttrParseState::parse(toks.into_iter());
// This token indicates that we're expecting a value to come next in
// the token stream.
let TransitionResult(Transition(sut), result) =
sut.parse_token(XirToken::AttrName(attr, S), &mut EmptyContext);
assert_eq!(result, Ok(ParseStatus::Incomplete));
assert_eq!(sut.next(), Some(Ok(Parsed::Incomplete)));
// But we provide something else unexpected.
let TransitionResult(Transition(sut), result) =
sut.parse_token(close_empty(S2), &mut EmptyContext);
assert_eq!(
result,
Err(AttrParseError::AttrValueExpected(attr, S, close_empty(S2)))
sut.next(),
Some(Err(ParseError::StateError(
AttrParseError::AttrValueExpected(attr, S, close_empty(S2))
)))
);
// We should not be in an accepting state,
// given that we haven't finished parsing the attribute.
assert!(!sut.is_accepting());
let (mut sut, _) =
sut.finalize().expect_err("unexpected accepting state");
// Despite this error,
// we should remain in a state that permits recovery should a
// proper token be substituted.
// Rather than checking for that state,
// let's actually attempt a recovery.
let recover = "value".intern();
let TransitionResult(Transition(sut), result) = sut
.parse_token(XirToken::AttrValue(recover, S2), &mut EmptyContext);
assert_eq!(
result,
Ok(ParseStatus::Object(Attr::new(attr, recover, (S, S2)))),
sut.next(),
Some(Ok(Parsed::Object(Attr::new(attr, recover, (S, S2))))),
);
// Finally, we should now be in an accepting state.
assert!(sut.is_accepting());
sut.finalize().expect("expected accepting state");
}
}