summaryrefslogtreecommitdiffstats
path: root/Doc/lib/libparser.tex
blob: 1f5d4fda750ac7fb191b391b61e1d7a03986f1ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
% libparser.tex
%
% Introductory documentation for the new parser built-in module.
%
% Copyright 1995 Virginia Polytechnic Institute and State University
% and Fred L. Drake, Jr.  This copyright notice must be distributed on
% all copies, but this document otherwise may be distributed as part
% of the Python distribution.  No fee may be charged for this document
% in any representation, either on paper or electronically.  This
% restriction does not affect other elements in a distributed package
% in any way.
%

\section{Built-in Module \sectcode{parser}}
\bimodindex{parser}


% ==== 2. ====
% Give a short overview of what the module does.
% If it is platform specific, mention this.
% Mention other important restrictions or general operating principles.

The \code{parser} module provides an interface to Python's internal
parser and byte-code compiler.  The primary purpose for this interface
is to allow Python code to edit the parse tree of a Python expression
and create executable code from this.  This can be better than trying
to parse and modify an arbitrary Python code fragment as a string, and
ensures that parsing is performed in a manner identical to the code
forming the application.  It's also faster.

There are a few things to note about this module which are important
to making use of the data structures created.  This is not a tutorial
on editing the parse trees for Python code.

Most importantly, a good understanding of the Python grammar processed
by the internal parser is required.  For full information on the
language syntax, refer to the Language Reference.  The parser itself
is created from a grammar specification defined in the file
\code{Grammar/Grammar} in the standard Python distribution.  The parse
trees stored in the ``AST objects'' created by this module are the
actual output from the internal parser when created by the
\code{expr()} or \code{suite()} functions, described below.  The AST
objects created by \code{tuple2ast()} faithfully simulate those
structures.

Each element of the tuples returned by \code{ast2tuple()} has a simple
form.  Tuples representing non-terminal elements in the grammar always
have a length greater than one.  The first element is an integer which
identifies a production in the grammar.  These integers are given
symbolic names in the C header file \code{Include/graminit.h} and the
Python module \code{Lib/symbol.py}.  Each additional element of the
tuple represents a component of the production as recognized in the
input string: these are always tuples which have the same form as the
parent.  An important aspect of this structure which should be noted
is that keywords used to identify the parent node type, such as the
keyword \code{if} in an \emph{if\_stmt}, are included in the node tree
without any special treatment.  For example, the \code{if} keyword is
represented by the tuple \code{(1, 'if')}, where \code{1} is the
numeric value associated with all \code{NAME} elements, including
variable and function names defined by the user.

Terminal elements are represented in much the same way, but without
any child elements and the addition of the source text which was
identified.  The example of the \code{if} keyword above is
representative.  The various types of terminal symbols are defined in
the C header file \code{Include/token.h} and the Python module
\code{Lib/token.py}.

The AST objects are not actually required to support the functionality
of this module, but are provided for three purposes: to allow an
application to amortize the cost of processing complex parse trees, to
provide a parse tree representation which conserves memory space when
compared to the Python tuple representation, and to ease the creation
of additional modules in C which manipulate parse trees.  A simple
``wrapper'' module may be created in Python if desired to hide the use
of AST objects.


% ==== 3. ====
% List the public functions defined by the module.  Begin with a
% standard phrase.  You may also list the exceptions and other data
% items defined in the module, insofar as they are important for the
% user.

The \code{parser} module defines the following functions:

% ---- 3.1. ----
% Redefine the ``indexsubitem'' macro to point to this module
% (alternatively, you can put this at the top of the file):

\renewcommand{\indexsubitem}{(in module parser)}

% ---- 3.2. ----
% For each function, use a ``funcdesc'' block.  This has exactly two
% parameters (each parameters is contained in a set of curly braces):
% the first parameter is the function name (this automatically
% generates an index entry); the second parameter is the function's
% argument list.  If there are no arguments, use an empty pair of
% curly braces.  If there is more than one argument, separate the
% arguments with backslash-comma.  Optional parts of the parameter
% list are contained in \optional{...} (this generates a set of square
% brackets around its parameter).  Arguments are automatically set in
% italics in the parameter list.  Each argument should be mentioned at
% least once in the description; each usage (even inside \code{...})
% should be enclosed in \var{...}.

\begin{funcdesc}{ast2tuple}{ast}
This function accepts an AST object from the caller in
\code{\var{ast}} and returns a Python tuple representing the
equivelent parse tree.  The resulting tuple representation can be used
for inspection or the creation of a new parse tree in tuple form.
This function does not fail so long as memory is available to build
the tuple representation.
\end{funcdesc}


