blob: a215c7ecacd73b8472861c7468e7b7b71e3a5709 [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 */
Victor Stinner3bb183d2018-11-22 18:38:38 +010031#undef Yield /* undefine macro conflicting with <winbase.h> */
Victor Stinner5f2df882018-11-12 00:56:19 +010032#include "ast.h"
Fred Drakeff9ea482000-04-19 13:54:15 +000033#include "graminit.h" /* symbols defined in the grammar */
34#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000035#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000036#include "token.h" /* token definitions */
Victor Stinner5f2df882018-11-12 00:56:19 +010037 /* ISTERMINAL() / ISNONTERMINAL() */
Benjamin Petersonf216c942008-10-31 02:28:05 +000038#include "grammar.h"
39#include "parsetok.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
Serhiy Storchakae5362ea2018-04-19 01:55:37 +0300134 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
Serhiy Storchakae5362ea2018-04-19 01:55:37 +0300141 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;
Serhiy Storchakae5362ea2018-04-19 01:55:37 +0300145 (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[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200205 {"compile", (PyCFunction)(void(*)(void))parser_compilest, PUBLIC_METHOD_TYPE,
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000206 PyDoc_STR("Compile this ST object into a code object.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200207 {"isexpr", (PyCFunction)(void(*)(void))parser_isexpr, PUBLIC_METHOD_TYPE,
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000208 PyDoc_STR("Determines if this ST object was created from an expression.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200209 {"issuite", (PyCFunction)(void(*)(void))parser_issuite, PUBLIC_METHOD_TYPE,
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000210 PyDoc_STR("Determines if this ST object was created from a suite.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200211 {"tolist", (PyCFunction)(void(*)(void))parser_st2list, PUBLIC_METHOD_TYPE,
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000212 PyDoc_STR("Creates a list-tree representation of this ST.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200213 {"totuple", (PyCFunction)(void(*)(void))parser_st2tuple, PUBLIC_METHOD_TYPE,
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000214 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 Rossum495da292019-03-07 12:38:08 -0800340 o->st_flags.cf_feature_version = PY_MINOR_VERSION;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000341 }
Fred Drake268397f1998-04-29 14:16:32 +0000342 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000343 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000344 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000345 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000346}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000347
348
Fred Drakec2683dd2001-07-17 19:32:05 +0000349/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000350 *
351 * This is called by a del statement that reduces the reference count to 0.
352 *
353 */
354static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000355parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000356{
Fred Drakec2683dd2001-07-17 19:32:05 +0000357 PyNode_Free(st->st_node);
358 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000359}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000360
Jesus Ceae9c53182012-08-03 14:28:37 +0200361static PyObject *
362parser_sizeof(PyST_Object *st, void *unused)
363{
364 Py_ssize_t res;
365
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +0200366 res = _PyObject_SIZE(Py_TYPE(st)) + _PyNode_SizeOf(st->st_node);
Jesus Ceae9c53182012-08-03 14:28:37 +0200367 return PyLong_FromSsize_t(res);
368}
369
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000370
Fred Drakec2683dd2001-07-17 19:32:05 +0000371/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000372 *
373 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000374 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000375 *
376 */
377static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000378parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000379{
Antoine Pitrou721738f2012-08-15 23:20:39 +0200380 int line_info = 0;
381 int col_info = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000382 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000383 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000384
Georg Brandl30704ea02008-07-23 15:07:12 +0000385 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000386
Martin v. Löwis1a214512008-06-11 05:26:20 +0000387 if (self == NULL || PyModule_Check(self)) {
Antoine Pitrou721738f2012-08-15 23:20:39 +0200388 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2tuple", keywords,
389 &PyST_Type, &self, &line_info,
390 &col_info);
Fred Drake268397f1998-04-29 14:16:32 +0000391 }
Fred Drake503d8d61998-04-13 18:45:18 +0000392 else
Antoine Pitrou721738f2012-08-15 23:20:39 +0200393 ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:totuple", &keywords[1],
394 &line_info, &col_info);
Fred Drake268397f1998-04-29 14:16:32 +0000395 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000396 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000397 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000398 * since it's known to work already.
399 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000400 res = node2tuple(((PyST_Object*)self)->st_node,
Antoine Pitrou721738f2012-08-15 23:20:39 +0200401 PyTuple_New, PyTuple_SetItem, line_info, col_info);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000402 }
403 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000404}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000405
406
Fred Drakec2683dd2001-07-17 19:32:05 +0000407/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000408 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000409 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000410 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000411 *
412 */
413static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000414parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000415{
Antoine Pitrou721738f2012-08-15 23:20:39 +0200416 int line_info = 0;
417 int col_info = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000418 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000419 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000420
Georg Brandl30704ea02008-07-23 15:07:12 +0000421 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000422
Martin v. Löwis1a214512008-06-11 05:26:20 +0000423 if (self == NULL || PyModule_Check(self))
Antoine Pitrou721738f2012-08-15 23:20:39 +0200424 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2list", keywords,
425 &PyST_Type, &self, &line_info,
426 &col_info);
Fred Drake503d8d61998-04-13 18:45:18 +0000427 else
Antoine Pitrou721738f2012-08-15 23:20:39 +0200428 ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:tolist", &keywords[1],
429 &line_info, &col_info);
Fred Drake503d8d61998-04-13 18:45:18 +0000430 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000431 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000432 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000433 * since it's known to work already.
434 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000435 res = node2tuple(self->st_node,
Antoine Pitrou721738f2012-08-15 23:20:39 +0200436 PyList_New, PyList_SetItem, line_info, col_info);
Guido van Rossum47478871996-08-21 14:32:37 +0000437 }
438 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000439}
Guido van Rossum47478871996-08-21 14:32:37 +0000440
441
Fred Drakec2683dd2001-07-17 19:32:05 +0000442/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000443 *
444 * This function creates code objects from the parse tree represented by
445 * the passed-in data object. An optional file name is passed in as well.
446 *
447 */
448static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000449parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000450{
Victor Stinner14e461d2013-08-26 22:28:21 +0200451 PyObject* res = NULL;
452 PyArena* arena = NULL;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000453 mod_ty mod;
Victor Stinner14e461d2013-08-26 22:28:21 +0200454 PyObject* filename = NULL;
Fred Drake503d8d61998-04-13 18:45:18 +0000455 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000456
Georg Brandl30704ea02008-07-23 15:07:12 +0000457 static char *keywords[] = {"st", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000458
Martin v. Löwis1a214512008-06-11 05:26:20 +0000459 if (self == NULL || PyModule_Check(self))
Victor Stinner14e461d2013-08-26 22:28:21 +0200460 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O&:compilest", keywords,
461 &PyST_Type, &self,
462 PyUnicode_FSDecoder, &filename);
Fred Drake503d8d61998-04-13 18:45:18 +0000463 else
Victor Stinner14e461d2013-08-26 22:28:21 +0200464 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O&:compile", &keywords[1],
465 PyUnicode_FSDecoder, &filename);
466 if (!ok)
467 goto error;
Fred Drake503d8d61998-04-13 18:45:18 +0000468
Victor Stinner14e461d2013-08-26 22:28:21 +0200469 if (filename == NULL) {
470 filename = PyUnicode_FromString("<syntax-tree>");
471 if (filename == NULL)
472 goto error;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000473 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000474
Victor Stinner14e461d2013-08-26 22:28:21 +0200475 arena = PyArena_New();
476 if (!arena)
477 goto error;
478
479 mod = PyAST_FromNodeObject(self->st_node, &self->st_flags,
480 filename, arena);
481 if (!mod)
482 goto error;
483
484 res = (PyObject *)PyAST_CompileObject(mod, filename,
485 &self->st_flags, -1, arena);
486error:
487 Py_XDECREF(filename);
488 if (arena != NULL)
489 PyArena_Free(arena);
490 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +0000491}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000492
493
494/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
495 * PyObject* parser_issuite(PyObject* self, PyObject* args)
496 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000497 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000498 * a statement suite, respectively. The return is a Python truth value.
499 *
500 */
501static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000502parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000503{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000504 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000505 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000506
Georg Brandl30704ea02008-07-23 15:07:12 +0000507 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000508
Martin v. Löwis1a214512008-06-11 05:26:20 +0000509 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000510 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000511 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000512 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000513 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000514
515 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000516 /* Check to see if the ST represents an expression or not. */
517 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000518 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000519 }
520 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000521}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000522
523
524static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000525parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000526{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000527 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000528 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000529
Georg Brandl30704ea02008-07-23 15:07:12 +0000530 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000531
Martin v. Löwis1a214512008-06-11 05:26:20 +0000532 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000533 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000534 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000535 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000536 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000537
538 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000539 /* Check to see if the ST represents an expression or not. */
540 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000541 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000542 }
543 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000544}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000545
546
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200547/* err_string(const char* message)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000548 *
549 * Sets the error string for an exception of type ParserError.
550 *
551 */
552static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200553err_string(const char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000554{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000555 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000556}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000557
558
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000559/* PyObject* parser_do_parse(PyObject* args, int type)
560 *
561 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000562 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000563 *
564 */
565static PyObject*
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200566parser_do_parse(PyObject *args, PyObject *kw, const char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000567{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000568 char* string = 0;
569 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000570 int flags = 0;
571 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000572
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000573 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000574
575 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000576 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
577 &_PyParser_Grammar,
578 (type == PyST_EXPR)
579 ? eval_input : file_input,
580 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 if (n) {
583 res = parser_newstobject(n, type);
Guido van Rossum495da292019-03-07 12:38:08 -0800584 if (res) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000585 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
Guido van Rossum495da292019-03-07 12:38:08 -0800586 ((PyST_Object *)res)->st_flags.cf_feature_version = PY_MINOR_VERSION;
587 }
Benjamin Petersonf216c942008-10-31 02:28:05 +0000588 }
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500589 else {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000590 PyParser_SetError(&err);
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500591 }
Benjamin Petersonf0cdbad2011-06-05 22:14:05 -0500592 PyParser_ClearError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000593 }
594 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000595}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000596
597
598/* PyObject* parser_expr(PyObject* self, PyObject* args)
599 * PyObject* parser_suite(PyObject* self, PyObject* args)
600 *
601 * External interfaces to the parser itself. Which is called determines if
602 * the parser attempts to recognize an expression ('eval' form) or statement
603 * suite ('exec' form). The real work is done by parser_do_parse() above.
604 *
605 */
606static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000607parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000608{
Fred Drake268397f1998-04-29 14:16:32 +0000609 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000610 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000611}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000612
613
614static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000615parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000616{
Fred Drake268397f1998-04-29 14:16:32 +0000617 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000618 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000619}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000620
621
622
Fred Drakec2683dd2001-07-17 19:32:05 +0000623/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000624 * object requires that the input tuple be valid without having to rely on
625 * catching an exception from the compiler. This is done to allow the
626 * compiler itself to remain fast, since most of its input will come from
627 * the parser directly, and therefore be known to be syntactically correct.
628 * This validation is done to ensure that we don't core dump the compile
629 * phase, returning an exception instead.
630 *
631 * Two aspects can be broken out in this code: creating a node tree from
632 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000633 * advantageous to expand the number of ST types to include funcdefs and
634 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000635 * here. They are not necessary, and not quite as useful in a raw form.
636 * For now, let's get expressions and suites working reliably.
637 */
638
639
Jeremy Hylton938ace62002-07-17 16:30:39 +0000640static node* build_node_tree(PyObject *tuple);
Benjamin Peterson53595c42016-06-02 11:30:18 -0700641
642static int
643validate_node(node *tree)
644{
645 int type = TYPE(tree);
646 int nch = NCH(tree);
647 dfa *nt_dfa;
648 state *dfa_state;
649 int pos, arc;
650
651 assert(ISNONTERMINAL(type));
652 type -= NT_OFFSET;
653 if (type >= _PyParser_Grammar.g_ndfas) {
654 PyErr_Format(parser_error, "Unrecognized node type %d.", TYPE(tree));
655 return 0;
656 }
657 nt_dfa = &_PyParser_Grammar.g_dfa[type];
658 REQ(tree, nt_dfa->d_type);
659
660 /* Run the DFA for this nonterminal. */
tyomitch1b304f92019-03-09 17:35:50 +0200661 dfa_state = nt_dfa->d_state;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700662 for (pos = 0; pos < nch; ++pos) {
663 node *ch = CHILD(tree, pos);
664 int ch_type = TYPE(ch);
tyomitchcb0748d2019-04-03 08:12:07 +0300665 if ((ch_type >= NT_OFFSET + _PyParser_Grammar.g_ndfas)
666 || (ISTERMINAL(ch_type) && (ch_type >= N_TOKENS))
667 || (ch_type < 0)
668 ) {
669 PyErr_Format(parser_error, "Unrecognized node type %d.", ch_type);
670 return 0;
671 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800672 if (ch_type == suite && TYPE(tree) == funcdef) {
673 /* This is the opposite hack of what we do in parser.c
674 (search for func_body_suite), except we don't ever
675 support type comments here. */
676 ch_type = func_body_suite;
677 }
Benjamin Peterson53595c42016-06-02 11:30:18 -0700678 for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
679 short a_label = dfa_state->s_arc[arc].a_lbl;
680 assert(a_label < _PyParser_Grammar.g_ll.ll_nlabels);
Pablo Galindo9a0000d2019-03-21 23:33:02 +0000681
682 const char *label_str = _PyParser_Grammar.g_ll.ll_label[a_label].lb_str;
683 if ((_PyParser_Grammar.g_ll.ll_label[a_label].lb_type == ch_type)
684 && ((ch->n_str == NULL) || (label_str == NULL)
685 || (strcmp(ch->n_str, label_str) == 0))
686 ) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300687 /* The child is acceptable; if non-terminal, validate it recursively. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700688 if (ISNONTERMINAL(ch_type) && !validate_node(ch))
689 return 0;
690
691 /* Update the state, and move on to the next child. */
692 dfa_state = &nt_dfa->d_state[dfa_state->s_arc[arc].a_arrow];
693 goto arc_found;
694 }
695 }
696 /* What would this state have accepted? */
697 {
698 short a_label = dfa_state->s_arc->a_lbl;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700699 if (!a_label) /* Wouldn't accept any more children */
700 goto illegal_num_children;
701
Pablo Galindo9a0000d2019-03-21 23:33:02 +0000702 int next_type = _PyParser_Grammar.g_ll.ll_label[a_label].lb_type;
703 const char *expected_str = _PyParser_Grammar.g_ll.ll_label[a_label].lb_str;
704
705 if (ISNONTERMINAL(next_type)) {
tyomitchcb0748d2019-04-03 08:12:07 +0300706 PyErr_Format(parser_error, "Expected %s, got %s.",
707 _PyParser_Grammar.g_dfa[next_type - NT_OFFSET].d_name,
708 ISTERMINAL(ch_type) ? _PyParser_TokenNames[ch_type] :
709 _PyParser_Grammar.g_dfa[ch_type - NT_OFFSET].d_name);
Pablo Galindo9a0000d2019-03-21 23:33:02 +0000710 }
711 else if (expected_str != NULL) {
712 PyErr_Format(parser_error, "Illegal terminal: expected '%s'.",
713 expected_str);
714 }
715 else {
Benjamin Peterson53595c42016-06-02 11:30:18 -0700716 PyErr_Format(parser_error, "Illegal terminal: expected %s.",
717 _PyParser_TokenNames[next_type]);
Pablo Galindo9a0000d2019-03-21 23:33:02 +0000718 }
Benjamin Peterson53595c42016-06-02 11:30:18 -0700719 return 0;
720 }
721
722arc_found:
723 continue;
724 }
725 /* Are we in a final state? If so, return 1 for successful validation. */
726 for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
727 if (!dfa_state->s_arc[arc].a_lbl) {
728 return 1;
729 }
730 }
731
732illegal_num_children:
733 PyErr_Format(parser_error,
734 "Illegal number of children for %s node.", nt_dfa->d_name);
735 return 0;
736}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000737
Fred Drakec2683dd2001-07-17 19:32:05 +0000738/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000739 *
740 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000741 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000742 * tuple can be validated. It does this by checking the first code of the
743 * tuple, and, if acceptable, builds the internal representation. If this
744 * step succeeds, the internal representation is validated as fully as
Benjamin Peterson53595c42016-06-02 11:30:18 -0700745 * possible with the recursive validate_node() routine defined above.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000746 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000747 * This function must be changed if support is to be added for PyST_FRAGMENT
748 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000749 *
750 */
751static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000752parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000753{
Fred Drake268397f1998-04-29 14:16:32 +0000754 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000755 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000756 PyObject *tuple;
757 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000758
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000759 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000760
Fred Drakec2683dd2001-07-17 19:32:05 +0000761 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000762 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000763 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000764 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000765 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000766 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000767 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000768 }
769 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000770 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000771 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000772 tree = build_node_tree(tuple);
773 if (tree != 0) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300774 node *validation_root = NULL;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700775 int tree_type = 0;
776 switch (TYPE(tree)) {
777 case eval_input:
Fred Drake0ac9b072000-09-12 21:58:06 +0000778 /* Might be an eval form. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700779 tree_type = PyST_EXPR;
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300780 validation_root = tree;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700781 break;
782 case encoding_decl:
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000783 /* This looks like an encoding_decl so far. */
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300784 if (NCH(tree) == 1) {
785 tree_type = PyST_SUITE;
786 validation_root = CHILD(tree, 0);
787 }
788 else {
Benjamin Peterson53595c42016-06-02 11:30:18 -0700789 err_string("Error Parsing encoding_decl");
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300790 }
791 break;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700792 case file_input:
793 /* This looks like an exec form so far. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700794 tree_type = PyST_SUITE;
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300795 validation_root = tree;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700796 break;
797 default:
Fred Drake0ac9b072000-09-12 21:58:06 +0000798 /* This is a fragment, at best. */
Fred Drake661ea262000-10-24 19:57:45 +0000799 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000800 }
Benjamin Peterson53595c42016-06-02 11:30:18 -0700801
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300802 if (validation_root != NULL && validate_node(validation_root))
Benjamin Peterson53595c42016-06-02 11:30:18 -0700803 st = parser_newstobject(tree, tree_type);
804 else
805 PyNode_Free(tree);
Guido van Rossum47478871996-08-21 14:32:37 +0000806 }
Andrew Svetlov737fb892012-12-18 21:14:22 +0200807 /* Make sure we raise an exception on all errors. We should never
Guido van Rossum47478871996-08-21 14:32:37 +0000808 * get this, but we'd do well to be sure something is done.
809 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000810 if (st == NULL && !PyErr_Occurred())
811 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000812
Fred Drakec2683dd2001-07-17 19:32:05 +0000813 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000814}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000815
816
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000817/* node* build_node_children()
818 *
819 * Iterate across the children of the current non-terminal node and build
820 * their structures. If successful, return the root of this portion of
821 * the tree, otherwise, 0. Any required exception will be specified already,
822 * and no memory will have been deallocated.
823 *
824 */
825static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000826build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000827{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000828 Py_ssize_t len = PyObject_Size(tuple);
829 Py_ssize_t i;
830 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000831
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300832 if (len < 0) {
833 return NULL;
834 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000835 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000836 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000837 PyObject* elem = PySequence_GetItem(tuple, i);
838 int ok = elem != NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200839 int type = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000840 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000841
Fred Drakeff9ea482000-04-19 13:54:15 +0000842 if (ok)
843 ok = PySequence_Check(elem);
844 if (ok) {
845 PyObject *temp = PySequence_GetItem(elem, 0);
846 if (temp == NULL)
847 ok = 0;
848 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000849 ok = PyLong_Check(temp);
Serhiy Storchaka78980432013-01-15 01:12:17 +0200850 if (ok) {
851 type = _PyLong_AsInt(temp);
852 if (type == -1 && PyErr_Occurred()) {
853 Py_DECREF(temp);
854 Py_DECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300855 return NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200856 }
857 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000858 Py_DECREF(temp);
859 }
860 }
861 if (!ok) {
Victor Stinner5f8d4852014-01-02 11:49:27 +0100862 PyObject *err = Py_BuildValue("Os", elem,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000863 "Illegal node construct.");
864 PyErr_SetObject(parser_error, err);
865 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000866 Py_XDECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300867 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000868 }
869 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000870 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000871 PyObject *temp;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000872 const char *temp_str;
Guido van Rossum47478871996-08-21 14:32:37 +0000873
Fred Drake0ac9b072000-09-12 21:58:06 +0000874 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000875 err_string("terminal nodes must have 2 or 3 entries");
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300876 Py_DECREF(elem);
877 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000878 }
879 temp = PySequence_GetItem(elem, 1);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300880 if (temp == NULL) {
881 Py_DECREF(elem);
882 return NULL;
883 }
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000884 if (!PyUnicode_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000885 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000886 "second item in terminal node must be a string,"
887 " found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000888 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000889 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000890 Py_DECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300891 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000892 }
893 if (len == 3) {
894 PyObject *o = PySequence_GetItem(elem, 2);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300895 if (o == NULL) {
896 Py_DECREF(temp);
897 Py_DECREF(elem);
898 return NULL;
899 }
900 if (PyLong_Check(o)) {
901 int num = _PyLong_AsInt(o);
902 if (num == -1 && PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000903 Py_DECREF(o);
904 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000905 Py_DECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300906 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000907 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300908 *line_num = num;
Fred Drakeff9ea482000-04-19 13:54:15 +0000909 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300910 else {
911 PyErr_Format(parser_error,
912 "third item in terminal node must be an"
913 " integer, found %s",
914 Py_TYPE(temp)->tp_name);
915 Py_DECREF(o);
916 Py_DECREF(temp);
917 Py_DECREF(elem);
918 return NULL;
919 }
920 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000921 }
Serhiy Storchaka06515832016-11-20 09:13:07 +0200922 temp_str = PyUnicode_AsUTF8AndSize(temp, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000923 if (temp_str == NULL) {
924 Py_DECREF(temp);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300925 Py_DECREF(elem);
926 return NULL;
Alexander Belopolskye239d232010-12-08 23:31:48 +0000927 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000928 strn = (char *)PyObject_MALLOC(len + 1);
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200929 if (strn == NULL) {
930 Py_DECREF(temp);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300931 Py_DECREF(elem);
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200932 PyErr_NoMemory();
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300933 return NULL;
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200934 }
935 (void) memcpy(strn, temp_str, len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000936 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000937 }
938 else if (!ISNONTERMINAL(type)) {
939 /*
940 * It has to be one or the other; this is an error.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200941 * Raise an exception.
Fred Drakeff9ea482000-04-19 13:54:15 +0000942 */
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300943 PyObject *err = Py_BuildValue("Os", elem, "unknown node type.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000944 PyErr_SetObject(parser_error, err);
945 Py_XDECREF(err);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300946 Py_DECREF(elem);
947 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000948 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000949 err = PyNode_AddChild(root, type, strn, *line_num, 0, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000950 if (err == E_NOMEM) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300951 Py_DECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000952 PyObject_FREE(strn);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300953 PyErr_NoMemory();
954 return NULL;
Fred Drake8b55b2d2001-12-05 22:10:44 +0000955 }
956 if (err == E_OVERFLOW) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300957 Py_DECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000958 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000959 PyErr_SetString(PyExc_ValueError,
960 "unsupported number of child nodes");
961 return NULL;
962 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000963
Fred Drakeff9ea482000-04-19 13:54:15 +0000964 if (ISNONTERMINAL(type)) {
965 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000966
Fred Drakeff9ea482000-04-19 13:54:15 +0000967 if (new_child != build_node_children(elem, new_child, line_num)) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300968 Py_DECREF(elem);
969 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000970 }
971 }
972 else if (type == NEWLINE) { /* It's true: we increment the */
973 ++(*line_num); /* line number *after* the newline! */
974 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300975 Py_DECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000976 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000977 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000978}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000979
980
981static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000982build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000983{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000984 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000985 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000986 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000987
Guido van Rossum47478871996-08-21 14:32:37 +0000988 if (temp != NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000989 num = PyLong_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000990 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000991 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000992 /*
993 * The tuple is simple, but it doesn't start with a start symbol.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200994 * Raise an exception now and be done with it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000995 */
Victor Stinner6684bdf2013-07-17 00:13:52 +0200996 tuple = Py_BuildValue("Os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000997 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000998 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000999 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001000 }
1001 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001002 /*
1003 * Not efficient, but that can be handled later.
1004 */
1005 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001006 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001007
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001008 if (num == encoding_decl) {
1009 encoding = PySequence_GetItem(tuple, 2);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001010 if (encoding == NULL) {
1011 PyErr_SetString(parser_error, "missed encoding");
1012 return NULL;
1013 }
1014 if (!PyUnicode_Check(encoding)) {
1015 PyErr_Format(parser_error,
1016 "encoding must be a string, found %.200s",
1017 Py_TYPE(encoding)->tp_name);
1018 Py_DECREF(encoding);
1019 return NULL;
1020 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001021 /* tuple isn't borrowed anymore here, need to DECREF */
1022 tuple = PySequence_GetSlice(tuple, 0, 2);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001023 if (tuple == NULL) {
1024 Py_DECREF(encoding);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001025 return NULL;
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001026 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001027 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001028 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +00001029 if (res != NULL) {
1030 if (res != build_node_children(tuple, res, &line_num)) {
1031 PyNode_Free(res);
1032 res = NULL;
1033 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001034 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +00001035 Py_ssize_t len;
Neal Norwitz3fcbea52007-08-26 04:51:28 +00001036 const char *temp;
Serhiy Storchaka06515832016-11-20 09:13:07 +02001037 temp = PyUnicode_AsUTF8AndSize(encoding, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001038 if (temp == NULL) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001039 PyNode_Free(res);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001040 Py_DECREF(encoding);
1041 Py_DECREF(tuple);
1042 return NULL;
1043 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00001044 res->n_str = (char *)PyObject_MALLOC(len + 1);
Victor Stinner3bd6abd2013-07-12 01:33:59 +02001045 if (res->n_str == NULL) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001046 PyNode_Free(res);
Victor Stinner3bd6abd2013-07-12 01:33:59 +02001047 Py_DECREF(encoding);
1048 Py_DECREF(tuple);
1049 PyErr_NoMemory();
1050 return NULL;
1051 }
1052 (void) memcpy(res->n_str, temp, len + 1);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001053 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001054 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001055 if (encoding != NULL) {
1056 Py_DECREF(encoding);
1057 Py_DECREF(tuple);
1058 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001059 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001060 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001061 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +00001062 * NONTERMINAL, we can't use it. Not sure the implementation
1063 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +00001064 */
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001065 PyObject *err = Py_BuildValue("Os", tuple,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001066 "Illegal component tuple.");
1067 PyErr_SetObject(parser_error, err);
1068 Py_XDECREF(err);
1069 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001070
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001071 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001072}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001073
1074
Fred Drake43f8f9b1998-04-13 16:25:46 +00001075static PyObject*
1076pickle_constructor = NULL;
1077
1078
1079static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00001080parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00001081{
Fred Drake268397f1998-04-29 14:16:32 +00001082 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00001083 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00001084 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00001085 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001086
Fred Drakec2683dd2001-07-17 19:32:05 +00001087 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001088 PyObject *newargs;
1089 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001090
Fred Drake2a6875e1999-09-20 22:32:18 +00001091 if ((empty_dict = PyDict_New()) == NULL)
1092 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00001093 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00001094 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00001095 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00001096 if (tuple != NULL) {
1097 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
1098 Py_DECREF(tuple);
1099 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001100 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001101 }
1102 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00001103 Py_XDECREF(empty_dict);
1104
Fred Drake43f8f9b1998-04-13 16:25:46 +00001105 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00001106}
Fred Drake43f8f9b1998-04-13 16:25:46 +00001107
1108
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001109/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00001110 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001111 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00001112 * We'd really have to write a wrapper around it all anyway to allow
1113 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001114 */
1115static PyMethodDef parser_functions[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001116 {"compilest", (PyCFunction)(void(*)(void))parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001117 PyDoc_STR("Compiles an ST object into a code object.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001118 {"expr", (PyCFunction)(void(*)(void))parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001119 PyDoc_STR("Creates an ST object from an expression.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001120 {"isexpr", (PyCFunction)(void(*)(void))parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001121 PyDoc_STR("Determines if an ST object was created from an expression.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001122 {"issuite", (PyCFunction)(void(*)(void))parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001123 PyDoc_STR("Determines if an ST object was created from a suite.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001124 {"suite", (PyCFunction)(void(*)(void))parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001125 PyDoc_STR("Creates an ST object from a suite.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001126 {"sequence2st", (PyCFunction)(void(*)(void))parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001127 PyDoc_STR("Creates an ST object from a tree representation.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001128 {"st2tuple", (PyCFunction)(void(*)(void))parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001129 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001130 {"st2list", (PyCFunction)(void(*)(void))parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001131 PyDoc_STR("Creates a list-tree representation of an ST.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001132 {"tuple2st", (PyCFunction)(void(*)(void))parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001133 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001134
Fred Drake43f8f9b1998-04-13 16:25:46 +00001135 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00001136 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00001137 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00001138
Fred Drake268397f1998-04-29 14:16:32 +00001139 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001140 };
1141
1142
Martin v. Löwis1a214512008-06-11 05:26:20 +00001143
1144static struct PyModuleDef parsermodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 PyModuleDef_HEAD_INIT,
1146 "parser",
1147 NULL,
1148 -1,
1149 parser_functions,
1150 NULL,
1151 NULL,
1152 NULL,
1153 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001154};
1155
1156PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00001157
Mark Hammond62b1ab12002-07-23 06:31:15 +00001158PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001159PyInit_parser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00001160{
Fred Drake13130bc2001-08-15 16:44:56 +00001161 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00001162
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001163 if (PyType_Ready(&PyST_Type) < 0)
1164 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001165 module = PyModule_Create(&parsermodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001166 if (module == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 return NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001168
Fred Drake7a15ba51999-09-09 14:21:52 +00001169 if (parser_error == 0)
1170 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001171
Tim Peters6a627252003-07-21 14:25:23 +00001172 if (parser_error == 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001173 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00001174 /* CAUTION: The code next used to skip bumping the refcount on
Martin v. Löwis1a214512008-06-11 05:26:20 +00001175 * parser_error. That's a disaster if PyInit_parser() gets called more
Tim Peters6a627252003-07-21 14:25:23 +00001176 * than once. By incref'ing, we ensure that each module dict that
1177 * gets created owns its reference to the shared parser_error object,
1178 * and the file static parser_error vrbl owns a reference too.
1179 */
1180 Py_INCREF(parser_error);
1181 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001182 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00001183
Fred Drakec2683dd2001-07-17 19:32:05 +00001184 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00001185 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001186
Fred Drake13130bc2001-08-15 16:44:56 +00001187 PyModule_AddStringConstant(module, "__copyright__",
1188 parser_copyright_string);
1189 PyModule_AddStringConstant(module, "__doc__",
1190 parser_doc_string);
1191 PyModule_AddStringConstant(module, "__version__",
1192 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001193
Fred Drake78bdb9b2001-07-19 20:17:15 +00001194 /* Register to support pickling.
1195 * If this fails, the import of this module will fail because an
1196 * exception will be raised here; should we clear the exception?
1197 */
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00001198 copyreg = PyImport_ImportModuleNoBlock("copyreg");
Fred Drake13130bc2001-08-15 16:44:56 +00001199 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001200 PyObject *func, *pickler;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001201 _Py_IDENTIFIER(pickle);
1202 _Py_IDENTIFIER(sequence2st);
1203 _Py_IDENTIFIER(_pickler);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001204
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001205 func = _PyObject_GetAttrId(copyreg, &PyId_pickle);
1206 pickle_constructor = _PyObject_GetAttrId(module, &PyId_sequence2st);
1207 pickler = _PyObject_GetAttrId(module, &PyId__pickler);
Fred Drakeff9ea482000-04-19 13:54:15 +00001208 Py_XINCREF(pickle_constructor);
1209 if ((func != NULL) && (pickle_constructor != NULL)
1210 && (pickler != NULL)) {
1211 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001212
Thomas Wouters477c8d52006-05-27 19:21:47 +00001213 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
1214 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00001215 Py_XDECREF(res);
1216 }
1217 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00001218 Py_XDECREF(pickle_constructor);
1219 Py_XDECREF(pickler);
1220 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001221 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00001222 return module;
Fred Drakeff9ea482000-04-19 13:54:15 +00001223}