Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="node797.html" /> | |
13 | <link rel="prev" href="node792.html" /> | |
14 | <link rel="parent" href="compiler.html" /> | |
15 | <link rel="next" href="node797.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>19.4 Using Visitors to Walk ASTs</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="19.3.3 Examples" | |
25 | href="node795.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="19. Python compiler package" | |
28 | href="compiler.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="19.5 Bytecode Generation" | |
31 | href="node797.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="node795.html">19.3.3 Examples</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="compiler.html">19. Python compiler package</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="node797.html">19.5 Bytecode Generation</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION0021400000000000000000"> | |
56 | 19.4 Using Visitors to Walk ASTs</A> | |
57 | </H1> | |
58 | ||
59 | <P> | |
60 | <A NAME="module-compiler.visitor"></A> | |
61 | <P> | |
62 | The visitor pattern is ... The <tt class="module"><a href="module-compiler.html">compiler</a></tt> package uses a | |
63 | variant on the visitor pattern that takes advantage of Python's | |
64 | introspection features to eliminate the need for much of the visitor's | |
65 | infrastructure. | |
66 | ||
67 | <P> | |
68 | The classes being visited do not need to be programmed to accept | |
69 | visitors. The visitor need only define visit methods for classes it | |
70 | is specifically interested in; a default visit method can handle the | |
71 | rest. | |
72 | ||
73 | <P> | |
74 | XXX The magic <tt class="method">visit()</tt> method for visitors. | |
75 | ||
76 | <P> | |
77 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
78 | <td><nobr><b><tt id='l2h-5032' xml:id='l2h-5032' class="function">walk</tt></b>(</nobr></td> | |
79 | <td><var>tree, visitor</var><big>[</big><var>, verbose</var><big>]</big><var></var>)</td></tr></table></dt> | |
80 | <dd> | |
81 | </dl> | |
82 | ||
83 | <P> | |
84 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
85 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-5033' xml:id='l2h-5033' class="class">ASTVisitor</tt></b>(</nobr></td> | |
86 | <td><var></var>)</td></tr></table></dt> | |
87 | <dd> | |
88 | ||
89 | <P> | |
90 | The <tt class="class">ASTVisitor</tt> is responsible for walking over the tree in the | |
91 | correct order. A walk begins with a call to <tt class="method">preorder()</tt>. For | |
92 | each node, it checks the <var>visitor</var> argument to <tt class="method">preorder()</tt> | |
93 | for a method named `visitNodeType,' where NodeType is the name of the | |
94 | node's class, e.g. for a <tt class="class">While</tt> node a <tt class="method">visitWhile()</tt> | |
95 | would be called. If the method exists, it is called with the node as | |
96 | its first argument. | |
97 | ||
98 | <P> | |
99 | The visitor method for a particular node type can control how child | |
100 | nodes are visited during the walk. The <tt class="class">ASTVisitor</tt> modifies | |
101 | the visitor argument by adding a visit method to the visitor; this | |
102 | method can be used to visit a particular child node. If no visitor is | |
103 | found for a particular node type, the <tt class="method">default()</tt> method is | |
104 | called. | |
105 | </dl> | |
106 | ||
107 | <P> | |
108 | <tt class="class">ASTVisitor</tt> objects have the following methods: | |
109 | ||
110 | <P> | |
111 | XXX describe extra arguments | |
112 | ||
113 | <P> | |
114 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
115 | <td><nobr><b><tt id='l2h-5034' xml:id='l2h-5034' class="method">default</tt></b>(</nobr></td> | |
116 | <td><var>node</var><big>[</big><var>, ...</var><big>]</big><var></var>)</td></tr></table></dt> | |
117 | <dd> | |
118 | </dl> | |
119 | ||
120 | <P> | |
121 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
122 | <td><nobr><b><tt id='l2h-5035' xml:id='l2h-5035' class="method">dispatch</tt></b>(</nobr></td> | |
123 | <td><var>node</var><big>[</big><var>, ...</var><big>]</big><var></var>)</td></tr></table></dt> | |
124 | <dd> | |
125 | </dl> | |
126 | ||
127 | <P> | |
128 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
129 | <td><nobr><b><tt id='l2h-5036' xml:id='l2h-5036' class="method">preorder</tt></b>(</nobr></td> | |
130 | <td><var>tree, visitor</var>)</td></tr></table></dt> | |
131 | <dd> | |
132 | </dl> | |
133 | ||
134 | <P> | |
135 | ||
136 | <DIV CLASS="navigation"> | |
137 | <div class='online-navigation'> | |
138 | <p></p><hr /> | |
139 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
140 | <tr> | |
141 | <td class='online-navigation'><a rel="prev" title="19.3.3 Examples" | |
142 | href="node795.html"><img src='../icons/previous.png' | |
143 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
144 | <td class='online-navigation'><a rel="parent" title="19. Python compiler package" | |
145 | href="compiler.html"><img src='../icons/up.png' | |
146 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
147 | <td class='online-navigation'><a rel="next" title="19.5 Bytecode Generation" | |
148 | href="node797.html"><img src='../icons/next.png' | |
149 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
150 | <td align="center" width="100%">Python Library Reference</td> | |
151 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
152 | href="contents.html"><img src='../icons/contents.png' | |
153 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
154 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
155 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
156 | <td class='online-navigation'><a rel="index" title="Index" | |
157 | href="genindex.html"><img src='../icons/index.png' | |
158 | border='0' height='32' alt='Index' width='32' /></A></td> | |
159 | </tr></table> | |
160 | <div class='online-navigation'> | |
161 | <b class="navlabel">Previous:</b> | |
162 | <a class="sectref" rel="prev" href="node795.html">19.3.3 Examples</A> | |
163 | <b class="navlabel">Up:</b> | |
164 | <a class="sectref" rel="parent" href="compiler.html">19. Python compiler package</A> | |
165 | <b class="navlabel">Next:</b> | |
166 | <a class="sectref" rel="next" href="node797.html">19.5 Bytecode Generation</A> | |
167 | </div> | |
168 | </div> | |
169 | <hr /> | |
170 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
171 | </DIV> | |
172 | <!--End of Navigation Panel--> | |
173 | <ADDRESS> | |
174 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
175 | </ADDRESS> | |
176 | </BODY> | |
177 | </HTML> |