在Boost.Spirit中,为什么向量(包含在结构中)需要融合包装器,而不是变体?

时间:2017-11-28 22:11:04

标签: c++ boost-spirit boost-spirit-qi

我想了解使用Boost.Spirit封装BOOST_FUSION_ADAPT_STRUCT时需要struct的确切方案。

以下是两个例子。一个示例是具有(仅)struct数据成员的单成员variant。此版本不需要在Fusion容器中包装结构的BOOST_FUSION_ADAPT_STRUCT宏。构造函数足以让Spirit根据传入的rhs实例化/填充属性。

(请参阅代码中的注释,以了解由于属性折叠规则,我认为Boost.Spirit正在为规则定义的rhs生成的属性类型中的注释。)

第二个示例是单个成员struct,其中(仅)vector数据成员。即使定义了构造函数以允许Spirit基于rhs填充属性,它也无法在没有BOOST_FUSION_ADAPT_STRUCT的情况下进行编译。

为什么会有区别?我想了解为什么在第一种情况下,可以使用构造函数来填充属性(struct),而在第二种情况下,构造函数不够,必须使用BOOST_FUSION_ADAPT_STRUCT

上面提到的例子如下。

示例1:变体

#include <string>
#include <vector>
#include <boost/variant.hpp>
#include <boost/spirit/include/qi.hpp>
namespace qi  = boost::spirit::qi;
typedef std::string::const_iterator It;

using intermediate = boost::variant<std::string, int>;

// Simple parser demonstrating successful build with 'works_great'
struct works_great // No need for BOOST_FUSION_ADAPT_STRUCT - whoopee!
                   // But why - even given the constructor??
{
    intermediate i;
    works_great() = default;
    works_great(intermediate i) : i{i} {}
};

// Not required for 'works_great' - constructors work just fine
//BOOST_FUSION_ADAPT_STRUCT(works_great, v)

struct parser : qi::grammar<It, works_great()>
{
    parser() : parser::base_type(works_great)
    {
        using namespace qi;
        intermediate = qi::string("test") | qi::int_;

        // rhs should have attribute of type 'variant',
        // matching the constructor
        works_great = '{' >> intermediate >> '}';
    }

  private:
    qi::rule<It, intermediate()>  intermediate;
    qi::rule<It, works_great()>   works_great;
};

int main()
{
    // The following all compiles/builds just fine
    // (I don't care about the actual runtime results).
    static const parser p;
    works_great wg;
    std::string const data {"{test}"};
    auto f(begin(data)), l(end(data));
    qi::parse(f,l,p,wg);
}

示例2:矢量

#include <string>
#include <vector>
#include <boost/variant.hpp>
#include <boost/spirit/include/qi.hpp>
namespace qi  = boost::spirit::qi;
typedef std::string::const_iterator It;

// We need BOOST_FUSION_ADAPT_STRUCT for this one, but not for the above.
// Constructors don't help. Only difference seems to be
// the vector (rather than variant).
struct not_so_much // not so much - unless BOOST_FUSION_ADAPT_STRUCT is used
{
    std::vector<int> s;

    // Constructors do not help here
    //not_so_much() = default;
    //not_so_much(std::vector<int> s) : s{std::move(s)} {}
};

// Required for 'not_so_much' - constructors don't work
BOOST_FUSION_ADAPT_STRUCT(not_so_much, s)

// Simple parser demonstrating successful build with 'not_so_much' -
// but only when BOOST_FUSION_ADAPT_STRUCT is used.
struct parser : qi::grammar<It, not_so_much()>
{
    parser() : parser::base_type(not_so_much)
    {
        using namespace qi;

        // Note: I know that 'eps' is required, below, to compile the 
        // single-member struct successfully

        // rhs should have attribute of type 'vector<int>',
        // matching the constructor as well...
        // but it doesn't work.
        not_so_much = eps >> (qi::int_ % "|");
    }

  private:
    qi::rule<It, not_so_much()> not_so_much;
};

int main()
{
    // The following all compiles/builds just fine
    static const parser p;
    not_so_much nm;
    std::string const data {"5|9|16"};
    auto f(begin(data)), l(end(data));
    qi::parse(f,l,p,nm);
}

1 个答案:

答案 0 :(得分:3)

差异是双重的:

  • 该属性不是容器
  • 默认构造函数允许将合成属性隐式转换为公开属性

后者的区别,你注意到了。第一个:不是那么多。

真正有原则的答案是:

  

Qi属性传播是一种启发式机器。

     

可悲的是,很少有东西可以优化性能(X3做得更好)。其中一个关键区域是一个例外,它是对容器的增量解析(即使是跨多个规则)¹。

     

这很有意义(因为即使例如逐个字符构建字符串也会非常慢......)。但它确实会带来意外(例如。boost::spirit::qi duplicate parsing on the outputUnderstanding Boost.spirit's string parser

     

¹(实际上也是非容器,但我离题了。我不认为它会在没有语义动作的情况下发挥作用)

一些不必要的体操

你实际上可以改变属性传播的时间点,并且没有适应性,但是我建议反对它:只是调整更加一致和自我描述:

<强> Live On Coliru

#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;

namespace Ast {
    using vec = std::vector<int>;
    struct not_so_much {
        vec s;

        not_so_much() = default;
        not_so_much(vec s) : s(std::move(s)) {}
    };
}

typedef std::string::const_iterator It;
typedef qi::rule<It, Ast::not_so_much()> Parser;

template <typename Expr> void do_test(Expr const& expression) {
    Parser const p = expression;
    Ast::not_so_much nm;

    std::string const data {"5|9|16"};
    It f = begin(data), l = end(data);

    if (qi::parse(f,l,p,nm)) {
        std::cout << "Parsed " << nm.s.size() << " elements: ";
        copy(nm.s.begin(), nm.s.end(), std::ostream_iterator<int>(std::cout, " "));
        std::cout << "\n";
    } else {
        std::cout << "Parse failed\n";
    }

    if (f != l)
        std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
}

int main() {
    using namespace qi;
    do_test(attr_cast<Ast::not_so_much, Ast::vec>(int_ % '|'));
    do_test(attr_cast<Ast::not_so_much>(int_ % '|'));

    do_test(as<Ast::vec>()[int_ % '|']);
}

打印

Parsed 3 elements: 5 9 16 
Parsed 3 elements: 5 9 16 
Parsed 3 elements: 5 9 16