使用提升精神将字符串解析为struct

时间:2013-05-19 04:43:23

标签: boost boost-spirit string-parsing boost-spirit-qi boost-fusion

我需要以下代码来解析字符串并将其移动到如下定义的结构:

#include "boost\spirit\include\classic.hpp"
#include "boost\spirit\include\qi.hpp"
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/filesystem.hpp>
#include <boost/format.hpp>
#include <regex>
#include <string>
#include <boost\chrono.hpp>
#include <ctype.h>
#include <iostream>

struct my_struct
{
   std::string s1;
   std::string s2;
   std::string s3;
   std::string s4;
   std::string s5;
   std::string s6;
   std::string s7;
   std::string s8;
   std::string s9;
   std::string s10;
   std::string s11;
};

BOOST_FUSION_ADAPT_STRUCT(
    my_struct,
    (std::string, s1 )
    (std::string, s2 )
    (std::string, s3 )
    (std::string, s4 )
    (std::string, s5 )
    (std::string, s6 )
    (std::string, s7 )
    (std::string, s8 )
    (std::string, s9 )
    (std::string, s10)
    (std::string, s11) 
)

我的语法是这样的:

template <typename Iterator>
struct my_struct_parser : qi::grammar<Iterator, my_struct(), ascii::space_type>
{
    my_struct_parser() : my_struct_parser::base_type(start)
    {
        using ascii::char_;
        using qi::digit;
        using qi::alpha;

        start %=
            qi::alpha>>"-"
            >>qi::repeat(9)[digit]>>"-"
            >>+(digit)>>"-"
            >>+(digit)>>"-"
            >>+(digit)>>"-"
            >>qi::repeat(5)[digit]>>"-"
            >>+char_("a-zA-Z")>>"-"
            >>qi::repeat(2)[digit]>>"-"
            >>+(digit)>>"-"
            >>+(digit)>>"-"
           >>+(qi::alpha)

           >>-('-'>>+(char_("a-zA-Z0-9@$")));
    }
    qi::rule<Iterator, my_struct(), ascii::space_type> start;
};

然后我使用以下代码行解析我的字符串:

       my_struct & emp;//this is passed as an argument to a function
       typedef my_struct_parser<iterator_type> my_struct_parser_type;
       my_struct_parser_type parser;
       std::string::const_iterator iter = filename.begin();
       std::string::const_iterator end = filename.end();
       bool r =
       boost::spirit::qi::phrase_parse(iter, end,parser,boost::spirit::ascii::space ,emp);

好的,问题是当我的struct有10个或更少的字段时我的代码编译得很好但是当我在struct中有更多字段时它会出错 ,所以我猜测这是因为参数SPIRIT_ARGUMENTS_LIMIT,因为它的默认值是10。

我尝试在包含精灵头文件之前将此参数定义为我想要的大小,但我仍然遇到编译错误

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:7)

如果你检查编译器抱怨的内容,你会看到如下内容:

....
/usr/include/boost/fusion/container/vector/convert.hpp:26:13: error: invalid use of incomplete type ‘struct boost::fusion::detail::as_vector<12>’
....
/usr/include/boost/fusion/container/vector/detail/as_vector.hpp:26:12: error: declaration of ‘struct boost::fusion::detail::as_vector<12>’

我前段时间遇到过这个问题。如果您的结构包含超过10个字段 您需要重新定义 FUSION_MAX_VECTOR_SIZE ,因为融合容器会聚合匹配的属性。

http://www.boost.org/doc/libs/1_52_0/libs/fusion/doc/html/fusion/container/vector.html http://www.boost.org/doc/libs/1_52_0/libs/spirit/doc/html/spirit/qi/quick_reference/compound_attribute_rules.html

我会在所有其他包含文件之前使用这两个定义来覆盖它们的默认值:

#define FUSION_MAX_VECTOR_SIZE      20
#define SPIRIT_ARGUMENTS_LIMIT      20