Problem Statement:
You have defined a structure in which you want to save result of a spirit's rule. There is another rule in your grammar which has exactly same data fields as the above rule but in a different order. How do you reuse your structure?
For this example lets use following structure:
struct Date
{
int month;
int day;
int year;
};
Now we convert it into a boost fusion sequence
BOOST_FUSION_ADAPT_STRUCT(
Date,
(int, month)
(int, day)
(int, year)
)
This is US date format.
we define this rule to parse US dates:
date_rule %= int_ >> lit(':') >> int_ >> lit(':') >> int_;
and we put the parsed result in our Date structure:
Date date;
phrase_parse(begin, end, date_rule, space, date);
now how to parse UK format dates? (day:month:year) ?
there are two options;
1) use Date structure to parse and swap the fields around.
2) define a new rule which does the swapping by itself.
here I describe option 2.
uk_date_format = int_ [at_c<1>(_val) = _1]
>> lit (':') >> int_[at_c<0>(_val) = _1]
>> lit (':') >> int_[at_c<2>(_val) = _1];
phrase_parse(begin, end, uk_date_format, space, date);
(at_c is used to access phoenix tuple.)
No comments:
Post a Comment