Skip to content

Commit ac30d3b

Browse files
committed
adopt final_fn crate
This makes us panic safe, but it also just reads much more nicely.
1 parent e95500c commit ac30d3b

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/formality-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ tracing-tree = { version = "0.2" }
2020
formality-macros = { version = "0.1.0", path = "../formality-macros" }
2121
anyhow = "1.0.75"
2222
contracts = "0.6.3"
23+
final_fn = "0.1.0"
2324

2425
[dev-dependencies]
2526
expect-test = "1.4.1"

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,11 @@ where
149149
};
150150
}
151151

152-
let pop_stack_before_return = |r: ParseResult<'t, T>| {
153-
STACK.with_borrow_mut(|stack| {
154-
let top = stack.pop().unwrap();
155-
assert!(top.matches::<L, T>(scope, text));
156-
});
157-
r
158-
};
152+
// Pop the stack before we return
153+
final_fn::final_fn!(STACK.with_borrow_mut(|stack| {
154+
let top = stack.pop().unwrap();
155+
assert!(top.matches::<L, T>(scope, text));
156+
}));
159157

160158
// EXAMPLE: Consider this grammar
161159
//
@@ -192,13 +190,13 @@ where
192190
let mut values = vec![];
193191
match op() {
194192
Ok(v) => values.push(v),
195-
Err(errs) => return pop_stack_before_return(Err(errs)),
193+
Err(errs) => return Err(errs),
196194
};
197195

198196
// Check whether there was recursion to begin with.
199197
let observed = with_top!(|top| top.observed);
200198
if !observed {
201-
return pop_stack_before_return(Ok(values.pop().unwrap())); // If not, we are done.
199+
return Ok(values.pop().unwrap()); // If not, we are done.
202200
}
203201

204202
// OK, this is the interesting case. We may be able to get a better parse.
@@ -219,7 +217,7 @@ where
219217
// Invoke the operation. As noted above, if we get a failed parse NOW,
220218
// we know we already found the best result, so we can just use it.
221219
let Ok(value1) = op() else {
222-
return pop_stack_before_return(Ok(values.pop().unwrap())); // If not, we are done.
220+
return Ok(values.pop().unwrap()); // If not, we are done.
223221
};
224222

225223
tracing::trace!("left-recursive grammar yielded: value1 = {:?}", value1);
@@ -230,7 +228,7 @@ where
230228
// succeeds, but we have to try again to see if there's a more complex
231229
// expression that can be produced (there isn't).
232230
if values.iter().any(|v| *v == value1) {
233-
return pop_stack_before_return(Ok(values.pop().unwrap())); // If not, we are done.
231+
return Ok(values.pop().unwrap()); // If not, we are done.
234232
}
235233

236234
// Otherwise, we have to try again.

0 commit comments

Comments
 (0)