tamer: parse::parser::Parser: Include errors in parse trace

Because of recovery, the trace otherwise paints a really confusing-looking
picture when given unexpected input.

This is large enough now that it really ought to be extracted from
`feed_tok`, but I'll wait to see how this evolves further.  I considered
adding color too, but it's not yet clear to me that the visual noise will be
all that helpful.

DEV-7145
main
Mike Gerwitz 2022-07-21 22:32:58 -04:00
parent 422f3d9c0c
commit c3dfcc565c
5 changed files with 37 additions and 14 deletions

View File

@ -214,8 +214,8 @@ pub mod test {
}
impl Display for EchoStateError {
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
unimplemented!()
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "test EchoStateError")
}
}
@ -227,7 +227,7 @@ pub mod test {
impl Diagnostic for EchoStateError {
fn describe(&self) -> Vec<AnnotatedSpan> {
unimplemented!()
vec![]
}
}

View File

@ -307,19 +307,32 @@ impl<S: ParseState, I: TokenStream<S::Token>> Parser<S, I> {
#[cfg(any(test, feature = "parser-trace-stderr"))]
{
let newst = self.state.as_ref().unwrap();
#[cfg(test)]
let cfg = "test";
#[cfg(feature = "parser-trace-stderr")]
let cfg = "feature = \"parser-trace-stderr\"";
eprint!(
"\
| ==> Parser after tok is {newst}.
| | {newst:?}
| | Lookahead: {la:?}
= note: this trace was output as a debugging aid because `cfg({cfg})`.\n\n",
| | Lookahead: {la:?}\n",
la = data.lookahead_ref(),
);
if let Some(err) = data.err_ref() {
eprint!(
"\
|
| ==> !!! error: {err}.
| | {err:?}\n",
);
}
#[cfg(test)]
let cfg = "test";
#[cfg(feature = "parser-trace-stderr")]
let cfg = "feature = \"parser-trace-stderr\"";
eprint!(
"note: this trace was output as a debugging aid \
because `cfg({cfg})`.\n\n",
);
}
use ParseStatus::{Incomplete, Object};

View File

@ -159,6 +159,16 @@ impl<S: ParseState> TransitionData<S> {
_ => None,
}
}
/// Reference to parsing error,
/// if any.
#[cfg(any(test, feature = "parser-trace-stderr"))]
pub(in super::super) fn err_ref(&self) -> Option<&S::Error> {
match self {
TransitionData::Result(Err(e), _) => Some(e),
_ => None,
}
}
}
/// A verb denoting a state transition.

View File

@ -256,14 +256,14 @@ fn attr_value_error() {
}
impl Display for FooError {
fn fmt(&self, _f: &mut std::fmt::Formatter) -> std::fmt::Result {
unimplemented!()
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "test FooError")
}
}
impl Diagnostic for FooError {
fn describe(&self) -> Vec<AnnotatedSpan> {
unimplemented!()
vec![]
}
}

View File

@ -194,13 +194,13 @@ fn empty_element_with_attr_bindings() {
impl Display for AttrValueError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
unimplemented!()
write!(f, "test AttrValueError")
}
}
impl Diagnostic for AttrValueError {
fn describe(&self) -> Vec<crate::diagnose::AnnotatedSpan> {
unimplemented!()
vec![]
}
}