How to force attribute propagation in x3 when using semantic actions
Alberto Luaces <[email protected]>
| Newsgroups | gmane.comp.parsers.spirit.general |
|---|---|
| Message-ID | <[email protected]> |
Hi,
I am just learning Spirit and x3. In my test, I parse a list of numbers
into a custom type resembling std::complex (just a pair of numbers).
Using BOOST_FUSION_ADAPT_STRUCT works just fine, but I am testing what
happens when I fill each structure by means of semantic actions. In
that case, I have to keep specifying manually the return value of each
rule, not only the one with semantic actions.
I have read that this is solved in Qi using the %= operator, but I do
not know how to do the same in x3.
Here is my rule definitions, just a modification of the first part of
the calc example:
namespace calculator_grammar
{
using x3::double_;
using x3::_val;
using x3::_attr;
// My custom type
struct comptype{
double a, b;
};
// This is the list rule aggregating the comptypes, but it only
// works if I specify the result type (std::vector<comptype>)
x3::rule<class _//, std::vector<comptype>, true
> const complist("complist");
// Semantic actions for filling the struct.
auto filla = [](auto & ctx){_val(ctx).a = _attr(ctx);};
auto fillb = [](auto & ctx){_val(ctx).b = _attr(ctx);};
// Specifying the return type just in this rule is fine by me,
// but I gues it does not have any effect whatsoever.
auto const mycomp =
x3::rule<class _, comptype>{"my comp parser"}=
x3::lit('(') >>
double_[filla] >> ',' >>
double_[fillb]>> ')'
;
// Let us pretend that this parses a list and not only two entities.
auto const complist_def =
mycomp >> "," >> mycomp;
BOOST_SPIRIT_DEFINE(
complist
);
auto calculator = complist;
}
using calculator_grammar::calculator;
}
How can I stop having to accumulate rule results (std::vector<...>) in
the definition of rules based on "mycomp"?
Thanks!
--
Alberto
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot