blob: 888ddaeb28dc8a19712f88babe4a901f70a82c7d [file] [log] [blame]
Alexander Belopolsky96d1eb12010-11-05 01:07:35 +00001:mod:`ast` --- Abstract Syntax Trees
2====================================
Georg Brandl0c77a822008-06-10 16:37:50 +00003
4.. module:: ast
5 :synopsis: Abstract Syntax Tree classes and manipulation.
6
7.. sectionauthor:: Martin v. Lรถwis <martin@v.loewis.de>
8.. sectionauthor:: Georg Brandl <georg@python.org>
9
Georg Brandl0c77a822008-06-10 16:37:50 +000010
11The :mod:`ast` module helps Python applications to process trees of the Python
12abstract syntax grammar. The abstract syntax itself might change with each
13Python release; this module helps to find out programmatically what the current
14grammar looks like.
15
Benjamin Petersonec9199b2008-11-08 17:05:00 +000016An abstract syntax tree can be generated by passing :data:`ast.PyCF_ONLY_AST` as
Georg Brandlc5605df2009-08-13 08:26:44 +000017a flag to the :func:`compile` built-in function, or using the :func:`parse`
Georg Brandl0c77a822008-06-10 16:37:50 +000018helper provided in this module. The result will be a tree of objects whose
Benjamin Petersonec9199b2008-11-08 17:05:00 +000019classes all inherit from :class:`ast.AST`. An abstract syntax tree can be
20compiled into a Python code object using the built-in :func:`compile` function.
Georg Brandl0c77a822008-06-10 16:37:50 +000021
Georg Brandl0c77a822008-06-10 16:37:50 +000022
23Node classes
24------------
25
26.. class:: AST
27
28 This is the base of all AST node classes. The actual node classes are
29 derived from the :file:`Parser/Python.asdl` file, which is reproduced
30 :ref:`below <abstract-grammar>`. They are defined in the :mod:`_ast` C
31 module and re-exported in :mod:`ast`.
32
33 There is one class defined for each left-hand side symbol in the abstract
34 grammar (for example, :class:`ast.stmt` or :class:`ast.expr`). In addition,
35 there is one class defined for each constructor on the right-hand side; these
36 classes inherit from the classes for the left-hand side trees. For example,
37 :class:`ast.BinOp` inherits from :class:`ast.expr`. For production rules
38 with alternatives (aka "sums"), the left-hand side class is abstract: only
39 instances of specific constructor nodes are ever created.
40
41 .. attribute:: _fields
42
43 Each concrete class has an attribute :attr:`_fields` which gives the names
44 of all child nodes.
45
46 Each instance of a concrete class has one attribute for each child node,
47 of the type as defined in the grammar. For example, :class:`ast.BinOp`
48 instances have an attribute :attr:`left` of type :class:`ast.expr`.
49
50 If these attributes are marked as optional in the grammar (using a
51 question mark), the value might be ``None``. If the attributes can have
52 zero-or-more values (marked with an asterisk), the values are represented
53 as Python lists. All possible attributes must be present and have valid
54 values when compiling an AST with :func:`compile`.
55
56 .. attribute:: lineno
57 col_offset
58
59 Instances of :class:`ast.expr` and :class:`ast.stmt` subclasses have
60 :attr:`lineno` and :attr:`col_offset` attributes. The :attr:`lineno` is
61 the line number of source text (1-indexed so the first line is line 1) and
62 the :attr:`col_offset` is the UTF-8 byte offset of the first token that
63 generated the node. The UTF-8 offset is recorded because the parser uses
64 UTF-8 internally.
65
66 The constructor of a class :class:`ast.T` parses its arguments as follows:
67
68 * If there are positional arguments, there must be as many as there are items
69 in :attr:`T._fields`; they will be assigned as attributes of these names.
70 * If there are keyword arguments, they will set the attributes of the same
71 names to the given values.
72
73 For example, to create and populate an :class:`ast.UnaryOp` node, you could
74 use ::
75
76 node = ast.UnaryOp()
77 node.op = ast.USub()
78 node.operand = ast.Num()
79 node.operand.n = 5
80 node.operand.lineno = 0
81 node.operand.col_offset = 0
82 node.lineno = 0
83 node.col_offset = 0
84
85 or the more compact ::
86
87 node = ast.UnaryOp(ast.USub(), ast.Num(5, lineno=0, col_offset=0),
88 lineno=0, col_offset=0)
89
90
91.. _abstract-grammar:
92
93Abstract Grammar
94----------------
95
96The module defines a string constant ``__version__`` which is the decimal
97Subversion revision number of the file shown below.
98
99The abstract grammar is currently defined as follows:
100
101.. literalinclude:: ../../Parser/Python.asdl
102
103
104:mod:`ast` Helpers
105------------------
106
Georg Brandl0c77a822008-06-10 16:37:50 +0000107Apart from the node classes, :mod:`ast` module defines these utility functions
108and classes for traversing abstract syntax trees:
109
110.. function:: parse(expr, filename='<unknown>', mode='exec')
111
112 Parse an expression into an AST node. Equivalent to ``compile(expr,
Benjamin Petersonec9199b2008-11-08 17:05:00 +0000113 filename, mode, ast.PyCF_ONLY_AST)``.
Georg Brandl0c77a822008-06-10 16:37:50 +0000114
Georg Brandl48310cd2009-01-03 21:18:54 +0000115
Georg Brandl0c77a822008-06-10 16:37:50 +0000116.. function:: literal_eval(node_or_string)
117
118 Safely evaluate an expression node or a string containing a Python
119 expression. The string or node provided may only consist of the following
120 Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
121 and ``None``.
122
123 This can be used for safely evaluating strings containing Python expressions
124 from untrusted sources without the need to parse the values oneself.
125
126
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +0000127.. function:: get_docstring(node, clean=True)
Georg Brandl0c77a822008-06-10 16:37:50 +0000128
129 Return the docstring of the given *node* (which must be a
130 :class:`FunctionDef`, :class:`ClassDef` or :class:`Module` node), or ``None``
131 if it has no docstring. If *clean* is true, clean up the docstring's
132 indentation with :func:`inspect.cleandoc`.
133
134
135.. function:: fix_missing_locations(node)
136
137 When you compile a node tree with :func:`compile`, the compiler expects
138 :attr:`lineno` and :attr:`col_offset` attributes for every node that supports
139 them. This is rather tedious to fill in for generated nodes, so this helper
140 adds these attributes recursively where not already set, by setting them to
141 the values of the parent node. It works recursively starting at *node*.
142
143
144.. function:: increment_lineno(node, n=1)
145
146 Increment the line number of each node in the tree starting at *node* by *n*.
147 This is useful to "move code" to a different location in a file.
148
149
150.. function:: copy_location(new_node, old_node)
151
152 Copy source location (:attr:`lineno` and :attr:`col_offset`) from *old_node*
153 to *new_node* if possible, and return *new_node*.
154
155
156.. function:: iter_fields(node)
157
158 Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
159 that is present on *node*.
160
161
162.. function:: iter_child_nodes(node)
163
164 Yield all direct child nodes of *node*, that is, all fields that are nodes
165 and all items of fields that are lists of nodes.
166
167
168.. function:: walk(node)
169
170 Recursively yield all child nodes of *node*, in no specified order. This is
171 useful if you only want to modify nodes in place and don't care about the
172 context.
173
174
175.. class:: NodeVisitor()
176
177 A node visitor base class that walks the abstract syntax tree and calls a
178 visitor function for every node found. This function may return a value
Georg Brandl36ab1ef2009-01-03 21:17:04 +0000179 which is forwarded by the :meth:`visit` method.
Georg Brandl0c77a822008-06-10 16:37:50 +0000180
181 This class is meant to be subclassed, with the subclass adding visitor
182 methods.
183
184 .. method:: visit(node)
185
186 Visit a node. The default implementation calls the method called
187 :samp:`self.visit_{classname}` where *classname* is the name of the node
188 class, or :meth:`generic_visit` if that method doesn't exist.
189
190 .. method:: generic_visit(node)
191
192 This visitor calls :meth:`visit` on all children of the node.
Georg Brandl48310cd2009-01-03 21:18:54 +0000193
Georg Brandl0c77a822008-06-10 16:37:50 +0000194 Note that child nodes of nodes that have a custom visitor method won't be
195 visited unless the visitor calls :meth:`generic_visit` or visits them
196 itself.
197
198 Don't use the :class:`NodeVisitor` if you want to apply changes to nodes
199 during traversal. For this a special visitor exists
200 (:class:`NodeTransformer`) that allows modifications.
201
202
203.. class:: NodeTransformer()
204
205 A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
206 allows modification of nodes.
207
Georg Brandl36ab1ef2009-01-03 21:17:04 +0000208 The :class:`NodeTransformer` will walk the AST and use the return value of
209 the visitor methods to replace or remove the old node. If the return value
210 of the visitor method is ``None``, the node will be removed from its
211 location, otherwise it is replaced with the return value. The return value
212 may be the original node in which case no replacement takes place.
Georg Brandl0c77a822008-06-10 16:37:50 +0000213
214 Here is an example transformer that rewrites all occurrences of name lookups
215 (``foo``) to ``data['foo']``::
216
217 class RewriteName(NodeTransformer):
218
219 def visit_Name(self, node):
220 return copy_location(Subscript(
221 value=Name(id='data', ctx=Load()),
222 slice=Index(value=Str(s=node.id)),
223 ctx=node.ctx
224 ), node)
225
226 Keep in mind that if the node you're operating on has child nodes you must
227 either transform the child nodes yourself or call the :meth:`generic_visit`
228 method for the node first.
229
230 For nodes that were part of a collection of statements (that applies to all
231 statement nodes), the visitor may also return a list of nodes rather than
232 just a single node.
233
234 Usually you use the transformer like this::
235
236 node = YourTransformer().visit(node)
237
238
239.. function:: dump(node, annotate_fields=True, include_attributes=False)
240
241 Return a formatted dump of the tree in *node*. This is mainly useful for
242 debugging purposes. The returned string will show the names and the values
243 for fields. This makes the code impossible to evaluate, so if evaluation is
244 wanted *annotate_fields* must be set to False. Attributes such as line
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000245 numbers and column offsets are not dumped by default. If this is wanted,
Georg Brandl0c77a822008-06-10 16:37:50 +0000246 *include_attributes* can be set to ``True``.