2016-10-11 78 views
1

我一直在用boost :: spirit實現一個解析器,它需要在輸出中生成google :: protobuf生成的類。Spirit :: Qi:被綁定到google-protobuf

我試圖按照page作爲背景。不幸的是,我不能使用屬性語法,因爲google :: protobuf生成的類只提供set/get方法。於是,我試着用DEFERED的boost ::鳳凰約束力,但我不知道如何從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 <>生成的屬性綁定到像A :: add_param()這樣的方法?

-

感謝

回答

相關問題