Parse a size prefixed protocol

Stephan Menzel <[email protected]> Mon, 30 Jul 2018 12:10:34 +0200
Newsgroups gmane.comp.parsers.spirit.general
Message-ID <CAEQ568t8bh2s38QiSFnV8r37d+H92UKaGe+Zfbp2sqihMXLNtw@mail.gmail.com>
Hello spirit folks,

I have a parser here that parses a variant of different redis messages that
can occur in lines read from a streambuf via multipass iterators. I mention
that in case it is relevant for the semantic action. Basically, I like to
extend the protocol by a size prefixed string.

So rather than:

template <typename InputIterator>
struct simple_string_parser : qi::grammar<InputIterator, std::string()> {

  simple_string_parser() : simple_string_parser::base_type(m_start,
"simple_string") {

    m_start %= '+' >> +(ascii::char_ - qi::eol) >> qi::eol;
  }

  qi::rule<InputIterator, std::string()>  m_start;
};

I would like to parse an int followed by a non-null-terminated sequence of
characters sized of that int. Which I want to assign to the string
attribute. Sooo..

template <typename InputIterator>
struct bulk_string_parser : qi::grammar<InputIterator, std::string()> {

  bulk_string_parser() : bulk_string_parser::base_type(m_start,
"bulk_string") {

    m_start %= '$' >> qi::ulong_long >> "\r\n" >> raw[*(ascii::char_ -
qi::eol)] [ ?? ]  >> qi::eol;
  }

  qi::rule<InputIterator, std::string()>  m_start;
};

I am assuming I somehow need to tell the char_ clause a fixed number of
characters that result from the first rule. I can use a local variable for
that, which is OK. But how can I parse some raw[] with a fixed length that
is only known at runtime and no regards as to what is in there, except
perhaps for the iterator going eoi?

Can anyone give me a hint here please?

Best regards...

Stephan

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

_______________________________________________
Spirit-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spirit-general