| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -416,6 +416,7 @@ \chapter*{Front Matter\label{front}} | |||
| 416 | 416 | \input{distutils} | |
| 417 | 417 | ||
| 418 | 418 | \input{compiler} % compiler package | |
| 419 | + \input{libast} | ||
| 419 | 420 | ||
| 420 | 421 | \input{libmisc} % Miscellaneous Services | |
| 421 | 422 | \input{libformatter} | |
| Original file line number | Diff line number | Diff 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} | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -76,7 +76,7 @@ Core and builtins | |||
| 76 | 76 | ||
| 77 | 77 | - A new AST parser implementation was completed. The abstract | |
| 78 | 78 | syntax tree is available for read-only (non-compile) access | |
| 79 | - to Python code. | ||
| 79 | + to Python code; an _ast module was added. | ||
| 80 | 80 | ||
| 81 | 81 | - SF bug #1167751: fix incorrect code being for generator expressions. | |
| 82 | 82 | The following code now raises a SyntaxError: foo(a = i for i in range(10)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,6 +23,7 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. | |||
| 23 | 23 | extern void PyMarshal_Init(void); | |
| 24 | 24 | extern void initimp(void); | |
| 25 | 25 | extern void initgc(void); | |
| 26 | + extern void init_ast(void); | ||
| 26 | 27 | ||
| 27 | 28 | struct _inittab _PyImport_Inittab[] = { | |
| 28 | 29 | ||
@@ -34,6 +35,9 @@ struct _inittab _PyImport_Inittab[] = { | |||
| 34 | 35 | /* This lives in import.c */ | |
| 35 | 36 | {"imp", initimp}, | |
| 36 | 37 | ||
| 38 | + /* This lives in Python/Python-ast.c */ | ||
| 39 | + {"_ast", init_ast}, | ||
| 40 | + | ||
| 37 | 41 | /* These entries are here for sys.builtin_module_names */ | |
| 38 | 42 | {"__main__", NULL}, | |
| 39 | 43 | {"__builtin__", NULL}, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -67,6 +67,7 @@ extern void init_codecs_kr(void); | |||
| 67 | 67 | extern void init_codecs_tw(void); | |
| 68 | 68 | extern void init_subprocess(void); | |
| 69 | 69 | extern void init_lsprof(void); | |
| 70 | + extern void init_ast(void); | ||
| 70 | 71 | ||
| 71 | 72 | /* tools/freeze/makeconfig.py marker for additional "extern" */ | |
| 72 | 73 | /* -- ADDMODULE MARKER 1 -- */ | |
@@ -77,6 +78,7 @@ extern void initimp(void); | |||
| 77 | 78 | struct _inittab _PyImport_Inittab[] = { | |
| 78 | 79 | ||
| 79 | 80 | {"array", initarray}, | |
| 81 | + {"_ast", init_ast}, | ||
| 80 | 82 | #ifdef MS_WINDOWS | |
| 81 | 83 | #ifndef MS_WIN64 | |
| 82 | 84 | {"audioop", initaudioop}, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments