blob: 82770a52ff4a196f92c1509761ab6ea14a546f02 [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040026 *
27 * To debug parser errors like
28 * "parser.ParserError: Expected node type 12, got 333."
29 * decode symbol numbers using the automatically-generated files
30 * Lib/symbol.h and Include/token.h.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000031 */
32
Fred Drakeff9ea482000-04-19 13:54:15 +000033#include "Python.h" /* general Python API */
Benjamin Petersonf216c942008-10-31 02:28:05 +000034#include "Python-ast.h" /* mod_ty */
Fred Drakeff9ea482000-04-19 13:54:15 +000035#include "graminit.h" /* symbols defined in the grammar */
36#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000037#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000038#include "token.h" /* token definitions */
Benjamin Petersonf216c942008-10-31 02:28:05 +000039#include "grammar.h"
40#include "parsetok.h"
Fred Drakeff9ea482000-04-19 13:54:15 +000041 /* ISTERMINAL() / ISNONTERMINAL() */
Benjamin Petersonf216c942008-10-31 02:28:05 +000042#undef Yield
43#include "ast.h"
Benjamin Petersonf216c942008-10-31 02:28:05 +000044
45extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000046
Fred Drake268397f1998-04-29 14:16:32 +000047#ifdef lint
48#include <note.h>
49#else
50#define NOTE(x)
51#endif
52
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000053/* String constants used to initialize module attributes.
54 *
55 */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020056static const char parser_copyright_string[] =
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000057"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000058University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
59Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
60Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000061
62
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000063PyDoc_STRVAR(parser_doc_string,
64"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000065
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020066static const char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000067
68
Martin v. Löwis18e16552006-02-15 17:27:45 +000069typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000070typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000071 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000072 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000073
Thomas Wouters7e474022000-07-16 12:04:32 +000074/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000075 * original copyright statement is included below, and continues to apply
76 * in full to the function immediately following. All other material is
77 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
78 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000079 * new naming conventions. Added arguments to provide support for creating
80 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000081 */
82
Guido van Rossum52f2c051993-11-10 12:53:24 +000083
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000084static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000085node2tuple(node *n, /* node to convert */
86 SeqMaker mkseq, /* create sequence */
87 SeqInserter addelem, /* func. to add elem. in seq. */
Thomas Wouters89f507f2006-12-13 04:49:30 +000088 int lineno, /* include line numbers? */
89 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000090{
Victor Stinnerdf4572c2013-07-12 01:35:10 +020091 PyObject *result = NULL, *w;
92
Guido van Rossum3d602e31996-07-21 02:33:56 +000093 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000094 Py_INCREF(Py_None);
Victor Stinnerdf4572c2013-07-12 01:35:10 +020095 return Py_None;
Guido van Rossum3d602e31996-07-21 02:33:56 +000096 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +020097
Guido van Rossum3d602e31996-07-21 02:33:56 +000098 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000099 int i;
Fred Drake268397f1998-04-29 14:16:32 +0000100
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200101 result = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
102 if (result == NULL)
103 goto error;
104
Christian Heimes217cfd12007-12-02 14:31:20 +0000105 w = PyLong_FromLong(TYPE(n));
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200106 if (w == NULL)
107 goto error;
108 (void) addelem(result, 0, w);
109
Fred Drakeff9ea482000-04-19 13:54:15 +0000110 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000111 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200112 if (w == NULL)
113 goto error;
114 (void) addelem(result, i+1, w);
Fred Drakeff9ea482000-04-19 13:54:15 +0000115 }
Tim Peters6a627252003-07-21 14:25:23 +0000116
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200117 if (TYPE(n) == encoding_decl) {
118 w = PyUnicode_FromString(STR(n));
119 if (w == NULL)
120 goto error;
121 (void) addelem(result, i+1, w);
122 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000123 }
124 else if (ISTERMINAL(TYPE(n))) {
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200125 result = mkseq(2 + lineno + col_offset);
126 if (result == NULL)
127 goto error;
128
129 w = PyLong_FromLong(TYPE(n));
130 if (w == NULL)
131 goto error;
132 (void) addelem(result, 0, w);
133
134 w = PyUnicode_FromString(STR(n));
135 if (w == NULL)
136 goto error;
137 (void) addelem(result, 1, w);
138
139 if (lineno == 1) {
140 w = PyLong_FromLong(n->n_lineno);
141 if (w == NULL)
142 goto error;
143 (void) addelem(result, 2, w);
Fred Drakeff9ea482000-04-19 13:54:15 +0000144 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200145
146 if (col_offset == 1) {
147 w = PyLong_FromLong(n->n_col_offset);
148 if (w == NULL)
149 goto error;
150 (void) addelem(result, 3, w);
151 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000152 }
153 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000154 PyErr_SetString(PyExc_SystemError,
155 "unrecognized parse tree node type");
156 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000157 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200158 return result;
159
160error:
161 Py_XDECREF(result);
162 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000163}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000164/*
165 * End of material copyrighted by Stichting Mathematisch Centrum.
166 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000167
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000168
169
170/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000171 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000172 * field of the object type to identify which any given object represents.
173 * These should probably go in an external header to allow other extensions
174 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000175 */
176
Fred Drakec2683dd2001-07-17 19:32:05 +0000177#define PyST_EXPR 1
178#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000179
180
181/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000182 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000183 * naming style, but the code uses the new naming convention.
184 */
185
186static PyObject*
187parser_error = 0;
188
189
Fred Drakec2683dd2001-07-17 19:32:05 +0000190typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000191 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000192 node* st_node; /* the node* returned by the parser */
193 int st_type; /* EXPR or SUITE ? */
Benjamin Petersonf216c942008-10-31 02:28:05 +0000194 PyCompilerFlags st_flags; /* Parser and compiler flags */
Fred Drakec2683dd2001-07-17 19:32:05 +0000195} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000196
197
Jeremy Hylton938ace62002-07-17 16:30:39 +0000198static void parser_free(PyST_Object *st);
Jesus Ceae9c53182012-08-03 14:28:37 +0200199static PyObject* parser_sizeof(PyST_Object *, void *);
Mark Dickinson211c6252009-02-01 10:28:51 +0000200static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op);
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000201static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
202static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
203static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
204static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
205static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
Fred Drake503d8d61998-04-13 18:45:18 +0000206
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000207#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
208
209static PyMethodDef parser_methods[] = {
210 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
211 PyDoc_STR("Compile this ST object into a code object.")},
212 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
213 PyDoc_STR("Determines if this ST object was created from an expression.")},
214 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
215 PyDoc_STR("Determines if this ST object was created from a suite.")},
216 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
217 PyDoc_STR("Creates a list-tree representation of this ST.")},
218 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
219 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Jesus Ceae9c53182012-08-03 14:28:37 +0200220 {"__sizeof__", (PyCFunction)parser_sizeof, METH_NOARGS,
221 PyDoc_STR("Returns size in memory, in bytes.")},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000222 {NULL, NULL, 0, NULL}
223};
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000224
Fred Drake268397f1998-04-29 14:16:32 +0000225static
Fred Drakec2683dd2001-07-17 19:32:05 +0000226PyTypeObject PyST_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000227 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000228 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000229 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000230 0, /* tp_itemsize */
231 (destructor)parser_free, /* tp_dealloc */
232 0, /* tp_print */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000233 0, /* tp_getattr */
Fred Drakeff9ea482000-04-19 13:54:15 +0000234 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000235 0, /* tp_reserved */
Fred Drakeff9ea482000-04-19 13:54:15 +0000236 0, /* tp_repr */
237 0, /* tp_as_number */
238 0, /* tp_as_sequence */
239 0, /* tp_as_mapping */
240 0, /* tp_hash */
241 0, /* tp_call */
242 0, /* tp_str */
243 0, /* tp_getattro */
244 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000245
246 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000247 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000248
Fred Drakeff9ea482000-04-19 13:54:15 +0000249 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000250
251 /* __doc__ */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000252 "Intermediate representation of a Python parse tree.",
253 0, /* tp_traverse */
254 0, /* tp_clear */
Mark Dickinson211c6252009-02-01 10:28:51 +0000255 parser_richcompare, /* tp_richcompare */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000256 0, /* tp_weaklistoffset */
257 0, /* tp_iter */
258 0, /* tp_iternext */
259 parser_methods, /* tp_methods */
Fred Drakec2683dd2001-07-17 19:32:05 +0000260}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000261
262
Mark Dickinson211c6252009-02-01 10:28:51 +0000263/* PyST_Type isn't subclassable, so just check ob_type */
264#define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type)
265
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000266static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000267parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000268{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000269 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000270
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000271 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000272 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000273
274 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000275 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000276
277 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000278 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000279
280 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000281 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000282
283 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000284 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000285
286 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000287 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000288
Fred Drakeff9ea482000-04-19 13:54:15 +0000289 if (v != 0)
290 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000291 }
292 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000293}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000294
Mark Dickinson211c6252009-02-01 10:28:51 +0000295/* parser_richcompare(PyObject* left, PyObject* right, int op)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000296 *
297 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
298 * This really just wraps a call to parser_compare_nodes() with some easy
299 * checks and protection code.
300 *
301 */
Mark Dickinson211c6252009-02-01 10:28:51 +0000302
303#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
304
305static PyObject *
306parser_richcompare(PyObject *left, PyObject *right, int op)
Guido van Rossum47478871996-08-21 14:32:37 +0000307{
Mark Dickinson211c6252009-02-01 10:28:51 +0000308 int result;
309 PyObject *v;
310
311 /* neither argument should be NULL, unless something's gone wrong */
312 if (left == NULL || right == NULL) {
313 PyErr_BadInternalCall();
314 return NULL;
315 }
316
317 /* both arguments should be instances of PyST_Object */
318 if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) {
319 v = Py_NotImplemented;
320 goto finished;
321 }
322
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000323 if (left == right)
Mark Dickinson211c6252009-02-01 10:28:51 +0000324 /* if arguments are identical, they're equal */
325 result = 0;
326 else
327 result = parser_compare_nodes(((PyST_Object *)left)->st_node,
328 ((PyST_Object *)right)->st_node);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000329
Mark Dickinson211c6252009-02-01 10:28:51 +0000330 /* Convert return value to a Boolean */
331 switch (op) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000332 case Py_EQ:
Mark Dickinson211c6252009-02-01 10:28:51 +0000333 v = TEST_COND(result == 0);
334 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000335 case Py_NE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000336 v = TEST_COND(result != 0);
337 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000338 case Py_LE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000339 v = TEST_COND(result <= 0);
340 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000341 case Py_GE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000342 v = TEST_COND(result >= 0);
343 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000344 case Py_LT:
Mark Dickinson211c6252009-02-01 10:28:51 +0000345 v = TEST_COND(result < 0);
346 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000347 case Py_GT:
Mark Dickinson211c6252009-02-01 10:28:51 +0000348 v = TEST_COND(result > 0);
349 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000350 default:
Mark Dickinson211c6252009-02-01 10:28:51 +0000351 PyErr_BadArgument();
352 return NULL;
353 }
354 finished:
355 Py_INCREF(v);
356 return v;
Fred Drakeff9ea482000-04-19 13:54:15 +0000357}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000358
Fred Drakec2683dd2001-07-17 19:32:05 +0000359/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000360 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000361 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000362 * 'wrapper' object that holds a node* and allows it to be passed around in
363 * Python code.
364 *
365 */
366static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000367parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000368{
Fred Drakec2683dd2001-07-17 19:32:05 +0000369 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000370
371 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000372 o->st_node = st;
373 o->st_type = type;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000374 o->st_flags.cf_flags = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000375 }
Fred Drake268397f1998-04-29 14:16:32 +0000376 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000377 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000378 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000379 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000380}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000381
382
Fred Drakec2683dd2001-07-17 19:32:05 +0000383/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000384 *
385 * This is called by a del statement that reduces the reference count to 0.
386 *
387 */
388static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000389parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000390{
Fred Drakec2683dd2001-07-17 19:32:05 +0000391 PyNode_Free(st->st_node);
392 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000393}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000394
Jesus Ceae9c53182012-08-03 14:28:37 +0200395static PyObject *
396parser_sizeof(PyST_Object *st, void *unused)
397{
398 Py_ssize_t res;
399
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +0200400 res = _PyObject_SIZE(Py_TYPE(st)) + _PyNode_SizeOf(st->st_node);
Jesus Ceae9c53182012-08-03 14:28:37 +0200401 return PyLong_FromSsize_t(res);
402}
403
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000404
Fred Drakec2683dd2001-07-17 19:32:05 +0000405/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000406 *
407 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000408 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000409 *
410 */
411static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000412parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000413{
Antoine Pitrou721738f2012-08-15 23:20:39 +0200414 int line_info = 0;
415 int col_info = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000416 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000417 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000418
Georg Brandl30704ea02008-07-23 15:07:12 +0000419 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000420
Martin v. Löwis1a214512008-06-11 05:26:20 +0000421 if (self == NULL || PyModule_Check(self)) {
Antoine Pitrou721738f2012-08-15 23:20:39 +0200422 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2tuple", keywords,
423 &PyST_Type, &self, &line_info,
424 &col_info);
Fred Drake268397f1998-04-29 14:16:32 +0000425 }
Fred Drake503d8d61998-04-13 18:45:18 +0000426 else
Antoine Pitrou721738f2012-08-15 23:20:39 +0200427 ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:totuple", &keywords[1],
428 &line_info, &col_info);
Fred Drake268397f1998-04-29 14:16:32 +0000429 if (ok != 0) {
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(((PyST_Object*)self)->st_node,
Antoine Pitrou721738f2012-08-15 23:20:39 +0200435 PyTuple_New, PyTuple_SetItem, line_info, col_info);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000436 }
437 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000438}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000439
440
Fred Drakec2683dd2001-07-17 19:32:05 +0000441/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000442 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000443 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000444 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000445 *
446 */
447static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000448parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000449{
Antoine Pitrou721738f2012-08-15 23:20:39 +0200450 int line_info = 0;
451 int col_info = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000452 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000453 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000454
Georg Brandl30704ea02008-07-23 15:07:12 +0000455 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000456
Martin v. Löwis1a214512008-06-11 05:26:20 +0000457 if (self == NULL || PyModule_Check(self))
Antoine Pitrou721738f2012-08-15 23:20:39 +0200458 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2list", keywords,
459 &PyST_Type, &self, &line_info,
460 &col_info);
Fred Drake503d8d61998-04-13 18:45:18 +0000461 else
Antoine Pitrou721738f2012-08-15 23:20:39 +0200462 ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:tolist", &keywords[1],
463 &line_info, &col_info);
Fred Drake503d8d61998-04-13 18:45:18 +0000464 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000465 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000466 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000467 * since it's known to work already.
468 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000469 res = node2tuple(self->st_node,
Antoine Pitrou721738f2012-08-15 23:20:39 +0200470 PyList_New, PyList_SetItem, line_info, col_info);
Guido van Rossum47478871996-08-21 14:32:37 +0000471 }
472 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000473}
Guido van Rossum47478871996-08-21 14:32:37 +0000474
475
Fred Drakec2683dd2001-07-17 19:32:05 +0000476/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000477 *
478 * This function creates code objects from the parse tree represented by
479 * the passed-in data object. An optional file name is passed in as well.
480 *
481 */
482static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000483parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000484{
Victor Stinner14e461d2013-08-26 22:28:21 +0200485 PyObject* res = NULL;
486 PyArena* arena = NULL;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000487 mod_ty mod;
Victor Stinner14e461d2013-08-26 22:28:21 +0200488 PyObject* filename = NULL;
Fred Drake503d8d61998-04-13 18:45:18 +0000489 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000490
Georg Brandl30704ea02008-07-23 15:07:12 +0000491 static char *keywords[] = {"st", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000492
Martin v. Löwis1a214512008-06-11 05:26:20 +0000493 if (self == NULL || PyModule_Check(self))
Victor Stinner14e461d2013-08-26 22:28:21 +0200494 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O&:compilest", keywords,
495 &PyST_Type, &self,
496 PyUnicode_FSDecoder, &filename);
Fred Drake503d8d61998-04-13 18:45:18 +0000497 else
Victor Stinner14e461d2013-08-26 22:28:21 +0200498 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O&:compile", &keywords[1],
499 PyUnicode_FSDecoder, &filename);
500 if (!ok)
501 goto error;
Fred Drake503d8d61998-04-13 18:45:18 +0000502
Victor Stinner14e461d2013-08-26 22:28:21 +0200503 if (filename == NULL) {
504 filename = PyUnicode_FromString("<syntax-tree>");
505 if (filename == NULL)
506 goto error;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000507 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000508
Victor Stinner14e461d2013-08-26 22:28:21 +0200509 arena = PyArena_New();
510 if (!arena)
511 goto error;
512
513 mod = PyAST_FromNodeObject(self->st_node, &self->st_flags,
514 filename, arena);
515 if (!mod)
516 goto error;
517
518 res = (PyObject *)PyAST_CompileObject(mod, filename,
519 &self->st_flags, -1, arena);
520error:
521 Py_XDECREF(filename);
522 if (arena != NULL)
523 PyArena_Free(arena);
524 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +0000525}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000526
527
528/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
529 * PyObject* parser_issuite(PyObject* self, PyObject* args)
530 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000531 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000532 * a statement suite, respectively. The return is a Python truth value.
533 *
534 */
535static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000536parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000537{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000538 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000539 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000540
Georg Brandl30704ea02008-07-23 15:07:12 +0000541 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000542
Martin v. Löwis1a214512008-06-11 05:26:20 +0000543 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000544 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000545 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000546 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000547 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000548
549 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000550 /* Check to see if the ST represents an expression or not. */
551 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000552 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000553 }
554 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000555}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000556
557
558static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000559parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000560{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000561 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000562 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000563
Georg Brandl30704ea02008-07-23 15:07:12 +0000564 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000565
Martin v. Löwis1a214512008-06-11 05:26:20 +0000566 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000567 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000568 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000569 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000570 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000571
572 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000573 /* Check to see if the ST represents an expression or not. */
574 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000575 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000576 }
577 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000578}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000579
580
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200581/* err_string(const char* message)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000582 *
583 * Sets the error string for an exception of type ParserError.
584 *
585 */
586static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200587err_string(const char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000588{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000589 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000590}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000591
592
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000593/* PyObject* parser_do_parse(PyObject* args, int type)
594 *
595 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000596 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000597 *
598 */
599static PyObject*
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200600parser_do_parse(PyObject *args, PyObject *kw, const char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000601{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000602 char* string = 0;
603 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000604 int flags = 0;
605 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000606
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000607 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000608
609 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000610 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
611 &_PyParser_Grammar,
612 (type == PyST_EXPR)
613 ? eval_input : file_input,
614 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 if (n) {
617 res = parser_newstobject(n, type);
Benjamin Petersonf216c942008-10-31 02:28:05 +0000618 if (res)
619 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
620 }
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500621 else {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000622 PyParser_SetError(&err);
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500623 }
Benjamin Petersonf0cdbad2011-06-05 22:14:05 -0500624 PyParser_ClearError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000625 }
626 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000627}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000628
629
630/* PyObject* parser_expr(PyObject* self, PyObject* args)
631 * PyObject* parser_suite(PyObject* self, PyObject* args)
632 *
633 * External interfaces to the parser itself. Which is called determines if
634 * the parser attempts to recognize an expression ('eval' form) or statement
635 * suite ('exec' form). The real work is done by parser_do_parse() above.
636 *
637 */
638static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000639parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000640{
Fred Drake268397f1998-04-29 14:16:32 +0000641 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000642 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000643}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000644
645
646static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000647parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000648{
Fred Drake268397f1998-04-29 14:16:32 +0000649 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000650 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000651}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000652
653
654
Fred Drakec2683dd2001-07-17 19:32:05 +0000655/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000656 * object requires that the input tuple be valid without having to rely on
657 * catching an exception from the compiler. This is done to allow the
658 * compiler itself to remain fast, since most of its input will come from
659 * the parser directly, and therefore be known to be syntactically correct.
660 * This validation is done to ensure that we don't core dump the compile
661 * phase, returning an exception instead.
662 *
663 * Two aspects can be broken out in this code: creating a node tree from
664 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000665 * advantageous to expand the number of ST types to include funcdefs and
666 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000667 * here. They are not necessary, and not quite as useful in a raw form.
668 * For now, let's get expressions and suites working reliably.
669 */
670
671
Jeremy Hylton938ace62002-07-17 16:30:39 +0000672static node* build_node_tree(PyObject *tuple);
Benjamin Peterson53595c42016-06-02 11:30:18 -0700673
674static int
675validate_node(node *tree)
676{
677 int type = TYPE(tree);
678 int nch = NCH(tree);
679 dfa *nt_dfa;
680 state *dfa_state;
681 int pos, arc;
682
683 assert(ISNONTERMINAL(type));
684 type -= NT_OFFSET;
685 if (type >= _PyParser_Grammar.g_ndfas) {
686 PyErr_Format(parser_error, "Unrecognized node type %d.", TYPE(tree));
687 return 0;
688 }
689 nt_dfa = &_PyParser_Grammar.g_dfa[type];
690 REQ(tree, nt_dfa->d_type);
691
692 /* Run the DFA for this nonterminal. */
693 dfa_state = &nt_dfa->d_state[nt_dfa->d_initial];
694 for (pos = 0; pos < nch; ++pos) {
695 node *ch = CHILD(tree, pos);
696 int ch_type = TYPE(ch);
697 for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
698 short a_label = dfa_state->s_arc[arc].a_lbl;
699 assert(a_label < _PyParser_Grammar.g_ll.ll_nlabels);
700 if (_PyParser_Grammar.g_ll.ll_label[a_label].lb_type == ch_type) {
701 /* The child is acceptable; if non-terminal, validate it recursively. */
702 if (ISNONTERMINAL(ch_type) && !validate_node(ch))
703 return 0;
704
705 /* Update the state, and move on to the next child. */
706 dfa_state = &nt_dfa->d_state[dfa_state->s_arc[arc].a_arrow];
707 goto arc_found;
708 }
709 }
710 /* What would this state have accepted? */
711 {
712 short a_label = dfa_state->s_arc->a_lbl;
713 int next_type;
714 if (!a_label) /* Wouldn't accept any more children */
715 goto illegal_num_children;
716
717 next_type = _PyParser_Grammar.g_ll.ll_label[a_label].lb_type;
718 if (ISNONTERMINAL(next_type))
719 PyErr_Format(parser_error, "Expected node type %d, got %d.",
720 next_type, ch_type);
721 else
722 PyErr_Format(parser_error, "Illegal terminal: expected %s.",
723 _PyParser_TokenNames[next_type]);
724 return 0;
725 }
726
727arc_found:
728 continue;
729 }
730 /* Are we in a final state? If so, return 1 for successful validation. */
731 for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
732 if (!dfa_state->s_arc[arc].a_lbl) {
733 return 1;
734 }
735 }
736
737illegal_num_children:
738 PyErr_Format(parser_error,
739 "Illegal number of children for %s node.", nt_dfa->d_name);
740 return 0;
741}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000742
Fred Drakec2683dd2001-07-17 19:32:05 +0000743/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000744 *
745 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000746 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000747 * tuple can be validated. It does this by checking the first code of the
748 * tuple, and, if acceptable, builds the internal representation. If this
749 * step succeeds, the internal representation is validated as fully as
Benjamin Peterson53595c42016-06-02 11:30:18 -0700750 * possible with the recursive validate_node() routine defined above.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000751 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000752 * This function must be changed if support is to be added for PyST_FRAGMENT
753 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000754 *
755 */
756static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000757parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000758{
Fred Drake268397f1998-04-29 14:16:32 +0000759 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000760 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000761 PyObject *tuple;
762 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000763
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000764 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000765
Fred Drakec2683dd2001-07-17 19:32:05 +0000766 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000767 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000768 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000769 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000770 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000771 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000772 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000773 }
774 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000775 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000776 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000777 tree = build_node_tree(tuple);
778 if (tree != 0) {
Benjamin Peterson53595c42016-06-02 11:30:18 -0700779 node *validation_root = tree;
780 int tree_type = 0;
781 switch (TYPE(tree)) {
782 case eval_input:
Fred Drake0ac9b072000-09-12 21:58:06 +0000783 /* Might be an eval form. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700784 tree_type = PyST_EXPR;
785 break;
786 case encoding_decl:
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000787 /* This looks like an encoding_decl so far. */
Benjamin Peterson53595c42016-06-02 11:30:18 -0700788 if (NCH(tree) != 1)
789 err_string("Error Parsing encoding_decl");
790 validation_root = CHILD(tree, 0);
791 /* Fall through */
792 case file_input:
793 /* This looks like an exec form so far. */
794
795 tree_type = PyST_SUITE;
796 break;
797 default:
Fred Drake0ac9b072000-09-12 21:58:06 +0000798 /* This is a fragment, at best. */
799 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000800 err_string("parse tree does not use a valid start symbol");
Benjamin Peterson53595c42016-06-02 11:30:18 -0700801 return (0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000802 }
Benjamin Peterson53595c42016-06-02 11:30:18 -0700803
804 if (validate_node(validation_root))
805 st = parser_newstobject(tree, tree_type);
806 else
807 PyNode_Free(tree);
Guido van Rossum47478871996-08-21 14:32:37 +0000808 }
Andrew Svetlov737fb892012-12-18 21:14:22 +0200809 /* Make sure we raise an exception on all errors. We should never
Guido van Rossum47478871996-08-21 14:32:37 +0000810 * get this, but we'd do well to be sure something is done.
811 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000812 if (st == NULL && !PyErr_Occurred())
813 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000814
Fred Drakec2683dd2001-07-17 19:32:05 +0000815 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000816}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000817
818
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000819/* node* build_node_children()
820 *
821 * Iterate across the children of the current non-terminal node and build
822 * their structures. If successful, return the root of this portion of
823 * the tree, otherwise, 0. Any required exception will be specified already,
824 * and no memory will have been deallocated.
825 *
826 */
827static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000828build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000829{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000830 Py_ssize_t len = PyObject_Size(tuple);
831 Py_ssize_t i;
832 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000833
834 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000835 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000836 PyObject* elem = PySequence_GetItem(tuple, i);
837 int ok = elem != NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200838 int type = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000839 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000840
Fred Drakeff9ea482000-04-19 13:54:15 +0000841 if (ok)
842 ok = PySequence_Check(elem);
843 if (ok) {
844 PyObject *temp = PySequence_GetItem(elem, 0);
845 if (temp == NULL)
846 ok = 0;
847 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000848 ok = PyLong_Check(temp);
Serhiy Storchaka78980432013-01-15 01:12:17 +0200849 if (ok) {
850 type = _PyLong_AsInt(temp);
851 if (type == -1 && PyErr_Occurred()) {
852 Py_DECREF(temp);
853 Py_DECREF(elem);
854 return 0;
855 }
856 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000857 Py_DECREF(temp);
858 }
859 }
860 if (!ok) {
Victor Stinner5f8d4852014-01-02 11:49:27 +0100861 PyObject *err = Py_BuildValue("Os", elem,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000862 "Illegal node construct.");
863 PyErr_SetObject(parser_error, err);
864 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000865 Py_XDECREF(elem);
866 return (0);
867 }
868 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000869 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000870 PyObject *temp;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000871 const char *temp_str;
Guido van Rossum47478871996-08-21 14:32:37 +0000872
Fred Drake0ac9b072000-09-12 21:58:06 +0000873 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000874 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000875 return 0;
876 }
877 temp = PySequence_GetItem(elem, 1);
878 if (temp == NULL)
879 return 0;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000880 if (!PyUnicode_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000881 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000882 "second item in terminal node must be a string,"
883 " found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000884 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000885 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000886 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000887 return 0;
888 }
889 if (len == 3) {
890 PyObject *o = PySequence_GetItem(elem, 2);
891 if (o != NULL) {
Serhiy Storchaka78980432013-01-15 01:12:17 +0200892 if (PyLong_Check(o)) {
893 int num = _PyLong_AsInt(o);
894 if (num == -1 && PyErr_Occurred()) {
895 Py_DECREF(o);
896 Py_DECREF(temp);
897 Py_DECREF(elem);
898 return 0;
899 }
900 *line_num = num;
901 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000902 else {
903 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000904 "third item in terminal node must be an"
905 " integer, found %s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 Py_TYPE(temp)->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000907 Py_DECREF(o);
908 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000909 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000910 return 0;
911 }
912 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000913 }
914 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000915 temp_str = _PyUnicode_AsStringAndSize(temp, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000916 if (temp_str == NULL) {
917 Py_DECREF(temp);
918 Py_XDECREF(elem);
919 return 0;
920 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000921 strn = (char *)PyObject_MALLOC(len + 1);
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200922 if (strn == NULL) {
923 Py_DECREF(temp);
924 Py_XDECREF(elem);
925 PyErr_NoMemory();
926 return 0;
927 }
928 (void) memcpy(strn, temp_str, len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000929 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000930 }
931 else if (!ISNONTERMINAL(type)) {
932 /*
933 * It has to be one or the other; this is an error.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200934 * Raise an exception.
Fred Drakeff9ea482000-04-19 13:54:15 +0000935 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000936 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
937 PyErr_SetObject(parser_error, err);
938 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000939 Py_XDECREF(elem);
940 return (0);
941 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000942 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000943 if (err == E_NOMEM) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000944 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000945 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000946 return (node *) PyErr_NoMemory();
947 }
948 if (err == E_OVERFLOW) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000949 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000950 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000951 PyErr_SetString(PyExc_ValueError,
952 "unsupported number of child nodes");
953 return NULL;
954 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000955
Fred Drakeff9ea482000-04-19 13:54:15 +0000956 if (ISNONTERMINAL(type)) {
957 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000958
Fred Drakeff9ea482000-04-19 13:54:15 +0000959 if (new_child != build_node_children(elem, new_child, line_num)) {
960 Py_XDECREF(elem);
961 return (0);
962 }
963 }
964 else if (type == NEWLINE) { /* It's true: we increment the */
965 ++(*line_num); /* line number *after* the newline! */
966 }
967 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000968 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000969 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000970}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000971
972
973static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000974build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000975{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000976 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000977 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000978 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000979
Guido van Rossum47478871996-08-21 14:32:37 +0000980 if (temp != NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000981 num = PyLong_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000982 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000983 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000984 /*
985 * The tuple is simple, but it doesn't start with a start symbol.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200986 * Raise an exception now and be done with it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000987 */
Victor Stinner6684bdf2013-07-17 00:13:52 +0200988 tuple = Py_BuildValue("Os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000989 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000990 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000991 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000992 }
993 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000994 /*
995 * Not efficient, but that can be handled later.
996 */
997 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000998 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000999
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001000 if (num == encoding_decl) {
1001 encoding = PySequence_GetItem(tuple, 2);
1002 /* tuple isn't borrowed anymore here, need to DECREF */
1003 tuple = PySequence_GetSlice(tuple, 0, 2);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001004 if (tuple == NULL)
1005 return NULL;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001006 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001007 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +00001008 if (res != NULL) {
1009 if (res != build_node_children(tuple, res, &line_num)) {
1010 PyNode_Free(res);
1011 res = NULL;
1012 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001013 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +00001014 Py_ssize_t len;
Neal Norwitz3fcbea52007-08-26 04:51:28 +00001015 const char *temp;
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001016 temp = _PyUnicode_AsStringAndSize(encoding, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001017 if (temp == NULL) {
1018 Py_DECREF(res);
1019 Py_DECREF(encoding);
1020 Py_DECREF(tuple);
1021 return NULL;
1022 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00001023 res->n_str = (char *)PyObject_MALLOC(len + 1);
Victor Stinner3bd6abd2013-07-12 01:33:59 +02001024 if (res->n_str == NULL) {
1025 Py_DECREF(res);
1026 Py_DECREF(encoding);
1027 Py_DECREF(tuple);
1028 PyErr_NoMemory();
1029 return NULL;
1030 }
1031 (void) memcpy(res->n_str, temp, len + 1);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001032 Py_DECREF(encoding);
1033 Py_DECREF(tuple);
1034 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001035 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001036 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001037 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001038 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +00001039 * NONTERMINAL, we can't use it. Not sure the implementation
1040 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +00001041 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001042 PyObject *err = Py_BuildValue("os", tuple,
1043 "Illegal component tuple.");
1044 PyErr_SetObject(parser_error, err);
1045 Py_XDECREF(err);
1046 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001047
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001048 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001049}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001050
1051
Fred Drake43f8f9b1998-04-13 16:25:46 +00001052static PyObject*
1053pickle_constructor = NULL;
1054
1055
1056static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00001057parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00001058{
Fred Drake268397f1998-04-29 14:16:32 +00001059 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00001060 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00001061 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00001062 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001063
Fred Drakec2683dd2001-07-17 19:32:05 +00001064 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001065 PyObject *newargs;
1066 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001067
Fred Drake2a6875e1999-09-20 22:32:18 +00001068 if ((empty_dict = PyDict_New()) == NULL)
1069 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00001070 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00001071 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00001072 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00001073 if (tuple != NULL) {
1074 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
1075 Py_DECREF(tuple);
1076 }
Fred Drake2a6875e1999-09-20 22:32:18 +00001077 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00001078 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001079 }
1080 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00001081 Py_XDECREF(empty_dict);
1082
Fred Drake43f8f9b1998-04-13 16:25:46 +00001083 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00001084}
Fred Drake43f8f9b1998-04-13 16:25:46 +00001085
1086
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001087/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00001088 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001089 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00001090 * We'd really have to write a wrapper around it all anyway to allow
1091 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001092 */
1093static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00001094 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001095 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001096 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001097 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001098 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001099 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001100 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001101 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001102 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001103 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001104 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001105 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001106 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001107 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001108 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001109 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00001110 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00001111 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001112
Fred Drake43f8f9b1998-04-13 16:25:46 +00001113 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00001114 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00001115 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00001116
Fred Drake268397f1998-04-29 14:16:32 +00001117 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001118 };
1119
1120
Martin v. Löwis1a214512008-06-11 05:26:20 +00001121
1122static struct PyModuleDef parsermodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 PyModuleDef_HEAD_INIT,
1124 "parser",
1125 NULL,
1126 -1,
1127 parser_functions,
1128 NULL,
1129 NULL,
1130 NULL,
1131 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001132};
1133
1134PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00001135
Mark Hammond62b1ab12002-07-23 06:31:15 +00001136PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001137PyInit_parser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00001138{
Fred Drake13130bc2001-08-15 16:44:56 +00001139 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00001140
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001141 if (PyType_Ready(&PyST_Type) < 0)
1142 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001143 module = PyModule_Create(&parsermodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001144 if (module == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 return NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001146
Fred Drake7a15ba51999-09-09 14:21:52 +00001147 if (parser_error == 0)
1148 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001149
Tim Peters6a627252003-07-21 14:25:23 +00001150 if (parser_error == 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001151 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00001152 /* CAUTION: The code next used to skip bumping the refcount on
Martin v. Löwis1a214512008-06-11 05:26:20 +00001153 * parser_error. That's a disaster if PyInit_parser() gets called more
Tim Peters6a627252003-07-21 14:25:23 +00001154 * than once. By incref'ing, we ensure that each module dict that
1155 * gets created owns its reference to the shared parser_error object,
1156 * and the file static parser_error vrbl owns a reference too.
1157 */
1158 Py_INCREF(parser_error);
1159 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001160 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00001161
Fred Drakec2683dd2001-07-17 19:32:05 +00001162 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00001163 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001164
Fred Drake13130bc2001-08-15 16:44:56 +00001165 PyModule_AddStringConstant(module, "__copyright__",
1166 parser_copyright_string);
1167 PyModule_AddStringConstant(module, "__doc__",
1168 parser_doc_string);
1169 PyModule_AddStringConstant(module, "__version__",
1170 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001171
Fred Drake78bdb9b2001-07-19 20:17:15 +00001172 /* Register to support pickling.
1173 * If this fails, the import of this module will fail because an
1174 * exception will be raised here; should we clear the exception?
1175 */
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00001176 copyreg = PyImport_ImportModuleNoBlock("copyreg");
Fred Drake13130bc2001-08-15 16:44:56 +00001177 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001178 PyObject *func, *pickler;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001179 _Py_IDENTIFIER(pickle);
1180 _Py_IDENTIFIER(sequence2st);
1181 _Py_IDENTIFIER(_pickler);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001182
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001183 func = _PyObject_GetAttrId(copyreg, &PyId_pickle);
1184 pickle_constructor = _PyObject_GetAttrId(module, &PyId_sequence2st);
1185 pickler = _PyObject_GetAttrId(module, &PyId__pickler);
Fred Drakeff9ea482000-04-19 13:54:15 +00001186 Py_XINCREF(pickle_constructor);
1187 if ((func != NULL) && (pickle_constructor != NULL)
1188 && (pickler != NULL)) {
1189 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00001190
Thomas Wouters477c8d52006-05-27 19:21:47 +00001191 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
1192 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00001193 Py_XDECREF(res);
1194 }
1195 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00001196 Py_XDECREF(pickle_constructor);
1197 Py_XDECREF(pickler);
1198 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00001199 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00001200 return module;
Fred Drakeff9ea482000-04-19 13:54:15 +00001201}