Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:19.3.3 ExamplesUp:19. Python compiler packageNext:19.5 Bytecode Generation

19.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 elminiate 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])

classASTVisitor()

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)


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:19.3.3 ExamplesUp:19. Python compiler packageNext:19.5 Bytecode Generation
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp