blob: 799a813468f143deb06b958c9a55c4a51830538c [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 *
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000027 */
28
Fred Drakeff9ea482000-04-19 13:54:15 +000029#include "Python.h" /* general Python API */
Benjamin Petersonf216c942008-10-31 02:28:05 +000030#include "Python-ast.h" /* mod_ty */
Fred Drakeff9ea482000-04-19 13:54:15 +000031#include "graminit.h" /* symbols defined in the grammar */
32#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000033#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000034#include "token.h" /* token definitions */
Benjamin Petersonf216c942008-10-31 02:28:05 +000035#include "grammar.h"
36#include "parsetok.h"
Fred Drakeff9ea482000-04-19 13:54:15 +000037 /* ISTERMINAL() / ISNONTERMINAL() */
Benjamin Petersonf216c942008-10-31 02:28:05 +000038#undef Yield
39#include "ast.h"
Benjamin Petersonf216c942008-10-31 02:28:05 +000040
41extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000042
Fred Drake268397f1998-04-29 14:16:32 +000043#ifdef lint
44#include <note.h>
45#else
46#define NOTE(x)
47#endif
48
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000049/* String constants used to initialize module attributes.
50 *
51 */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020052static const char parser_copyright_string[] =
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000053"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000054University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
55Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
56Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000057
58
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000059PyDoc_STRVAR(parser_doc_string,
60"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000061
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020062static const char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000063
64
Martin v. Löwis18e16552006-02-15 17:27:45 +000065typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000066typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000067 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000068 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000069
Thomas Wouters7e474022000-07-16 12:04:32 +000070/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000071 * original copyright statement is included below, and continues to apply
72 * in full to the function immediately following. All other material is
73 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
74 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000075 * new naming conventions. Added arguments to provide support for creating
76 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000077 */
78
Guido van Rossum52f2c051993-11-10 12:53:24 +000079
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000080static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000081node2tuple(node *n, /* node to convert */
82 SeqMaker mkseq, /* create sequence */
83 SeqInserter addelem, /* func. to add elem. in seq. */
Thomas Wouters89f507f2006-12-13 04:49:30 +000084 int lineno, /* include line numbers? */
85 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000086{
Victor Stinnerdf4572c2013-07-12 01:35:10 +020087 PyObject *result = NULL, *w;
88
Guido van Rossum3d602e31996-07-21 02:33:56 +000089 if (n == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +020090 Py_RETURN_NONE;
Guido van Rossum3d602e31996-07-21 02:33:56 +000091 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +020092
Guido van Rossum3d602e31996-07-21 02:33:56 +000093 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000094 int i;
Fred Drake268397f1998-04-29 14:16:32 +000095
Victor Stinnerdf4572c2013-07-12 01:35:10 +020096 result = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
97 if (result == NULL)
98 goto error;
99
Christian Heimes217cfd12007-12-02 14:31:20 +0000100 w = PyLong_FromLong(TYPE(n));
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200101 if (w == NULL)
102 goto error;
103 (void) addelem(result, 0, w);
104
Fred Drakeff9ea482000-04-19 13:54:15 +0000105 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000106 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200107 if (w == NULL)
108 goto error;
109 (void) addelem(result, i+1, w);
Fred Drakeff9ea482000-04-19 13:54:15 +0000110 }
Tim Peters6a627252003-07-21 14:25:23 +0000111
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200112 if (TYPE(n) == encoding_decl) {
113 w = PyUnicode_FromString(STR(n));
114 if (w == NULL)
115 goto error;
116 (void) addelem(result, i+1, w);
117 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000118 }
119 else if (ISTERMINAL(TYPE(n))) {
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200120 result = mkseq(2 + lineno + col_offset);
121 if (result == NULL)
122 goto error;
123
124 w = PyLong_FromLong(TYPE(n));
125 if (w == NULL)
126 goto error;
127 (void) addelem(result, 0, w);
128
129 w = PyUnicode_FromString(STR(n));
130 if (w == NULL)
131 goto error;
132 (void) addelem(result, 1, w);
133
Miss Islington (bot)b27c71c2018-04-18 22:10:36 -0700134 if (lineno) {
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200135 w = PyLong_FromLong(n->n_lineno);
136 if (w == NULL)
137 goto error;
138 (void) addelem(result, 2, w);
Fred Drakeff9ea482000-04-19 13:54:15 +0000139 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200140
Miss Islington (bot)b27c71c2018-04-18 22:10:36 -0700141 if (col_offset) {
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200142 w = PyLong_FromLong(n->n_col_offset);
143 if (w == NULL)
144 goto error;
Miss Islington (bot)b27c71c2018-04-18 22:10:36 -0700145 (void) addelem(result, 2 + lineno, w);
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200146 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000147 }
148 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000149 PyErr_SetString(PyExc_SystemError,
150 "unrecognized parse tree node type");
151 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000152 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200153 return result;
154
155error:
156 Py_XDECREF(result);
157 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000158}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000159/*
160 * End of material copyrighted by Stichting Mathematisch Centrum.
161 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000162
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000163
164
165/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000166 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000167 * field of the object type to identify which any given object represents.
168 * These should probably go in an external header to allow other extensions
169 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000170 */
171
Fred Drakec2683dd2001-07-17 19:32:05 +0000172#define PyST_EXPR 1
173#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000174
175
176/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000177 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000178 * naming style, but the code uses the new naming convention.
179 */
180
181static PyObject*
182parser_error = 0;
183
184
Fred Drakec2683dd2001-07-17 19:32:05 +0000185typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000186 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000187 node* st_node; /* the node* returned by the parser */
188 int st_type; /* EXPR or SUITE ? */
Benjamin Petersonf216c942008-10-31 02:28:05 +0000189 PyCompilerFlags st_flags; /* Parser and compiler flags */
Fred Drakec2683dd2001-07-17 19:32:05 +0000190} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000191
192
Jeremy Hylton938ace62002-07-17 16:30:39 +0000193static void parser_free(PyST_Object *st);
Jesus Ceae9c53182012-08-03 14:28:37 +0200194static PyObject* parser_sizeof(PyST_Object *, void *);
Mark Dickinson211c6252009-02-01 10:28:51 +0000195static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op);
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000196static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
197static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
198static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
199static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
200static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
Fred Drake503d8d61998-04-13 18:45:18 +0000201
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000202#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
203
204static PyMethodDef parser_methods[] = {
205 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
206 PyDoc_STR("Compile this ST object into a code object.")},
207 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
208 PyDoc_STR("Determines if this ST object was created from an expression.")},
209 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
210 PyDoc_STR("Determines if this ST object was created from a suite.")},
211 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
212 PyDoc_STR("Creates a list-tree representation of this ST.")},
213 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
214 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Jesus Ceae9c53182012-08-03 14:28:37 +0200215 {"__sizeof__", (PyCFunction)parser_sizeof, METH_NOARGS,
216 PyDoc_STR("Returns size in memory, in bytes.")},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000217 {NULL, NULL, 0, NULL}
218};
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000219
Fred Drake268397f1998-04-29 14:16:32 +0000220static
Fred Drakec2683dd2001-07-17 19:32:05 +0000221PyTypeObject PyST_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000222 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000223 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000224 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000225 0, /* tp_itemsize */
226 (destructor)parser_free, /* tp_dealloc */
227 0, /* tp_print */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000228 0, /* tp_getattr */
Fred Drakeff9ea482000-04-19 13:54:15 +0000229 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000230 0, /* tp_reserved */
Fred Drakeff9ea482000-04-19 13:54:15 +0000231 0, /* tp_repr */
232 0, /* tp_as_number */
233 0, /* tp_as_sequence */
234 0, /* tp_as_mapping */
235 0, /* tp_hash */
236 0, /* tp_call */
237 0, /* tp_str */
238 0, /* tp_getattro */
239 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000240
241 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000242 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000243
Fred Drakeff9ea482000-04-19 13:54:15 +0000244 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000245
246 /* __doc__ */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000247 "Intermediate representation of a Python parse tree.",
248 0, /* tp_traverse */
249 0, /* tp_clear */
Mark Dickinson211c6252009-02-01 10:28:51 +0000250 parser_richcompare, /* tp_richcompare */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000251 0, /* tp_weaklistoffset */
252 0, /* tp_iter */
253 0, /* tp_iternext */
254 parser_methods, /* tp_methods */
Fred Drakec2683dd2001-07-17 19:32:05 +0000255}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000256
257
Mark Dickinson211c6252009-02-01 10:28:51 +0000258/* PyST_Type isn't subclassable, so just check ob_type */
259#define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type)
260
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000261static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000262parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000263{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000264 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000265
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000266 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000267 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000268
269 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000270 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000271
272 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000273 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000274
275 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000276 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000277
278 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000279 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000280
281 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000282 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000283
Fred Drakeff9ea482000-04-19 13:54:15 +0000284 if (v != 0)
285 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000286 }
287 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000288}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000289
Mark Dickinson211c6252009-02-01 10:28:51 +0000290/* parser_richcompare(PyObject* left, PyObject* right, int op)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000291 *
292 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
293 * This really just wraps a call to parser_compare_nodes() with some easy
294 * checks and protection code.
295 *
296 */
Mark Dickinson211c6252009-02-01 10:28:51 +0000297
Mark Dickinson211c6252009-02-01 10:28:51 +0000298static PyObject *
299parser_richcompare(PyObject *left, PyObject *right, int op)
Guido van Rossum47478871996-08-21 14:32:37 +0000300{
Mark Dickinson211c6252009-02-01 10:28:51 +0000301 int result;
Mark Dickinson211c6252009-02-01 10:28:51 +0000302
303 /* neither argument should be NULL, unless something's gone wrong */
304 if (left == NULL || right == NULL) {
305 PyErr_BadInternalCall();
306 return NULL;
307 }
308
309 /* both arguments should be instances of PyST_Object */
310 if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) {
stratakise8b19652017-11-02 11:32:54 +0100311 Py_RETURN_NOTIMPLEMENTED;
Mark Dickinson211c6252009-02-01 10:28:51 +0000312 }
313
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000314 if (left == right)
Mark Dickinson211c6252009-02-01 10:28:51 +0000315 /* if arguments are identical, they're equal */
316 result = 0;
317 else
318 result = parser_compare_nodes(((PyST_Object *)left)->st_node,
319 ((PyST_Object *)right)->st_node);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000320
stratakise8b19652017-11-02 11:32:54 +0100321 Py_RETURN_RICHCOMPARE(result, 0, op);
Fred Drakeff9ea482000-04-19 13:54:15 +0000322}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000323
Fred Drakec2683dd2001-07-17 19:32:05 +0000324/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000325 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000326 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000327 * 'wrapper' object that holds a node* and allows it to be passed around in
328 * Python code.
329 *
330 */
331static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000332parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000333{
Fred Drakec2683dd2001-07-17 19:32:05 +0000334 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000335
336 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000337 o->st_node = st;
338 o->st_type = type;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000339 o->st_flags.cf_flags = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000340 }
Fred Drake268397f1998-04-29 14:16:32 +0000341 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000342 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000343 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000344 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000345}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000346
347
Fred Drakec2683dd2001-07-17 19:32:05 +0000348/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000349 *
350 * This is called by a del statement that reduces the reference count to 0.
351 *
352 */
353static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000354parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000355{
Fred Drakec2683dd2001-07-17 19:32:05 +0000356 PyNode_Free(st->st_node);
357 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000358}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000359
Jesus Ceae9c53182012-08-03 14:28:37 +0200360static PyObject *
361parser_sizeof(PyST_Object *st, void *unused)
362{
363 Py_ssize_t res;
364
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +0200365 res = _PyObject_SIZE(Py_TYPE(st)) + _PyNode_SizeOf(st->st_node);
Jesus Ceae9c53182012-08-03 14:28:37 +0200366 return PyLong_FromSsize_t(res);
367}
368
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000369
Fred Drakec2683dd2001-07-17 19:32:05 +0000370/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000371 *
372 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000373 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000374 *
375 */
376static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000377parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000378{
Antoine Pitrou721738f2012-08-15 23:20:39 +0200379 int line_info = 0;
380 int col_info = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000381 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000382 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000383
Georg Brandl30704ea02008-07-23 15:07:12 +0000384 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000385
Martin v. Löwis1a214512008-06-11 05:26:20 +0000386 if (self == NULL || PyModule_Check(self)) {
Antoine Pitrou721738f2012-08-15 23:20:39 +0200387 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2tuple", keywords,
388 &PyST_Type, &self, &line_info,
389 &col_info);
Fred Drake268397f1998-04-29 14:16:32 +0000390 }
Fred Drake503d8d61998-04-13 18:45:18 +0000391 else
Antoine Pitrou721738f2012-08-15 23:20:39 +0200392 ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:totuple", &keywords[1],
393 &line_info, &col_info);
Fred Drake268397f1998-04-29 14:16:32 +0000394 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000395 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000396 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000397 * since it's known to work already.
398 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000399 res = node2tuple(((PyST_Object*)self)->st_node,
Antoine Pitrou721738f2012-08-15 23:20:39 +0200400 PyTuple_New, PyTuple_SetItem, line_info, col_info);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000401 }
402 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000403}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000404
405
Fred Drakec2683dd2001-07-17 19:32:05 +0000406/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000407 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000408 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000409 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000410 *
411 */
412static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000413parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000414{
Antoine Pitrou721738f2012-08-15 23:20:39 +0200415 int line_info = 0;
416 int col_info = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000417 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000418 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000419
Georg Brandl30704ea02008-07-23 15:07:12 +0000420 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000421
Martin v. Löwis1a214512008-06-11 05:26:20 +0000422 if (self == NULL || PyModule_Check(self))
Antoine Pitrou721738f2012-08-15 23:20:39 +0200423 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2list", keywords,
424 &PyST_Type, &self, &line_info,
425 &col_info);
Fred Drake503d8d61998-04-13 18:45:18 +0000426 else
Antoine Pitrou721738f2012-08-15 23:20:39 +0200427 ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:tolist", &keywords[1],
428 &line_info, &col_info);
Fred Drake503d8d61998-04-13 18:45:18 +0000429 if (ok) {
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(self->st_node,
Antoine Pitrou721738f2012-08-15 23:20:39 +0200435 PyList_New, PyList_SetItem, line_info, col_info);
Guido van Rossum47478871996-08-21 14:32:37 +0000436 }
437 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000438}
Guido van Rossum47478871996-08-21 14:32:37 +0000439
440
Fred Drakec2683dd2001-07-17 19:32:05 +0000441/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000442 *
443 * This function creates code objects from the parse tree represented by
444 * the passed-in data object. An optional file name is passed in as well.
445 *
446 */
447static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000448parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000449{
Victor Stinner14e461d2013-08-26 22:28:21 +0200450 PyObject* res = NULL;
451 PyArena* arena = NULL;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000452 mod_ty mod;
Victor Stinner14e461d2013-08-26 22:28:21 +0200453 PyObject* filename = NULL;
Fred Drake503d8d61998-04-13 18:45:18 +0000454 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000455
Georg Brandl30704ea02008-07-23 15:07:12 +0000456 static char *keywords[] = {"st", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000457
Martin v. Löwis1a214512008-06-11 05:26:20 +0000458 if (self == NULL || PyModule_Check(self))
Victor Stinner14e461d2013-08-26 22:28:21 +0200459 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O&:compilest", keywords,
460 &PyST_Type, &self,
461 PyUnicode_FSDecoder, &filename);
Fred Drake503d8d61998-04-13 18:45:18 +0000462 else
Victor Stinner14e461d2013-08-26 22:28:21 +0200463 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O&:compile", &keywords[1],
464 PyUnicode_FSDecoder, &filename);
465 if (!ok)
466 goto error;
Fred Drake503d8d61998-04-13 18:45:18 +0000467
Victor Stinner14e461d2013-08-26 22:28:21 +0200468 if (filename == NULL) {
469 filename = PyUnicode_FromString("<syntax-tree>");
470 if (filename == NULL)
471 goto error;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000472 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000473
Victor Stinner14e461d2013-08-26 22:28:21 +0200474 arena = PyArena_New();
475 if (!arena)
476 goto error;
477
478 mod = PyAST_FromNodeObject(self->st_node, &self->st_flags,
479 filename, arena);
480 if (!mod)
481 goto error;
482
483 res = (PyObject *)PyAST_CompileObject(mod, filename,
484 &self->st_flags, -1, arena);
485error:
486 Py_XDECREF(filename);
487 if (arena != NULL)
488 PyArena_Free(arena);
489 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +0000490}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000491
492
493/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
494 * PyObject* parser_issuite(PyObject* self, PyObject* args)
495 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000496 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000497 * a statement suite, respectively. The return is a Python truth value.
498 *
499 */
500static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000501parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000502{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000503 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000504 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000505
Georg Brandl30704ea02008-07-23 15:07:12 +0000506 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000507
Martin v. Löwis1a214512008-06-11 05:26:20 +0000508 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000509 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000510 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000511 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000512 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000513
514 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000515 /* Check to see if the ST represents an expression or not. */
516 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000517 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000518 }
519 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000520}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000521
522
523static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000524parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000525{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000526 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000527 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000528
Georg Brandl30704ea02008-07-23 15:07:12 +0000529 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000530
Martin v. Löwis1a214512008-06-11 05:26:20 +0000531 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000532 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000533 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000534 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000535 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000536
537 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000538 /* Check to see if the ST represents an expression or not. */
539 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000540 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000541 }
542 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000543}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000544
545
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200546/* err_string(const char* message)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000547 *
548 * Sets the error string for an exception of type ParserError.
549 *
550 */
551static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200552err_string(const char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000553{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000554 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000555}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000556
557
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000558/* PyObject* parser_do_parse(PyObject* args, int type)
559 *
560 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000561 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000562 *
563 */
564static PyObject*
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200565parser_do_parse(PyObject *args, PyObject *kw, const char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000566{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000567 char* string = 0;
568 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000569 int flags = 0;
570 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000571
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000572 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000573
574 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000575 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
576 &_PyParser_Grammar,
577 (type == PyST_EXPR)
578 ? eval_input : file_input,
579 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 if (n) {
582 res = parser_newstobject(n, type);
Benjamin Petersonf216c942008-10-31 02:28:05 +0000583 if (res)
584 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
585 }
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500586 else {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000587 PyParser_SetError(&err);
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500588 }
Benjamin Petersonf0cdbad2011-06-05 22:14:05 -0500589 PyParser_ClearError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000590 }
591 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000592}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000593
594
595/* PyObject* parser_expr(PyObject* self, PyObject* args)
596 * PyObject* parser_suite(PyObject* self, PyObject* args)
597 *
598 * External interfaces to the parser itself. Which is called determines if
599 * the parser attempts to recognize an expression ('eval' form) or statement
600 * suite ('exec' form). The real work is done by parser_do_parse() above.
601 *
602 */
603static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000604parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000605{
Fred Drake268397f1998-04-29 14:16:32 +0000606 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000607 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000608}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000609
610
611static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000612parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000613{
Fred Drake268397f1998-04-29 14:16:32 +0000614 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000615 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000616}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000617
618
619
Fred Drakec2683dd2001-07-17 19:32:05 +0000620/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000621 * object requires that the input tuple be valid without having to rely on
622 * catching an exception from the compiler. This is done to allow the
623 * compiler itself to remain fast, since most of its input will come from
624 * the parser directly, and therefore be known to be syntactically correct.
625 * This validation is done to ensure that we don't core dump the compile
626 * phase, returning an exception instead.
627 *
628 * Two aspects can be broken out in this code: creating a node tree from
629 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000630 * advantageous to expand the number of ST types to include funcdefs and
631 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000632 * here. They are not necessary, and not quite as useful in a raw form.
633 * For now, let's get expressions and suites working reliably.
634 */
635
636
Jeremy Hylton938ace62002-07-17 16:30:39 +0000637static node* build_node_tree(PyObject *tuple);
Benjamin Peterson53595c42016-06-02 11:30:18 -0700638
639static int
640validate_node(node *tree)
641{
642 int type = TYPE(tree);
643 int nch = NCH(tree);
644 dfa *nt_dfa;
645 state *dfa_state;
646 int pos, arc;
647
648 assert(ISNONTERMINAL(type));
649 type -= NT_OFFSET;
650 if (type >= _PyParser_Grammar.g_ndfas) {
651 PyErr_Format(parser_error, "Unrecognized node type %d.", TYPE(tree));
652 return 0;
653 }
654 nt_dfa = &_PyParser_Grammar.g_dfa[type];
655 REQ(tree, nt_dfa->d_type);
656
657 /* Run the DFA for this nonterminal. */
658 dfa_state = &nt_dfa->d_state[nt_dfa->d_initial];
659 for (pos = 0; pos < nch; ++pos) {
660 node *ch = CHILD(tree, pos);
661 int ch_type = TYPE(ch);
Pablo Galindo513d1422019-04-03 14:34:59 -0400662 if ((ch_type >= NT_OFFSET + _PyParser_Grammar.g_ndfas)
663 || (ISTERMINAL(ch_type) && (ch_type >= N_TOKENS))
664 || (ch_type < 0)
665 ) {
666 PyErr_Format(parser_error, "Unrecognized node type %d.", ch_type);
667 return 0;
668 }
Benjamin Peterson53595c42016-06-02 11:30:18 -0700669 for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
670 short a_label = dfa_state->s_arc[arc].a_lbl;
671 assert(a_label < _PyParser_Grammar.g_ll.ll_nlabels);
Miss Islington (bot)00eb97b2019-03-21 16:56:20 -0700672
673 const char *label_str = _PyParser_Grammar.g_ll.ll_label[a_label].lb_str;
674 if ((_PyParser_Grammar.g_ll.ll_label[a_label].lb_type == ch_type)
675 && ((ch->n_str == NULL) || (label_str == NULL)
676 || (strcmp(ch->n_str, label_str) == 0))
677 ) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300678 /* The child is acceptable; if non-terminal, validate it recursively. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700679 if (ISNONTERMINAL(ch_type) && !validate_node(ch))
680 return 0;
681
682 /* Update the state, and move on to the next child. */
683 dfa_state = &nt_dfa->d_state[dfa_state->s_arc[arc].a_arrow];
684 goto arc_found;
685 }
686 }
687 /* What would this state have accepted? */
688 {
689 short a_label = dfa_state->s_arc->a_lbl;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700690 if (!a_label) /* Wouldn't accept any more children */
691 goto illegal_num_children;
692
Miss Islington (bot)00eb97b2019-03-21 16:56:20 -0700693 int next_type = _PyParser_Grammar.g_ll.ll_label[a_label].lb_type;
694 const char *expected_str = _PyParser_Grammar.g_ll.ll_label[a_label].lb_str;
695
696 if (ISNONTERMINAL(next_type)) {
Pablo Galindo513d1422019-04-03 14:34:59 -0400697 PyErr_Format(parser_error, "Expected %s, got %s.",
698 _PyParser_Grammar.g_dfa[next_type - NT_OFFSET].d_name,
699 ISTERMINAL(ch_type) ? _PyParser_TokenNames[ch_type] :
700 _PyParser_Grammar.g_dfa[ch_type - NT_OFFSET].d_name);
Miss Islington (bot)00eb97b2019-03-21 16:56:20 -0700701 }
702 else if (expected_str != NULL) {
703 PyErr_Format(parser_error, "Illegal terminal: expected '%s'.",
704 expected_str);
705 }
706 else {
Benjamin Peterson53595c42016-06-02 11:30:18 -0700707 PyErr_Format(parser_error, "Illegal terminal: expected %s.",
708 _PyParser_TokenNames[next_type]);
Miss Islington (bot)00eb97b2019-03-21 16:56:20 -0700709 }
Benjamin Peterson53595c42016-06-02 11:30:18 -0700710 return 0;
711 }
712
713arc_found:
714 continue;
715 }
716 /* Are we in a final state? If so, return 1 for successful validation. */
717 for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
718 if (!dfa_state->s_arc[arc].a_lbl) {
719 return 1;
720 }
721 }
722
723illegal_num_children:
724 PyErr_Format(parser_error,
725 "Illegal number of children for %s node.", nt_dfa->d_name);
726 return 0;
727}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000728
Fred Drakec2683dd2001-07-17 19:32:05 +0000729/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000730 *
731 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000732 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000733 * tuple can be validated. It does this by checking the first code of the
734 * tuple, and, if acceptable, builds the internal representation. If this
735 * step succeeds, the internal representation is validated as fully as
Benjamin Peterson53595c42016-06-02 11:30:18 -0700736 * possible with the recursive validate_node() routine defined above.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000737 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000738 * This function must be changed if support is to be added for PyST_FRAGMENT
739 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000740 *
741 */
742static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000743parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000744{
Fred Drake268397f1998-04-29 14:16:32 +0000745 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000746 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000747 PyObject *tuple;
748 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000749
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000750 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000751
Fred Drakec2683dd2001-07-17 19:32:05 +0000752 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000753 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000754 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000755 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000756 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000757 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000758 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000759 }
760 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000761 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000762 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000763 tree = build_node_tree(tuple);
764 if (tree != 0) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300765 node *validation_root = NULL;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700766 int tree_type = 0;
767 switch (TYPE(tree)) {
768 case eval_input:
Fred Drake0ac9b072000-09-12 21:58:06 +0000769 /* Might be an eval form. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700770 tree_type = PyST_EXPR;
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300771 validation_root = tree;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700772 break;
773 case encoding_decl:
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000774 /* This looks like an encoding_decl so far. */
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300775 if (NCH(tree) == 1) {
776 tree_type = PyST_SUITE;
777 validation_root = CHILD(tree, 0);
778 }
779 else {
Benjamin Peterson53595c42016-06-02 11:30:18 -0700780 err_string("Error Parsing encoding_decl");
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300781 }
782 break;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700783 case file_input:
784 /* This looks like an exec form so far. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700785 tree_type = PyST_SUITE;
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300786 validation_root = tree;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700787 break;
788 default:
Fred Drake0ac9b072000-09-12 21:58:06 +0000789 /* This is a fragment, at best. */
Fred Drake661ea262000-10-24 19:57:45 +0000790 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000791 }
Benjamin Peterson53595c42016-06-02 11:30:18 -0700792
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300793 if (validation_root != NULL && validate_node(validation_root))
Benjamin Peterson53595c42016-06-02 11:30:18 -0700794 st = parser_newstobject(tree, tree_type);
795 else
796 PyNode_Free(tree);
Guido van Rossum47478871996-08-21 14:32:37 +0000797 }
Andrew Svetlov737fb892012-12-18 21:14:22 +0200798 /* Make sure we raise an exception on all errors. We should never
Guido van Rossum47478871996-08-21 14:32:37 +0000799 * get this, but we'd do well to be sure something is done.
800 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000801 if (st == NULL && !PyErr_Occurred())
802 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000803
Fred Drakec2683dd2001-07-17 19:32:05 +0000804 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000805}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000806
807
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000808/* node* build_node_children()
809 *
810 * Iterate across the children of the current non-terminal node and build
811 * their structures. If successful, return the root of this portion of
812 * the tree, otherwise, 0. Any required exception will be specified already,
813 * and no memory will have been deallocated.
814 *
815 */
816static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000817build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000818{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000819 Py_ssize_t len = PyObject_Size(tuple);
820 Py_ssize_t i;
821 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000822
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300823 if (len < 0) {
824 return NULL;
825 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000826 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000827 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000828 PyObject* elem = PySequence_GetItem(tuple, i);
829 int ok = elem != NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200830 int type = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000831 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000832
Fred Drakeff9ea482000-04-19 13:54:15 +0000833 if (ok)
834 ok = PySequence_Check(elem);
835 if (ok) {
836 PyObject *temp = PySequence_GetItem(elem, 0);
837 if (temp == NULL)
838 ok = 0;
839 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000840 ok = PyLong_Check(temp);
Serhiy Storchaka78980432013-01-15 01:12:17 +0200841 if (ok) {
842 type = _PyLong_AsInt(temp);
843 if (type == -1 && PyErr_Occurred()) {
844 Py_DECREF(temp);
845 Py_DECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300846 return NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200847 }
848 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000849 Py_DECREF(temp);
850 }
851 }
852 if (!ok) {
Victor Stinner5f8d4852014-01-02 11:49:27 +0100853 PyObject *err = Py_BuildValue("Os", elem,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000854 "Illegal node construct.");
855 PyErr_SetObject(parser_error, err);
856 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000857 Py_XDECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300858 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000859 }
860 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000861 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000862 PyObject *temp;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000863 const char *temp_str;
Guido van Rossum47478871996-08-21 14:32:37 +0000864
Fred Drake0ac9b072000-09-12 21:58:06 +0000865 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000866 err_string("terminal nodes must have 2 or 3 entries");
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300867 Py_DECREF(elem);
868 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000869 }
870 temp = PySequence_GetItem(elem, 1);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300871 if (temp == NULL) {
872 Py_DECREF(elem);
873 return NULL;
874 }
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000875 if (!PyUnicode_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000876 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000877 "second item in terminal node must be a string,"
878 " found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000879 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000880 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000881 Py_DECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300882 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000883 }
884 if (len == 3) {
885 PyObject *o = PySequence_GetItem(elem, 2);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300886 if (o == NULL) {
887 Py_DECREF(temp);
888 Py_DECREF(elem);
889 return NULL;
890 }
891 if (PyLong_Check(o)) {
892 int num = _PyLong_AsInt(o);
893 if (num == -1 && PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000894 Py_DECREF(o);
895 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000896 Py_DECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300897 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000898 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300899 *line_num = num;
Fred Drakeff9ea482000-04-19 13:54:15 +0000900 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300901 else {
902 PyErr_Format(parser_error,
903 "third item in terminal node must be an"
904 " integer, found %s",
905 Py_TYPE(temp)->tp_name);
906 Py_DECREF(o);
907 Py_DECREF(temp);
908 Py_DECREF(elem);
909 return NULL;
910 }
911 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000912 }
Serhiy Storchaka06515832016-11-20 09:13:07 +0200913 temp_str = PyUnicode_AsUTF8AndSize(temp, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000914 if (temp_str == NULL) {
915 Py_DECREF(temp);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300916 Py_DECREF(elem);
917 return NULL;
Alexander Belopolskye239d232010-12-08 23:31:48 +0000918 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000919 strn = (char *)PyObject_MALLOC(len + 1);
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200920 if (strn == NULL) {
921 Py_DECREF(temp);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300922 Py_DECREF(elem);
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200923 PyErr_NoMemory();
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300924 return NULL;
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200925 }
926 (void) memcpy(strn, temp_str, len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000927 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000928 }
929 else if (!ISNONTERMINAL(type)) {
930 /*
931 * It has to be one or the other; this is an error.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200932 * Raise an exception.
Fred Drakeff9ea482000-04-19 13:54:15 +0000933 */
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300934 PyObject *err = Py_BuildValue("Os", elem, "unknown node type.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000935 PyErr_SetObject(parser_error, err);
936 Py_XDECREF(err);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300937 Py_DECREF(elem);
938 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000939 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000940 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000941 if (err == E_NOMEM) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300942 Py_DECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000943 PyObject_FREE(strn);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300944 PyErr_NoMemory();
945 return NULL;
Fred Drake8b55b2d2001-12-05 22:10:44 +0000946 }
947 if (err == E_OVERFLOW) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300948 Py_DECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000949 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000950 PyErr_SetString(PyExc_ValueError,
951 "unsupported number of child nodes");
952 return NULL;
953 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000954
Fred Drakeff9ea482000-04-19 13:54:15 +0000955 if (ISNONTERMINAL(type)) {
956 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000957
Fred Drakeff9ea482000-04-19 13:54:15 +0000958 if (new_child != build_node_children(elem, new_child, line_num)) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300959 Py_DECREF(elem);
960 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000961 }
962 }
963 else if (type == NEWLINE) { /* It's true: we increment the */
964 ++(*line_num); /* line number *after* the newline! */
965 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300966 Py_DECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000967 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000968 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000969}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000970
971
972static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000973build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000974{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000975 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000976 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000977 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000978
Guido van Rossum47478871996-08-21 14:32:37 +0000979 if (temp != NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000980 num = PyLong_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000981 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000982 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000983 /*
984 * The tuple is simple, but it doesn't start with a start symbol.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200985 * Raise an exception now and be done with it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000986 */
Victor Stinner6684bdf2013-07-17 00:13:52 +0200987 tuple = Py_BuildValue("Os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000988 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000989 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000990 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000991 }
992 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000993 /*
994 * Not efficient, but that can be handled later.
995 */
996 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000997 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000998
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000999 if (num == encoding_decl) {
1000 encoding = PySequence_GetItem(tuple, 2);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001001 if (encoding == NULL) {
1002 PyErr_SetString(parser_error, "missed encoding");
1003 return NULL;
1004 }
1005 if (!PyUnicode_Check(encoding)) {
1006 PyErr_Format(parser_error,
1007 "encoding must be a string, found %.200s",
1008 Py_TYPE(encoding)->tp_name);
1009 Py_DECREF(encoding);
1010 return NULL;
1011 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001012 /* tuple isn't borrowed anymore here, need to DECREF */
1013 tuple = PySequence_GetSlice(tuple, 0, 2);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001014 if (tuple == NULL) {
1015 Py_DECREF(encoding);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001016 return NULL;
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001017 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001018 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001019 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +00001020 if (res != NULL) {
1021 if (res != build_node_children(tuple, res, &line_num)) {
1022 PyNode_Free(res);
1023 res = NULL;
1024 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001025 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +00001026 Py_ssize_t len;
Neal Norwitz3fcbea52007-08-26 04:51:28 +00001027 const char *temp;
Serhiy Storchaka06515832016-11-20 09:13:07 +02001028 temp = PyUnicode_AsUTF8AndSize(encoding, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001029 if (temp == NULL) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001030 PyNode_Free(res);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001031 Py_DECREF(encoding);
1032 Py_DECREF(tuple);
1033 return NULL;
1034 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00001035 res->n_str = (char *)PyObject_MALLOC(len + 1);
Victor Stinner3bd6abd2013-07-12 01:33:59 +02001036 if (res->n_str == NULL) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001037 PyNode_Free(res);
Victor Stinner3bd6abd2013-07-12 01:33:59 +02001038 Py_DECREF(encoding);
1039 Py_DECREF(tuple);
1040 PyErr_NoMemory();
1041 return NULL;
1042 }
1043 (void) memcpy(res->n_str, temp, len + 1);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001044 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001045 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001046 if (encoding != NULL) {
1047 Py_DECREF(encoding);
1048 Py_DECREF(tuple);
1049 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001050 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001051 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001052 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +00001053 * NONTERMINAL, we can't use it. Not sure the implementation
1054 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +00001055 */
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001056 PyObject *err = Py_BuildValue("Os", tuple,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001057 "Illegal component tuple.");
1058 PyErr_SetObject(parser_error, err);
1059 Py_XDECREF(err);
1060 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001061
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001062 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001063}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001064
1065
Fred Drake43f8f9b1998-04-13 16:25:46 +00001066static PyObject*
1067pickle_constructor = NULL;
1068
1069
1070static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00001071parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00001072{
Fred Drake268397f1998-04-29 14:16:32 +00001073 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00001074 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00001075 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00001076 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001077
Fred Drakec2683dd2001-07-17 19:32:05 +00001078 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001079 PyObject *newargs;
1080 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001081
Fred Drake2a6875e1999-09-20 22:32:18 +00001082 if ((empty_dict = PyDict_New()) == NULL)
1083 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00001084 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00001085 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00001086 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00001087 if (tuple != NULL) {
1088 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
1089 Py_DECREF(tuple);
1090 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001091 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001092 }
1093 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00001094 Py_XDECREF(empty_dict);
1095
Fred Drake43f8f9b1998-04-13 16:25:46 +00001096 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00001097}
Fred Drake43f8f9b1998-04-13 16:25:46 +00001098
1099
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001100/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00001101 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001102 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00001103 * We'd really have to write a wrapper around it all anyway to allow
1104 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001105 */
1106static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00001107 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001108 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001109 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001110 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001111 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001112 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001113 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001114 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001115 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001116 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001117 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001118 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001119 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001120 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001121 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001122 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001123 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001124 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001125
Fred Drake43f8f9b1998-04-13 16:25:46 +00001126 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00001127 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00001128 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00001129
Fred Drake268397f1998-04-29 14:16:32 +00001130 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001131 };
1132
1133
Martin v. Löwis1a214512008-06-11 05:26:20 +00001134
1135static struct PyModuleDef parsermodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 PyModuleDef_HEAD_INIT,
1137 "parser",
1138 NULL,
1139 -1,
1140 parser_functions,
1141 NULL,
1142 NULL,
1143 NULL,
1144 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001145};
1146
1147PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00001148
Mark Hammond62b1ab12002-07-23 06:31:15 +00001149PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001150PyInit_parser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00001151{
Fred Drake13130bc2001-08-15 16:44:56 +00001152 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00001153
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001154 if (PyType_Ready(&PyST_Type) < 0)
1155 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001156 module = PyModule_Create(&parsermodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001157 if (module == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 return NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001159
Fred Drake7a15ba51999-09-09 14:21:52 +00001160 if (parser_error == 0)
1161 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001162
Tim Peters6a627252003-07-21 14:25:23 +00001163 if (parser_error == 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001164 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00001165 /* CAUTION: The code next used to skip bumping the refcount on
Martin v. Löwis1a214512008-06-11 05:26:20 +00001166 * parser_error. That's a disaster if PyInit_parser() gets called more
Tim Peters6a627252003-07-21 14:25:23 +00001167 * than once. By incref'ing, we ensure that each module dict that
1168 * gets created owns its reference to the shared parser_error object,
1169 * and the file static parser_error vrbl owns a reference too.
1170 */
1171 Py_INCREF(parser_error);
1172 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001173 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00001174
Fred Drakec2683dd2001-07-17 19:32:05 +00001175 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00001176 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001177
Fred Drake13130bc2001-08-15 16:44:56 +00001178 PyModule_AddStringConstant(module, "__copyright__",
1179 parser_copyright_string);
1180 PyModule_AddStringConstant(module, "__doc__",
1181 parser_doc_string);
1182 PyModule_AddStringConstant(module, "__version__",
1183 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001184
Fred Drake78bdb9b2001-07-19 20:17:15 +00001185 /* Register to support pickling.
1186 * If this fails, the import of this module will fail because an
1187 * exception will be raised here; should we clear the exception?
1188 */
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00001189 copyreg = PyImport_ImportModuleNoBlock("copyreg");
Fred Drake13130bc2001-08-15 16:44:56 +00001190 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001191 PyObject *func, *pickler;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001192 _Py_IDENTIFIER(pickle);
1193 _Py_IDENTIFIER(sequence2st);
1194 _Py_IDENTIFIER(_pickler);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001195
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001196 func = _PyObject_GetAttrId(copyreg, &PyId_pickle);
1197 pickle_constructor = _PyObject_GetAttrId(module, &PyId_sequence2st);
1198 pickler = _PyObject_GetAttrId(module, &PyId__pickler);
Fred Drakeff9ea482000-04-19 13:54:15 +00001199 Py_XINCREF(pickle_constructor);
1200 if ((func != NULL) && (pickle_constructor != NULL)
1201 && (pickler != NULL)) {
1202 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001203
Thomas Wouters477c8d52006-05-27 19:21:47 +00001204 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
1205 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00001206 Py_XDECREF(res);
1207 }
1208 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00001209 Py_XDECREF(pickle_constructor);
1210 Py_XDECREF(pickler);
1211 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001212 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00001213 return module;
Fred Drakeff9ea482000-04-19 13:54:15 +00001214}