rust-peg icon indicating copy to clipboard operation
rust-peg copied to clipboard

`&mut` rule argument doesn't work with `precedence!{}`

Open kevinmehall opened this issue 1 year ago • 0 comments

Minimal example:

    pub rule mut_prec(a: &mut ()) -> () = precedence! {
        "a" { a; () }
    }
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
error[E0507]: cannot move out of `a`, a captured variable in an `Fn` closure
  --> $DIR/rule_args.rs:31:13
   |
30 |     pub rule mut_prec(a: &mut ()) -> () = precedence! {
   |                       -                   ---------- captured by this `Fn` closure
   |                       |
   |                       captured outer variable
31 |         "a" { a; () }
   |             ^^-^^^^^^
   |             | |
   |             | move occurs because `a` has type `&mut ()`, which does not implement the `Copy` trait
   |             move out of `a` occurs here
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈

Discussed in https://github.com/kevinmehall/rust-peg/discussions/312

Originally posted by lfnoise July 17, 2022 I created a working parser for a non-trivial language syntax in a few hours, so that was pretty neat, thanks.

I needed to pass a &mut symbol table struct down the tree of parsing rules. This worked fine for all but precedence climbing and left recursive rules. The left recursion gave me an error message that arguments weren't supported.. fair enough. Precedence climbing just wouldn't compile. It gave an error about not being able to capture a mutable in the rule closure and needing a FnMut instead of a Fn. Left recursion was easy to solve, but creating separate nested rules for a dozen levels of precedence by hand was not so nice. My parser works, but is not as simple it would be with the precedence! macro.

Did I do something wrong? Is there a way to pass an argument down into the @ (next lower precedence rule) somehow? Here's the code. Thank you.

kevinmehall avatar Jul 18 '22 05:07 kevinmehall