3

I've been reading up on semantic actions and I have a rule that looks like this:

  property_rule %=
    identifier_rule % ','
    >> lit(L":")
    >> type_specification_rule
    >> -(lit(L":=") >> +(alnum - ';'))
    >> lit(L";");

The property_rule is defined as

qi::rule<Iterator, property(), space_type> property_rule;

Now, I also want to support operator so what I want is to change the rule to something like

...
>> -(( lit(L":=") || lit(L"≡")[SEMANTIC_ACTION_HERE]) >> +(alnum - ';'))
...

In the semantic action, I want to change the property that is being parsed, specifically setting its field is_constant to true. The property is Fusion-adapted. How do I do it?

Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166

1 Answers1

3

I would - as ever - avoid the semantic action (Boost Spirit: "Semantic actions are evil"? ).

I'd simply synthesize the value for is_constant on both branches of the alternative:

>> -(( lit(L":=") || lit(L"≡")[SEMANTIC_ACTION_HERE]) >> +(alnum - ';'))

would become instead:

>> -(
        (L":=" >> attr(false) | L"≡" >> ::attr(true)) >> +(alnum - ';')
    )

Notes:

  1. the lit is implied
  2. you probably do not want || parser operator
  3. this assumes that the is_constant field is adapted in the fusion sequence
Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Umm, you are making the assumption that when the fusion sequence is adapted, the `bool is_constant` is present in the order such that it comes *before* the name of the variable. It doesn't. It comes after. As a consequence, `attr(false)` would try to assign a `bool` to a `wstring`. Any way to fix this? – Dmitri Nesteruk May 14 '17 at 18:44
  • Yeah. Fix the order in the rule. Adapt in a more useful order. Use a named fusion view :) (Also, I had no way to even guess about the presence or absence of "variables" in the grammar, either before or after the operator-like symbol. I just noted that since you felt comfortable just mentioning "fusion adapted", I was comfortable just mentioning I was relying on that). – sehe May 14 '17 at 20:31
  • I've changed fusion order but now the `+(alnum - ';')` part doesn't parse into its requisite `wstring`, any idea why that could be? – Dmitri Nesteruk May 15 '17 at 11:40
  • No, because you're not showing a SSCCE (out on a limb, try [`qi::as_wstring`](http://www.boost.org/doc/libs/1_64_0/libs/spirit/doc/html/spirit/qi/reference/directive/as.html)) – sehe May 15 '17 at 11:40
  • (In case you want to look at a Phoenix - not auto-propagate - approach, see e.g. the [PS. section here](http://stackoverflow.com/a/17347961/85371)) – sehe May 15 '17 at 11:40