Consider strings of the form (int ":" int )*. I want to parse such a string into a vector of integers, in the following way: if the second value is odd, add the first to the result. Otherwise, do nothing for this pair.
I am trying around with
auto pair_ = x3::rule<class pair_, int>()
= x3::int_ >> ":" >> x3::int_;
auto vec = x3::rule<class vec, std::vector<int>>()
= (*pair_[
([](auto &c){
if(x3::_attr(c).second % 2)
x3::_val(c).push_back(x3::_attr(c).first);
})
]);
which seems to be wrong.
Additional task: make : int optional, and default to 1. I tried
auto pair_ = x3::rule<class pair_, int>()
= x3::int_ >> ((":" >> x3::int_) | x3::attr(1));
Example: 10 11:0 12:3 should become the vector [10, 12].
which also seems to be wrong.
How do I do it correctly?
Your
pair_rule exposes only an int. ChangingWill work, although you have to include
directly or indirectly. The optional version also just works:
Live On Coliru
Printing