diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2008-03-30 20:03:44 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2008-03-30 20:03:44 (GMT) |
commit | 618dc5e06459dab5fae2dc0a8caee7d15afd6410 (patch) | |
tree | 2881b2c079821ef43895742f3c65171acf582cc1 /Doc/library/_ast.rst | |
parent | d3372793d6de665be2f866e9245a33bcefeaaa76 (diff) | |
download | cpython-618dc5e06459dab5fae2dc0a8caee7d15afd6410.zip cpython-618dc5e06459dab5fae2dc0a8caee7d15afd6410.tar.gz cpython-618dc5e06459dab5fae2dc0a8caee7d15afd6410.tar.bz2 |
Merged revisions 62004 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62004 | georg.brandl | 2008-03-28 13:11:56 +0100 (Fr, 28 Mär 2008) | 4 lines
Patch #1810 by Thomas Lee, reviewed by myself:
allow compiling Python AST objects into code objects
in compile().
........
Diffstat (limited to 'Doc/library/_ast.rst')
-rw-r--r-- | Doc/library/_ast.rst | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/Doc/library/_ast.rst b/Doc/library/_ast.rst index 9f56156..518798e 100644 --- a/Doc/library/_ast.rst +++ b/Doc/library/_ast.rst @@ -10,16 +10,16 @@ Abstract Syntax Trees The ``_ast`` module helps Python applications to process trees of the Python -abstract syntax grammar. The Python compiler currently provides read-only access -to such trees, meaning that applications can only create a tree for a given -piece of Python source code; generating :term:`bytecode` from a (potentially modified) -tree is not supported. The abstract syntax itself might change with each Python -release; this module helps to find out programmatically what the current grammar -looks like. +abstract syntax grammar. The abstract syntax itself might change with each +Python release; this module helps to find out programmatically what the current +grammar looks like. -An abstract syntax tree can be generated by passing ``_ast.PyCF_ONLY_AST`` as a -flag to the :func:`compile` builtin function. The result will be a tree of -objects whose classes all inherit from ``_ast.AST``. +An abstract syntax tree can be generated by passing :data:`_ast.PyCF_ONLY_AST` +as a flag to the :func:`compile` builtin function. The result will be a tree of +objects whose classes all inherit from :class:`_ast.AST`. + +A modified abstract syntax tree can be compiled into a Python code object using +the built-in :func:`compile` function. The actual classes are derived from the ``Parser/Python.asdl`` file, which is reproduced below. There is one class defined for each left-hand side symbol in @@ -39,12 +39,15 @@ attribute ``left`` of type ``_ast.expr``. Instances of ``_ast.expr`` and ``_ast.stmt`` subclasses also have lineno and col_offset attributes. The lineno is the line number of source text (1 indexed so the first line is line 1) and the col_offset is the utf8 byte offset of the first token that generated the -node. The utf8 offset is recorded because the parser uses utf8 internally. +node. The utf8 offset is recorded because the parser uses utf8 internally. If these attributes are marked as optional in the grammar (using a question mark), the value might be ``None``. If the attributes can have zero-or-more values (marked with an asterisk), the values are represented as Python lists. +The constructors of all ``_ast`` classes don't take arguments; instead, if you +create instances, you must assign the required attributes separately. + Abstract Grammar ---------------- |