qi :: rule,将继承的属性作为继承的属性 [英] qi::rule with inherited attribute as inherited attribute

查看:98
本文介绍了qi :: rule,将继承的属性作为继承的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一条规则1

Let's say we have a rule1

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

我们认为获取一个int作为属性是不够的,我们还想获取原始数据(boost :: iterator_range).我们可能有很多与Rule1类型相同的规则.因此,最好有一个通用的解决方案.因此,我们可以定义另一个规则2.

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

rule2与测试代码配合良好.

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

但是,如果rule1具有继承的属性,请说一个布尔值.

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

出于测试目的,我们将true从rule2传递给rule1.

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

但是编译器将报告error_invalid_e测试压缩错误.这有什么问题吗?谢谢.

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

推荐答案

phx :: bind实际上解决了此问题.

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

这篇关于qi :: rule,将继承的属性作为继承的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