Suppose I have a structure that I want to parse into with Spirit Qi, that is defined as such:
struct data_
{
bool export;
std::wstring name;
data_() : export(false) {}
};
Also, suppose the struct has been adapted to fusion like this:
BOOST_FUSION_ADAPT_STRUCT(
data_,
(bool, export)
(std::wstring, name)
)
And the associated rule is:
qi::rule<Iterator, data_(), skipper<Iterator> > rule_data;
rule_data = -lexeme["SpecialText" >> !(alnum | '_')] [ boost::phoenix::at_c<0> = true ] // If this string is found, , set "export" to true
> lexeme["Name" >> !(alnum | '_')] // this is supposed to go into the "name" member
This compiles, so far, so good. However, "name" stays empty now!
So essentially, I am asking: Given that "SpecialText" precedes "Name", how would I synthesize a boolean attribute for "export" properly, rather than a string?
EDIT After pulling my hair out on this, I randomly stumbled upon the "matches[]" parser, which seems to do what I want.
Nonetheless, the question still exists in the general form, for example, if I wanted to return a certain string or other data type instead of a bool. Essentially, how to set a specific member of a struct attribute via a semantic action.
How to set a struct member.
Option 1 (
phx::bind
)Given a struct
S
You can assign to a field (e.g.
target_field
) like so:Now, you can make the
bind
more readable, by doing something like:Proof of concept: live on Coliru
Option 2 (fusion sequences)
You can treat a struct as a fusion sequence by using adaptation:
Now you can use phoenix lazy functions on these sequences in your semantic action:
I don't prefer this style (because it 'degrades' an expressive struct to ... a tuple of sorts), but it might come in handy. Live on Coliru