blob: 2c079add0e61f1df92ee7f654bf9a819ea403311 [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{
Guido van Rossum3d602e31996-07-21 02:33:56 +000086 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000087 Py_INCREF(Py_None);
88 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000089 }
90 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000091 int i;
92 PyObject *v;
93 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000094
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000095 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000096 if (v == NULL)
97 return (v);
Christian Heimes217cfd12007-12-02 14:31:20 +000098 w = PyLong_FromLong(TYPE(n));
Fred Drakeff9ea482000-04-19 13:54:15 +000099 if (w == NULL) {
100 Py_DECREF(v);
101 return ((PyObject*) NULL);
102 }
103 (void) addelem(v, 0, w);
104 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000105 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000106 if (w == NULL) {
107 Py_DECREF(v);
108 return ((PyObject*) NULL);
109 }
110 (void) addelem(v, i+1, w);
111 }
Tim Peters6a627252003-07-21 14:25:23 +0000112
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000113 if (TYPE(n) == encoding_decl)
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000114 (void) addelem(v, i+1, PyUnicode_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000115 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000116 }
117 else if (ISTERMINAL(TYPE(n))) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000118 PyObject *result = mkseq(2 + lineno + col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000119 if (result != NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000120 (void) addelem(result, 0, PyLong_FromLong(TYPE(n)));
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000121 (void) addelem(result, 1, PyUnicode_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000122 if (lineno == 1)
Christian Heimes217cfd12007-12-02 14:31:20 +0000123 (void) addelem(result, 2, PyLong_FromLong(n->n_lineno));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000124 if (col_offset == 1)
Christian Heimes217cfd12007-12-02 14:31:20 +0000125 (void) addelem(result, 3, PyLong_FromLong(n->n_col_offset));
Fred Drakeff9ea482000-04-19 13:54:15 +0000126 }
127 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000128 }
129 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000130 PyErr_SetString(PyExc_SystemError,
131 "unrecognized parse tree node type");
132 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000133 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000134}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000135/*
136 * End of material copyrighted by Stichting Mathematisch Centrum.
137 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000138
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000139
140
141/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000142 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000143 * field of the object type to identify which any given object represents.
144 * These should probably go in an external header to allow other extensions
145 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000146 */
147
Fred Drakec2683dd2001-07-17 19:32:05 +0000148#define PyST_EXPR 1
149#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000150
151
152/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000153 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000154 * naming style, but the code uses the new naming convention.
155 */
156
157static PyObject*
158parser_error = 0;
159
160
Fred Drakec2683dd2001-07-17 19:32:05 +0000161typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000162 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000163 node* st_node; /* the node* returned by the parser */
164 int st_type; /* EXPR or SUITE ? */
Benjamin Petersonf216c942008-10-31 02:28:05 +0000165 PyCompilerFlags st_flags; /* Parser and compiler flags */
Fred Drakec2683dd2001-07-17 19:32:05 +0000166} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000167
168
Jeremy Hylton938ace62002-07-17 16:30:39 +0000169static void parser_free(PyST_Object *st);
Jesus Ceae9c53182012-08-03 14:28:37 +0200170static PyObject* parser_sizeof(PyST_Object *, void *);
Mark Dickinson211c6252009-02-01 10:28:51 +0000171static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op);
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000172static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
173static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
174static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
175static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
176static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
Fred Drake503d8d61998-04-13 18:45:18 +0000177
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000178#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
179
180static PyMethodDef parser_methods[] = {
181 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
182 PyDoc_STR("Compile this ST object into a code object.")},
183 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
184 PyDoc_STR("Determines if this ST object was created from an expression.")},
185 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
186 PyDoc_STR("Determines if this ST object was created from a suite.")},
187 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
188 PyDoc_STR("Creates a list-tree representation of this ST.")},
189 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
190 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Jesus Ceae9c53182012-08-03 14:28:37 +0200191 {"__sizeof__", (PyCFunction)parser_sizeof, METH_NOARGS,
192 PyDoc_STR("Returns size in memory, in bytes.")},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000193 {NULL, NULL, 0, NULL}
194};
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000195
Fred Drake268397f1998-04-29 14:16:32 +0000196static
Fred Drakec2683dd2001-07-17 19:32:05 +0000197PyTypeObject PyST_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000198 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000199 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000200 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000201 0, /* tp_itemsize */
202 (destructor)parser_free, /* tp_dealloc */
203 0, /* tp_print */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000204 0, /* tp_getattr */
Fred Drakeff9ea482000-04-19 13:54:15 +0000205 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000206 0, /* tp_reserved */
Fred Drakeff9ea482000-04-19 13:54:15 +0000207 0, /* tp_repr */
208 0, /* tp_as_number */
209 0, /* tp_as_sequence */
210 0, /* tp_as_mapping */
211 0, /* tp_hash */
212 0, /* tp_call */
213 0, /* tp_str */
214 0, /* tp_getattro */
215 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000216
217 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000218 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000219
Fred Drakeff9ea482000-04-19 13:54:15 +0000220 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000221
222 /* __doc__ */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000223 "Intermediate representation of a Python parse tree.",
224 0, /* tp_traverse */
225 0, /* tp_clear */
Mark Dickinson211c6252009-02-01 10:28:51 +0000226 parser_richcompare, /* tp_richcompare */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000227 0, /* tp_weaklistoffset */
228 0, /* tp_iter */
229 0, /* tp_iternext */
230 parser_methods, /* tp_methods */
Fred Drakec2683dd2001-07-17 19:32:05 +0000231}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000232
233
Mark Dickinson211c6252009-02-01 10:28:51 +0000234/* PyST_Type isn't subclassable, so just check ob_type */
235#define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type)
236
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000237static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000238parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000239{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000240 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000241
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000242 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000243 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000244
245 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000246 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247
248 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000249 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000250
251 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000252 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000253
254 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000255 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000256
257 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000258 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000259
Fred Drakeff9ea482000-04-19 13:54:15 +0000260 if (v != 0)
261 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000262 }
263 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000264}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265
Mark Dickinson211c6252009-02-01 10:28:51 +0000266/* parser_richcompare(PyObject* left, PyObject* right, int op)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000267 *
268 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
269 * This really just wraps a call to parser_compare_nodes() with some easy
270 * checks and protection code.
271 *
272 */
Mark Dickinson211c6252009-02-01 10:28:51 +0000273
274#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
275
276static PyObject *
277parser_richcompare(PyObject *left, PyObject *right, int op)
Guido van Rossum47478871996-08-21 14:32:37 +0000278{
Mark Dickinson211c6252009-02-01 10:28:51 +0000279 int result;
280 PyObject *v;
281
282 /* neither argument should be NULL, unless something's gone wrong */
283 if (left == NULL || right == NULL) {
284 PyErr_BadInternalCall();
285 return NULL;
286 }
287
288 /* both arguments should be instances of PyST_Object */
289 if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) {
290 v = Py_NotImplemented;
291 goto finished;
292 }
293
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000294 if (left == right)
Mark Dickinson211c6252009-02-01 10:28:51 +0000295 /* if arguments are identical, they're equal */
296 result = 0;
297 else
298 result = parser_compare_nodes(((PyST_Object *)left)->st_node,
299 ((PyST_Object *)right)->st_node);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000300
Mark Dickinson211c6252009-02-01 10:28:51 +0000301 /* Convert return value to a Boolean */
302 switch (op) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000303 case Py_EQ:
Mark Dickinson211c6252009-02-01 10:28:51 +0000304 v = TEST_COND(result == 0);
305 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000306 case Py_NE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000307 v = TEST_COND(result != 0);
308 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000309 case Py_LE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000310 v = TEST_COND(result <= 0);
311 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000312 case Py_GE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000313 v = TEST_COND(result >= 0);
314 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000315 case Py_LT:
Mark Dickinson211c6252009-02-01 10:28:51 +0000316 v = TEST_COND(result < 0);
317 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000318 case Py_GT:
Mark Dickinson211c6252009-02-01 10:28:51 +0000319 v = TEST_COND(result > 0);
320 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000321 default:
Mark Dickinson211c6252009-02-01 10:28:51 +0000322 PyErr_BadArgument();
323 return NULL;
324 }
325 finished:
326 Py_INCREF(v);
327 return v;
Fred Drakeff9ea482000-04-19 13:54:15 +0000328}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000329
Fred Drakec2683dd2001-07-17 19:32:05 +0000330/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000331 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000332 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000333 * 'wrapper' object that holds a node* and allows it to be passed around in
334 * Python code.
335 *
336 */
337static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000338parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000339{
Fred Drakec2683dd2001-07-17 19:32:05 +0000340 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000341
342 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000343 o->st_node = st;
344 o->st_type = type;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000345 o->st_flags.cf_flags = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000346 }
Fred Drake268397f1998-04-29 14:16:32 +0000347 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000348 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000349 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000350 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000351}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000352
353
Fred Drakec2683dd2001-07-17 19:32:05 +0000354/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000355 *
356 * This is called by a del statement that reduces the reference count to 0.
357 *
358 */
359static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000360parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000361{
Fred Drakec2683dd2001-07-17 19:32:05 +0000362 PyNode_Free(st->st_node);
363 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000364}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000365
Jesus Ceae9c53182012-08-03 14:28:37 +0200366static PyObject *
367parser_sizeof(PyST_Object *st, void *unused)
368{
369 Py_ssize_t res;
370
371 res = sizeof(PyST_Object) + _PyNode_SizeOf(st->st_node);
372 return PyLong_FromSsize_t(res);
373}
374
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000375
Fred Drakec2683dd2001-07-17 19:32:05 +0000376/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000377 *
378 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000379 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000380 *
381 */
382static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000383parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000384{
Guido van Rossum47478871996-08-21 14:32:37 +0000385 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000386 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000387 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000388 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000389
Georg Brandl30704ea2008-07-23 15:07:12 +0000390 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000391
Martin v. Löwis1a214512008-06-11 05:26:20 +0000392 if (self == NULL || PyModule_Check(self)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000393 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
394 &PyST_Type, &self, &line_option,
395 &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000396 }
Fred Drake503d8d61998-04-13 18:45:18 +0000397 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000398 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
399 &line_option, &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000400 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000401 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000402 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000403 if (line_option != NULL) {
404 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
405 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000406 if (col_option != NULL) {
407 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
408 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000409 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000410 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000411 * since it's known to work already.
412 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000413 res = node2tuple(((PyST_Object*)self)->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000414 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000415 }
416 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000417}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000418
419
Fred Drakec2683dd2001-07-17 19:32:05 +0000420/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000421 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000422 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000423 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000424 *
425 */
426static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000427parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000428{
Guido van Rossum47478871996-08-21 14:32:37 +0000429 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000430 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000431 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000432 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000433
Georg Brandl30704ea2008-07-23 15:07:12 +0000434 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000435
Martin v. Löwis1a214512008-06-11 05:26:20 +0000436 if (self == NULL || PyModule_Check(self))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000437 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
438 &PyST_Type, &self, &line_option,
439 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000440 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000441 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
442 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000443 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000444 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000445 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000446 if (line_option != 0) {
447 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
448 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000449 if (col_option != NULL) {
450 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
451 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000452 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000453 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000454 * since it's known to work already.
455 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000456 res = node2tuple(self->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000457 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000458 }
459 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000460}
Guido van Rossum47478871996-08-21 14:32:37 +0000461
462
Fred Drakec2683dd2001-07-17 19:32:05 +0000463/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000464 *
465 * This function creates code objects from the parse tree represented by
466 * the passed-in data object. An optional file name is passed in as well.
467 *
468 */
469static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000470parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000471{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000472 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000473 PyArena* arena;
474 mod_ty mod;
Fred Drakec2683dd2001-07-17 19:32:05 +0000475 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000476 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000477
Georg Brandl30704ea2008-07-23 15:07:12 +0000478 static char *keywords[] = {"st", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000479
Martin v. Löwis1a214512008-06-11 05:26:20 +0000480 if (self == NULL || PyModule_Check(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000481 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
482 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000483 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000484 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000485 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000486
Benjamin Petersonf216c942008-10-31 02:28:05 +0000487 if (ok) {
488 arena = PyArena_New();
489 if (arena) {
490 mod = PyAST_FromNode(self->st_node, &(self->st_flags), str, arena);
491 if (mod) {
492 res = (PyObject *)PyAST_Compile(mod, str, &(self->st_flags), arena);
493 }
494 PyArena_Free(arena);
495 }
496 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000497
498 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000499}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000500
501
502/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
503 * PyObject* parser_issuite(PyObject* self, PyObject* args)
504 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000505 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000506 * a statement suite, respectively. The return is a Python truth value.
507 *
508 */
509static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000510parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000511{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000512 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000513 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000514
Georg Brandl30704ea2008-07-23 15:07:12 +0000515 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000516
Martin v. Löwis1a214512008-06-11 05:26:20 +0000517 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000518 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000519 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000520 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000521 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000522
523 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000524 /* Check to see if the ST represents an expression or not. */
525 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000526 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000527 }
528 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000529}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000530
531
532static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000533parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000534{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000535 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000536 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000537
Georg Brandl30704ea2008-07-23 15:07:12 +0000538 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000539
Martin v. Löwis1a214512008-06-11 05:26:20 +0000540 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000541 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000542 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000543 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000544 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000545
546 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000547 /* Check to see if the ST represents an expression or not. */
548 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000549 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000550 }
551 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000552}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000553
554
Guido van Rossum3d602e31996-07-21 02:33:56 +0000555/* err_string(char* message)
556 *
557 * Sets the error string for an exception of type ParserError.
558 *
559 */
560static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000561err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000562{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000563 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000564}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000565
566
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000567/* PyObject* parser_do_parse(PyObject* args, int type)
568 *
569 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000570 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000571 *
572 */
573static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000574parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000575{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000576 char* string = 0;
577 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000578 int flags = 0;
579 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000580
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000581 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000582
583 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000584 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
585 &_PyParser_Grammar,
586 (type == PyST_EXPR)
587 ? eval_input : file_input,
588 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 if (n) {
591 res = parser_newstobject(n, type);
Benjamin Petersonf216c942008-10-31 02:28:05 +0000592 if (res)
593 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
594 }
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500595 else {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000596 PyParser_SetError(&err);
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500597 }
Benjamin Petersonf0cdbad2011-06-05 22:14:05 -0500598 PyParser_ClearError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000599 }
600 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000601}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000602
603
604/* PyObject* parser_expr(PyObject* self, PyObject* args)
605 * PyObject* parser_suite(PyObject* self, PyObject* args)
606 *
607 * External interfaces to the parser itself. Which is called determines if
608 * the parser attempts to recognize an expression ('eval' form) or statement
609 * suite ('exec' form). The real work is done by parser_do_parse() above.
610 *
611 */
612static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000613parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000614{
Fred Drake268397f1998-04-29 14:16:32 +0000615 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000616 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000617}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000618
619
620static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000621parser_suite(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:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000625}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000626
627
628
Fred Drakec2683dd2001-07-17 19:32:05 +0000629/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000630 * object requires that the input tuple be valid without having to rely on
631 * catching an exception from the compiler. This is done to allow the
632 * compiler itself to remain fast, since most of its input will come from
633 * the parser directly, and therefore be known to be syntactically correct.
634 * This validation is done to ensure that we don't core dump the compile
635 * phase, returning an exception instead.
636 *
637 * Two aspects can be broken out in this code: creating a node tree from
638 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000639 * advantageous to expand the number of ST types to include funcdefs and
640 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000641 * here. They are not necessary, and not quite as useful in a raw form.
642 * For now, let's get expressions and suites working reliably.
643 */
644
645
Jeremy Hylton938ace62002-07-17 16:30:39 +0000646static node* build_node_tree(PyObject *tuple);
647static int validate_expr_tree(node *tree);
648static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000649static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000650
Fred Drakec2683dd2001-07-17 19:32:05 +0000651/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000652 *
653 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000654 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000655 * tuple can be validated. It does this by checking the first code of the
656 * tuple, and, if acceptable, builds the internal representation. If this
657 * step succeeds, the internal representation is validated as fully as
658 * possible with the various validate_*() routines defined below.
659 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000660 * This function must be changed if support is to be added for PyST_FRAGMENT
661 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000662 *
663 */
664static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000665parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000666{
Fred Drake268397f1998-04-29 14:16:32 +0000667 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000668 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000669 PyObject *tuple;
670 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000671
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000672 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000673
Fred Drakec2683dd2001-07-17 19:32:05 +0000674 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000675 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000676 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000677 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000678 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000679 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000680 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000681 }
682 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000683 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000684 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000685 tree = build_node_tree(tuple);
686 if (tree != 0) {
687 int start_sym = TYPE(tree);
688 if (start_sym == eval_input) {
689 /* Might be an eval form. */
690 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000691 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000692 else
693 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000694 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000695 else if (start_sym == file_input) {
696 /* This looks like an exec form so far. */
697 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000698 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000699 else
700 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000701 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000702 else if (start_sym == encoding_decl) {
703 /* This looks like an encoding_decl so far. */
704 if (validate_encoding_decl(tree))
705 st = parser_newstobject(tree, PyST_SUITE);
706 else
707 PyNode_Free(tree);
708 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000709 else {
710 /* This is a fragment, at best. */
711 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000712 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000713 }
Guido van Rossum47478871996-08-21 14:32:37 +0000714 }
Guido van Rossum47478871996-08-21 14:32:37 +0000715 /* Make sure we throw an exception on all errors. We should never
716 * get this, but we'd do well to be sure something is done.
717 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000718 if (st == NULL && !PyErr_Occurred())
719 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000720
Fred Drakec2683dd2001-07-17 19:32:05 +0000721 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000722}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000723
724
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000725/* node* build_node_children()
726 *
727 * Iterate across the children of the current non-terminal node and build
728 * their structures. If successful, return the root of this portion of
729 * the tree, otherwise, 0. Any required exception will be specified already,
730 * and no memory will have been deallocated.
731 *
732 */
733static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000734build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000735{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000736 Py_ssize_t len = PyObject_Size(tuple);
737 Py_ssize_t i;
738 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000739
740 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000741 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000742 PyObject* elem = PySequence_GetItem(tuple, i);
743 int ok = elem != NULL;
744 long type = 0;
745 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000746
Fred Drakeff9ea482000-04-19 13:54:15 +0000747 if (ok)
748 ok = PySequence_Check(elem);
749 if (ok) {
750 PyObject *temp = PySequence_GetItem(elem, 0);
751 if (temp == NULL)
752 ok = 0;
753 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000754 ok = PyLong_Check(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000755 if (ok)
Christian Heimes217cfd12007-12-02 14:31:20 +0000756 type = PyLong_AS_LONG(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000757 Py_DECREF(temp);
758 }
759 }
760 if (!ok) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000761 PyObject *err = Py_BuildValue("os", elem,
762 "Illegal node construct.");
763 PyErr_SetObject(parser_error, err);
764 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000765 Py_XDECREF(elem);
766 return (0);
767 }
768 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000769 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000770 PyObject *temp;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000771 const char *temp_str;
Guido van Rossum47478871996-08-21 14:32:37 +0000772
Fred Drake0ac9b072000-09-12 21:58:06 +0000773 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000774 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000775 return 0;
776 }
777 temp = PySequence_GetItem(elem, 1);
778 if (temp == NULL)
779 return 0;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000780 if (!PyUnicode_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000781 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000782 "second item in terminal node must be a string,"
783 " found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000784 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000785 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000786 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000787 return 0;
788 }
789 if (len == 3) {
790 PyObject *o = PySequence_GetItem(elem, 2);
791 if (o != NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000792 if (PyLong_Check(o))
793 *line_num = PyLong_AS_LONG(o);
Fred Drake0ac9b072000-09-12 21:58:06 +0000794 else {
795 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000796 "third item in terminal node must be an"
797 " integer, found %s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 Py_TYPE(temp)->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000799 Py_DECREF(o);
800 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000801 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000802 return 0;
803 }
804 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000805 }
806 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000807 temp_str = _PyUnicode_AsStringAndSize(temp, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000808 if (temp_str == NULL) {
809 Py_DECREF(temp);
810 Py_XDECREF(elem);
811 return 0;
812 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000813 strn = (char *)PyObject_MALLOC(len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000814 if (strn != NULL)
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000815 (void) memcpy(strn, temp_str, len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000816 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000817 }
818 else if (!ISNONTERMINAL(type)) {
819 /*
820 * It has to be one or the other; this is an error.
821 * Throw an exception.
822 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000823 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
824 PyErr_SetObject(parser_error, err);
825 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000826 Py_XDECREF(elem);
827 return (0);
828 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000829 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000830 if (err == E_NOMEM) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000831 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000832 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000833 return (node *) PyErr_NoMemory();
834 }
835 if (err == E_OVERFLOW) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000836 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000837 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000838 PyErr_SetString(PyExc_ValueError,
839 "unsupported number of child nodes");
840 return NULL;
841 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000842
Fred Drakeff9ea482000-04-19 13:54:15 +0000843 if (ISNONTERMINAL(type)) {
844 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000845
Fred Drakeff9ea482000-04-19 13:54:15 +0000846 if (new_child != build_node_children(elem, new_child, line_num)) {
847 Py_XDECREF(elem);
848 return (0);
849 }
850 }
851 else if (type == NEWLINE) { /* It's true: we increment the */
852 ++(*line_num); /* line number *after* the newline! */
853 }
854 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000855 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000856 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000857}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000858
859
860static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000861build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000862{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000863 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000864 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000865 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000866
Guido van Rossum47478871996-08-21 14:32:37 +0000867 if (temp != NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000868 num = PyLong_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000869 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000870 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000871 /*
872 * The tuple is simple, but it doesn't start with a start symbol.
873 * Throw an exception now and be done with it.
874 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000875 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000876 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000877 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000878 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000879 }
880 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000881 /*
882 * Not efficient, but that can be handled later.
883 */
884 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000885 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000886
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000887 if (num == encoding_decl) {
888 encoding = PySequence_GetItem(tuple, 2);
889 /* tuple isn't borrowed anymore here, need to DECREF */
890 tuple = PySequence_GetSlice(tuple, 0, 2);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000891 if (tuple == NULL)
892 return NULL;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000893 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000894 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000895 if (res != NULL) {
896 if (res != build_node_children(tuple, res, &line_num)) {
897 PyNode_Free(res);
898 res = NULL;
899 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000900 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000901 Py_ssize_t len;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000902 const char *temp;
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000903 temp = _PyUnicode_AsStringAndSize(encoding, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000904 if (temp == NULL) {
905 Py_DECREF(res);
906 Py_DECREF(encoding);
907 Py_DECREF(tuple);
908 return NULL;
909 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000910 res->n_str = (char *)PyObject_MALLOC(len + 1);
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000911 if (res->n_str != NULL && temp != NULL)
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000912 (void) memcpy(res->n_str, temp, len + 1);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000913 Py_DECREF(encoding);
914 Py_DECREF(tuple);
915 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000916 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000917 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000918 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000919 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000920 * NONTERMINAL, we can't use it. Not sure the implementation
921 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000922 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000923 PyObject *err = Py_BuildValue("os", tuple,
924 "Illegal component tuple.");
925 PyErr_SetObject(parser_error, err);
926 Py_XDECREF(err);
927 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000928
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000929 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000930}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000931
932
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000933/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000934 * Validation routines used within the validation section:
935 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000936static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000937
Fred Drakeff9ea482000-04-19 13:54:15 +0000938#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
939#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
940#define validate_colon(ch) validate_terminal(ch, COLON, ":")
941#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
942#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
943#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
944#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
945#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
946#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
947#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
948#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
949#define validate_star(ch) validate_terminal(ch, STAR, "*")
950#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
951#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
952#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000953#define validate_at(ch) validate_terminal(ch, AT, "@")
Mark Dickinsonea7e9f92012-04-29 18:34:40 +0100954#define validate_rarrow(ch) validate_terminal(ch, RARROW, "->")
Fred Drakeff9ea482000-04-19 13:54:15 +0000955#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000956
Fred Drake0ac9b072000-09-12 21:58:06 +0000957#define VALIDATER(n) static int validate_##n(node *tree)
958
Fred Drakeff9ea482000-04-19 13:54:15 +0000959VALIDATER(node); VALIDATER(small_stmt);
960VALIDATER(class); VALIDATER(node);
961VALIDATER(parameters); VALIDATER(suite);
962VALIDATER(testlist); VALIDATER(varargslist);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000963VALIDATER(vfpdef);
Fred Drakeff9ea482000-04-19 13:54:15 +0000964VALIDATER(stmt); VALIDATER(simple_stmt);
965VALIDATER(expr_stmt); VALIDATER(power);
Guido van Rossum452bf512007-02-09 05:32:43 +0000966VALIDATER(del_stmt);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000967VALIDATER(return_stmt); VALIDATER(raise_stmt);
968VALIDATER(import_stmt); VALIDATER(import_stmt);
969VALIDATER(import_name); VALIDATER(yield_stmt);
Mark Dickinson407b3bd2012-04-29 22:18:31 +0100970VALIDATER(global_stmt); VALIDATER(nonlocal_stmt);
971VALIDATER(assert_stmt);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000972VALIDATER(compound_stmt); VALIDATER(test_or_star_expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000973VALIDATER(while); VALIDATER(for);
974VALIDATER(try); VALIDATER(except_clause);
975VALIDATER(test); VALIDATER(and_test);
976VALIDATER(not_test); VALIDATER(comparison);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000977VALIDATER(comp_op);
Guido van Rossum0368b722007-05-11 16:50:42 +0000978VALIDATER(star_expr); VALIDATER(expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000979VALIDATER(xor_expr); VALIDATER(and_expr);
980VALIDATER(shift_expr); VALIDATER(arith_expr);
981VALIDATER(term); VALIDATER(factor);
982VALIDATER(atom); VALIDATER(lambdef);
983VALIDATER(trailer); VALIDATER(subscript);
984VALIDATER(subscriptlist); VALIDATER(sliceop);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000985VALIDATER(exprlist); VALIDATER(dictorsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000986VALIDATER(arglist); VALIDATER(argument);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000987VALIDATER(comp_for);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000988VALIDATER(comp_iter); VALIDATER(comp_if);
989VALIDATER(testlist_comp); VALIDATER(yield_expr);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000990VALIDATER(or_test);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000991VALIDATER(test_nocond); VALIDATER(lambdef_nocond);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000992VALIDATER(yield_arg);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000993
Fred Drake0ac9b072000-09-12 21:58:06 +0000994#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000995
Fred Drakeff9ea482000-04-19 13:54:15 +0000996#define is_even(n) (((n) & 1) == 0)
997#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000998
999
1000static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001001validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +00001002{
Fred Drake0ac9b072000-09-12 21:58:06 +00001003 if (TYPE(n) != t) {
1004 PyErr_Format(parser_error, "Expected node type %d, got %d.",
1005 t, TYPE(n));
1006 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001007 }
Fred Drake0ac9b072000-09-12 21:58:06 +00001008 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00001009}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001010
1011
Fred Drakee7ab64e2000-04-25 04:14:46 +00001012/* Verifies that the number of child nodes is exactly 'num', raising
1013 * an exception if it isn't. The exception message does not indicate
1014 * the exact number of nodes, allowing this to be used to raise the
1015 * "right" exception when the wrong number of nodes is present in a
1016 * specific variant of a statement's syntax. This is commonly used
1017 * in that fashion.
1018 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001019static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001020validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001021{
Guido van Rossum3d602e31996-07-21 02:33:56 +00001022 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001023 PyErr_Format(parser_error,
1024 "Illegal number of children for %s node.", name);
1025 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001026 }
Fred Drake0ac9b072000-09-12 21:58:06 +00001027 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00001028}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001029
1030
1031static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001032validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +00001033{
Guido van Rossum3d602e31996-07-21 02:33:56 +00001034 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +00001035 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001036
1037 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001038 PyErr_Format(parser_error,
1039 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001040 }
1041 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001042}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001043
1044
Guido van Rossum47478871996-08-21 14:32:37 +00001045/* X (',' X) [',']
1046 */
1047static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +00001048validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +00001049 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001050{
1051 int nch = NCH(tree);
1052 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +00001053 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +00001054
1055 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001056 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +00001057 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001058 if (is_even(nch))
1059 res = validate_comma(CHILD(tree, --nch));
1060 if (res && nch > 1) {
1061 int pos = 1;
1062 for ( ; res && pos < nch; pos += 2)
1063 res = (validate_comma(CHILD(tree, pos))
1064 && vfunc(CHILD(tree, pos + 1)));
1065 }
Guido van Rossum47478871996-08-21 14:32:37 +00001066 }
1067 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001068}
Guido van Rossum47478871996-08-21 14:32:37 +00001069
1070
Fred Drakecff283c2000-08-21 22:24:43 +00001071/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001072 *
1073 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001074 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00001075 */
Guido van Rossum47478871996-08-21 14:32:37 +00001076static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001077validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001078{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001079 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +00001080 int res = (validate_ntype(tree, classdef) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001082
Guido van Rossum3d602e31996-07-21 02:33:56 +00001083 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001084 res = (validate_name(CHILD(tree, 0), "class")
1085 && validate_ntype(CHILD(tree, 1), NAME)
1086 && validate_colon(CHILD(tree, nch - 2))
1087 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001088 }
Brett Cannonf4189912005-04-09 02:30:16 +00001089 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001090 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +00001091 }
Guido van Rossumfc158e22007-11-15 19:17:28 +00001092
Brett Cannonf4189912005-04-09 02:30:16 +00001093 if (res) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 if (nch == 7) {
1095 res = ((validate_lparen(CHILD(tree, 2)) &&
1096 validate_arglist(CHILD(tree, 3)) &&
1097 validate_rparen(CHILD(tree, 4))));
1098 }
1099 else if (nch == 6) {
1100 res = (validate_lparen(CHILD(tree,2)) &&
1101 validate_rparen(CHILD(tree,3)));
1102 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001103 }
1104 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001105}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001106
1107
Guido van Rossum3d602e31996-07-21 02:33:56 +00001108/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001109 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001110 */
Guido van Rossum47478871996-08-21 14:32:37 +00001111static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001112validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001113{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001114 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001115 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001116 && (nch >= 4)
1117 && validate_name(CHILD(tree, 0), "if")
1118 && validate_test(CHILD(tree, 1))
1119 && validate_colon(CHILD(tree, 2))
1120 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001121
1122 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001123 /* ... 'else' ':' suite */
1124 res = (validate_name(CHILD(tree, nch - 3), "else")
1125 && validate_colon(CHILD(tree, nch - 2))
1126 && validate_suite(CHILD(tree, nch - 1)));
1127 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001128 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001129 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001130 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001131 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001132 /* Will catch the case for nch < 4 */
1133 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001134 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001135 /* ... ('elif' test ':' suite)+ ... */
1136 int j = 4;
1137 while ((j < nch) && res) {
1138 res = (validate_name(CHILD(tree, j), "elif")
1139 && validate_colon(CHILD(tree, j + 2))
1140 && validate_test(CHILD(tree, j + 1))
1141 && validate_suite(CHILD(tree, j + 3)));
1142 j += 4;
1143 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001144 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001145 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001146}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001147
1148
Guido van Rossum3d602e31996-07-21 02:33:56 +00001149/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001150 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001151 *
1152 */
Guido van Rossum47478871996-08-21 14:32:37 +00001153static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001154validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001155{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001156 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001157 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001158
Guido van Rossum3d602e31996-07-21 02:33:56 +00001159 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001160 res = (validate_lparen(CHILD(tree, 0))
1161 && validate_rparen(CHILD(tree, nch - 1)));
1162 if (res && (nch == 3))
1163 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001164 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001165 else {
1166 (void) validate_numnodes(tree, 2, "parameters");
1167 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001168 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001169}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001170
1171
Fred Drakecff283c2000-08-21 22:24:43 +00001172/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001173 *
1174 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001175 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001176 * | NEWLINE INDENT stmt+ DEDENT
1177 */
Guido van Rossum47478871996-08-21 14:32:37 +00001178static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001179validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001180{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001181 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001182 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001183
Guido van Rossum3d602e31996-07-21 02:33:56 +00001184 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001185 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001186 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001187 /* NEWLINE INDENT stmt+ DEDENT */
1188 res = (validate_newline(CHILD(tree, 0))
1189 && validate_indent(CHILD(tree, 1))
1190 && validate_stmt(CHILD(tree, 2))
1191 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001192
Fred Drakeff9ea482000-04-19 13:54:15 +00001193 if (res && (nch > 4)) {
1194 int i = 3;
1195 --nch; /* forget the DEDENT */
1196 for ( ; res && (i < nch); ++i)
1197 res = validate_stmt(CHILD(tree, i));
1198 }
1199 else if (nch < 4)
1200 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001201 }
1202 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001203}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001204
1205
Guido van Rossum47478871996-08-21 14:32:37 +00001206static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001207validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001208{
Guido van Rossum47478871996-08-21 14:32:37 +00001209 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001210 validate_test, "testlist"));
1211}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001212
Guido van Rossum1c917072001-10-15 15:44:05 +00001213static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00001214validate_testlist_star_expr(node *tl)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001215{
Benjamin Peterson4905e802009-09-27 02:43:28 +00001216 return (validate_repeating_list(tl, testlist_star_expr, validate_test_or_star_expr,
1217 "testlist"));
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001218}
1219
1220
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001221/* validate either vfpdef or tfpdef.
1222 * vfpdef: NAME
1223 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001224 */
1225static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001226validate_vfpdef(node *tree)
Neal Norwitzc1505362006-12-28 06:47:50 +00001227{
1228 int nch = NCH(tree);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001229 if (TYPE(tree) == vfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001230 return nch == 1 && validate_name(CHILD(tree, 0), NULL);
1231 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001232 else if (TYPE(tree) == tfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001233 if (nch == 1) {
1234 return validate_name(CHILD(tree, 0), NULL);
1235 }
1236 else if (nch == 3) {
1237 return validate_name(CHILD(tree, 0), NULL) &&
1238 validate_colon(CHILD(tree, 1)) &&
1239 validate_test(CHILD(tree, 2));
1240 }
1241 }
1242 return 0;
1243}
1244
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001245/* '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001246 * ..or tfpdef in place of vfpdef. vfpdef: NAME; tfpdef: NAME [':' test]
Fred Drakecff283c2000-08-21 22:24:43 +00001247 */
1248static int
1249validate_varargslist_trailer(node *tree, int start)
1250{
1251 int nch = NCH(tree);
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001252 int res = 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001253
1254 if (nch <= start) {
1255 err_string("expected variable argument trailer for varargslist");
1256 return 0;
1257 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001258 if (TYPE(CHILD(tree, start)) == STAR) {
Fred Drakecff283c2000-08-21 22:24:43 +00001259 /*
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001260 * '*' [vfpdef]
Fred Drakecff283c2000-08-21 22:24:43 +00001261 */
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001262 res = validate_star(CHILD(tree, start++));
1263 if (res && start < nch && (TYPE(CHILD(tree, start)) == vfpdef ||
1264 TYPE(CHILD(tree, start)) == tfpdef))
1265 res = validate_vfpdef(CHILD(tree, start++));
1266 /*
1267 * (',' vfpdef ['=' test])*
1268 */
1269 while (res && start + 1 < nch && (
1270 TYPE(CHILD(tree, start + 1)) == vfpdef ||
1271 TYPE(CHILD(tree, start + 1)) == tfpdef)) {
1272 res = (validate_comma(CHILD(tree, start++))
1273 && validate_vfpdef(CHILD(tree, start++)));
1274 if (res && start + 1 < nch && TYPE(CHILD(tree, start)) == EQUAL)
1275 res = (validate_equal(CHILD(tree, start++))
1276 && validate_test(CHILD(tree, start++)));
1277 }
1278 /*
1279 * [',' '**' vfpdef]
1280 */
1281 if (res && start + 2 < nch && TYPE(CHILD(tree, start+1)) == DOUBLESTAR)
1282 res = (validate_comma(CHILD(tree, start++))
1283 && validate_doublestar(CHILD(tree, start++))
1284 && validate_vfpdef(CHILD(tree, start++)));
1285 }
1286 else if (TYPE(CHILD(tree, start)) == DOUBLESTAR) {
1287 /*
1288 * '**' vfpdef
1289 */
1290 if (start + 1 < nch)
1291 res = (validate_doublestar(CHILD(tree, start++))
1292 && validate_vfpdef(CHILD(tree, start++)));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001293 else {
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001294 res = 0;
1295 err_string("expected vfpdef after ** in varargslist trailer");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001296 }
Fred Drakecff283c2000-08-21 22:24:43 +00001297 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001298 else {
1299 res = 0;
1300 err_string("expected * or ** in varargslist trailer");
Fred Drakecff283c2000-08-21 22:24:43 +00001301 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001302
1303 if (res && start != nch) {
1304 res = 0;
1305 err_string("unexpected extra children in varargslist trailer");
1306 }
Fred Drakecff283c2000-08-21 22:24:43 +00001307 return res;
1308}
1309
1310
Neal Norwitzc1505362006-12-28 06:47:50 +00001311/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001312 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001313 * Validate typedargslist or varargslist.
1314 *
1315 * typedargslist: ((tfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001316 * ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
1317 * '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001318 * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001319 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001320 * varargslist: ((vfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001321 * ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
1322 * '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001323 * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001324 * vfpdef: NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001325 *
1326 */
Guido van Rossum47478871996-08-21 14:32:37 +00001327static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001328validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001329{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001330 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001331 int res = (TYPE(tree) == varargslist ||
1332 TYPE(tree) == typedargslist) &&
1333 (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001334 int sym;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001335 node *ch;
1336 int i = 0;
Guido van Rossumfc158e22007-11-15 19:17:28 +00001337
Fred Drakeb6429a22000-12-11 22:08:27 +00001338 if (!res)
1339 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001340 if (nch < 1) {
1341 err_string("varargslist missing child nodes");
1342 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001343 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001344 while (i < nch) {
Guido van Rossumfc158e22007-11-15 19:17:28 +00001345 ch = CHILD(tree, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001346 sym = TYPE(ch);
1347 if (sym == vfpdef || sym == tfpdef) {
1348 /* validate (vfpdef ['=' test] ',')+ */
1349 res = validate_vfpdef(ch);
Fred Drakecff283c2000-08-21 22:24:43 +00001350 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001351 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1352 res = (validate_equal(CHILD(tree, i))
1353 && validate_test(CHILD(tree, i+1)));
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001354 if (res)
1355 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001356 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001357 if (res && i < nch) {
1358 res = validate_comma(CHILD(tree, i));
1359 ++i;
Fred Drakecff283c2000-08-21 22:24:43 +00001360 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001361 } else if (sym == DOUBLESTAR || sym == STAR) {
1362 res = validate_varargslist_trailer(tree, i);
1363 break;
1364 } else {
1365 res = 0;
1366 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001367 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001368 }
Fred Drakecff283c2000-08-21 22:24:43 +00001369 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001370}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001371
1372
Nick Coghlan650f0d02007-04-15 12:05:43 +00001373/* comp_iter: comp_for | comp_if
Fred Drakecff283c2000-08-21 22:24:43 +00001374 */
1375static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001376validate_comp_iter(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001377{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001378 int res = (validate_ntype(tree, comp_iter)
1379 && validate_numnodes(tree, 1, "comp_iter"));
1380 if (res && TYPE(CHILD(tree, 0)) == comp_for)
1381 res = validate_comp_for(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001382 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001383 res = validate_comp_if(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001384
1385 return res;
1386}
1387
Nick Coghlan650f0d02007-04-15 12:05:43 +00001388/* comp_for: 'for' exprlist 'in' test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001389 */
1390static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001391validate_comp_for(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001392{
1393 int nch = NCH(tree);
1394 int res;
1395
1396 if (nch == 5)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001397 res = validate_comp_iter(CHILD(tree, 4));
Fred Drakecff283c2000-08-21 22:24:43 +00001398 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001399 res = validate_numnodes(tree, 4, "comp_for");
Raymond Hettinger354433a2004-05-19 08:20:33 +00001400
1401 if (res)
1402 res = (validate_name(CHILD(tree, 0), "for")
1403 && validate_exprlist(CHILD(tree, 1))
1404 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001405 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001406
1407 return res;
1408}
1409
Nick Coghlan650f0d02007-04-15 12:05:43 +00001410/* comp_if: 'if' test_nocond [comp_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001411 */
1412static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001413validate_comp_if(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001414{
1415 int nch = NCH(tree);
1416 int res;
1417
1418 if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001419 res = validate_comp_iter(CHILD(tree, 2));
Fred Drakecff283c2000-08-21 22:24:43 +00001420 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001421 res = validate_numnodes(tree, 2, "comp_if");
Fred Drakecff283c2000-08-21 22:24:43 +00001422
1423 if (res)
1424 res = (validate_name(CHILD(tree, 0), "if")
Nick Coghlan650f0d02007-04-15 12:05:43 +00001425 && validate_test_nocond(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001426
1427 return res;
1428}
1429
1430
Guido van Rossum3d602e31996-07-21 02:33:56 +00001431/* simple_stmt | compound_stmt
1432 *
1433 */
Guido van Rossum47478871996-08-21 14:32:37 +00001434static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001435validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001436{
1437 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001438 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001439
Guido van Rossum3d602e31996-07-21 02:33:56 +00001440 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001441 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001442
Fred Drakeff9ea482000-04-19 13:54:15 +00001443 if (TYPE(tree) == simple_stmt)
1444 res = validate_simple_stmt(tree);
1445 else
1446 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001447 }
1448 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001449}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001450
1451
Guido van Rossum3d602e31996-07-21 02:33:56 +00001452/* small_stmt (';' small_stmt)* [';'] NEWLINE
1453 *
1454 */
Guido van Rossum47478871996-08-21 14:32:37 +00001455static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001456validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001457{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001458 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001459 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001460 && (nch >= 2)
1461 && validate_small_stmt(CHILD(tree, 0))
1462 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001463
Guido van Rossum3d602e31996-07-21 02:33:56 +00001464 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001465 res = validate_numnodes(tree, 2, "simple_stmt");
1466 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001467 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001468 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001469 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001470 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001471
Fred Drakeff9ea482000-04-19 13:54:15 +00001472 for (i = 1; res && (i < nch); i += 2)
1473 res = (validate_semi(CHILD(tree, i))
1474 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001475 }
1476 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001477}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001478
1479
Guido van Rossum47478871996-08-21 14:32:37 +00001480static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001481validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001482{
1483 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001484 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001485
Fred Drake0ac9b072000-09-12 21:58:06 +00001486 if (res) {
1487 int ntype = TYPE(CHILD(tree, 0));
1488
1489 if ( (ntype == expr_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001490 || (ntype == del_stmt)
1491 || (ntype == pass_stmt)
1492 || (ntype == flow_stmt)
1493 || (ntype == import_stmt)
1494 || (ntype == global_stmt)
Mark Dickinson407b3bd2012-04-29 22:18:31 +01001495 || (ntype == nonlocal_stmt)
Georg Brandl7cae87c2006-09-06 06:51:57 +00001496 || (ntype == assert_stmt))
Fred Drake0ac9b072000-09-12 21:58:06 +00001497 res = validate_node(CHILD(tree, 0));
1498 else {
1499 res = 0;
1500 err_string("illegal small_stmt child type");
1501 }
1502 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001503 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001504 res = 0;
1505 PyErr_Format(parser_error,
1506 "Unrecognized child node of small_stmt: %d.",
1507 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001508 }
1509 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001510}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001511
1512
1513/* compound_stmt:
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00001514 * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
Guido van Rossum3d602e31996-07-21 02:33:56 +00001515 */
Guido van Rossum47478871996-08-21 14:32:37 +00001516static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001517validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001518{
1519 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001520 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001521 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001522
1523 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001524 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001525
1526 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001527 ntype = TYPE(tree);
1528 if ( (ntype == if_stmt)
1529 || (ntype == while_stmt)
1530 || (ntype == for_stmt)
1531 || (ntype == try_stmt)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00001532 || (ntype == with_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001533 || (ntype == funcdef)
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001534 || (ntype == classdef)
1535 || (ntype == decorated))
Fred Drakeff9ea482000-04-19 13:54:15 +00001536 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001537 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001538 res = 0;
1539 PyErr_Format(parser_error,
1540 "Illegal compound statement type: %d.", TYPE(tree));
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
Guido van Rossum47478871996-08-21 14:32:37 +00001545static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00001546validate_yield_or_testlist(node *tree, int tse)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001547{
Benjamin Peterson4905e802009-09-27 02:43:28 +00001548 if (TYPE(tree) == yield_expr) {
1549 return validate_yield_expr(tree);
1550 }
1551 else {
1552 if (tse)
1553 return validate_testlist_star_expr(tree);
1554 else
1555 return validate_testlist(tree);
1556 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001557}
1558
1559static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001560validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001561{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001562 int j;
1563 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001564 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001565 && is_odd(nch)
Benjamin Peterson4905e802009-09-27 02:43:28 +00001566 && validate_testlist_star_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001567
Fred Drake28f739a2000-08-25 22:42:40 +00001568 if (res && nch == 3
1569 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001570 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
Benjamin Peterson4905e802009-09-27 02:43:28 +00001571 && validate_yield_or_testlist(CHILD(tree, 2), 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001572
Fred Drake28f739a2000-08-25 22:42:40 +00001573 if (res) {
1574 char *s = STR(CHILD(CHILD(tree, 1), 0));
1575
1576 res = (strcmp(s, "+=") == 0
1577 || strcmp(s, "-=") == 0
1578 || strcmp(s, "*=") == 0
1579 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001580 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001581 || strcmp(s, "%=") == 0
1582 || strcmp(s, "&=") == 0
1583 || strcmp(s, "|=") == 0
1584 || strcmp(s, "^=") == 0
1585 || strcmp(s, "<<=") == 0
1586 || strcmp(s, ">>=") == 0
1587 || strcmp(s, "**=") == 0);
1588 if (!res)
Ezio Melotti42da6632011-03-15 05:18:48 +02001589 err_string("illegal augmented assignment operator");
Fred Drake28f739a2000-08-25 22:42:40 +00001590 }
1591 }
1592 else {
1593 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001594 res = validate_equal(CHILD(tree, j))
Benjamin Peterson4905e802009-09-27 02:43:28 +00001595 && validate_yield_or_testlist(CHILD(tree, j + 1), 1);
Fred Drake28f739a2000-08-25 22:42:40 +00001596 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001597 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001598}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001599
1600
Guido van Rossum47478871996-08-21 14:32:37 +00001601static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001602validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001603{
1604 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001605 && validate_name(CHILD(tree, 0), "del")
1606 && validate_exprlist(CHILD(tree, 1)));
1607}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001608
1609
Guido van Rossum47478871996-08-21 14:32:37 +00001610static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001611validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001612{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001613 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001614 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001615 && ((nch == 1) || (nch == 2))
1616 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001617
Guido van Rossum3d602e31996-07-21 02:33:56 +00001618 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001619 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001620
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001621 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001622}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001623
1624
Mark Dickinsoncf360b92012-05-07 12:01:27 +01001625/*
1626 * raise_stmt:
1627 *
1628 * 'raise' [test ['from' test]]
1629 */
Guido van Rossum47478871996-08-21 14:32:37 +00001630static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001631validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001632{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001633 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001634 int res = (validate_ntype(tree, raise_stmt)
Mark Dickinsoncf360b92012-05-07 12:01:27 +01001635 && ((nch == 1) || (nch == 2) || (nch == 4)));
1636
1637 if (!res && !PyErr_Occurred())
1638 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001639
Guido van Rossum3d602e31996-07-21 02:33:56 +00001640 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001641 res = validate_name(CHILD(tree, 0), "raise");
1642 if (res && (nch >= 2))
1643 res = validate_test(CHILD(tree, 1));
Mark Dickinsoncf360b92012-05-07 12:01:27 +01001644 if (res && (nch == 4)) {
1645 res = (validate_name(CHILD(tree, 2), "from")
Fred Drakeff9ea482000-04-19 13:54:15 +00001646 && validate_test(CHILD(tree, 3)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001647 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001648 }
1649 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001650}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001651
1652
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001653/* yield_expr: 'yield' [yield_arg]
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001654 */
1655static int
1656validate_yield_expr(node *tree)
1657{
1658 int nch = NCH(tree);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001659 if (nch < 1 || nch > 2)
1660 return 0;
1661 if (!validate_ntype(tree, yield_expr))
1662 return 0;
1663 if (!validate_name(CHILD(tree, 0), "yield"))
1664 return 0;
1665 if (nch == 2) {
1666 if (!validate_yield_arg(CHILD(tree, 1)))
1667 return 0;
1668 }
1669 return 1;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001670}
1671
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001672/* yield_arg: 'from' test | testlist
1673 */
1674static int
1675validate_yield_arg(node *tree)
1676{
1677 int nch = NCH(tree);
1678 if (!validate_ntype(tree, yield_arg))
1679 return 0;
1680 switch (nch) {
1681 case 1:
1682 if (!validate_testlist(CHILD(tree, nch - 1)))
1683 return 0;
1684 break;
1685 case 2:
1686 if (!validate_name(CHILD(tree, 0), "from"))
1687 return 0;
1688 if (!validate_test(CHILD(tree, 1)))
1689 return 0;
1690 break;
1691 default:
1692 return 0;
1693 }
1694 return 1;
1695}
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001696
1697/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001698 */
1699static int
1700validate_yield_stmt(node *tree)
1701{
1702 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001703 && validate_numnodes(tree, 1, "yield_stmt")
1704 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001705}
1706
1707
Fred Drakecff283c2000-08-21 22:24:43 +00001708static int
1709validate_import_as_name(node *tree)
1710{
1711 int nch = NCH(tree);
1712 int ok = validate_ntype(tree, import_as_name);
1713
1714 if (ok) {
1715 if (nch == 1)
1716 ok = validate_name(CHILD(tree, 0), NULL);
1717 else if (nch == 3)
1718 ok = (validate_name(CHILD(tree, 0), NULL)
1719 && validate_name(CHILD(tree, 1), "as")
1720 && validate_name(CHILD(tree, 2), NULL));
1721 else
1722 ok = validate_numnodes(tree, 3, "import_as_name");
1723 }
1724 return ok;
1725}
1726
1727
Fred Drake71137082001-01-07 05:59:59 +00001728/* dotted_name: NAME ("." NAME)*
1729 */
1730static int
1731validate_dotted_name(node *tree)
1732{
1733 int nch = NCH(tree);
1734 int res = (validate_ntype(tree, dotted_name)
1735 && is_odd(nch)
1736 && validate_name(CHILD(tree, 0), NULL));
1737 int i;
1738
1739 for (i = 1; res && (i < nch); i += 2) {
1740 res = (validate_dot(CHILD(tree, i))
1741 && validate_name(CHILD(tree, i+1), NULL));
1742 }
1743 return res;
1744}
1745
1746
Fred Drakecff283c2000-08-21 22:24:43 +00001747/* dotted_as_name: dotted_name [NAME NAME]
1748 */
1749static int
1750validate_dotted_as_name(node *tree)
1751{
1752 int nch = NCH(tree);
1753 int res = validate_ntype(tree, dotted_as_name);
1754
1755 if (res) {
1756 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001757 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001758 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001759 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001760 && validate_name(CHILD(tree, 1), "as")
1761 && validate_name(CHILD(tree, 2), NULL));
1762 else {
1763 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001764 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001765 }
1766 }
1767 return res;
1768}
1769
1770
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001771/* dotted_as_name (',' dotted_as_name)* */
1772static int
1773validate_dotted_as_names(node *tree)
1774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 int nch = NCH(tree);
1776 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1777 int i;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 for (i = 1; res && (i < nch); i += 2)
1780 res = (validate_comma(CHILD(tree, i))
1781 && validate_dotted_as_name(CHILD(tree, i + 1)));
1782 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001783}
1784
1785
1786/* import_as_name (',' import_as_name)* [','] */
1787static int
1788validate_import_as_names(node *tree)
1789{
1790 int nch = NCH(tree);
1791 int res = validate_import_as_name(CHILD(tree, 0));
1792 int i;
1793
1794 for (i = 1; res && (i + 1 < nch); i += 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 res = (validate_comma(CHILD(tree, i))
1796 && validate_import_as_name(CHILD(tree, i + 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001797 return (res);
1798}
1799
1800
1801/* 'import' dotted_as_names */
1802static int
1803validate_import_name(node *tree)
1804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 return (validate_ntype(tree, import_name)
1806 && validate_numnodes(tree, 2, "import_name")
1807 && validate_name(CHILD(tree, 0), "import")
1808 && validate_dotted_as_names(CHILD(tree, 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001809}
1810
Mark Dickinsonfeb3b752010-07-04 18:38:57 +00001811/* Helper function to count the number of leading dots (or ellipsis tokens) in
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001812 * 'from ...module import name'
1813 */
1814static int
1815count_from_dots(node *tree)
1816{
Mark Dickinsonfeb3b752010-07-04 18:38:57 +00001817 int i;
1818 for (i = 1; i < NCH(tree); i++)
1819 if (TYPE(CHILD(tree, i)) != DOT && TYPE(CHILD(tree, i)) != ELLIPSIS)
1820 break;
1821 return i - 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001822}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001823
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +00001824/* import_from: ('from' ('.'* dotted_name | '.'+)
1825 * 'import' ('*' | '(' import_as_names ')' | import_as_names))
Guido van Rossum3d602e31996-07-21 02:33:56 +00001826 */
Guido van Rossum47478871996-08-21 14:32:37 +00001827static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001828validate_import_from(node *tree)
1829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 int nch = NCH(tree);
1831 int ndots = count_from_dots(tree);
1832 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1833 int offset = ndots + havename;
1834 int res = validate_ntype(tree, import_from)
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +00001835 && (offset >= 1)
1836 && (nch >= 3 + offset)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 && validate_name(CHILD(tree, 0), "from")
1838 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1839 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1842 res = ((nch == offset + 5)
1843 && validate_lparen(CHILD(tree, offset + 2))
1844 && validate_import_as_names(CHILD(tree, offset + 3))
1845 && validate_rparen(CHILD(tree, offset + 4)));
1846 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1847 res = validate_import_as_names(CHILD(tree, offset + 2));
1848 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001849}
1850
1851
1852/* import_stmt: import_name | import_from */
1853static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001854validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001855{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001856 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001857 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001858
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001859 if (res) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 if (ntype == import_name || ntype == import_from)
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001863 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001864 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001865 res = 0;
1866 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001867 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001868 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001869 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001870 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001871 PyErr_Format(parser_error,
1872 "Unrecognized child node of import_stmt: %d.",
1873 TYPE(CHILD(tree, 0)));
1874 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001875 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001876}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001877
1878
Mark Dickinson407b3bd2012-04-29 22:18:31 +01001879/* global_stmt:
1880 *
1881 * 'global' NAME (',' NAME)*
1882 */
Guido van Rossum47478871996-08-21 14:32:37 +00001883static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001884validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001885{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001886 int j;
1887 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001888 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001889 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001890
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001891 if (!res && !PyErr_Occurred())
1892 err_string("illegal global statement");
1893
Guido van Rossum3d602e31996-07-21 02:33:56 +00001894 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001895 res = (validate_name(CHILD(tree, 0), "global")
1896 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001897 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001898 res = (validate_comma(CHILD(tree, j))
1899 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001900
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001901 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001902}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001903
Mark Dickinson407b3bd2012-04-29 22:18:31 +01001904/* nonlocal_stmt:
1905 *
1906 * 'nonlocal' NAME (',' NAME)*
1907 */
1908static int
1909validate_nonlocal_stmt(node *tree)
1910{
1911 int j;
1912 int nch = NCH(tree);
1913 int res = (validate_ntype(tree, nonlocal_stmt)
1914 && is_even(nch) && (nch >= 2));
1915
1916 if (!res && !PyErr_Occurred())
1917 err_string("illegal nonlocal statement");
1918
1919 if (res)
1920 res = (validate_name(CHILD(tree, 0), "nonlocal")
1921 && validate_ntype(CHILD(tree, 1), NAME));
1922 for (j = 2; res && (j < nch); j += 2)
1923 res = (validate_comma(CHILD(tree, j))
1924 && validate_ntype(CHILD(tree, j + 1), NAME));
1925
1926 return res;
1927}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001928
Guido van Rossum925e5471997-04-02 05:32:13 +00001929/* assert_stmt:
1930 *
1931 * 'assert' test [',' test]
1932 */
1933static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001934validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001935{
1936 int nch = NCH(tree);
1937 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001938 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001939 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001940 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001941
1942 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001943 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001944 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001945 res = (validate_comma(CHILD(tree, 2))
1946 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001947
1948 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001949}
Guido van Rossum925e5471997-04-02 05:32:13 +00001950
1951
Guido van Rossum47478871996-08-21 14:32:37 +00001952static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001953validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001954{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001955 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001956 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001957 && ((nch == 4) || (nch == 7))
1958 && validate_name(CHILD(tree, 0), "while")
1959 && validate_test(CHILD(tree, 1))
1960 && validate_colon(CHILD(tree, 2))
1961 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001962
Guido van Rossum3d602e31996-07-21 02:33:56 +00001963 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001964 res = (validate_name(CHILD(tree, 4), "else")
1965 && validate_colon(CHILD(tree, 5))
1966 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001967
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001968 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001969}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001970
1971
Guido van Rossum47478871996-08-21 14:32:37 +00001972static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001973validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001974{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001975 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001976 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001977 && ((nch == 6) || (nch == 9))
1978 && validate_name(CHILD(tree, 0), "for")
1979 && validate_exprlist(CHILD(tree, 1))
1980 && validate_name(CHILD(tree, 2), "in")
1981 && validate_testlist(CHILD(tree, 3))
1982 && validate_colon(CHILD(tree, 4))
1983 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001984
Guido van Rossum3d602e31996-07-21 02:33:56 +00001985 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001986 res = (validate_name(CHILD(tree, 6), "else")
1987 && validate_colon(CHILD(tree, 7))
1988 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001989
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001990 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001991}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001992
1993
Guido van Rossum3d602e31996-07-21 02:33:56 +00001994/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001995 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Georg Brandleee31162008-12-07 15:15:22 +00001996 ['finally' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001997 * | 'try' ':' suite 'finally' ':' suite
1998 *
1999 */
Guido van Rossum47478871996-08-21 14:32:37 +00002000static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002001validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002002{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002003 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002004 int pos = 3;
2005 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002006 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002007
2008 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002009 res = (validate_name(CHILD(tree, 0), "try")
2010 && validate_colon(CHILD(tree, 1))
2011 && validate_suite(CHILD(tree, 2))
2012 && validate_colon(CHILD(tree, nch - 2))
2013 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00002014 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00002015 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00002016 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
2017 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00002018
2019 PyErr_Format(parser_error,
2020 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002021 }
Georg Brandleee31162008-12-07 15:15:22 +00002022 /* Handle try/finally statement */
2023 if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
2024 (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
2025 res = (validate_numnodes(tree, 6, "try/finally")
2026 && validate_colon(CHILD(tree, 4))
2027 && validate_suite(CHILD(tree, 5)));
2028 return (res);
2029 }
2030 /* try/except statement: skip past except_clause sections */
Antoine Pitrou37d1c182009-05-14 21:54:00 +00002031 while (res && pos < nch && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002032 res = (validate_except_clause(CHILD(tree, pos))
2033 && validate_colon(CHILD(tree, pos + 1))
2034 && validate_suite(CHILD(tree, pos + 2)));
2035 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002036 }
Georg Brandleee31162008-12-07 15:15:22 +00002037 /* skip else clause */
Antoine Pitrou37d1c182009-05-14 21:54:00 +00002038 if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) &&
Georg Brandleee31162008-12-07 15:15:22 +00002039 (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
2040 res = (validate_colon(CHILD(tree, pos + 1))
2041 && validate_suite(CHILD(tree, pos + 2)));
2042 pos += 3;
2043 }
2044 if (res && pos < nch) {
2045 /* last clause must be a finally */
2046 res = (validate_name(CHILD(tree, pos), "finally")
2047 && validate_numnodes(tree, pos + 3, "try/except/finally")
2048 && validate_colon(CHILD(tree, pos + 1))
2049 && validate_suite(CHILD(tree, pos + 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002050 }
2051 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002052}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002053
2054
Guido van Rossum47478871996-08-21 14:32:37 +00002055static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002056validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002057{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002058 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002059 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00002060 && ((nch == 1) || (nch == 2) || (nch == 4))
2061 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002062
Guido van Rossum3d602e31996-07-21 02:33:56 +00002063 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002064 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002065 if (res && (nch == 4))
Guido van Rossumb940e112007-01-10 16:19:56 +00002066 res = (validate_name(CHILD(tree, 2), "as")
2067 && validate_ntype(CHILD(tree, 3), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002068
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002069 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002070}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002071
2072
Guido van Rossum47478871996-08-21 14:32:37 +00002073static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002074validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002075{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002076 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002077 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002078
Guido van Rossum3d602e31996-07-21 02:33:56 +00002079 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002080 res = ((nch == 1)
2081 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002082 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002083 res = validate_or_test(CHILD(tree, 0));
2084 res = (res && (nch == 1 || (nch == 5 &&
2085 validate_name(CHILD(tree, 1), "if") &&
2086 validate_or_test(CHILD(tree, 2)) &&
2087 validate_name(CHILD(tree, 3), "else") &&
2088 validate_test(CHILD(tree, 4)))));
2089 }
2090 return (res);
2091}
2092
2093static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002094validate_test_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002095{
2096 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002097 int res = validate_ntype(tree, test_nocond) && (nch == 1);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002098
Nick Coghlan650f0d02007-04-15 12:05:43 +00002099 if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
2100 res = (validate_lambdef_nocond(CHILD(tree, 0)));
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002101 else if (res) {
2102 res = (validate_or_test(CHILD(tree, 0)));
2103 }
2104 return (res);
2105}
2106
2107static int
2108validate_or_test(node *tree)
2109{
2110 int nch = NCH(tree);
2111 int res = validate_ntype(tree, or_test) && is_odd(nch);
2112
2113 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002114 int pos;
2115 res = validate_and_test(CHILD(tree, 0));
2116 for (pos = 1; res && (pos < nch); pos += 2)
2117 res = (validate_name(CHILD(tree, pos), "or")
2118 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002119 }
2120 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002121}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002122
2123
Guido van Rossum47478871996-08-21 14:32:37 +00002124static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002125validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002126{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002127 int pos;
2128 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002129 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002130 && is_odd(nch)
2131 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002132
Guido van Rossum3d602e31996-07-21 02:33:56 +00002133 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002134 res = (validate_name(CHILD(tree, pos), "and")
2135 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002136
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002137 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002138}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002139
2140
Guido van Rossum47478871996-08-21 14:32:37 +00002141static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002142validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002143{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002144 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002145 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002146
Guido van Rossum3d602e31996-07-21 02:33:56 +00002147 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002148 if (nch == 2)
2149 res = (validate_name(CHILD(tree, 0), "not")
2150 && validate_not_test(CHILD(tree, 1)));
2151 else if (nch == 1)
2152 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002153 }
2154 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002155}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002156
2157
Guido van Rossum47478871996-08-21 14:32:37 +00002158static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002159validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002160{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002161 int pos;
2162 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002163 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002164 && is_odd(nch)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002165 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002166
Guido van Rossum3d602e31996-07-21 02:33:56 +00002167 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002168 res = (validate_comp_op(CHILD(tree, pos))
Benjamin Peterson4905e802009-09-27 02:43:28 +00002169 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002170
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002171 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002172}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002173
2174
Guido van Rossum47478871996-08-21 14:32:37 +00002175static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002176validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002177{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002178 int res = 0;
2179 int nch = NCH(tree);
2180
Guido van Rossum3d602e31996-07-21 02:33:56 +00002181 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002182 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002183 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002184 /*
2185 * Only child will be a terminal with a well-defined symbolic name
2186 * or a NAME with a string of either 'is' or 'in'
2187 */
2188 tree = CHILD(tree, 0);
2189 switch (TYPE(tree)) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002190 case LESS:
2191 case GREATER:
2192 case EQEQUAL:
2193 case EQUAL:
2194 case LESSEQUAL:
2195 case GREATEREQUAL:
2196 case NOTEQUAL:
Fred Drakeff9ea482000-04-19 13:54:15 +00002197 res = 1;
2198 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002199 case NAME:
Fred Drakeff9ea482000-04-19 13:54:15 +00002200 res = ((strcmp(STR(tree), "in") == 0)
2201 || (strcmp(STR(tree), "is") == 0));
2202 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002203 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002204 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002205 }
2206 break;
2207 default:
Fred Drake661ea262000-10-24 19:57:45 +00002208 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002209 break;
2210 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002211 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002212 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002213 res = (validate_ntype(CHILD(tree, 0), NAME)
2214 && validate_ntype(CHILD(tree, 1), NAME)
2215 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2216 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2217 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2218 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2219 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002220 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002221 }
2222 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002223}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002224
2225
Guido van Rossum47478871996-08-21 14:32:37 +00002226static int
Guido van Rossum0368b722007-05-11 16:50:42 +00002227validate_star_expr(node *tree)
2228{
2229 int res = validate_ntype(tree, star_expr);
2230 if (!res) return res;
Benjamin Peterson4905e802009-09-27 02:43:28 +00002231 if (!validate_numnodes(tree, 2, "star_expr"))
2232 return 0;
2233 return validate_ntype(CHILD(tree, 0), STAR) && \
2234 validate_expr(CHILD(tree, 1));
Guido van Rossum0368b722007-05-11 16:50:42 +00002235}
2236
2237
2238static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002239validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002240{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002241 int j;
2242 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002243 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002244 && is_odd(nch)
2245 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002246
Guido van Rossum3d602e31996-07-21 02:33:56 +00002247 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002248 res = (validate_xor_expr(CHILD(tree, j))
2249 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002250
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002251 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002252}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002253
2254
Guido van Rossum47478871996-08-21 14:32:37 +00002255static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002256validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002257{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002258 int j;
2259 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002260 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002261 && is_odd(nch)
2262 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002263
Guido van Rossum3d602e31996-07-21 02:33:56 +00002264 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002265 res = (validate_circumflex(CHILD(tree, j - 1))
2266 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002267
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002268 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002269}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002270
2271
Guido van Rossum47478871996-08-21 14:32:37 +00002272static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002273validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002274{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002275 int pos;
2276 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002277 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002278 && is_odd(nch)
2279 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002280
Guido van Rossum3d602e31996-07-21 02:33:56 +00002281 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002282 res = (validate_ampersand(CHILD(tree, pos))
2283 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002284
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002285 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002286}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002287
2288
2289static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002290validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002291 {
2292 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002293 int nch = NCH(tree);
2294 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002295 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002296
Guido van Rossum3d602e31996-07-21 02:33:56 +00002297 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002298 if (TYPE(CHILD(tree, pos)) != op1)
2299 res = validate_ntype(CHILD(tree, pos), op2);
2300 if (res)
2301 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002302 }
2303 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002304}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002305
2306
Guido van Rossum47478871996-08-21 14:32:37 +00002307static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002308validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002309{
2310 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002311 && validate_chain_two_ops(tree, validate_arith_expr,
2312 LEFTSHIFT, RIGHTSHIFT));
2313}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002314
2315
Guido van Rossum47478871996-08-21 14:32:37 +00002316static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002317validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002318{
2319 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002320 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2321}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002322
2323
Guido van Rossum47478871996-08-21 14:32:37 +00002324static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002325validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002326{
2327 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002328 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002329 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002330 && is_odd(nch)
2331 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002332
Guido van Rossum3d602e31996-07-21 02:33:56 +00002333 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002334 res = (((TYPE(CHILD(tree, pos)) == STAR)
2335 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002336 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002337 || (TYPE(CHILD(tree, pos)) == PERCENT))
2338 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002339
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002340 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002341}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002342
2343
Guido van Rossum3d602e31996-07-21 02:33:56 +00002344/* factor:
2345 *
2346 * factor: ('+'|'-'|'~') factor | power
2347 */
Guido van Rossum47478871996-08-21 14:32:37 +00002348static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002349validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002350{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002351 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002352 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002353 && (((nch == 2)
2354 && ((TYPE(CHILD(tree, 0)) == PLUS)
2355 || (TYPE(CHILD(tree, 0)) == MINUS)
2356 || (TYPE(CHILD(tree, 0)) == TILDE))
2357 && validate_factor(CHILD(tree, 1)))
2358 || ((nch == 1)
2359 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002360 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002361}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002362
2363
Guido van Rossum3d602e31996-07-21 02:33:56 +00002364/* power:
2365 *
2366 * power: atom trailer* ('**' factor)*
2367 */
Guido van Rossum47478871996-08-21 14:32:37 +00002368static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002369validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002370{
2371 int pos = 1;
2372 int nch = NCH(tree);
2373 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002374 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002375
2376 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002377 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002378 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002379 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002380 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002381 return (0);
2382 }
2383 for ( ; res && (pos < (nch - 1)); pos += 2)
2384 res = (validate_doublestar(CHILD(tree, pos))
2385 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002386 }
2387 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002388}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002389
2390
Guido van Rossum47478871996-08-21 14:32:37 +00002391static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002392validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002393{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002394 int pos;
2395 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002396 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002397
Fred Drakecff283c2000-08-21 22:24:43 +00002398 if (res && nch < 1)
2399 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002400 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002401 switch (TYPE(CHILD(tree, 0))) {
2402 case LPAR:
2403 res = ((nch <= 3)
2404 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002405
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002406 if (res && (nch == 3)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 if (TYPE(CHILD(tree, 1))==yield_expr)
2408 res = validate_yield_expr(CHILD(tree, 1));
2409 else
2410 res = validate_testlist_comp(CHILD(tree, 1));
2411 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002412 break;
2413 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002414 if (nch == 2)
2415 res = validate_ntype(CHILD(tree, 1), RSQB);
2416 else if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002417 res = (validate_testlist_comp(CHILD(tree, 1))
Fred Drakecff283c2000-08-21 22:24:43 +00002418 && validate_ntype(CHILD(tree, 2), RSQB));
2419 else {
2420 res = 0;
2421 err_string("illegal list display atom");
2422 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002423 break;
2424 case LBRACE:
2425 res = ((nch <= 3)
2426 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002427
Fred Drakeff9ea482000-04-19 13:54:15 +00002428 if (res && (nch == 3))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002429 res = validate_dictorsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002430 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002431 case NAME:
2432 case NUMBER:
Mark Dickinsonda029fb2012-05-07 17:24:04 +01002433 case ELLIPSIS:
Fred Drakeff9ea482000-04-19 13:54:15 +00002434 res = (nch == 1);
2435 break;
2436 case STRING:
2437 for (pos = 1; res && (pos < nch); ++pos)
2438 res = validate_ntype(CHILD(tree, pos), STRING);
2439 break;
2440 default:
2441 res = 0;
2442 break;
2443 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002444 }
2445 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002446}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002447
2448
Nick Coghlan650f0d02007-04-15 12:05:43 +00002449/* testlist_comp:
2450 * test ( comp_for | (',' test)* [','] )
Fred Drake85bf3bb2000-08-23 15:35:26 +00002451 */
Fred Drakecff283c2000-08-21 22:24:43 +00002452static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002453validate_testlist_comp(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00002454{
2455 int nch = NCH(tree);
2456 int ok = nch;
2457
2458 if (nch == 0)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002459 err_string("missing child nodes of testlist_comp");
2460 else {
Benjamin Peterson4905e802009-09-27 02:43:28 +00002461 ok = validate_test_or_star_expr(CHILD(tree, 0));
Nick Coghlan650f0d02007-04-15 12:05:43 +00002462 }
Fred Drakecff283c2000-08-21 22:24:43 +00002463
2464 /*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002465 * comp_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002466 */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002467 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2468 ok = validate_comp_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002469 else {
2470 /* (',' test)* [','] */
2471 int i = 1;
2472 while (ok && nch - i >= 2) {
2473 ok = (validate_comma(CHILD(tree, i))
Benjamin Peterson4905e802009-09-27 02:43:28 +00002474 && validate_test_or_star_expr(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002475 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002476 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002477 if (ok && i == nch-1)
2478 ok = validate_comma(CHILD(tree, i));
2479 else if (i != nch) {
2480 ok = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002481 err_string("illegal trailing nodes for testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002482 }
2483 }
2484 return ok;
2485}
Fred Drakecff283c2000-08-21 22:24:43 +00002486
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002487/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002488 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002489 */
2490static int
2491validate_decorator(node *tree)
2492{
2493 int ok;
2494 int nch = NCH(tree);
2495 ok = (validate_ntype(tree, decorator) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 (nch == 3 || nch == 5 || nch == 6) &&
2497 validate_at(CHILD(tree, 0)) &&
2498 validate_dotted_name(CHILD(tree, 1)) &&
2499 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002500
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002501 if (ok && nch != 3) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 ok = (validate_lparen(CHILD(tree, 2)) &&
2503 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 if (ok && nch == 6)
2506 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002507 }
2508
2509 return ok;
2510}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002511
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002512/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002513 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002514 */
2515static int
2516validate_decorators(node *tree)
2517{
Guido van Rossumfc158e22007-11-15 19:17:28 +00002518 int i, nch, ok;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002519 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002520 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002521
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002522 for (i = 0; ok && i < nch; ++i)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002524
2525 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002526}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002527
Georg Brandl0c315622009-05-25 21:10:36 +00002528/* with_item:
2529 * test ['as' expr]
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002530 */
2531static int
Georg Brandl0c315622009-05-25 21:10:36 +00002532validate_with_item(node *tree)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002533{
2534 int nch = NCH(tree);
Georg Brandl0c315622009-05-25 21:10:36 +00002535 int ok = (validate_ntype(tree, with_item)
2536 && (nch == 1 || nch == 3)
2537 && validate_test(CHILD(tree, 0)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 if (ok && nch == 3)
Georg Brandl0c315622009-05-25 21:10:36 +00002539 ok = (validate_name(CHILD(tree, 1), "as")
2540 && validate_expr(CHILD(tree, 2)));
2541 return ok;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002542}
2543
Georg Brandl0c315622009-05-25 21:10:36 +00002544/* with_stmt:
2545 * 0 1 ... -2 -1
2546 * 'with' with_item (',' with_item)* ':' suite
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002547 */
2548static int
2549validate_with_stmt(node *tree)
2550{
Georg Brandl0c315622009-05-25 21:10:36 +00002551 int i;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002552 int nch = NCH(tree);
2553 int ok = (validate_ntype(tree, with_stmt)
Georg Brandl0c315622009-05-25 21:10:36 +00002554 && (nch % 2 == 0)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002555 && validate_name(CHILD(tree, 0), "with")
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002556 && validate_colon(RCHILD(tree, -2))
2557 && validate_suite(RCHILD(tree, -1)));
Georg Brandl0c315622009-05-25 21:10:36 +00002558 for (i = 1; ok && i < nch - 2; i += 2)
2559 ok = validate_with_item(CHILD(tree, i));
2560 return ok;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002561}
2562
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01002563/* funcdef: 'def' NAME parameters ['->' test] ':' suite */
2564
Guido van Rossum47478871996-08-21 14:32:37 +00002565static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002566validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002567{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002568 int nch = NCH(tree);
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01002569 int res = validate_ntype(tree, funcdef);
2570 if (res) {
2571 if (nch == 5) {
2572 res = (validate_name(CHILD(tree, 0), "def")
2573 && validate_ntype(CHILD(tree, 1), NAME)
2574 && validate_parameters(CHILD(tree, 2))
2575 && validate_colon(CHILD(tree, 3))
2576 && validate_suite(CHILD(tree, 4)));
2577 }
2578 else if (nch == 7) {
2579 res = (validate_name(CHILD(tree, 0), "def")
2580 && validate_ntype(CHILD(tree, 1), NAME)
2581 && validate_parameters(CHILD(tree, 2))
2582 && validate_rarrow(CHILD(tree, 3))
2583 && validate_test(CHILD(tree, 4))
2584 && validate_colon(CHILD(tree, 5))
2585 && validate_suite(CHILD(tree, 6)));
2586 }
2587 else {
2588 res = 0;
2589 err_string("illegal number of children for funcdef");
2590 }
2591 }
2592 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00002593}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002594
2595
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002596/* decorated
2597 * decorators (classdef | funcdef)
2598 */
2599static int
2600validate_decorated(node *tree)
2601{
Mark Dickinson2bd61a92010-07-04 16:37:31 +00002602 int nch = NCH(tree);
2603 int ok = (validate_ntype(tree, decorated)
2604 && (nch == 2)
2605 && validate_decorators(RCHILD(tree, -2)));
2606 if (TYPE(RCHILD(tree, -1)) == funcdef)
2607 ok = ok && validate_funcdef(RCHILD(tree, -1));
2608 else
2609 ok = ok && validate_class(RCHILD(tree, -1));
2610 return ok;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002611}
2612
Guido van Rossum47478871996-08-21 14:32:37 +00002613static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002614validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002615{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002616 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002617 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002618 && ((nch == 3) || (nch == 4))
2619 && validate_name(CHILD(tree, 0), "lambda")
2620 && validate_colon(CHILD(tree, nch - 2))
2621 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002622
Guido van Rossum3d602e31996-07-21 02:33:56 +00002623 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002624 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002625 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002626 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002627
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002628 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002629}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002630
2631
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002632static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002633validate_lambdef_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002634{
2635 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002636 int res = (validate_ntype(tree, lambdef_nocond)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002637 && ((nch == 3) || (nch == 4))
2638 && validate_name(CHILD(tree, 0), "lambda")
2639 && validate_colon(CHILD(tree, nch - 2))
2640 && validate_test(CHILD(tree, nch - 1)));
2641
2642 if (res && (nch == 4))
2643 res = validate_varargslist(CHILD(tree, 1));
2644 else if (!res && !PyErr_Occurred())
Nick Coghlan650f0d02007-04-15 12:05:43 +00002645 (void) validate_numnodes(tree, 3, "lambdef_nocond");
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002646
2647 return (res);
2648}
2649
2650
Guido van Rossum3d602e31996-07-21 02:33:56 +00002651/* arglist:
2652 *
Fred Drakecff283c2000-08-21 22:24:43 +00002653 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002654 */
Guido van Rossum47478871996-08-21 14:32:37 +00002655static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002656validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002657{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002658 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002659 int i = 0;
2660 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002661
2662 if (nch <= 0)
2663 /* raise the right error from having an invalid number of children */
2664 return validate_numnodes(tree, nch + 1, "arglist");
2665
Raymond Hettinger354433a2004-05-19 08:20:33 +00002666 if (nch > 1) {
2667 for (i=0; i<nch; i++) {
2668 if (TYPE(CHILD(tree, i)) == argument) {
2669 node *ch = CHILD(tree, i);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002670 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
Raymond Hettinger354433a2004-05-19 08:20:33 +00002671 err_string("need '(', ')' for generator expression");
2672 return 0;
2673 }
2674 }
2675 }
2676 }
2677
Fred Drakecff283c2000-08-21 22:24:43 +00002678 while (ok && nch-i >= 2) {
2679 /* skip leading (argument ',') */
2680 ok = (validate_argument(CHILD(tree, i))
2681 && validate_comma(CHILD(tree, i+1)));
2682 if (ok)
2683 i += 2;
2684 else
2685 PyErr_Clear();
2686 }
2687 ok = 1;
2688 if (nch-i > 0) {
2689 /*
2690 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002691 */
Fred Drakecff283c2000-08-21 22:24:43 +00002692 int sym = TYPE(CHILD(tree, i));
2693
2694 if (sym == argument) {
2695 ok = validate_argument(CHILD(tree, i));
2696 if (ok && i+1 != nch) {
2697 err_string("illegal arglist specification"
2698 " (extra stuff on end)");
2699 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002700 }
Fred Drakecff283c2000-08-21 22:24:43 +00002701 }
2702 else if (sym == STAR) {
2703 ok = validate_star(CHILD(tree, i));
2704 if (ok && (nch-i == 2))
2705 ok = validate_test(CHILD(tree, i+1));
2706 else if (ok && (nch-i == 5))
2707 ok = (validate_test(CHILD(tree, i+1))
2708 && validate_comma(CHILD(tree, i+2))
2709 && validate_doublestar(CHILD(tree, i+3))
2710 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002711 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002712 err_string("illegal use of '*' in arglist");
2713 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002714 }
2715 }
Fred Drakecff283c2000-08-21 22:24:43 +00002716 else if (sym == DOUBLESTAR) {
2717 if (nch-i == 2)
2718 ok = (validate_doublestar(CHILD(tree, i))
2719 && validate_test(CHILD(tree, i+1)));
2720 else {
2721 err_string("illegal use of '**' in arglist");
2722 ok = 0;
2723 }
2724 }
2725 else {
2726 err_string("illegal arglist specification");
2727 ok = 0;
2728 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002729 }
2730 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002731}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002732
2733
2734
2735/* argument:
2736 *
Nick Coghlan650f0d02007-04-15 12:05:43 +00002737 * [test '='] test [comp_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002738 */
Guido van Rossum47478871996-08-21 14:32:37 +00002739static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002740validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002741{
2742 int nch = NCH(tree);
2743 int res = (validate_ntype(tree, argument)
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002744 && ((nch == 1) || (nch == 2) || (nch == 3)));
2745 if (res)
2746 res = validate_test(CHILD(tree, 0));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002747 if (res && (nch == 2))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002748 res = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002749 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002750 res = (validate_equal(CHILD(tree, 1))
2751 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002752
2753 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002754}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002755
2756
2757
2758/* trailer:
2759 *
Guido van Rossum47478871996-08-21 14:32:37 +00002760 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002761 */
Guido van Rossum47478871996-08-21 14:32:37 +00002762static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002763validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002764{
2765 int nch = NCH(tree);
2766 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002767
2768 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002769 switch (TYPE(CHILD(tree, 0))) {
2770 case LPAR:
2771 res = validate_rparen(CHILD(tree, nch - 1));
2772 if (res && (nch == 3))
2773 res = validate_arglist(CHILD(tree, 1));
2774 break;
2775 case LSQB:
2776 res = (validate_numnodes(tree, 3, "trailer")
2777 && validate_subscriptlist(CHILD(tree, 1))
2778 && validate_ntype(CHILD(tree, 2), RSQB));
2779 break;
2780 case DOT:
2781 res = (validate_numnodes(tree, 2, "trailer")
2782 && validate_ntype(CHILD(tree, 1), NAME));
2783 break;
2784 default:
2785 res = 0;
2786 break;
2787 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002788 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002789 else {
2790 (void) validate_numnodes(tree, 2, "trailer");
2791 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002792 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002793}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002794
2795
Guido van Rossum47478871996-08-21 14:32:37 +00002796/* subscriptlist:
2797 *
2798 * subscript (',' subscript)* [',']
2799 */
2800static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002801validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002802{
2803 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002804 validate_subscript, "subscriptlist"));
2805}
Guido van Rossum47478871996-08-21 14:32:37 +00002806
2807
Guido van Rossum3d602e31996-07-21 02:33:56 +00002808/* subscript:
2809 *
Guido van Rossum47478871996-08-21 14:32:37 +00002810 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002811 */
Guido van Rossum47478871996-08-21 14:32:37 +00002812static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002813validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002814{
Guido van Rossum47478871996-08-21 14:32:37 +00002815 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002816 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002817 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002818
Guido van Rossum47478871996-08-21 14:32:37 +00002819 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002820 if (!PyErr_Occurred())
2821 err_string("invalid number of arguments for subscript node");
2822 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002823 }
Guido van Rossum47478871996-08-21 14:32:37 +00002824 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002825 /* take care of ('.' '.' '.') possibility */
2826 return (validate_numnodes(tree, 3, "subscript")
2827 && validate_dot(CHILD(tree, 0))
2828 && validate_dot(CHILD(tree, 1))
2829 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002830 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002831 if (TYPE(CHILD(tree, 0)) == test)
2832 res = validate_test(CHILD(tree, 0));
2833 else
2834 res = validate_colon(CHILD(tree, 0));
2835 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002836 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002837 /* Must be [test] ':' [test] [sliceop],
2838 * but at least one of the optional components will
2839 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002840 */
2841 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002842 res = validate_test(CHILD(tree, 0));
2843 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002844 }
2845 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002846 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002847 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002848 int rem = nch - ++offset;
2849 if (rem) {
2850 if (TYPE(CHILD(tree, offset)) == test) {
2851 res = validate_test(CHILD(tree, offset));
2852 ++offset;
2853 --rem;
2854 }
2855 if (res && rem)
2856 res = validate_sliceop(CHILD(tree, offset));
2857 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002858 }
2859 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002860}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002861
2862
Guido van Rossum47478871996-08-21 14:32:37 +00002863static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002864validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002865{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002866 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002867 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002868 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002869 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002870 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002871 }
Guido van Rossum47478871996-08-21 14:32:37 +00002872 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002873 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002874 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002875 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002876
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002877 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002878}
Guido van Rossum47478871996-08-21 14:32:37 +00002879
2880
2881static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00002882validate_test_or_star_expr(node *n)
2883{
2884 if (TYPE(n) == test)
2885 return validate_test(n);
2886 return validate_star_expr(n);
2887}
2888
2889static int
2890validate_expr_or_star_expr(node *n)
2891{
2892 if (TYPE(n) == expr)
2893 return validate_expr(n);
2894 return validate_star_expr(n);
2895}
2896
2897
2898static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002899validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002900{
2901 return (validate_repeating_list(tree, exprlist,
Benjamin Peterson4905e802009-09-27 02:43:28 +00002902 validate_expr_or_star_expr, "exprlist"));
Fred Drakeff9ea482000-04-19 13:54:15 +00002903}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002904
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002905/*
2906 * dictorsetmaker:
2907 *
2908 * (test ':' test (comp_for | (',' test ':' test)* [','])) |
2909 * (test (comp_for | (',' test)* [',']))
2910 */
Guido van Rossum47478871996-08-21 14:32:37 +00002911static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002912validate_dictorsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002913{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002914 int nch = NCH(tree);
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002915 int res;
2916 int i = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002917
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002918 res = validate_ntype(tree, dictorsetmaker);
2919 if (!res)
2920 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002921
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002922 if (nch - i < 1) {
2923 (void) validate_numnodes(tree, 1, "dictorsetmaker");
2924 return 0;
2925 }
2926
2927 res = validate_test(CHILD(tree, i++));
2928 if (!res)
2929 return 0;
2930
2931 if (nch - i >= 2 && TYPE(CHILD(tree, i)) == COLON) {
2932 /* Dictionary display or dictionary comprehension. */
2933 res = (validate_colon(CHILD(tree, i++))
2934 && validate_test(CHILD(tree, i++)));
2935 if (!res)
2936 return 0;
2937
2938 if (nch - i >= 1 && TYPE(CHILD(tree, i)) == comp_for) {
2939 /* Dictionary comprehension. */
2940 res = validate_comp_for(CHILD(tree, i++));
2941 if (!res)
2942 return 0;
2943 }
2944 else {
2945 /* Dictionary display. */
2946 while (nch - i >= 4) {
2947 res = (validate_comma(CHILD(tree, i++))
2948 && validate_test(CHILD(tree, i++))
2949 && validate_colon(CHILD(tree, i++))
2950 && validate_test(CHILD(tree, i++)));
2951 if (!res)
2952 return 0;
2953 }
2954 if (nch - i == 1) {
2955 res = validate_comma(CHILD(tree, i++));
2956 if (!res)
2957 return 0;
2958 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002959 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002960 }
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002961 else {
2962 /* Set display or set comprehension. */
2963 if (nch - i >= 1 && TYPE(CHILD(tree, i)) == comp_for) {
2964 /* Set comprehension. */
2965 res = validate_comp_for(CHILD(tree, i++));
2966 if (!res)
2967 return 0;
2968 }
2969 else {
2970 /* Set display. */
2971 while (nch - i >= 2) {
2972 res = (validate_comma(CHILD(tree, i++))
2973 && validate_test(CHILD(tree, i++)));
2974 if (!res)
2975 return 0;
2976 }
2977 if (nch - i == 1) {
2978 res = validate_comma(CHILD(tree, i++));
2979 if (!res)
2980 return 0;
2981 }
2982 }
2983 }
2984
2985 if (nch - i > 0) {
2986 err_string("Illegal trailing nodes for dictorsetmaker.");
2987 return 0;
2988 }
2989
2990 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00002991}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002992
2993
Guido van Rossum47478871996-08-21 14:32:37 +00002994static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002995validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002996{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002997 int pos;
2998 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002999 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00003000 && (nch >= 2)
3001 && validate_testlist(CHILD(tree, 0))
3002 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003003
Guido van Rossum3d602e31996-07-21 02:33:56 +00003004 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00003005 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003006
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003007 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003008}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003009
3010
Guido van Rossum47478871996-08-21 14:32:37 +00003011static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003012validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003013{
Fred Drakeff9ea482000-04-19 13:54:15 +00003014 int nch = 0; /* num. children on current node */
3015 int res = 1; /* result value */
3016 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00003017
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00003018 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003019 nch = NCH(tree);
3020 next = 0;
3021 switch (TYPE(tree)) {
3022 /*
3023 * Definition nodes.
3024 */
3025 case funcdef:
3026 res = validate_funcdef(tree);
3027 break;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00003028 case with_stmt:
3029 res = validate_with_stmt(tree);
3030 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003031 case classdef:
3032 res = validate_class(tree);
3033 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003034 case decorated:
3035 res = validate_decorated(tree);
3036 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003037 /*
3038 * "Trivial" parse tree nodes.
3039 * (Why did I call these trivial?)
3040 */
3041 case stmt:
3042 res = validate_stmt(tree);
3043 break;
3044 case small_stmt:
3045 /*
Mark Dickinson407b3bd2012-04-29 22:18:31 +01003046 * expr_stmt | del_stmt | pass_stmt | flow_stmt |
3047 * import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00003048 */
3049 res = validate_small_stmt(tree);
3050 break;
3051 case flow_stmt:
3052 res = (validate_numnodes(tree, 1, "flow_stmt")
3053 && ((TYPE(CHILD(tree, 0)) == break_stmt)
3054 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00003055 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00003056 || (TYPE(CHILD(tree, 0)) == return_stmt)
3057 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
3058 if (res)
3059 next = CHILD(tree, 0);
3060 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00003061 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003062 break;
Fred Drake02126f22001-07-17 02:59:15 +00003063 case yield_stmt:
3064 res = validate_yield_stmt(tree);
3065 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003066 /*
3067 * Compound statements.
3068 */
3069 case simple_stmt:
3070 res = validate_simple_stmt(tree);
3071 break;
3072 case compound_stmt:
3073 res = validate_compound_stmt(tree);
3074 break;
3075 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00003076 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00003077 */
3078 case expr_stmt:
3079 res = validate_expr_stmt(tree);
3080 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003081 case del_stmt:
3082 res = validate_del_stmt(tree);
3083 break;
3084 case pass_stmt:
3085 res = (validate_numnodes(tree, 1, "pass")
3086 && validate_name(CHILD(tree, 0), "pass"));
3087 break;
3088 case break_stmt:
3089 res = (validate_numnodes(tree, 1, "break")
3090 && validate_name(CHILD(tree, 0), "break"));
3091 break;
3092 case continue_stmt:
3093 res = (validate_numnodes(tree, 1, "continue")
3094 && validate_name(CHILD(tree, 0), "continue"));
3095 break;
3096 case return_stmt:
3097 res = validate_return_stmt(tree);
3098 break;
3099 case raise_stmt:
3100 res = validate_raise_stmt(tree);
3101 break;
3102 case import_stmt:
3103 res = validate_import_stmt(tree);
3104 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 case import_name:
3106 res = validate_import_name(tree);
3107 break;
3108 case import_from:
3109 res = validate_import_from(tree);
3110 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003111 case global_stmt:
3112 res = validate_global_stmt(tree);
3113 break;
Mark Dickinson407b3bd2012-04-29 22:18:31 +01003114 case nonlocal_stmt:
3115 res = validate_nonlocal_stmt(tree);
3116 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003117 case assert_stmt:
3118 res = validate_assert_stmt(tree);
3119 break;
3120 case if_stmt:
3121 res = validate_if(tree);
3122 break;
3123 case while_stmt:
3124 res = validate_while(tree);
3125 break;
3126 case for_stmt:
3127 res = validate_for(tree);
3128 break;
3129 case try_stmt:
3130 res = validate_try(tree);
3131 break;
3132 case suite:
3133 res = validate_suite(tree);
3134 break;
3135 /*
3136 * Expression nodes.
3137 */
3138 case testlist:
3139 res = validate_testlist(tree);
3140 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003141 case yield_expr:
3142 res = validate_yield_expr(tree);
3143 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003144 case test:
3145 res = validate_test(tree);
3146 break;
3147 case and_test:
3148 res = validate_and_test(tree);
3149 break;
3150 case not_test:
3151 res = validate_not_test(tree);
3152 break;
3153 case comparison:
3154 res = validate_comparison(tree);
3155 break;
3156 case exprlist:
3157 res = validate_exprlist(tree);
3158 break;
3159 case comp_op:
3160 res = validate_comp_op(tree);
3161 break;
3162 case expr:
3163 res = validate_expr(tree);
3164 break;
3165 case xor_expr:
3166 res = validate_xor_expr(tree);
3167 break;
3168 case and_expr:
3169 res = validate_and_expr(tree);
3170 break;
3171 case shift_expr:
3172 res = validate_shift_expr(tree);
3173 break;
3174 case arith_expr:
3175 res = validate_arith_expr(tree);
3176 break;
3177 case term:
3178 res = validate_term(tree);
3179 break;
3180 case factor:
3181 res = validate_factor(tree);
3182 break;
3183 case power:
3184 res = validate_power(tree);
3185 break;
3186 case atom:
3187 res = validate_atom(tree);
3188 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003189
Fred Drakeff9ea482000-04-19 13:54:15 +00003190 default:
3191 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00003192 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003193 res = 0;
3194 break;
3195 }
3196 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003197 }
3198 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003199}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003200
3201
Guido van Rossum47478871996-08-21 14:32:37 +00003202static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003203validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003204{
3205 int res = validate_eval_input(tree);
3206
3207 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003208 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003209
3210 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003211}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003212
3213
Guido van Rossum3d602e31996-07-21 02:33:56 +00003214/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003215 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003216 */
Guido van Rossum47478871996-08-21 14:32:37 +00003217static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003218validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003219{
Fred Drakec2683dd2001-07-17 19:32:05 +00003220 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003221 int nch = NCH(tree) - 1;
3222 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003223 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003224
Fred Drakec2683dd2001-07-17 19:32:05 +00003225 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003226 if (TYPE(CHILD(tree, j)) == stmt)
3227 res = validate_stmt(CHILD(tree, j));
3228 else
3229 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003230 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003231 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003232 * user. Hopefully, this won't be needed. If a user reports getting
3233 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003234 */
3235 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003236 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003237
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003238 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003239}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003240
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003241static int
3242validate_encoding_decl(node *tree)
3243{
3244 int nch = NCH(tree);
3245 int res = ((nch == 1)
3246 && validate_file_input(CHILD(tree, 0)));
3247
3248 if (!res && !PyErr_Occurred())
3249 err_string("Error Parsing encoding_decl");
3250
3251 return res;
3252}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003253
Fred Drake43f8f9b1998-04-13 16:25:46 +00003254static PyObject*
3255pickle_constructor = NULL;
3256
3257
3258static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003259parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003260{
Fred Drake268397f1998-04-29 14:16:32 +00003261 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003262 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003263 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003264 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003265
Fred Drakec2683dd2001-07-17 19:32:05 +00003266 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003267 PyObject *newargs;
3268 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003269
Fred Drake2a6875e1999-09-20 22:32:18 +00003270 if ((empty_dict = PyDict_New()) == NULL)
3271 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003272 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003273 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003274 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003275 if (tuple != NULL) {
3276 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3277 Py_DECREF(tuple);
3278 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003279 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003280 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003281 }
3282 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003283 Py_XDECREF(empty_dict);
3284
Fred Drake43f8f9b1998-04-13 16:25:46 +00003285 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003286}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003287
3288
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003289/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003290 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003291 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003292 * We'd really have to write a wrapper around it all anyway to allow
3293 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003294 */
3295static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003296 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003297 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003298 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003299 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003300 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003301 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003302 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003303 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003304 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003305 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003306 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003307 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003308 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003309 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003310 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003311 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003312 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003313 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003314
Fred Drake43f8f9b1998-04-13 16:25:46 +00003315 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003316 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003317 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003318
Fred Drake268397f1998-04-29 14:16:32 +00003319 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003320 };
3321
3322
Martin v. Löwis1a214512008-06-11 05:26:20 +00003323
3324static struct PyModuleDef parsermodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 PyModuleDef_HEAD_INIT,
3326 "parser",
3327 NULL,
3328 -1,
3329 parser_functions,
3330 NULL,
3331 NULL,
3332 NULL,
3333 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00003334};
3335
3336PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003337
Mark Hammond62b1ab12002-07-23 06:31:15 +00003338PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003339PyInit_parser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003340{
Fred Drake13130bc2001-08-15 16:44:56 +00003341 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003342
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00003343 if (PyType_Ready(&PyST_Type) < 0)
3344 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00003345 module = PyModule_Create(&parsermodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003346 if (module == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003347 return NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003348
Fred Drake7a15ba51999-09-09 14:21:52 +00003349 if (parser_error == 0)
3350 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003351
Tim Peters6a627252003-07-21 14:25:23 +00003352 if (parser_error == 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003353 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003354 /* CAUTION: The code next used to skip bumping the refcount on
Martin v. Löwis1a214512008-06-11 05:26:20 +00003355 * parser_error. That's a disaster if PyInit_parser() gets called more
Tim Peters6a627252003-07-21 14:25:23 +00003356 * than once. By incref'ing, we ensure that each module dict that
3357 * gets created owns its reference to the shared parser_error object,
3358 * and the file static parser_error vrbl owns a reference too.
3359 */
3360 Py_INCREF(parser_error);
3361 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003362 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003363
Fred Drakec2683dd2001-07-17 19:32:05 +00003364 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003365 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003366
Fred Drake13130bc2001-08-15 16:44:56 +00003367 PyModule_AddStringConstant(module, "__copyright__",
3368 parser_copyright_string);
3369 PyModule_AddStringConstant(module, "__doc__",
3370 parser_doc_string);
3371 PyModule_AddStringConstant(module, "__version__",
3372 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003373
Fred Drake78bdb9b2001-07-19 20:17:15 +00003374 /* Register to support pickling.
3375 * If this fails, the import of this module will fail because an
3376 * exception will be raised here; should we clear the exception?
3377 */
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003378 copyreg = PyImport_ImportModuleNoBlock("copyreg");
Fred Drake13130bc2001-08-15 16:44:56 +00003379 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003380 PyObject *func, *pickler;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003381 _Py_IDENTIFIER(pickle);
3382 _Py_IDENTIFIER(sequence2st);
3383 _Py_IDENTIFIER(_pickler);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003384
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02003385 func = _PyObject_GetAttrId(copyreg, &PyId_pickle);
3386 pickle_constructor = _PyObject_GetAttrId(module, &PyId_sequence2st);
3387 pickler = _PyObject_GetAttrId(module, &PyId__pickler);
Fred Drakeff9ea482000-04-19 13:54:15 +00003388 Py_XINCREF(pickle_constructor);
3389 if ((func != NULL) && (pickle_constructor != NULL)
3390 && (pickler != NULL)) {
3391 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003392
Thomas Wouters477c8d52006-05-27 19:21:47 +00003393 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3394 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003395 Py_XDECREF(res);
3396 }
3397 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003398 Py_XDECREF(pickle_constructor);
3399 Py_XDECREF(pickler);
3400 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003401 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00003402 return module;
Fred Drakeff9ea482000-04-19 13:54:15 +00003403}