\begin{funcdesc}{compileast}{ast\optional{\, filename \code{= '<ast>'}}}
The Python byte compiler can be invoked on an AST object to produce
code objects which can be used as part of an \code{exec} statement or
a call to the built-in \code{eval()} function.  This function provides
the interface to the compiler, passing the internal parse tree from
\code{\var{ast}} to the parser, using the source file name specified
by the \code{\var{filename}} parameter.  The default value supplied
for \code{\var{filename}} indicates that the source was an AST object.
\end{funcdesc}


\begin{funcdesc}{expr}{string}
The \code{expr()} function parses the parameter \code{\var{string}}
as if it were an input to \code{compile(\var{string}, 'eval')}.  If
the parse succeeds, an AST object is created to hold the internal
parse tree representation, otherwise an appropriate exception is
thrown.
\end{funcdesc}


\begin{funcdesc}{isexpr}{ast}
When \code{\var{ast}} represents an \code{'eval'} form, this function
returns a true value (\code{1}), otherwise it returns false
(\code{0}).  This is useful, since code objects normally cannot be
queried for this information using existing built-in functions.  Note
that the code objects created by \code{compileast()} cannot be queried
like this either, and are identical to those created by the built-in
\code{compile()} function.
\end{funcdesc}


\begin{funcdesc}{issuite}{ast}
This function mirrors \code{isexpr()} in that it reports whether an
AST object represents a suite of statements.  It is not safe to assume
that this function is equivelent to \code{not isexpr(\var{ast})}, as
additional syntactic fragments may be supported in the future.
\end{funcdesc}


\begin{funcdesc}{suite}{string}
The \code{suite()} function parses the parameter \code{\var{string}}
as if it were an input to \code{compile(\var{string}, 'exec')}.  If
the parse succeeds, an AST object is created to hold the internal
parse tree representation, otherwise an appropriate exception is
thrown.
\end{funcdesc}


\begin{funcdesc}{tuple2ast}{tuple}
This function accepts a parse tree represented as a tuple and builds
an internal representation if possible.  If it can validate that the
tree conforms to the Python syntax and all nodes are valid node types
in the host version of Python, an AST object is created from the
internal representation and returned to the called.  If there is a
problem creating the internal representation, or if the tree cannot be
validated, a \code{ParserError} exception is thrown.  An AST object
created this way should not be assumed to compile correctly; normal
exceptions thrown by compilation may still be initiated when the AST
object is passed to \code{compileast()}.  This will normally indicate
problems not related to syntax (such as a \code{MemoryError}
exception).
\end{funcdesc}


% --- 3.4. ---
% Exceptions are described using a ``excdesc'' block.  This has only
% one parameter: the exception name.

\subsection{Exceptions and Error Handling}

The parser module defines a single exception, but may also pass other
built-in exceptions from other portions of the Python runtime
environment.  See each function for information about the exceptions
it can raise.

\begin{excdesc}{ParserError}
Exception raised when a failure occurs within the parser module.  This
is generally produced for validation failures rather than the built in
\code{SyntaxError} thrown during normal parsing.
The exception argument is either a string describing the reason of the
failure or a tuple containing a tuple causing the failure from a parse
tree passed to \code{tuple2ast()} and an explanatory string.  Calls to
\code{tuple2ast()} need to be able to handle either type of exception,
while calls to other functions in the module will only need to be
aware of the simple string values.
\end{excdesc}

Note that the functions \code{compileast()}, \code{expr()}, and
\code{suite()} may throw exceptions which are normally thrown by the
parsing and compilation process.  These include the built in
exceptions \code{MemoryError}, \code{OverflowError},
\code{SyntaxError}, and \code{SystemError}.  In these cases, these
exceptions carry all the meaning normally associated with them.  Refer
to the descriptions of each function for detailed information.

% ---- 3.5. ----
% There is no standard block type for classes.  I generally use
% ``funcdesc'' blocks, since class instantiation looks very much like
% a function call.


% ==== 4. ====
% Now is probably a good time for a complete example.  (Alternatively,
% an example giving the flavor of the module may be given before the
% detailed list of functions.)

\subsection{Example}

A simple example:

\begin{verbatim}
>>> import parser
>>> ast = parser.expr('a + 5')
>>> code = parser.compileast(ast)
>>> a = 5
>>> eval(code)
10
\end{verbatim}


\subsection{AST Objects}

AST objects (returned by \code{expr()}, \code{suite()}, and
\code{tuple2ast()}, described above) have no methods of their own.
Some of the functions defined which accept an AST object as their
first argument may change to object methods in the future.

Ordered and equality comparisons are supported between AST objects.

\renewcommand{\indexsubitem}{(ast method)}

%\begin{funcdesc}{empty}{}
%Empty the can into the trash.
%\end{funcdesc}