blob: a4443350ef1cdc56d4c9db1cf899110690c3a8f5 [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040026 *
27 * To debug parser errors like
28 * "parser.ParserError: Expected node type 12, got 333."
29 * decode symbol numbers using the automatically-generated files
30 * Lib/symbol.h and Include/token.h.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000031 */
32
Fred Drakeff9ea482000-04-19 13:54:15 +000033#include "Python.h" /* general Python API */
Benjamin Petersonf216c942008-10-31 02:28:05 +000034#include "Python-ast.h" /* mod_ty */
Fred Drakeff9ea482000-04-19 13:54:15 +000035#include "graminit.h" /* symbols defined in the grammar */
36#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000037#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000038#include "token.h" /* token definitions */
Benjamin Petersonf216c942008-10-31 02:28:05 +000039#include "grammar.h"
40#include "parsetok.h"
Fred Drakeff9ea482000-04-19 13:54:15 +000041 /* ISTERMINAL() / ISNONTERMINAL() */
Benjamin Petersonf216c942008-10-31 02:28:05 +000042#undef Yield
43#include "ast.h"
Benjamin Petersonf216c942008-10-31 02:28:05 +000044
45extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000046
Fred Drake268397f1998-04-29 14:16:32 +000047#ifdef lint
48#include <note.h>
49#else
50#define NOTE(x)
51#endif
52
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000053/* String constants used to initialize module attributes.
54 *
55 */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020056static const char parser_copyright_string[] =
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000057"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000058University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
59Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
60Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000061
62
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000063PyDoc_STRVAR(parser_doc_string,
64"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000065
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020066static const char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000067
68
Martin v. Löwis18e16552006-02-15 17:27:45 +000069typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000070typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000071 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000072 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000073
Thomas Wouters7e474022000-07-16 12:04:32 +000074/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000075 * original copyright statement is included below, and continues to apply
76 * in full to the function immediately following. All other material is
77 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
78 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000079 * new naming conventions. Added arguments to provide support for creating
80 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000081 */
82
Guido van Rossum52f2c051993-11-10 12:53:24 +000083
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000084static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000085node2tuple(node *n, /* node to convert */
86 SeqMaker mkseq, /* create sequence */
87 SeqInserter addelem, /* func. to add elem. in seq. */
Thomas Wouters89f507f2006-12-13 04:49:30 +000088 int lineno, /* include line numbers? */
89 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000090{
Victor Stinnerdf4572c2013-07-12 01:35:10 +020091 PyObject *result = NULL, *w;
92
Guido van Rossum3d602e31996-07-21 02:33:56 +000093 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000094 Py_INCREF(Py_None);
Victor Stinnerdf4572c2013-07-12 01:35:10 +020095 return Py_None;
Guido van Rossum3d602e31996-07-21 02:33:56 +000096 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +020097
Guido van Rossum3d602e31996-07-21 02:33:56 +000098 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000099 int i;
Fred Drake268397f1998-04-29 14:16:32 +0000100
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200101 result = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
102 if (result == NULL)
103 goto error;
104
Christian Heimes217cfd12007-12-02 14:31:20 +0000105 w = PyLong_FromLong(TYPE(n));
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200106 if (w == NULL)
107 goto error;
108 (void) addelem(result, 0, w);
109
Fred Drakeff9ea482000-04-19 13:54:15 +0000110 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000111 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200112 if (w == NULL)
113 goto error;
114 (void) addelem(result, i+1, w);
Fred Drakeff9ea482000-04-19 13:54:15 +0000115 }
Tim Peters6a627252003-07-21 14:25:23 +0000116
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200117 if (TYPE(n) == encoding_decl) {
118 w = PyUnicode_FromString(STR(n));
119 if (w == NULL)
120 goto error;
121 (void) addelem(result, i+1, w);
122 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000123 }
124 else if (ISTERMINAL(TYPE(n))) {
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200125 result = mkseq(2 + lineno + col_offset);
126 if (result == NULL)
127 goto error;
128
129 w = PyLong_FromLong(TYPE(n));
130 if (w == NULL)
131 goto error;
132 (void) addelem(result, 0, w);
133
134 w = PyUnicode_FromString(STR(n));
135 if (w == NULL)
136 goto error;
137 (void) addelem(result, 1, w);
138
139 if (lineno == 1) {
140 w = PyLong_FromLong(n->n_lineno);
141 if (w == NULL)
142 goto error;
143 (void) addelem(result, 2, w);
Fred Drakeff9ea482000-04-19 13:54:15 +0000144 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200145
146 if (col_offset == 1) {
147 w = PyLong_FromLong(n->n_col_offset);
148 if (w == NULL)
149 goto error;
150 (void) addelem(result, 3, w);
151 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000152 }
153 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000154 PyErr_SetString(PyExc_SystemError,
155 "unrecognized parse tree node type");
156 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000157 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200158 return result;
159
160error:
161 Py_XDECREF(result);
162 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000163}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000164/*
165 * End of material copyrighted by Stichting Mathematisch Centrum.
166 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000167
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000168
169
170/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000171 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000172 * field of the object type to identify which any given object represents.
173 * These should probably go in an external header to allow other extensions
174 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000175 */
176
Fred Drakec2683dd2001-07-17 19:32:05 +0000177#define PyST_EXPR 1
178#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000179
180
181/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000182 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000183 * naming style, but the code uses the new naming convention.
184 */
185
186static PyObject*
187parser_error = 0;
188
189
Fred Drakec2683dd2001-07-17 19:32:05 +0000190typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000191 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000192 node* st_node; /* the node* returned by the parser */
193 int st_type; /* EXPR or SUITE ? */
Benjamin Petersonf216c942008-10-31 02:28:05 +0000194 PyCompilerFlags st_flags; /* Parser and compiler flags */
Fred Drakec2683dd2001-07-17 19:32:05 +0000195} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000196
197
Jeremy Hylton938ace62002-07-17 16:30:39 +0000198static void parser_free(PyST_Object *st);
Jesus Ceae9c53182012-08-03 14:28:37 +0200199static PyObject* parser_sizeof(PyST_Object *, void *);
Mark Dickinson211c6252009-02-01 10:28:51 +0000200static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op);
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000201static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
202static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
203static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
204static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
205static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
Fred Drake503d8d61998-04-13 18:45:18 +0000206
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000207#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
208
209static PyMethodDef parser_methods[] = {
210 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
211 PyDoc_STR("Compile this ST object into a code object.")},
212 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
213 PyDoc_STR("Determines if this ST object was created from an expression.")},
214 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
215 PyDoc_STR("Determines if this ST object was created from a suite.")},
216 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
217 PyDoc_STR("Creates a list-tree representation of this ST.")},
218 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
219 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Jesus Ceae9c53182012-08-03 14:28:37 +0200220 {"__sizeof__", (PyCFunction)parser_sizeof, METH_NOARGS,
221 PyDoc_STR("Returns size in memory, in bytes.")},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000222 {NULL, NULL, 0, NULL}
223};
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000224
Fred Drake268397f1998-04-29 14:16:32 +0000225static
Fred Drakec2683dd2001-07-17 19:32:05 +0000226PyTypeObject PyST_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000227 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000228 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000229 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000230 0, /* tp_itemsize */
231 (destructor)parser_free, /* tp_dealloc */
232 0, /* tp_print */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000233 0, /* tp_getattr */
Fred Drakeff9ea482000-04-19 13:54:15 +0000234 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000235 0, /* tp_reserved */
Fred Drakeff9ea482000-04-19 13:54:15 +0000236 0, /* tp_repr */
237 0, /* tp_as_number */
238 0, /* tp_as_sequence */
239 0, /* tp_as_mapping */
240 0, /* tp_hash */
241 0, /* tp_call */
242 0, /* tp_str */
243 0, /* tp_getattro */
244 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000245
246 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000247 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000248
Fred Drakeff9ea482000-04-19 13:54:15 +0000249 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000250
251 /* __doc__ */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000252 "Intermediate representation of a Python parse tree.",
253 0, /* tp_traverse */
254 0, /* tp_clear */
Mark Dickinson211c6252009-02-01 10:28:51 +0000255 parser_richcompare, /* tp_richcompare */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000256 0, /* tp_weaklistoffset */
257 0, /* tp_iter */
258 0, /* tp_iternext */
259 parser_methods, /* tp_methods */
Fred Drakec2683dd2001-07-17 19:32:05 +0000260}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000261
262
Mark Dickinson211c6252009-02-01 10:28:51 +0000263/* PyST_Type isn't subclassable, so just check ob_type */
264#define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type)
265
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000266static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000267parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000268{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000269 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000270
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000271 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000272 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000273
274 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000275 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000276
277 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000278 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000279
280 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000281 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000282
283 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000284 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000285
286 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000287 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000288
Fred Drakeff9ea482000-04-19 13:54:15 +0000289 if (v != 0)
290 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000291 }
292 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000293}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000294
Mark Dickinson211c6252009-02-01 10:28:51 +0000295/* parser_richcompare(PyObject* left, PyObject* right, int op)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000296 *
297 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
298 * This really just wraps a call to parser_compare_nodes() with some easy
299 * checks and protection code.
300 *
301 */
Mark Dickinson211c6252009-02-01 10:28:51 +0000302
303#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
304
305static PyObject *
306parser_richcompare(PyObject *left, PyObject *right, int op)
Guido van Rossum47478871996-08-21 14:32:37 +0000307{
Mark Dickinson211c6252009-02-01 10:28:51 +0000308 int result;
309 PyObject *v;
310
311 /* neither argument should be NULL, unless something's gone wrong */
312 if (left == NULL || right == NULL) {
313 PyErr_BadInternalCall();
314 return NULL;
315 }
316
317 /* both arguments should be instances of PyST_Object */
318 if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) {
319 v = Py_NotImplemented;
320 goto finished;
321 }
322
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000323 if (left == right)
Mark Dickinson211c6252009-02-01 10:28:51 +0000324 /* if arguments are identical, they're equal */
325 result = 0;
326 else
327 result = parser_compare_nodes(((PyST_Object *)left)->st_node,
328 ((PyST_Object *)right)->st_node);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000329
Mark Dickinson211c6252009-02-01 10:28:51 +0000330 /* Convert return value to a Boolean */
331 switch (op) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000332 case Py_EQ:
Mark Dickinson211c6252009-02-01 10:28:51 +0000333 v = TEST_COND(result == 0);
334 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000335 case Py_NE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000336 v = TEST_COND(result != 0);
337 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000338 case Py_LE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000339 v = TEST_COND(result <= 0);
340 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000341 case Py_GE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000342 v = TEST_COND(result >= 0);
343 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000344 case Py_LT:
Mark Dickinson211c6252009-02-01 10:28:51 +0000345 v = TEST_COND(result < 0);
346 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000347 case Py_GT:
Mark Dickinson211c6252009-02-01 10:28:51 +0000348 v = TEST_COND(result > 0);
349 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000350 default:
Mark Dickinson211c6252009-02-01 10:28:51 +0000351 PyErr_BadArgument();
352 return NULL;
353 }
354 finished:
355 Py_INCREF(v);
356 return v;
Fred Drakeff9ea482000-04-19 13:54:15 +0000357}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000358
Fred Drakec2683dd2001-07-17 19:32:05 +0000359/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000360 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000361 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000362 * 'wrapper' object that holds a node* and allows it to be passed around in
363 * Python code.
364 *
365 */
366static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000367parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000368{
Fred Drakec2683dd2001-07-17 19:32:05 +0000369 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000370
371 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000372 o->st_node = st;
373 o->st_type = type;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000374 o->st_flags.cf_flags = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000375 }
Fred Drake268397f1998-04-29 14:16:32 +0000376 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000377 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000378 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000379 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000380}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000381
382
Fred Drakec2683dd2001-07-17 19:32:05 +0000383/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000384 *
385 * This is called by a del statement that reduces the reference count to 0.
386 *
387 */
388static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000389parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000390{
Fred Drakec2683dd2001-07-17 19:32:05 +0000391 PyNode_Free(st->st_node);
392 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000393}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000394
Jesus Ceae9c53182012-08-03 14:28:37 +0200395static PyObject *
396parser_sizeof(PyST_Object *st, void *unused)
397{
398 Py_ssize_t res;
399
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +0200400 res = _PyObject_SIZE(Py_TYPE(st)) + _PyNode_SizeOf(st->st_node);
Jesus Ceae9c53182012-08-03 14:28:37 +0200401 return PyLong_FromSsize_t(res);
402}
403
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000404
Fred Drakec2683dd2001-07-17 19:32:05 +0000405/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000406 *
407 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000408 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000409 *
410 */
411static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000412parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000413{
Antoine Pitrou721738f2012-08-15 23:20:39 +0200414 int line_info = 0;
415 int col_info = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000416 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000417 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000418
Georg Brandl30704ea02008-07-23 15:07:12 +0000419 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000420
Martin v. Löwis1a214512008-06-11 05:26:20 +0000421 if (self == NULL || PyModule_Check(self)) {
Antoine Pitrou721738f2012-08-15 23:20:39 +0200422 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2tuple", keywords,
423 &PyST_Type, &self, &line_info,
424 &col_info);
Fred Drake268397f1998-04-29 14:16:32 +0000425 }
Fred Drake503d8d61998-04-13 18:45:18 +0000426 else
Antoine Pitrou721738f2012-08-15 23:20:39 +0200427 ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:totuple", &keywords[1],
428 &line_info, &col_info);
Fred Drake268397f1998-04-29 14:16:32 +0000429 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000430 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000431 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000432 * since it's known to work already.
433 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000434 res = node2tuple(((PyST_Object*)self)->st_node,
Antoine Pitrou721738f2012-08-15 23:20:39 +0200435 PyTuple_New, PyTuple_SetItem, line_info, col_info);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000436 }
437 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000438}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000439
440
Fred Drakec2683dd2001-07-17 19:32:05 +0000441/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000442 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000443 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000444 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000445 *
446 */
447static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000448parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000449{
Antoine Pitrou721738f2012-08-15 23:20:39 +0200450 int line_info = 0;
451 int col_info = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000452 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000453 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000454
Georg Brandl30704ea02008-07-23 15:07:12 +0000455 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000456
Martin v. Löwis1a214512008-06-11 05:26:20 +0000457 if (self == NULL || PyModule_Check(self))
Antoine Pitrou721738f2012-08-15 23:20:39 +0200458 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2list", keywords,
459 &PyST_Type, &self, &line_info,
460 &col_info);
Fred Drake503d8d61998-04-13 18:45:18 +0000461 else
Antoine Pitrou721738f2012-08-15 23:20:39 +0200462 ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:tolist", &keywords[1],
463 &line_info, &col_info);
Fred Drake503d8d61998-04-13 18:45:18 +0000464 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000465 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000466 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000467 * since it's known to work already.
468 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000469 res = node2tuple(self->st_node,
Antoine Pitrou721738f2012-08-15 23:20:39 +0200470 PyList_New, PyList_SetItem, line_info, col_info);
Guido van Rossum47478871996-08-21 14:32:37 +0000471 }
472 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000473}
Guido van Rossum47478871996-08-21 14:32:37 +0000474
475
Fred Drakec2683dd2001-07-17 19:32:05 +0000476/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000477 *
478 * This function creates code objects from the parse tree represented by
479 * the passed-in data object. An optional file name is passed in as well.
480 *
481 */
482static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000483parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000484{
Victor Stinner14e461d2013-08-26 22:28:21 +0200485 PyObject* res = NULL;
486 PyArena* arena = NULL;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000487 mod_ty mod;
Victor Stinner14e461d2013-08-26 22:28:21 +0200488 PyObject* filename = NULL;
Fred Drake503d8d61998-04-13 18:45:18 +0000489 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000490
Georg Brandl30704ea02008-07-23 15:07:12 +0000491 static char *keywords[] = {"st", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000492
Martin v. Löwis1a214512008-06-11 05:26:20 +0000493 if (self == NULL || PyModule_Check(self))
Victor Stinner14e461d2013-08-26 22:28:21 +0200494 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O&:compilest", keywords,
495 &PyST_Type, &self,
496 PyUnicode_FSDecoder, &filename);
Fred Drake503d8d61998-04-13 18:45:18 +0000497 else
Victor Stinner14e461d2013-08-26 22:28:21 +0200498 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O&:compile", &keywords[1],
499 PyUnicode_FSDecoder, &filename);
500 if (!ok)
501 goto error;
Fred Drake503d8d61998-04-13 18:45:18 +0000502
Victor Stinner14e461d2013-08-26 22:28:21 +0200503 if (filename == NULL) {
504 filename = PyUnicode_FromString("<syntax-tree>");
505 if (filename == NULL)
506 goto error;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000507 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000508
Victor Stinner14e461d2013-08-26 22:28:21 +0200509 arena = PyArena_New();
510 if (!arena)
511 goto error;
512
513 mod = PyAST_FromNodeObject(self->st_node, &self->st_flags,
514 filename, arena);
515 if (!mod)
516 goto error;
517
518 res = (PyObject *)PyAST_CompileObject(mod, filename,
519 &self->st_flags, -1, arena);
520error:
521 Py_XDECREF(filename);
522 if (arena != NULL)
523 PyArena_Free(arena);
524 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +0000525}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000526
527
528/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
529 * PyObject* parser_issuite(PyObject* self, PyObject* args)
530 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000531 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000532 * a statement suite, respectively. The return is a Python truth value.
533 *
534 */
535static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000536parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000537{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000538 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000539 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000540
Georg Brandl30704ea02008-07-23 15:07:12 +0000541 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000542
Martin v. Löwis1a214512008-06-11 05:26:20 +0000543 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000544 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000545 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000546 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000547 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000548
549 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000550 /* Check to see if the ST represents an expression or not. */
551 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000552 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000553 }
554 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000555}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000556
557
558static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000559parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000560{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000561 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000562 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000563
Georg Brandl30704ea02008-07-23 15:07:12 +0000564 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000565
Martin v. Löwis1a214512008-06-11 05:26:20 +0000566 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000567 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000568 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000569 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000570 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000571
572 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000573 /* Check to see if the ST represents an expression or not. */
574 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000575 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000576 }
577 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000578}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000579
580
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200581/* err_string(const char* message)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000582 *
583 * Sets the error string for an exception of type ParserError.
584 *
585 */
586static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200587err_string(const char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000588{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000589 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000590}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000591
592
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000593/* PyObject* parser_do_parse(PyObject* args, int type)
594 *
595 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000596 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000597 *
598 */
599static PyObject*
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200600parser_do_parse(PyObject *args, PyObject *kw, const char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000601{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000602 char* string = 0;
603 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000604 int flags = 0;
605 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000606
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000607 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000608
609 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000610 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
611 &_PyParser_Grammar,
612 (type == PyST_EXPR)
613 ? eval_input : file_input,
614 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 if (n) {
617 res = parser_newstobject(n, type);
Benjamin Petersonf216c942008-10-31 02:28:05 +0000618 if (res)
619 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
620 }
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500621 else {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000622 PyParser_SetError(&err);
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500623 }
Benjamin Petersonf0cdbad2011-06-05 22:14:05 -0500624 PyParser_ClearError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000625 }
626 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000627}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000628
629
630/* PyObject* parser_expr(PyObject* self, PyObject* args)
631 * PyObject* parser_suite(PyObject* self, PyObject* args)
632 *
633 * External interfaces to the parser itself. Which is called determines if
634 * the parser attempts to recognize an expression ('eval' form) or statement
635 * suite ('exec' form). The real work is done by parser_do_parse() above.
636 *
637 */
638static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000639parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000640{
Fred Drake268397f1998-04-29 14:16:32 +0000641 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000642 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000643}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000644
645
646static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000647parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000648{
Fred Drake268397f1998-04-29 14:16:32 +0000649 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000650 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000651}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000652
653
654
Fred Drakec2683dd2001-07-17 19:32:05 +0000655/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000656 * object requires that the input tuple be valid without having to rely on
657 * catching an exception from the compiler. This is done to allow the
658 * compiler itself to remain fast, since most of its input will come from
659 * the parser directly, and therefore be known to be syntactically correct.
660 * This validation is done to ensure that we don't core dump the compile
661 * phase, returning an exception instead.
662 *
663 * Two aspects can be broken out in this code: creating a node tree from
664 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000665 * advantageous to expand the number of ST types to include funcdefs and
666 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000667 * here. They are not necessary, and not quite as useful in a raw form.
668 * For now, let's get expressions and suites working reliably.
669 */
670
671
Jeremy Hylton938ace62002-07-17 16:30:39 +0000672static node* build_node_tree(PyObject *tuple);
Benjamin Peterson53595c42016-06-02 11:30:18 -0700673
674static int
675validate_node(node *tree)
676{
677 int type = TYPE(tree);
678 int nch = NCH(tree);
679 dfa *nt_dfa;
680 state *dfa_state;
681 int pos, arc;
682
683 assert(ISNONTERMINAL(type));
684 type -= NT_OFFSET;
685 if (type >= _PyParser_Grammar.g_ndfas) {
686 PyErr_Format(parser_error, "Unrecognized node type %d.", TYPE(tree));
687 return 0;
688 }
689 nt_dfa = &_PyParser_Grammar.g_dfa[type];
690 REQ(tree, nt_dfa->d_type);
691
692 /* Run the DFA for this nonterminal. */
693 dfa_state = &nt_dfa->d_state[nt_dfa->d_initial];
694 for (pos = 0; pos < nch; ++pos) {
695 node *ch = CHILD(tree, pos);
696 int ch_type = TYPE(ch);
697 for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
698 short a_label = dfa_state->s_arc[arc].a_lbl;
699 assert(a_label < _PyParser_Grammar.g_ll.ll_nlabels);
700 if (_PyParser_Grammar.g_ll.ll_label[a_label].lb_type == ch_type) {
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300701 /* The child is acceptable; if non-terminal, validate it recursively. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700702 if (ISNONTERMINAL(ch_type) && !validate_node(ch))
703 return 0;
704
705 /* Update the state, and move on to the next child. */
706 dfa_state = &nt_dfa->d_state[dfa_state->s_arc[arc].a_arrow];
707 goto arc_found;
708 }
709 }
710 /* What would this state have accepted? */
711 {
712 short a_label = dfa_state->s_arc->a_lbl;
713 int next_type;
714 if (!a_label) /* Wouldn't accept any more children */
715 goto illegal_num_children;
716
717 next_type = _PyParser_Grammar.g_ll.ll_label[a_label].lb_type;
718 if (ISNONTERMINAL(next_type))
719 PyErr_Format(parser_error, "Expected node type %d, got %d.",
720 next_type, ch_type);
721 else
722 PyErr_Format(parser_error, "Illegal terminal: expected %s.",
723 _PyParser_TokenNames[next_type]);
724 return 0;
725 }
726
727arc_found:
728 continue;
729 }
730 /* Are we in a final state? If so, return 1 for successful validation. */
731 for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
732 if (!dfa_state->s_arc[arc].a_lbl) {
733 return 1;
734 }
735 }
736
737illegal_num_children:
738 PyErr_Format(parser_error,
739 "Illegal number of children for %s node.", nt_dfa->d_name);
740 return 0;
741}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000742
Fred Drakec2683dd2001-07-17 19:32:05 +0000743/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000744 *
745 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000746 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000747 * tuple can be validated. It does this by checking the first code of the
748 * tuple, and, if acceptable, builds the internal representation. If this
749 * step succeeds, the internal representation is validated as fully as
Benjamin Peterson53595c42016-06-02 11:30:18 -0700750 * possible with the recursive validate_node() routine defined above.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000751 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000752 * This function must be changed if support is to be added for PyST_FRAGMENT
753 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000754 *
755 */
756static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000757parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000758{
Fred Drake268397f1998-04-29 14:16:32 +0000759 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000760 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000761 PyObject *tuple;
762 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000763
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000764 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000765
Fred Drakec2683dd2001-07-17 19:32:05 +0000766 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000767 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000768 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000769 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000770 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000771 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000772 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000773 }
774 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000775 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000776 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000777 tree = build_node_tree(tuple);
778 if (tree != 0) {
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300779 node *validation_root = NULL;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700780 int tree_type = 0;
781 switch (TYPE(tree)) {
782 case eval_input:
Fred Drake0ac9b072000-09-12 21:58:06 +0000783 /* Might be an eval form. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700784 tree_type = PyST_EXPR;
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300785 validation_root = tree;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700786 break;
787 case encoding_decl:
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000788 /* This looks like an encoding_decl so far. */
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300789 if (NCH(tree) == 1) {
790 tree_type = PyST_SUITE;
791 validation_root = CHILD(tree, 0);
792 }
793 else {
Benjamin Peterson53595c42016-06-02 11:30:18 -0700794 err_string("Error Parsing encoding_decl");
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300795 }
796 break;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700797 case file_input:
798 /* This looks like an exec form so far. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700799 tree_type = PyST_SUITE;
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300800 validation_root = tree;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700801 break;
802 default:
Fred Drake0ac9b072000-09-12 21:58:06 +0000803 /* This is a fragment, at best. */
Fred Drake661ea262000-10-24 19:57:45 +0000804 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000805 }
Benjamin Peterson53595c42016-06-02 11:30:18 -0700806
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300807 if (validation_root != NULL && validate_node(validation_root))
Benjamin Peterson53595c42016-06-02 11:30:18 -0700808 st = parser_newstobject(tree, tree_type);
809 else
810 PyNode_Free(tree);
Guido van Rossum47478871996-08-21 14:32:37 +0000811 }
Andrew Svetlov737fb892012-12-18 21:14:22 +0200812 /* Make sure we raise an exception on all errors. We should never
Guido van Rossum47478871996-08-21 14:32:37 +0000813 * get this, but we'd do well to be sure something is done.
814 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000815 if (st == NULL && !PyErr_Occurred())
816 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000817
Fred Drakec2683dd2001-07-17 19:32:05 +0000818 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000819}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000820
821
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000822/* node* build_node_children()
823 *
824 * Iterate across the children of the current non-terminal node and build
825 * their structures. If successful, return the root of this portion of
826 * the tree, otherwise, 0. Any required exception will be specified already,
827 * and no memory will have been deallocated.
828 *
829 */
830static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000831build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000832{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000833 Py_ssize_t len = PyObject_Size(tuple);
834 Py_ssize_t i;
835 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000836
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300837 if (len < 0) {
838 return NULL;
839 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000840 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000841 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000842 PyObject* elem = PySequence_GetItem(tuple, i);
843 int ok = elem != NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200844 int type = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000845 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000846
Fred Drakeff9ea482000-04-19 13:54:15 +0000847 if (ok)
848 ok = PySequence_Check(elem);
849 if (ok) {
850 PyObject *temp = PySequence_GetItem(elem, 0);
851 if (temp == NULL)
852 ok = 0;
853 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000854 ok = PyLong_Check(temp);
Serhiy Storchaka78980432013-01-15 01:12:17 +0200855 if (ok) {
856 type = _PyLong_AsInt(temp);
857 if (type == -1 && PyErr_Occurred()) {
858 Py_DECREF(temp);
859 Py_DECREF(elem);
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300860 return NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200861 }
862 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000863 Py_DECREF(temp);
864 }
865 }
866 if (!ok) {
Victor Stinner5f8d4852014-01-02 11:49:27 +0100867 PyObject *err = Py_BuildValue("Os", elem,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000868 "Illegal node construct.");
869 PyErr_SetObject(parser_error, err);
870 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000871 Py_XDECREF(elem);
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300872 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000873 }
874 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000875 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000876 PyObject *temp;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000877 const char *temp_str;
Guido van Rossum47478871996-08-21 14:32:37 +0000878
Fred Drake0ac9b072000-09-12 21:58:06 +0000879 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000880 err_string("terminal nodes must have 2 or 3 entries");
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300881 Py_DECREF(elem);
882 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000883 }
884 temp = PySequence_GetItem(elem, 1);
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300885 if (temp == NULL) {
886 Py_DECREF(elem);
887 return NULL;
888 }
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000889 if (!PyUnicode_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000890 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000891 "second item in terminal node must be a string,"
892 " found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000893 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000894 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000895 Py_DECREF(elem);
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300896 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000897 }
898 if (len == 3) {
899 PyObject *o = PySequence_GetItem(elem, 2);
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300900 if (o == NULL) {
901 Py_DECREF(temp);
902 Py_DECREF(elem);
903 return NULL;
904 }
905 if (PyLong_Check(o)) {
906 int num = _PyLong_AsInt(o);
907 if (num == -1 && PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000908 Py_DECREF(o);
909 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000910 Py_DECREF(elem);
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300911 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000912 }
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300913 *line_num = num;
Fred Drakeff9ea482000-04-19 13:54:15 +0000914 }
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300915 else {
916 PyErr_Format(parser_error,
917 "third item in terminal node must be an"
918 " integer, found %s",
919 Py_TYPE(temp)->tp_name);
920 Py_DECREF(o);
921 Py_DECREF(temp);
922 Py_DECREF(elem);
923 return NULL;
924 }
925 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000926 }
Serhiy Storchaka06515832016-11-20 09:13:07 +0200927 temp_str = PyUnicode_AsUTF8AndSize(temp, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000928 if (temp_str == NULL) {
929 Py_DECREF(temp);
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300930 Py_DECREF(elem);
931 return NULL;
Alexander Belopolskye239d232010-12-08 23:31:48 +0000932 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000933 strn = (char *)PyObject_MALLOC(len + 1);
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200934 if (strn == NULL) {
935 Py_DECREF(temp);
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300936 Py_DECREF(elem);
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200937 PyErr_NoMemory();
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300938 return NULL;
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200939 }
940 (void) memcpy(strn, temp_str, len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000941 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000942 }
943 else if (!ISNONTERMINAL(type)) {
944 /*
945 * It has to be one or the other; this is an error.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200946 * Raise an exception.
Fred Drakeff9ea482000-04-19 13:54:15 +0000947 */
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300948 PyObject *err = Py_BuildValue("Os", elem, "unknown node type.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000949 PyErr_SetObject(parser_error, err);
950 Py_XDECREF(err);
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300951 Py_DECREF(elem);
952 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000953 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000954 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000955 if (err == E_NOMEM) {
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300956 Py_DECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000957 PyObject_FREE(strn);
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300958 PyErr_NoMemory();
959 return NULL;
Fred Drake8b55b2d2001-12-05 22:10:44 +0000960 }
961 if (err == E_OVERFLOW) {
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300962 Py_DECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000963 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000964 PyErr_SetString(PyExc_ValueError,
965 "unsupported number of child nodes");
966 return NULL;
967 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000968
Fred Drakeff9ea482000-04-19 13:54:15 +0000969 if (ISNONTERMINAL(type)) {
970 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000971
Fred Drakeff9ea482000-04-19 13:54:15 +0000972 if (new_child != build_node_children(elem, new_child, line_num)) {
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300973 Py_DECREF(elem);
974 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000975 }
976 }
977 else if (type == NEWLINE) { /* It's true: we increment the */
978 ++(*line_num); /* line number *after* the newline! */
979 }
Serhiy Storchaka39dedb62017-04-19 23:22:19 +0300980 Py_DECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000981 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000982 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000983}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000984
985
986static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000987build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000988{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000989 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000990 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000991 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000992
Guido van Rossum47478871996-08-21 14:32:37 +0000993 if (temp != NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000994 num = PyLong_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000995 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000996 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000997 /*
998 * The tuple is simple, but it doesn't start with a start symbol.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200999 * Raise an exception now and be done with it.
Fred Drakeff9ea482000-04-19 13:54:15 +00001000 */
Victor Stinner6684bdf2013-07-17 00:13:52 +02001001 tuple = Py_BuildValue("Os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +00001002 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +00001003 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001004 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001005 }
1006 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001007 /*
1008 * Not efficient, but that can be handled later.
1009 */
1010 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001011 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001012
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001013 if (num == encoding_decl) {
1014 encoding = PySequence_GetItem(tuple, 2);
Serhiy Storchaka39dedb62017-04-19 23:22:19 +03001015 if (encoding == NULL) {
1016 PyErr_SetString(parser_error, "missed encoding");
1017 return NULL;
1018 }
1019 if (!PyUnicode_Check(encoding)) {
1020 PyErr_Format(parser_error,
1021 "encoding must be a string, found %.200s",
1022 Py_TYPE(encoding)->tp_name);
1023 Py_DECREF(encoding);
1024 return NULL;
1025 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001026 /* tuple isn't borrowed anymore here, need to DECREF */
1027 tuple = PySequence_GetSlice(tuple, 0, 2);
Serhiy Storchaka39dedb62017-04-19 23:22:19 +03001028 if (tuple == NULL) {
1029 Py_DECREF(encoding);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001030 return NULL;
Serhiy Storchaka39dedb62017-04-19 23:22:19 +03001031 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001032 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001033 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +00001034 if (res != NULL) {
1035 if (res != build_node_children(tuple, res, &line_num)) {
1036 PyNode_Free(res);
1037 res = NULL;
1038 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001039 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +00001040 Py_ssize_t len;
Neal Norwitz3fcbea52007-08-26 04:51:28 +00001041 const char *temp;
Serhiy Storchaka06515832016-11-20 09:13:07 +02001042 temp = PyUnicode_AsUTF8AndSize(encoding, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001043 if (temp == NULL) {
Serhiy Storchaka39dedb62017-04-19 23:22:19 +03001044 PyNode_Free(res);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001045 Py_DECREF(encoding);
1046 Py_DECREF(tuple);
1047 return NULL;
1048 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00001049 res->n_str = (char *)PyObject_MALLOC(len + 1);
Victor Stinner3bd6abd2013-07-12 01:33:59 +02001050 if (res->n_str == NULL) {
Serhiy Storchaka39dedb62017-04-19 23:22:19 +03001051 PyNode_Free(res);
Victor Stinner3bd6abd2013-07-12 01:33:59 +02001052 Py_DECREF(encoding);
1053 Py_DECREF(tuple);
1054 PyErr_NoMemory();
1055 return NULL;
1056 }
1057 (void) memcpy(res->n_str, temp, len + 1);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001058 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001059 }
Serhiy Storchaka39dedb62017-04-19 23:22:19 +03001060 if (encoding != NULL) {
1061 Py_DECREF(encoding);
1062 Py_DECREF(tuple);
1063 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001064 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001065 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001066 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +00001067 * NONTERMINAL, we can't use it. Not sure the implementation
1068 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +00001069 */
Serhiy Storchaka39dedb62017-04-19 23:22:19 +03001070 PyObject *err = Py_BuildValue("Os", tuple,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001071 "Illegal component tuple.");
1072 PyErr_SetObject(parser_error, err);
1073 Py_XDECREF(err);
1074 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001075
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001076 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001077}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001078
1079
Fred Drake43f8f9b1998-04-13 16:25:46 +00001080static PyObject*
1081pickle_constructor = NULL;
1082
1083
1084static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00001085parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00001086{
Fred Drake268397f1998-04-29 14:16:32 +00001087 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00001088 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00001089 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00001090 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001091
Fred Drakec2683dd2001-07-17 19:32:05 +00001092 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001093 PyObject *newargs;
1094 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001095
Fred Drake2a6875e1999-09-20 22:32:18 +00001096 if ((empty_dict = PyDict_New()) == NULL)
1097 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00001098 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00001099 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00001100 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00001101 if (tuple != NULL) {
1102 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
1103 Py_DECREF(tuple);
1104 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001105 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001106 }
1107 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00001108 Py_XDECREF(empty_dict);
1109
Fred Drake43f8f9b1998-04-13 16:25:46 +00001110 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00001111}
Fred Drake43f8f9b1998-04-13 16:25:46 +00001112
1113
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001114/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00001115 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001116 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00001117 * We'd really have to write a wrapper around it all anyway to allow
1118 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001119 */
1120static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00001121 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001122 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001123 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001124 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001125 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001126 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001127 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001128 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001129 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001130 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001131 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001132 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001133 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001134 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001135 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001136 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001137 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001138 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001139
Fred Drake43f8f9b1998-04-13 16:25:46 +00001140 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00001141 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00001142 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00001143
Fred Drake268397f1998-04-29 14:16:32 +00001144 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001145 };
1146
1147
Martin v. Löwis1a214512008-06-11 05:26:20 +00001148
1149static struct PyModuleDef parsermodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 PyModuleDef_HEAD_INIT,
1151 "parser",
1152 NULL,
1153 -1,
1154 parser_functions,
1155 NULL,
1156 NULL,
1157 NULL,
1158 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001159};
1160
1161PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00001162
Mark Hammond62b1ab12002-07-23 06:31:15 +00001163PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001164PyInit_parser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00001165{
Fred Drake13130bc2001-08-15 16:44:56 +00001166 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00001167
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001168 if (PyType_Ready(&PyST_Type) < 0)
1169 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001170 module = PyModule_Create(&parsermodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001171 if (module == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 return NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001173
Fred Drake7a15ba51999-09-09 14:21:52 +00001174 if (parser_error == 0)
1175 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001176
Tim Peters6a627252003-07-21 14:25:23 +00001177 if (parser_error == 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001178 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00001179 /* CAUTION: The code next used to skip bumping the refcount on
Martin v. Löwis1a214512008-06-11 05:26:20 +00001180 * parser_error. That's a disaster if PyInit_parser() gets called more
Tim Peters6a627252003-07-21 14:25:23 +00001181 * than once. By incref'ing, we ensure that each module dict that
1182 * gets created owns its reference to the shared parser_error object,
1183 * and the file static parser_error vrbl owns a reference too.
1184 */
1185 Py_INCREF(parser_error);
1186 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001187 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00001188
Fred Drakec2683dd2001-07-17 19:32:05 +00001189 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00001190 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001191
Fred Drake13130bc2001-08-15 16:44:56 +00001192 PyModule_AddStringConstant(module, "__copyright__",
1193 parser_copyright_string);
1194 PyModule_AddStringConstant(module, "__doc__",
1195 parser_doc_string);
1196 PyModule_AddStringConstant(module, "__version__",
1197 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001198
Fred Drake78bdb9b2001-07-19 20:17:15 +00001199 /* Register to support pickling.
1200 * If this fails, the import of this module will fail because an
1201 * exception will be raised here; should we clear the exception?
1202 */
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00001203 copyreg = PyImport_ImportModuleNoBlock("copyreg");
Fred Drake13130bc2001-08-15 16:44:56 +00001204 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001205 PyObject *func, *pickler;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001206 _Py_IDENTIFIER(pickle);
1207 _Py_IDENTIFIER(sequence2st);
1208 _Py_IDENTIFIER(_pickler);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001209
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001210 func = _PyObject_GetAttrId(copyreg, &PyId_pickle);
1211 pickle_constructor = _PyObject_GetAttrId(module, &PyId_sequence2st);
1212 pickler = _PyObject_GetAttrId(module, &PyId__pickler);
Fred Drakeff9ea482000-04-19 13:54:15 +00001213 Py_XINCREF(pickle_constructor);
1214 if ((func != NULL) && (pickle_constructor != NULL)
1215 && (pickler != NULL)) {
1216 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001217
Thomas Wouters477c8d52006-05-27 19:21:47 +00001218 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
1219 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00001220 Py_XDECREF(res);
1221 }
1222 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00001223 Py_XDECREF(pickle_constructor);
1224 Py_XDECREF(pickler);
1225 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001226 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00001227 return module;
Fred Drakeff9ea482000-04-19 13:54:15 +00001228}