blob: 929f2deb16c428655cc7f088e80bdf5fc9db7dfd [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) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +020094 Py_RETURN_NONE;
Guido van Rossum3d602e31996-07-21 02:33:56 +000095 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +020096
Guido van Rossum3d602e31996-07-21 02:33:56 +000097 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000098 int i;
Fred Drake268397f1998-04-29 14:16:32 +000099
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200100 result = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
101 if (result == NULL)
102 goto error;
103
Christian Heimes217cfd12007-12-02 14:31:20 +0000104 w = PyLong_FromLong(TYPE(n));
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200105 if (w == NULL)
106 goto error;
107 (void) addelem(result, 0, w);
108
Fred Drakeff9ea482000-04-19 13:54:15 +0000109 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000110 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200111 if (w == NULL)
112 goto error;
113 (void) addelem(result, i+1, w);
Fred Drakeff9ea482000-04-19 13:54:15 +0000114 }
Tim Peters6a627252003-07-21 14:25:23 +0000115
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200116 if (TYPE(n) == encoding_decl) {
117 w = PyUnicode_FromString(STR(n));
118 if (w == NULL)
119 goto error;
120 (void) addelem(result, i+1, w);
121 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000122 }
123 else if (ISTERMINAL(TYPE(n))) {
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200124 result = mkseq(2 + lineno + col_offset);
125 if (result == NULL)
126 goto error;
127
128 w = PyLong_FromLong(TYPE(n));
129 if (w == NULL)
130 goto error;
131 (void) addelem(result, 0, w);
132
133 w = PyUnicode_FromString(STR(n));
134 if (w == NULL)
135 goto error;
136 (void) addelem(result, 1, w);
137
138 if (lineno == 1) {
139 w = PyLong_FromLong(n->n_lineno);
140 if (w == NULL)
141 goto error;
142 (void) addelem(result, 2, w);
Fred Drakeff9ea482000-04-19 13:54:15 +0000143 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200144
145 if (col_offset == 1) {
146 w = PyLong_FromLong(n->n_col_offset);
147 if (w == NULL)
148 goto error;
149 (void) addelem(result, 3, w);
150 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000151 }
152 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000153 PyErr_SetString(PyExc_SystemError,
154 "unrecognized parse tree node type");
155 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000156 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200157 return result;
158
159error:
160 Py_XDECREF(result);
161 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000162}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000163/*
164 * End of material copyrighted by Stichting Mathematisch Centrum.
165 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000166
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000167
168
169/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000170 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000171 * field of the object type to identify which any given object represents.
172 * These should probably go in an external header to allow other extensions
173 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000174 */
175
Fred Drakec2683dd2001-07-17 19:32:05 +0000176#define PyST_EXPR 1
177#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000178
179
180/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000181 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000182 * naming style, but the code uses the new naming convention.
183 */
184
185static PyObject*
186parser_error = 0;
187
188
Fred Drakec2683dd2001-07-17 19:32:05 +0000189typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000190 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000191 node* st_node; /* the node* returned by the parser */
192 int st_type; /* EXPR or SUITE ? */
Benjamin Petersonf216c942008-10-31 02:28:05 +0000193 PyCompilerFlags st_flags; /* Parser and compiler flags */
Fred Drakec2683dd2001-07-17 19:32:05 +0000194} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000195
196
Jeremy Hylton938ace62002-07-17 16:30:39 +0000197static void parser_free(PyST_Object *st);
Jesus Ceae9c53182012-08-03 14:28:37 +0200198static PyObject* parser_sizeof(PyST_Object *, void *);
Mark Dickinson211c6252009-02-01 10:28:51 +0000199static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op);
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000200static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
201static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
202static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
203static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
204static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
Fred Drake503d8d61998-04-13 18:45:18 +0000205
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000206#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
207
208static PyMethodDef parser_methods[] = {
209 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
210 PyDoc_STR("Compile this ST object into a code object.")},
211 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
212 PyDoc_STR("Determines if this ST object was created from an expression.")},
213 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
214 PyDoc_STR("Determines if this ST object was created from a suite.")},
215 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
216 PyDoc_STR("Creates a list-tree representation of this ST.")},
217 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
218 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Jesus Ceae9c53182012-08-03 14:28:37 +0200219 {"__sizeof__", (PyCFunction)parser_sizeof, METH_NOARGS,
220 PyDoc_STR("Returns size in memory, in bytes.")},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000221 {NULL, NULL, 0, NULL}
222};
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000223
Fred Drake268397f1998-04-29 14:16:32 +0000224static
Fred Drakec2683dd2001-07-17 19:32:05 +0000225PyTypeObject PyST_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000226 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000227 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000228 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000229 0, /* tp_itemsize */
230 (destructor)parser_free, /* tp_dealloc */
231 0, /* tp_print */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000232 0, /* tp_getattr */
Fred Drakeff9ea482000-04-19 13:54:15 +0000233 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000234 0, /* tp_reserved */
Fred Drakeff9ea482000-04-19 13:54:15 +0000235 0, /* tp_repr */
236 0, /* tp_as_number */
237 0, /* tp_as_sequence */
238 0, /* tp_as_mapping */
239 0, /* tp_hash */
240 0, /* tp_call */
241 0, /* tp_str */
242 0, /* tp_getattro */
243 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000244
245 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000246 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000247
Fred Drakeff9ea482000-04-19 13:54:15 +0000248 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000249
250 /* __doc__ */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000251 "Intermediate representation of a Python parse tree.",
252 0, /* tp_traverse */
253 0, /* tp_clear */
Mark Dickinson211c6252009-02-01 10:28:51 +0000254 parser_richcompare, /* tp_richcompare */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000255 0, /* tp_weaklistoffset */
256 0, /* tp_iter */
257 0, /* tp_iternext */
258 parser_methods, /* tp_methods */
Fred Drakec2683dd2001-07-17 19:32:05 +0000259}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000260
261
Mark Dickinson211c6252009-02-01 10:28:51 +0000262/* PyST_Type isn't subclassable, so just check ob_type */
263#define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type)
264
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000266parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000267{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000268 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000269
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000270 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000271 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000272
273 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000274 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000275
276 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000277 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000278
279 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000280 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000281
282 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000283 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000284
285 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000286 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000287
Fred Drakeff9ea482000-04-19 13:54:15 +0000288 if (v != 0)
289 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000290 }
291 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000292}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000293
Mark Dickinson211c6252009-02-01 10:28:51 +0000294/* parser_richcompare(PyObject* left, PyObject* right, int op)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000295 *
296 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
297 * This really just wraps a call to parser_compare_nodes() with some easy
298 * checks and protection code.
299 *
300 */
Mark Dickinson211c6252009-02-01 10:28:51 +0000301
302#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
303
304static PyObject *
305parser_richcompare(PyObject *left, PyObject *right, int op)
Guido van Rossum47478871996-08-21 14:32:37 +0000306{
Mark Dickinson211c6252009-02-01 10:28:51 +0000307 int result;
308 PyObject *v;
309
310 /* neither argument should be NULL, unless something's gone wrong */
311 if (left == NULL || right == NULL) {
312 PyErr_BadInternalCall();
313 return NULL;
314 }
315
316 /* both arguments should be instances of PyST_Object */
317 if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) {
318 v = Py_NotImplemented;
319 goto finished;
320 }
321
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000322 if (left == right)
Mark Dickinson211c6252009-02-01 10:28:51 +0000323 /* if arguments are identical, they're equal */
324 result = 0;
325 else
326 result = parser_compare_nodes(((PyST_Object *)left)->st_node,
327 ((PyST_Object *)right)->st_node);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000328
Mark Dickinson211c6252009-02-01 10:28:51 +0000329 /* Convert return value to a Boolean */
330 switch (op) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000331 case Py_EQ:
Mark Dickinson211c6252009-02-01 10:28:51 +0000332 v = TEST_COND(result == 0);
333 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000334 case Py_NE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000335 v = TEST_COND(result != 0);
336 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000337 case Py_LE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000338 v = TEST_COND(result <= 0);
339 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000340 case Py_GE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000341 v = TEST_COND(result >= 0);
342 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000343 case Py_LT:
Mark Dickinson211c6252009-02-01 10:28:51 +0000344 v = TEST_COND(result < 0);
345 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000346 case Py_GT:
Mark Dickinson211c6252009-02-01 10:28:51 +0000347 v = TEST_COND(result > 0);
348 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000349 default:
Mark Dickinson211c6252009-02-01 10:28:51 +0000350 PyErr_BadArgument();
351 return NULL;
352 }
353 finished:
354 Py_INCREF(v);
355 return v;
Fred Drakeff9ea482000-04-19 13:54:15 +0000356}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000357
Fred Drakec2683dd2001-07-17 19:32:05 +0000358/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000359 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000360 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000361 * 'wrapper' object that holds a node* and allows it to be passed around in
362 * Python code.
363 *
364 */
365static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000366parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000367{
Fred Drakec2683dd2001-07-17 19:32:05 +0000368 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000369
370 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000371 o->st_node = st;
372 o->st_type = type;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000373 o->st_flags.cf_flags = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000374 }
Fred Drake268397f1998-04-29 14:16:32 +0000375 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000376 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000377 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000378 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000379}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000380
381
Fred Drakec2683dd2001-07-17 19:32:05 +0000382/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000383 *
384 * This is called by a del statement that reduces the reference count to 0.
385 *
386 */
387static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000388parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000389{
Fred Drakec2683dd2001-07-17 19:32:05 +0000390 PyNode_Free(st->st_node);
391 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000392}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000393
Jesus Ceae9c53182012-08-03 14:28:37 +0200394static PyObject *
395parser_sizeof(PyST_Object *st, void *unused)
396{
397 Py_ssize_t res;
398
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +0200399 res = _PyObject_SIZE(Py_TYPE(st)) + _PyNode_SizeOf(st->st_node);
Jesus Ceae9c53182012-08-03 14:28:37 +0200400 return PyLong_FromSsize_t(res);
401}
402
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000403
Fred Drakec2683dd2001-07-17 19:32:05 +0000404/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000405 *
406 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000407 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000408 *
409 */
410static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000411parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000412{
Antoine Pitrou721738f2012-08-15 23:20:39 +0200413 int line_info = 0;
414 int col_info = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000415 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000416 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000417
Georg Brandl30704ea02008-07-23 15:07:12 +0000418 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000419
Martin v. Löwis1a214512008-06-11 05:26:20 +0000420 if (self == NULL || PyModule_Check(self)) {
Antoine Pitrou721738f2012-08-15 23:20:39 +0200421 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2tuple", keywords,
422 &PyST_Type, &self, &line_info,
423 &col_info);
Fred Drake268397f1998-04-29 14:16:32 +0000424 }
Fred Drake503d8d61998-04-13 18:45:18 +0000425 else
Antoine Pitrou721738f2012-08-15 23:20:39 +0200426 ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:totuple", &keywords[1],
427 &line_info, &col_info);
Fred Drake268397f1998-04-29 14:16:32 +0000428 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000429 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000430 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000431 * since it's known to work already.
432 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000433 res = node2tuple(((PyST_Object*)self)->st_node,
Antoine Pitrou721738f2012-08-15 23:20:39 +0200434 PyTuple_New, PyTuple_SetItem, line_info, col_info);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000435 }
436 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000437}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000438
439
Fred Drakec2683dd2001-07-17 19:32:05 +0000440/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000441 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000442 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000443 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000444 *
445 */
446static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000447parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000448{
Antoine Pitrou721738f2012-08-15 23:20:39 +0200449 int line_info = 0;
450 int col_info = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000451 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000452 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000453
Georg Brandl30704ea02008-07-23 15:07:12 +0000454 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000455
Martin v. Löwis1a214512008-06-11 05:26:20 +0000456 if (self == NULL || PyModule_Check(self))
Antoine Pitrou721738f2012-08-15 23:20:39 +0200457 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2list", keywords,
458 &PyST_Type, &self, &line_info,
459 &col_info);
Fred Drake503d8d61998-04-13 18:45:18 +0000460 else
Antoine Pitrou721738f2012-08-15 23:20:39 +0200461 ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:tolist", &keywords[1],
462 &line_info, &col_info);
Fred Drake503d8d61998-04-13 18:45:18 +0000463 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000464 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000465 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000466 * since it's known to work already.
467 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000468 res = node2tuple(self->st_node,
Antoine Pitrou721738f2012-08-15 23:20:39 +0200469 PyList_New, PyList_SetItem, line_info, col_info);
Guido van Rossum47478871996-08-21 14:32:37 +0000470 }
471 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000472}
Guido van Rossum47478871996-08-21 14:32:37 +0000473
474
Fred Drakec2683dd2001-07-17 19:32:05 +0000475/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000476 *
477 * This function creates code objects from the parse tree represented by
478 * the passed-in data object. An optional file name is passed in as well.
479 *
480 */
481static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000482parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000483{
Victor Stinner14e461d2013-08-26 22:28:21 +0200484 PyObject* res = NULL;
485 PyArena* arena = NULL;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000486 mod_ty mod;
Victor Stinner14e461d2013-08-26 22:28:21 +0200487 PyObject* filename = NULL;
Fred Drake503d8d61998-04-13 18:45:18 +0000488 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000489
Georg Brandl30704ea02008-07-23 15:07:12 +0000490 static char *keywords[] = {"st", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000491
Martin v. Löwis1a214512008-06-11 05:26:20 +0000492 if (self == NULL || PyModule_Check(self))
Victor Stinner14e461d2013-08-26 22:28:21 +0200493 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O&:compilest", keywords,
494 &PyST_Type, &self,
495 PyUnicode_FSDecoder, &filename);
Fred Drake503d8d61998-04-13 18:45:18 +0000496 else
Victor Stinner14e461d2013-08-26 22:28:21 +0200497 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O&:compile", &keywords[1],
498 PyUnicode_FSDecoder, &filename);
499 if (!ok)
500 goto error;
Fred Drake503d8d61998-04-13 18:45:18 +0000501
Victor Stinner14e461d2013-08-26 22:28:21 +0200502 if (filename == NULL) {
503 filename = PyUnicode_FromString("<syntax-tree>");
504 if (filename == NULL)
505 goto error;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000506 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000507
Victor Stinner14e461d2013-08-26 22:28:21 +0200508 arena = PyArena_New();
509 if (!arena)
510 goto error;
511
512 mod = PyAST_FromNodeObject(self->st_node, &self->st_flags,
513 filename, arena);
514 if (!mod)
515 goto error;
516
517 res = (PyObject *)PyAST_CompileObject(mod, filename,
518 &self->st_flags, -1, arena);
519error:
520 Py_XDECREF(filename);
521 if (arena != NULL)
522 PyArena_Free(arena);
523 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +0000524}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000525
526
527/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
528 * PyObject* parser_issuite(PyObject* self, PyObject* args)
529 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000530 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000531 * a statement suite, respectively. The return is a Python truth value.
532 *
533 */
534static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000535parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000536{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000537 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000538 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000539
Georg Brandl30704ea02008-07-23 15:07:12 +0000540 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000541
Martin v. Löwis1a214512008-06-11 05:26:20 +0000542 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000543 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000544 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000545 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000546 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000547
548 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000549 /* Check to see if the ST represents an expression or not. */
550 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000551 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000552 }
553 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000554}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000555
556
557static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000558parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000559{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000560 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000561 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000562
Georg Brandl30704ea02008-07-23 15:07:12 +0000563 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000564
Martin v. Löwis1a214512008-06-11 05:26:20 +0000565 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000566 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000567 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000568 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000569 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000570
571 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000572 /* Check to see if the ST represents an expression or not. */
573 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000574 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000575 }
576 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000577}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000578
579
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200580/* err_string(const char* message)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000581 *
582 * Sets the error string for an exception of type ParserError.
583 *
584 */
585static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200586err_string(const char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000587{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000588 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000589}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000590
591
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000592/* PyObject* parser_do_parse(PyObject* args, int type)
593 *
594 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000595 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000596 *
597 */
598static PyObject*
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200599parser_do_parse(PyObject *args, PyObject *kw, const char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000600{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000601 char* string = 0;
602 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000603 int flags = 0;
604 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000605
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000606 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000607
608 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000609 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
610 &_PyParser_Grammar,
611 (type == PyST_EXPR)
612 ? eval_input : file_input,
613 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 if (n) {
616 res = parser_newstobject(n, type);
Benjamin Petersonf216c942008-10-31 02:28:05 +0000617 if (res)
618 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
619 }
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500620 else {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000621 PyParser_SetError(&err);
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500622 }
Benjamin Petersonf0cdbad2011-06-05 22:14:05 -0500623 PyParser_ClearError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000624 }
625 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000626}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000627
628
629/* PyObject* parser_expr(PyObject* self, PyObject* args)
630 * PyObject* parser_suite(PyObject* self, PyObject* args)
631 *
632 * External interfaces to the parser itself. Which is called determines if
633 * the parser attempts to recognize an expression ('eval' form) or statement
634 * suite ('exec' form). The real work is done by parser_do_parse() above.
635 *
636 */
637static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000638parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000639{
Fred Drake268397f1998-04-29 14:16:32 +0000640 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000641 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000642}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000643
644
645static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000646parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000647{
Fred Drake268397f1998-04-29 14:16:32 +0000648 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000649 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000650}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000651
652
653
Fred Drakec2683dd2001-07-17 19:32:05 +0000654/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000655 * object requires that the input tuple be valid without having to rely on
656 * catching an exception from the compiler. This is done to allow the
657 * compiler itself to remain fast, since most of its input will come from
658 * the parser directly, and therefore be known to be syntactically correct.
659 * This validation is done to ensure that we don't core dump the compile
660 * phase, returning an exception instead.
661 *
662 * Two aspects can be broken out in this code: creating a node tree from
663 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000664 * advantageous to expand the number of ST types to include funcdefs and
665 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000666 * here. They are not necessary, and not quite as useful in a raw form.
667 * For now, let's get expressions and suites working reliably.
668 */
669
670
Jeremy Hylton938ace62002-07-17 16:30:39 +0000671static node* build_node_tree(PyObject *tuple);
Benjamin Peterson53595c42016-06-02 11:30:18 -0700672
673static int
674validate_node(node *tree)
675{
676 int type = TYPE(tree);
677 int nch = NCH(tree);
678 dfa *nt_dfa;
679 state *dfa_state;
680 int pos, arc;
681
682 assert(ISNONTERMINAL(type));
683 type -= NT_OFFSET;
684 if (type >= _PyParser_Grammar.g_ndfas) {
685 PyErr_Format(parser_error, "Unrecognized node type %d.", TYPE(tree));
686 return 0;
687 }
688 nt_dfa = &_PyParser_Grammar.g_dfa[type];
689 REQ(tree, nt_dfa->d_type);
690
691 /* Run the DFA for this nonterminal. */
692 dfa_state = &nt_dfa->d_state[nt_dfa->d_initial];
693 for (pos = 0; pos < nch; ++pos) {
694 node *ch = CHILD(tree, pos);
695 int ch_type = TYPE(ch);
696 for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
697 short a_label = dfa_state->s_arc[arc].a_lbl;
698 assert(a_label < _PyParser_Grammar.g_ll.ll_nlabels);
699 if (_PyParser_Grammar.g_ll.ll_label[a_label].lb_type == ch_type) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300700 /* The child is acceptable; if non-terminal, validate it recursively. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700701 if (ISNONTERMINAL(ch_type) && !validate_node(ch))
702 return 0;
703
704 /* Update the state, and move on to the next child. */
705 dfa_state = &nt_dfa->d_state[dfa_state->s_arc[arc].a_arrow];
706 goto arc_found;
707 }
708 }
709 /* What would this state have accepted? */
710 {
711 short a_label = dfa_state->s_arc->a_lbl;
712 int next_type;
713 if (!a_label) /* Wouldn't accept any more children */
714 goto illegal_num_children;
715
716 next_type = _PyParser_Grammar.g_ll.ll_label[a_label].lb_type;
717 if (ISNONTERMINAL(next_type))
718 PyErr_Format(parser_error, "Expected node type %d, got %d.",
719 next_type, ch_type);
720 else
721 PyErr_Format(parser_error, "Illegal terminal: expected %s.",
722 _PyParser_TokenNames[next_type]);
723 return 0;
724 }
725
726arc_found:
727 continue;
728 }
729 /* Are we in a final state? If so, return 1 for successful validation. */
730 for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
731 if (!dfa_state->s_arc[arc].a_lbl) {
732 return 1;
733 }
734 }
735
736illegal_num_children:
737 PyErr_Format(parser_error,
738 "Illegal number of children for %s node.", nt_dfa->d_name);
739 return 0;
740}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000741
Fred Drakec2683dd2001-07-17 19:32:05 +0000742/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000743 *
744 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000745 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000746 * tuple can be validated. It does this by checking the first code of the
747 * tuple, and, if acceptable, builds the internal representation. If this
748 * step succeeds, the internal representation is validated as fully as
Benjamin Peterson53595c42016-06-02 11:30:18 -0700749 * possible with the recursive validate_node() routine defined above.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000750 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000751 * This function must be changed if support is to be added for PyST_FRAGMENT
752 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000753 *
754 */
755static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000756parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000757{
Fred Drake268397f1998-04-29 14:16:32 +0000758 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000759 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000760 PyObject *tuple;
761 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000762
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000763 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000764
Fred Drakec2683dd2001-07-17 19:32:05 +0000765 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000766 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000767 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000768 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000769 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000770 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000771 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000772 }
773 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000774 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000775 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000776 tree = build_node_tree(tuple);
777 if (tree != 0) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300778 node *validation_root = NULL;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700779 int tree_type = 0;
780 switch (TYPE(tree)) {
781 case eval_input:
Fred Drake0ac9b072000-09-12 21:58:06 +0000782 /* Might be an eval form. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700783 tree_type = PyST_EXPR;
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300784 validation_root = tree;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700785 break;
786 case encoding_decl:
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000787 /* This looks like an encoding_decl so far. */
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300788 if (NCH(tree) == 1) {
789 tree_type = PyST_SUITE;
790 validation_root = CHILD(tree, 0);
791 }
792 else {
Benjamin Peterson53595c42016-06-02 11:30:18 -0700793 err_string("Error Parsing encoding_decl");
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300794 }
795 break;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700796 case file_input:
797 /* This looks like an exec form so far. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700798 tree_type = PyST_SUITE;
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300799 validation_root = tree;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700800 break;
801 default:
Fred Drake0ac9b072000-09-12 21:58:06 +0000802 /* This is a fragment, at best. */
Fred Drake661ea262000-10-24 19:57:45 +0000803 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000804 }
Benjamin Peterson53595c42016-06-02 11:30:18 -0700805
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300806 if (validation_root != NULL && validate_node(validation_root))
Benjamin Peterson53595c42016-06-02 11:30:18 -0700807 st = parser_newstobject(tree, tree_type);
808 else
809 PyNode_Free(tree);
Guido van Rossum47478871996-08-21 14:32:37 +0000810 }
Andrew Svetlov737fb892012-12-18 21:14:22 +0200811 /* Make sure we raise an exception on all errors. We should never
Guido van Rossum47478871996-08-21 14:32:37 +0000812 * get this, but we'd do well to be sure something is done.
813 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000814 if (st == NULL && !PyErr_Occurred())
815 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000816
Fred Drakec2683dd2001-07-17 19:32:05 +0000817 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000818}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000819
820
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000821/* node* build_node_children()
822 *
823 * Iterate across the children of the current non-terminal node and build
824 * their structures. If successful, return the root of this portion of
825 * the tree, otherwise, 0. Any required exception will be specified already,
826 * and no memory will have been deallocated.
827 *
828 */
829static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000830build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000831{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000832 Py_ssize_t len = PyObject_Size(tuple);
833 Py_ssize_t i;
834 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000835
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300836 if (len < 0) {
837 return NULL;
838 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000839 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000840 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000841 PyObject* elem = PySequence_GetItem(tuple, i);
842 int ok = elem != NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200843 int type = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000844 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000845
Fred Drakeff9ea482000-04-19 13:54:15 +0000846 if (ok)
847 ok = PySequence_Check(elem);
848 if (ok) {
849 PyObject *temp = PySequence_GetItem(elem, 0);
850 if (temp == NULL)
851 ok = 0;
852 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000853 ok = PyLong_Check(temp);
Serhiy Storchaka78980432013-01-15 01:12:17 +0200854 if (ok) {
855 type = _PyLong_AsInt(temp);
856 if (type == -1 && PyErr_Occurred()) {
857 Py_DECREF(temp);
858 Py_DECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300859 return NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200860 }
861 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000862 Py_DECREF(temp);
863 }
864 }
865 if (!ok) {
Victor Stinner5f8d4852014-01-02 11:49:27 +0100866 PyObject *err = Py_BuildValue("Os", elem,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000867 "Illegal node construct.");
868 PyErr_SetObject(parser_error, err);
869 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000870 Py_XDECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300871 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000872 }
873 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000874 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000875 PyObject *temp;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000876 const char *temp_str;
Guido van Rossum47478871996-08-21 14:32:37 +0000877
Fred Drake0ac9b072000-09-12 21:58:06 +0000878 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000879 err_string("terminal nodes must have 2 or 3 entries");
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300880 Py_DECREF(elem);
881 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000882 }
883 temp = PySequence_GetItem(elem, 1);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300884 if (temp == NULL) {
885 Py_DECREF(elem);
886 return NULL;
887 }
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000888 if (!PyUnicode_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000889 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000890 "second item in terminal node must be a string,"
891 " found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000892 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000893 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000894 Py_DECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300895 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000896 }
897 if (len == 3) {
898 PyObject *o = PySequence_GetItem(elem, 2);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300899 if (o == NULL) {
900 Py_DECREF(temp);
901 Py_DECREF(elem);
902 return NULL;
903 }
904 if (PyLong_Check(o)) {
905 int num = _PyLong_AsInt(o);
906 if (num == -1 && PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000907 Py_DECREF(o);
908 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000909 Py_DECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300910 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000911 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300912 *line_num = num;
Fred Drakeff9ea482000-04-19 13:54:15 +0000913 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300914 else {
915 PyErr_Format(parser_error,
916 "third item in terminal node must be an"
917 " integer, found %s",
918 Py_TYPE(temp)->tp_name);
919 Py_DECREF(o);
920 Py_DECREF(temp);
921 Py_DECREF(elem);
922 return NULL;
923 }
924 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000925 }
Serhiy Storchaka06515832016-11-20 09:13:07 +0200926 temp_str = PyUnicode_AsUTF8AndSize(temp, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000927 if (temp_str == NULL) {
928 Py_DECREF(temp);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300929 Py_DECREF(elem);
930 return NULL;
Alexander Belopolskye239d232010-12-08 23:31:48 +0000931 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000932 strn = (char *)PyObject_MALLOC(len + 1);
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200933 if (strn == NULL) {
934 Py_DECREF(temp);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300935 Py_DECREF(elem);
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200936 PyErr_NoMemory();
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300937 return NULL;
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200938 }
939 (void) memcpy(strn, temp_str, len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000940 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000941 }
942 else if (!ISNONTERMINAL(type)) {
943 /*
944 * It has to be one or the other; this is an error.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200945 * Raise an exception.
Fred Drakeff9ea482000-04-19 13:54:15 +0000946 */
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300947 PyObject *err = Py_BuildValue("Os", elem, "unknown node type.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000948 PyErr_SetObject(parser_error, err);
949 Py_XDECREF(err);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300950 Py_DECREF(elem);
951 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000952 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000953 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000954 if (err == E_NOMEM) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300955 Py_DECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000956 PyObject_FREE(strn);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300957 PyErr_NoMemory();
958 return NULL;
Fred Drake8b55b2d2001-12-05 22:10:44 +0000959 }
960 if (err == E_OVERFLOW) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300961 Py_DECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000962 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000963 PyErr_SetString(PyExc_ValueError,
964 "unsupported number of child nodes");
965 return NULL;
966 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000967
Fred Drakeff9ea482000-04-19 13:54:15 +0000968 if (ISNONTERMINAL(type)) {
969 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000970
Fred Drakeff9ea482000-04-19 13:54:15 +0000971 if (new_child != build_node_children(elem, new_child, line_num)) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300972 Py_DECREF(elem);
973 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000974 }
975 }
976 else if (type == NEWLINE) { /* It's true: we increment the */
977 ++(*line_num); /* line number *after* the newline! */
978 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300979 Py_DECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000980 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000981 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000982}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000983
984
985static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000986build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000987{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000988 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000989 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000990 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000991
Guido van Rossum47478871996-08-21 14:32:37 +0000992 if (temp != NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000993 num = PyLong_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000994 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000995 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000996 /*
997 * The tuple is simple, but it doesn't start with a start symbol.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200998 * Raise an exception now and be done with it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000999 */
Victor Stinner6684bdf2013-07-17 00:13:52 +02001000 tuple = Py_BuildValue("Os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +00001001 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +00001002 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001003 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001004 }
1005 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001006 /*
1007 * Not efficient, but that can be handled later.
1008 */
1009 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001010 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001011
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001012 if (num == encoding_decl) {
1013 encoding = PySequence_GetItem(tuple, 2);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001014 if (encoding == NULL) {
1015 PyErr_SetString(parser_error, "missed encoding");
1016 return NULL;
1017 }
1018 if (!PyUnicode_Check(encoding)) {
1019 PyErr_Format(parser_error,
1020 "encoding must be a string, found %.200s",
1021 Py_TYPE(encoding)->tp_name);
1022 Py_DECREF(encoding);
1023 return NULL;
1024 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001025 /* tuple isn't borrowed anymore here, need to DECREF */
1026 tuple = PySequence_GetSlice(tuple, 0, 2);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001027 if (tuple == NULL) {
1028 Py_DECREF(encoding);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001029 return NULL;
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001030 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001031 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001032 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +00001033 if (res != NULL) {
1034 if (res != build_node_children(tuple, res, &line_num)) {
1035 PyNode_Free(res);
1036 res = NULL;
1037 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001038 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +00001039 Py_ssize_t len;
Neal Norwitz3fcbea52007-08-26 04:51:28 +00001040 const char *temp;
Serhiy Storchaka06515832016-11-20 09:13:07 +02001041 temp = PyUnicode_AsUTF8AndSize(encoding, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001042 if (temp == NULL) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001043 PyNode_Free(res);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001044 Py_DECREF(encoding);
1045 Py_DECREF(tuple);
1046 return NULL;
1047 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00001048 res->n_str = (char *)PyObject_MALLOC(len + 1);
Victor Stinner3bd6abd2013-07-12 01:33:59 +02001049 if (res->n_str == NULL) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001050 PyNode_Free(res);
Victor Stinner3bd6abd2013-07-12 01:33:59 +02001051 Py_DECREF(encoding);
1052 Py_DECREF(tuple);
1053 PyErr_NoMemory();
1054 return NULL;
1055 }
1056 (void) memcpy(res->n_str, temp, len + 1);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001057 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001058 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001059 if (encoding != NULL) {
1060 Py_DECREF(encoding);
1061 Py_DECREF(tuple);
1062 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001063 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001064 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001065 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +00001066 * NONTERMINAL, we can't use it. Not sure the implementation
1067 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +00001068 */
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001069 PyObject *err = Py_BuildValue("Os", tuple,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001070 "Illegal component tuple.");
1071 PyErr_SetObject(parser_error, err);
1072 Py_XDECREF(err);
1073 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001074
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001075 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001076}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001077
1078
Fred Drake43f8f9b1998-04-13 16:25:46 +00001079static PyObject*
1080pickle_constructor = NULL;
1081
1082
1083static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00001084parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00001085{
Fred Drake268397f1998-04-29 14:16:32 +00001086 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00001087 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00001088 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00001089 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001090
Fred Drakec2683dd2001-07-17 19:32:05 +00001091 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001092 PyObject *newargs;
1093 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001094
Fred Drake2a6875e1999-09-20 22:32:18 +00001095 if ((empty_dict = PyDict_New()) == NULL)
1096 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00001097 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00001098 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00001099 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00001100 if (tuple != NULL) {
1101 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
1102 Py_DECREF(tuple);
1103 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001104 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001105 }
1106 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00001107 Py_XDECREF(empty_dict);
1108
Fred Drake43f8f9b1998-04-13 16:25:46 +00001109 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00001110}
Fred Drake43f8f9b1998-04-13 16:25:46 +00001111
1112
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001113/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00001114 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001115 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00001116 * We'd really have to write a wrapper around it all anyway to allow
1117 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001118 */
1119static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00001120 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001121 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001122 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001123 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001124 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001125 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001126 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001127 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001128 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001129 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001130 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001131 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001132 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001133 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001134 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001135 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001136 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001137 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001138
Fred Drake43f8f9b1998-04-13 16:25:46 +00001139 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00001140 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00001141 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00001142
Fred Drake268397f1998-04-29 14:16:32 +00001143 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001144 };
1145
1146
Martin v. Löwis1a214512008-06-11 05:26:20 +00001147
1148static struct PyModuleDef parsermodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 PyModuleDef_HEAD_INIT,
1150 "parser",
1151 NULL,
1152 -1,
1153 parser_functions,
1154 NULL,
1155 NULL,
1156 NULL,
1157 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001158};
1159
1160PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00001161
Mark Hammond62b1ab12002-07-23 06:31:15 +00001162PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001163PyInit_parser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00001164{
Fred Drake13130bc2001-08-15 16:44:56 +00001165 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00001166
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001167 if (PyType_Ready(&PyST_Type) < 0)
1168 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001169 module = PyModule_Create(&parsermodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001170 if (module == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 return NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001172
Fred Drake7a15ba51999-09-09 14:21:52 +00001173 if (parser_error == 0)
1174 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001175
Tim Peters6a627252003-07-21 14:25:23 +00001176 if (parser_error == 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001177 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00001178 /* CAUTION: The code next used to skip bumping the refcount on
Martin v. Löwis1a214512008-06-11 05:26:20 +00001179 * parser_error. That's a disaster if PyInit_parser() gets called more
Tim Peters6a627252003-07-21 14:25:23 +00001180 * than once. By incref'ing, we ensure that each module dict that
1181 * gets created owns its reference to the shared parser_error object,
1182 * and the file static parser_error vrbl owns a reference too.
1183 */
1184 Py_INCREF(parser_error);
1185 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001186 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00001187
Fred Drakec2683dd2001-07-17 19:32:05 +00001188 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00001189 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001190
Fred Drake13130bc2001-08-15 16:44:56 +00001191 PyModule_AddStringConstant(module, "__copyright__",
1192 parser_copyright_string);
1193 PyModule_AddStringConstant(module, "__doc__",
1194 parser_doc_string);
1195 PyModule_AddStringConstant(module, "__version__",
1196 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001197
Fred Drake78bdb9b2001-07-19 20:17:15 +00001198 /* Register to support pickling.
1199 * If this fails, the import of this module will fail because an
1200 * exception will be raised here; should we clear the exception?
1201 */
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00001202 copyreg = PyImport_ImportModuleNoBlock("copyreg");
Fred Drake13130bc2001-08-15 16:44:56 +00001203 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001204 PyObject *func, *pickler;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001205 _Py_IDENTIFIER(pickle);
1206 _Py_IDENTIFIER(sequence2st);
1207 _Py_IDENTIFIER(_pickler);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001208
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001209 func = _PyObject_GetAttrId(copyreg, &PyId_pickle);
1210 pickle_constructor = _PyObject_GetAttrId(module, &PyId_sequence2st);
1211 pickler = _PyObject_GetAttrId(module, &PyId__pickler);
Fred Drakeff9ea482000-04-19 13:54:15 +00001212 Py_XINCREF(pickle_constructor);
1213 if ((func != NULL) && (pickle_constructor != NULL)
1214 && (pickler != NULL)) {
1215 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001216
Thomas Wouters477c8d52006-05-27 19:21:47 +00001217 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
1218 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00001219 Py_XDECREF(res);
1220 }
1221 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00001222 Py_XDECREF(pickle_constructor);
1223 Py_XDECREF(pickler);
1224 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001225 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00001226 return module;
Fred Drakeff9ea482000-04-19 13:54:15 +00001227}