简体   繁体   English

提升精神还原解析

[英]Boost spirit revert parsing

I want to parse a file containing the following structure: 我想解析包含以下结构的文件:

some
garbage *&%
section1 {
    section_content
}
section2 {
    section_content
}

The rule parsing section_name1 { ... } section_name2 { ... } is already defined: 规则解析section_name1 { ... } section_name2 { ... }已经定义:

section_name_rule = lexeme[+char_("A-Za-z0-9_")];
section = section_name_rule > lit("{") > /*some complicated things*/... > lit("}");
sections %= +section;

So I need to skip any garbage until the sections rule is met. 所以我需要跳过任何垃圾,直到满足sections规则。 Is there any way to accomplish this? 有没有办法实现这个目标? I have tried seek[sections] , but it seems not to work. 我试过seek[sections] ,但似乎没有用。

EDIT : I localized the reason why seek is not working: if I use follows operator( >> ), then it works. 编辑 :我本地化了搜索不工作的原因:如果我使用跟随运算符( >> ),那么它的工作原理。 If expectation parser is used ( > ), then it throws an exception. 如果使用期望解析器( > ),则会抛出异常。 Here is a sample code: 这是一个示例代码:

#define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/repository/include/qi_seek.hpp>
#include <boost/spirit/include/phoenix.hpp>

namespace qi = boost::spirit::qi;
using boost::phoenix::push_back;

struct section_t {
    std::string name, contents;
    friend std::ostream& operator<<(std::ostream& os, section_t const& s) { return os << "section_t[" << s.name << "] {" << s.contents << "}"; }
};

BOOST_FUSION_ADAPT_STRUCT(section_t, (std::string, name)(std::string, contents))

    typedef std::vector<section_t> sections_t;

    template <typename It, typename Skipper = qi::space_type>
    struct grammar : qi::grammar<It, sections_t(), Skipper>
{
    grammar() : grammar::base_type(start) {
        using namespace qi;
        using boost::spirit::repository::qi::seek;
        section_name_rule = lexeme[+char_("A-Za-z0-9_")];
        //Replacing '>>'s with '>'s throws an exception, while this works as expected!!
        section = section_name_rule
            >>
            lit("{") >> lexeme[*~char_('}')] >> lit("}");
        start = seek [ hold[section[push_back(qi::_val, qi::_1)]] ]
            >> *(section[push_back(qi::_val, qi::_1)]);
    }
    private:
    qi::rule<It, sections_t(),  Skipper> start;
    qi::rule<It, section_t(),   Skipper> section;
    qi::rule<It, std::string(), Skipper> section_name_rule;
};

int main() {
    typedef std::string::const_iterator iter;
    std::string storage("sdfsdf\n sd:fgdfg section1 {dummy } section2 {dummy  } section3 {dummy  }");
    iter f(storage.begin()), l(storage.end());
    sections_t sections;
    if (qi::phrase_parse(f, l, grammar<iter>(), qi::space, sections))
    {
        for(auto& s : sections)
            std::cout << "Parsed: " << s << "\n";
    }
    if (f != l)
        std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
}

So in the real example my entire grammar is constructed with expectation operators. 所以在真实的例子中,我的整个语法是用期望运算符构造的。 Do I have to change everything to make the "seek" work, or is there any other way (let's say, seek a simple "{", and revert one section_name_rule back)?? 我是否必须更改所有内容以使“搜索”工作,或者是否有任何其他方式(让我们说,寻找一个简单的“{”,然后还原一个section_name_rule)?

Here's a demonstration, using Hamlet for inspiration: Live On Coliru 这是一个演示,使用哈姆雷特的灵感: Live On Coliru

start = *seek [ no_skip[eol] >> hold [section] ];

Notes: 笔记:

  • drop the expectation points 降低期望点
  • optimize by requiring start of line before section name 通过要求在节名称之前开始行来优化

Example input: 输入示例:

some
garbage *&%
section1 {
   Claudius: ...But now, my cousin Hamlet, and my son —
   Hamlet: A little more than kin, and less than kind.
}
WE CAN DO MOAR GARBAGE
section2 {
   Claudius: How is it that the clouds still hang on you?
   Hamlet: Not so my lord; I am too much i' the sun 
}

Output: 输出:

Parsed: section_t[section1] {Claudius: ...But now, my cousin Hamlet, and my son —
   Hamlet: A little more than kin, and less than kind.
}
Parsed: section_t[section2] {Claudius: How is it that the clouds still hang on you?
   Hamlet: Not so my lord; I am too much i' the sun 
}

Reference Listing 参考清单

// #define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/repository/include/qi_seek.hpp>

namespace qi = boost::spirit::qi;

struct section_t { 
    std::string name, contents;
    friend std::ostream& operator<<(std::ostream& os, section_t const& s) { return os << "section_t[" << s.name << "] {" << s.contents << "}"; }
};

BOOST_FUSION_ADAPT_STRUCT(section_t, (std::string, name)(std::string, contents))

typedef std::vector<section_t> sections_t;

template <typename It, typename Skipper = qi::space_type>
struct grammar : qi::grammar<It, sections_t(), Skipper>
{
    grammar() : grammar::base_type(start) {
        using namespace qi;
        using boost::spirit::repository::qi::seek;

        section_name_rule = lexeme[+char_("A-Za-z0-9_")];
        section           = section_name_rule >> '{' >> lexeme[*~char_('}')] >> '}';
        start             = *seek [ no_skip[eol] >> hold [section] ];

        BOOST_SPIRIT_DEBUG_NODES((start)(section)(section_name_rule))
    }
  private:
    qi::rule<It, sections_t(),  Skipper> start;
    qi::rule<It, section_t(),   Skipper> section;
    qi::rule<It, std::string(), Skipper> section_name_rule;
};

int main() {
    using It = boost::spirit::istream_iterator;
    It f(std::cin >> std::noskipws), l;

    sections_t sections;
    if (qi::phrase_parse(f, l, grammar<It>(), qi::space, sections))
    {
        for(auto& s : sections)
            std::cout << "Parsed: " << s << "\n";
    }
    if (f != l)
        std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM