qi::rule with inherited attribute as inherited att

2019-05-26 06:01发布

问题:

Let's say we have a rule1

qi::rule<std::string::iterator, int()> rule1 = qi::int_[qi::_val=qi::_1];

And we decide getting an int as attribute is not enough, we also want to get the raw data (boost::iterator_range). We may have a lot of rules with the same type as rule1. So it's better to have a generic solution for this. Therefore we could define another rule2.

qi::rule<
    std::string::iterator,
    std::pair<int, boost::iterator_range<std::string::iterator>>(
        qi::rule<std::string::iterator, int()>&
    )
> rule2 = qi::raw[
    qi::lazy(qi::_r1)[at_c<0>(qi::_val)=qi::_1]
][at_c<1>(qi::_val)=qi::_1];

The rule2 is working well with the test code.

std::pair<int, boost::iterator_range<std::string::iterator>> result;
auto itBegin=boost::begin(str);
auto itEnd=boost::end(str);
if (qi::parse(itBegin, itEnd, rule2(phx::ref(rule1)), result)) {
    std::cout<<"MATCH! result = "<<result.first<<", "<<std::string(boost::begin(result.second), boost::end(result.second))<<std::endl;
} else {
    std::cout<<"NOT MATCH!"<<std::endl;
}

But if rule1 takes an inherited attribute say a bool.

qi::rule<std::string::iterator, int(bool)> rule1 = qi::int_[
    if_(qi::_r1)[qi::_val=qi::_1]
    .else_[qi::_val=-1]
;

For the testing purpose, we simple pass a true to rule1 from rule2.

qi::rule<
    std::string::iterator,
    std::pair<int, boost::iterator_range<std::string::iterator>>(
        qi::rule<std::string::iterator, int(bool)>&
    )
> rule2 = qi::raw[
    qi::lazy(qi::_r1)(true)[at_c<0>(qi::_val)=qi::_1]
][at_c<1>(qi::_val)=qi::_1];

But the compiler will report an error_invalid_e test xpression error. Is there something wrong in this? Thanks.

回答1:

phx::bind actually solves this problem.

qi::rule<
    std::string::iterator,
    std::pair<int, boost::iterator_range<std::string::iterator>>(
        qi::rule<std::string::iterator, int(bool)>&
    )
> rule2 = qi::raw[
    qi::lazy(phx::bind(qi::_r1,true))[at_c<0>(qi::_val)=qi::_1]
][at_c<1>(qi::_val)=qi::_1];