blob: 7a6e4552079670d32a5797119a746d8172b4e795 [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(...)".
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000026 */
27
Fred Drakeff9ea482000-04-19 13:54:15 +000028#include "Python.h" /* general Python API */
Benjamin Petersonf216c942008-10-31 02:28:05 +000029#include "Python-ast.h" /* mod_ty */
Fred Drakeff9ea482000-04-19 13:54:15 +000030#include "graminit.h" /* symbols defined in the grammar */
31#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000032#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000033#include "token.h" /* token definitions */
Benjamin Petersonf216c942008-10-31 02:28:05 +000034#include "grammar.h"
35#include "parsetok.h"
Fred Drakeff9ea482000-04-19 13:54:15 +000036 /* ISTERMINAL() / ISNONTERMINAL() */
Benjamin Petersonf216c942008-10-31 02:28:05 +000037#undef Yield
38#include "ast.h"
Benjamin Petersonf216c942008-10-31 02:28:05 +000039
40extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000041
Fred Drake268397f1998-04-29 14:16:32 +000042#ifdef lint
43#include <note.h>
44#else
45#define NOTE(x)
46#endif
47
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000048/* String constants used to initialize module attributes.
49 *
50 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000051static char parser_copyright_string[] =
52"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000053University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
54Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
55Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000056
57
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000058PyDoc_STRVAR(parser_doc_string,
59"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000060
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000061static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000062
63
Martin v. Löwis18e16552006-02-15 17:27:45 +000064typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000065typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000066 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000067 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000068
Thomas Wouters7e474022000-07-16 12:04:32 +000069/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000070 * original copyright statement is included below, and continues to apply
71 * in full to the function immediately following. All other material is
72 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
73 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000074 * new naming conventions. Added arguments to provide support for creating
75 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000076 */
77
Guido van Rossum52f2c051993-11-10 12:53:24 +000078
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000079static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000080node2tuple(node *n, /* node to convert */
81 SeqMaker mkseq, /* create sequence */
82 SeqInserter addelem, /* func. to add elem. in seq. */
Thomas Wouters89f507f2006-12-13 04:49:30 +000083 int lineno, /* include line numbers? */
84 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000085{
Victor Stinnerdf4572c2013-07-12 01:35:10 +020086 PyObject *result = NULL, *w;
87
Guido van Rossum3d602e31996-07-21 02:33:56 +000088 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000089 Py_INCREF(Py_None);
Victor Stinnerdf4572c2013-07-12 01:35:10 +020090 return Py_None;
Guido van Rossum3d602e31996-07-21 02:33:56 +000091 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +020092
Guido van Rossum3d602e31996-07-21 02:33:56 +000093 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000094 int i;
Fred Drake268397f1998-04-29 14:16:32 +000095
Victor Stinnerdf4572c2013-07-12 01:35:10 +020096 result = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
97 if (result == NULL)
98 goto error;
99
Christian Heimes217cfd12007-12-02 14:31:20 +0000100 w = PyLong_FromLong(TYPE(n));
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200101 if (w == NULL)
102 goto error;
103 (void) addelem(result, 0, w);
104
Fred Drakeff9ea482000-04-19 13:54:15 +0000105 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000106 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200107 if (w == NULL)
108 goto error;
109 (void) addelem(result, i+1, w);
Fred Drakeff9ea482000-04-19 13:54:15 +0000110 }
Tim Peters6a627252003-07-21 14:25:23 +0000111
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200112 if (TYPE(n) == encoding_decl) {
113 w = PyUnicode_FromString(STR(n));
114 if (w == NULL)
115 goto error;
116 (void) addelem(result, i+1, w);
117 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000118 }
119 else if (ISTERMINAL(TYPE(n))) {
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200120 result = mkseq(2 + lineno + col_offset);
121 if (result == NULL)
122 goto error;
123
124 w = PyLong_FromLong(TYPE(n));
125 if (w == NULL)
126 goto error;
127 (void) addelem(result, 0, w);
128
129 w = PyUnicode_FromString(STR(n));
130 if (w == NULL)
131 goto error;
132 (void) addelem(result, 1, w);
133
134 if (lineno == 1) {
135 w = PyLong_FromLong(n->n_lineno);
136 if (w == NULL)
137 goto error;
138 (void) addelem(result, 2, w);
Fred Drakeff9ea482000-04-19 13:54:15 +0000139 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200140
141 if (col_offset == 1) {
142 w = PyLong_FromLong(n->n_col_offset);
143 if (w == NULL)
144 goto error;
145 (void) addelem(result, 3, w);
146 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000147 }
148 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000149 PyErr_SetString(PyExc_SystemError,
150 "unrecognized parse tree node type");
151 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000152 }
Victor Stinnerdf4572c2013-07-12 01:35:10 +0200153 return result;
154
155error:
156 Py_XDECREF(result);
157 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000158}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000159/*
160 * End of material copyrighted by Stichting Mathematisch Centrum.
161 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000162
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000163
164
165/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000166 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000167 * field of the object type to identify which any given object represents.
168 * These should probably go in an external header to allow other extensions
169 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000170 */
171
Fred Drakec2683dd2001-07-17 19:32:05 +0000172#define PyST_EXPR 1
173#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000174
175
176/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000177 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000178 * naming style, but the code uses the new naming convention.
179 */
180
181static PyObject*
182parser_error = 0;
183
184
Fred Drakec2683dd2001-07-17 19:32:05 +0000185typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000186 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000187 node* st_node; /* the node* returned by the parser */
188 int st_type; /* EXPR or SUITE ? */
Benjamin Petersonf216c942008-10-31 02:28:05 +0000189 PyCompilerFlags st_flags; /* Parser and compiler flags */
Fred Drakec2683dd2001-07-17 19:32:05 +0000190} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000191
192
Jeremy Hylton938ace62002-07-17 16:30:39 +0000193static void parser_free(PyST_Object *st);
Jesus Ceae9c53182012-08-03 14:28:37 +0200194static PyObject* parser_sizeof(PyST_Object *, void *);
Mark Dickinson211c6252009-02-01 10:28:51 +0000195static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op);
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000196static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
197static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
198static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
199static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
200static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
Fred Drake503d8d61998-04-13 18:45:18 +0000201
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000202#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
203
204static PyMethodDef parser_methods[] = {
205 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
206 PyDoc_STR("Compile this ST object into a code object.")},
207 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
208 PyDoc_STR("Determines if this ST object was created from an expression.")},
209 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
210 PyDoc_STR("Determines if this ST object was created from a suite.")},
211 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
212 PyDoc_STR("Creates a list-tree representation of this ST.")},
213 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
214 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Jesus Ceae9c53182012-08-03 14:28:37 +0200215 {"__sizeof__", (PyCFunction)parser_sizeof, METH_NOARGS,
216 PyDoc_STR("Returns size in memory, in bytes.")},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000217 {NULL, NULL, 0, NULL}
218};
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000219
Fred Drake268397f1998-04-29 14:16:32 +0000220static
Fred Drakec2683dd2001-07-17 19:32:05 +0000221PyTypeObject PyST_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000222 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000223 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000224 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000225 0, /* tp_itemsize */
226 (destructor)parser_free, /* tp_dealloc */
227 0, /* tp_print */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000228 0, /* tp_getattr */
Fred Drakeff9ea482000-04-19 13:54:15 +0000229 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000230 0, /* tp_reserved */
Fred Drakeff9ea482000-04-19 13:54:15 +0000231 0, /* tp_repr */
232 0, /* tp_as_number */
233 0, /* tp_as_sequence */
234 0, /* tp_as_mapping */
235 0, /* tp_hash */
236 0, /* tp_call */
237 0, /* tp_str */
238 0, /* tp_getattro */
239 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000240
241 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000242 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000243
Fred Drakeff9ea482000-04-19 13:54:15 +0000244 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000245
246 /* __doc__ */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000247 "Intermediate representation of a Python parse tree.",
248 0, /* tp_traverse */
249 0, /* tp_clear */
Mark Dickinson211c6252009-02-01 10:28:51 +0000250 parser_richcompare, /* tp_richcompare */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000251 0, /* tp_weaklistoffset */
252 0, /* tp_iter */
253 0, /* tp_iternext */
254 parser_methods, /* tp_methods */
Fred Drakec2683dd2001-07-17 19:32:05 +0000255}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000256
257
Mark Dickinson211c6252009-02-01 10:28:51 +0000258/* PyST_Type isn't subclassable, so just check ob_type */
259#define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type)
260
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000261static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000262parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000263{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000264 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000265
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000266 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000267 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000268
269 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000270 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000271
272 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000273 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000274
275 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000276 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000277
278 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000279 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000280
281 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000282 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000283
Fred Drakeff9ea482000-04-19 13:54:15 +0000284 if (v != 0)
285 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000286 }
287 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000288}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000289
Mark Dickinson211c6252009-02-01 10:28:51 +0000290/* parser_richcompare(PyObject* left, PyObject* right, int op)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000291 *
292 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
293 * This really just wraps a call to parser_compare_nodes() with some easy
294 * checks and protection code.
295 *
296 */
Mark Dickinson211c6252009-02-01 10:28:51 +0000297
298#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
299
300static PyObject *
301parser_richcompare(PyObject *left, PyObject *right, int op)
Guido van Rossum47478871996-08-21 14:32:37 +0000302{
Mark Dickinson211c6252009-02-01 10:28:51 +0000303 int result;
304 PyObject *v;
305
306 /* neither argument should be NULL, unless something's gone wrong */
307 if (left == NULL || right == NULL) {
308 PyErr_BadInternalCall();
309 return NULL;
310 }
311
312 /* both arguments should be instances of PyST_Object */
313 if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) {
314 v = Py_NotImplemented;
315 goto finished;
316 }
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
Mark Dickinson211c6252009-02-01 10:28:51 +0000325 /* Convert return value to a Boolean */
326 switch (op) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000327 case Py_EQ:
Mark Dickinson211c6252009-02-01 10:28:51 +0000328 v = TEST_COND(result == 0);
329 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000330 case Py_NE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000331 v = TEST_COND(result != 0);
332 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000333 case Py_LE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000334 v = TEST_COND(result <= 0);
335 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000336 case Py_GE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000337 v = TEST_COND(result >= 0);
338 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000339 case Py_LT:
Mark Dickinson211c6252009-02-01 10:28:51 +0000340 v = TEST_COND(result < 0);
341 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000342 case Py_GT:
Mark Dickinson211c6252009-02-01 10:28:51 +0000343 v = TEST_COND(result > 0);
344 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000345 default:
Mark Dickinson211c6252009-02-01 10:28:51 +0000346 PyErr_BadArgument();
347 return NULL;
348 }
349 finished:
350 Py_INCREF(v);
351 return v;
Fred Drakeff9ea482000-04-19 13:54:15 +0000352}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000353
Fred Drakec2683dd2001-07-17 19:32:05 +0000354/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000355 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000356 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000357 * 'wrapper' object that holds a node* and allows it to be passed around in
358 * Python code.
359 *
360 */
361static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000362parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000363{
Fred Drakec2683dd2001-07-17 19:32:05 +0000364 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000365
366 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000367 o->st_node = st;
368 o->st_type = type;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000369 o->st_flags.cf_flags = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000370 }
Fred Drake268397f1998-04-29 14:16:32 +0000371 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000372 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000373 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000374 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000375}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000376
377
Fred Drakec2683dd2001-07-17 19:32:05 +0000378/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000379 *
380 * This is called by a del statement that reduces the reference count to 0.
381 *
382 */
383static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000384parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000385{
Fred Drakec2683dd2001-07-17 19:32:05 +0000386 PyNode_Free(st->st_node);
387 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000388}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000389
Jesus Ceae9c53182012-08-03 14:28:37 +0200390static PyObject *
391parser_sizeof(PyST_Object *st, void *unused)
392{
393 Py_ssize_t res;
394
395 res = sizeof(PyST_Object) + _PyNode_SizeOf(st->st_node);
396 return PyLong_FromSsize_t(res);
397}
398
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000399
Fred Drakec2683dd2001-07-17 19:32:05 +0000400/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000401 *
402 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000403 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000404 *
405 */
406static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000407parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000408{
Antoine Pitrou721738f2012-08-15 23:20:39 +0200409 int line_info = 0;
410 int col_info = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000411 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000412 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000413
Georg Brandl30704ea02008-07-23 15:07:12 +0000414 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000415
Martin v. Löwis1a214512008-06-11 05:26:20 +0000416 if (self == NULL || PyModule_Check(self)) {
Antoine Pitrou721738f2012-08-15 23:20:39 +0200417 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2tuple", keywords,
418 &PyST_Type, &self, &line_info,
419 &col_info);
Fred Drake268397f1998-04-29 14:16:32 +0000420 }
Fred Drake503d8d61998-04-13 18:45:18 +0000421 else
Antoine Pitrou721738f2012-08-15 23:20:39 +0200422 ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:totuple", &keywords[1],
423 &line_info, &col_info);
Fred Drake268397f1998-04-29 14:16:32 +0000424 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000425 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000426 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000427 * since it's known to work already.
428 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000429 res = node2tuple(((PyST_Object*)self)->st_node,
Antoine Pitrou721738f2012-08-15 23:20:39 +0200430 PyTuple_New, PyTuple_SetItem, line_info, col_info);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000431 }
432 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000433}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000434
435
Fred Drakec2683dd2001-07-17 19:32:05 +0000436/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000437 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000438 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000439 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000440 *
441 */
442static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000443parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000444{
Antoine Pitrou721738f2012-08-15 23:20:39 +0200445 int line_info = 0;
446 int col_info = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000447 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000448 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000449
Georg Brandl30704ea02008-07-23 15:07:12 +0000450 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000451
Martin v. Löwis1a214512008-06-11 05:26:20 +0000452 if (self == NULL || PyModule_Check(self))
Antoine Pitrou721738f2012-08-15 23:20:39 +0200453 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2list", keywords,
454 &PyST_Type, &self, &line_info,
455 &col_info);
Fred Drake503d8d61998-04-13 18:45:18 +0000456 else
Antoine Pitrou721738f2012-08-15 23:20:39 +0200457 ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:tolist", &keywords[1],
458 &line_info, &col_info);
Fred Drake503d8d61998-04-13 18:45:18 +0000459 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000460 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000461 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000462 * since it's known to work already.
463 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000464 res = node2tuple(self->st_node,
Antoine Pitrou721738f2012-08-15 23:20:39 +0200465 PyList_New, PyList_SetItem, line_info, col_info);
Guido van Rossum47478871996-08-21 14:32:37 +0000466 }
467 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000468}
Guido van Rossum47478871996-08-21 14:32:37 +0000469
470
Fred Drakec2683dd2001-07-17 19:32:05 +0000471/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000472 *
473 * This function creates code objects from the parse tree represented by
474 * the passed-in data object. An optional file name is passed in as well.
475 *
476 */
477static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000478parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000479{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000480 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000481 PyArena* arena;
482 mod_ty mod;
Fred Drakec2683dd2001-07-17 19:32:05 +0000483 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000484 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000485
Georg Brandl30704ea02008-07-23 15:07:12 +0000486 static char *keywords[] = {"st", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000487
Martin v. Löwis1a214512008-06-11 05:26:20 +0000488 if (self == NULL || PyModule_Check(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000489 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
490 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000491 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000492 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000493 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000494
Benjamin Petersonf216c942008-10-31 02:28:05 +0000495 if (ok) {
496 arena = PyArena_New();
497 if (arena) {
498 mod = PyAST_FromNode(self->st_node, &(self->st_flags), str, arena);
499 if (mod) {
500 res = (PyObject *)PyAST_Compile(mod, str, &(self->st_flags), arena);
501 }
502 PyArena_Free(arena);
503 }
504 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000505
506 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000507}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000508
509
510/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
511 * PyObject* parser_issuite(PyObject* self, PyObject* args)
512 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000513 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000514 * a statement suite, respectively. The return is a Python truth value.
515 *
516 */
517static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000518parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000519{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000520 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000521 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000522
Georg Brandl30704ea02008-07-23 15:07:12 +0000523 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000524
Martin v. Löwis1a214512008-06-11 05:26:20 +0000525 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000526 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000527 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000528 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000529 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000530
531 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000532 /* Check to see if the ST represents an expression or not. */
533 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000534 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000535 }
536 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000537}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000538
539
540static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000541parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000542{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000543 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000544 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000545
Georg Brandl30704ea02008-07-23 15:07:12 +0000546 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000547
Martin v. Löwis1a214512008-06-11 05:26:20 +0000548 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000549 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000550 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000551 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000552 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000553
554 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000555 /* Check to see if the ST represents an expression or not. */
556 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000557 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000558 }
559 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000560}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000561
562
Guido van Rossum3d602e31996-07-21 02:33:56 +0000563/* err_string(char* message)
564 *
565 * Sets the error string for an exception of type ParserError.
566 *
567 */
568static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000569err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000570{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000571 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000572}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000573
574
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000575/* PyObject* parser_do_parse(PyObject* args, int type)
576 *
577 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000578 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000579 *
580 */
581static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000582parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000583{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000584 char* string = 0;
585 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000586 int flags = 0;
587 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000588
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000589 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000590
591 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000592 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
593 &_PyParser_Grammar,
594 (type == PyST_EXPR)
595 ? eval_input : file_input,
596 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 if (n) {
599 res = parser_newstobject(n, type);
Benjamin Petersonf216c942008-10-31 02:28:05 +0000600 if (res)
601 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
602 }
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500603 else {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000604 PyParser_SetError(&err);
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500605 }
Benjamin Petersonf0cdbad2011-06-05 22:14:05 -0500606 PyParser_ClearError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000607 }
608 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000609}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000610
611
612/* PyObject* parser_expr(PyObject* self, PyObject* args)
613 * PyObject* parser_suite(PyObject* self, PyObject* args)
614 *
615 * External interfaces to the parser itself. Which is called determines if
616 * the parser attempts to recognize an expression ('eval' form) or statement
617 * suite ('exec' form). The real work is done by parser_do_parse() above.
618 *
619 */
620static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000621parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000622{
Fred Drake268397f1998-04-29 14:16:32 +0000623 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000624 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000625}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000626
627
628static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000629parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000630{
Fred Drake268397f1998-04-29 14:16:32 +0000631 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000632 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000633}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000634
635
636
Fred Drakec2683dd2001-07-17 19:32:05 +0000637/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000638 * object requires that the input tuple be valid without having to rely on
639 * catching an exception from the compiler. This is done to allow the
640 * compiler itself to remain fast, since most of its input will come from
641 * the parser directly, and therefore be known to be syntactically correct.
642 * This validation is done to ensure that we don't core dump the compile
643 * phase, returning an exception instead.
644 *
645 * Two aspects can be broken out in this code: creating a node tree from
646 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000647 * advantageous to expand the number of ST types to include funcdefs and
648 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000649 * here. They are not necessary, and not quite as useful in a raw form.
650 * For now, let's get expressions and suites working reliably.
651 */
652
653
Jeremy Hylton938ace62002-07-17 16:30:39 +0000654static node* build_node_tree(PyObject *tuple);
655static int validate_expr_tree(node *tree);
656static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000657static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000658
Fred Drakec2683dd2001-07-17 19:32:05 +0000659/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000660 *
661 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000662 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000663 * tuple can be validated. It does this by checking the first code of the
664 * tuple, and, if acceptable, builds the internal representation. If this
665 * step succeeds, the internal representation is validated as fully as
666 * possible with the various validate_*() routines defined below.
667 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000668 * This function must be changed if support is to be added for PyST_FRAGMENT
669 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000670 *
671 */
672static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000673parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000674{
Fred Drake268397f1998-04-29 14:16:32 +0000675 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000676 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000677 PyObject *tuple;
678 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000679
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000680 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000681
Fred Drakec2683dd2001-07-17 19:32:05 +0000682 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000683 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000684 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000685 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000686 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000687 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000688 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000689 }
690 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000691 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000692 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000693 tree = build_node_tree(tuple);
694 if (tree != 0) {
695 int start_sym = TYPE(tree);
696 if (start_sym == eval_input) {
697 /* Might be an eval form. */
698 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000699 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000700 else
701 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000702 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000703 else if (start_sym == file_input) {
704 /* This looks like an exec form so far. */
705 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000706 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000707 else
708 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000709 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000710 else if (start_sym == encoding_decl) {
711 /* This looks like an encoding_decl so far. */
712 if (validate_encoding_decl(tree))
713 st = parser_newstobject(tree, PyST_SUITE);
714 else
715 PyNode_Free(tree);
716 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000717 else {
718 /* This is a fragment, at best. */
719 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000720 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000721 }
Guido van Rossum47478871996-08-21 14:32:37 +0000722 }
Andrew Svetlov737fb892012-12-18 21:14:22 +0200723 /* Make sure we raise an exception on all errors. We should never
Guido van Rossum47478871996-08-21 14:32:37 +0000724 * get this, but we'd do well to be sure something is done.
725 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000726 if (st == NULL && !PyErr_Occurred())
727 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000728
Fred Drakec2683dd2001-07-17 19:32:05 +0000729 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000730}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000731
732
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000733/* node* build_node_children()
734 *
735 * Iterate across the children of the current non-terminal node and build
736 * their structures. If successful, return the root of this portion of
737 * the tree, otherwise, 0. Any required exception will be specified already,
738 * and no memory will have been deallocated.
739 *
740 */
741static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000742build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000743{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000744 Py_ssize_t len = PyObject_Size(tuple);
745 Py_ssize_t i;
746 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000747
748 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000749 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000750 PyObject* elem = PySequence_GetItem(tuple, i);
751 int ok = elem != NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200752 int type = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000753 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000754
Fred Drakeff9ea482000-04-19 13:54:15 +0000755 if (ok)
756 ok = PySequence_Check(elem);
757 if (ok) {
758 PyObject *temp = PySequence_GetItem(elem, 0);
759 if (temp == NULL)
760 ok = 0;
761 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000762 ok = PyLong_Check(temp);
Serhiy Storchaka78980432013-01-15 01:12:17 +0200763 if (ok) {
764 type = _PyLong_AsInt(temp);
765 if (type == -1 && PyErr_Occurred()) {
766 Py_DECREF(temp);
767 Py_DECREF(elem);
768 return 0;
769 }
770 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000771 Py_DECREF(temp);
772 }
773 }
774 if (!ok) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000775 PyObject *err = Py_BuildValue("os", elem,
776 "Illegal node construct.");
777 PyErr_SetObject(parser_error, err);
778 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000779 Py_XDECREF(elem);
780 return (0);
781 }
782 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000783 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000784 PyObject *temp;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000785 const char *temp_str;
Guido van Rossum47478871996-08-21 14:32:37 +0000786
Fred Drake0ac9b072000-09-12 21:58:06 +0000787 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000788 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000789 return 0;
790 }
791 temp = PySequence_GetItem(elem, 1);
792 if (temp == NULL)
793 return 0;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000794 if (!PyUnicode_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000795 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000796 "second item in terminal node must be a string,"
797 " found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000798 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000799 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000800 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000801 return 0;
802 }
803 if (len == 3) {
804 PyObject *o = PySequence_GetItem(elem, 2);
805 if (o != NULL) {
Serhiy Storchaka78980432013-01-15 01:12:17 +0200806 if (PyLong_Check(o)) {
807 int num = _PyLong_AsInt(o);
808 if (num == -1 && PyErr_Occurred()) {
809 Py_DECREF(o);
810 Py_DECREF(temp);
811 Py_DECREF(elem);
812 return 0;
813 }
814 *line_num = num;
815 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000816 else {
817 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000818 "third item in terminal node must be an"
819 " integer, found %s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 Py_TYPE(temp)->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000821 Py_DECREF(o);
822 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000823 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000824 return 0;
825 }
826 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000827 }
828 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000829 temp_str = _PyUnicode_AsStringAndSize(temp, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000830 if (temp_str == NULL) {
831 Py_DECREF(temp);
832 Py_XDECREF(elem);
833 return 0;
834 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000835 strn = (char *)PyObject_MALLOC(len + 1);
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200836 if (strn == NULL) {
837 Py_DECREF(temp);
838 Py_XDECREF(elem);
839 PyErr_NoMemory();
840 return 0;
841 }
842 (void) memcpy(strn, temp_str, len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000843 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000844 }
845 else if (!ISNONTERMINAL(type)) {
846 /*
847 * It has to be one or the other; this is an error.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200848 * Raise an exception.
Fred Drakeff9ea482000-04-19 13:54:15 +0000849 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000850 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
851 PyErr_SetObject(parser_error, err);
852 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000853 Py_XDECREF(elem);
854 return (0);
855 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000856 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000857 if (err == E_NOMEM) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000858 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000859 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000860 return (node *) PyErr_NoMemory();
861 }
862 if (err == E_OVERFLOW) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000863 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000864 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000865 PyErr_SetString(PyExc_ValueError,
866 "unsupported number of child nodes");
867 return NULL;
868 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000869
Fred Drakeff9ea482000-04-19 13:54:15 +0000870 if (ISNONTERMINAL(type)) {
871 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000872
Fred Drakeff9ea482000-04-19 13:54:15 +0000873 if (new_child != build_node_children(elem, new_child, line_num)) {
874 Py_XDECREF(elem);
875 return (0);
876 }
877 }
878 else if (type == NEWLINE) { /* It's true: we increment the */
879 ++(*line_num); /* line number *after* the newline! */
880 }
881 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000882 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000883 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000884}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000885
886
887static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000888build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000889{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000890 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000891 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000892 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000893
Guido van Rossum47478871996-08-21 14:32:37 +0000894 if (temp != NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000895 num = PyLong_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000896 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000897 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000898 /*
899 * The tuple is simple, but it doesn't start with a start symbol.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200900 * Raise an exception now and be done with it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000901 */
Victor Stinner6684bdf2013-07-17 00:13:52 +0200902 tuple = Py_BuildValue("Os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000903 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000904 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000905 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000906 }
907 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000908 /*
909 * Not efficient, but that can be handled later.
910 */
911 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000912 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000913
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000914 if (num == encoding_decl) {
915 encoding = PySequence_GetItem(tuple, 2);
916 /* tuple isn't borrowed anymore here, need to DECREF */
917 tuple = PySequence_GetSlice(tuple, 0, 2);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000918 if (tuple == NULL)
919 return NULL;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000920 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000921 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000922 if (res != NULL) {
923 if (res != build_node_children(tuple, res, &line_num)) {
924 PyNode_Free(res);
925 res = NULL;
926 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000927 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000928 Py_ssize_t len;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000929 const char *temp;
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000930 temp = _PyUnicode_AsStringAndSize(encoding, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000931 if (temp == NULL) {
932 Py_DECREF(res);
933 Py_DECREF(encoding);
934 Py_DECREF(tuple);
935 return NULL;
936 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000937 res->n_str = (char *)PyObject_MALLOC(len + 1);
Victor Stinner3bd6abd2013-07-12 01:33:59 +0200938 if (res->n_str == NULL) {
939 Py_DECREF(res);
940 Py_DECREF(encoding);
941 Py_DECREF(tuple);
942 PyErr_NoMemory();
943 return NULL;
944 }
945 (void) memcpy(res->n_str, temp, len + 1);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000946 Py_DECREF(encoding);
947 Py_DECREF(tuple);
948 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000949 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000950 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000951 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000952 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000953 * NONTERMINAL, we can't use it. Not sure the implementation
954 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000955 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000956 PyObject *err = Py_BuildValue("os", tuple,
957 "Illegal component tuple.");
958 PyErr_SetObject(parser_error, err);
959 Py_XDECREF(err);
960 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000961
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000962 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000963}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000964
965
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000966/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000967 * Validation routines used within the validation section:
968 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000969static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000970
Fred Drakeff9ea482000-04-19 13:54:15 +0000971#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
972#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
973#define validate_colon(ch) validate_terminal(ch, COLON, ":")
974#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
975#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
976#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
977#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
978#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
979#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
980#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
981#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
982#define validate_star(ch) validate_terminal(ch, STAR, "*")
983#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
984#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
985#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000986#define validate_at(ch) validate_terminal(ch, AT, "@")
Mark Dickinsonea7e9f92012-04-29 18:34:40 +0100987#define validate_rarrow(ch) validate_terminal(ch, RARROW, "->")
Fred Drakeff9ea482000-04-19 13:54:15 +0000988#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000989
Fred Drake0ac9b072000-09-12 21:58:06 +0000990#define VALIDATER(n) static int validate_##n(node *tree)
991
Fred Drakeff9ea482000-04-19 13:54:15 +0000992VALIDATER(node); VALIDATER(small_stmt);
993VALIDATER(class); VALIDATER(node);
994VALIDATER(parameters); VALIDATER(suite);
995VALIDATER(testlist); VALIDATER(varargslist);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000996VALIDATER(vfpdef);
Fred Drakeff9ea482000-04-19 13:54:15 +0000997VALIDATER(stmt); VALIDATER(simple_stmt);
998VALIDATER(expr_stmt); VALIDATER(power);
Guido van Rossum452bf512007-02-09 05:32:43 +0000999VALIDATER(del_stmt);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001000VALIDATER(return_stmt); VALIDATER(raise_stmt);
1001VALIDATER(import_stmt); VALIDATER(import_stmt);
1002VALIDATER(import_name); VALIDATER(yield_stmt);
Mark Dickinson407b3bd2012-04-29 22:18:31 +01001003VALIDATER(global_stmt); VALIDATER(nonlocal_stmt);
1004VALIDATER(assert_stmt);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001005VALIDATER(compound_stmt); VALIDATER(test_or_star_expr);
Fred Drakeff9ea482000-04-19 13:54:15 +00001006VALIDATER(while); VALIDATER(for);
1007VALIDATER(try); VALIDATER(except_clause);
1008VALIDATER(test); VALIDATER(and_test);
1009VALIDATER(not_test); VALIDATER(comparison);
Guido van Rossumfc158e22007-11-15 19:17:28 +00001010VALIDATER(comp_op);
Guido van Rossum0368b722007-05-11 16:50:42 +00001011VALIDATER(star_expr); VALIDATER(expr);
Fred Drakeff9ea482000-04-19 13:54:15 +00001012VALIDATER(xor_expr); VALIDATER(and_expr);
1013VALIDATER(shift_expr); VALIDATER(arith_expr);
1014VALIDATER(term); VALIDATER(factor);
1015VALIDATER(atom); VALIDATER(lambdef);
1016VALIDATER(trailer); VALIDATER(subscript);
1017VALIDATER(subscriptlist); VALIDATER(sliceop);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001018VALIDATER(exprlist); VALIDATER(dictorsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +00001019VALIDATER(arglist); VALIDATER(argument);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001020VALIDATER(comp_for);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001021VALIDATER(comp_iter); VALIDATER(comp_if);
1022VALIDATER(testlist_comp); VALIDATER(yield_expr);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001023VALIDATER(or_test);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001024VALIDATER(test_nocond); VALIDATER(lambdef_nocond);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001025VALIDATER(yield_arg);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001026
Fred Drake0ac9b072000-09-12 21:58:06 +00001027#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001028
Fred Drakeff9ea482000-04-19 13:54:15 +00001029#define is_even(n) (((n) & 1) == 0)
1030#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001031
1032
1033static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001034validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +00001035{
Fred Drake0ac9b072000-09-12 21:58:06 +00001036 if (TYPE(n) != t) {
1037 PyErr_Format(parser_error, "Expected node type %d, got %d.",
1038 t, TYPE(n));
1039 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001040 }
Fred Drake0ac9b072000-09-12 21:58:06 +00001041 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00001042}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001043
1044
Fred Drakee7ab64e2000-04-25 04:14:46 +00001045/* Verifies that the number of child nodes is exactly 'num', raising
1046 * an exception if it isn't. The exception message does not indicate
1047 * the exact number of nodes, allowing this to be used to raise the
1048 * "right" exception when the wrong number of nodes is present in a
1049 * specific variant of a statement's syntax. This is commonly used
1050 * in that fashion.
1051 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001052static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001053validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001054{
Guido van Rossum3d602e31996-07-21 02:33:56 +00001055 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001056 PyErr_Format(parser_error,
1057 "Illegal number of children for %s node.", name);
1058 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001059 }
Fred Drake0ac9b072000-09-12 21:58:06 +00001060 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00001061}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001062
1063
1064static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001065validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +00001066{
Guido van Rossum3d602e31996-07-21 02:33:56 +00001067 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +00001068 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001069
1070 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001071 PyErr_Format(parser_error,
1072 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001073 }
1074 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001075}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001076
1077
Guido van Rossum47478871996-08-21 14:32:37 +00001078/* X (',' X) [',']
1079 */
1080static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +00001081validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +00001082 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001083{
1084 int nch = NCH(tree);
1085 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +00001086 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +00001087
1088 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001089 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +00001090 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001091 if (is_even(nch))
1092 res = validate_comma(CHILD(tree, --nch));
1093 if (res && nch > 1) {
1094 int pos = 1;
1095 for ( ; res && pos < nch; pos += 2)
1096 res = (validate_comma(CHILD(tree, pos))
1097 && vfunc(CHILD(tree, pos + 1)));
1098 }
Guido van Rossum47478871996-08-21 14:32:37 +00001099 }
1100 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001101}
Guido van Rossum47478871996-08-21 14:32:37 +00001102
1103
Fred Drakecff283c2000-08-21 22:24:43 +00001104/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001105 *
1106 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001107 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00001108 */
Guido van Rossum47478871996-08-21 14:32:37 +00001109static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001110validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001111{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001112 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +00001113 int res = (validate_ntype(tree, classdef) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001115
Guido van Rossum3d602e31996-07-21 02:33:56 +00001116 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001117 res = (validate_name(CHILD(tree, 0), "class")
1118 && validate_ntype(CHILD(tree, 1), NAME)
1119 && validate_colon(CHILD(tree, nch - 2))
1120 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001121 }
Brett Cannonf4189912005-04-09 02:30:16 +00001122 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001123 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +00001124 }
Guido van Rossumfc158e22007-11-15 19:17:28 +00001125
Brett Cannonf4189912005-04-09 02:30:16 +00001126 if (res) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 if (nch == 7) {
1128 res = ((validate_lparen(CHILD(tree, 2)) &&
1129 validate_arglist(CHILD(tree, 3)) &&
1130 validate_rparen(CHILD(tree, 4))));
1131 }
1132 else if (nch == 6) {
1133 res = (validate_lparen(CHILD(tree,2)) &&
1134 validate_rparen(CHILD(tree,3)));
1135 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001136 }
1137 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001138}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001139
1140
Guido van Rossum3d602e31996-07-21 02:33:56 +00001141/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001142 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001143 */
Guido van Rossum47478871996-08-21 14:32:37 +00001144static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001145validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001146{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001147 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001148 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001149 && (nch >= 4)
1150 && validate_name(CHILD(tree, 0), "if")
1151 && validate_test(CHILD(tree, 1))
1152 && validate_colon(CHILD(tree, 2))
1153 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001154
1155 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001156 /* ... 'else' ':' suite */
1157 res = (validate_name(CHILD(tree, nch - 3), "else")
1158 && validate_colon(CHILD(tree, nch - 2))
1159 && validate_suite(CHILD(tree, nch - 1)));
1160 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001161 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001162 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001163 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001164 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001165 /* Will catch the case for nch < 4 */
1166 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001167 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001168 /* ... ('elif' test ':' suite)+ ... */
1169 int j = 4;
1170 while ((j < nch) && res) {
1171 res = (validate_name(CHILD(tree, j), "elif")
1172 && validate_colon(CHILD(tree, j + 2))
1173 && validate_test(CHILD(tree, j + 1))
1174 && validate_suite(CHILD(tree, j + 3)));
1175 j += 4;
1176 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001177 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001178 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001179}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001180
1181
Guido van Rossum3d602e31996-07-21 02:33:56 +00001182/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001183 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001184 *
1185 */
Guido van Rossum47478871996-08-21 14:32:37 +00001186static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001187validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001188{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001189 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001190 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001191
Guido van Rossum3d602e31996-07-21 02:33:56 +00001192 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001193 res = (validate_lparen(CHILD(tree, 0))
1194 && validate_rparen(CHILD(tree, nch - 1)));
1195 if (res && (nch == 3))
1196 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001197 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001198 else {
1199 (void) validate_numnodes(tree, 2, "parameters");
1200 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001201 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001202}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001203
1204
Fred Drakecff283c2000-08-21 22:24:43 +00001205/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001206 *
1207 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001208 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001209 * | NEWLINE INDENT stmt+ DEDENT
1210 */
Guido van Rossum47478871996-08-21 14:32:37 +00001211static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001212validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001213{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001214 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001215 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001216
Guido van Rossum3d602e31996-07-21 02:33:56 +00001217 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001218 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001219 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001220 /* NEWLINE INDENT stmt+ DEDENT */
1221 res = (validate_newline(CHILD(tree, 0))
1222 && validate_indent(CHILD(tree, 1))
1223 && validate_stmt(CHILD(tree, 2))
1224 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001225
Fred Drakeff9ea482000-04-19 13:54:15 +00001226 if (res && (nch > 4)) {
1227 int i = 3;
1228 --nch; /* forget the DEDENT */
1229 for ( ; res && (i < nch); ++i)
1230 res = validate_stmt(CHILD(tree, i));
1231 }
1232 else if (nch < 4)
1233 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001234 }
1235 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001236}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001237
1238
Guido van Rossum47478871996-08-21 14:32:37 +00001239static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001240validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001241{
Guido van Rossum47478871996-08-21 14:32:37 +00001242 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001243 validate_test, "testlist"));
1244}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001245
Guido van Rossum1c917072001-10-15 15:44:05 +00001246static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00001247validate_testlist_star_expr(node *tl)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001248{
Benjamin Peterson4905e802009-09-27 02:43:28 +00001249 return (validate_repeating_list(tl, testlist_star_expr, validate_test_or_star_expr,
1250 "testlist"));
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001251}
1252
1253
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001254/* validate either vfpdef or tfpdef.
1255 * vfpdef: NAME
1256 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001257 */
1258static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001259validate_vfpdef(node *tree)
Neal Norwitzc1505362006-12-28 06:47:50 +00001260{
1261 int nch = NCH(tree);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001262 if (TYPE(tree) == vfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001263 return nch == 1 && validate_name(CHILD(tree, 0), NULL);
1264 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001265 else if (TYPE(tree) == tfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001266 if (nch == 1) {
1267 return validate_name(CHILD(tree, 0), NULL);
1268 }
1269 else if (nch == 3) {
1270 return validate_name(CHILD(tree, 0), NULL) &&
1271 validate_colon(CHILD(tree, 1)) &&
1272 validate_test(CHILD(tree, 2));
1273 }
1274 }
1275 return 0;
1276}
1277
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001278/* '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001279 * ..or tfpdef in place of vfpdef. vfpdef: NAME; tfpdef: NAME [':' test]
Fred Drakecff283c2000-08-21 22:24:43 +00001280 */
1281static int
1282validate_varargslist_trailer(node *tree, int start)
1283{
1284 int nch = NCH(tree);
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001285 int res = 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001286
1287 if (nch <= start) {
1288 err_string("expected variable argument trailer for varargslist");
1289 return 0;
1290 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001291 if (TYPE(CHILD(tree, start)) == STAR) {
Fred Drakecff283c2000-08-21 22:24:43 +00001292 /*
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001293 * '*' [vfpdef]
Fred Drakecff283c2000-08-21 22:24:43 +00001294 */
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001295 res = validate_star(CHILD(tree, start++));
1296 if (res && start < nch && (TYPE(CHILD(tree, start)) == vfpdef ||
1297 TYPE(CHILD(tree, start)) == tfpdef))
1298 res = validate_vfpdef(CHILD(tree, start++));
1299 /*
1300 * (',' vfpdef ['=' test])*
1301 */
1302 while (res && start + 1 < nch && (
1303 TYPE(CHILD(tree, start + 1)) == vfpdef ||
1304 TYPE(CHILD(tree, start + 1)) == tfpdef)) {
1305 res = (validate_comma(CHILD(tree, start++))
1306 && validate_vfpdef(CHILD(tree, start++)));
1307 if (res && start + 1 < nch && TYPE(CHILD(tree, start)) == EQUAL)
1308 res = (validate_equal(CHILD(tree, start++))
1309 && validate_test(CHILD(tree, start++)));
1310 }
1311 /*
1312 * [',' '**' vfpdef]
1313 */
1314 if (res && start + 2 < nch && TYPE(CHILD(tree, start+1)) == DOUBLESTAR)
1315 res = (validate_comma(CHILD(tree, start++))
1316 && validate_doublestar(CHILD(tree, start++))
1317 && validate_vfpdef(CHILD(tree, start++)));
1318 }
1319 else if (TYPE(CHILD(tree, start)) == DOUBLESTAR) {
1320 /*
1321 * '**' vfpdef
1322 */
1323 if (start + 1 < nch)
1324 res = (validate_doublestar(CHILD(tree, start++))
1325 && validate_vfpdef(CHILD(tree, start++)));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001326 else {
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001327 res = 0;
1328 err_string("expected vfpdef after ** in varargslist trailer");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001329 }
Fred Drakecff283c2000-08-21 22:24:43 +00001330 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001331 else {
1332 res = 0;
1333 err_string("expected * or ** in varargslist trailer");
Fred Drakecff283c2000-08-21 22:24:43 +00001334 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001335
1336 if (res && start != nch) {
1337 res = 0;
1338 err_string("unexpected extra children in varargslist trailer");
1339 }
Fred Drakecff283c2000-08-21 22:24:43 +00001340 return res;
1341}
1342
1343
Neal Norwitzc1505362006-12-28 06:47:50 +00001344/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001345 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001346 * Validate typedargslist or varargslist.
1347 *
1348 * typedargslist: ((tfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001349 * ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
1350 * '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001351 * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001352 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001353 * varargslist: ((vfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001354 * ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
1355 * '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001356 * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001357 * vfpdef: NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001358 *
1359 */
Guido van Rossum47478871996-08-21 14:32:37 +00001360static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001361validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001362{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001363 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001364 int res = (TYPE(tree) == varargslist ||
1365 TYPE(tree) == typedargslist) &&
1366 (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001367 int sym;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001368 node *ch;
1369 int i = 0;
Guido van Rossumfc158e22007-11-15 19:17:28 +00001370
Fred Drakeb6429a22000-12-11 22:08:27 +00001371 if (!res)
1372 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001373 if (nch < 1) {
1374 err_string("varargslist missing child nodes");
1375 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001376 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001377 while (i < nch) {
Guido van Rossumfc158e22007-11-15 19:17:28 +00001378 ch = CHILD(tree, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001379 sym = TYPE(ch);
1380 if (sym == vfpdef || sym == tfpdef) {
1381 /* validate (vfpdef ['=' test] ',')+ */
1382 res = validate_vfpdef(ch);
Fred Drakecff283c2000-08-21 22:24:43 +00001383 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001384 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1385 res = (validate_equal(CHILD(tree, i))
1386 && validate_test(CHILD(tree, i+1)));
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001387 if (res)
1388 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001389 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001390 if (res && i < nch) {
1391 res = validate_comma(CHILD(tree, i));
1392 ++i;
Fred Drakecff283c2000-08-21 22:24:43 +00001393 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001394 } else if (sym == DOUBLESTAR || sym == STAR) {
1395 res = validate_varargslist_trailer(tree, i);
1396 break;
1397 } else {
1398 res = 0;
1399 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001400 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001401 }
Fred Drakecff283c2000-08-21 22:24:43 +00001402 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001403}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001404
1405
Nick Coghlan650f0d02007-04-15 12:05:43 +00001406/* comp_iter: comp_for | comp_if
Fred Drakecff283c2000-08-21 22:24:43 +00001407 */
1408static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001409validate_comp_iter(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001410{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001411 int res = (validate_ntype(tree, comp_iter)
1412 && validate_numnodes(tree, 1, "comp_iter"));
1413 if (res && TYPE(CHILD(tree, 0)) == comp_for)
1414 res = validate_comp_for(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001415 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001416 res = validate_comp_if(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001417
1418 return res;
1419}
1420
Nick Coghlan650f0d02007-04-15 12:05:43 +00001421/* comp_for: 'for' exprlist 'in' test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001422 */
1423static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001424validate_comp_for(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001425{
1426 int nch = NCH(tree);
1427 int res;
1428
1429 if (nch == 5)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001430 res = validate_comp_iter(CHILD(tree, 4));
Fred Drakecff283c2000-08-21 22:24:43 +00001431 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001432 res = validate_numnodes(tree, 4, "comp_for");
Raymond Hettinger354433a2004-05-19 08:20:33 +00001433
1434 if (res)
1435 res = (validate_name(CHILD(tree, 0), "for")
1436 && validate_exprlist(CHILD(tree, 1))
1437 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001438 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001439
1440 return res;
1441}
1442
Nick Coghlan650f0d02007-04-15 12:05:43 +00001443/* comp_if: 'if' test_nocond [comp_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001444 */
1445static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001446validate_comp_if(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001447{
1448 int nch = NCH(tree);
1449 int res;
1450
1451 if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001452 res = validate_comp_iter(CHILD(tree, 2));
Fred Drakecff283c2000-08-21 22:24:43 +00001453 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001454 res = validate_numnodes(tree, 2, "comp_if");
Fred Drakecff283c2000-08-21 22:24:43 +00001455
1456 if (res)
1457 res = (validate_name(CHILD(tree, 0), "if")
Nick Coghlan650f0d02007-04-15 12:05:43 +00001458 && validate_test_nocond(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001459
1460 return res;
1461}
1462
1463
Guido van Rossum3d602e31996-07-21 02:33:56 +00001464/* simple_stmt | compound_stmt
1465 *
1466 */
Guido van Rossum47478871996-08-21 14:32:37 +00001467static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001468validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001469{
1470 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001471 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001472
Guido van Rossum3d602e31996-07-21 02:33:56 +00001473 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001474 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001475
Fred Drakeff9ea482000-04-19 13:54:15 +00001476 if (TYPE(tree) == simple_stmt)
1477 res = validate_simple_stmt(tree);
1478 else
1479 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001480 }
1481 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001482}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001483
1484
Guido van Rossum3d602e31996-07-21 02:33:56 +00001485/* small_stmt (';' small_stmt)* [';'] NEWLINE
1486 *
1487 */
Guido van Rossum47478871996-08-21 14:32:37 +00001488static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001489validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001490{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001491 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001492 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001493 && (nch >= 2)
1494 && validate_small_stmt(CHILD(tree, 0))
1495 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001496
Guido van Rossum3d602e31996-07-21 02:33:56 +00001497 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001498 res = validate_numnodes(tree, 2, "simple_stmt");
1499 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001500 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001501 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001502 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001503 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001504
Fred Drakeff9ea482000-04-19 13:54:15 +00001505 for (i = 1; res && (i < nch); i += 2)
1506 res = (validate_semi(CHILD(tree, i))
1507 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001508 }
1509 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001510}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001511
1512
Guido van Rossum47478871996-08-21 14:32:37 +00001513static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001514validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001515{
1516 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001517 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001518
Fred Drake0ac9b072000-09-12 21:58:06 +00001519 if (res) {
1520 int ntype = TYPE(CHILD(tree, 0));
1521
1522 if ( (ntype == expr_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001523 || (ntype == del_stmt)
1524 || (ntype == pass_stmt)
1525 || (ntype == flow_stmt)
1526 || (ntype == import_stmt)
1527 || (ntype == global_stmt)
Mark Dickinson407b3bd2012-04-29 22:18:31 +01001528 || (ntype == nonlocal_stmt)
Georg Brandl7cae87c2006-09-06 06:51:57 +00001529 || (ntype == assert_stmt))
Fred Drake0ac9b072000-09-12 21:58:06 +00001530 res = validate_node(CHILD(tree, 0));
1531 else {
1532 res = 0;
1533 err_string("illegal small_stmt child type");
1534 }
1535 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001536 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001537 res = 0;
1538 PyErr_Format(parser_error,
1539 "Unrecognized child node of small_stmt: %d.",
1540 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001541 }
1542 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001543}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001544
1545
1546/* compound_stmt:
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00001547 * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
Guido van Rossum3d602e31996-07-21 02:33:56 +00001548 */
Guido van Rossum47478871996-08-21 14:32:37 +00001549static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001550validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001551{
1552 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001553 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001554 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001555
1556 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001557 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001558
1559 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001560 ntype = TYPE(tree);
1561 if ( (ntype == if_stmt)
1562 || (ntype == while_stmt)
1563 || (ntype == for_stmt)
1564 || (ntype == try_stmt)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00001565 || (ntype == with_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001566 || (ntype == funcdef)
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001567 || (ntype == classdef)
1568 || (ntype == decorated))
Fred Drakeff9ea482000-04-19 13:54:15 +00001569 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001570 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001571 res = 0;
1572 PyErr_Format(parser_error,
1573 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001574 }
1575 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001576}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001577
Guido van Rossum47478871996-08-21 14:32:37 +00001578static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00001579validate_yield_or_testlist(node *tree, int tse)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001580{
Benjamin Peterson4905e802009-09-27 02:43:28 +00001581 if (TYPE(tree) == yield_expr) {
1582 return validate_yield_expr(tree);
1583 }
1584 else {
1585 if (tse)
1586 return validate_testlist_star_expr(tree);
1587 else
1588 return validate_testlist(tree);
1589 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001590}
1591
1592static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001593validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001594{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001595 int j;
1596 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001597 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001598 && is_odd(nch)
Benjamin Peterson4905e802009-09-27 02:43:28 +00001599 && validate_testlist_star_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001600
Fred Drake28f739a2000-08-25 22:42:40 +00001601 if (res && nch == 3
1602 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001603 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
Benjamin Peterson4905e802009-09-27 02:43:28 +00001604 && validate_yield_or_testlist(CHILD(tree, 2), 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001605
Fred Drake28f739a2000-08-25 22:42:40 +00001606 if (res) {
1607 char *s = STR(CHILD(CHILD(tree, 1), 0));
1608
1609 res = (strcmp(s, "+=") == 0
1610 || strcmp(s, "-=") == 0
1611 || strcmp(s, "*=") == 0
1612 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001613 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001614 || strcmp(s, "%=") == 0
1615 || strcmp(s, "&=") == 0
1616 || strcmp(s, "|=") == 0
1617 || strcmp(s, "^=") == 0
1618 || strcmp(s, "<<=") == 0
1619 || strcmp(s, ">>=") == 0
1620 || strcmp(s, "**=") == 0);
1621 if (!res)
Ezio Melotti42da6632011-03-15 05:18:48 +02001622 err_string("illegal augmented assignment operator");
Fred Drake28f739a2000-08-25 22:42:40 +00001623 }
1624 }
1625 else {
1626 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001627 res = validate_equal(CHILD(tree, j))
Benjamin Peterson4905e802009-09-27 02:43:28 +00001628 && validate_yield_or_testlist(CHILD(tree, j + 1), 1);
Fred Drake28f739a2000-08-25 22:42:40 +00001629 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001630 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001631}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001632
1633
Guido van Rossum47478871996-08-21 14:32:37 +00001634static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001635validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001636{
1637 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001638 && validate_name(CHILD(tree, 0), "del")
1639 && validate_exprlist(CHILD(tree, 1)));
1640}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001641
1642
Guido van Rossum47478871996-08-21 14:32:37 +00001643static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001644validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001645{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001646 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001647 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001648 && ((nch == 1) || (nch == 2))
1649 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001650
Guido van Rossum3d602e31996-07-21 02:33:56 +00001651 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001652 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001653
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001654 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001655}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001656
1657
Mark Dickinsoncf360b92012-05-07 12:01:27 +01001658/*
1659 * raise_stmt:
1660 *
1661 * 'raise' [test ['from' test]]
1662 */
Guido van Rossum47478871996-08-21 14:32:37 +00001663static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001664validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001665{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001666 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001667 int res = (validate_ntype(tree, raise_stmt)
Mark Dickinsoncf360b92012-05-07 12:01:27 +01001668 && ((nch == 1) || (nch == 2) || (nch == 4)));
1669
1670 if (!res && !PyErr_Occurred())
1671 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001672
Guido van Rossum3d602e31996-07-21 02:33:56 +00001673 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001674 res = validate_name(CHILD(tree, 0), "raise");
1675 if (res && (nch >= 2))
1676 res = validate_test(CHILD(tree, 1));
Mark Dickinsoncf360b92012-05-07 12:01:27 +01001677 if (res && (nch == 4)) {
1678 res = (validate_name(CHILD(tree, 2), "from")
Fred Drakeff9ea482000-04-19 13:54:15 +00001679 && validate_test(CHILD(tree, 3)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001680 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001681 }
1682 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001683}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001684
1685
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001686/* yield_expr: 'yield' [yield_arg]
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001687 */
1688static int
1689validate_yield_expr(node *tree)
1690{
1691 int nch = NCH(tree);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001692 if (nch < 1 || nch > 2)
1693 return 0;
1694 if (!validate_ntype(tree, yield_expr))
1695 return 0;
1696 if (!validate_name(CHILD(tree, 0), "yield"))
1697 return 0;
1698 if (nch == 2) {
1699 if (!validate_yield_arg(CHILD(tree, 1)))
1700 return 0;
1701 }
1702 return 1;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001703}
1704
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001705/* yield_arg: 'from' test | testlist
1706 */
1707static int
1708validate_yield_arg(node *tree)
1709{
1710 int nch = NCH(tree);
1711 if (!validate_ntype(tree, yield_arg))
1712 return 0;
1713 switch (nch) {
1714 case 1:
1715 if (!validate_testlist(CHILD(tree, nch - 1)))
1716 return 0;
1717 break;
1718 case 2:
1719 if (!validate_name(CHILD(tree, 0), "from"))
1720 return 0;
1721 if (!validate_test(CHILD(tree, 1)))
1722 return 0;
1723 break;
1724 default:
1725 return 0;
1726 }
1727 return 1;
1728}
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001729
1730/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001731 */
1732static int
1733validate_yield_stmt(node *tree)
1734{
1735 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001736 && validate_numnodes(tree, 1, "yield_stmt")
1737 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001738}
1739
1740
Fred Drakecff283c2000-08-21 22:24:43 +00001741static int
1742validate_import_as_name(node *tree)
1743{
1744 int nch = NCH(tree);
1745 int ok = validate_ntype(tree, import_as_name);
1746
1747 if (ok) {
1748 if (nch == 1)
1749 ok = validate_name(CHILD(tree, 0), NULL);
1750 else if (nch == 3)
1751 ok = (validate_name(CHILD(tree, 0), NULL)
1752 && validate_name(CHILD(tree, 1), "as")
1753 && validate_name(CHILD(tree, 2), NULL));
1754 else
1755 ok = validate_numnodes(tree, 3, "import_as_name");
1756 }
1757 return ok;
1758}
1759
1760
Fred Drake71137082001-01-07 05:59:59 +00001761/* dotted_name: NAME ("." NAME)*
1762 */
1763static int
1764validate_dotted_name(node *tree)
1765{
1766 int nch = NCH(tree);
1767 int res = (validate_ntype(tree, dotted_name)
1768 && is_odd(nch)
1769 && validate_name(CHILD(tree, 0), NULL));
1770 int i;
1771
1772 for (i = 1; res && (i < nch); i += 2) {
1773 res = (validate_dot(CHILD(tree, i))
1774 && validate_name(CHILD(tree, i+1), NULL));
1775 }
1776 return res;
1777}
1778
1779
Fred Drakecff283c2000-08-21 22:24:43 +00001780/* dotted_as_name: dotted_name [NAME NAME]
1781 */
1782static int
1783validate_dotted_as_name(node *tree)
1784{
1785 int nch = NCH(tree);
1786 int res = validate_ntype(tree, dotted_as_name);
1787
1788 if (res) {
1789 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001790 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001791 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001792 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001793 && validate_name(CHILD(tree, 1), "as")
1794 && validate_name(CHILD(tree, 2), NULL));
1795 else {
1796 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001797 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001798 }
1799 }
1800 return res;
1801}
1802
1803
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001804/* dotted_as_name (',' dotted_as_name)* */
1805static int
1806validate_dotted_as_names(node *tree)
1807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 int nch = NCH(tree);
1809 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1810 int i;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 for (i = 1; res && (i < nch); i += 2)
1813 res = (validate_comma(CHILD(tree, i))
1814 && validate_dotted_as_name(CHILD(tree, i + 1)));
1815 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001816}
1817
1818
1819/* import_as_name (',' import_as_name)* [','] */
1820static int
1821validate_import_as_names(node *tree)
1822{
1823 int nch = NCH(tree);
1824 int res = validate_import_as_name(CHILD(tree, 0));
1825 int i;
1826
1827 for (i = 1; res && (i + 1 < nch); i += 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 res = (validate_comma(CHILD(tree, i))
1829 && validate_import_as_name(CHILD(tree, i + 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001830 return (res);
1831}
1832
1833
1834/* 'import' dotted_as_names */
1835static int
1836validate_import_name(node *tree)
1837{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 return (validate_ntype(tree, import_name)
1839 && validate_numnodes(tree, 2, "import_name")
1840 && validate_name(CHILD(tree, 0), "import")
1841 && validate_dotted_as_names(CHILD(tree, 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001842}
1843
Mark Dickinsonfeb3b752010-07-04 18:38:57 +00001844/* Helper function to count the number of leading dots (or ellipsis tokens) in
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001845 * 'from ...module import name'
1846 */
1847static int
1848count_from_dots(node *tree)
1849{
Mark Dickinsonfeb3b752010-07-04 18:38:57 +00001850 int i;
1851 for (i = 1; i < NCH(tree); i++)
1852 if (TYPE(CHILD(tree, i)) != DOT && TYPE(CHILD(tree, i)) != ELLIPSIS)
1853 break;
1854 return i - 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001855}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001856
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +00001857/* import_from: ('from' ('.'* dotted_name | '.'+)
1858 * 'import' ('*' | '(' import_as_names ')' | import_as_names))
Guido van Rossum3d602e31996-07-21 02:33:56 +00001859 */
Guido van Rossum47478871996-08-21 14:32:37 +00001860static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001861validate_import_from(node *tree)
1862{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 int nch = NCH(tree);
1864 int ndots = count_from_dots(tree);
1865 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1866 int offset = ndots + havename;
1867 int res = validate_ntype(tree, import_from)
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +00001868 && (offset >= 1)
1869 && (nch >= 3 + offset)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 && validate_name(CHILD(tree, 0), "from")
1871 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1872 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1875 res = ((nch == offset + 5)
1876 && validate_lparen(CHILD(tree, offset + 2))
1877 && validate_import_as_names(CHILD(tree, offset + 3))
1878 && validate_rparen(CHILD(tree, offset + 4)));
1879 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1880 res = validate_import_as_names(CHILD(tree, offset + 2));
1881 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001882}
1883
1884
1885/* import_stmt: import_name | import_from */
1886static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001887validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001888{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001889 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001890 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001891
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001892 if (res) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 if (ntype == import_name || ntype == import_from)
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001896 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001897 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001898 res = 0;
1899 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001900 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001901 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001902 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001903 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001904 PyErr_Format(parser_error,
1905 "Unrecognized child node of import_stmt: %d.",
1906 TYPE(CHILD(tree, 0)));
1907 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001908 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001909}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001910
1911
Mark Dickinson407b3bd2012-04-29 22:18:31 +01001912/* global_stmt:
1913 *
1914 * 'global' NAME (',' NAME)*
1915 */
Guido van Rossum47478871996-08-21 14:32:37 +00001916static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001917validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001918{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001919 int j;
1920 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001921 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001922 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001923
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001924 if (!res && !PyErr_Occurred())
1925 err_string("illegal global statement");
1926
Guido van Rossum3d602e31996-07-21 02:33:56 +00001927 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001928 res = (validate_name(CHILD(tree, 0), "global")
1929 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001930 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001931 res = (validate_comma(CHILD(tree, j))
1932 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001933
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001934 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001935}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001936
Mark Dickinson407b3bd2012-04-29 22:18:31 +01001937/* nonlocal_stmt:
1938 *
1939 * 'nonlocal' NAME (',' NAME)*
1940 */
1941static int
1942validate_nonlocal_stmt(node *tree)
1943{
1944 int j;
1945 int nch = NCH(tree);
1946 int res = (validate_ntype(tree, nonlocal_stmt)
1947 && is_even(nch) && (nch >= 2));
1948
1949 if (!res && !PyErr_Occurred())
1950 err_string("illegal nonlocal statement");
1951
1952 if (res)
1953 res = (validate_name(CHILD(tree, 0), "nonlocal")
1954 && validate_ntype(CHILD(tree, 1), NAME));
1955 for (j = 2; res && (j < nch); j += 2)
1956 res = (validate_comma(CHILD(tree, j))
1957 && validate_ntype(CHILD(tree, j + 1), NAME));
1958
1959 return res;
1960}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001961
Guido van Rossum925e5471997-04-02 05:32:13 +00001962/* assert_stmt:
1963 *
1964 * 'assert' test [',' test]
1965 */
1966static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001967validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001968{
1969 int nch = NCH(tree);
1970 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001971 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001972 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001973 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001974
1975 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001976 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001977 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001978 res = (validate_comma(CHILD(tree, 2))
1979 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001980
1981 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001982}
Guido van Rossum925e5471997-04-02 05:32:13 +00001983
1984
Guido van Rossum47478871996-08-21 14:32:37 +00001985static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001986validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001987{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001988 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001989 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001990 && ((nch == 4) || (nch == 7))
1991 && validate_name(CHILD(tree, 0), "while")
1992 && validate_test(CHILD(tree, 1))
1993 && validate_colon(CHILD(tree, 2))
1994 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001995
Guido van Rossum3d602e31996-07-21 02:33:56 +00001996 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001997 res = (validate_name(CHILD(tree, 4), "else")
1998 && validate_colon(CHILD(tree, 5))
1999 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002000
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002001 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002002}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002003
2004
Guido van Rossum47478871996-08-21 14:32:37 +00002005static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002006validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002007{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002008 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002009 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002010 && ((nch == 6) || (nch == 9))
2011 && validate_name(CHILD(tree, 0), "for")
2012 && validate_exprlist(CHILD(tree, 1))
2013 && validate_name(CHILD(tree, 2), "in")
2014 && validate_testlist(CHILD(tree, 3))
2015 && validate_colon(CHILD(tree, 4))
2016 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002017
Guido van Rossum3d602e31996-07-21 02:33:56 +00002018 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00002019 res = (validate_name(CHILD(tree, 6), "else")
2020 && validate_colon(CHILD(tree, 7))
2021 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002022
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002023 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002024}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002025
2026
Guido van Rossum3d602e31996-07-21 02:33:56 +00002027/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00002028 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Georg Brandleee31162008-12-07 15:15:22 +00002029 ['finally' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002030 * | 'try' ':' suite 'finally' ':' suite
2031 *
2032 */
Guido van Rossum47478871996-08-21 14:32:37 +00002033static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002034validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002035{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002036 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002037 int pos = 3;
2038 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002039 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002040
2041 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002042 res = (validate_name(CHILD(tree, 0), "try")
2043 && validate_colon(CHILD(tree, 1))
2044 && validate_suite(CHILD(tree, 2))
2045 && validate_colon(CHILD(tree, nch - 2))
2046 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00002047 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00002048 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00002049 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
2050 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00002051
2052 PyErr_Format(parser_error,
2053 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002054 }
Georg Brandleee31162008-12-07 15:15:22 +00002055 /* Handle try/finally statement */
2056 if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
2057 (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
2058 res = (validate_numnodes(tree, 6, "try/finally")
2059 && validate_colon(CHILD(tree, 4))
2060 && validate_suite(CHILD(tree, 5)));
2061 return (res);
2062 }
2063 /* try/except statement: skip past except_clause sections */
Antoine Pitrou37d1c182009-05-14 21:54:00 +00002064 while (res && pos < nch && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002065 res = (validate_except_clause(CHILD(tree, pos))
2066 && validate_colon(CHILD(tree, pos + 1))
2067 && validate_suite(CHILD(tree, pos + 2)));
2068 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002069 }
Georg Brandleee31162008-12-07 15:15:22 +00002070 /* skip else clause */
Antoine Pitrou37d1c182009-05-14 21:54:00 +00002071 if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) &&
Georg Brandleee31162008-12-07 15:15:22 +00002072 (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
2073 res = (validate_colon(CHILD(tree, pos + 1))
2074 && validate_suite(CHILD(tree, pos + 2)));
2075 pos += 3;
2076 }
2077 if (res && pos < nch) {
2078 /* last clause must be a finally */
2079 res = (validate_name(CHILD(tree, pos), "finally")
2080 && validate_numnodes(tree, pos + 3, "try/except/finally")
2081 && validate_colon(CHILD(tree, pos + 1))
2082 && validate_suite(CHILD(tree, pos + 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002083 }
2084 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002085}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002086
2087
Guido van Rossum47478871996-08-21 14:32:37 +00002088static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002089validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002090{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002091 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002092 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00002093 && ((nch == 1) || (nch == 2) || (nch == 4))
2094 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002095
Guido van Rossum3d602e31996-07-21 02:33:56 +00002096 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002097 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002098 if (res && (nch == 4))
Guido van Rossumb940e112007-01-10 16:19:56 +00002099 res = (validate_name(CHILD(tree, 2), "as")
2100 && validate_ntype(CHILD(tree, 3), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002101
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002102 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002103}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002104
2105
Guido van Rossum47478871996-08-21 14:32:37 +00002106static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002107validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002108{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002109 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002110 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002111
Guido van Rossum3d602e31996-07-21 02:33:56 +00002112 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002113 res = ((nch == 1)
2114 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002115 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002116 res = validate_or_test(CHILD(tree, 0));
2117 res = (res && (nch == 1 || (nch == 5 &&
2118 validate_name(CHILD(tree, 1), "if") &&
2119 validate_or_test(CHILD(tree, 2)) &&
2120 validate_name(CHILD(tree, 3), "else") &&
2121 validate_test(CHILD(tree, 4)))));
2122 }
2123 return (res);
2124}
2125
2126static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002127validate_test_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002128{
2129 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002130 int res = validate_ntype(tree, test_nocond) && (nch == 1);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002131
Nick Coghlan650f0d02007-04-15 12:05:43 +00002132 if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
2133 res = (validate_lambdef_nocond(CHILD(tree, 0)));
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002134 else if (res) {
2135 res = (validate_or_test(CHILD(tree, 0)));
2136 }
2137 return (res);
2138}
2139
2140static int
2141validate_or_test(node *tree)
2142{
2143 int nch = NCH(tree);
2144 int res = validate_ntype(tree, or_test) && is_odd(nch);
2145
2146 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002147 int pos;
2148 res = validate_and_test(CHILD(tree, 0));
2149 for (pos = 1; res && (pos < nch); pos += 2)
2150 res = (validate_name(CHILD(tree, pos), "or")
2151 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002152 }
2153 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002154}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002155
2156
Guido van Rossum47478871996-08-21 14:32:37 +00002157static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002158validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002159{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002160 int pos;
2161 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002162 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002163 && is_odd(nch)
2164 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002165
Guido van Rossum3d602e31996-07-21 02:33:56 +00002166 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002167 res = (validate_name(CHILD(tree, pos), "and")
2168 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002169
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002170 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002171}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002172
2173
Guido van Rossum47478871996-08-21 14:32:37 +00002174static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002175validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002176{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002177 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002178 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002179
Guido van Rossum3d602e31996-07-21 02:33:56 +00002180 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002181 if (nch == 2)
2182 res = (validate_name(CHILD(tree, 0), "not")
2183 && validate_not_test(CHILD(tree, 1)));
2184 else if (nch == 1)
2185 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002186 }
2187 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002188}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002189
2190
Guido van Rossum47478871996-08-21 14:32:37 +00002191static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002192validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002193{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002194 int pos;
2195 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002196 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002197 && is_odd(nch)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002198 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002199
Guido van Rossum3d602e31996-07-21 02:33:56 +00002200 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002201 res = (validate_comp_op(CHILD(tree, pos))
Benjamin Peterson4905e802009-09-27 02:43:28 +00002202 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002203
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002204 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002205}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002206
2207
Guido van Rossum47478871996-08-21 14:32:37 +00002208static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002209validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002210{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002211 int res = 0;
2212 int nch = NCH(tree);
2213
Guido van Rossum3d602e31996-07-21 02:33:56 +00002214 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002215 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002216 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002217 /*
2218 * Only child will be a terminal with a well-defined symbolic name
2219 * or a NAME with a string of either 'is' or 'in'
2220 */
2221 tree = CHILD(tree, 0);
2222 switch (TYPE(tree)) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002223 case LESS:
2224 case GREATER:
2225 case EQEQUAL:
2226 case EQUAL:
2227 case LESSEQUAL:
2228 case GREATEREQUAL:
2229 case NOTEQUAL:
Fred Drakeff9ea482000-04-19 13:54:15 +00002230 res = 1;
2231 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002232 case NAME:
Fred Drakeff9ea482000-04-19 13:54:15 +00002233 res = ((strcmp(STR(tree), "in") == 0)
2234 || (strcmp(STR(tree), "is") == 0));
2235 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002236 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002237 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002238 }
2239 break;
2240 default:
Fred Drake661ea262000-10-24 19:57:45 +00002241 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002242 break;
2243 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002244 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002245 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002246 res = (validate_ntype(CHILD(tree, 0), NAME)
2247 && validate_ntype(CHILD(tree, 1), NAME)
2248 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2249 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2250 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2251 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2252 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002253 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002254 }
2255 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002256}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002257
2258
Guido van Rossum47478871996-08-21 14:32:37 +00002259static int
Guido van Rossum0368b722007-05-11 16:50:42 +00002260validate_star_expr(node *tree)
2261{
2262 int res = validate_ntype(tree, star_expr);
2263 if (!res) return res;
Benjamin Peterson4905e802009-09-27 02:43:28 +00002264 if (!validate_numnodes(tree, 2, "star_expr"))
2265 return 0;
2266 return validate_ntype(CHILD(tree, 0), STAR) && \
2267 validate_expr(CHILD(tree, 1));
Guido van Rossum0368b722007-05-11 16:50:42 +00002268}
2269
2270
2271static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002272validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002273{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002274 int j;
2275 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002276 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002277 && is_odd(nch)
2278 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002279
Guido van Rossum3d602e31996-07-21 02:33:56 +00002280 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002281 res = (validate_xor_expr(CHILD(tree, j))
2282 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002283
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002284 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002285}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002286
2287
Guido van Rossum47478871996-08-21 14:32:37 +00002288static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002289validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002290{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002291 int j;
2292 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002293 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002294 && is_odd(nch)
2295 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002296
Guido van Rossum3d602e31996-07-21 02:33:56 +00002297 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002298 res = (validate_circumflex(CHILD(tree, j - 1))
2299 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002300
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002301 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002302}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002303
2304
Guido van Rossum47478871996-08-21 14:32:37 +00002305static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002306validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002307{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002308 int pos;
2309 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002310 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002311 && is_odd(nch)
2312 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002313
Guido van Rossum3d602e31996-07-21 02:33:56 +00002314 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002315 res = (validate_ampersand(CHILD(tree, pos))
2316 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002317
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002318 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002319}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002320
2321
2322static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002323validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002324 {
2325 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002326 int nch = NCH(tree);
2327 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002328 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002329
Guido van Rossum3d602e31996-07-21 02:33:56 +00002330 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002331 if (TYPE(CHILD(tree, pos)) != op1)
2332 res = validate_ntype(CHILD(tree, pos), op2);
2333 if (res)
2334 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002335 }
2336 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002337}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002338
2339
Guido van Rossum47478871996-08-21 14:32:37 +00002340static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002341validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002342{
2343 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002344 && validate_chain_two_ops(tree, validate_arith_expr,
2345 LEFTSHIFT, RIGHTSHIFT));
2346}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002347
2348
Guido van Rossum47478871996-08-21 14:32:37 +00002349static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002350validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002351{
2352 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002353 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2354}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002355
2356
Guido van Rossum47478871996-08-21 14:32:37 +00002357static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002358validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002359{
2360 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002361 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002362 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002363 && is_odd(nch)
2364 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002365
Guido van Rossum3d602e31996-07-21 02:33:56 +00002366 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002367 res = (((TYPE(CHILD(tree, pos)) == STAR)
2368 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002369 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002370 || (TYPE(CHILD(tree, pos)) == PERCENT))
2371 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002372
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002373 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002374}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002375
2376
Guido van Rossum3d602e31996-07-21 02:33:56 +00002377/* factor:
2378 *
2379 * factor: ('+'|'-'|'~') factor | power
2380 */
Guido van Rossum47478871996-08-21 14:32:37 +00002381static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002382validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002383{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002384 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002385 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002386 && (((nch == 2)
2387 && ((TYPE(CHILD(tree, 0)) == PLUS)
2388 || (TYPE(CHILD(tree, 0)) == MINUS)
2389 || (TYPE(CHILD(tree, 0)) == TILDE))
2390 && validate_factor(CHILD(tree, 1)))
2391 || ((nch == 1)
2392 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002393 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002394}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002395
2396
Guido van Rossum3d602e31996-07-21 02:33:56 +00002397/* power:
2398 *
2399 * power: atom trailer* ('**' factor)*
2400 */
Guido van Rossum47478871996-08-21 14:32:37 +00002401static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002402validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002403{
2404 int pos = 1;
2405 int nch = NCH(tree);
2406 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002407 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002408
2409 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002410 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002411 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002412 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002413 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002414 return (0);
2415 }
2416 for ( ; res && (pos < (nch - 1)); pos += 2)
2417 res = (validate_doublestar(CHILD(tree, pos))
2418 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002419 }
2420 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002421}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002422
2423
Guido van Rossum47478871996-08-21 14:32:37 +00002424static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002425validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002426{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002427 int pos;
2428 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002429 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002430
Fred Drakecff283c2000-08-21 22:24:43 +00002431 if (res && nch < 1)
2432 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002433 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002434 switch (TYPE(CHILD(tree, 0))) {
2435 case LPAR:
2436 res = ((nch <= 3)
2437 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002438
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002439 if (res && (nch == 3)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 if (TYPE(CHILD(tree, 1))==yield_expr)
2441 res = validate_yield_expr(CHILD(tree, 1));
2442 else
2443 res = validate_testlist_comp(CHILD(tree, 1));
2444 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002445 break;
2446 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002447 if (nch == 2)
2448 res = validate_ntype(CHILD(tree, 1), RSQB);
2449 else if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002450 res = (validate_testlist_comp(CHILD(tree, 1))
Fred Drakecff283c2000-08-21 22:24:43 +00002451 && validate_ntype(CHILD(tree, 2), RSQB));
2452 else {
2453 res = 0;
2454 err_string("illegal list display atom");
2455 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002456 break;
2457 case LBRACE:
2458 res = ((nch <= 3)
2459 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002460
Fred Drakeff9ea482000-04-19 13:54:15 +00002461 if (res && (nch == 3))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002462 res = validate_dictorsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002463 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002464 case NAME:
2465 case NUMBER:
Mark Dickinsonda029fb2012-05-07 17:24:04 +01002466 case ELLIPSIS:
Fred Drakeff9ea482000-04-19 13:54:15 +00002467 res = (nch == 1);
2468 break;
2469 case STRING:
2470 for (pos = 1; res && (pos < nch); ++pos)
2471 res = validate_ntype(CHILD(tree, pos), STRING);
2472 break;
2473 default:
2474 res = 0;
2475 break;
2476 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002477 }
2478 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002479}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002480
2481
Nick Coghlan650f0d02007-04-15 12:05:43 +00002482/* testlist_comp:
2483 * test ( comp_for | (',' test)* [','] )
Fred Drake85bf3bb2000-08-23 15:35:26 +00002484 */
Fred Drakecff283c2000-08-21 22:24:43 +00002485static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002486validate_testlist_comp(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00002487{
2488 int nch = NCH(tree);
2489 int ok = nch;
2490
2491 if (nch == 0)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002492 err_string("missing child nodes of testlist_comp");
2493 else {
Benjamin Peterson4905e802009-09-27 02:43:28 +00002494 ok = validate_test_or_star_expr(CHILD(tree, 0));
Nick Coghlan650f0d02007-04-15 12:05:43 +00002495 }
Fred Drakecff283c2000-08-21 22:24:43 +00002496
2497 /*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002498 * comp_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002499 */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002500 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2501 ok = validate_comp_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002502 else {
2503 /* (',' test)* [','] */
2504 int i = 1;
2505 while (ok && nch - i >= 2) {
2506 ok = (validate_comma(CHILD(tree, i))
Benjamin Peterson4905e802009-09-27 02:43:28 +00002507 && validate_test_or_star_expr(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002508 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002509 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002510 if (ok && i == nch-1)
2511 ok = validate_comma(CHILD(tree, i));
2512 else if (i != nch) {
2513 ok = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002514 err_string("illegal trailing nodes for testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002515 }
2516 }
2517 return ok;
2518}
Fred Drakecff283c2000-08-21 22:24:43 +00002519
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002520/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002521 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002522 */
2523static int
2524validate_decorator(node *tree)
2525{
2526 int ok;
2527 int nch = NCH(tree);
2528 ok = (validate_ntype(tree, decorator) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 (nch == 3 || nch == 5 || nch == 6) &&
2530 validate_at(CHILD(tree, 0)) &&
2531 validate_dotted_name(CHILD(tree, 1)) &&
2532 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002533
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002534 if (ok && nch != 3) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 ok = (validate_lparen(CHILD(tree, 2)) &&
2536 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 if (ok && nch == 6)
2539 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002540 }
2541
2542 return ok;
2543}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002544
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002545/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002546 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002547 */
2548static int
2549validate_decorators(node *tree)
2550{
Guido van Rossumfc158e22007-11-15 19:17:28 +00002551 int i, nch, ok;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002552 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002553 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002554
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002555 for (i = 0; ok && i < nch; ++i)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002557
2558 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002559}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002560
Georg Brandl0c315622009-05-25 21:10:36 +00002561/* with_item:
2562 * test ['as' expr]
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002563 */
2564static int
Georg Brandl0c315622009-05-25 21:10:36 +00002565validate_with_item(node *tree)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002566{
2567 int nch = NCH(tree);
Georg Brandl0c315622009-05-25 21:10:36 +00002568 int ok = (validate_ntype(tree, with_item)
2569 && (nch == 1 || nch == 3)
2570 && validate_test(CHILD(tree, 0)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 if (ok && nch == 3)
Georg Brandl0c315622009-05-25 21:10:36 +00002572 ok = (validate_name(CHILD(tree, 1), "as")
2573 && validate_expr(CHILD(tree, 2)));
2574 return ok;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002575}
2576
Georg Brandl0c315622009-05-25 21:10:36 +00002577/* with_stmt:
2578 * 0 1 ... -2 -1
2579 * 'with' with_item (',' with_item)* ':' suite
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002580 */
2581static int
2582validate_with_stmt(node *tree)
2583{
Georg Brandl0c315622009-05-25 21:10:36 +00002584 int i;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002585 int nch = NCH(tree);
2586 int ok = (validate_ntype(tree, with_stmt)
Georg Brandl0c315622009-05-25 21:10:36 +00002587 && (nch % 2 == 0)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002588 && validate_name(CHILD(tree, 0), "with")
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002589 && validate_colon(RCHILD(tree, -2))
2590 && validate_suite(RCHILD(tree, -1)));
Georg Brandl0c315622009-05-25 21:10:36 +00002591 for (i = 1; ok && i < nch - 2; i += 2)
2592 ok = validate_with_item(CHILD(tree, i));
2593 return ok;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002594}
2595
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01002596/* funcdef: 'def' NAME parameters ['->' test] ':' suite */
2597
Guido van Rossum47478871996-08-21 14:32:37 +00002598static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002599validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002600{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002601 int nch = NCH(tree);
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01002602 int res = validate_ntype(tree, funcdef);
2603 if (res) {
2604 if (nch == 5) {
2605 res = (validate_name(CHILD(tree, 0), "def")
2606 && validate_ntype(CHILD(tree, 1), NAME)
2607 && validate_parameters(CHILD(tree, 2))
2608 && validate_colon(CHILD(tree, 3))
2609 && validate_suite(CHILD(tree, 4)));
2610 }
2611 else if (nch == 7) {
2612 res = (validate_name(CHILD(tree, 0), "def")
2613 && validate_ntype(CHILD(tree, 1), NAME)
2614 && validate_parameters(CHILD(tree, 2))
2615 && validate_rarrow(CHILD(tree, 3))
2616 && validate_test(CHILD(tree, 4))
2617 && validate_colon(CHILD(tree, 5))
2618 && validate_suite(CHILD(tree, 6)));
2619 }
2620 else {
2621 res = 0;
2622 err_string("illegal number of children for funcdef");
2623 }
2624 }
2625 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00002626}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002627
2628
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002629/* decorated
2630 * decorators (classdef | funcdef)
2631 */
2632static int
2633validate_decorated(node *tree)
2634{
Mark Dickinson2bd61a92010-07-04 16:37:31 +00002635 int nch = NCH(tree);
2636 int ok = (validate_ntype(tree, decorated)
2637 && (nch == 2)
2638 && validate_decorators(RCHILD(tree, -2)));
2639 if (TYPE(RCHILD(tree, -1)) == funcdef)
2640 ok = ok && validate_funcdef(RCHILD(tree, -1));
2641 else
2642 ok = ok && validate_class(RCHILD(tree, -1));
2643 return ok;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002644}
2645
Guido van Rossum47478871996-08-21 14:32:37 +00002646static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002647validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002648{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002649 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002650 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002651 && ((nch == 3) || (nch == 4))
2652 && validate_name(CHILD(tree, 0), "lambda")
2653 && validate_colon(CHILD(tree, nch - 2))
2654 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002655
Guido van Rossum3d602e31996-07-21 02:33:56 +00002656 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002657 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002658 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002659 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002660
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002661 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002662}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002663
2664
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002665static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002666validate_lambdef_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002667{
2668 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002669 int res = (validate_ntype(tree, lambdef_nocond)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002670 && ((nch == 3) || (nch == 4))
2671 && validate_name(CHILD(tree, 0), "lambda")
2672 && validate_colon(CHILD(tree, nch - 2))
2673 && validate_test(CHILD(tree, nch - 1)));
2674
2675 if (res && (nch == 4))
2676 res = validate_varargslist(CHILD(tree, 1));
2677 else if (!res && !PyErr_Occurred())
Nick Coghlan650f0d02007-04-15 12:05:43 +00002678 (void) validate_numnodes(tree, 3, "lambdef_nocond");
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002679
2680 return (res);
2681}
2682
2683
Guido van Rossum3d602e31996-07-21 02:33:56 +00002684/* arglist:
2685 *
Fred Drakecff283c2000-08-21 22:24:43 +00002686 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002687 */
Guido van Rossum47478871996-08-21 14:32:37 +00002688static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002689validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002690{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002691 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002692 int i = 0;
2693 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002694
2695 if (nch <= 0)
2696 /* raise the right error from having an invalid number of children */
2697 return validate_numnodes(tree, nch + 1, "arglist");
2698
Raymond Hettinger354433a2004-05-19 08:20:33 +00002699 if (nch > 1) {
2700 for (i=0; i<nch; i++) {
2701 if (TYPE(CHILD(tree, i)) == argument) {
2702 node *ch = CHILD(tree, i);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002703 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
Raymond Hettinger354433a2004-05-19 08:20:33 +00002704 err_string("need '(', ')' for generator expression");
2705 return 0;
2706 }
2707 }
2708 }
2709 }
2710
Fred Drakecff283c2000-08-21 22:24:43 +00002711 while (ok && nch-i >= 2) {
2712 /* skip leading (argument ',') */
2713 ok = (validate_argument(CHILD(tree, i))
2714 && validate_comma(CHILD(tree, i+1)));
2715 if (ok)
2716 i += 2;
2717 else
2718 PyErr_Clear();
2719 }
2720 ok = 1;
2721 if (nch-i > 0) {
2722 /*
2723 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002724 */
Fred Drakecff283c2000-08-21 22:24:43 +00002725 int sym = TYPE(CHILD(tree, i));
2726
2727 if (sym == argument) {
2728 ok = validate_argument(CHILD(tree, i));
2729 if (ok && i+1 != nch) {
2730 err_string("illegal arglist specification"
2731 " (extra stuff on end)");
2732 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002733 }
Fred Drakecff283c2000-08-21 22:24:43 +00002734 }
2735 else if (sym == STAR) {
2736 ok = validate_star(CHILD(tree, i));
2737 if (ok && (nch-i == 2))
2738 ok = validate_test(CHILD(tree, i+1));
2739 else if (ok && (nch-i == 5))
2740 ok = (validate_test(CHILD(tree, i+1))
2741 && validate_comma(CHILD(tree, i+2))
2742 && validate_doublestar(CHILD(tree, i+3))
2743 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002744 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002745 err_string("illegal use of '*' in arglist");
2746 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002747 }
2748 }
Fred Drakecff283c2000-08-21 22:24:43 +00002749 else if (sym == DOUBLESTAR) {
2750 if (nch-i == 2)
2751 ok = (validate_doublestar(CHILD(tree, i))
2752 && validate_test(CHILD(tree, i+1)));
2753 else {
2754 err_string("illegal use of '**' in arglist");
2755 ok = 0;
2756 }
2757 }
2758 else {
2759 err_string("illegal arglist specification");
2760 ok = 0;
2761 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002762 }
2763 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002764}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002765
2766
2767
2768/* argument:
2769 *
Nick Coghlan650f0d02007-04-15 12:05:43 +00002770 * [test '='] test [comp_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002771 */
Guido van Rossum47478871996-08-21 14:32:37 +00002772static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002773validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002774{
2775 int nch = NCH(tree);
2776 int res = (validate_ntype(tree, argument)
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002777 && ((nch == 1) || (nch == 2) || (nch == 3)));
2778 if (res)
2779 res = validate_test(CHILD(tree, 0));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002780 if (res && (nch == 2))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002781 res = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002782 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002783 res = (validate_equal(CHILD(tree, 1))
2784 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002785
2786 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002787}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002788
2789
2790
2791/* trailer:
2792 *
Guido van Rossum47478871996-08-21 14:32:37 +00002793 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002794 */
Guido van Rossum47478871996-08-21 14:32:37 +00002795static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002796validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002797{
2798 int nch = NCH(tree);
2799 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002800
2801 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002802 switch (TYPE(CHILD(tree, 0))) {
2803 case LPAR:
2804 res = validate_rparen(CHILD(tree, nch - 1));
2805 if (res && (nch == 3))
2806 res = validate_arglist(CHILD(tree, 1));
2807 break;
2808 case LSQB:
2809 res = (validate_numnodes(tree, 3, "trailer")
2810 && validate_subscriptlist(CHILD(tree, 1))
2811 && validate_ntype(CHILD(tree, 2), RSQB));
2812 break;
2813 case DOT:
2814 res = (validate_numnodes(tree, 2, "trailer")
2815 && validate_ntype(CHILD(tree, 1), NAME));
2816 break;
2817 default:
2818 res = 0;
2819 break;
2820 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002821 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002822 else {
2823 (void) validate_numnodes(tree, 2, "trailer");
2824 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002825 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002826}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002827
2828
Guido van Rossum47478871996-08-21 14:32:37 +00002829/* subscriptlist:
2830 *
2831 * subscript (',' subscript)* [',']
2832 */
2833static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002834validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002835{
2836 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002837 validate_subscript, "subscriptlist"));
2838}
Guido van Rossum47478871996-08-21 14:32:37 +00002839
2840
Guido van Rossum3d602e31996-07-21 02:33:56 +00002841/* subscript:
2842 *
Guido van Rossum47478871996-08-21 14:32:37 +00002843 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002844 */
Guido van Rossum47478871996-08-21 14:32:37 +00002845static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002846validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002847{
Guido van Rossum47478871996-08-21 14:32:37 +00002848 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002849 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002850 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002851
Guido van Rossum47478871996-08-21 14:32:37 +00002852 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002853 if (!PyErr_Occurred())
2854 err_string("invalid number of arguments for subscript node");
2855 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002856 }
Guido van Rossum47478871996-08-21 14:32:37 +00002857 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002858 /* take care of ('.' '.' '.') possibility */
2859 return (validate_numnodes(tree, 3, "subscript")
2860 && validate_dot(CHILD(tree, 0))
2861 && validate_dot(CHILD(tree, 1))
2862 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002863 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002864 if (TYPE(CHILD(tree, 0)) == test)
2865 res = validate_test(CHILD(tree, 0));
2866 else
2867 res = validate_colon(CHILD(tree, 0));
2868 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002869 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002870 /* Must be [test] ':' [test] [sliceop],
2871 * but at least one of the optional components will
2872 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002873 */
2874 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002875 res = validate_test(CHILD(tree, 0));
2876 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002877 }
2878 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002879 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002880 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002881 int rem = nch - ++offset;
2882 if (rem) {
2883 if (TYPE(CHILD(tree, offset)) == test) {
2884 res = validate_test(CHILD(tree, offset));
2885 ++offset;
2886 --rem;
2887 }
2888 if (res && rem)
2889 res = validate_sliceop(CHILD(tree, offset));
2890 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002891 }
2892 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002893}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002894
2895
Guido van Rossum47478871996-08-21 14:32:37 +00002896static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002897validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002898{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002899 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002900 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002901 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002902 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002903 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002904 }
Guido van Rossum47478871996-08-21 14:32:37 +00002905 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002906 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002907 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002908 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002909
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002910 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002911}
Guido van Rossum47478871996-08-21 14:32:37 +00002912
2913
2914static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00002915validate_test_or_star_expr(node *n)
2916{
2917 if (TYPE(n) == test)
2918 return validate_test(n);
2919 return validate_star_expr(n);
2920}
2921
2922static int
2923validate_expr_or_star_expr(node *n)
2924{
2925 if (TYPE(n) == expr)
2926 return validate_expr(n);
2927 return validate_star_expr(n);
2928}
2929
2930
2931static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002932validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002933{
2934 return (validate_repeating_list(tree, exprlist,
Benjamin Peterson4905e802009-09-27 02:43:28 +00002935 validate_expr_or_star_expr, "exprlist"));
Fred Drakeff9ea482000-04-19 13:54:15 +00002936}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002937
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002938/*
2939 * dictorsetmaker:
2940 *
2941 * (test ':' test (comp_for | (',' test ':' test)* [','])) |
2942 * (test (comp_for | (',' test)* [',']))
2943 */
Guido van Rossum47478871996-08-21 14:32:37 +00002944static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002945validate_dictorsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002946{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002947 int nch = NCH(tree);
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002948 int res;
2949 int i = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002950
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002951 res = validate_ntype(tree, dictorsetmaker);
2952 if (!res)
2953 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002954
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002955 if (nch - i < 1) {
2956 (void) validate_numnodes(tree, 1, "dictorsetmaker");
2957 return 0;
2958 }
2959
2960 res = validate_test(CHILD(tree, i++));
2961 if (!res)
2962 return 0;
2963
2964 if (nch - i >= 2 && TYPE(CHILD(tree, i)) == COLON) {
2965 /* Dictionary display or dictionary comprehension. */
2966 res = (validate_colon(CHILD(tree, i++))
2967 && validate_test(CHILD(tree, i++)));
2968 if (!res)
2969 return 0;
2970
2971 if (nch - i >= 1 && TYPE(CHILD(tree, i)) == comp_for) {
2972 /* Dictionary comprehension. */
2973 res = validate_comp_for(CHILD(tree, i++));
2974 if (!res)
2975 return 0;
2976 }
2977 else {
2978 /* Dictionary display. */
2979 while (nch - i >= 4) {
2980 res = (validate_comma(CHILD(tree, i++))
2981 && validate_test(CHILD(tree, i++))
2982 && validate_colon(CHILD(tree, i++))
2983 && validate_test(CHILD(tree, i++)));
2984 if (!res)
2985 return 0;
2986 }
2987 if (nch - i == 1) {
2988 res = validate_comma(CHILD(tree, i++));
2989 if (!res)
2990 return 0;
2991 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002992 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002993 }
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002994 else {
2995 /* Set display or set comprehension. */
2996 if (nch - i >= 1 && TYPE(CHILD(tree, i)) == comp_for) {
2997 /* Set comprehension. */
2998 res = validate_comp_for(CHILD(tree, i++));
2999 if (!res)
3000 return 0;
3001 }
3002 else {
3003 /* Set display. */
3004 while (nch - i >= 2) {
3005 res = (validate_comma(CHILD(tree, i++))
3006 && validate_test(CHILD(tree, i++)));
3007 if (!res)
3008 return 0;
3009 }
3010 if (nch - i == 1) {
3011 res = validate_comma(CHILD(tree, i++));
3012 if (!res)
3013 return 0;
3014 }
3015 }
3016 }
3017
3018 if (nch - i > 0) {
3019 err_string("Illegal trailing nodes for dictorsetmaker.");
3020 return 0;
3021 }
3022
3023 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00003024}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003025
3026
Guido van Rossum47478871996-08-21 14:32:37 +00003027static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003028validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003029{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003030 int pos;
3031 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003032 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00003033 && (nch >= 2)
3034 && validate_testlist(CHILD(tree, 0))
3035 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003036
Guido van Rossum3d602e31996-07-21 02:33:56 +00003037 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00003038 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003039
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003040 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003041}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003042
3043
Guido van Rossum47478871996-08-21 14:32:37 +00003044static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003045validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003046{
Fred Drakeff9ea482000-04-19 13:54:15 +00003047 int nch = 0; /* num. children on current node */
3048 int res = 1; /* result value */
3049 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00003050
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00003051 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003052 nch = NCH(tree);
3053 next = 0;
3054 switch (TYPE(tree)) {
3055 /*
3056 * Definition nodes.
3057 */
3058 case funcdef:
3059 res = validate_funcdef(tree);
3060 break;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00003061 case with_stmt:
3062 res = validate_with_stmt(tree);
3063 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003064 case classdef:
3065 res = validate_class(tree);
3066 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 case decorated:
3068 res = validate_decorated(tree);
3069 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003070 /*
3071 * "Trivial" parse tree nodes.
3072 * (Why did I call these trivial?)
3073 */
3074 case stmt:
3075 res = validate_stmt(tree);
3076 break;
3077 case small_stmt:
3078 /*
Mark Dickinson407b3bd2012-04-29 22:18:31 +01003079 * expr_stmt | del_stmt | pass_stmt | flow_stmt |
3080 * import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00003081 */
3082 res = validate_small_stmt(tree);
3083 break;
3084 case flow_stmt:
3085 res = (validate_numnodes(tree, 1, "flow_stmt")
3086 && ((TYPE(CHILD(tree, 0)) == break_stmt)
3087 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00003088 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00003089 || (TYPE(CHILD(tree, 0)) == return_stmt)
3090 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
3091 if (res)
3092 next = CHILD(tree, 0);
3093 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00003094 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003095 break;
Fred Drake02126f22001-07-17 02:59:15 +00003096 case yield_stmt:
3097 res = validate_yield_stmt(tree);
3098 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003099 /*
3100 * Compound statements.
3101 */
3102 case simple_stmt:
3103 res = validate_simple_stmt(tree);
3104 break;
3105 case compound_stmt:
3106 res = validate_compound_stmt(tree);
3107 break;
3108 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00003109 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00003110 */
3111 case expr_stmt:
3112 res = validate_expr_stmt(tree);
3113 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003114 case del_stmt:
3115 res = validate_del_stmt(tree);
3116 break;
3117 case pass_stmt:
3118 res = (validate_numnodes(tree, 1, "pass")
3119 && validate_name(CHILD(tree, 0), "pass"));
3120 break;
3121 case break_stmt:
3122 res = (validate_numnodes(tree, 1, "break")
3123 && validate_name(CHILD(tree, 0), "break"));
3124 break;
3125 case continue_stmt:
3126 res = (validate_numnodes(tree, 1, "continue")
3127 && validate_name(CHILD(tree, 0), "continue"));
3128 break;
3129 case return_stmt:
3130 res = validate_return_stmt(tree);
3131 break;
3132 case raise_stmt:
3133 res = validate_raise_stmt(tree);
3134 break;
3135 case import_stmt:
3136 res = validate_import_stmt(tree);
3137 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 case import_name:
3139 res = validate_import_name(tree);
3140 break;
3141 case import_from:
3142 res = validate_import_from(tree);
3143 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003144 case global_stmt:
3145 res = validate_global_stmt(tree);
3146 break;
Mark Dickinson407b3bd2012-04-29 22:18:31 +01003147 case nonlocal_stmt:
3148 res = validate_nonlocal_stmt(tree);
3149 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003150 case assert_stmt:
3151 res = validate_assert_stmt(tree);
3152 break;
3153 case if_stmt:
3154 res = validate_if(tree);
3155 break;
3156 case while_stmt:
3157 res = validate_while(tree);
3158 break;
3159 case for_stmt:
3160 res = validate_for(tree);
3161 break;
3162 case try_stmt:
3163 res = validate_try(tree);
3164 break;
3165 case suite:
3166 res = validate_suite(tree);
3167 break;
3168 /*
3169 * Expression nodes.
3170 */
3171 case testlist:
3172 res = validate_testlist(tree);
3173 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003174 case yield_expr:
3175 res = validate_yield_expr(tree);
3176 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003177 case test:
3178 res = validate_test(tree);
3179 break;
3180 case and_test:
3181 res = validate_and_test(tree);
3182 break;
3183 case not_test:
3184 res = validate_not_test(tree);
3185 break;
3186 case comparison:
3187 res = validate_comparison(tree);
3188 break;
3189 case exprlist:
3190 res = validate_exprlist(tree);
3191 break;
3192 case comp_op:
3193 res = validate_comp_op(tree);
3194 break;
3195 case expr:
3196 res = validate_expr(tree);
3197 break;
3198 case xor_expr:
3199 res = validate_xor_expr(tree);
3200 break;
3201 case and_expr:
3202 res = validate_and_expr(tree);
3203 break;
3204 case shift_expr:
3205 res = validate_shift_expr(tree);
3206 break;
3207 case arith_expr:
3208 res = validate_arith_expr(tree);
3209 break;
3210 case term:
3211 res = validate_term(tree);
3212 break;
3213 case factor:
3214 res = validate_factor(tree);
3215 break;
3216 case power:
3217 res = validate_power(tree);
3218 break;
3219 case atom:
3220 res = validate_atom(tree);
3221 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003222
Fred Drakeff9ea482000-04-19 13:54:15 +00003223 default:
3224 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00003225 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003226 res = 0;
3227 break;
3228 }
3229 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003230 }
3231 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003232}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003233
3234
Guido van Rossum47478871996-08-21 14:32:37 +00003235static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003236validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003237{
3238 int res = validate_eval_input(tree);
3239
3240 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003241 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003242
3243 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003244}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003245
3246
Guido van Rossum3d602e31996-07-21 02:33:56 +00003247/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003248 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003249 */
Guido van Rossum47478871996-08-21 14:32:37 +00003250static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003251validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003252{
Fred Drakec2683dd2001-07-17 19:32:05 +00003253 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003254 int nch = NCH(tree) - 1;
3255 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003256 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003257
Fred Drakec2683dd2001-07-17 19:32:05 +00003258 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003259 if (TYPE(CHILD(tree, j)) == stmt)
3260 res = validate_stmt(CHILD(tree, j));
3261 else
3262 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003263 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003264 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003265 * user. Hopefully, this won't be needed. If a user reports getting
3266 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003267 */
3268 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003269 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003270
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003271 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003272}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003273
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003274static int
3275validate_encoding_decl(node *tree)
3276{
3277 int nch = NCH(tree);
3278 int res = ((nch == 1)
3279 && validate_file_input(CHILD(tree, 0)));
3280
3281 if (!res && !PyErr_Occurred())
3282 err_string("Error Parsing encoding_decl");
3283
3284 return res;
3285}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003286
Fred Drake43f8f9b1998-04-13 16:25:46 +00003287static PyObject*
3288pickle_constructor = NULL;
3289
3290
3291static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003292parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003293{
Fred Drake268397f1998-04-29 14:16:32 +00003294 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003295 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003296 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003297 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003298
Fred Drakec2683dd2001-07-17 19:32:05 +00003299 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003300 PyObject *newargs;
3301 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003302
Fred Drake2a6875e1999-09-20 22:32:18 +00003303 if ((empty_dict = PyDict_New()) == NULL)
3304 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003305 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003306 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003307 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003308 if (tuple != NULL) {
3309 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3310 Py_DECREF(tuple);
3311 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003312 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003313 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003314 }
3315 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003316 Py_XDECREF(empty_dict);
3317
Fred Drake43f8f9b1998-04-13 16:25:46 +00003318 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003319}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003320
3321
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003322/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003323 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003324 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003325 * We'd really have to write a wrapper around it all anyway to allow
3326 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003327 */
3328static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003329 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003330 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003331 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003332 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003333 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003334 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003335 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003336 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003337 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003338 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003339 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003340 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003341 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003342 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003343 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003344 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003345 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003346 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003347
Fred Drake43f8f9b1998-04-13 16:25:46 +00003348 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003349 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003350 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003351
Fred Drake268397f1998-04-29 14:16:32 +00003352 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003353 };
3354
3355
Martin v. Löwis1a214512008-06-11 05:26:20 +00003356
3357static struct PyModuleDef parsermodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 PyModuleDef_HEAD_INIT,
3359 "parser",
3360 NULL,
3361 -1,
3362 parser_functions,
3363 NULL,
3364 NULL,
3365 NULL,
3366 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00003367};
3368
3369PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003370
Mark Hammond62b1ab12002-07-23 06:31:15 +00003371PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003372PyInit_parser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003373{
Fred Drake13130bc2001-08-15 16:44:56 +00003374 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003375
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00003376 if (PyType_Ready(&PyST_Type) < 0)
3377 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00003378 module = PyModule_Create(&parsermodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003379 if (module == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380 return NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003381
Fred Drake7a15ba51999-09-09 14:21:52 +00003382 if (parser_error == 0)
3383 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003384
Tim Peters6a627252003-07-21 14:25:23 +00003385 if (parser_error == 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003386 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003387 /* CAUTION: The code next used to skip bumping the refcount on
Martin v. Löwis1a214512008-06-11 05:26:20 +00003388 * parser_error. That's a disaster if PyInit_parser() gets called more
Tim Peters6a627252003-07-21 14:25:23 +00003389 * than once. By incref'ing, we ensure that each module dict that
3390 * gets created owns its reference to the shared parser_error object,
3391 * and the file static parser_error vrbl owns a reference too.
3392 */
3393 Py_INCREF(parser_error);
3394 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003395 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003396
Fred Drakec2683dd2001-07-17 19:32:05 +00003397 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003398 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003399
Fred Drake13130bc2001-08-15 16:44:56 +00003400 PyModule_AddStringConstant(module, "__copyright__",
3401 parser_copyright_string);
3402 PyModule_AddStringConstant(module, "__doc__",
3403 parser_doc_string);
3404 PyModule_AddStringConstant(module, "__version__",
3405 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003406
Fred Drake78bdb9b2001-07-19 20:17:15 +00003407 /* Register to support pickling.
3408 * If this fails, the import of this module will fail because an
3409 * exception will be raised here; should we clear the exception?
3410 */
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003411 copyreg = PyImport_ImportModuleNoBlock("copyreg");
Fred Drake13130bc2001-08-15 16:44:56 +00003412 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003413 PyObject *func, *pickler;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003414 _Py_IDENTIFIER(pickle);
3415 _Py_IDENTIFIER(sequence2st);
3416 _Py_IDENTIFIER(_pickler);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003417
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02003418 func = _PyObject_GetAttrId(copyreg, &PyId_pickle);
3419 pickle_constructor = _PyObject_GetAttrId(module, &PyId_sequence2st);
3420 pickler = _PyObject_GetAttrId(module, &PyId__pickler);
Fred Drakeff9ea482000-04-19 13:54:15 +00003421 Py_XINCREF(pickle_constructor);
3422 if ((func != NULL) && (pickle_constructor != NULL)
3423 && (pickler != NULL)) {
3424 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003425
Thomas Wouters477c8d52006-05-27 19:21:47 +00003426 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3427 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003428 Py_XDECREF(res);
3429 }
3430 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003431 Py_XDECREF(pickle_constructor);
3432 Py_XDECREF(pickler);
3433 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003434 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00003435 return module;
Fred Drakeff9ea482000-04-19 13:54:15 +00003436}