I am trying to write a shell language parser in Boost.Spirit. However, I am unclear about some basic issues regarding semantics of rule
s.
Looking at the documentation, there are members r.alias()
and r.copy()
of rule
. IIUC, these members should return a reference to the rule and a copy of the rule's contents, respectively. However, it is not clearly specified what happens when I just use the rule in a definition of another rule. From my experiments, I found mutually recursive rules can be defined by:
rule<Iter> r1, r2;
r1 = ... >> r2 >> ...;
r2 = ... >> r1 >> ...;
which suggests the rules are taken by reference inside parser expressions. The problem is, what does it do when the variable goes out of scope, eg:
rule<Iter> r1;
{
rule<Iter> r2;
r1 = ... >> r2 >> ...;
r2 = ... >> r1 >> ...;
}
... // use r1
On the same note, would assigning to a rule from a parsing expression containing a rvalue of type rule work (r.copy()
would be a rvalue of type rule
too, isn't it)? eg.
rule<Iter> f() { return char_('a') << char_('b'); }
rule<Iter> r1 = ... << f();
Can anybody enlighten me on the detailed semantics of rule
's copies and references, and possibly correct any misconceptions in this post?