[Python-ideas] Yield-from example: A parser

Antoine Pitrou solipsis at pitrou.net
Wed Feb 18 00:18:09 CET 2009


Greg Ewing <greg.ewing at ...> writes:
> If everything could always be done using pulls, there
> would be no need for send() in the first place.

Sorry if I'm missing something, but send() allows a bidirectional exchange of
values, it doesn't seem to matter whether you pull or you push.

> Not sure what you mean by that. There's no single place
> in the parser that loops until the input is exhausted.

How about:

   def scanner(text):
     for m in pat.finditer(text):
       token = m.group(0)
       print "Feeding:", repr(token)
       yield token
     yield None # to signal EOF

and:

   def parse_items(closing_tag = None):
     elems = []
     while 1:
       token = token_stream.next()
       if not token:
         break # EOF
       [etc.]

It looks like parse_items pulls from token_stream until exhaustion.






More information about the Python-ideas mailing list