Spirit :: Qi:deogle绑定到google-protobuf

时间:2016-10-11 08:15:20

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

我一直在使用boost :: spirit实现一个解析器,需要在输出时生成google :: protobuf生成的类。

我尝试按照page作为背景。不幸的是,我无法使用属性语法,因为google :: protobuf生成的类只提供了set / get方法。所以,我尝试使用boost :: phoenix进行deferred绑定,但我不确定如何绑定A类的add_param()方法(参见下面的代码和第99行的注释):

#define BOOST_SPIRIT_DEBUG

#include <string>
#include <iostream>
#include <sstream>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/std_pair.hpp>

namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;

class A
{
public:
    A(): _i(0) {}
    A(int i) : _i(i) 
    {
        std::cout << "i=" << i << std::endl;
    }

    void set_i(int i) 
    { 
        _i = i; 
    }

    void add_param(const std::string& value)
    {
        _params.push_back(value);
    }

    std::string to_string() const
    {
        std::stringstream ss;
        ss << "_i=[" << _i << "], _params=[";
        for (auto& p : _params)
            ss << p << ",";
        ss << "]" << std::endl;
        return ss.str();
    }

private:
    int _i;
    std::vector < std::string > _params;
};

class B
{
public:
    B() : _id(), _av() {}
    B(int id, const std::vector<A>& av) : _id(id), _av(av) {}

    void set_id(int id)
    {
        _id = id;
    }

    void add_A(const A& a)
    {
        _av.push_back(a);
    }

    std::string to_string() const
    {
        std::stringstream ss;
        ss << "_id=[" << _id << "], _av=[";
        for (auto& av : _av)
            ss << av.to_string() << ",";
        ss << "]" << std::endl;
        return ss.str();
    }
private:
    int _id;
    std::vector<A> _av;
};

namespace Utils
{
    int mul2(int i) 
    { 
        return i * 2; 
    }
}

template <typename Iterator, typename Skipper>
struct grammar_B: qi::grammar<Iterator, Skipper>
{
    grammar_B(B& ctx) : grammar_B::base_type(start)
    {
        // A
        i_rule = qi::int_[ qi::_val = phx::bind(Utils::mul2, qi::_1) ];
        param_rule = *(qi::char_ - ',' - '!');

        a_rule = (qi::lit("$")
                  >> i_rule
                  >> qi::lit(":")
                  >> *(param_rule >> +(qi::lit(","))) // how to bind to A::add_param() ?
                  >> qi::lit("!")) [ qi::_val = phx::construct<A>(qi::_1) ];

        // B
        id_rule = qi::int_[ phx::bind(&B::set_id, phx::ref(ctx), qi::_1) ];     

        start %= id_rule >> qi::lit("=") >> a_rule;

        BOOST_SPIRIT_DEBUG_NODE(id_rule);
        BOOST_SPIRIT_DEBUG_NODE(i_rule);
        BOOST_SPIRIT_DEBUG_NODE(param_rule);
    }
private:
    qi::rule<Iterator, Skipper> start;

    qi::rule<Iterator, int, Skipper> i_rule;
    qi::rule<Iterator, std::string(), Skipper> param_rule;
    qi::rule<Iterator, A(), Skipper> a_rule;

    qi::rule<Iterator, int, Skipper> id_rule;
};

int main(int argc, char* argv[])
{

    std::string input = "1=$100:param1,param2!";
    B ctx;
    grammar_B<decltype(std::begin(input)), qi::space_type> p(ctx);

    auto f(std::begin(input)), l(std::end(input));

    if (qi::phrase_parse(f, l, p, qi::space))
        std::cout << "Success: " << ctx.to_string() << std::endl;
    else
        std::cout << "failed" << std::endl;

    return 0;
}

Coliru Example

目前,我有两个问题:

1)我的方法是否可以通过延迟绑定实现?

2)如何绑定std :: vector&lt;&gt;生成属性,如A :: add_param()?

-

由于

1 个答案:

答案 0 :(得分:0)

好的,最后我能够实现我需要的东西: Coliry Example