提振精神期望失败

时间:2011-02-17 12:31:09

标签: c++ parsing boost-spirit boost-spirit-qi template-meta-programming

我想解析以下结构的向量:

BOOST_FUSION_ADAPT_STRUCT(
    event_model::OTNDescriptor,
    (int32_t, qualifier)
    (int32_t, ordinal)
    (std::string, name)
    (int32_t, type)
)

我的语法如下:

struct swat_types_ : qi::symbols<char, unsigned>
{
    swat_types_()
    {
        using namespace event_model;

        add
            ("int", SWAT_INT4)
            ("int4", SWAT_INT4)
            ("int8", SWAT_INT8)
            ("bigint", SWAT_INT8)
            ("string", SWAT_STRING)
        ;
    } 
} swat_types;

template<typename Iterator>
struct Rules
{
    qi::rule<Iterator, event_model::OTNDescriptor(), ascii::space_type>
        data_member_line;
    qi::rule<Iterator, std::string()> data_name;
    qi::rule<Iterator, void(int&, std::string&)> data_identifier_pair;
    qi::rule<   Iterator,
                std::vector< event_model::OTNDescriptor>(),
                ascii::space_type> dm_lines;

    Rules() {
        data_name =
            + (char_("a","z") | char_("A","Z") | char_('_'));

        data_identifier_pair =
            lexeme[int_ [ _r1 = _1] > ':' > data_name [ _r2 = _1]];

        data_member_line =
            eps [ at_c<0>(_val) = event_model::OTN_REQUIRED ]
            >>  -( no_case[lit("optional")]
                        [at_c<0>(_val) = event_model::OTN_OPTIONAL]
                |   no_case[lit("required")]
                        [at_c<0>(_val) = event_model::OTN_REQUIRED])
            > data_identifier_pair( at_c<1>(_val), at_c<2>(_val) )
            > no_case[swat_types [at_c<3>(_val) = _1]]

            > ';'
        ;
        //dm_lines = data_member_line >> data_member_line;
        dm_lines = data_member_line >> *(data_member_line);
    }
};

我的哈纳斯看起来像这样:

std::string str4("REquireD 0:lala int4; REquireD 1:googoo int4; ");

std::string::const_iterator iter4=str4.begin();
std::string::const_iterator end4=str4.end();

std::vector<event_model::OTNDescriptor> res4;

r = phrase_parse(iter4, end4, rules.dm_lines, boost::spirit::ascii::space, res4);

for(std::vector<event_model::OTNDescriptor>::iterator it = res4.begin(); it < ires4.end(); it++)
{
    std::cout << it->name << "\n";
}

如果我将规则规范从kleene星形规范切换到序列匹配没有错误。

//dm_lines = data_member_line >> data_member_line;
dm_lines = data_member_line >> *(data_member_line);

否则我在尝试解析我的例句时会出现期望错误(显示在harnass中)。

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::spirit::qi::expectation_failure<__gnu_cxx::__normal_iterator<char const*, std::string> > > >'
  what():  boost::spirit::qi::expectation_failure
Aborted

理想情况下,我想编写像dm_lines = +(data_member_line)这样的规则(这也不起作用)。当使用'*'和'+'运算符时,会发生什么导致期望失败?但是在匹配序列时不是吗?我该如何解决呢?

1 个答案:

答案 0 :(得分:3)

期望失败的原因是,只要您开始使用plus或Kleene,嵌入式解析器(data_member_line)将被多次调用。它的最后一次调用自然会失败,因为没有更多的输入可用。在你的情况下,这个事件将被“识别”到第一个期望点,因为之前的所有组件都是可选的(它们永远不会失败)。