kursjan / petitparser2

A high-performance top-down parser
MIT License
41 stars 19 forks source link

Cannot run on Zinc streams #55

Open jecisc opened 4 years ago

jecisc commented 4 years ago

In the doc it is written that PP now works on stream. I tried to parse a file but it failed. I suppose it is because file stream implementation of Pharo changed to use Zinc now.

ZnCharacterReadStream(Object)>>doesNotUnderstand: #asPetit2Stream
Message>>sentTo:
ZnCharacterReadStream(Object)>>doesNotUnderstand: #asPetit2Stream
PP2ActionNode(PP2Node)>>parseAdaptable:
PP2ActionNode(PP2Node)>>parseContext:
PP2ActionNode(PP2Node)>>parse:
kursjan commented 4 years ago

This should definitely work, I will look into this asap.

kursjan commented 4 years ago

Indeed, there is no extension for ZnCharacaterStream. But you can build your own stream on top of ZincStream:

| byteStream stream |
byteStream := ZnClient new
    url: 'http://pharo.org';
    streaming: true;
    get.
stream := PP2CharacterStream on: byteStream encoder: ZnUTF8Encoder new.

^ PP2HtmlHeaderGrammar new optimize parse: stream

See PP2HtmlHeader class>>example

kursjan commented 4 years ago

Which version of Pharo are you using? It seems there is an implementation of asPetit2Stream in Pharo 6.1:

ZnCharacterReadStream>>asPetit2Stream
    ^ PP2BufferStream on: self

As a workaround, consider creating your own PP2BufferStream on ZnCharacterReadStream by simply wrapping to PP2BufferStream.

kursjan commented 4 years ago

I also improved the documentation of PP2 related streams in https://github.com/kursjan/petitparser2/commit/1e20acad0febb34a798e9ac44404a83d300b17c9

jecisc commented 4 years ago

I am on Pharo 8.

In the end I cannot use this feature but I was repporting it in case you want to add syntactic suggar.