tamer: New parser-trace-stderr feature flag

This flag allows toggling the parser trace that was previously only
available to tests.  Unfortunately, at the time of writing, Cargo cannot
enable flags in profiles, so I have to check for either `test` or this flag
being set to enable relevant features.

This trace is useful as I start to run the parser against existing code
written in TAME so that our existing systems can help to guide my
development.  Unlike the current tests, it also allows seeing real-world
data as part of the lowering pipeline, where multiple `Parser`s are in
play.

Having this feature flag also makes this feature more easily discoverable to
those wishing to observe how the lowering pipeline works.

DEV-7145
main
Mike Gerwitz 2022-07-21 22:05:21 -04:00
parent de35cc37fd
commit 422f3d9c0c
3 changed files with 21 additions and 6 deletions

View File

@ -46,3 +46,10 @@ unicode-width = "0.1.5"
# and the flag removed.
[features]
# Cause `Parser` to emit a verbose, human-readable trace to stderr for every
# token. This is not intended to be machine-readable, so please do not
# parse it.
#
# This is enabled automatically for the `test` profile.
parser-trace-stderr = []

View File

@ -265,7 +265,11 @@ impl<S: ParseState, I: TokenStream<S::Token>> Parser<S, I> {
// then you may have a `Display::fmt` or `Debug::fmt` panic,
// like a `todo!` or `unimplemented!`,
// in your `Token` or `ParseState`.
#[cfg(test)]
//
// Unfortunately Cargo can't enable this feature for us for
// profiles;
// see <https://github.com/rust-lang/cargo/issues/2911>.
#[cfg(any(test, feature = "parser-trace-stderr"))]
{
let st = self.state.as_ref().unwrap();
@ -300,17 +304,21 @@ impl<S: ParseState, I: TokenStream<S::Token>> Parser<S, I> {
self.state.replace(state);
// Remainder of the trace after the transition.
#[cfg(test)]
#[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: {:?}
= note: this trace was output as a debugging aid because `cfg(test)`.\n\n",
data.lookahead_ref()
| | Lookahead: {la:?}
= note: this trace was output as a debugging aid because `cfg({cfg})`.\n\n",
la = data.lookahead_ref(),
);
}

View File

@ -149,7 +149,7 @@ pub(in super::super) enum TransitionData<S: ParseState> {
impl<S: ParseState> TransitionData<S> {
/// Reference to the token of lookahead,
/// if any.
#[cfg(test)]
#[cfg(any(test, feature = "parser-trace-stderr"))]
pub(in super::super) fn lookahead_ref(
&self,
) -> Option<&Lookahead<S::Token>> {