|
Boost : |
From: Bjorn Reese (breese_at_[hidden])
Date: 2019-09-29 10:56:03
On 9/23/19 6:11 PM, Vinnie Falco wrote:
> On Mon, Sep 23, 2019 at 8:58 AM Bjorn Reese via Boost
> <boost_at_[hidden]> wrote:
>> ...online parser...
>> A push parser (SAX)...
>> A tree parser (DOM)
>
> I have no experience with these terms other than occasionally coming
> across them in my Google searching adventures. The parsers that I have
> written take as input one or more buffers of contiguous characters,
> and produce as "output" a series of calls to abstract member functions
> which are implemented in the derived class. These calls represent
> tokens or events, such as "key string", "object begin", "array end".
> So what would we call this in the taxonomy above?
So if I understand your model correctly, it parses as many token as
possible, and each token results in a callback. When you reach the end
of the buffer, you manually suspend parsing and resume when more data
is fed into it.
That sounds like a variation of a push parser.
It may be easier to understand the parser models in C++ terms, although
these terms do not accurately cover the parser models.
A pull parser is equivalent to a forward iterator. The user tells it
when to advance to the next element.
A pull parser usually has a richer interface than a C++ iterator
because we are iterating over heterogenous elements. We therefore need
to query multiple attributes of the current element, such as
* The type of the current element (e.g. integer, string, start of
array)
* The converted value (e.g. text-to-integer conversion, or JSON string
to UTF-8 string conversion)
* The unconverted value (a string view of the input corresponding to
the current value)
A push parser is equivalent to a loop with a visitor. The loop traverses
the entire input, and for each recognized element it calls the visitor.
Assuming you already have a rich iterator, then it is straight-forward
to use this for the loop and call a visitor in each iteration. That is,
push parsers are easily build on top of pull parsers. You can find a
simple example of this here:
https://github.com/breese/trial.protocol/tree/develop/example/json/push_parser
A more elaborate example shows an alternative implementation of
boost::property_tree JSON parser using a pull parser:
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk