77
88import types
99from collections .abc import Callable
10- from typing import Any
10+ from typing import Any , Final
1111
1212from .tree import Tree
1313
14+ __all__ : Final [tuple [str , ...]] = "Ast" , "NodeVisitor"
15+
1416
1517# ----------------------------------------------------------------------
1618# Abstract Syntax Tree class
@@ -34,12 +36,13 @@ def visit(self, node):
3436
3537 while stack :
3638 try :
37- last = stack [- 1 ]
38- if isinstance (last , types .GeneratorType ):
39- stack .append (last .send (last_result ))
39+ stack_top = stack [- 1 ]
40+ if isinstance (stack_top , types .GeneratorType ):
41+ stack .append (stack_top .send (last_result ))
4042 last_result = None
41- elif isinstance (last , self .node_type ):
42- stack .append (self ._visit (stack .pop ()))
43+ elif isinstance (stack_top , self .node_type ):
44+ stack .pop ()
45+ stack .append (self ._visit (stack_top ))
4346 else :
4447 last_result = stack .pop ()
4548 except StopIteration :
@@ -55,7 +58,10 @@ def generic_visit(self, node: Ast):
5558 raise RuntimeError (f"No visit_{ node .token } () method defined" )
5659
5760 def filter_inorder (
58- self , node , filter_func : Callable [[Any ], bool ], child_selector : Callable [[Ast ], bool ] = lambda x : True
61+ self ,
62+ node ,
63+ filter_func : Callable [[Any ], bool ],
64+ child_selector : Callable [[Ast ], bool ] = lambda x : True ,
5965 ):
6066 """Visit the tree inorder, but only those that return true for filter_func and visiting children which
6167 return true for child_selector.
0 commit comments