Re: parsing a stream

Seth <[email protected]> Thu, 5 Jul 2018 17:30:22 +0200
Newsgroups gmane.comp.parsers.spirit.general
Message-ID <[email protected]>
On 05-07-18 16:32, Patrick Welche wrote:
> Great - any clue on how?
Just use boost::spirit::istream_iterator

> but if I say, "iter, iter+3" then parsing will fail, and I can't carry
> on with iter (now pointing to iter+3), end, i.e., try to split the tiny
> string into 2 and do 2 batches.
Ah, so you want resumable functions. No Spirit parsers aren't
"resumable". You you can, of course, restart the parsing. This would be
a natural fit with "scatter gather" buffers (like Asio's).

A somewhat related use-case from another sample involves parsing from
multiple lines, read separately, I wrote it using Asio's buffer-sequence:

    std::vector<boost::asio::const_buffer> bufs;

    for (auto& line : m_input_lines) {
        bufs.push_back(boost::asio::buffer(line));
        bufs.push_back(boost::asio::buffer("\n", 1));
    }

    return parse_input(buffers_begin(bufs), buffers_end(bufs));

The `parse_input` function just takes two iterators and applies a Qi
grammar on it.

I think also the Asio HTTP server examples contain some samples of
incremental/repeated request parsing until a complete request has been
received. E.g.

  /// Parse some data. The tribool return value is true when a complete
request
  /// has been parsed, false if the data is invalid, indeterminate when more
  /// data is required. The InputIterator return value indicates how
much of the
  /// input has been consumed.

Sadly that example doesn't use Spirit, but you could imagine doing
similar things using Spirit.
You could look at older versions of Boost Beast (they dropped the Spirit
dependency somewhere along the road, maybe they had what I seemed to
remember).

Finally, you could probably use stackful coroutines and adapt the
iterator range to use the pull end.

------------------------------------------------------------------------------
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