Boost spirit revers parsing

I want to parse a file containing the following structure:

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

      

Rule parsing is section_name1 { ... } section_name2 { ... }

already defined:

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 rule is met sections

. Is there a way to do this? I tried seek[sections]

it but doesn't seem to work.

EDIT : I have localized the reason why the search is not working: if I use the next ( >>

) operator then it works. If the wait parser ( >

) is used, it throws an exception. Here's some 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 '>> with '> 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 a real-world example, my entire grammar is built with wait statements. Do I have to change everything to make the "search" work, or is there some other way (say, search for a simple "{" and return one name rule back) ??

+1


source to share


1 answer


Here's a demonstration of using Hamlet for inspiration: Live On Coliru

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

      

Notes:

  • remove waiting points
  • optimize by requiring start of line before section name

Input example:



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 
}

      

List of links

// #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";
}

      

+3


source







All Articles