13

我正在尝试使用带有可变重复因子的 Boost Spirit X3 指令重复。基本思想是标头+有效负载,其中标头指定有效负载的大小。一个简单的示例“3 1 2 3”被解释为 header = 3, data= {1, 2, 3} (3 个整数)。

我只能从灵气文档中找到例子。它使用 boost phoenix reference 来包装变量因子: http: //www.boost.org/doc/libs/1_50_0/libs/spirit/doc/html/spirit/qi/reference/directive/repeat.html

std::string str;
int n;
test_parser_attr("\x0bHello World",
    char_[phx::ref(n) = _1] >> repeat(phx::ref(n))[char_], str);
std::cout << n << ',' << str << std::endl;  // will print "11,Hello World"

我为精神 x3 写了以下简单的例子,没有运气:

#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <string>
#include <iostream>

namespace x3 = boost::spirit::x3;
using x3::uint_;
using x3::int_;
using x3::phrase_parse;
using x3::repeat;
using x3::space;
using std::string;
using std::cout;
using std::endl;

int main( int argc, char **argv )
{
  string data("3 1 2 3");
  string::iterator begin = data.begin();
  string::iterator end = data.end();

  unsigned int n = 0;

  auto f = [&n]( auto &ctx ) { n = x3::_attr(ctx); };
  bool r = phrase_parse( begin, end, uint_[f] >> repeat(boost::phoenix::ref(n))[int_], space );
  if ( r && begin == end  )
    cout << "Parse success!" << endl; 
  else
    cout << "Parse failed, remaining: " << string(begin,end) << endl;

  return 0;
}

使用 boost 1.59.0 和 clang++ (flags: -std=c++14) 编译上面的代码会得到以下结果:

boost_1_59_0/boost/spirit/home/x3/directive/repeat.hpp:72:47: error: no matching constructor for

      initialization of 'proto_child0' (aka 'boost::reference_wrapper<unsigned int>')

            typename RepeatCountLimit::type i{};

如果我硬编码repeat(3)而不是repeat(boost::phoenix::ref(n))它可以正常工作,但这不是一个可能的解决方案,因为它应该支持可变重复因子。

编译repeat(n)成功完成,但解析失败,输出如下: “Parse failed, remaining: 1 2 3"

查看源代码,它调用模板类型变量boost/spirit/home/x3/directive/repeat.hpp:72的空构造函数,然后在 for 循环期间分配,迭代 min 和 max。然而,由于类型是一个引用,它应该在构造函数中初始化,所以编译失败。查看之前库版本 boost/spirit/home/qi/directive/repeat.hpp:162 的等效源代码,它是直接分配的:RepeatCountLimit::typei

        typename LoopIter::type i = iter.start();

我不确定我在这里做错了什么,或者 x3 当前是否不支持可变重复因子。我将不胜感激解决此问题的一些帮助。谢谢你。

4

1 回答 1

10

根据我收集到的信息,阅读源代码和邮件列表,Phoenix 根本没有集成到 X3 中:原因是 c++14 使得它的大部分都过时了。

我同意这留下了 Qi 曾经有优雅解决方案的一些地方,例如eps(DEFERRED_CONDITION)lazy(*RULE_PTR)Nabialek 技巧),事实上,这种情况。

Spirit X3 仍在开发中,因此我们可能会看到添加了此功能¹

目前,Spirit X3 有一个用于有状态上下文的通用工具。在某些情况下,这基本上替换locals<>了继承的参数,并且在这种特殊情况下也可以/制作/验证元素的数量:

  • x3::with²

以下是您可以使用它的方法:

with<_n>(std::ref(n)) 
    [ omit[uint_[number] ] >> 
    *(eps [more] >> int_) >> eps [done] ]

这里,_n是一个标记类型,用于标识要检索的上下文元素get<_n>(cxtx)

请注意,目前我们必须对左值使用引用包装器,n因为这with<_n>(0u)会导致上下文中的常量元素。我想这也是随着 X# 的成熟而提升的 QoI

现在,对于语义操作:

unsigned n;
struct _n{};

auto number = [](auto &ctx) { get<_n>(ctx).get() = _attr(ctx); };

这会将解析的无符号数存储到上下文中。(事实上​​,由于ref(n)绑定,它现在实际上并不是上下文的一部分,如前所述

auto more   = [](auto &ctx) { _pass(ctx) = get<_n>(ctx) >  _val(ctx).size(); };

在这里,我们检查我们实际上不是“满的”——即允许更多的整数

auto done   = [](auto &ctx) { _pass(ctx) = get<_n>(ctx) == _val(ctx).size(); };

在这里,我们检查我们是否“满” - 即不允许更多整数。

把它们放在一起:

Live On Coliru

#include <string>
#include <iostream>
#include <iomanip>

#include <boost/spirit/home/x3.hpp>

int main() {
    for (std::string const input : { 
            "3 1 2 3", // correct
            "4 1 2 3", // too few
            "2 1 2 3", // too many
            // 
            "   3 1 2 3   ",
        })
    {
        std::cout << "\nParsing " << std::left << std::setw(20) << ("'" + input + "':");

        std::vector<int> v;

        bool ok;
        {
            using namespace boost::spirit::x3;

            unsigned n;
            struct _n{};

            auto number = [](auto &ctx) { get<_n>(ctx).get() = _attr(ctx); };
            auto more   = [](auto &ctx) { _pass(ctx) = get<_n>(ctx) >  _val(ctx).size(); };
            auto done   = [](auto &ctx) { _pass(ctx) = get<_n>(ctx) == _val(ctx).size(); };

            auto r = rule<struct _r, std::vector<int> > {} 
                  %= with<_n>(std::ref(n)) 
                        [ omit[uint_[number] ] >> *(eps [more] >> int_) >> eps [done] ];

            ok = phrase_parse(input.begin(), input.end(), r >> eoi, space, v);
        }

        if (ok) {
            std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout << v.size() << " elements: ", " "));
        } else {
            std::cout << "Parse failed";
        }
    }
}

哪个打印:

Parsing '3 1 2 3':          3 elements: 1 2 3 
Parsing '4 1 2 3':          Parse failed
Parsing '2 1 2 3':          Parse failed
Parsing '   3 1 2 3   ':    3 elements: 1 2 3 

¹ 在 [spirit-general] 邮件列表中提供您的支持/声音 :)

² 找不到合适的文档链接,但在一些示例中使用

于 2015-11-10T10:34:17.090 回答