2

我需要一个带有选项的结构的逗号分隔输出。例如,如果我有这个结构:

MyStruct 
{ 
    boost::optional<std::string> one;
    boost::optional<int> two;
    boost::optional<float> three;
};

类似的输出:{ "string", 1, 3.0 } or { "string" } or { 1, 3.0 }等等。

现在,我有这样的代码:

struct MyStruct
{
    boost::optional<std::string> one;
    boost::optional<int> two;
    boost::optional<float> three;
};

BOOST_FUSION_ADAPT_STRUCT
(MyStruct,
one,
two,
three)

template<typename Iterator>
struct MyKarmaGrammar : boost::spirit::karma::grammar<Iterator, MyStruct()>
{
    MyKarmaGrammar() : MyKarmaGrammar::base_type(request_)
    {
        using namespace std::literals::string_literals;
        namespace karma = boost::spirit::karma;

        using karma::int_;
        using karma::double_;
        using karma::string;
        using karma::lit;
        using karma::_r1;

        key_ = '"' << string(_r1) << '"';

        str_prop_ = key_(_r1) << ':' 
            << string
            ;

        int_prop_ = key_(_r1) << ':' 
            << int_
            ;

        dbl_prop_ = key_(_r1) << ':' 
            << double_
            ;

        //REQUEST
        request_ = '{'
            <<  -str_prop_("one"s) <<
                -int_prop_("two"s) <<
                -dbl_prop_("three"s)
            << '}'
            ;
    }

private:
    //GENERAL RULES
    boost::spirit::karma::rule<Iterator, void(std::string)> key_;
    boost::spirit::karma::rule<Iterator, double(std::string)> dbl_prop_;
    boost::spirit::karma::rule<Iterator, int(std::string)> int_prop_;
    boost::spirit::karma::rule<Iterator, std::string(std::string)> str_prop_;

    //REQUEST
    boost::spirit::karma::rule<Iterator, MyStruct()> request_;
};

int main()
{
    using namespace std::literals::string_literals;

    MyStruct request = {std::string("one"), 2, 3.1};

    std::string generated;
    std::back_insert_iterator<std::string> sink(generated);

    MyKarmaGrammar<std::back_insert_iterator<std::string>> serializer;

    boost::spirit::karma::generate(sink, serializer, request);

    std::cout << generated << std::endl;
}

这可行,但我需要一个逗号分隔的输出。我尝试使用如下语法:

request_ = '{'
    <<  (str_prop_("one"s) |
        int_prop_("two"s) |
        dbl_prop_("three"s)) % ','
    << '}'
    ;

但我收到此编译错误:

/usr/include/boost/spirit/home/support/container.hpp:194:52: error: no type named ‘const_iterator’ in ‘struct MyStruct’
         typedef typename Container::const_iterator type;

谢谢!

4

1 回答 1

2

您的结构不是容器,因此 list-operator%将不起作用。文档声明它希望该属性是一个容器类型。

因此,就像在 Qi 对应部分中我向您展示了创建条件delim产生式一样:

 delim         = (&qi::lit('}')) | ',';

