blob: b0a749a3bc17b62c8d6fc7930ab3eaaa91047f63 [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040026 *
27 * To debug parser errors like
28 * "parser.ParserError: Expected node type 12, got 333."
29 * decode symbol numbers using the automatically-generated files
30 * Lib/symbol.h and Include/token.h.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000031 */
32
Fred Drakeff9ea482000-04-19 13:54:15 +000033#include "Python.h" /* general Python API */
Benjamin Petersonf216c942008-10-31 02:28:05 +000034#include "Python-ast.h" /* mod_ty */
Victor Stinner3bb183d2018-11-22 18:38:38 +010035#undef Yield /* undefine macro conflicting with <winbase.h> */
Victor Stinner5f2df882018-11-12 00:56:19 +010036#include "ast.h"
Fred Drakeff9ea482000-04-19 13:54:15 +000037#include "graminit.h" /* symbols defined in the grammar */
38#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000039#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000040#include "token.h" /* token definitions */
Victor Stinner5f2df882018-11-12 00:56:19 +010041 /* ISTERMINAL() / ISNONTERMINAL() */
Benjamin Petersonf216c942008-10-31 02:28:05 +000042#include "grammar.h"
43#include "parsetok.h"
Benjamin Petersonf216c942008-10-31 02:28:05 +000044
45extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000046
Fred Drake268397f1998-04-29 14:16:32 +000047#ifdef lint
48#include <note.h>
49#else
50#define NOTE(x)
51#endif
52
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000053/* String constants used to initialize module attributes.
54 *
55 */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020056static const char parser_copyright_string[] =
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000057"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000058University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
59Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
60Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000061
62
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000063PyDoc_STRVAR(parser_doc_string,
64"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000065
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020066static const char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000067
68
Martin v. Löwis18e16552006-02-15 17:27:45 +000069typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000070typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000071 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000072 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000073
Thomas Wouters7e474022000-07-16 12:04:32 +000074/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000075 * original copyright statement is included below, and continues to apply
76 * in full to the function immediately following. All other material is
77 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
78 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000079 * new naming conventions. Added arguments to provide support for creating
80 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000081 */
82
Guido van Rossum52f2c051993-11-10 12:53:24 +000083
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000084static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000085node2tuple(node *n, /* node to convert */
86 SeqMaker mkseq, /* create sequence */
87 SeqInserter addelem, /* func. to add elem. in seq. */
Thomas Wouters89f507f2006-12-13 04:49:30 +000088 int lineno, /* include line numbers? */
89 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000090{
Victor Stinnerdf4572c2013-07-12 01:35:10 +020091 PyObject *result = NULL, *w;
92
Guido van Rossum3d602e31996-07-21 02:33:56 +000093 if (n == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +020094 Py_RETURN_NONE;
Guido van Rossum3d602e31996-07-21 02:33:56 +000095 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +020096
Guido van Rossum3d602e31996-07-21 02:33:56 +000097 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000098 int i;
Fred Drake268397f1998-04-29 14:16:32 +000099
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200100 result = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
101 if (result == NULL)
102 goto error;
103
Christian Heimes217cfd12007-12-02 14:31:20 +0000104 w = PyLong_FromLong(TYPE(n));
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200105 if (w == NULL)
106 goto error;
107 (void) addelem(result, 0, w);
108
Fred Drakeff9ea482000-04-19 13:54:15 +0000109 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000110 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200111 if (w == NULL)
112 goto error;
113 (void) addelem(result, i+1, w);
Fred Drakeff9ea482000-04-19 13:54:15 +0000114 }
Tim Peters6a627252003-07-21 14:25:23 +0000115
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200116 if (TYPE(n) == encoding_decl) {
117 w = PyUnicode_FromString(STR(n));
118 if (w == NULL)
119 goto error;
120 (void) addelem(result, i+1, w);
121 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000122 }
123 else if (ISTERMINAL(TYPE(n))) {
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200124 result = mkseq(2 + lineno + col_offset);
125 if (result == NULL)
126 goto error;
127
128 w = PyLong_FromLong(TYPE(n));
129 if (w == NULL)
130 goto error;
131 (void) addelem(result, 0, w);
132
133 w = PyUnicode_FromString(STR(n));
134 if (w == NULL)
135 goto error;
136 (void) addelem(result, 1, w);
137
Serhiy Storchakae5362ea2018-04-19 01:55:37 +0300138 if (lineno) {
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200139 w = PyLong_FromLong(n->n_lineno);
140 if (w == NULL)
141 goto error;
142 (void) addelem(result, 2, w);
Fred Drakeff9ea482000-04-19 13:54:15 +0000143 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200144
Serhiy Storchakae5362ea2018-04-19 01:55:37 +0300145 if (col_offset) {
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200146 w = PyLong_FromLong(n->n_col_offset);
147 if (w == NULL)
148 goto error;
Serhiy Storchakae5362ea2018-04-19 01:55:37 +0300149 (void) addelem(result, 2 + lineno, w);
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200150 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000151 }
152 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000153 PyErr_SetString(PyExc_SystemError,
154 "unrecognized parse tree node type");
155 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000156 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200157 return result;
158
159error:
160 Py_XDECREF(result);
161 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000162}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000163/*
164 * End of material copyrighted by Stichting Mathematisch Centrum.
165 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000166
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000167
168
169/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000170 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000171 * field of the object type to identify which any given object represents.
172 * These should probably go in an external header to allow other extensions
173 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000174 */
175
Fred Drakec2683dd2001-07-17 19:32:05 +0000176#define PyST_EXPR 1
177#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000178
179
180/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000181 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000182 * naming style, but the code uses the new naming convention.
183 */
184
185static PyObject*
186parser_error = 0;
187
188
Fred Drakec2683dd2001-07-17 19:32:05 +0000189typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000190 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000191 node* st_node; /* the node* returned by the parser */
192 int st_type; /* EXPR or SUITE ? */
Benjamin Petersonf216c942008-10-31 02:28:05 +0000193 PyCompilerFlags st_flags; /* Parser and compiler flags */
Fred Drakec2683dd2001-07-17 19:32:05 +0000194} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000195
196
Jeremy Hylton938ace62002-07-17 16:30:39 +0000197static void parser_free(PyST_Object *st);
Jesus Ceae9c53182012-08-03 14:28:37 +0200198static PyObject* parser_sizeof(PyST_Object *, void *);
Mark Dickinson211c6252009-02-01 10:28:51 +0000199static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op);
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000200static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
201static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
202static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
203static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
204static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
Fred Drake503d8d61998-04-13 18:45:18 +0000205
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000206#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
207
208static PyMethodDef parser_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200209 {"compile", (PyCFunction)(void(*)(void))parser_compilest, PUBLIC_METHOD_TYPE,
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000210 PyDoc_STR("Compile this ST object into a code object.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200211 {"isexpr", (PyCFunction)(void(*)(void))parser_isexpr, PUBLIC_METHOD_TYPE,
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000212 PyDoc_STR("Determines if this ST object was created from an expression.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200213 {"issuite", (PyCFunction)(void(*)(void))parser_issuite, PUBLIC_METHOD_TYPE,
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000214 PyDoc_STR("Determines if this ST object was created from a suite.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200215 {"tolist", (PyCFunction)(void(*)(void))parser_st2list, PUBLIC_METHOD_TYPE,
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000216 PyDoc_STR("Creates a list-tree representation of this ST.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200217 {"totuple", (PyCFunction)(void(*)(void))parser_st2tuple, PUBLIC_METHOD_TYPE,
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000218 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Jesus Ceae9c53182012-08-03 14:28:37 +0200219 {"__sizeof__", (PyCFunction)parser_sizeof, METH_NOARGS,
220 PyDoc_STR("Returns size in memory, in bytes.")},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000221 {NULL, NULL, 0, NULL}
222};
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000223
Fred Drake268397f1998-04-29 14:16:32 +0000224static
Fred Drakec2683dd2001-07-17 19:32:05 +0000225PyTypeObject PyST_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000226 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000227 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000228 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000229 0, /* tp_itemsize */
230 (destructor)parser_free, /* tp_dealloc */
231 0, /* tp_print */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000232 0, /* tp_getattr */
Fred Drakeff9ea482000-04-19 13:54:15 +0000233 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000234 0, /* tp_reserved */
Fred Drakeff9ea482000-04-19 13:54:15 +0000235 0, /* tp_repr */
236 0, /* tp_as_number */
237 0, /* tp_as_sequence */
238 0, /* tp_as_mapping */
239 0, /* tp_hash */
240 0, /* tp_call */
241 0, /* tp_str */
242 0, /* tp_getattro */
243 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000244
245 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000246 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000247
Fred Drakeff9ea482000-04-19 13:54:15 +0000248 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000249
250 /* __doc__ */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000251 "Intermediate representation of a Python parse tree.",
252 0, /* tp_traverse */
253 0, /* tp_clear */
Mark Dickinson211c6252009-02-01 10:28:51 +0000254 parser_richcompare, /* tp_richcompare */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000255 0, /* tp_weaklistoffset */
256 0, /* tp_iter */
257 0, /* tp_iternext */
258 parser_methods, /* tp_methods */
Fred Drakec2683dd2001-07-17 19:32:05 +0000259}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000260
261
Mark Dickinson211c6252009-02-01 10:28:51 +0000262/* PyST_Type isn't subclassable, so just check ob_type */
263#define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type)
264
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000266parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000267{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000268 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000269
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000270 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000271 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000272
273 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000274 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000275
276 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000277 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000278
279 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000280 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000281
282 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000283 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000284
285 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000286 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000287
Fred Drakeff9ea482000-04-19 13:54:15 +0000288 if (v != 0)
289 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000290 }
291 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000292}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000293
Mark Dickinson211c6252009-02-01 10:28:51 +0000294/* parser_richcompare(PyObject* left, PyObject* right, int op)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000295 *
296 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
297 * This really just wraps a call to parser_compare_nodes() with some easy
298 * checks and protection code.
299 *
300 */
Mark Dickinson211c6252009-02-01 10:28:51 +0000301
Mark Dickinson211c6252009-02-01 10:28:51 +0000302static PyObject *
303parser_richcompare(PyObject *left, PyObject *right, int op)
Guido van Rossum47478871996-08-21 14:32:37 +0000304{
Mark Dickinson211c6252009-02-01 10:28:51 +0000305 int result;
Mark Dickinson211c6252009-02-01 10:28:51 +0000306
307 /* neither argument should be NULL, unless something's gone wrong */
308 if (left == NULL || right == NULL) {
309 PyErr_BadInternalCall();
310 return NULL;
311 }
312
313 /* both arguments should be instances of PyST_Object */
314 if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) {
stratakise8b19652017-11-02 11:32:54 +0100315 Py_RETURN_NOTIMPLEMENTED;
Mark Dickinson211c6252009-02-01 10:28:51 +0000316 }
317
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000318 if (left == right)
Mark Dickinson211c6252009-02-01 10:28:51 +0000319 /* if arguments are identical, they're equal */
320 result = 0;
321 else
322 result = parser_compare_nodes(((PyST_Object *)left)->st_node,
323 ((PyST_Object *)right)->st_node);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000324
stratakise8b19652017-11-02 11:32:54 +0100325 Py_RETURN_RICHCOMPARE(result, 0, op);
Fred Drakeff9ea482000-04-19 13:54:15 +0000326}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000327
Fred Drakec2683dd2001-07-17 19:32:05 +0000328/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000329 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000330 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000331 * 'wrapper' object that holds a node* and allows it to be passed around in
332 * Python code.
333 *
334 */
335static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000336parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000337{
Fred Drakec2683dd2001-07-17 19:32:05 +0000338 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000339
340 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000341 o->st_node = st;
342 o->st_type = type;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000343 o->st_flags.cf_flags = 0;
Guido van Rossum495da292019-03-07 12:38:08 -0800344 o->st_flags.cf_feature_version = PY_MINOR_VERSION;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000345 }
Fred Drake268397f1998-04-29 14:16:32 +0000346 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000347 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000348 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000349 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000350}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000351
352
Fred Drakec2683dd2001-07-17 19:32:05 +0000353/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000354 *
355 * This is called by a del statement that reduces the reference count to 0.
356 *
357 */
358static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000359parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000360{
Fred Drakec2683dd2001-07-17 19:32:05 +0000361 PyNode_Free(st->st_node);
362 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000363}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000364
Jesus Ceae9c53182012-08-03 14:28:37 +0200365static PyObject *
366parser_sizeof(PyST_Object *st, void *unused)
367{
368 Py_ssize_t res;
369
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +0200370 res = _PyObject_SIZE(Py_TYPE(st)) + _PyNode_SizeOf(st->st_node);
Jesus Ceae9c53182012-08-03 14:28:37 +0200371 return PyLong_FromSsize_t(res);
372}
373
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000374
Fred Drakec2683dd2001-07-17 19:32:05 +0000375/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000376 *
377 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000378 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000379 *
380 */
381static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000382parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000383{
Antoine Pitrou721738f2012-08-15 23:20:39 +0200384 int line_info = 0;
385 int col_info = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000386 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000387 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000388
Georg Brandl30704ea02008-07-23 15:07:12 +0000389 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000390
Martin v. Löwis1a214512008-06-11 05:26:20 +0000391 if (self == NULL || PyModule_Check(self)) {
Antoine Pitrou721738f2012-08-15 23:20:39 +0200392 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2tuple", keywords,
393 &PyST_Type, &self, &line_info,
394 &col_info);
Fred Drake268397f1998-04-29 14:16:32 +0000395 }
Fred Drake503d8d61998-04-13 18:45:18 +0000396 else
Antoine Pitrou721738f2012-08-15 23:20:39 +0200397 ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:totuple", &keywords[1],
398 &line_info, &col_info);
Fred Drake268397f1998-04-29 14:16:32 +0000399 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000400 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000401 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000402 * since it's known to work already.
403 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000404 res = node2tuple(((PyST_Object*)self)->st_node,
Antoine Pitrou721738f2012-08-15 23:20:39 +0200405 PyTuple_New, PyTuple_SetItem, line_info, col_info);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000406 }
407 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000408}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000409
410
Fred Drakec2683dd2001-07-17 19:32:05 +0000411/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000412 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000413 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000414 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000415 *
416 */
417static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000418parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000419{
Antoine Pitrou721738f2012-08-15 23:20:39 +0200420 int line_info = 0;
421 int col_info = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000422 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000423 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000424
Georg Brandl30704ea02008-07-23 15:07:12 +0000425 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000426
Martin v. Löwis1a214512008-06-11 05:26:20 +0000427 if (self == NULL || PyModule_Check(self))
Antoine Pitrou721738f2012-08-15 23:20:39 +0200428 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2list", keywords,
429 &PyST_Type, &self, &line_info,
430 &col_info);
Fred Drake503d8d61998-04-13 18:45:18 +0000431 else
Antoine Pitrou721738f2012-08-15 23:20:39 +0200432 ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:tolist", &keywords[1],
433 &line_info, &col_info);
Fred Drake503d8d61998-04-13 18:45:18 +0000434 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000435 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000436 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000437 * since it's known to work already.
438 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000439 res = node2tuple(self->st_node,
Antoine Pitrou721738f2012-08-15 23:20:39 +0200440 PyList_New, PyList_SetItem, line_info, col_info);
Guido van Rossum47478871996-08-21 14:32:37 +0000441 }
442 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000443}
Guido van Rossum47478871996-08-21 14:32:37 +0000444
445
Fred Drakec2683dd2001-07-17 19:32:05 +0000446/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000447 *
448 * This function creates code objects from the parse tree represented by
449 * the passed-in data object. An optional file name is passed in as well.
450 *
451 */
452static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000453parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000454{
Victor Stinner14e461d2013-08-26 22:28:21 +0200455 PyObject* res = NULL;
456 PyArena* arena = NULL;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000457 mod_ty mod;
Victor Stinner14e461d2013-08-26 22:28:21 +0200458 PyObject* filename = NULL;
Fred Drake503d8d61998-04-13 18:45:18 +0000459 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000460
Georg Brandl30704ea02008-07-23 15:07:12 +0000461 static char *keywords[] = {"st", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000462
Martin v. Löwis1a214512008-06-11 05:26:20 +0000463 if (self == NULL || PyModule_Check(self))
Victor Stinner14e461d2013-08-26 22:28:21 +0200464 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O&:compilest", keywords,
465 &PyST_Type, &self,
466 PyUnicode_FSDecoder, &filename);
Fred Drake503d8d61998-04-13 18:45:18 +0000467 else
Victor Stinner14e461d2013-08-26 22:28:21 +0200468 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O&:compile", &keywords[1],
469 PyUnicode_FSDecoder, &filename);
470 if (!ok)
471 goto error;
Fred Drake503d8d61998-04-13 18:45:18 +0000472
Victor Stinner14e461d2013-08-26 22:28:21 +0200473 if (filename == NULL) {
474 filename = PyUnicode_FromString("<syntax-tree>");
475 if (filename == NULL)
476 goto error;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000477 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000478
Victor Stinner14e461d2013-08-26 22:28:21 +0200479 arena = PyArena_New();
480 if (!arena)
481 goto error;
482
483 mod = PyAST_FromNodeObject(self->st_node, &self->st_flags,
484 filename, arena);
485 if (!mod)
486 goto error;
487
488 res = (PyObject *)PyAST_CompileObject(mod, filename,
489 &self->st_flags, -1, arena);
490error:
491 Py_XDECREF(filename);
492 if (arena != NULL)
493 PyArena_Free(arena);
494 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +0000495}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000496
497
498/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
499 * PyObject* parser_issuite(PyObject* self, PyObject* args)
500 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000501 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000502 * a statement suite, respectively. The return is a Python truth value.
503 *
504 */
505static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000506parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000507{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000508 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000509 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000510
Georg Brandl30704ea02008-07-23 15:07:12 +0000511 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000512
Martin v. Löwis1a214512008-06-11 05:26:20 +0000513 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000514 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000515 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000516 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000517 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000518
519 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000520 /* Check to see if the ST represents an expression or not. */
521 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000522 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000523 }
524 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000525}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000526
527
528static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000529parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000530{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000531 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000532 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000533
Georg Brandl30704ea02008-07-23 15:07:12 +0000534 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000535
Martin v. Löwis1a214512008-06-11 05:26:20 +0000536 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000537 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000538 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000539 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000540 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000541
542 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000543 /* Check to see if the ST represents an expression or not. */
544 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000545 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000546 }
547 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000548}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000549
550
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200551/* err_string(const char* message)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000552 *
553 * Sets the error string for an exception of type ParserError.
554 *
555 */
556static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200557err_string(const char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000558{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000559 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000560}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000561
562
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000563/* PyObject* parser_do_parse(PyObject* args, int type)
564 *
565 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000566 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000567 *
568 */
569static PyObject*
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200570parser_do_parse(PyObject *args, PyObject *kw, const char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000571{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000572 char* string = 0;
573 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000574 int flags = 0;
575 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000576
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000577 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000578
579 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000580 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
581 &_PyParser_Grammar,
582 (type == PyST_EXPR)
583 ? eval_input : file_input,
584 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 if (n) {
587 res = parser_newstobject(n, type);
Guido van Rossum495da292019-03-07 12:38:08 -0800588 if (res) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000589 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
Guido van Rossum495da292019-03-07 12:38:08 -0800590 ((PyST_Object *)res)->st_flags.cf_feature_version = PY_MINOR_VERSION;
591 }
Benjamin Petersonf216c942008-10-31 02:28:05 +0000592 }
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500593 else {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000594 PyParser_SetError(&err);
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500595 }
Benjamin Petersonf0cdbad2011-06-05 22:14:05 -0500596 PyParser_ClearError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000597 }
598 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000599}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000600
601
602/* PyObject* parser_expr(PyObject* self, PyObject* args)
603 * PyObject* parser_suite(PyObject* self, PyObject* args)
604 *
605 * External interfaces to the parser itself. Which is called determines if
606 * the parser attempts to recognize an expression ('eval' form) or statement
607 * suite ('exec' form). The real work is done by parser_do_parse() above.
608 *
609 */
610static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000611parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000612{
Fred Drake268397f1998-04-29 14:16:32 +0000613 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000614 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000615}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000616
617
618static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000619parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000620{
Fred Drake268397f1998-04-29 14:16:32 +0000621 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000622 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000623}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000624
625
626
Fred Drakec2683dd2001-07-17 19:32:05 +0000627/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000628 * object requires that the input tuple be valid without having to rely on
629 * catching an exception from the compiler. This is done to allow the
630 * compiler itself to remain fast, since most of its input will come from
631 * the parser directly, and therefore be known to be syntactically correct.
632 * This validation is done to ensure that we don't core dump the compile
633 * phase, returning an exception instead.
634 *
635 * Two aspects can be broken out in this code: creating a node tree from
636 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000637 * advantageous to expand the number of ST types to include funcdefs and
638 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000639 * here. They are not necessary, and not quite as useful in a raw form.
640 * For now, let's get expressions and suites working reliably.
641 */
642
643
Jeremy Hylton938ace62002-07-17 16:30:39 +0000644static node* build_node_tree(PyObject *tuple);
Benjamin Peterson53595c42016-06-02 11:30:18 -0700645
646static int
647validate_node(node *tree)
648{
649 int type = TYPE(tree);
650 int nch = NCH(tree);
651 dfa *nt_dfa;
652 state *dfa_state;
653 int pos, arc;
654
655 assert(ISNONTERMINAL(type));
656 type -= NT_OFFSET;
657 if (type >= _PyParser_Grammar.g_ndfas) {
658 PyErr_Format(parser_error, "Unrecognized node type %d.", TYPE(tree));
659 return 0;
660 }
661 nt_dfa = &_PyParser_Grammar.g_dfa[type];
662 REQ(tree, nt_dfa->d_type);
663
664 /* Run the DFA for this nonterminal. */
tyomitch1b304f92019-03-09 17:35:50 +0200665 dfa_state = nt_dfa->d_state;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700666 for (pos = 0; pos < nch; ++pos) {
667 node *ch = CHILD(tree, pos);
668 int ch_type = TYPE(ch);
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800669 if (ch_type == suite && TYPE(tree) == funcdef) {
670 /* This is the opposite hack of what we do in parser.c
671 (search for func_body_suite), except we don't ever
672 support type comments here. */
673 ch_type = func_body_suite;
674 }
Benjamin Peterson53595c42016-06-02 11:30:18 -0700675 for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
676 short a_label = dfa_state->s_arc[arc].a_lbl;
677 assert(a_label < _PyParser_Grammar.g_ll.ll_nlabels);
678 if (_PyParser_Grammar.g_ll.ll_label[a_label].lb_type == ch_type) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300679 /* The child is acceptable; if non-terminal, validate it recursively. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700680 if (ISNONTERMINAL(ch_type) && !validate_node(ch))
681 return 0;
682
683 /* Update the state, and move on to the next child. */
684 dfa_state = &nt_dfa->d_state[dfa_state->s_arc[arc].a_arrow];
685 goto arc_found;
686 }
687 }
688 /* What would this state have accepted? */
689 {
690 short a_label = dfa_state->s_arc->a_lbl;
691 int next_type;
692 if (!a_label) /* Wouldn't accept any more children */
693 goto illegal_num_children;
694
695 next_type = _PyParser_Grammar.g_ll.ll_label[a_label].lb_type;
696 if (ISNONTERMINAL(next_type))
697 PyErr_Format(parser_error, "Expected node type %d, got %d.",
698 next_type, ch_type);
699 else
700 PyErr_Format(parser_error, "Illegal terminal: expected %s.",
701 _PyParser_TokenNames[next_type]);
702 return 0;
703 }
704
705arc_found:
706 continue;
707 }
708 /* Are we in a final state? If so, return 1 for successful validation. */
709 for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
710 if (!dfa_state->s_arc[arc].a_lbl) {
711 return 1;
712 }
713 }
714
715illegal_num_children:
716 PyErr_Format(parser_error,
717 "Illegal number of children for %s node.", nt_dfa->d_name);
718 return 0;
719}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000720
Fred Drakec2683dd2001-07-17 19:32:05 +0000721/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000722 *
723 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000724 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000725 * tuple can be validated. It does this by checking the first code of the
726 * tuple, and, if acceptable, builds the internal representation. If this
727 * step succeeds, the internal representation is validated as fully as
Benjamin Peterson53595c42016-06-02 11:30:18 -0700728 * possible with the recursive validate_node() routine defined above.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000729 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000730 * This function must be changed if support is to be added for PyST_FRAGMENT
731 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000732 *
733 */
734static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000735parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000736{
Fred Drake268397f1998-04-29 14:16:32 +0000737 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000738 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000739 PyObject *tuple;
740 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000741
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000742 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000743
Fred Drakec2683dd2001-07-17 19:32:05 +0000744 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000745 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000746 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000747 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000748 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000749 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000750 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000751 }
752 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000753 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000754 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000755 tree = build_node_tree(tuple);
756 if (tree != 0) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300757 node *validation_root = NULL;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700758 int tree_type = 0;
759 switch (TYPE(tree)) {
760 case eval_input:
Fred Drake0ac9b072000-09-12 21:58:06 +0000761 /* Might be an eval form. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700762 tree_type = PyST_EXPR;
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300763 validation_root = tree;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700764 break;
765 case encoding_decl:
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000766 /* This looks like an encoding_decl so far. */
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300767 if (NCH(tree) == 1) {
768 tree_type = PyST_SUITE;
769 validation_root = CHILD(tree, 0);
770 }
771 else {
Benjamin Peterson53595c42016-06-02 11:30:18 -0700772 err_string("Error Parsing encoding_decl");
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300773 }
774 break;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700775 case file_input:
776 /* This looks like an exec form so far. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700777 tree_type = PyST_SUITE;
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300778 validation_root = tree;
Benjamin Peterson53595c42016-06-02 11:30:18 -0700779 break;
780 default:
Fred Drake0ac9b072000-09-12 21:58:06 +0000781 /* This is a fragment, at best. */
Fred Drake661ea262000-10-24 19:57:45 +0000782 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000783 }
Benjamin Peterson53595c42016-06-02 11:30:18 -0700784
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300785 if (validation_root != NULL && validate_node(validation_root))
Benjamin Peterson53595c42016-06-02 11:30:18 -0700786 st = parser_newstobject(tree, tree_type);
787 else
788 PyNode_Free(tree);
Guido van Rossum47478871996-08-21 14:32:37 +0000789 }
Andrew Svetlov737fb892012-12-18 21:14:22 +0200790 /* Make sure we raise an exception on all errors. We should never
Guido van Rossum47478871996-08-21 14:32:37 +0000791 * get this, but we'd do well to be sure something is done.
792 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000793 if (st == NULL && !PyErr_Occurred())
794 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000795
Fred Drakec2683dd2001-07-17 19:32:05 +0000796 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000797}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000798
799
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000800/* node* build_node_children()
801 *
802 * Iterate across the children of the current non-terminal node and build
803 * their structures. If successful, return the root of this portion of
804 * the tree, otherwise, 0. Any required exception will be specified already,
805 * and no memory will have been deallocated.
806 *
807 */
808static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000809build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000810{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000811 Py_ssize_t len = PyObject_Size(tuple);
812 Py_ssize_t i;
813 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000814
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300815 if (len < 0) {
816 return NULL;
817 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000818 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000819 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000820 PyObject* elem = PySequence_GetItem(tuple, i);
821 int ok = elem != NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200822 int type = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000823 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000824
Fred Drakeff9ea482000-04-19 13:54:15 +0000825 if (ok)
826 ok = PySequence_Check(elem);
827 if (ok) {
828 PyObject *temp = PySequence_GetItem(elem, 0);
829 if (temp == NULL)
830 ok = 0;
831 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000832 ok = PyLong_Check(temp);
Serhiy Storchaka78980432013-01-15 01:12:17 +0200833 if (ok) {
834 type = _PyLong_AsInt(temp);
835 if (type == -1 && PyErr_Occurred()) {
836 Py_DECREF(temp);
837 Py_DECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300838 return NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200839 }
840 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000841 Py_DECREF(temp);
842 }
843 }
844 if (!ok) {
Victor Stinner5f8d4852014-01-02 11:49:27 +0100845 PyObject *err = Py_BuildValue("Os", elem,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000846 "Illegal node construct.");
847 PyErr_SetObject(parser_error, err);
848 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000849 Py_XDECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300850 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000851 }
852 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000853 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000854 PyObject *temp;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000855 const char *temp_str;
Guido van Rossum47478871996-08-21 14:32:37 +0000856
Fred Drake0ac9b072000-09-12 21:58:06 +0000857 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000858 err_string("terminal nodes must have 2 or 3 entries");
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300859 Py_DECREF(elem);
860 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000861 }
862 temp = PySequence_GetItem(elem, 1);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300863 if (temp == NULL) {
864 Py_DECREF(elem);
865 return NULL;
866 }
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000867 if (!PyUnicode_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000868 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000869 "second item in terminal node must be a string,"
870 " found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000871 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000872 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000873 Py_DECREF(elem);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300874 return NULL;
Fred Drake0ac9b072000-09-12 21:58:06 +0000875 }
876 if (len == 3) {
877 PyObject *o = PySequence_GetItem(elem, 2);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300878 if (o == NULL) {
879 Py_DECREF(temp);
880 Py_DECREF(elem);
881 return NULL;
882 }
883 if (PyLong_Check(o)) {
884 int num = _PyLong_AsInt(o);
885 if (num == -1 && PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000886 Py_DECREF(o);
887 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 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300891 *line_num = num;
Fred Drakeff9ea482000-04-19 13:54:15 +0000892 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300893 else {
894 PyErr_Format(parser_error,
895 "third item in terminal node must be an"
896 " integer, found %s",
897 Py_TYPE(temp)->tp_name);
898 Py_DECREF(o);
899 Py_DECREF(temp);
900 Py_DECREF(elem);
901 return NULL;
902 }
903 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000904 }
Serhiy Storchaka06515832016-11-20 09:13:07 +0200905 temp_str = PyUnicode_AsUTF8AndSize(temp, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000906 if (temp_str == NULL) {
907 Py_DECREF(temp);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300908 Py_DECREF(elem);
909 return NULL;
Alexander Belopolskye239d232010-12-08 23:31:48 +0000910 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000911 strn = (char *)PyObject_MALLOC(len + 1);
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200912 if (strn == NULL) {
913 Py_DECREF(temp);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300914 Py_DECREF(elem);
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200915 PyErr_NoMemory();
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300916 return NULL;
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200917 }
918 (void) memcpy(strn, temp_str, len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000919 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000920 }
921 else if (!ISNONTERMINAL(type)) {
922 /*
923 * It has to be one or the other; this is an error.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200924 * Raise an exception.
Fred Drakeff9ea482000-04-19 13:54:15 +0000925 */
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300926 PyObject *err = Py_BuildValue("Os", elem, "unknown node type.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000927 PyErr_SetObject(parser_error, err);
928 Py_XDECREF(err);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300929 Py_DECREF(elem);
930 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000931 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000932 err = PyNode_AddChild(root, type, strn, *line_num, 0, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000933 if (err == E_NOMEM) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300934 Py_DECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000935 PyObject_FREE(strn);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300936 PyErr_NoMemory();
937 return NULL;
Fred Drake8b55b2d2001-12-05 22:10:44 +0000938 }
939 if (err == E_OVERFLOW) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300940 Py_DECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000941 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000942 PyErr_SetString(PyExc_ValueError,
943 "unsupported number of child nodes");
944 return NULL;
945 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000946
Fred Drakeff9ea482000-04-19 13:54:15 +0000947 if (ISNONTERMINAL(type)) {
948 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000949
Fred Drakeff9ea482000-04-19 13:54:15 +0000950 if (new_child != build_node_children(elem, new_child, line_num)) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300951 Py_DECREF(elem);
952 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000953 }
954 }
955 else if (type == NEWLINE) { /* It's true: we increment the */
956 ++(*line_num); /* line number *after* the newline! */
957 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300958 Py_DECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000959 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000960 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000961}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000962
963
964static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000965build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000966{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000967 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000968 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000969 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000970
Guido van Rossum47478871996-08-21 14:32:37 +0000971 if (temp != NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000972 num = PyLong_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000973 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000974 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000975 /*
976 * The tuple is simple, but it doesn't start with a start symbol.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200977 * Raise an exception now and be done with it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000978 */
Victor Stinner6684bdf2013-07-17 00:13:52 +0200979 tuple = Py_BuildValue("Os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000980 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000981 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000982 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000983 }
984 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000985 /*
986 * Not efficient, but that can be handled later.
987 */
988 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000989 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000990
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000991 if (num == encoding_decl) {
992 encoding = PySequence_GetItem(tuple, 2);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300993 if (encoding == NULL) {
994 PyErr_SetString(parser_error, "missed encoding");
995 return NULL;
996 }
997 if (!PyUnicode_Check(encoding)) {
998 PyErr_Format(parser_error,
999 "encoding must be a string, found %.200s",
1000 Py_TYPE(encoding)->tp_name);
1001 Py_DECREF(encoding);
1002 return NULL;
1003 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001004 /* tuple isn't borrowed anymore here, need to DECREF */
1005 tuple = PySequence_GetSlice(tuple, 0, 2);
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001006 if (tuple == NULL) {
1007 Py_DECREF(encoding);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001008 return NULL;
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001009 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001010 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001011 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +00001012 if (res != NULL) {
1013 if (res != build_node_children(tuple, res, &line_num)) {
1014 PyNode_Free(res);
1015 res = NULL;
1016 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001017 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +00001018 Py_ssize_t len;
Neal Norwitz3fcbea52007-08-26 04:51:28 +00001019 const char *temp;
Serhiy Storchaka06515832016-11-20 09:13:07 +02001020 temp = PyUnicode_AsUTF8AndSize(encoding, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001021 if (temp == NULL) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001022 PyNode_Free(res);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001023 Py_DECREF(encoding);
1024 Py_DECREF(tuple);
1025 return NULL;
1026 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00001027 res->n_str = (char *)PyObject_MALLOC(len + 1);
Victor Stinner3bd6abd2013-07-12 01:33:59 +02001028 if (res->n_str == NULL) {
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001029 PyNode_Free(res);
Victor Stinner3bd6abd2013-07-12 01:33:59 +02001030 Py_DECREF(encoding);
1031 Py_DECREF(tuple);
1032 PyErr_NoMemory();
1033 return NULL;
1034 }
1035 (void) memcpy(res->n_str, temp, len + 1);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001036 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001037 }
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001038 if (encoding != NULL) {
1039 Py_DECREF(encoding);
1040 Py_DECREF(tuple);
1041 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001042 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001043 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001044 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +00001045 * NONTERMINAL, we can't use it. Not sure the implementation
1046 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +00001047 */
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001048 PyObject *err = Py_BuildValue("Os", tuple,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001049 "Illegal component tuple.");
1050 PyErr_SetObject(parser_error, err);
1051 Py_XDECREF(err);
1052 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001053
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001054 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001055}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001056
1057
Fred Drake43f8f9b1998-04-13 16:25:46 +00001058static PyObject*
1059pickle_constructor = NULL;
1060
1061
1062static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00001063parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00001064{
Fred Drake268397f1998-04-29 14:16:32 +00001065 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00001066 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00001067 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00001068 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001069
Fred Drakec2683dd2001-07-17 19:32:05 +00001070 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001071 PyObject *newargs;
1072 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001073
Fred Drake2a6875e1999-09-20 22:32:18 +00001074 if ((empty_dict = PyDict_New()) == NULL)
1075 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00001076 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00001077 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00001078 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00001079 if (tuple != NULL) {
1080 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
1081 Py_DECREF(tuple);
1082 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001083 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001084 }
1085 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00001086 Py_XDECREF(empty_dict);
1087
Fred Drake43f8f9b1998-04-13 16:25:46 +00001088 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00001089}
Fred Drake43f8f9b1998-04-13 16:25:46 +00001090
1091
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001092/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00001093 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001094 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00001095 * We'd really have to write a wrapper around it all anyway to allow
1096 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001097 */
1098static PyMethodDef parser_functions[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001099 {"compilest", (PyCFunction)(void(*)(void))parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001100 PyDoc_STR("Compiles an ST object into a code object.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001101 {"expr", (PyCFunction)(void(*)(void))parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001102 PyDoc_STR("Creates an ST object from an expression.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001103 {"isexpr", (PyCFunction)(void(*)(void))parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001104 PyDoc_STR("Determines if an ST object was created from an expression.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001105 {"issuite", (PyCFunction)(void(*)(void))parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001106 PyDoc_STR("Determines if an ST object was created from a suite.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001107 {"suite", (PyCFunction)(void(*)(void))parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001108 PyDoc_STR("Creates an ST object from a suite.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001109 {"sequence2st", (PyCFunction)(void(*)(void))parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001110 PyDoc_STR("Creates an ST object from a tree representation.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001111 {"st2tuple", (PyCFunction)(void(*)(void))parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001112 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001113 {"st2list", (PyCFunction)(void(*)(void))parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001114 PyDoc_STR("Creates a list-tree representation of an ST.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001115 {"tuple2st", (PyCFunction)(void(*)(void))parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001116 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001117
Fred Drake43f8f9b1998-04-13 16:25:46 +00001118 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00001119 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00001120 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00001121
Fred Drake268397f1998-04-29 14:16:32 +00001122 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001123 };
1124
1125
Martin v. Löwis1a214512008-06-11 05:26:20 +00001126
1127static struct PyModuleDef parsermodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 PyModuleDef_HEAD_INIT,
1129 "parser",
1130 NULL,
1131 -1,
1132 parser_functions,
1133 NULL,
1134 NULL,
1135 NULL,
1136 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001137};
1138
1139PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00001140
Mark Hammond62b1ab12002-07-23 06:31:15 +00001141PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001142PyInit_parser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00001143{
Fred Drake13130bc2001-08-15 16:44:56 +00001144 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00001145
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001146 if (PyType_Ready(&PyST_Type) < 0)
1147 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001148 module = PyModule_Create(&parsermodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001149 if (module == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 return NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001151
Fred Drake7a15ba51999-09-09 14:21:52 +00001152 if (parser_error == 0)
1153 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001154
Tim Peters6a627252003-07-21 14:25:23 +00001155 if (parser_error == 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001156 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00001157 /* CAUTION: The code next used to skip bumping the refcount on
Martin v. Löwis1a214512008-06-11 05:26:20 +00001158 * parser_error. That's a disaster if PyInit_parser() gets called more
Tim Peters6a627252003-07-21 14:25:23 +00001159 * than once. By incref'ing, we ensure that each module dict that
1160 * gets created owns its reference to the shared parser_error object,
1161 * and the file static parser_error vrbl owns a reference too.
1162 */
1163 Py_INCREF(parser_error);
1164 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001165 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00001166
Fred Drakec2683dd2001-07-17 19:32:05 +00001167 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00001168 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001169
Fred Drake13130bc2001-08-15 16:44:56 +00001170 PyModule_AddStringConstant(module, "__copyright__",
1171 parser_copyright_string);
1172 PyModule_AddStringConstant(module, "__doc__",
1173 parser_doc_string);
1174 PyModule_AddStringConstant(module, "__version__",
1175 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001176
Fred Drake78bdb9b2001-07-19 20:17:15 +00001177 /* Register to support pickling.
1178 * If this fails, the import of this module will fail because an
1179 * exception will be raised here; should we clear the exception?
1180 */
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00001181 copyreg = PyImport_ImportModuleNoBlock("copyreg");
Fred Drake13130bc2001-08-15 16:44:56 +00001182 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001183 PyObject *func, *pickler;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001184 _Py_IDENTIFIER(pickle);
1185 _Py_IDENTIFIER(sequence2st);
1186 _Py_IDENTIFIER(_pickler);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001187
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001188 func = _PyObject_GetAttrId(copyreg, &PyId_pickle);
1189 pickle_constructor = _PyObject_GetAttrId(module, &PyId_sequence2st);
1190 pickler = _PyObject_GetAttrId(module, &PyId__pickler);
Fred Drakeff9ea482000-04-19 13:54:15 +00001191 Py_XINCREF(pickle_constructor);
1192 if ((func != NULL) && (pickle_constructor != NULL)
1193 && (pickler != NULL)) {
1194 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001195
Thomas Wouters477c8d52006-05-27 19:21:47 +00001196 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
1197 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00001198 Py_XDECREF(res);
1199 }
1200 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00001201 Py_XDECREF(pickle_constructor);
1202 Py_XDECREF(pickler);
1203 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001204 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00001205 return module;
Fred Drakeff9ea482000-04-19 13:54:15 +00001206}