Skip to content

Commit

Permalink
Wrap user-supplied code in immediately-invoked closure
Browse files Browse the repository at this point in the history
Allow `return` and `?` in the code block (#246), and prevent statements
like `break`/`continue` from messing up enclosing loops.
  • Loading branch information
kevinmehall committed Jan 18, 2021
1 parent 998cf96 commit 41fdbcb
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions peg-macros/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ fn compile_expr(context: &Context, e: &Expr, result_used: bool) -> TokenStream {
ActionExpr(ref exprs, ref code, is_cond) => labeled_seq(context, &exprs, {
if *is_cond {
quote! {
match { #code } {
match (||{ #code })() {
Ok(res) => ::peg::RuleResult::Matched(__pos, res),
Err(expected) => {
__err_state.mark_failure(__pos, expected);
Expand All @@ -622,7 +622,7 @@ fn compile_expr(context: &Context, e: &Expr, result_used: bool) -> TokenStream {
}
}
} else {
quote! { ::peg::RuleResult::Matched(__pos, { #code }) }
quote! { ::peg::RuleResult::Matched(__pos, (||{ #code })()) }
}
}),
MatchStrExpr(ref expr) => {
Expand Down Expand Up @@ -728,7 +728,7 @@ fn compile_expr(context: &Context, e: &Expr, result_used: bool) -> TokenStream {
{
quote! {
let #l_arg = __infix_result;
__infix_result = #action;
__infix_result = (||{#action})();
::peg::RuleResult::Matched(__pos, ())
}
},
Expand All @@ -744,7 +744,7 @@ fn compile_expr(context: &Context, e: &Expr, result_used: bool) -> TokenStream {
labeled_seq(context, &op.elements[..op.elements.len()-1], {
quote!{
if let ::peg::RuleResult::Matched(__pos, #r_arg) = __recurse(__pos, #new_prec, __state, __err_state) {
::peg::RuleResult::Matched(__pos, #action)
::peg::RuleResult::Matched(__pos, (||{#action})())
} else { ::peg::RuleResult::Failed }
}
})
Expand All @@ -753,7 +753,7 @@ fn compile_expr(context: &Context, e: &Expr, result_used: bool) -> TokenStream {
_ => {
// atom
pre_rules.push(labeled_seq(context, &op.elements, {
quote! { ::peg::RuleResult::Matched(__pos, #action) }
quote! { ::peg::RuleResult::Matched(__pos, (||{#action})()) }
}));
}
};
Expand Down

0 comments on commit 41fdbcb

Please sign in to comment.