I prototyped a macro for a crate I've been working on, pidgin. Pidgin facilitates constructing vanilla regexes instrumented and wrapped to provide parse trees for non-recursive grammars. Right now it has a procedural interface which is pretty opaque. I wanted to make a macro so you could do this:
let g = grammar!{
TOP => <foo> | <bar>
bar => ("cat") | ("camel")
foo => ("dog") | ("dromedary")
};
The prototype works. The code.
pidgin-tester (master #) $ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.06s
Running `target/debug/pidgin-tester`
(?bBw)
larry := [((?), [G("bob", Some(5), Some(5), false)]), ((?), [G("bar", Some(3), None, false)]), ((?a), [S("this", None, Some(1), false), G("ba
r", Some(0), None, false), S("that", None, None, false), G("bob", Some(1), None, false)])]
bob := [((?), [R("foo")])]
bar := [((?i), [V(["baz", "plugh"], None, None, false)])]
(?)
foo := [((?), [S("bar", None, None, false)])]
pidgin-tester (master #) $
However, when I tried sticking it in pidgin
in src/macros.rs
I started getting double borrowing errors. Also, if I paste it into the playground and try to run it there I get the same errors. I don't understand what differentiates these contexts such that different borrowing is happening. Here is a sample error:
Compiling playground v0.0.1 (/playground)
error[E0499]: cannot borrow `m` as mutable more than once at a time
--> src/main.rs:18:28
|
18 | grammar!(@rules l, m, $($rest)+ );
| ^ mutable borrow starts here in previous iteration of loop
19 | PseudoGrammar::new(l, m, $mflags)
20 | });
| - mutable borrow ends here
...
274 | let g = grammar!{
| _____________-
275 | | (?bBw)
276 | | larry => <bob>{5} | <bar>{3,}
277 | | larry -> ("this")? <bar>* ("that") <bob>+
278 | | bob => r("foo")
279 | | bar => (?i) [&v]
280 | | };
| |_____- in this macro invocation
I'm a rust newbie. This is my first macro (and it's a bit of a doozie). Any help would be appreciated.