It is not valid to use recursive tree traversals in production code because
ast (parse) trees can be arbitrarily deep. Consider the expression: a = 1
+ (2 + (... 500)...). Recursive traversal of this expression would require
more than 1500 entries in python's parse stack!
The tot.visit method is the culprit. It contains the recursion. The visit
method must go away.
The TokenOrderTraverser class must become a generator. Here's how:
1. The visit method will be "distributed" into all visitors as follows:
- Each visitor will start by calling *tot.start_node*. This method will
contain everything *before *the recursive call in visit.
- Visitors will *yield from*(z) instead of calling self.visit(z).
- Each visitor will end by calling *tot. end_node*. This method will
contain everything *after *the recursive call in visit.
2. The put/eat methods will contain the only yield statements.
This works because the "yield from" statements produce the desired results
without recursion.
And that should be that.
Edward
P.S. A note about debugging generators. Many years ago I asked the python
community how to "trace" a complex generator. Nobody had any ideas.
Yesterday I saw how:
def seq():
"""A test generator."""
for i in range(5):
yield i
def dump(it, tag):
"""A generator that dumps another generator."""
aList = list(it)
g.printObj(aList, tag=tag)
for z in aList:
yield z
# For debugging.
for i in dump(seq(), tag='seq'):
print(i)
print('done')
This should work for any generator, no matter how complex.
EKR
--
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/leo-editor/06064f2c-4ce5-48c5-943c-08c7971ddb76%40googlegroups.com.