boost::spirit::x3 属性兼容性规则,直觉还是代码?

boost::spirit::x3 attribute compatibility rules, intuition or code?

是否有文档描述了各种 spirit::x3 规则定义操作如何影响属性兼容性?

我很惊讶:

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

无法移动到适应融合的结构中:

struct Name {
    std::string value;
};

我暂时去掉了第一个强制性的字母字符,但我仍然想表达一个规则,定义名称字符串必须以字母开头。这是我需要尝试添加 eps 直到它工作的情况之一,还是有上述无法工作的明确原因?

很抱歉,如果这已经写在某个地方,我找不到了。

如果你不在开发分支上,你就没有修复那个单元素序列适应错误,所以是的,可能就是这样。

由于属性 transformation/propagation 的通用性,有很大的回旋余地,但当然它只是记录在案并最终在代码中。换句话说:没有魔法。

在 Qi 时代,我只需用 qi::as<>qi::attr_cast<> 拼写出所需的转换就可以得到 "fixed"。 X3 还没有,但你可以使用规则很容易地模仿它:

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

打印:

Parsed: Halleo123_1

自动化

因为 X3 与 c++14 核心语言特性配合得很好,所以减少输入并不难: