Skip to content

Commit 2dc6a97

Browse files
committed
refactor tracing for clearer printouts
1 parent af41c9a commit 2dc6a97

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

crates/formality-core/src/parse/parser.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ where
2424
{
2525
scope: &'s Scope<L>,
2626
start_text: &'t str,
27-
#[allow(dead_code)]
28-
tracing_span: tracing::span::EnteredSpan,
2927
nonterminal_name: &'static str,
3028
successes: Vec<(SuccessfulParse<'t, T>, Precedence)>,
3129
failures: Set<ParseError<'t>>,
@@ -96,21 +94,20 @@ where
9694
name = nonterminal_name,
9795
?scope,
9896
?text
99-
)
100-
.entered();
97+
);
98+
let guard = tracing_span.enter();
10199

102100
let mut parser = Self {
103101
scope,
104102
start_text: text,
105103
nonterminal_name,
106-
tracing_span,
107104
successes: vec![],
108105
failures: set![],
109106
};
110107

111108
op(&mut parser);
112109

113-
parser.finish()
110+
parser.finish(guard)
114111
}
115112

116113
/// Shorthand for `parse_variant` where the parsing operation is to
@@ -184,7 +181,7 @@ where
184181
}
185182
}
186183

187-
fn finish(self) -> ParseResult<'t, T> {
184+
fn finish(self, guard: tracing::span::Entered<'_>) -> ParseResult<'t, T> {
188185
// If we did not parse anything successfully, then return an error.
189186
// There are two possibilities: some of our variants may have made
190187
// progress but ultimately failed. If so, self.failures will be non-empty,
@@ -195,6 +192,8 @@ where
195192
// observe that we did not consume any tokens and will ignore our messawge
196193
// and put its own (e.g., "failed to find a Y here").
197194
if self.successes.is_empty() {
195+
// It's better to print this result alongside the main parsing section.
196+
drop(guard);
198197
return if self.failures.is_empty() {
199198
tracing::trace!("parsing failed: no variants were able to consume a single token");
200199
Err(ParseError::at(
@@ -228,6 +227,8 @@ where
228227
.all(|(s_j, j)| i == j || Self::is_preferable(s_i, s_j))
229228
{
230229
let (s_i, _) = self.successes.into_iter().skip(i).next().unwrap();
230+
// It's better to print this result alongside the main parsing section.
231+
drop(guard);
231232
tracing::trace!("best parse = `{:?}`", s_i);
232233
return Ok(s_i);
233234
}

0 commit comments

Comments
 (0)