Skip to content

Commit c0cbdcf

Browse files
author
martin.v.loewis
committed
Create _ast module.
Cleanup Python-ast.c generation. git-svn-id: http://svn.python.org/projects/python/trunk@42589 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 1539658 commit c0cbdcf

7 files changed

Lines changed: 485 additions & 221 deletions

File tree

Doc/lib/lib.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ \chapter*{Front Matter\label{front}}
416416
\input{distutils}
417417

418418
\input{compiler} % compiler package
419+
\input{libast}
419420

420421
\input{libmisc} % Miscellaneous Services
421422
\input{libformatter}

Doc/lib/libast.tex

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
% XXX Label can't be _ast?
2+
% XXX Where should this section/chapter go?
3+
\chapter{Abstract Syntax Trees\label{ast}}
4+
5+
\sectionauthor{Martin v. L\"owis}{martin@v.loewis.de}
6+
7+
The \code{_ast} module helps Python applications to process
8+
trees of the Python abstract syntax grammar. The Python compiler
9+
currently provides read-only access to such trees, meaning that
10+
applications can only create a tree for a given piece of Python
11+
source code; generating byte code from a (potentially modified)
12+
tree is not supported. The abstract syntax itself might change with
13+
each Python release; this module helps to find out programmatically
14+
what the current grammar looks like.
15+
16+
An abstract syntax tree can be generated by passing \code{_ast.PyCF_ONLY_AST}
17+
as a flag to the \function{compile} builtin function. The result will be a tree
18+
of objects whose classes all inherit from \code{_ast.AST}.
19+
20+
The actual classes are derived from the \code{Parser/Python.asdl} file,
21+
which is reproduced below. There is one class defined for each left-hand
22+
side symbol in the abstract grammar (for example, \code{_ast.stmt} or \code{_ast.expr}).
23+
In addition, there is one class defined for each constructor on the
24+
right-hand side; these classes inherit from the classes for the left-hand
25+
side trees. For example, \code{_ast.BinOp} inherits from \code{_ast.expr}.
26+
For production rules with alternatives (aka "sums"), the left-hand side
27+
class is abstract: only instances of specific constructor nodes are ever
28+
created.
29+
30+
Each concrete class has an attribute \code{_fields} which gives the
31+
names of all child nodes.
32+
33+
Each instance of a concrete class has one attribute for each child node,
34+
of the type as defined in the grammar. For example, \code{_ast.BinOp}
35+
instances have an attribute \code{left} of type \code{_ast.expr}.
36+
37+
If these attributes are marked as optional in the grammar (using a
38+
question mark), the value might be \code{None}. If the attributes
39+
can have zero-or-more values (marked with an asterisk), the
40+
values are represented as Python lists.
41+
42+
\subsection{Abstract Grammar}
43+
44+
The abstract grammar is currently defined as follows:
45+
46+
\verbatiminput{../../Parser/Python.asdl}

Misc/NEWS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Core and builtins
7676

7777
- A new AST parser implementation was completed. The abstract
7878
syntax tree is available for read-only (non-compile) access
79-
to Python code.
79+
to Python code; an _ast module was added.
8080

8181
- SF bug #1167751: fix incorrect code being for generator expressions.
8282
The following code now raises a SyntaxError: foo(a = i for i in range(10))

Modules/config.c.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
2323
extern void PyMarshal_Init(void);
2424
extern void initimp(void);
2525
extern void initgc(void);
26+
extern void init_ast(void);
2627

2728
struct _inittab _PyImport_Inittab[] = {
2829

@@ -34,6 +35,9 @@ struct _inittab _PyImport_Inittab[] = {
3435
/* This lives in import.c */
3536
{"imp", initimp},
3637

38+
/* This lives in Python/Python-ast.c */
39+
{"_ast", init_ast},
40+
3741
/* These entries are here for sys.builtin_module_names */
3842
{"__main__", NULL},
3943
{"__builtin__", NULL},

PC/config.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ extern void init_codecs_kr(void);
6767
extern void init_codecs_tw(void);
6868
extern void init_subprocess(void);
6969
extern void init_lsprof(void);
70+
extern void init_ast(void);
7071

7172
/* tools/freeze/makeconfig.py marker for additional "extern" */
7273
/* -- ADDMODULE MARKER 1 -- */
@@ -77,6 +78,7 @@ extern void initimp(void);
7778
struct _inittab _PyImport_Inittab[] = {
7879

7980
{"array", initarray},
81+
{"_ast", init_ast},
8082
#ifdef MS_WINDOWS
8183
#ifndef MS_WIN64
8284
{"audioop", initaudioop},

0 commit comments

Comments
 (0)