[squeak-dev] [ANN] CoroutineReadStream (again)

Stephen Pair stephen at pairhome.net
Mon Dec 14 22:36:34 UTC 2009


On Mon, Dec 14, 2009 at 4:28 PM, Igor Stasenko <siguctua at gmail.com> wrote:
>
> As to me, that CoroutineReadStream is nice wrapping , providing a
> transformation from block iterators
> to stream. It switching from the 'push' logic (where provider pushing
> values to consumer) to 'pull' (where consumer asks provider to give
> next value(s), when ready to process next value).
>
> An iteration blocks usually don't decide if they willing to get next
> value or when (hence 'push'), and as a consequence its hard to switch
> the iterator's logic in the middle of iteration.


Yes, precisely.  I find it particularly handy when dealing with parsers.
 Parsers (i.e. combinatorial parsers) usually use the activation stack to
keep track of their progress and do their work.  This means they need to
drive execution.  For those that are familiar with PEGs and grammars that
don't make use of a lexer or tokenizer, you may know that dealing with
whitespace adds a lot of clutter to a grammar.  Since we can have PEGs that
are designed to work with arbitrary objects and not just characters, you
could solve this problem by first creating a token parser that transforms
characters into tokens, omitting whitespace.  Next, you have the parser for
your language that transforms the tokens into whatever you like.  Due to the
usage of the activation stack by the token parser I mentioned, it's not easy
to chain the parsers together.  CoroutineReadStream makes it simpler to do.

Here's an overly simplified and contrived illustration:

streamOfTokens :=
CoroutineReadStream onBlock:
[ :outputStream |
tokenParser
parse: someCharacterStream
into:
[ :token |
outputStream acceptNextObject: token]].

someLanguageParser
parse: streamOfTokens
into:
[ :parseNodeOrSomething |
self doSomethingWith: parseNodeOrSomething].


- Stephen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20091214/68deba57/attachment.htm


More information about the Squeak-dev mailing list