boost :: spirit :: x3属性兼容性规则,直觉还是代码? [英] boost::spirit::x3 attribute compatibility rules, intuition or code?

查看:265
本文介绍了boost :: spirit :: x3属性兼容性规则,直觉还是代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么地方描述了各种spirit :: x3规则定义操作如何影响属性兼容性?



我很惊讶于:

  x3 :: lexeme [x3 :: alpha> *(x3 :: alnum | x3 :: char _('_'))] 

移动到融合适应的结构:

  struct Name {
std :: string value;
};

暂时,我摆脱了第一个强制性字母字符,但我仍然想表达一个定义名称字符串必须以字母开头的规则。是这种情况下,我需要尝试添加 eps ,直到它工作,或有一个陈述的原因,为什么上述不能工作?

$

解决方案

如果这是写在某个地方, p>如果你不在开发中,你没有修复单元素序列适应性错误,所以是可能的。



由于属性变换/传播的一般性,有很多摆动的空间,但当然它只是记录,最终在代码中。换句话说:没有魔法。



在天气里,我会通过 qi :: as<> qi :: attr_cast<> 。 X3没有它,但你可以使用一个规则来模仿它很容易:



Live on Coliru

  #include< iostream> 
#include< boost / fusion / adapted / struct.hpp>
#include< boost / spirit / home / x3.hpp>

命名空间x3 = boost :: spirit :: x3;

struct Name {
std :: string value;
};

BOOST_FUSION_ADAPT_STRUCT(名称,值)

int main(){

std :: string const input =Halleo123_1;
名字

bool ok = x3 :: parse(input.begin(),input.end(),
x3 :: rule< struct _,std :: string> {} =
x3 :: alpha>> *(x3 :: alnum | x3 :: char _('_')),
out);

if(ok)
std :: cout<< Parsed:<< out.value<< \\\
;
else
std :: cout<< 解析失败\\\
;
}

列印:

 已解析:Halleo123_1 



自动完成



因为X3使用c ++ 14核心语言功能很好,所以不难减少输入:



了解Boost.Spirit中的列表运算符(%)


Is there a document somewhere which describes how various spirit::x3 rule definition operations affect attribute compatibility?

I was surprised when:

x3::lexeme[ x3::alpha > *(x3::alnum | x3::char_('_')) ]

could not be moved into a fusion-adapted struct:

struct Name {
    std::string value;
};

For the time being, I got rid of the first mandatory alphabetical character, but I would still like to express a rule which defines that the name string must begin with a letter. Is this one of those situations where I need to try adding eps around until it works, or is there a stated reason why the above couldn't work?

I apologize if this has been written down somewhere, I couldn't find it.

解决方案

If you're not on the develop you don't have the fix for that single-element sequence adaptiation bug, so yeah it's probably that.

Due to the genericity of attribute transformation/propagation, there's a lot of wiggle room, but of course it's just documented and ultimately in the code. In other words: there's no magic.

In the Qi days I'd have "fixed" this by just spelling out the desired transform with qi::as<> or qi::attr_cast<>. X3 doesn't have it (yet), but you can use a rule to mimick it very easily:

Live On Coliru

#include <iostream>
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/home/x3.hpp>

namespace x3 = boost::spirit::x3;

struct Name {
    std::string value;
};

BOOST_FUSION_ADAPT_STRUCT(Name, value)

int main() {

    std::string const input = "Halleo123_1";
    Name out;

    bool ok = x3::parse(input.begin(), input.end(),
            x3::rule<struct _, std::string>{} =
            x3::alpha >> *(x3::alnum | x3::char_('_')),
            out);

    if (ok)
        std::cout << "Parsed: " << out.value << "\n";
    else
        std::cout << "Parse failed\n";
}

Prints:

Parsed: Halleo123_1

Automate it

Because X3 works so nicely with c++14 core language features, it's not hard to reduce typing:

Understanding the List Operator (%) in Boost.Spirit

这篇关于boost :: spirit :: x3属性兼容性规则,直觉还是代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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