blob: a302681eca056855c8b458d2412f6d4380833b55 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`parser` --- Access Python parse trees
2===========================================
3
4.. module:: parser
5 :synopsis: Access parse trees for Python source code.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
9
Christian Heimes5b5e81c2007-12-31 16:14:33 +000010.. Copyright 1995 Virginia Polytechnic Institute and State University and Fred
11 L. Drake, Jr. This copyright notice must be distributed on all copies, but
12 this document otherwise may be distributed as part of the Python
13 distribution. No fee may be charged for this document in any representation,
14 either on paper or electronically. This restriction does not affect other
15 elements in a distributed package in any way.
Georg Brandl116aa622007-08-15 14:28:22 +000016
17.. index:: single: parsing; Python source code
18
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040019--------------
20
Georg Brandl116aa622007-08-15 14:28:22 +000021The :mod:`parser` module provides an interface to Python's internal parser and
22byte-code compiler. The primary purpose for this interface is to allow Python
23code to edit the parse tree of a Python expression and create executable code
24from this. This is better than trying to parse and modify an arbitrary Python
25code fragment as a string because parsing is performed in a manner identical to
26the code forming the application. It is also faster.
27
Georg Brandl0c77a822008-06-10 16:37:50 +000028.. note::
29
30 From Python 2.5 onward, it's much more convenient to cut in at the Abstract
31 Syntax Tree (AST) generation and compilation stage, using the :mod:`ast`
32 module.
33
Georg Brandl116aa622007-08-15 14:28:22 +000034There are a few things to note about this module which are important to making
35use of the data structures created. This is not a tutorial on editing the parse
36trees for Python code, but some examples of using the :mod:`parser` module are
37presented.
38
39Most importantly, a good understanding of the Python grammar processed by the
40internal parser is required. For full information on the language syntax, refer
41to :ref:`reference-index`. The parser
42itself is created from a grammar specification defined in the file
43:file:`Grammar/Grammar` in the standard Python distribution. The parse trees
Georg Brandl0c77a822008-06-10 16:37:50 +000044stored in the ST objects created by this module are the actual output from the
Georg Brandl116aa622007-08-15 14:28:22 +000045internal parser when created by the :func:`expr` or :func:`suite` functions,
Georg Brandl0c77a822008-06-10 16:37:50 +000046described below. The ST objects created by :func:`sequence2st` faithfully
Georg Brandl116aa622007-08-15 14:28:22 +000047simulate those structures. Be aware that the values of the sequences which are
48considered "correct" will vary from one version of Python to another as the
49formal grammar for the language is revised. However, transporting code from one
50Python version to another as source text will always allow correct parse trees
51to be created in the target version, with the only restriction being that
52migrating to an older version of the interpreter will not support more recent
53language constructs. The parse trees are not typically compatible from one
54version to another, whereas source code has always been forward-compatible.
55
Georg Brandl0c77a822008-06-10 16:37:50 +000056Each element of the sequences returned by :func:`st2list` or :func:`st2tuple`
Georg Brandl116aa622007-08-15 14:28:22 +000057has a simple form. Sequences representing non-terminal elements in the grammar
58always have a length greater than one. The first element is an integer which
59identifies a production in the grammar. These integers are given symbolic names
60in the C header file :file:`Include/graminit.h` and the Python module
61:mod:`symbol`. Each additional element of the sequence represents a component
62of the production as recognized in the input string: these are always sequences
63which have the same form as the parent. An important aspect of this structure
64which should be noted is that keywords used to identify the parent node type,
65such as the keyword :keyword:`if` in an :const:`if_stmt`, are included in the
Serhiy Storchaka2b57c432018-12-19 08:09:46 +020066node tree without any special treatment. For example, the :keyword:`!if` keyword
Georg Brandl116aa622007-08-15 14:28:22 +000067is represented by the tuple ``(1, 'if')``, where ``1`` is the numeric value
68associated with all :const:`NAME` tokens, including variable and function names
69defined by the user. In an alternate form returned when line number information
70is requested, the same token might be represented as ``(1, 'if', 12)``, where
71the ``12`` represents the line number at which the terminal symbol was found.
72
73Terminal elements are represented in much the same way, but without any child
74elements and the addition of the source text which was identified. The example
75of the :keyword:`if` keyword above is representative. The various types of
76terminal symbols are defined in the C header file :file:`Include/token.h` and
77the Python module :mod:`token`.
78
Georg Brandl0c77a822008-06-10 16:37:50 +000079The ST objects are not required to support the functionality of this module,
Georg Brandl116aa622007-08-15 14:28:22 +000080but are provided for three purposes: to allow an application to amortize the
81cost of processing complex parse trees, to provide a parse tree representation
82which conserves memory space when compared to the Python list or tuple
83representation, and to ease the creation of additional modules in C which
84manipulate parse trees. A simple "wrapper" class may be created in Python to
Georg Brandl0c77a822008-06-10 16:37:50 +000085hide the use of ST objects.
Georg Brandl116aa622007-08-15 14:28:22 +000086
87The :mod:`parser` module defines functions for a few distinct purposes. The
Georg Brandl0c77a822008-06-10 16:37:50 +000088most important purposes are to create ST objects and to convert ST objects to
Georg Brandl116aa622007-08-15 14:28:22 +000089other representations such as parse trees and compiled code objects, but there
90are also functions which serve to query the type of parse tree represented by an
Georg Brandl0c77a822008-06-10 16:37:50 +000091ST object.
Georg Brandl116aa622007-08-15 14:28:22 +000092
93
94.. seealso::
95
96 Module :mod:`symbol`
97 Useful constants representing internal nodes of the parse tree.
98
99 Module :mod:`token`
100 Useful constants representing leaf nodes of the parse tree and functions for
101 testing node values.
102
103
Georg Brandl0c77a822008-06-10 16:37:50 +0000104.. _creating-sts:
Georg Brandl116aa622007-08-15 14:28:22 +0000105
Georg Brandl0c77a822008-06-10 16:37:50 +0000106Creating ST Objects
107-------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000108
Georg Brandl0c77a822008-06-10 16:37:50 +0000109ST objects may be created from source code or from a parse tree. When creating
110an ST object from source, different functions are used to create the ``'eval'``
Georg Brandl116aa622007-08-15 14:28:22 +0000111and ``'exec'`` forms.
112
113
114.. function:: expr(source)
115
116 The :func:`expr` function parses the parameter *source* as if it were an input
Georg Brandl0c77a822008-06-10 16:37:50 +0000117 to ``compile(source, 'file.py', 'eval')``. If the parse succeeds, an ST object
Georg Brandl116aa622007-08-15 14:28:22 +0000118 is created to hold the internal parse tree representation, otherwise an
Georg Brandl7cb13192010-08-03 12:06:29 +0000119 appropriate exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121
122.. function:: suite(source)
123
124 The :func:`suite` function parses the parameter *source* as if it were an input
Georg Brandl0c77a822008-06-10 16:37:50 +0000125 to ``compile(source, 'file.py', 'exec')``. If the parse succeeds, an ST object
Georg Brandl116aa622007-08-15 14:28:22 +0000126 is created to hold the internal parse tree representation, otherwise an
Georg Brandl7cb13192010-08-03 12:06:29 +0000127 appropriate exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000128
129
Georg Brandl0c77a822008-06-10 16:37:50 +0000130.. function:: sequence2st(sequence)
Georg Brandl116aa622007-08-15 14:28:22 +0000131
132 This function accepts a parse tree represented as a sequence and builds an
133 internal representation if possible. If it can validate that the tree conforms
134 to the Python grammar and all nodes are valid node types in the host version of
Georg Brandl0c77a822008-06-10 16:37:50 +0000135 Python, an ST object is created from the internal representation and returned
Georg Brandl116aa622007-08-15 14:28:22 +0000136 to the called. If there is a problem creating the internal representation, or
Georg Brandl7cb13192010-08-03 12:06:29 +0000137 if the tree cannot be validated, a :exc:`ParserError` exception is raised. An
Georg Brandl0c77a822008-06-10 16:37:50 +0000138 ST object created this way should not be assumed to compile correctly; normal
Georg Brandl7cb13192010-08-03 12:06:29 +0000139 exceptions raised by compilation may still be initiated when the ST object is
Georg Brandl0c77a822008-06-10 16:37:50 +0000140 passed to :func:`compilest`. This may indicate problems not related to syntax
Georg Brandl116aa622007-08-15 14:28:22 +0000141 (such as a :exc:`MemoryError` exception), but may also be due to constructs such
142 as the result of parsing ``del f(0)``, which escapes the Python parser but is
143 checked by the bytecode compiler.
144
145 Sequences representing terminal tokens may be represented as either two-element
146 lists of the form ``(1, 'name')`` or as three-element lists of the form ``(1,
147 'name', 56)``. If the third element is present, it is assumed to be a valid
148 line number. The line number may be specified for any subset of the terminal
149 symbols in the input tree.
150
151
Georg Brandl0c77a822008-06-10 16:37:50 +0000152.. function:: tuple2st(sequence)
Georg Brandl116aa622007-08-15 14:28:22 +0000153
Georg Brandl0c77a822008-06-10 16:37:50 +0000154 This is the same function as :func:`sequence2st`. This entry point is
Georg Brandl116aa622007-08-15 14:28:22 +0000155 maintained for backward compatibility.
156
157
Georg Brandl0c77a822008-06-10 16:37:50 +0000158.. _converting-sts:
Georg Brandl116aa622007-08-15 14:28:22 +0000159
Georg Brandl0c77a822008-06-10 16:37:50 +0000160Converting ST Objects
161---------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000162
Georg Brandl0c77a822008-06-10 16:37:50 +0000163ST objects, regardless of the input used to create them, may be converted to
Georg Brandl116aa622007-08-15 14:28:22 +0000164parse trees represented as list- or tuple- trees, or may be compiled into
165executable code objects. Parse trees may be extracted with or without line
166numbering information.
167
168
Georg Brandl18244152009-09-02 20:34:52 +0000169.. function:: st2list(st, line_info=False, col_info=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000170
Georg Brandl30704ea02008-07-23 15:07:12 +0000171 This function accepts an ST object from the caller in *st* and returns a
Georg Brandl116aa622007-08-15 14:28:22 +0000172 Python list representing the equivalent parse tree. The resulting list
173 representation can be used for inspection or the creation of a new parse tree in
174 list form. This function does not fail so long as memory is available to build
175 the list representation. If the parse tree will only be used for inspection,
Georg Brandl0c77a822008-06-10 16:37:50 +0000176 :func:`st2tuple` should be used instead to reduce memory consumption and
Georg Brandl116aa622007-08-15 14:28:22 +0000177 fragmentation. When the list representation is required, this function is
178 significantly faster than retrieving a tuple representation and converting that
179 to nested lists.
180
181 If *line_info* is true, line number information will be included for all
182 terminal tokens as a third element of the list representing the token. Note
183 that the line number provided specifies the line on which the token *ends*.
184 This information is omitted if the flag is false or omitted.
185
186
Georg Brandl18244152009-09-02 20:34:52 +0000187.. function:: st2tuple(st, line_info=False, col_info=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000188
Georg Brandl30704ea02008-07-23 15:07:12 +0000189 This function accepts an ST object from the caller in *st* and returns a
Georg Brandl116aa622007-08-15 14:28:22 +0000190 Python tuple representing the equivalent parse tree. Other than returning a
Georg Brandl0c77a822008-06-10 16:37:50 +0000191 tuple instead of a list, this function is identical to :func:`st2list`.
Georg Brandl116aa622007-08-15 14:28:22 +0000192
193 If *line_info* is true, line number information will be included for all
194 terminal tokens as a third element of the list representing the token. This
195 information is omitted if the flag is false or omitted.
196
197
Georg Brandl18244152009-09-02 20:34:52 +0000198.. function:: compilest(st, filename='<syntax-tree>')
Georg Brandl116aa622007-08-15 14:28:22 +0000199
200 .. index::
201 builtin: exec
202 builtin: eval
203
Georg Brandl0c77a822008-06-10 16:37:50 +0000204 The Python byte compiler can be invoked on an ST object to produce code objects
Georg Brandl116aa622007-08-15 14:28:22 +0000205 which can be used as part of a call to the built-in :func:`exec` or :func:`eval`
206 functions. This function provides the interface to the compiler, passing the
Georg Brandl30704ea02008-07-23 15:07:12 +0000207 internal parse tree from *st* to the parser, using the source file name
Georg Brandl116aa622007-08-15 14:28:22 +0000208 specified by the *filename* parameter. The default value supplied for *filename*
Georg Brandl0c77a822008-06-10 16:37:50 +0000209 indicates that the source was an ST object.
Georg Brandl116aa622007-08-15 14:28:22 +0000210
Georg Brandl0c77a822008-06-10 16:37:50 +0000211 Compiling an ST object may result in exceptions related to compilation; an
Georg Brandl116aa622007-08-15 14:28:22 +0000212 example would be a :exc:`SyntaxError` caused by the parse tree for ``del f(0)``:
213 this statement is considered legal within the formal grammar for Python but is
214 not a legal language construct. The :exc:`SyntaxError` raised for this
215 condition is actually generated by the Python byte-compiler normally, which is
216 why it can be raised at this point by the :mod:`parser` module. Most causes of
217 compilation failure can be diagnosed programmatically by inspection of the parse
218 tree.
219
220
Georg Brandl0c77a822008-06-10 16:37:50 +0000221.. _querying-sts:
Georg Brandl116aa622007-08-15 14:28:22 +0000222
Georg Brandl0c77a822008-06-10 16:37:50 +0000223Queries on ST Objects
224---------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000225
Georg Brandl0c77a822008-06-10 16:37:50 +0000226Two functions are provided which allow an application to determine if an ST was
Georg Brandl116aa622007-08-15 14:28:22 +0000227created as an expression or a suite. Neither of these functions can be used to
Georg Brandl0c77a822008-06-10 16:37:50 +0000228determine if an ST was created from source code via :func:`expr` or
229:func:`suite` or from a parse tree via :func:`sequence2st`.
Georg Brandl116aa622007-08-15 14:28:22 +0000230
231
Georg Brandl30704ea02008-07-23 15:07:12 +0000232.. function:: isexpr(st)
Georg Brandl116aa622007-08-15 14:28:22 +0000233
234 .. index:: builtin: compile
235
Georg Brandl30704ea02008-07-23 15:07:12 +0000236 When *st* represents an ``'eval'`` form, this function returns true, otherwise
Georg Brandl116aa622007-08-15 14:28:22 +0000237 it returns false. This is useful, since code objects normally cannot be queried
238 for this information using existing built-in functions. Note that the code
Georg Brandl0c77a822008-06-10 16:37:50 +0000239 objects created by :func:`compilest` cannot be queried like this either, and
Georg Brandl116aa622007-08-15 14:28:22 +0000240 are identical to those created by the built-in :func:`compile` function.
241
242
Georg Brandl30704ea02008-07-23 15:07:12 +0000243.. function:: issuite(st)
Georg Brandl116aa622007-08-15 14:28:22 +0000244
Georg Brandl0c77a822008-06-10 16:37:50 +0000245 This function mirrors :func:`isexpr` in that it reports whether an ST object
Georg Brandl116aa622007-08-15 14:28:22 +0000246 represents an ``'exec'`` form, commonly known as a "suite." It is not safe to
Georg Brandl30704ea02008-07-23 15:07:12 +0000247 assume that this function is equivalent to ``not isexpr(st)``, as additional
Georg Brandl116aa622007-08-15 14:28:22 +0000248 syntactic fragments may be supported in the future.
249
250
Georg Brandl0c77a822008-06-10 16:37:50 +0000251.. _st-errors:
Georg Brandl116aa622007-08-15 14:28:22 +0000252
253Exceptions and Error Handling
254-----------------------------
255
256The parser module defines a single exception, but may also pass other built-in
257exceptions from other portions of the Python runtime environment. See each
258function for information about the exceptions it can raise.
259
260
261.. exception:: ParserError
262
263 Exception raised when a failure occurs within the parser module. This is
Georg Brandl7cb13192010-08-03 12:06:29 +0000264 generally produced for validation failures rather than the built-in
265 :exc:`SyntaxError` raised during normal parsing. The exception argument is
Georg Brandl116aa622007-08-15 14:28:22 +0000266 either a string describing the reason of the failure or a tuple containing a
Georg Brandl0c77a822008-06-10 16:37:50 +0000267 sequence causing the failure from a parse tree passed to :func:`sequence2st`
268 and an explanatory string. Calls to :func:`sequence2st` need to be able to
Georg Brandl116aa622007-08-15 14:28:22 +0000269 handle either type of exception, while calls to other functions in the module
270 will only need to be aware of the simple string values.
271
Georg Brandl0c77a822008-06-10 16:37:50 +0000272Note that the functions :func:`compilest`, :func:`expr`, and :func:`suite` may
Éric Araujoff2a4ba2010-11-30 17:20:31 +0000273raise exceptions which are normally raised by the parsing and compilation
Georg Brandl116aa622007-08-15 14:28:22 +0000274process. These include the built in exceptions :exc:`MemoryError`,
275:exc:`OverflowError`, :exc:`SyntaxError`, and :exc:`SystemError`. In these
276cases, these exceptions carry all the meaning normally associated with them.
277Refer to the descriptions of each function for detailed information.
278
279
Georg Brandl0c77a822008-06-10 16:37:50 +0000280.. _st-objects:
Georg Brandl116aa622007-08-15 14:28:22 +0000281
Georg Brandl0c77a822008-06-10 16:37:50 +0000282ST Objects
283----------
Georg Brandl116aa622007-08-15 14:28:22 +0000284
Georg Brandl0c77a822008-06-10 16:37:50 +0000285Ordered and equality comparisons are supported between ST objects. Pickling of
286ST objects (using the :mod:`pickle` module) is also supported.
Georg Brandl116aa622007-08-15 14:28:22 +0000287
288
Georg Brandl0c77a822008-06-10 16:37:50 +0000289.. data:: STType
Georg Brandl116aa622007-08-15 14:28:22 +0000290
291 The type of the objects returned by :func:`expr`, :func:`suite` and
Georg Brandl0c77a822008-06-10 16:37:50 +0000292 :func:`sequence2st`.
Georg Brandl116aa622007-08-15 14:28:22 +0000293
Georg Brandl0c77a822008-06-10 16:37:50 +0000294ST objects have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000295
296
Georg Brandl18244152009-09-02 20:34:52 +0000297.. method:: ST.compile(filename='<syntax-tree>')
Georg Brandl116aa622007-08-15 14:28:22 +0000298
Georg Brandl0c77a822008-06-10 16:37:50 +0000299 Same as ``compilest(st, filename)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000300
301
Georg Brandl0c77a822008-06-10 16:37:50 +0000302.. method:: ST.isexpr()
Georg Brandl116aa622007-08-15 14:28:22 +0000303
Georg Brandl0c77a822008-06-10 16:37:50 +0000304 Same as ``isexpr(st)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000305
306
Georg Brandl0c77a822008-06-10 16:37:50 +0000307.. method:: ST.issuite()
Georg Brandl116aa622007-08-15 14:28:22 +0000308
Georg Brandl0c77a822008-06-10 16:37:50 +0000309 Same as ``issuite(st)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000310
311
Georg Brandl18244152009-09-02 20:34:52 +0000312.. method:: ST.tolist(line_info=False, col_info=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000313
Georg Brandl18244152009-09-02 20:34:52 +0000314 Same as ``st2list(st, line_info, col_info)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000315
316
Georg Brandl18244152009-09-02 20:34:52 +0000317.. method:: ST.totuple(line_info=False, col_info=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000318
Georg Brandl18244152009-09-02 20:34:52 +0000319 Same as ``st2tuple(st, line_info, col_info)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000320
321
Georg Brandl047e4862010-10-17 10:22:28 +0000322Example: Emulation of :func:`compile`
323-------------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000324
325While many useful operations may take place between parsing and bytecode
326generation, the simplest operation is to do nothing. For this purpose, using
327the :mod:`parser` module to produce an intermediate data structure is equivalent
328to the code ::
329
330 >>> code = compile('a + 5', 'file.py', 'eval')
331 >>> a = 5
332 >>> eval(code)
333 10
334
335The equivalent operation using the :mod:`parser` module is somewhat longer, and
Georg Brandl0c77a822008-06-10 16:37:50 +0000336allows the intermediate internal parse tree to be retained as an ST object::
Georg Brandl116aa622007-08-15 14:28:22 +0000337
338 >>> import parser
Georg Brandl0c77a822008-06-10 16:37:50 +0000339 >>> st = parser.expr('a + 5')
340 >>> code = st.compile('file.py')
Georg Brandl116aa622007-08-15 14:28:22 +0000341 >>> a = 5
342 >>> eval(code)
343 10
344
Georg Brandl0c77a822008-06-10 16:37:50 +0000345An application which needs both ST and code objects can package this code into
Georg Brandl116aa622007-08-15 14:28:22 +0000346readily available functions::
347
348 import parser
349
350 def load_suite(source_string):
Georg Brandl0c77a822008-06-10 16:37:50 +0000351 st = parser.suite(source_string)
352 return st, st.compile()
Georg Brandl116aa622007-08-15 14:28:22 +0000353
354 def load_expression(source_string):
Georg Brandl0c77a822008-06-10 16:37:50 +0000355 st = parser.expr(source_string)
356 return st, st.compile()