Search code examples
c++boostc++17boost-spiritboost-spirit-qi

Capturing string until end of line with Boost.Spirit.Qi


I'm trying to write a custom struct to capture string until EOL or EOS as part of a larger grammar but I couldn't compile it. I followed tutorial code, yet I couldn't figure out errors in the below snippet.

#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>

#include <string>
#include <cassert>

namespace qi = boost::spirit::qi;

namespace project {

struct RawValue {
    std::string str;
};

}

BOOST_FUSION_ADAPT_STRUCT(project::RawValue, str)

namespace project::parser {

struct RawValueParser : qi::grammar<std::string::const_iterator, RawValue(), qi::space_type> {
  RawValueParser() : RawValueParser::base_type(expr) {
    using qi::blank;
    using qi::char_;
    using qi::eol;
    using qi::lexeme;

    expr %= lexeme[*(char_ - eol) >> (char_ - blank)];
  }

  qi::rule<iterator_type, RawValue(), skipper_type> expr;
};

}

int main() {
  project::parser::RawValueParser expr;
  project::RawValue result;
  std::string s1 = "test1";
  auto iter = s1.cbegin();
  auto end = s1.cend();
  assert(qi::phrase_parse(iter, end, expr, qi::space, result));
  return 0;
}

https://wandbox.org/permlink/PjaaYwO0Qz18998y

This should be a fairly simple example. What am I doing wrong?


Solution

  • The compile error originates here:

    test.cpp|21 col 58| required from here
    /home/sehe/custom/spirit/include/boost/spirit/home/qi/nonterminal/grammar.hpp|77 col 13| error: static assertion failed: incompatible_start_rule
    ||    77 |             BOOST_SPIRIT_ASSERT_MSG(
    ||       |             ^~~~~~~~~~~~~~~~~~~~~~~
    

    If you go to the indicated location (see also How do I grok boost spirit compiler errors):

    // If you see the assertion below failing then the start rule
    // passed to the constructor of the grammar is not compatible with
    // the grammar (i.e. it uses different template parameters).
    BOOST_SPIRIT_ASSERT_MSG(
        (is_same<start_type, rule<Iterator_, T1_, T2_, T3_, T4_> >::value)
      , incompatible_start_rule, (rule<Iterator_, T1_, T2_, T3_, T4_>));
    

    That's your problem. The grammar is <It, RawValue(), qi::space_type> but the start rule is <iterator_type, RawValue(), skipper_type>, where

        static_assert(std::is_same_v<It, iterator_type>);
        static_assert(std::is_same_v<qi::space_type, skipper_type>);
        qi::space_type _y = skipper_type{};
    

    reveals that the second assert fails. The third line leads to more detail diagnostic: Live

    test.cpp|33 col 29| error: could not convert ‘boost::spirit::qi::grammar<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >, project::RawValue(), boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >, 0> >::skipper_type{}’ from ‘boost::spirit::qi::grammar<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >, project::RawValue(), boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >, 0> >::skipper_type’ {aka ‘boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >’} to ‘boost::spirit::standard::space_type’ {aka ‘boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::te
    ||    33 |         qi::space_type _y = skipper_type{};
    ||       |                             ^~~~~~~~~~~~~~
    ||       |                             |
    ||       |                             boost::spirit::qi::grammar<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >, project::RawValue(), boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >, 0> >::skipper_type {aka boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >}
    

    You can tease the whole thing apart, but suffice it to say that your assumption that skipper_type would equal the third template argument was wrong.

    Simply spell the types you expect out: Live

    qi::rule<It, RawValue(), qi::space_type> expr;
    

    Side Notes

    1. lexeme disables the skipper. Consider simplifying the whole thing with that in mind: Live (see also Boost spirit skipper issues)

    2. operator %= has no meaning wihtout semantic actions

    3. expr seems a misnomer

    4. negated character classes are more efficient than subtracted parsers; did you mean (char_ - blank) to mean more than eol?

    5. End of input is now not accepted instead of eol. Perhaps you should allow it. I'd write it like

      start = *~qi::char_("\n\r") >> (eol | eoi);
      

    Simplified Listing

    Live On Coliru

    #include <boost/fusion/adapted/struct.hpp>
    #include <boost/spirit/include/qi.hpp>
    
    #include <iomanip>
    
    namespace qi = boost::spirit::qi;
    using It = std::string::const_iterator;
    
    struct RawValue { std::string str; };
    
    BOOST_FUSION_ADAPT_STRUCT(RawValue, str)
    
    struct RawValueParser : qi::grammar<It, RawValue()> {
        RawValueParser() : RawValueParser::base_type(start)
        {
            using namespace qi;
            start = *~qi::char_("\n\r") >> (eol | eoi);
        }
    
      private:
        qi::rule<It, RawValue()> start;
    };
    
    int main() {
        RawValueParser expr;
    
        for (std::string const s : {
                 "test1",
                 " test2\tbla   \n",
             }) {
    
            RawValue result;
            std::cout
                << std::boolalpha
                //<< qi::phrase_parse(begin(s), end(s), expr, qi::space, result)
                << qi::parse(begin(s), end(s), expr, result) << " -> "
                << std::quoted(result.str) << "\n";
        }
    }
    

    Prints

    true -> "test1"
    true -> " test2 bla   "