你在这里需要类似的东西。然而,关于它的一切都被颠倒了。我们不需要从 a 的存在中“检测”输入序列的结尾{,而是需要从“自从打开大括号以来没有输出字段”跟踪前面字段的缺失。

这有点棘手,因为所需的状态不能来自与输入相同的来源。为了简单起见,我们将在这里使用解析器成员¹:

private:
  bool _is_first_field;

现在,当我们生成左大括号时,我们要将其初始化为true

    auto _f = px::ref(_is_first_field); // short-hand
    request_ %= lit('{') [ _f = true ]

注意:使用 of%=而不是=告诉 Spirit 我们希望发生自动属性传播,尽管存在语义动作 ( [ _f = true ])

现在,我们需要生成分隔符:

    delim = eps(_f) | ", ";

简单的。用法也很简单,除了我们需要有条件 reset_f

    auto reset = boost::proto::deep_copy(eps [ _f = false ]);

    str_prop_ %= (delim << key_(_r1) << string  << reset) | ""; 
    int_prop_ %= (delim << key_(_r1) << int_    << reset) | ""; 
    dbl_prop_ %= (delim << key_(_r1) << double_ << reset) | ""; 

这里非常微妙的一点是,我将声明的规则属性类型从T更改为optional<T>。这允许 Karma 施展魔法使值生成器在它为空 ( boost::none) 时失败,并跳过reset!

ka::rule<Iterator, boost::optional<double>(std::string)> dbl_prop_;
ka::rule<Iterator, boost::optional<int>(std::string)> int_prop_;
ka::rule<Iterator, boost::optional<std::string>(std::string)> str_prop_;

现在,让我们整理一些测试用例:

测试用例

Live On Coliru

#include "iostream"
#include <boost/optional/optional_io.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <string>

struct MyStruct {
    boost::optional<std::string> one;
    boost::optional<int> two;
    boost::optional<double> three;
};

BOOST_FUSION_ADAPT_STRUCT(MyStruct, one, two, three)

namespace ka = boost::spirit::karma;
namespace px = boost::phoenix;

template<typename Iterator>
struct MyKarmaGrammar : ka::grammar<Iterator, MyStruct()> {
    MyKarmaGrammar() : MyKarmaGrammar::base_type(request_) {
        using namespace std::literals::string_literals;

        using ka::int_;
        using ka::double_;
        using ka::string;
        using ka::lit;
        using ka::eps;
        using ka::_r1;

        auto _f    = px::ref(_is_first_field);
        auto reset = boost::proto::deep_copy(eps [ _f = false ]);

        key_ = '"' << string(_r1) << "\":";

        delim = eps(_f) | ", ";

        str_prop_ %= (delim << key_(_r1) << string  << reset) | ""; 
        int_prop_ %= (delim << key_(_r1) << int_    << reset) | ""; 
        dbl_prop_ %= (delim << key_(_r1) << double_ << reset) | ""; 

        //REQUEST
        request_ %= lit('{') [ _f = true ]
            <<  str_prop_("one"s) <<
                int_prop_("two"s) <<
                dbl_prop_("three"s)
            << '}';
    }

  private:
    bool _is_first_field = true;
    //GENERAL RULES
    ka::rule<Iterator, void(std::string)> key_;
    ka::rule<Iterator, boost::optional<double>(std::string)> dbl_prop_;
    ka::rule<Iterator, boost::optional<int>(std::string)> int_prop_;
    ka::rule<Iterator, boost::optional<std::string>(std::string)> str_prop_;
    ka::rule<Iterator> delim;

    //REQUEST
    ka::rule<Iterator, MyStruct()> request_;
};

template <typename T> std::array<boost::optional<T>, 2> option(T const& v) {
    return { { v, boost::none } };
}

int main() {
    using namespace std::literals::string_literals;

    for (auto a : option("one"s))
    for (auto b : option(2))
    for (auto c : option(3.1))
    for (auto request : { MyStruct { a, b, c } }) {
        std::string generated;
        std::back_insert_iterator<std::string> sink(generated);

        MyKarmaGrammar<std::back_insert_iterator<std::string>> serializer;

        ka::generate(sink, serializer, request);

        std::cout << boost::fusion::as_vector(request) << ":\t" << generated << "\n";
    }
}

印刷:

( one  2  3.1): {"one":one, "two":2, "three":3.1}
( one  2 --):   {"one":one, "two":2}
( one --  3.1): {"one":one, "three":3.1}
( one -- --):   {"one":one}
(--  2  3.1):   {"two":2, "three":3.1}
(--  2 --):     {"two":2}
(-- --  3.1):   {"three":3.1}
(-- -- --):     {}

¹请注意,这限制了解析器的重入使用,以及使其成为非常量等karma::locals是对此的真正答案,增加了一点复杂性

于 2017-04-25T20:32:52.410 回答