Finalizing D2
Daniel Keep
daniel.keep.lists at gmail.com
Sun May 24 09:51:43 PDT 2009
Michel Fortin wrote:
> ...
>
> A callback API isn't necessarily SAX. A callback API doesn't necessarily
> have to parse everything until completion, it could parse only the next
> token and call the appropriate callback.
When I talk "callback api," I mean something fundamentally like SAX.
The reason is that if your callback api only does a single callback, all
you've really done is move the switch statement inside the function call
at the cost of having to define a crapload of functions outside of it.
> If I can construct a range class/struct over my callback API I'll be
> happy. And if I can recursively call the parser API inside a callback
> handler so I can reuse the call stack while parsing then I'll be very
> happy.
I don't see how constructing a range over a callback api will work.
Callback apis are inversion of control, ranges aren't.
As for using a callback api recursively, that just seems like a lot of
work to replicate the way a pull api works in the first place.
>> Something like Tango's PullParser is the superior API because although
>> it's more verbose up-front, that's as bad as it gets. Plus, you can
>> actually do stuff like call subroutines.
>
> All that is needed really is a callback system that parses only one
> token. Then the callback can update the PullParser state, or the
> token-range state, run in a loop to produce a SAX-like API, or directly
> do what you want to do, which may include parsing more tokens using
> different callbacks until you reach a closing tag.
Like I said, this seems like a lot of work to bolt a callback interface
onto something a pull api is designed for.
At best, you'll end up rewriting this:
> foreach( tt ; pp )
> {
> switch( tt )
> {
> case XmlTokenType.StartElement: blah(pp.name); break;
> ...
> }
> }
to this:
> pp.parse
> (
> XmlToken(Type.StartElement, {blah(pp.name);}),
> ...
> );
Except of course that you now can't easily control the loop, nor can do
you do fall-through on the cases.
More information about the Digitalmars-d
mailing list