blob: 079d00f32aa6cd3bbed2bcd12dc41d37e7c4e532 [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 */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200227 0, /* tp_vectorcall_offset */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000228 0, /* tp_getattr */
Fred Drakeff9ea482000-04-19 13:54:15 +0000229 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200230 0, /* tp_as_async */
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;
Victor Stinner37d66d72019-06-13 02:16:41 +0200339 o->st_flags = _PyCompilerFlags_INIT;
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);
Guido van Rossum495da292019-03-07 12:38:08 -0800583 if (res) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000584 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
Guido van Rossum495da292019-03-07 12:38:08 -0800585 ((PyST_Object *)res)->st_flags.cf_feature_version = PY_MINOR_VERSION;
586 }
Benjamin Petersonf216c942008-10-31 02:28:05 +0000587 }
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500588 else {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000589 PyParser_SetError(&err);
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500590 }
Benjamin Petersonf0cdbad2011-06-05 22:14:05 -0500591 PyParser_ClearError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000592 }
593 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000594}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000595
596
597/* PyObject* parser_expr(PyObject* self, PyObject* args)
598 * PyObject* parser_suite(PyObject* self, PyObject* args)
599 *
600 * External interfaces to the parser itself. Which is called determines if
601 * the parser attempts to recognize an expression ('eval' form) or statement
602 * suite ('exec' form). The real work is done by parser_do_parse() above.
603 *
604 */
605static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000606parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000607{
Fred Drake268397f1998-04-29 14:16:32 +0000608 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000609 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000610}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000611
612
613static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000614parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000615{
Fred Drake268397f1998-04-29 14:16:32 +0000616 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000617 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000618}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000619
620
621
Fred Drakec2683dd2001-07-17 19:32:05 +0000622/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000623 * object requires that the input tuple be valid without having to rely on
624 * catching an exception from the compiler. This is done to allow the
625 * compiler itself to remain fast, since most of its input will come from
626 * the parser directly, and therefore be known to be syntactically correct.
627 * This validation is done to ensure that we don't core dump the compile
628 * phase, returning an exception instead.
629 *
630 * Two aspects can be broken out in this code: creating a node tree from
631 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000632 * advantageous to expand the number of ST types to include funcdefs and
633 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000634 * here. They are not necessary, and not quite as useful in a raw form.
635 * For now, let's get expressions and suites working reliably.
636 */
637
638
Jeremy Hylton938ace62002-07-17 16:30:39 +0000639static node* build_node_tree(PyObject *tuple);
Benjamin Peterson53595c42016-06-02 11:30:18 -0700640
641static int
642validate_node(node *tree)
643{
644 int type = TYPE(tree);
645 int nch = NCH(tree);
Benjamin Peterson53595c42016-06-02 11:30:18 -0700646 state *dfa_state;
647 int pos, arc;
648
649 assert(ISNONTERMINAL(type));
650 type -= NT_OFFSET;
651 if (type >= _PyParser_Grammar.g_ndfas) {
652 PyErr_Format(parser_error, "Unrecognized node type %d.", TYPE(tree));
653 return 0;
654 }
Inada Naoki09415ff2019-04-23 20:39:37 +0900655 const dfa *nt_dfa = &_PyParser_Grammar.g_dfa[type];
Benjamin Peterson53595c42016-06-02 11:30:18 -0700656 REQ(tree, nt_dfa->d_type);
657
658 /* Run the DFA for this nonterminal. */
tyomitch1b304f92019-03-09 17:35:50 +0200659 dfa_state = nt_dfa->d_state;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700660 for (pos = 0; pos < nch; ++pos) {
661 node *ch = CHILD(tree, pos);
662 int ch_type = TYPE(ch);
tyomitchcb0748d2019-04-03 08:12:07 +0300663 if ((ch_type >= NT_OFFSET + _PyParser_Grammar.g_ndfas)
664 || (ISTERMINAL(ch_type) && (ch_type >= N_TOKENS))
665 || (ch_type < 0)
666 ) {
667 PyErr_Format(parser_error, "Unrecognized node type %d.", ch_type);
668 return 0;
669 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800670 if (ch_type == suite && TYPE(tree) == funcdef) {
671 /* This is the opposite hack of what we do in parser.c
672 (search for func_body_suite), except we don't ever
673 support type comments here. */
674 ch_type = func_body_suite;
675 }
Benjamin Peterson53595c42016-06-02 11:30:18 -0700676 for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
677 short a_label = dfa_state->s_arc[arc].a_lbl;
678 assert(a_label < _PyParser_Grammar.g_ll.ll_nlabels);
Pablo Galindo9a0000d2019-03-21 23:33:02 +0000679
680 const char *label_str = _PyParser_Grammar.g_ll.ll_label[a_label].lb_str;
681 if ((_PyParser_Grammar.g_ll.ll_label[a_label].lb_type == ch_type)
682 && ((ch->n_str == NULL) || (label_str == NULL)
683 || (strcmp(ch->n_str, label_str) == 0))
684 ) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300685 /* The child is acceptable; if non-terminal, validate it recursively. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700686 if (ISNONTERMINAL(ch_type) && !validate_node(ch))
687 return 0;
688
689 /* Update the state, and move on to the next child. */
690 dfa_state = &nt_dfa->d_state[dfa_state->s_arc[arc].a_arrow];
691 goto arc_found;
692 }
693 }
694 /* What would this state have accepted? */
695 {
696 short a_label = dfa_state->s_arc->a_lbl;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700697 if (!a_label) /* Wouldn't accept any more children */
698 goto illegal_num_children;
699
Pablo Galindo9a0000d2019-03-21 23:33:02 +0000700 int next_type = _PyParser_Grammar.g_ll.ll_label[a_label].lb_type;
701 const char *expected_str = _PyParser_Grammar.g_ll.ll_label[a_label].lb_str;
702
703 if (ISNONTERMINAL(next_type)) {
tyomitchcb0748d2019-04-03 08:12:07 +0300704 PyErr_Format(parser_error, "Expected %s, got %s.",
705 _PyParser_Grammar.g_dfa[next_type - NT_OFFSET].d_name,
706 ISTERMINAL(ch_type) ? _PyParser_TokenNames[ch_type] :
707 _PyParser_Grammar.g_dfa[ch_type - NT_OFFSET].d_name);
Pablo Galindo9a0000d2019-03-21 23:33:02 +0000708 }
709 else if (expected_str != NULL) {
710 PyErr_Format(parser_error, "Illegal terminal: expected '%s'.",
711 expected_str);
712 }
713 else {
Benjamin Peterson53595c42016-06-02 11:30:18 -0700714 PyErr_Format(parser_error, "Illegal terminal: expected %s.",
715 _PyParser_TokenNames[next_type]);
Pablo Galindo9a0000d2019-03-21 23:33:02 +0000716 }
Benjamin Peterson53595c42016-06-02 11:30:18 -0700717 return 0;
718 }
719
720arc_found:
721 continue;
722 }
723 /* Are we in a final state? If so, return 1 for successful validation. */
724 for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
725 if (!dfa_state->s_arc[arc].a_lbl) {
726 return 1;
727 }
728 }
729
730illegal_num_children:
731 PyErr_Format(parser_error,
732 "Illegal number of children for %s node.", nt_dfa->d_name);
733 return 0;
734}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000735
Fred Drakec2683dd2001-07-17 19:32:05 +0000736/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000737 *
738 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000739 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000740 * tuple can be validated. It does this by checking the first code of the
741 * tuple, and, if acceptable, builds the internal representation. If this
742 * step succeeds, the internal representation is validated as fully as
Benjamin Peterson53595c42016-06-02 11:30:18 -0700743 * possible with the recursive validate_node() routine defined above.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000744 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000745 * This function must be changed if support is to be added for PyST_FRAGMENT
746 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000747 *
748 */
749static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000750parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000751{
Fred Drake268397f1998-04-29 14:16:32 +0000752 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000753 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000754 PyObject *tuple;
755 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000756
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000757 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000758
Fred Drakec2683dd2001-07-17 19:32:05 +0000759 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000760 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000761 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000762 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000763 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000764 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000765 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000766 }
767 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000768 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000769 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000770 tree = build_node_tree(tuple);
771 if (tree != 0) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300772 node *validation_root = NULL;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700773 int tree_type = 0;
774 switch (TYPE(tree)) {
775 case eval_input:
Fred Drake0ac9b072000-09-12 21:58:06 +0000776 /* Might be an eval form. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700777 tree_type = PyST_EXPR;
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300778 validation_root = tree;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700779 break;
780 case encoding_decl:
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000781 /* This looks like an encoding_decl so far. */
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300782 if (NCH(tree) == 1) {
783 tree_type = PyST_SUITE;
784 validation_root = CHILD(tree, 0);
785 }
786 else {
Benjamin Peterson53595c42016-06-02 11:30:18 -0700787 err_string("Error Parsing encoding_decl");
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300788 }
789 break;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700790 case file_input:
791 /* This looks like an exec form so far. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700792 tree_type = PyST_SUITE;
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300793 validation_root = tree;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700794 break;
795 default:
Fred Drake0ac9b072000-09-12 21:58:06 +0000796 /* This is a fragment, at best. */
Fred Drake661ea262000-10-24 19:57:45 +0000797 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000798 }
Benjamin Peterson53595c42016-06-02 11:30:18 -0700799
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300800 if (validation_root != NULL && validate_node(validation_root))
Benjamin Peterson53595c42016-06-02 11:30:18 -0700801 st = parser_newstobject(tree, tree_type);
802 else
803 PyNode_Free(tree);
Guido van Rossum47478871996-08-21 14:32:37 +0000804 }
Andrew Svetlov737fb892012-12-18 21:14:22 +0200805 /* Make sure we raise an exception on all errors. We should never
Guido van Rossum47478871996-08-21 14:32:37 +0000806 * get this, but we'd do well to be sure something is done.
807 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000808 if (st == NULL && !PyErr_Occurred())
809 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000810
Fred Drakec2683dd2001-07-17 19:32:05 +0000811 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000812}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000813
814
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000815/* node* build_node_children()
816 *
817 * Iterate across the children of the current non-terminal node and build
818 * their structures. If successful, return the root of this portion of
819 * the tree, otherwise, 0. Any required exception will be specified already,
820 * and no memory will have been deallocated.
821 *
822 */
823static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000824build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000825{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000826 Py_ssize_t len = PyObject_Size(tuple);
827 Py_ssize_t i;
828 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000829
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300830 if (len < 0) {
831 return NULL;
832 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000833 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000834 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000835 PyObject* elem = PySequence_GetItem(tuple, i);
836 int ok = elem != NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200837 int type = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000838 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000839
Fred Drakeff9ea482000-04-19 13:54:15 +0000840 if (ok)
841 ok = PySequence_Check(elem);
842 if (ok) {
843 PyObject *temp = PySequence_GetItem(elem, 0);
844 if (temp == NULL)
845 ok = 0;
846 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000847 ok = PyLong_Check(temp);
Serhiy Storchaka78980432013-01-15 01:12:17 +0200848 if (ok) {
849 type = _PyLong_AsInt(temp);
850 if (type == -1 && PyErr_Occurred()) {
851 Py_DECREF(temp);
852 Py_DECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300853 return NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200854 }
855 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000856 Py_DECREF(temp);
857 }
858 }
859 if (!ok) {
Victor Stinner5f8d4852014-01-02 11:49:27 +0100860 PyObject *err = Py_BuildValue("Os", elem,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000861 "Illegal node construct.");
862 PyErr_SetObject(parser_error, err);
863 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000864 Py_XDECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300865 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000866 }
867 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000868 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000869 PyObject *temp;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000870 const char *temp_str;
Guido van Rossum47478871996-08-21 14:32:37 +0000871
Fred Drake0ac9b072000-09-12 21:58:06 +0000872 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000873 err_string("terminal nodes must have 2 or 3 entries");
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300874 Py_DECREF(elem);
875 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000876 }
877 temp = PySequence_GetItem(elem, 1);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300878 if (temp == NULL) {
879 Py_DECREF(elem);
880 return NULL;
881 }
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000882 if (!PyUnicode_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000883 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000884 "second item in terminal node must be a string,"
885 " found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000886 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000887 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000888 Py_DECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300889 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000890 }
891 if (len == 3) {
892 PyObject *o = PySequence_GetItem(elem, 2);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300893 if (o == NULL) {
894 Py_DECREF(temp);
895 Py_DECREF(elem);
896 return NULL;
897 }
898 if (PyLong_Check(o)) {
899 int num = _PyLong_AsInt(o);
900 if (num == -1 && PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000901 Py_DECREF(o);
902 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000903 Py_DECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300904 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000905 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300906 *line_num = num;
Fred Drakeff9ea482000-04-19 13:54:15 +0000907 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300908 else {
909 PyErr_Format(parser_error,
910 "third item in terminal node must be an"
911 " integer, found %s",
912 Py_TYPE(temp)->tp_name);
913 Py_DECREF(o);
914 Py_DECREF(temp);
915 Py_DECREF(elem);
916 return NULL;
917 }
918 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000919 }
Serhiy Storchaka06515832016-11-20 09:13:07 +0200920 temp_str = PyUnicode_AsUTF8AndSize(temp, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000921 if (temp_str == NULL) {
922 Py_DECREF(temp);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300923 Py_DECREF(elem);
924 return NULL;
Alexander Belopolskye239d232010-12-08 23:31:48 +0000925 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000926 strn = (char *)PyObject_MALLOC(len + 1);
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200927 if (strn == NULL) {
928 Py_DECREF(temp);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300929 Py_DECREF(elem);
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200930 PyErr_NoMemory();
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300931 return NULL;
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200932 }
933 (void) memcpy(strn, temp_str, len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000934 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000935 }
936 else if (!ISNONTERMINAL(type)) {
937 /*
938 * It has to be one or the other; this is an error.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200939 * Raise an exception.
Fred Drakeff9ea482000-04-19 13:54:15 +0000940 */
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300941 PyObject *err = Py_BuildValue("Os", elem, "unknown node type.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000942 PyErr_SetObject(parser_error, err);
943 Py_XDECREF(err);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300944 Py_DECREF(elem);
945 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000946 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000947 err = PyNode_AddChild(root, type, strn, *line_num, 0, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000948 if (err == E_NOMEM) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300949 Py_DECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000950 PyObject_FREE(strn);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300951 PyErr_NoMemory();
952 return NULL;
Fred Drake8b55b2d2001-12-05 22:10:44 +0000953 }
954 if (err == E_OVERFLOW) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300955 Py_DECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000956 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000957 PyErr_SetString(PyExc_ValueError,
958 "unsupported number of child nodes");
959 return NULL;
960 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000961
Fred Drakeff9ea482000-04-19 13:54:15 +0000962 if (ISNONTERMINAL(type)) {
963 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000964
Fred Drakeff9ea482000-04-19 13:54:15 +0000965 if (new_child != build_node_children(elem, new_child, line_num)) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300966 Py_DECREF(elem);
967 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000968 }
969 }
970 else if (type == NEWLINE) { /* It's true: we increment the */
971 ++(*line_num); /* line number *after* the newline! */
972 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300973 Py_DECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000974 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000975 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000976}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000977
978
979static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000980build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000981{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000982 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000983 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000984 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000985
Guido van Rossum47478871996-08-21 14:32:37 +0000986 if (temp != NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000987 num = PyLong_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000988 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000989 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000990 /*
991 * The tuple is simple, but it doesn't start with a start symbol.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200992 * Raise an exception now and be done with it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000993 */
Victor Stinner6684bdf2013-07-17 00:13:52 +0200994 tuple = Py_BuildValue("Os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000995 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000996 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000997 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000998 }
999 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001000 /*
1001 * Not efficient, but that can be handled later.
1002 */
1003 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001004 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001005
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001006 if (num == encoding_decl) {
1007 encoding = PySequence_GetItem(tuple, 2);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001008 if (encoding == NULL) {
1009 PyErr_SetString(parser_error, "missed encoding");
1010 return NULL;
1011 }
1012 if (!PyUnicode_Check(encoding)) {
1013 PyErr_Format(parser_error,
1014 "encoding must be a string, found %.200s",
1015 Py_TYPE(encoding)->tp_name);
1016 Py_DECREF(encoding);
1017 return NULL;
1018 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001019 /* tuple isn't borrowed anymore here, need to DECREF */
1020 tuple = PySequence_GetSlice(tuple, 0, 2);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001021 if (tuple == NULL) {
1022 Py_DECREF(encoding);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001023 return NULL;
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001024 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001025 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001026 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +00001027 if (res != NULL) {
1028 if (res != build_node_children(tuple, res, &line_num)) {
1029 PyNode_Free(res);
1030 res = NULL;
1031 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001032 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +00001033 Py_ssize_t len;
Neal Norwitz3fcbea52007-08-26 04:51:28 +00001034 const char *temp;
Serhiy Storchaka06515832016-11-20 09:13:07 +02001035 temp = PyUnicode_AsUTF8AndSize(encoding, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001036 if (temp == NULL) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001037 PyNode_Free(res);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001038 Py_DECREF(encoding);
1039 Py_DECREF(tuple);
1040 return NULL;
1041 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00001042 res->n_str = (char *)PyObject_MALLOC(len + 1);
Victor Stinner3bd6abd2013-07-12 01:33:59 +02001043 if (res->n_str == NULL) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001044 PyNode_Free(res);
Victor Stinner3bd6abd2013-07-12 01:33:59 +02001045 Py_DECREF(encoding);
1046 Py_DECREF(tuple);
1047 PyErr_NoMemory();
1048 return NULL;
1049 }
1050 (void) memcpy(res->n_str, temp, len + 1);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001051 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001052 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001053 if (encoding != NULL) {
1054 Py_DECREF(encoding);
1055 Py_DECREF(tuple);
1056 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001057 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001058 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001059 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +00001060 * NONTERMINAL, we can't use it. Not sure the implementation
1061 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +00001062 */
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001063 PyObject *err = Py_BuildValue("Os", tuple,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001064 "Illegal component tuple.");
1065 PyErr_SetObject(parser_error, err);
1066 Py_XDECREF(err);
1067 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001068
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001069 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001070}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001071
1072
Fred Drake43f8f9b1998-04-13 16:25:46 +00001073static PyObject*
1074pickle_constructor = NULL;
1075
1076
1077static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00001078parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00001079{
Fred Drake268397f1998-04-29 14:16:32 +00001080 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00001081 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00001082 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00001083 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001084
Fred Drakec2683dd2001-07-17 19:32:05 +00001085 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001086 PyObject *newargs;
1087 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001088
Fred Drake2a6875e1999-09-20 22:32:18 +00001089 if ((empty_dict = PyDict_New()) == NULL)
1090 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00001091 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00001092 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00001093 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00001094 if (tuple != NULL) {
1095 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
1096 Py_DECREF(tuple);
1097 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001098 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001099 }
1100 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00001101 Py_XDECREF(empty_dict);
1102
Fred Drake43f8f9b1998-04-13 16:25:46 +00001103 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00001104}
Fred Drake43f8f9b1998-04-13 16:25:46 +00001105
1106
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001107/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00001108 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001109 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00001110 * We'd really have to write a wrapper around it all anyway to allow
1111 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001112 */
1113static PyMethodDef parser_functions[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001114 {"compilest", (PyCFunction)(void(*)(void))parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001115 PyDoc_STR("Compiles an ST object into a code object.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001116 {"expr", (PyCFunction)(void(*)(void))parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001117 PyDoc_STR("Creates an ST object from an expression.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001118 {"isexpr", (PyCFunction)(void(*)(void))parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001119 PyDoc_STR("Determines if an ST object was created from an expression.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001120 {"issuite", (PyCFunction)(void(*)(void))parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001121 PyDoc_STR("Determines if an ST object was created from a suite.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001122 {"suite", (PyCFunction)(void(*)(void))parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001123 PyDoc_STR("Creates an ST object from a suite.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001124 {"sequence2st", (PyCFunction)(void(*)(void))parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001125 PyDoc_STR("Creates an ST object from a tree representation.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001126 {"st2tuple", (PyCFunction)(void(*)(void))parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001127 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001128 {"st2list", (PyCFunction)(void(*)(void))parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001129 PyDoc_STR("Creates a list-tree representation of an ST.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001130 {"tuple2st", (PyCFunction)(void(*)(void))parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001131 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001132
Fred Drake43f8f9b1998-04-13 16:25:46 +00001133 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00001134 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00001135 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00001136
Fred Drake268397f1998-04-29 14:16:32 +00001137 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001138 };
1139
1140
Martin v. Löwis1a214512008-06-11 05:26:20 +00001141
1142static struct PyModuleDef parsermodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 PyModuleDef_HEAD_INIT,
1144 "parser",
1145 NULL,
1146 -1,
1147 parser_functions,
1148 NULL,
1149 NULL,
1150 NULL,
1151 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001152};
1153
1154PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00001155
Mark Hammond62b1ab12002-07-23 06:31:15 +00001156PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001157PyInit_parser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00001158{
Fred Drake13130bc2001-08-15 16:44:56 +00001159 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00001160
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001161 if (PyType_Ready(&PyST_Type) < 0)
1162 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001163 module = PyModule_Create(&parsermodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001164 if (module == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 return NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001166
Fred Drake7a15ba51999-09-09 14:21:52 +00001167 if (parser_error == 0)
1168 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001169
Tim Peters6a627252003-07-21 14:25:23 +00001170 if (parser_error == 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001171 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00001172 /* CAUTION: The code next used to skip bumping the refcount on
Martin v. Löwis1a214512008-06-11 05:26:20 +00001173 * parser_error. That's a disaster if PyInit_parser() gets called more
Tim Peters6a627252003-07-21 14:25:23 +00001174 * than once. By incref'ing, we ensure that each module dict that
1175 * gets created owns its reference to the shared parser_error object,
1176 * and the file static parser_error vrbl owns a reference too.
1177 */
1178 Py_INCREF(parser_error);
1179 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001180 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00001181
Fred Drakec2683dd2001-07-17 19:32:05 +00001182 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00001183 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001184
Fred Drake13130bc2001-08-15 16:44:56 +00001185 PyModule_AddStringConstant(module, "__copyright__",
1186 parser_copyright_string);
1187 PyModule_AddStringConstant(module, "__doc__",
1188 parser_doc_string);
1189 PyModule_AddStringConstant(module, "__version__",
1190 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001191
Fred Drake78bdb9b2001-07-19 20:17:15 +00001192 /* Register to support pickling.
1193 * If this fails, the import of this module will fail because an
1194 * exception will be raised here; should we clear the exception?
1195 */
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00001196 copyreg = PyImport_ImportModuleNoBlock("copyreg");
Fred Drake13130bc2001-08-15 16:44:56 +00001197 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001198 PyObject *func, *pickler;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001199 _Py_IDENTIFIER(pickle);
1200 _Py_IDENTIFIER(sequence2st);
1201 _Py_IDENTIFIER(_pickler);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001202
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001203 func = _PyObject_GetAttrId(copyreg, &PyId_pickle);
1204 pickle_constructor = _PyObject_GetAttrId(module, &PyId_sequence2st);
1205 pickler = _PyObject_GetAttrId(module, &PyId__pickler);
Fred Drakeff9ea482000-04-19 13:54:15 +00001206 Py_XINCREF(pickle_constructor);
1207 if ((func != NULL) && (pickle_constructor != NULL)
1208 && (pickler != NULL)) {
1209 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001210
Thomas Wouters477c8d52006-05-27 19:21:47 +00001211 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
1212 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00001213 Py_XDECREF(res);
1214 }
1215 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00001216 Py_XDECREF(pickle_constructor);
1217 Py_XDECREF(pickler);
1218 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001219 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00001220 return module;
Fred Drakeff9ea482000-04-19 13:54:15 +00001221}