Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex

31.4 Using Visitors to Walk ASTs

The visitor pattern is ... Thecompiler package uses avariant on the visitor pattern that takes advantage of Python'sintrospection features to eliminate the need for much of the visitor'sinfrastructure.

The classes being visited do not need to be programmed to acceptvisitors. The visitor need only define visit methods for classes itis specifically interested in; a default visit method can handle therest.

XXX The magicvisit() method for visitors.

walk(tree, visitor[, verbose])

class ASTVisitor()

TheASTVisitor is responsible for walking over the tree in thecorrect order. A walk begins with a call topreorder(). Foreach node, it checks thevisitor argument topreorder()for a method named `visitNodeType,' where NodeType is the name of thenode's class, e.g. for aWhile node avisitWhile()would be called. If the method exists, it is called with the node asits first argument.

The visitor method for a particular node type can control how childnodes are visited during the walk. TheASTVisitor modifiesthe visitor argument by adding a visit method to the visitor; thismethod can be used to visit a particular child node. If no visitor isfound for a particular node type, thedefault() method iscalled.

ASTVisitor objects have the following methods:

XXX describe extra arguments

default(node[, ...])

dispatch(node[, ...])

preorder(tree, visitor)


Up one LevelPython Library ReferenceContentsModule IndexIndex

Release 2.5.2, documentation updated on 21st February, 2008.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2025 Movatter.jp