Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`parser` --- Access Python parse trees |
| 2 | =========================================== |
| 3 | |
| 4 | .. module:: parser |
| 5 | :synopsis: Access parse trees for Python source code. |
| 6 | .. moduleauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 7 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 8 | |
| 9 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 10 | .. 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | |
| 17 | .. index:: single: parsing; Python source code |
| 18 | |
| 19 | The :mod:`parser` module provides an interface to Python's internal parser and |
| 20 | byte-code compiler. The primary purpose for this interface is to allow Python |
| 21 | code to edit the parse tree of a Python expression and create executable code |
| 22 | from this. This is better than trying to parse and modify an arbitrary Python |
| 23 | code fragment as a string because parsing is performed in a manner identical to |
| 24 | the code forming the application. It is also faster. |
| 25 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 26 | .. note:: |
| 27 | |
| 28 | From Python 2.5 onward, it's much more convenient to cut in at the Abstract |
| 29 | Syntax Tree (AST) generation and compilation stage, using the :mod:`ast` |
| 30 | module. |
| 31 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | There are a few things to note about this module which are important to making |
| 33 | use of the data structures created. This is not a tutorial on editing the parse |
| 34 | trees for Python code, but some examples of using the :mod:`parser` module are |
| 35 | presented. |
| 36 | |
| 37 | Most importantly, a good understanding of the Python grammar processed by the |
| 38 | internal parser is required. For full information on the language syntax, refer |
| 39 | to :ref:`reference-index`. The parser |
| 40 | itself is created from a grammar specification defined in the file |
| 41 | :file:`Grammar/Grammar` in the standard Python distribution. The parse trees |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 42 | stored in the ST objects created by this module are the actual output from the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 | internal parser when created by the :func:`expr` or :func:`suite` functions, |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 44 | described below. The ST objects created by :func:`sequence2st` faithfully |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 45 | simulate those structures. Be aware that the values of the sequences which are |
| 46 | considered "correct" will vary from one version of Python to another as the |
| 47 | formal grammar for the language is revised. However, transporting code from one |
| 48 | Python version to another as source text will always allow correct parse trees |
| 49 | to be created in the target version, with the only restriction being that |
| 50 | migrating to an older version of the interpreter will not support more recent |
| 51 | language constructs. The parse trees are not typically compatible from one |
| 52 | version to another, whereas source code has always been forward-compatible. |
| 53 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 54 | Each element of the sequences returned by :func:`st2list` or :func:`st2tuple` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | has a simple form. Sequences representing non-terminal elements in the grammar |
| 56 | always have a length greater than one. The first element is an integer which |
| 57 | identifies a production in the grammar. These integers are given symbolic names |
| 58 | in the C header file :file:`Include/graminit.h` and the Python module |
| 59 | :mod:`symbol`. Each additional element of the sequence represents a component |
| 60 | of the production as recognized in the input string: these are always sequences |
| 61 | which have the same form as the parent. An important aspect of this structure |
| 62 | which should be noted is that keywords used to identify the parent node type, |
| 63 | such as the keyword :keyword:`if` in an :const:`if_stmt`, are included in the |
| 64 | node tree without any special treatment. For example, the :keyword:`if` keyword |
| 65 | is represented by the tuple ``(1, 'if')``, where ``1`` is the numeric value |
| 66 | associated with all :const:`NAME` tokens, including variable and function names |
| 67 | defined by the user. In an alternate form returned when line number information |
| 68 | is requested, the same token might be represented as ``(1, 'if', 12)``, where |
| 69 | the ``12`` represents the line number at which the terminal symbol was found. |
| 70 | |
| 71 | Terminal elements are represented in much the same way, but without any child |
| 72 | elements and the addition of the source text which was identified. The example |
| 73 | of the :keyword:`if` keyword above is representative. The various types of |
| 74 | terminal symbols are defined in the C header file :file:`Include/token.h` and |
| 75 | the Python module :mod:`token`. |
| 76 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 77 | The ST objects are not required to support the functionality of this module, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | but are provided for three purposes: to allow an application to amortize the |
| 79 | cost of processing complex parse trees, to provide a parse tree representation |
| 80 | which conserves memory space when compared to the Python list or tuple |
| 81 | representation, and to ease the creation of additional modules in C which |
| 82 | manipulate parse trees. A simple "wrapper" class may be created in Python to |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 83 | hide the use of ST objects. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 84 | |
| 85 | The :mod:`parser` module defines functions for a few distinct purposes. The |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 86 | most important purposes are to create ST objects and to convert ST objects to |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 | other representations such as parse trees and compiled code objects, but there |
| 88 | are also functions which serve to query the type of parse tree represented by an |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 89 | ST object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | .. seealso:: |
| 93 | |
| 94 | Module :mod:`symbol` |
| 95 | Useful constants representing internal nodes of the parse tree. |
| 96 | |
| 97 | Module :mod:`token` |
| 98 | Useful constants representing leaf nodes of the parse tree and functions for |
| 99 | testing node values. |
| 100 | |
| 101 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 102 | .. _creating-sts: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 103 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 104 | Creating ST Objects |
| 105 | ------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 106 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 107 | ST objects may be created from source code or from a parse tree. When creating |
| 108 | an ST object from source, different functions are used to create the ``'eval'`` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | and ``'exec'`` forms. |
| 110 | |
| 111 | |
| 112 | .. function:: expr(source) |
| 113 | |
| 114 | The :func:`expr` function parses the parameter *source* as if it were an input |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 115 | to ``compile(source, 'file.py', 'eval')``. If the parse succeeds, an ST object |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 116 | is created to hold the internal parse tree representation, otherwise an |
Georg Brandl | 7cb1319 | 2010-08-03 12:06:29 +0000 | [diff] [blame] | 117 | appropriate exception is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 118 | |
| 119 | |
| 120 | .. function:: suite(source) |
| 121 | |
| 122 | The :func:`suite` function parses the parameter *source* as if it were an input |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 123 | to ``compile(source, 'file.py', 'exec')``. If the parse succeeds, an ST object |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 | is created to hold the internal parse tree representation, otherwise an |
Georg Brandl | 7cb1319 | 2010-08-03 12:06:29 +0000 | [diff] [blame] | 125 | appropriate exception is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 126 | |
| 127 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 128 | .. function:: sequence2st(sequence) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 129 | |
| 130 | This function accepts a parse tree represented as a sequence and builds an |
| 131 | internal representation if possible. If it can validate that the tree conforms |
| 132 | to the Python grammar and all nodes are valid node types in the host version of |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 133 | Python, an ST object is created from the internal representation and returned |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 134 | to the called. If there is a problem creating the internal representation, or |
Georg Brandl | 7cb1319 | 2010-08-03 12:06:29 +0000 | [diff] [blame] | 135 | if the tree cannot be validated, a :exc:`ParserError` exception is raised. An |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 136 | ST object created this way should not be assumed to compile correctly; normal |
Georg Brandl | 7cb1319 | 2010-08-03 12:06:29 +0000 | [diff] [blame] | 137 | exceptions raised by compilation may still be initiated when the ST object is |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 138 | passed to :func:`compilest`. This may indicate problems not related to syntax |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 139 | (such as a :exc:`MemoryError` exception), but may also be due to constructs such |
| 140 | as the result of parsing ``del f(0)``, which escapes the Python parser but is |
| 141 | checked by the bytecode compiler. |
| 142 | |
| 143 | Sequences representing terminal tokens may be represented as either two-element |
| 144 | lists of the form ``(1, 'name')`` or as three-element lists of the form ``(1, |
| 145 | 'name', 56)``. If the third element is present, it is assumed to be a valid |
| 146 | line number. The line number may be specified for any subset of the terminal |
| 147 | symbols in the input tree. |
| 148 | |
| 149 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 150 | .. function:: tuple2st(sequence) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 151 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 152 | This is the same function as :func:`sequence2st`. This entry point is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 153 | maintained for backward compatibility. |
| 154 | |
| 155 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 156 | .. _converting-sts: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 157 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 158 | Converting ST Objects |
| 159 | --------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 160 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 161 | ST objects, regardless of the input used to create them, may be converted to |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | parse trees represented as list- or tuple- trees, or may be compiled into |
| 163 | executable code objects. Parse trees may be extracted with or without line |
| 164 | numbering information. |
| 165 | |
| 166 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 167 | .. function:: st2list(st, line_info=False, col_info=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 168 | |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 169 | This function accepts an ST object from the caller in *st* and returns a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 170 | Python list representing the equivalent parse tree. The resulting list |
| 171 | representation can be used for inspection or the creation of a new parse tree in |
| 172 | list form. This function does not fail so long as memory is available to build |
| 173 | the list representation. If the parse tree will only be used for inspection, |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 174 | :func:`st2tuple` should be used instead to reduce memory consumption and |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 175 | fragmentation. When the list representation is required, this function is |
| 176 | significantly faster than retrieving a tuple representation and converting that |
| 177 | to nested lists. |
| 178 | |
| 179 | If *line_info* is true, line number information will be included for all |
| 180 | terminal tokens as a third element of the list representing the token. Note |
| 181 | that the line number provided specifies the line on which the token *ends*. |
| 182 | This information is omitted if the flag is false or omitted. |
| 183 | |
| 184 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 185 | .. function:: st2tuple(st, line_info=False, col_info=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 186 | |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 187 | This function accepts an ST object from the caller in *st* and returns a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 188 | Python tuple representing the equivalent parse tree. Other than returning a |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 189 | tuple instead of a list, this function is identical to :func:`st2list`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 190 | |
| 191 | If *line_info* is true, line number information will be included for all |
| 192 | terminal tokens as a third element of the list representing the token. This |
| 193 | information is omitted if the flag is false or omitted. |
| 194 | |
| 195 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 196 | .. function:: compilest(st, filename='<syntax-tree>') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 197 | |
| 198 | .. index:: |
| 199 | builtin: exec |
| 200 | builtin: eval |
| 201 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 202 | The Python byte compiler can be invoked on an ST object to produce code objects |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 203 | which can be used as part of a call to the built-in :func:`exec` or :func:`eval` |
| 204 | functions. This function provides the interface to the compiler, passing the |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 205 | internal parse tree from *st* to the parser, using the source file name |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 206 | specified by the *filename* parameter. The default value supplied for *filename* |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 207 | indicates that the source was an ST object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 208 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 209 | Compiling an ST object may result in exceptions related to compilation; an |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 210 | example would be a :exc:`SyntaxError` caused by the parse tree for ``del f(0)``: |
| 211 | this statement is considered legal within the formal grammar for Python but is |
| 212 | not a legal language construct. The :exc:`SyntaxError` raised for this |
| 213 | condition is actually generated by the Python byte-compiler normally, which is |
| 214 | why it can be raised at this point by the :mod:`parser` module. Most causes of |
| 215 | compilation failure can be diagnosed programmatically by inspection of the parse |
| 216 | tree. |
| 217 | |
| 218 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 219 | .. _querying-sts: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 220 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 221 | Queries on ST Objects |
| 222 | --------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 224 | Two functions are provided which allow an application to determine if an ST was |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 225 | created as an expression or a suite. Neither of these functions can be used to |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 226 | determine if an ST was created from source code via :func:`expr` or |
| 227 | :func:`suite` or from a parse tree via :func:`sequence2st`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 228 | |
| 229 | |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 230 | .. function:: isexpr(st) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 231 | |
| 232 | .. index:: builtin: compile |
| 233 | |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 234 | When *st* represents an ``'eval'`` form, this function returns true, otherwise |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 235 | it returns false. This is useful, since code objects normally cannot be queried |
| 236 | for this information using existing built-in functions. Note that the code |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 237 | objects created by :func:`compilest` cannot be queried like this either, and |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 238 | are identical to those created by the built-in :func:`compile` function. |
| 239 | |
| 240 | |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 241 | .. function:: issuite(st) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 242 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 243 | This function mirrors :func:`isexpr` in that it reports whether an ST object |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 244 | represents an ``'exec'`` form, commonly known as a "suite." It is not safe to |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 245 | assume that this function is equivalent to ``not isexpr(st)``, as additional |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 246 | syntactic fragments may be supported in the future. |
| 247 | |
| 248 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 249 | .. _st-errors: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 250 | |
| 251 | Exceptions and Error Handling |
| 252 | ----------------------------- |
| 253 | |
| 254 | The parser module defines a single exception, but may also pass other built-in |
| 255 | exceptions from other portions of the Python runtime environment. See each |
| 256 | function for information about the exceptions it can raise. |
| 257 | |
| 258 | |
| 259 | .. exception:: ParserError |
| 260 | |
| 261 | Exception raised when a failure occurs within the parser module. This is |
Georg Brandl | 7cb1319 | 2010-08-03 12:06:29 +0000 | [diff] [blame] | 262 | generally produced for validation failures rather than the built-in |
| 263 | :exc:`SyntaxError` raised during normal parsing. The exception argument is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 264 | either a string describing the reason of the failure or a tuple containing a |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 265 | sequence causing the failure from a parse tree passed to :func:`sequence2st` |
| 266 | and an explanatory string. Calls to :func:`sequence2st` need to be able to |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 267 | handle either type of exception, while calls to other functions in the module |
| 268 | will only need to be aware of the simple string values. |
| 269 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 270 | Note that the functions :func:`compilest`, :func:`expr`, and :func:`suite` may |
Georg Brandl | 7cb1319 | 2010-08-03 12:06:29 +0000 | [diff] [blame] | 271 | raise exceptions which are normally thrown by the parsing and compilation |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 272 | process. These include the built in exceptions :exc:`MemoryError`, |
| 273 | :exc:`OverflowError`, :exc:`SyntaxError`, and :exc:`SystemError`. In these |
| 274 | cases, these exceptions carry all the meaning normally associated with them. |
| 275 | Refer to the descriptions of each function for detailed information. |
| 276 | |
| 277 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 278 | .. _st-objects: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 279 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 280 | ST Objects |
| 281 | ---------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 282 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 283 | Ordered and equality comparisons are supported between ST objects. Pickling of |
| 284 | ST objects (using the :mod:`pickle` module) is also supported. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 285 | |
| 286 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 287 | .. data:: STType |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 288 | |
| 289 | The type of the objects returned by :func:`expr`, :func:`suite` and |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 290 | :func:`sequence2st`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 291 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 292 | ST objects have the following methods: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 293 | |
| 294 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 295 | .. method:: ST.compile(filename='<syntax-tree>') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 296 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 297 | Same as ``compilest(st, filename)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 298 | |
| 299 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 300 | .. method:: ST.isexpr() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 301 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 302 | Same as ``isexpr(st)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 303 | |
| 304 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 305 | .. method:: ST.issuite() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 306 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 307 | Same as ``issuite(st)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 308 | |
| 309 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 310 | .. method:: ST.tolist(line_info=False, col_info=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 311 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 312 | Same as ``st2list(st, line_info, col_info)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 313 | |
| 314 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 315 | .. method:: ST.totuple(line_info=False, col_info=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 316 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 317 | Same as ``st2tuple(st, line_info, col_info)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 318 | |
| 319 | |
Georg Brandl | 047e486 | 2010-10-17 10:22:28 +0000 | [diff] [blame] | 320 | Example: Emulation of :func:`compile` |
| 321 | ------------------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 322 | |
| 323 | While many useful operations may take place between parsing and bytecode |
| 324 | generation, the simplest operation is to do nothing. For this purpose, using |
| 325 | the :mod:`parser` module to produce an intermediate data structure is equivalent |
| 326 | to the code :: |
| 327 | |
| 328 | >>> code = compile('a + 5', 'file.py', 'eval') |
| 329 | >>> a = 5 |
| 330 | >>> eval(code) |
| 331 | 10 |
| 332 | |
| 333 | The equivalent operation using the :mod:`parser` module is somewhat longer, and |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 334 | allows the intermediate internal parse tree to be retained as an ST object:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 335 | |
| 336 | >>> import parser |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 337 | >>> st = parser.expr('a + 5') |
| 338 | >>> code = st.compile('file.py') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 339 | >>> a = 5 |
| 340 | >>> eval(code) |
| 341 | 10 |
| 342 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 343 | An application which needs both ST and code objects can package this code into |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 344 | readily available functions:: |
| 345 | |
| 346 | import parser |
| 347 | |
| 348 | def load_suite(source_string): |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 349 | st = parser.suite(source_string) |
| 350 | return st, st.compile() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 351 | |
| 352 | def load_expression(source_string): |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 353 | st = parser.expr(source_string) |
| 354 | return st, st.compile() |