/Doc/library/ast.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 261 lines · 174 code · 87 blank · 0 comment · 0 complexity · 48ea184b42efdc288ed7680293544bc8 MD5 · raw file

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