annotate Doc/libparser.tex @ 3216:d00a79ebb027 legacy-trunk

Added missing imgpbm and imgsgi modules
author Jack Jansen <jack.jansen@cwi.nl>
date Thu, 12 Oct 1995 10:23:27 +0000
parents 21cd50a4a3f7
children 56746eedfe03
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3198
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 % libparser.tex
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 %
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 % Introductory documentation for the new parser built-in module.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 %
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 % Copyright 1995 Virginia Polytechnic Institute and State University
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 % and Fred L. Drake, Jr. This copyright notice must be distributed on
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 % all copies, but this document otherwise may be distributed as part
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 % of the Python distribution. No fee may be charged for this document
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 % in any representation, either on paper or electronically. This
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 % restriction does not affect other elements in a distributed package
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 % in any way.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 %
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 \section{Built-in Module \sectcode{parser}}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 \bimodindex{parser}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 % ==== 2. ====
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 % Give a short overview of what the module does.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 % If it is platform specific, mention this.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 % Mention other important restrictions or general operating principles.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 The \code{parser} module provides an interface to Python's internal
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 parser and byte-code compiler. The primary purpose for this interface
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 is to allow Python code to edit the parse tree of a Python expression
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 and create executable code from this. This can be better than trying
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 to parse and modify an arbitrary Python code fragment as a string, and
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 ensures that parsing is performed in a manner identical to the code
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 forming the application. It's also faster.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 There are a few things to note about this module which are important
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 to making use of the data structures created. This is not a tutorial
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 on editing the parse trees for Python code.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 Most importantly, a good understanding of the Python grammar processed
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 by the internal parser is required. For full information on the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 language syntax, refer to the Language Reference. The parser itself
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 is created from a grammar specification defined in the file
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 \code{Grammar/Grammar} in the standard Python distribution. The parse
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 trees stored in the ``AST objects'' created by this module are the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 actual output from the internal parser when created by the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 \code{expr()} or \code{suite()} functions, described below. The AST
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 objects created by \code{tuple2ast()} faithfully simulate those
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 structures.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 Each element of the tuples returned by \code{ast2tuple()} has a simple
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 form. Tuples representing non-terminal elements in the grammar always
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 have a length greater than one. The first element is an integer which
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 identifies a production in the grammar. These integers are given
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 symbolic names in the C header file \code{Include/graminit.h} and the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 Python module \code{Lib/symbol.py}. Each additional element of the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 tuple represents a component of the production as recognized in the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 input string: these are always tuples which have the same form as the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 parent. An important aspect of this structure which should be noted
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 is that keywords used to identify the parent node type, such as the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 keyword \code{if} in an \emph{if\_stmt}, are included in the node tree
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 without any special treatment. For example, the \code{if} keyword is
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 represented by the tuple \code{(1, 'if')}, where \code{1} is the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 numeric value associated with all \code{NAME} elements, including
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 variable and function names defined by the user.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 Terminal elements are represented in much the same way, but without
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 any child elements and the addition of the source text which was
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 identified. The example of the \code{if} keyword above is
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 representative. The various types of terminal symbols are defined in
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 the C header file \code{Include/token.h} and the Python module
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 \code{Lib/token.py}.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 The AST objects are not actually required to support the functionality
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 of this module, but are provided for three purposes: to allow an
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 application to amortize the cost of processing complex parse trees, to
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 provide a parse tree representation which conserves memory space when
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 compared to the Python tuple representation, and to ease the creation
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 of additional modules in C which manipulate parse trees. A simple
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 ``wrapper'' module may be created in Python if desired to hide the use
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 of AST objects.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 % ==== 3. ====
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 % List the public functions defined by the module. Begin with a
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 % standard phrase. You may also list the exceptions and other data
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 % items defined in the module, insofar as they are important for the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 % user.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 The \code{parser} module defines the following functions:
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 % ---- 3.1. ----
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 % Redefine the ``indexsubitem'' macro to point to this module
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 % (alternatively, you can put this at the top of the file):
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 \renewcommand{\indexsubitem}{(in module parser)}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 % ---- 3.2. ----
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 % For each function, use a ``funcdesc'' block. This has exactly two
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 % parameters (each parameters is contained in a set of curly braces):
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 % the first parameter is the function name (this automatically
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 % generates an index entry); the second parameter is the function's
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 % argument list. If there are no arguments, use an empty pair of
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 % curly braces. If there is more than one argument, separate the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 % arguments with backslash-comma. Optional parts of the parameter
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 % list are contained in \optional{...} (this generates a set of square
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 % brackets around its parameter). Arguments are automatically set in
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 % italics in the parameter list. Each argument should be mentioned at
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 % least once in the description; each usage (even inside \code{...})
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 % should be enclosed in \var{...}.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 \begin{funcdesc}{ast2tuple}{ast}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 This function accepts an AST object from the caller in
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 \code{\var{ast}} and returns a Python tuple representing the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 equivelent parse tree. The resulting tuple representation can be used
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 for inspection or the creation of a new parse tree in tuple form.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 This function does not fail so long as memory is available to build
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 the tuple representation.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 \end{funcdesc}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 \begin{funcdesc}{compileast}{ast\optional{\, filename \code{= '<ast>'}}}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 The Python byte compiler can be invoked on an AST object to produce
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 code objects which can be used as part of an \code{exec} statement or
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 a call to the built-in \code{eval()} function. This function provides
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 the interface to the compiler, passing the internal parse tree from
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 \code{\var{ast}} to the parser, using the source file name specified
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 by the \code{\var{filename}} parameter. The default value supplied
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 for \code{\var{filename}} indicates that the source was an AST object.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 \end{funcdesc}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 \begin{funcdesc}{expr}{string}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 The \code{expr()} function parses the parameter \code{\var{string}}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 as if it were an input to \code{compile(\var{string}, 'eval')}. If
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 the parse succeeds, an AST object is created to hold the internal
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 parse tree representation, otherwise an appropriate exception is
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 thrown.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 \end{funcdesc}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
135
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 \begin{funcdesc}{isexpr}{ast}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 When \code{\var{ast}} represents an \code{'eval'} form, this function
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 returns a true value (\code{1}), otherwise it returns false
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 (\code{0}). This is useful, since code objects normally cannot be
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 queried for this information using existing built-in functions. Note
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 that the code objects created by \code{compileast()} cannot be queried
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 like this either, and are identical to those created by the built-in
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 \code{compile()} function.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 \end{funcdesc}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 \begin{funcdesc}{issuite}{ast}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 This function mirrors \code{isexpr()} in that it reports whether an
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 AST object represents a suite of statements. It is not safe to assume
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 that this function is equivelent to \code{not isexpr(\var{ast})}, as
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 additional syntactic fragments may be supported in the future.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 \end{funcdesc}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 \begin{funcdesc}{suite}{string}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 The \code{suite()} function parses the parameter \code{\var{string}}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 as if it were an input to \code{compile(\var{string}, 'exec')}. If
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 the parse succeeds, an AST object is created to hold the internal
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 parse tree representation, otherwise an appropriate exception is
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 thrown.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 \end{funcdesc}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
163
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
164
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 \begin{funcdesc}{tuple2ast}{tuple}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 This function accepts a parse tree represented as a tuple and builds
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 an internal representation if possible. If it can validate that the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 tree conforms to the Python syntax and all nodes are valid node types
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 in the host version of Python, an AST object is created from the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 internal representation and returned to the called. If there is a
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 problem creating the internal representation, or if the tree cannot be
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 validated, a \code{ParserError} exception is thrown. An AST object
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 created this way should not be assumed to compile correctly; normal
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 exceptions thrown by compilation may still be initiated when the AST
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 object is passed to \code{compileast()}. This will normally indicate
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 problems not related to syntax (such as a \code{MemoryError}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 exception).
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 \end{funcdesc}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
179
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 % --- 3.4. ---
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 % Exceptions are described using a ``excdesc'' block. This has only
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 % one parameter: the exception name.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
184
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 \subsection{Exceptions and Error Handling}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
186
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 The parser module defines a single exception, but may also pass other
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 built-in exceptions from other portions of the Python runtime
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 environment. See each function for information about the exceptions
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 it can raise.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 \begin{excdesc}{ParserError}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 Exception raised when a failure occurs within the parser module. This
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 is generally produced for validation failures rather than the built in
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 \code{SyntaxError} thrown during normal parsing.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 The exception argument is either a string describing the reason of the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 failure or a tuple containing a tuple causing the failure from a parse
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 tree passed to \code{tuple2ast()} and an explanatory string. Calls to
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 \code{tuple2ast()} need to be able to handle either type of exception,
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 while calls to other functions in the module will only need to be
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 aware of the simple string values.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 \end{excdesc}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
203
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 Note that the functions \code{compileast()}, \code{expr()}, and
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 \code{suite()} may throw exceptions which are normally thrown by the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 parsing and compilation process. These include the built in
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 exceptions \code{MemoryError}, \code{OverflowError},
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 \code{SyntaxError}, and \code{SystemError}. In these cases, these
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 exceptions carry all the meaning normally associated with them. Refer
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 to the descriptions of each function for detailed information.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 % ---- 3.5. ----
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 % There is no standard block type for classes. I generally use
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 % ``funcdesc'' blocks, since class instantiation looks very much like
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 % a function call.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
216
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 % ==== 4. ====
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 % Now is probably a good time for a complete example. (Alternatively,
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 % an example giving the flavor of the module may be given before the
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 % detailed list of functions.)
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 \subsection{Example}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
224
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 A simple example:
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 \begin{verbatim}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 >>> import parser
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 >>> ast = parser.expr('a + 5')
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 >>> code = parser.compileast(ast)
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 >>> a = 5
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 >>> eval(code)
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 10
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 \end{verbatim}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
235
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 \subsection{AST Objects}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
238
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 AST objects (returned by \code{expr()}, \code{suite()}, and
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 \code{tuple2ast()}, described above) have no methods of their own.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 Some of the functions defined which accept an AST object as their
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 first argument may change to object methods in the future.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
243
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 Ordered and equality comparisons are supported between AST objects.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
245
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 \renewcommand{\indexsubitem}{(ast method)}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
247
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 %\begin{funcdesc}{empty}{}
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 %Empty the can into the trash.
21cd50a4a3f7 Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 %\end{funcdesc}