qi::rule with inherited attribute as inherited attribute

混江龙づ霸主 提交于 2019-12-22 18:43:20

问题


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];


来源:https://stackoverflow.com/questions/42634929/qirule-with-inherited-attribute-as-inherited-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!