blob: 9497aaee43598d60d8146441e300876c82c28fc4 [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) {
303 case Py_EQ:
304 v = TEST_COND(result == 0);
305 break;
306 case Py_NE:
307 v = TEST_COND(result != 0);
308 break;
309 case Py_LE:
310 v = TEST_COND(result <= 0);
311 break;
312 case Py_GE:
313 v = TEST_COND(result >= 0);
314 break;
315 case Py_LT:
316 v = TEST_COND(result < 0);
317 break;
318 case Py_GT:
319 v = TEST_COND(result > 0);
320 break;
321 default:
322 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 Brandl30704ea02008-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) {
Antoine Pitrou6f430e42012-08-15 23:18:25 +0200404 lineno = PyObject_IsTrue(line_option);
405 if (lineno < 0)
406 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000407 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000408 if (col_option != NULL) {
Antoine Pitrou6f430e42012-08-15 23:18:25 +0200409 col_offset = PyObject_IsTrue(col_option);
410 if (col_offset < 0)
411 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000412 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000413 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000414 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000415 * since it's known to work already.
416 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000417 res = node2tuple(((PyST_Object*)self)->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000418 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000419 }
420 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000421}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000422
423
Fred Drakec2683dd2001-07-17 19:32:05 +0000424/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000425 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000426 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000427 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000428 *
429 */
430static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000431parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000432{
Guido van Rossum47478871996-08-21 14:32:37 +0000433 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000434 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000435 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000436 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000437
Georg Brandl30704ea02008-07-23 15:07:12 +0000438 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000439
Martin v. Löwis1a214512008-06-11 05:26:20 +0000440 if (self == NULL || PyModule_Check(self))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000441 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
442 &PyST_Type, &self, &line_option,
443 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000444 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000445 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
446 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000447 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000448 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000449 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000450 if (line_option != 0) {
Antoine Pitrou6f430e42012-08-15 23:18:25 +0200451 lineno = PyObject_IsTrue(line_option);
452 if (lineno < 0)
453 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000454 }
Antoine Pitrou6f430e42012-08-15 23:18:25 +0200455 if (col_option != 0) {
456 col_offset = PyObject_IsTrue(col_option);
457 if (col_offset < 0)
458 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000459 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000460 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000461 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000462 * since it's known to work already.
463 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000464 res = node2tuple(self->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000465 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000466 }
467 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000468}
Guido van Rossum47478871996-08-21 14:32:37 +0000469
470
Fred Drakec2683dd2001-07-17 19:32:05 +0000471/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000472 *
473 * This function creates code objects from the parse tree represented by
474 * the passed-in data object. An optional file name is passed in as well.
475 *
476 */
477static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000478parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000479{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000480 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000481 PyArena* arena;
482 mod_ty mod;
Fred Drakec2683dd2001-07-17 19:32:05 +0000483 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000484 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000485
Georg Brandl30704ea02008-07-23 15:07:12 +0000486 static char *keywords[] = {"st", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000487
Martin v. Löwis1a214512008-06-11 05:26:20 +0000488 if (self == NULL || PyModule_Check(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000489 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
490 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000491 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000492 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000493 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000494
Benjamin Petersonf216c942008-10-31 02:28:05 +0000495 if (ok) {
496 arena = PyArena_New();
497 if (arena) {
498 mod = PyAST_FromNode(self->st_node, &(self->st_flags), str, arena);
499 if (mod) {
500 res = (PyObject *)PyAST_Compile(mod, str, &(self->st_flags), arena);
501 }
502 PyArena_Free(arena);
503 }
504 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000505
506 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000507}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000508
509
510/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
511 * PyObject* parser_issuite(PyObject* self, PyObject* args)
512 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000513 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000514 * a statement suite, respectively. The return is a Python truth value.
515 *
516 */
517static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000518parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000519{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000520 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000521 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000522
Georg Brandl30704ea02008-07-23 15:07:12 +0000523 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000524
Martin v. Löwis1a214512008-06-11 05:26:20 +0000525 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000526 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000527 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000528 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000529 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000530
531 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000532 /* Check to see if the ST represents an expression or not. */
533 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000534 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000535 }
536 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000537}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000538
539
540static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000541parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000542{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000543 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000544 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000545
Georg Brandl30704ea02008-07-23 15:07:12 +0000546 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000547
Martin v. Löwis1a214512008-06-11 05:26:20 +0000548 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000549 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000550 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000551 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000552 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000553
554 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000555 /* Check to see if the ST represents an expression or not. */
556 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000557 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000558 }
559 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000560}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000561
562
Guido van Rossum3d602e31996-07-21 02:33:56 +0000563/* err_string(char* message)
564 *
565 * Sets the error string for an exception of type ParserError.
566 *
567 */
568static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000569err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000570{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000571 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000572}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000573
574
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000575/* PyObject* parser_do_parse(PyObject* args, int type)
576 *
577 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000578 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000579 *
580 */
581static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000582parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000583{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000584 char* string = 0;
585 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000586 int flags = 0;
587 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000588
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000589 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000590
591 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000592 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
593 &_PyParser_Grammar,
594 (type == PyST_EXPR)
595 ? eval_input : file_input,
596 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 if (n) {
599 res = parser_newstobject(n, type);
Benjamin Petersonf216c942008-10-31 02:28:05 +0000600 if (res)
601 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
602 }
603 else
604 PyParser_SetError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000605 }
606 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000607}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000608
609
610/* PyObject* parser_expr(PyObject* self, PyObject* args)
611 * PyObject* parser_suite(PyObject* self, PyObject* args)
612 *
613 * External interfaces to the parser itself. Which is called determines if
614 * the parser attempts to recognize an expression ('eval' form) or statement
615 * suite ('exec' form). The real work is done by parser_do_parse() above.
616 *
617 */
618static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000619parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000620{
Fred Drake268397f1998-04-29 14:16:32 +0000621 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000622 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000623}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000624
625
626static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000627parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000628{
Fred Drake268397f1998-04-29 14:16:32 +0000629 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000630 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000631}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000632
633
634
Fred Drakec2683dd2001-07-17 19:32:05 +0000635/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000636 * object requires that the input tuple be valid without having to rely on
637 * catching an exception from the compiler. This is done to allow the
638 * compiler itself to remain fast, since most of its input will come from
639 * the parser directly, and therefore be known to be syntactically correct.
640 * This validation is done to ensure that we don't core dump the compile
641 * phase, returning an exception instead.
642 *
643 * Two aspects can be broken out in this code: creating a node tree from
644 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000645 * advantageous to expand the number of ST types to include funcdefs and
646 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000647 * here. They are not necessary, and not quite as useful in a raw form.
648 * For now, let's get expressions and suites working reliably.
649 */
650
651
Jeremy Hylton938ace62002-07-17 16:30:39 +0000652static node* build_node_tree(PyObject *tuple);
653static int validate_expr_tree(node *tree);
654static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000655static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000656
Fred Drakec2683dd2001-07-17 19:32:05 +0000657/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000658 *
659 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000660 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000661 * tuple can be validated. It does this by checking the first code of the
662 * tuple, and, if acceptable, builds the internal representation. If this
663 * step succeeds, the internal representation is validated as fully as
664 * possible with the various validate_*() routines defined below.
665 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000666 * This function must be changed if support is to be added for PyST_FRAGMENT
667 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000668 *
669 */
670static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000671parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000672{
Fred Drake268397f1998-04-29 14:16:32 +0000673 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000674 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000675 PyObject *tuple;
676 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000677
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000678 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000679
Fred Drakec2683dd2001-07-17 19:32:05 +0000680 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000681 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000682 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000683 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000684 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000685 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000686 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000687 }
688 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000689 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000690 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000691 tree = build_node_tree(tuple);
692 if (tree != 0) {
693 int start_sym = TYPE(tree);
694 if (start_sym == eval_input) {
695 /* Might be an eval form. */
696 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000697 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000698 else
699 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000700 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000701 else if (start_sym == file_input) {
702 /* This looks like an exec form so far. */
703 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000704 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000705 else
706 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000707 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000708 else if (start_sym == encoding_decl) {
709 /* This looks like an encoding_decl so far. */
710 if (validate_encoding_decl(tree))
711 st = parser_newstobject(tree, PyST_SUITE);
712 else
713 PyNode_Free(tree);
714 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000715 else {
716 /* This is a fragment, at best. */
717 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000718 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000719 }
Guido van Rossum47478871996-08-21 14:32:37 +0000720 }
Andrew Svetlov737fb892012-12-18 21:14:22 +0200721 /* Make sure we raise an exception on all errors. We should never
Guido van Rossum47478871996-08-21 14:32:37 +0000722 * get this, but we'd do well to be sure something is done.
723 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000724 if (st == NULL && !PyErr_Occurred())
725 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000726
Fred Drakec2683dd2001-07-17 19:32:05 +0000727 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000728}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000729
730
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000731/* node* build_node_children()
732 *
733 * Iterate across the children of the current non-terminal node and build
734 * their structures. If successful, return the root of this portion of
735 * the tree, otherwise, 0. Any required exception will be specified already,
736 * and no memory will have been deallocated.
737 *
738 */
739static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000740build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000741{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000742 Py_ssize_t len = PyObject_Size(tuple);
743 Py_ssize_t i;
744 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000745
746 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000747 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000748 PyObject* elem = PySequence_GetItem(tuple, i);
749 int ok = elem != NULL;
750 long type = 0;
751 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000752
Fred Drakeff9ea482000-04-19 13:54:15 +0000753 if (ok)
754 ok = PySequence_Check(elem);
755 if (ok) {
756 PyObject *temp = PySequence_GetItem(elem, 0);
757 if (temp == NULL)
758 ok = 0;
759 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000760 ok = PyLong_Check(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000761 if (ok)
Christian Heimes217cfd12007-12-02 14:31:20 +0000762 type = PyLong_AS_LONG(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000763 Py_DECREF(temp);
764 }
765 }
766 if (!ok) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000767 PyObject *err = Py_BuildValue("os", elem,
768 "Illegal node construct.");
769 PyErr_SetObject(parser_error, err);
770 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000771 Py_XDECREF(elem);
772 return (0);
773 }
774 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000775 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000776 PyObject *temp;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000777 const char *temp_str;
Guido van Rossum47478871996-08-21 14:32:37 +0000778
Fred Drake0ac9b072000-09-12 21:58:06 +0000779 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000780 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000781 return 0;
782 }
783 temp = PySequence_GetItem(elem, 1);
784 if (temp == NULL)
785 return 0;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000786 if (!PyUnicode_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000787 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000788 "second item in terminal node must be a string,"
789 " found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000790 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000791 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000792 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000793 return 0;
794 }
795 if (len == 3) {
796 PyObject *o = PySequence_GetItem(elem, 2);
797 if (o != NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000798 if (PyLong_Check(o))
799 *line_num = PyLong_AS_LONG(o);
Fred Drake0ac9b072000-09-12 21:58:06 +0000800 else {
801 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000802 "third item in terminal node must be an"
803 " integer, found %s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 Py_TYPE(temp)->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000805 Py_DECREF(o);
806 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000807 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000808 return 0;
809 }
810 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000811 }
812 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000813 temp_str = _PyUnicode_AsStringAndSize(temp, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000814 if (temp_str == NULL) {
815 Py_DECREF(temp);
816 Py_XDECREF(elem);
817 return 0;
818 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000819 strn = (char *)PyObject_MALLOC(len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000820 if (strn != NULL)
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000821 (void) memcpy(strn, temp_str, len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000822 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000823 }
824 else if (!ISNONTERMINAL(type)) {
825 /*
826 * It has to be one or the other; this is an error.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200827 * Raise an exception.
Fred Drakeff9ea482000-04-19 13:54:15 +0000828 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000829 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
830 PyErr_SetObject(parser_error, err);
831 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000832 Py_XDECREF(elem);
833 return (0);
834 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000835 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000836 if (err == E_NOMEM) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000837 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000838 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000839 return (node *) PyErr_NoMemory();
840 }
841 if (err == E_OVERFLOW) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000842 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000843 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000844 PyErr_SetString(PyExc_ValueError,
845 "unsupported number of child nodes");
846 return NULL;
847 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000848
Fred Drakeff9ea482000-04-19 13:54:15 +0000849 if (ISNONTERMINAL(type)) {
850 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000851
Fred Drakeff9ea482000-04-19 13:54:15 +0000852 if (new_child != build_node_children(elem, new_child, line_num)) {
853 Py_XDECREF(elem);
854 return (0);
855 }
856 }
857 else if (type == NEWLINE) { /* It's true: we increment the */
858 ++(*line_num); /* line number *after* the newline! */
859 }
860 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000861 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000862 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000863}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000864
865
866static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000867build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000868{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000869 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000870 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000871 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000872
Guido van Rossum47478871996-08-21 14:32:37 +0000873 if (temp != NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000874 num = PyLong_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000875 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000876 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000877 /*
878 * The tuple is simple, but it doesn't start with a start symbol.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200879 * Raise an exception now and be done with it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000880 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000881 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000882 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000883 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000884 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000885 }
886 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000887 /*
888 * Not efficient, but that can be handled later.
889 */
890 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000891 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000892
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000893 if (num == encoding_decl) {
894 encoding = PySequence_GetItem(tuple, 2);
895 /* tuple isn't borrowed anymore here, need to DECREF */
896 tuple = PySequence_GetSlice(tuple, 0, 2);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000897 if (tuple == NULL)
898 return NULL;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000899 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000900 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000901 if (res != NULL) {
902 if (res != build_node_children(tuple, res, &line_num)) {
903 PyNode_Free(res);
904 res = NULL;
905 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000906 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000907 Py_ssize_t len;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000908 const char *temp;
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000909 temp = _PyUnicode_AsStringAndSize(encoding, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000910 if (temp == NULL) {
911 Py_DECREF(res);
912 Py_DECREF(encoding);
913 Py_DECREF(tuple);
914 return NULL;
915 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000916 res->n_str = (char *)PyObject_MALLOC(len + 1);
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000917 if (res->n_str != NULL && temp != NULL)
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000918 (void) memcpy(res->n_str, temp, len + 1);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000919 Py_DECREF(encoding);
920 Py_DECREF(tuple);
921 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000922 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000923 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000924 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000925 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000926 * NONTERMINAL, we can't use it. Not sure the implementation
927 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000928 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000929 PyObject *err = Py_BuildValue("os", tuple,
930 "Illegal component tuple.");
931 PyErr_SetObject(parser_error, err);
932 Py_XDECREF(err);
933 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000934
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000935 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000936}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000937
938
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000939/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000940 * Validation routines used within the validation section:
941 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000942static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000943
Fred Drakeff9ea482000-04-19 13:54:15 +0000944#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
945#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
946#define validate_colon(ch) validate_terminal(ch, COLON, ":")
947#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
948#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
949#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
950#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
951#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
952#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
953#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
954#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
955#define validate_star(ch) validate_terminal(ch, STAR, "*")
956#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
957#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
958#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000959#define validate_at(ch) validate_terminal(ch, AT, "@")
Mark Dickinsonea7e9f92012-04-29 18:34:40 +0100960#define validate_rarrow(ch) validate_terminal(ch, RARROW, "->")
Fred Drakeff9ea482000-04-19 13:54:15 +0000961#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000962
Fred Drake0ac9b072000-09-12 21:58:06 +0000963#define VALIDATER(n) static int validate_##n(node *tree)
964
Fred Drakeff9ea482000-04-19 13:54:15 +0000965VALIDATER(node); VALIDATER(small_stmt);
966VALIDATER(class); VALIDATER(node);
967VALIDATER(parameters); VALIDATER(suite);
968VALIDATER(testlist); VALIDATER(varargslist);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000969VALIDATER(vfpdef);
Fred Drakeff9ea482000-04-19 13:54:15 +0000970VALIDATER(stmt); VALIDATER(simple_stmt);
971VALIDATER(expr_stmt); VALIDATER(power);
Guido van Rossum452bf512007-02-09 05:32:43 +0000972VALIDATER(del_stmt);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000973VALIDATER(return_stmt); VALIDATER(raise_stmt);
974VALIDATER(import_stmt); VALIDATER(import_stmt);
975VALIDATER(import_name); VALIDATER(yield_stmt);
Mark Dickinson407b3bd2012-04-29 22:18:31 +0100976VALIDATER(global_stmt); VALIDATER(nonlocal_stmt);
977VALIDATER(assert_stmt);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000978VALIDATER(compound_stmt); VALIDATER(test_or_star_expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000979VALIDATER(while); VALIDATER(for);
980VALIDATER(try); VALIDATER(except_clause);
981VALIDATER(test); VALIDATER(and_test);
982VALIDATER(not_test); VALIDATER(comparison);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000983VALIDATER(comp_op);
Guido van Rossum0368b722007-05-11 16:50:42 +0000984VALIDATER(star_expr); VALIDATER(expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000985VALIDATER(xor_expr); VALIDATER(and_expr);
986VALIDATER(shift_expr); VALIDATER(arith_expr);
987VALIDATER(term); VALIDATER(factor);
988VALIDATER(atom); VALIDATER(lambdef);
989VALIDATER(trailer); VALIDATER(subscript);
990VALIDATER(subscriptlist); VALIDATER(sliceop);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000991VALIDATER(exprlist); VALIDATER(dictorsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000992VALIDATER(arglist); VALIDATER(argument);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000993VALIDATER(comp_for);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000994VALIDATER(comp_iter); VALIDATER(comp_if);
995VALIDATER(testlist_comp); VALIDATER(yield_expr);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000996VALIDATER(or_test);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000997VALIDATER(test_nocond); VALIDATER(lambdef_nocond);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000998
Fred Drake0ac9b072000-09-12 21:58:06 +0000999#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001000
Fred Drakeff9ea482000-04-19 13:54:15 +00001001#define is_even(n) (((n) & 1) == 0)
1002#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001003
1004
1005static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001006validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +00001007{
Fred Drake0ac9b072000-09-12 21:58:06 +00001008 if (TYPE(n) != t) {
1009 PyErr_Format(parser_error, "Expected node type %d, got %d.",
1010 t, TYPE(n));
1011 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001012 }
Fred Drake0ac9b072000-09-12 21:58:06 +00001013 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00001014}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001015
1016
Fred Drakee7ab64e2000-04-25 04:14:46 +00001017/* Verifies that the number of child nodes is exactly 'num', raising
1018 * an exception if it isn't. The exception message does not indicate
1019 * the exact number of nodes, allowing this to be used to raise the
1020 * "right" exception when the wrong number of nodes is present in a
1021 * specific variant of a statement's syntax. This is commonly used
1022 * in that fashion.
1023 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001024static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001025validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001026{
Guido van Rossum3d602e31996-07-21 02:33:56 +00001027 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001028 PyErr_Format(parser_error,
1029 "Illegal number of children for %s node.", name);
1030 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001031 }
Fred Drake0ac9b072000-09-12 21:58:06 +00001032 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00001033}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001034
1035
1036static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001037validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +00001038{
Guido van Rossum3d602e31996-07-21 02:33:56 +00001039 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +00001040 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001041
1042 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001043 PyErr_Format(parser_error,
1044 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001045 }
1046 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001047}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001048
1049
Guido van Rossum47478871996-08-21 14:32:37 +00001050/* X (',' X) [',']
1051 */
1052static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +00001053validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +00001054 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001055{
1056 int nch = NCH(tree);
1057 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +00001058 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +00001059
1060 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001061 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +00001062 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001063 if (is_even(nch))
1064 res = validate_comma(CHILD(tree, --nch));
1065 if (res && nch > 1) {
1066 int pos = 1;
1067 for ( ; res && pos < nch; pos += 2)
1068 res = (validate_comma(CHILD(tree, pos))
1069 && vfunc(CHILD(tree, pos + 1)));
1070 }
Guido van Rossum47478871996-08-21 14:32:37 +00001071 }
1072 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001073}
Guido van Rossum47478871996-08-21 14:32:37 +00001074
1075
Fred Drakecff283c2000-08-21 22:24:43 +00001076/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001077 *
1078 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001079 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00001080 */
Guido van Rossum47478871996-08-21 14:32:37 +00001081static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001082validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001083{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001084 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +00001085 int res = (validate_ntype(tree, classdef) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001087
Guido van Rossum3d602e31996-07-21 02:33:56 +00001088 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001089 res = (validate_name(CHILD(tree, 0), "class")
1090 && validate_ntype(CHILD(tree, 1), NAME)
1091 && validate_colon(CHILD(tree, nch - 2))
1092 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001093 }
Brett Cannonf4189912005-04-09 02:30:16 +00001094 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001095 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +00001096 }
Guido van Rossumfc158e22007-11-15 19:17:28 +00001097
Brett Cannonf4189912005-04-09 02:30:16 +00001098 if (res) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 if (nch == 7) {
1100 res = ((validate_lparen(CHILD(tree, 2)) &&
1101 validate_arglist(CHILD(tree, 3)) &&
1102 validate_rparen(CHILD(tree, 4))));
1103 }
1104 else if (nch == 6) {
1105 res = (validate_lparen(CHILD(tree,2)) &&
1106 validate_rparen(CHILD(tree,3)));
1107 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001108 }
1109 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001110}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001111
1112
Guido van Rossum3d602e31996-07-21 02:33:56 +00001113/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001114 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001115 */
Guido van Rossum47478871996-08-21 14:32:37 +00001116static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001117validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001118{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001119 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001120 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001121 && (nch >= 4)
1122 && validate_name(CHILD(tree, 0), "if")
1123 && validate_test(CHILD(tree, 1))
1124 && validate_colon(CHILD(tree, 2))
1125 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001126
1127 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001128 /* ... 'else' ':' suite */
1129 res = (validate_name(CHILD(tree, nch - 3), "else")
1130 && validate_colon(CHILD(tree, nch - 2))
1131 && validate_suite(CHILD(tree, nch - 1)));
1132 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001133 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001134 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001135 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001136 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001137 /* Will catch the case for nch < 4 */
1138 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001139 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001140 /* ... ('elif' test ':' suite)+ ... */
1141 int j = 4;
1142 while ((j < nch) && res) {
1143 res = (validate_name(CHILD(tree, j), "elif")
1144 && validate_colon(CHILD(tree, j + 2))
1145 && validate_test(CHILD(tree, j + 1))
1146 && validate_suite(CHILD(tree, j + 3)));
1147 j += 4;
1148 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001149 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001150 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001151}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001152
1153
Guido van Rossum3d602e31996-07-21 02:33:56 +00001154/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001155 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001156 *
1157 */
Guido van Rossum47478871996-08-21 14:32:37 +00001158static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001159validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001160{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001161 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001162 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001163
Guido van Rossum3d602e31996-07-21 02:33:56 +00001164 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001165 res = (validate_lparen(CHILD(tree, 0))
1166 && validate_rparen(CHILD(tree, nch - 1)));
1167 if (res && (nch == 3))
1168 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001169 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001170 else {
1171 (void) validate_numnodes(tree, 2, "parameters");
1172 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001173 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001174}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001175
1176
Fred Drakecff283c2000-08-21 22:24:43 +00001177/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001178 *
1179 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001180 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001181 * | NEWLINE INDENT stmt+ DEDENT
1182 */
Guido van Rossum47478871996-08-21 14:32:37 +00001183static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001184validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001185{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001186 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001187 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001188
Guido van Rossum3d602e31996-07-21 02:33:56 +00001189 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001190 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001191 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001192 /* NEWLINE INDENT stmt+ DEDENT */
1193 res = (validate_newline(CHILD(tree, 0))
1194 && validate_indent(CHILD(tree, 1))
1195 && validate_stmt(CHILD(tree, 2))
1196 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001197
Fred Drakeff9ea482000-04-19 13:54:15 +00001198 if (res && (nch > 4)) {
1199 int i = 3;
1200 --nch; /* forget the DEDENT */
1201 for ( ; res && (i < nch); ++i)
1202 res = validate_stmt(CHILD(tree, i));
1203 }
1204 else if (nch < 4)
1205 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001206 }
1207 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001208}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001209
1210
Guido van Rossum47478871996-08-21 14:32:37 +00001211static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001212validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001213{
Guido van Rossum47478871996-08-21 14:32:37 +00001214 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001215 validate_test, "testlist"));
1216}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001217
Guido van Rossum1c917072001-10-15 15:44:05 +00001218static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00001219validate_testlist_star_expr(node *tl)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001220{
Benjamin Peterson4905e802009-09-27 02:43:28 +00001221 return (validate_repeating_list(tl, testlist_star_expr, validate_test_or_star_expr,
1222 "testlist"));
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001223}
1224
1225
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001226/* validate either vfpdef or tfpdef.
1227 * vfpdef: NAME
1228 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001229 */
1230static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001231validate_vfpdef(node *tree)
Neal Norwitzc1505362006-12-28 06:47:50 +00001232{
1233 int nch = NCH(tree);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001234 if (TYPE(tree) == vfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001235 return nch == 1 && validate_name(CHILD(tree, 0), NULL);
1236 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001237 else if (TYPE(tree) == tfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001238 if (nch == 1) {
1239 return validate_name(CHILD(tree, 0), NULL);
1240 }
1241 else if (nch == 3) {
1242 return validate_name(CHILD(tree, 0), NULL) &&
1243 validate_colon(CHILD(tree, 1)) &&
1244 validate_test(CHILD(tree, 2));
1245 }
1246 }
1247 return 0;
1248}
1249
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001250/* '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001251 * ..or tfpdef in place of vfpdef. vfpdef: NAME; tfpdef: NAME [':' test]
Fred Drakecff283c2000-08-21 22:24:43 +00001252 */
1253static int
1254validate_varargslist_trailer(node *tree, int start)
1255{
1256 int nch = NCH(tree);
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001257 int res = 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001258
1259 if (nch <= start) {
1260 err_string("expected variable argument trailer for varargslist");
1261 return 0;
1262 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001263 if (TYPE(CHILD(tree, start)) == STAR) {
Fred Drakecff283c2000-08-21 22:24:43 +00001264 /*
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001265 * '*' [vfpdef]
Fred Drakecff283c2000-08-21 22:24:43 +00001266 */
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001267 res = validate_star(CHILD(tree, start++));
1268 if (res && start < nch && (TYPE(CHILD(tree, start)) == vfpdef ||
1269 TYPE(CHILD(tree, start)) == tfpdef))
1270 res = validate_vfpdef(CHILD(tree, start++));
1271 /*
1272 * (',' vfpdef ['=' test])*
1273 */
1274 while (res && start + 1 < nch && (
1275 TYPE(CHILD(tree, start + 1)) == vfpdef ||
1276 TYPE(CHILD(tree, start + 1)) == tfpdef)) {
1277 res = (validate_comma(CHILD(tree, start++))
1278 && validate_vfpdef(CHILD(tree, start++)));
1279 if (res && start + 1 < nch && TYPE(CHILD(tree, start)) == EQUAL)
1280 res = (validate_equal(CHILD(tree, start++))
1281 && validate_test(CHILD(tree, start++)));
1282 }
1283 /*
1284 * [',' '**' vfpdef]
1285 */
1286 if (res && start + 2 < nch && TYPE(CHILD(tree, start+1)) == DOUBLESTAR)
1287 res = (validate_comma(CHILD(tree, start++))
1288 && validate_doublestar(CHILD(tree, start++))
1289 && validate_vfpdef(CHILD(tree, start++)));
1290 }
1291 else if (TYPE(CHILD(tree, start)) == DOUBLESTAR) {
1292 /*
1293 * '**' vfpdef
1294 */
1295 if (start + 1 < nch)
1296 res = (validate_doublestar(CHILD(tree, start++))
1297 && validate_vfpdef(CHILD(tree, start++)));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001298 else {
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001299 res = 0;
1300 err_string("expected vfpdef after ** in varargslist trailer");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001301 }
Fred Drakecff283c2000-08-21 22:24:43 +00001302 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001303 else {
1304 res = 0;
1305 err_string("expected * or ** in varargslist trailer");
Fred Drakecff283c2000-08-21 22:24:43 +00001306 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001307
1308 if (res && start != nch) {
1309 res = 0;
1310 err_string("unexpected extra children in varargslist trailer");
1311 }
Fred Drakecff283c2000-08-21 22:24:43 +00001312 return res;
1313}
1314
1315
Neal Norwitzc1505362006-12-28 06:47:50 +00001316/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001317 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001318 * Validate typedargslist or varargslist.
1319 *
1320 * typedargslist: ((tfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001321 * ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
1322 * '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001323 * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001324 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001325 * varargslist: ((vfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001326 * ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
1327 * '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001328 * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001329 * vfpdef: NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001330 *
1331 */
Guido van Rossum47478871996-08-21 14:32:37 +00001332static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001333validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001334{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001335 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001336 int res = (TYPE(tree) == varargslist ||
1337 TYPE(tree) == typedargslist) &&
1338 (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001339 int sym;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001340 node *ch;
1341 int i = 0;
Guido van Rossumfc158e22007-11-15 19:17:28 +00001342
Fred Drakeb6429a22000-12-11 22:08:27 +00001343 if (!res)
1344 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001345 if (nch < 1) {
1346 err_string("varargslist missing child nodes");
1347 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001348 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001349 while (i < nch) {
Guido van Rossumfc158e22007-11-15 19:17:28 +00001350 ch = CHILD(tree, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001351 sym = TYPE(ch);
1352 if (sym == vfpdef || sym == tfpdef) {
1353 /* validate (vfpdef ['=' test] ',')+ */
1354 res = validate_vfpdef(ch);
Fred Drakecff283c2000-08-21 22:24:43 +00001355 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001356 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1357 res = (validate_equal(CHILD(tree, i))
1358 && validate_test(CHILD(tree, i+1)));
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001359 if (res)
1360 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001361 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001362 if (res && i < nch) {
1363 res = validate_comma(CHILD(tree, i));
1364 ++i;
Fred Drakecff283c2000-08-21 22:24:43 +00001365 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001366 } else if (sym == DOUBLESTAR || sym == STAR) {
1367 res = validate_varargslist_trailer(tree, i);
1368 break;
1369 } else {
1370 res = 0;
1371 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001372 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001373 }
Fred Drakecff283c2000-08-21 22:24:43 +00001374 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001375}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001376
1377
Nick Coghlan650f0d02007-04-15 12:05:43 +00001378/* comp_iter: comp_for | comp_if
Fred Drakecff283c2000-08-21 22:24:43 +00001379 */
1380static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001381validate_comp_iter(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001382{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001383 int res = (validate_ntype(tree, comp_iter)
1384 && validate_numnodes(tree, 1, "comp_iter"));
1385 if (res && TYPE(CHILD(tree, 0)) == comp_for)
1386 res = validate_comp_for(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001387 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001388 res = validate_comp_if(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001389
1390 return res;
1391}
1392
Nick Coghlan650f0d02007-04-15 12:05:43 +00001393/* comp_for: 'for' exprlist 'in' test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001394 */
1395static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001396validate_comp_for(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001397{
1398 int nch = NCH(tree);
1399 int res;
1400
1401 if (nch == 5)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001402 res = validate_comp_iter(CHILD(tree, 4));
Fred Drakecff283c2000-08-21 22:24:43 +00001403 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001404 res = validate_numnodes(tree, 4, "comp_for");
Raymond Hettinger354433a2004-05-19 08:20:33 +00001405
1406 if (res)
1407 res = (validate_name(CHILD(tree, 0), "for")
1408 && validate_exprlist(CHILD(tree, 1))
1409 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001410 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001411
1412 return res;
1413}
1414
Nick Coghlan650f0d02007-04-15 12:05:43 +00001415/* comp_if: 'if' test_nocond [comp_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001416 */
1417static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001418validate_comp_if(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001419{
1420 int nch = NCH(tree);
1421 int res;
1422
1423 if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001424 res = validate_comp_iter(CHILD(tree, 2));
Fred Drakecff283c2000-08-21 22:24:43 +00001425 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001426 res = validate_numnodes(tree, 2, "comp_if");
Fred Drakecff283c2000-08-21 22:24:43 +00001427
1428 if (res)
1429 res = (validate_name(CHILD(tree, 0), "if")
Nick Coghlan650f0d02007-04-15 12:05:43 +00001430 && validate_test_nocond(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001431
1432 return res;
1433}
1434
1435
Guido van Rossum3d602e31996-07-21 02:33:56 +00001436/* simple_stmt | compound_stmt
1437 *
1438 */
Guido van Rossum47478871996-08-21 14:32:37 +00001439static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001440validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001441{
1442 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001443 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001444
Guido van Rossum3d602e31996-07-21 02:33:56 +00001445 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001446 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001447
Fred Drakeff9ea482000-04-19 13:54:15 +00001448 if (TYPE(tree) == simple_stmt)
1449 res = validate_simple_stmt(tree);
1450 else
1451 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001452 }
1453 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001454}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001455
1456
Guido van Rossum3d602e31996-07-21 02:33:56 +00001457/* small_stmt (';' small_stmt)* [';'] NEWLINE
1458 *
1459 */
Guido van Rossum47478871996-08-21 14:32:37 +00001460static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001461validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001462{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001463 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001464 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001465 && (nch >= 2)
1466 && validate_small_stmt(CHILD(tree, 0))
1467 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001468
Guido van Rossum3d602e31996-07-21 02:33:56 +00001469 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001470 res = validate_numnodes(tree, 2, "simple_stmt");
1471 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001472 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001473 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001474 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001475 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001476
Fred Drakeff9ea482000-04-19 13:54:15 +00001477 for (i = 1; res && (i < nch); i += 2)
1478 res = (validate_semi(CHILD(tree, i))
1479 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001480 }
1481 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001482}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001483
1484
Guido van Rossum47478871996-08-21 14:32:37 +00001485static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001486validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001487{
1488 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001489 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001490
Fred Drake0ac9b072000-09-12 21:58:06 +00001491 if (res) {
1492 int ntype = TYPE(CHILD(tree, 0));
1493
1494 if ( (ntype == expr_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001495 || (ntype == del_stmt)
1496 || (ntype == pass_stmt)
1497 || (ntype == flow_stmt)
1498 || (ntype == import_stmt)
1499 || (ntype == global_stmt)
Mark Dickinson407b3bd2012-04-29 22:18:31 +01001500 || (ntype == nonlocal_stmt)
Georg Brandl7cae87c2006-09-06 06:51:57 +00001501 || (ntype == assert_stmt))
Fred Drake0ac9b072000-09-12 21:58:06 +00001502 res = validate_node(CHILD(tree, 0));
1503 else {
1504 res = 0;
1505 err_string("illegal small_stmt child type");
1506 }
1507 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001508 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001509 res = 0;
1510 PyErr_Format(parser_error,
1511 "Unrecognized child node of small_stmt: %d.",
1512 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001513 }
1514 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001515}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001516
1517
1518/* compound_stmt:
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00001519 * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
Guido van Rossum3d602e31996-07-21 02:33:56 +00001520 */
Guido van Rossum47478871996-08-21 14:32:37 +00001521static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001522validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001523{
1524 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001525 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001526 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001527
1528 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001529 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001530
1531 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001532 ntype = TYPE(tree);
1533 if ( (ntype == if_stmt)
1534 || (ntype == while_stmt)
1535 || (ntype == for_stmt)
1536 || (ntype == try_stmt)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00001537 || (ntype == with_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001538 || (ntype == funcdef)
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001539 || (ntype == classdef)
1540 || (ntype == decorated))
Fred Drakeff9ea482000-04-19 13:54:15 +00001541 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001542 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001543 res = 0;
1544 PyErr_Format(parser_error,
1545 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001546 }
1547 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001548}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001549
Guido van Rossum47478871996-08-21 14:32:37 +00001550static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00001551validate_yield_or_testlist(node *tree, int tse)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001552{
Benjamin Peterson4905e802009-09-27 02:43:28 +00001553 if (TYPE(tree) == yield_expr) {
1554 return validate_yield_expr(tree);
1555 }
1556 else {
1557 if (tse)
1558 return validate_testlist_star_expr(tree);
1559 else
1560 return validate_testlist(tree);
1561 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001562}
1563
1564static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001565validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001566{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001567 int j;
1568 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001569 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001570 && is_odd(nch)
Benjamin Peterson4905e802009-09-27 02:43:28 +00001571 && validate_testlist_star_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001572
Fred Drake28f739a2000-08-25 22:42:40 +00001573 if (res && nch == 3
1574 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001575 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
Benjamin Peterson4905e802009-09-27 02:43:28 +00001576 && validate_yield_or_testlist(CHILD(tree, 2), 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001577
Fred Drake28f739a2000-08-25 22:42:40 +00001578 if (res) {
1579 char *s = STR(CHILD(CHILD(tree, 1), 0));
1580
1581 res = (strcmp(s, "+=") == 0
1582 || strcmp(s, "-=") == 0
1583 || strcmp(s, "*=") == 0
1584 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001585 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001586 || strcmp(s, "%=") == 0
1587 || strcmp(s, "&=") == 0
1588 || strcmp(s, "|=") == 0
1589 || strcmp(s, "^=") == 0
1590 || strcmp(s, "<<=") == 0
1591 || strcmp(s, ">>=") == 0
1592 || strcmp(s, "**=") == 0);
1593 if (!res)
Ezio Melotti42da6632011-03-15 05:18:48 +02001594 err_string("illegal augmented assignment operator");
Fred Drake28f739a2000-08-25 22:42:40 +00001595 }
1596 }
1597 else {
1598 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001599 res = validate_equal(CHILD(tree, j))
Benjamin Peterson4905e802009-09-27 02:43:28 +00001600 && validate_yield_or_testlist(CHILD(tree, j + 1), 1);
Fred Drake28f739a2000-08-25 22:42:40 +00001601 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001602 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001603}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001604
1605
Guido van Rossum47478871996-08-21 14:32:37 +00001606static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001607validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001608{
1609 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001610 && validate_name(CHILD(tree, 0), "del")
1611 && validate_exprlist(CHILD(tree, 1)));
1612}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001613
1614
Guido van Rossum47478871996-08-21 14:32:37 +00001615static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001616validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001617{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001618 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001619 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001620 && ((nch == 1) || (nch == 2))
1621 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001622
Guido van Rossum3d602e31996-07-21 02:33:56 +00001623 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001624 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001625
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001626 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001627}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001628
1629
Mark Dickinsoncf360b92012-05-07 12:01:27 +01001630/*
1631 * raise_stmt:
1632 *
1633 * 'raise' [test ['from' test]]
1634 */
Guido van Rossum47478871996-08-21 14:32:37 +00001635static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001636validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001637{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001638 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001639 int res = (validate_ntype(tree, raise_stmt)
Mark Dickinsoncf360b92012-05-07 12:01:27 +01001640 && ((nch == 1) || (nch == 2) || (nch == 4)));
1641
1642 if (!res && !PyErr_Occurred())
1643 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001644
Guido van Rossum3d602e31996-07-21 02:33:56 +00001645 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001646 res = validate_name(CHILD(tree, 0), "raise");
1647 if (res && (nch >= 2))
1648 res = validate_test(CHILD(tree, 1));
Mark Dickinsoncf360b92012-05-07 12:01:27 +01001649 if (res && (nch == 4)) {
1650 res = (validate_name(CHILD(tree, 2), "from")
Fred Drakeff9ea482000-04-19 13:54:15 +00001651 && validate_test(CHILD(tree, 3)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001652 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001653 }
1654 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001655}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001656
1657
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001658/* yield_expr: 'yield' [testlist]
1659 */
1660static int
1661validate_yield_expr(node *tree)
1662{
1663 int nch = NCH(tree);
1664 int res = (validate_ntype(tree, yield_expr)
1665 && ((nch == 1) || (nch == 2))
1666 && validate_name(CHILD(tree, 0), "yield"));
1667
1668 if (res && (nch == 2))
1669 res = validate_testlist(CHILD(tree, 1));
1670
1671 return (res);
1672}
1673
1674
1675/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001676 */
1677static int
1678validate_yield_stmt(node *tree)
1679{
1680 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001681 && validate_numnodes(tree, 1, "yield_stmt")
1682 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001683}
1684
1685
Fred Drakecff283c2000-08-21 22:24:43 +00001686static int
1687validate_import_as_name(node *tree)
1688{
1689 int nch = NCH(tree);
1690 int ok = validate_ntype(tree, import_as_name);
1691
1692 if (ok) {
1693 if (nch == 1)
1694 ok = validate_name(CHILD(tree, 0), NULL);
1695 else if (nch == 3)
1696 ok = (validate_name(CHILD(tree, 0), NULL)
1697 && validate_name(CHILD(tree, 1), "as")
1698 && validate_name(CHILD(tree, 2), NULL));
1699 else
1700 ok = validate_numnodes(tree, 3, "import_as_name");
1701 }
1702 return ok;
1703}
1704
1705
Fred Drake71137082001-01-07 05:59:59 +00001706/* dotted_name: NAME ("." NAME)*
1707 */
1708static int
1709validate_dotted_name(node *tree)
1710{
1711 int nch = NCH(tree);
1712 int res = (validate_ntype(tree, dotted_name)
1713 && is_odd(nch)
1714 && validate_name(CHILD(tree, 0), NULL));
1715 int i;
1716
1717 for (i = 1; res && (i < nch); i += 2) {
1718 res = (validate_dot(CHILD(tree, i))
1719 && validate_name(CHILD(tree, i+1), NULL));
1720 }
1721 return res;
1722}
1723
1724
Fred Drakecff283c2000-08-21 22:24:43 +00001725/* dotted_as_name: dotted_name [NAME NAME]
1726 */
1727static int
1728validate_dotted_as_name(node *tree)
1729{
1730 int nch = NCH(tree);
1731 int res = validate_ntype(tree, dotted_as_name);
1732
1733 if (res) {
1734 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001735 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001736 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001737 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001738 && validate_name(CHILD(tree, 1), "as")
1739 && validate_name(CHILD(tree, 2), NULL));
1740 else {
1741 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001742 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001743 }
1744 }
1745 return res;
1746}
1747
1748
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001749/* dotted_as_name (',' dotted_as_name)* */
1750static int
1751validate_dotted_as_names(node *tree)
1752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 int nch = NCH(tree);
1754 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1755 int i;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 for (i = 1; res && (i < nch); i += 2)
1758 res = (validate_comma(CHILD(tree, i))
1759 && validate_dotted_as_name(CHILD(tree, i + 1)));
1760 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001761}
1762
1763
1764/* import_as_name (',' import_as_name)* [','] */
1765static int
1766validate_import_as_names(node *tree)
1767{
1768 int nch = NCH(tree);
1769 int res = validate_import_as_name(CHILD(tree, 0));
1770 int i;
1771
1772 for (i = 1; res && (i + 1 < nch); i += 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 res = (validate_comma(CHILD(tree, i))
1774 && validate_import_as_name(CHILD(tree, i + 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001775 return (res);
1776}
1777
1778
1779/* 'import' dotted_as_names */
1780static int
1781validate_import_name(node *tree)
1782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 return (validate_ntype(tree, import_name)
1784 && validate_numnodes(tree, 2, "import_name")
1785 && validate_name(CHILD(tree, 0), "import")
1786 && validate_dotted_as_names(CHILD(tree, 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001787}
1788
Mark Dickinsonfeb3b752010-07-04 18:38:57 +00001789/* Helper function to count the number of leading dots (or ellipsis tokens) in
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001790 * 'from ...module import name'
1791 */
1792static int
1793count_from_dots(node *tree)
1794{
Mark Dickinsonfeb3b752010-07-04 18:38:57 +00001795 int i;
1796 for (i = 1; i < NCH(tree); i++)
1797 if (TYPE(CHILD(tree, i)) != DOT && TYPE(CHILD(tree, i)) != ELLIPSIS)
1798 break;
1799 return i - 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001800}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001801
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +00001802/* import_from: ('from' ('.'* dotted_name | '.'+)
1803 * 'import' ('*' | '(' import_as_names ')' | import_as_names))
Guido van Rossum3d602e31996-07-21 02:33:56 +00001804 */
Guido van Rossum47478871996-08-21 14:32:37 +00001805static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001806validate_import_from(node *tree)
1807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 int nch = NCH(tree);
1809 int ndots = count_from_dots(tree);
1810 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1811 int offset = ndots + havename;
1812 int res = validate_ntype(tree, import_from)
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +00001813 && (offset >= 1)
1814 && (nch >= 3 + offset)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 && validate_name(CHILD(tree, 0), "from")
1816 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1817 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1820 res = ((nch == offset + 5)
1821 && validate_lparen(CHILD(tree, offset + 2))
1822 && validate_import_as_names(CHILD(tree, offset + 3))
1823 && validate_rparen(CHILD(tree, offset + 4)));
1824 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1825 res = validate_import_as_names(CHILD(tree, offset + 2));
1826 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001827}
1828
1829
1830/* import_stmt: import_name | import_from */
1831static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001832validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001833{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001834 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001835 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001836
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001837 if (res) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 if (ntype == import_name || ntype == import_from)
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001841 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001842 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001843 res = 0;
1844 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001845 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001846 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001847 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001848 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001849 PyErr_Format(parser_error,
1850 "Unrecognized child node of import_stmt: %d.",
1851 TYPE(CHILD(tree, 0)));
1852 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001853 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001854}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001855
1856
Mark Dickinson407b3bd2012-04-29 22:18:31 +01001857/* global_stmt:
1858 *
1859 * 'global' NAME (',' NAME)*
1860 */
Guido van Rossum47478871996-08-21 14:32:37 +00001861static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001862validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001863{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001864 int j;
1865 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001866 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001867 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001868
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001869 if (!res && !PyErr_Occurred())
1870 err_string("illegal global statement");
1871
Guido van Rossum3d602e31996-07-21 02:33:56 +00001872 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001873 res = (validate_name(CHILD(tree, 0), "global")
1874 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001875 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001876 res = (validate_comma(CHILD(tree, j))
1877 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001878
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001879 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001880}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001881
Mark Dickinson407b3bd2012-04-29 22:18:31 +01001882/* nonlocal_stmt:
1883 *
1884 * 'nonlocal' NAME (',' NAME)*
1885 */
1886static int
1887validate_nonlocal_stmt(node *tree)
1888{
1889 int j;
1890 int nch = NCH(tree);
1891 int res = (validate_ntype(tree, nonlocal_stmt)
1892 && is_even(nch) && (nch >= 2));
1893
1894 if (!res && !PyErr_Occurred())
1895 err_string("illegal nonlocal statement");
1896
1897 if (res)
1898 res = (validate_name(CHILD(tree, 0), "nonlocal")
1899 && validate_ntype(CHILD(tree, 1), NAME));
1900 for (j = 2; res && (j < nch); j += 2)
1901 res = (validate_comma(CHILD(tree, j))
1902 && validate_ntype(CHILD(tree, j + 1), NAME));
1903
1904 return res;
1905}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001906
Guido van Rossum925e5471997-04-02 05:32:13 +00001907/* assert_stmt:
1908 *
1909 * 'assert' test [',' test]
1910 */
1911static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001912validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001913{
1914 int nch = NCH(tree);
1915 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001916 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001917 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001918 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001919
1920 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001921 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001922 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001923 res = (validate_comma(CHILD(tree, 2))
1924 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001925
1926 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001927}
Guido van Rossum925e5471997-04-02 05:32:13 +00001928
1929
Guido van Rossum47478871996-08-21 14:32:37 +00001930static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001931validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001932{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001933 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001934 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001935 && ((nch == 4) || (nch == 7))
1936 && validate_name(CHILD(tree, 0), "while")
1937 && validate_test(CHILD(tree, 1))
1938 && validate_colon(CHILD(tree, 2))
1939 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001940
Guido van Rossum3d602e31996-07-21 02:33:56 +00001941 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001942 res = (validate_name(CHILD(tree, 4), "else")
1943 && validate_colon(CHILD(tree, 5))
1944 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001945
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001946 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001947}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001948
1949
Guido van Rossum47478871996-08-21 14:32:37 +00001950static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001951validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001952{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001953 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001954 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001955 && ((nch == 6) || (nch == 9))
1956 && validate_name(CHILD(tree, 0), "for")
1957 && validate_exprlist(CHILD(tree, 1))
1958 && validate_name(CHILD(tree, 2), "in")
1959 && validate_testlist(CHILD(tree, 3))
1960 && validate_colon(CHILD(tree, 4))
1961 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001962
Guido van Rossum3d602e31996-07-21 02:33:56 +00001963 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001964 res = (validate_name(CHILD(tree, 6), "else")
1965 && validate_colon(CHILD(tree, 7))
1966 && validate_suite(CHILD(tree, 8)));
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 Rossum3d602e31996-07-21 02:33:56 +00001972/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001973 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Georg Brandleee31162008-12-07 15:15:22 +00001974 ['finally' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001975 * | 'try' ':' suite 'finally' ':' suite
1976 *
1977 */
Guido van Rossum47478871996-08-21 14:32:37 +00001978static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001979validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001980{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001981 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001982 int pos = 3;
1983 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001984 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001985
1986 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001987 res = (validate_name(CHILD(tree, 0), "try")
1988 && validate_colon(CHILD(tree, 1))
1989 && validate_suite(CHILD(tree, 2))
1990 && validate_colon(CHILD(tree, nch - 2))
1991 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001992 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001993 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001994 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1995 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001996
1997 PyErr_Format(parser_error,
1998 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001999 }
Georg Brandleee31162008-12-07 15:15:22 +00002000 /* Handle try/finally statement */
2001 if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
2002 (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
2003 res = (validate_numnodes(tree, 6, "try/finally")
2004 && validate_colon(CHILD(tree, 4))
2005 && validate_suite(CHILD(tree, 5)));
2006 return (res);
2007 }
2008 /* try/except statement: skip past except_clause sections */
Antoine Pitrou37d1c182009-05-14 21:54:00 +00002009 while (res && pos < nch && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002010 res = (validate_except_clause(CHILD(tree, pos))
2011 && validate_colon(CHILD(tree, pos + 1))
2012 && validate_suite(CHILD(tree, pos + 2)));
2013 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002014 }
Georg Brandleee31162008-12-07 15:15:22 +00002015 /* skip else clause */
Antoine Pitrou37d1c182009-05-14 21:54:00 +00002016 if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) &&
Georg Brandleee31162008-12-07 15:15:22 +00002017 (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
2018 res = (validate_colon(CHILD(tree, pos + 1))
2019 && validate_suite(CHILD(tree, pos + 2)));
2020 pos += 3;
2021 }
2022 if (res && pos < nch) {
2023 /* last clause must be a finally */
2024 res = (validate_name(CHILD(tree, pos), "finally")
2025 && validate_numnodes(tree, pos + 3, "try/except/finally")
2026 && validate_colon(CHILD(tree, pos + 1))
2027 && validate_suite(CHILD(tree, pos + 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002028 }
2029 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002030}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002031
2032
Guido van Rossum47478871996-08-21 14:32:37 +00002033static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002034validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002035{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002036 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002037 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00002038 && ((nch == 1) || (nch == 2) || (nch == 4))
2039 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002040
Guido van Rossum3d602e31996-07-21 02:33:56 +00002041 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002042 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002043 if (res && (nch == 4))
Guido van Rossumb940e112007-01-10 16:19:56 +00002044 res = (validate_name(CHILD(tree, 2), "as")
2045 && validate_ntype(CHILD(tree, 3), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002046
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002047 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002048}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002049
2050
Guido van Rossum47478871996-08-21 14:32:37 +00002051static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002052validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002053{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002054 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002055 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002056
Guido van Rossum3d602e31996-07-21 02:33:56 +00002057 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002058 res = ((nch == 1)
2059 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002060 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002061 res = validate_or_test(CHILD(tree, 0));
2062 res = (res && (nch == 1 || (nch == 5 &&
2063 validate_name(CHILD(tree, 1), "if") &&
2064 validate_or_test(CHILD(tree, 2)) &&
2065 validate_name(CHILD(tree, 3), "else") &&
2066 validate_test(CHILD(tree, 4)))));
2067 }
2068 return (res);
2069}
2070
2071static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002072validate_test_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002073{
2074 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002075 int res = validate_ntype(tree, test_nocond) && (nch == 1);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002076
Nick Coghlan650f0d02007-04-15 12:05:43 +00002077 if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
2078 res = (validate_lambdef_nocond(CHILD(tree, 0)));
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002079 else if (res) {
2080 res = (validate_or_test(CHILD(tree, 0)));
2081 }
2082 return (res);
2083}
2084
2085static int
2086validate_or_test(node *tree)
2087{
2088 int nch = NCH(tree);
2089 int res = validate_ntype(tree, or_test) && is_odd(nch);
2090
2091 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002092 int pos;
2093 res = validate_and_test(CHILD(tree, 0));
2094 for (pos = 1; res && (pos < nch); pos += 2)
2095 res = (validate_name(CHILD(tree, pos), "or")
2096 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002097 }
2098 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002099}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002100
2101
Guido van Rossum47478871996-08-21 14:32:37 +00002102static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002103validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002104{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002105 int pos;
2106 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002107 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002108 && is_odd(nch)
2109 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002110
Guido van Rossum3d602e31996-07-21 02:33:56 +00002111 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002112 res = (validate_name(CHILD(tree, pos), "and")
2113 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002114
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002115 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002116}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002117
2118
Guido van Rossum47478871996-08-21 14:32:37 +00002119static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002120validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002121{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002122 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002123 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002124
Guido van Rossum3d602e31996-07-21 02:33:56 +00002125 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002126 if (nch == 2)
2127 res = (validate_name(CHILD(tree, 0), "not")
2128 && validate_not_test(CHILD(tree, 1)));
2129 else if (nch == 1)
2130 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002131 }
2132 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002133}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002134
2135
Guido van Rossum47478871996-08-21 14:32:37 +00002136static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002137validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002138{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002139 int pos;
2140 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002141 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002142 && is_odd(nch)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002143 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002144
Guido van Rossum3d602e31996-07-21 02:33:56 +00002145 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002146 res = (validate_comp_op(CHILD(tree, pos))
Benjamin Peterson4905e802009-09-27 02:43:28 +00002147 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002148
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002149 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002150}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002151
2152
Guido van Rossum47478871996-08-21 14:32:37 +00002153static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002154validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002155{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002156 int res = 0;
2157 int nch = NCH(tree);
2158
Guido van Rossum3d602e31996-07-21 02:33:56 +00002159 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002160 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002161 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002162 /*
2163 * Only child will be a terminal with a well-defined symbolic name
2164 * or a NAME with a string of either 'is' or 'in'
2165 */
2166 tree = CHILD(tree, 0);
2167 switch (TYPE(tree)) {
2168 case LESS:
2169 case GREATER:
2170 case EQEQUAL:
2171 case EQUAL:
2172 case LESSEQUAL:
2173 case GREATEREQUAL:
2174 case NOTEQUAL:
2175 res = 1;
2176 break;
2177 case NAME:
2178 res = ((strcmp(STR(tree), "in") == 0)
2179 || (strcmp(STR(tree), "is") == 0));
2180 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002181 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002182 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002183 }
2184 break;
2185 default:
Fred Drake661ea262000-10-24 19:57:45 +00002186 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002187 break;
2188 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002189 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002190 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002191 res = (validate_ntype(CHILD(tree, 0), NAME)
2192 && validate_ntype(CHILD(tree, 1), NAME)
2193 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2194 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2195 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2196 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2197 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002198 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002199 }
2200 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002201}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002202
2203
Guido van Rossum47478871996-08-21 14:32:37 +00002204static int
Guido van Rossum0368b722007-05-11 16:50:42 +00002205validate_star_expr(node *tree)
2206{
2207 int res = validate_ntype(tree, star_expr);
2208 if (!res) return res;
Benjamin Peterson4905e802009-09-27 02:43:28 +00002209 if (!validate_numnodes(tree, 2, "star_expr"))
2210 return 0;
2211 return validate_ntype(CHILD(tree, 0), STAR) && \
2212 validate_expr(CHILD(tree, 1));
Guido van Rossum0368b722007-05-11 16:50:42 +00002213}
2214
2215
2216static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002217validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002218{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002219 int j;
2220 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002221 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002222 && is_odd(nch)
2223 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002224
Guido van Rossum3d602e31996-07-21 02:33:56 +00002225 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002226 res = (validate_xor_expr(CHILD(tree, j))
2227 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002228
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002229 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002230}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002231
2232
Guido van Rossum47478871996-08-21 14:32:37 +00002233static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002234validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002235{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002236 int j;
2237 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002238 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002239 && is_odd(nch)
2240 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002241
Guido van Rossum3d602e31996-07-21 02:33:56 +00002242 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002243 res = (validate_circumflex(CHILD(tree, j - 1))
2244 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002245
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002246 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002247}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002248
2249
Guido van Rossum47478871996-08-21 14:32:37 +00002250static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002251validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002252{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002253 int pos;
2254 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002255 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002256 && is_odd(nch)
2257 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002258
Guido van Rossum3d602e31996-07-21 02:33:56 +00002259 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002260 res = (validate_ampersand(CHILD(tree, pos))
2261 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002262
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002263 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002264}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002265
2266
2267static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002268validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002269 {
2270 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002271 int nch = NCH(tree);
2272 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002273 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002274
Guido van Rossum3d602e31996-07-21 02:33:56 +00002275 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002276 if (TYPE(CHILD(tree, pos)) != op1)
2277 res = validate_ntype(CHILD(tree, pos), op2);
2278 if (res)
2279 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002280 }
2281 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002282}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002283
2284
Guido van Rossum47478871996-08-21 14:32:37 +00002285static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002286validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002287{
2288 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002289 && validate_chain_two_ops(tree, validate_arith_expr,
2290 LEFTSHIFT, RIGHTSHIFT));
2291}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002292
2293
Guido van Rossum47478871996-08-21 14:32:37 +00002294static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002295validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002296{
2297 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002298 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2299}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002300
2301
Guido van Rossum47478871996-08-21 14:32:37 +00002302static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002303validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002304{
2305 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002306 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002307 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002308 && is_odd(nch)
2309 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002310
Guido van Rossum3d602e31996-07-21 02:33:56 +00002311 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002312 res = (((TYPE(CHILD(tree, pos)) == STAR)
2313 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002314 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002315 || (TYPE(CHILD(tree, pos)) == PERCENT))
2316 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002317
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002318 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002319}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002320
2321
Guido van Rossum3d602e31996-07-21 02:33:56 +00002322/* factor:
2323 *
2324 * factor: ('+'|'-'|'~') factor | power
2325 */
Guido van Rossum47478871996-08-21 14:32:37 +00002326static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002327validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002328{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002329 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002330 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002331 && (((nch == 2)
2332 && ((TYPE(CHILD(tree, 0)) == PLUS)
2333 || (TYPE(CHILD(tree, 0)) == MINUS)
2334 || (TYPE(CHILD(tree, 0)) == TILDE))
2335 && validate_factor(CHILD(tree, 1)))
2336 || ((nch == 1)
2337 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002338 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002339}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002340
2341
Guido van Rossum3d602e31996-07-21 02:33:56 +00002342/* power:
2343 *
2344 * power: atom trailer* ('**' factor)*
2345 */
Guido van Rossum47478871996-08-21 14:32:37 +00002346static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002347validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002348{
2349 int pos = 1;
2350 int nch = NCH(tree);
2351 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002352 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002353
2354 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002355 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002356 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002357 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002358 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002359 return (0);
2360 }
2361 for ( ; res && (pos < (nch - 1)); pos += 2)
2362 res = (validate_doublestar(CHILD(tree, pos))
2363 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002364 }
2365 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002366}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002367
2368
Guido van Rossum47478871996-08-21 14:32:37 +00002369static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002370validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002371{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002372 int pos;
2373 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002374 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002375
Fred Drakecff283c2000-08-21 22:24:43 +00002376 if (res && nch < 1)
2377 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002378 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002379 switch (TYPE(CHILD(tree, 0))) {
2380 case LPAR:
2381 res = ((nch <= 3)
2382 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002383
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002384 if (res && (nch == 3)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 if (TYPE(CHILD(tree, 1))==yield_expr)
2386 res = validate_yield_expr(CHILD(tree, 1));
2387 else
2388 res = validate_testlist_comp(CHILD(tree, 1));
2389 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002390 break;
2391 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002392 if (nch == 2)
2393 res = validate_ntype(CHILD(tree, 1), RSQB);
2394 else if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002395 res = (validate_testlist_comp(CHILD(tree, 1))
Fred Drakecff283c2000-08-21 22:24:43 +00002396 && validate_ntype(CHILD(tree, 2), RSQB));
2397 else {
2398 res = 0;
2399 err_string("illegal list display atom");
2400 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002401 break;
2402 case LBRACE:
2403 res = ((nch <= 3)
2404 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002405
Fred Drakeff9ea482000-04-19 13:54:15 +00002406 if (res && (nch == 3))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002407 res = validate_dictorsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002408 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002409 case NAME:
2410 case NUMBER:
Mark Dickinsonda029fb2012-05-07 17:24:04 +01002411 case ELLIPSIS:
Fred Drakeff9ea482000-04-19 13:54:15 +00002412 res = (nch == 1);
2413 break;
2414 case STRING:
2415 for (pos = 1; res && (pos < nch); ++pos)
2416 res = validate_ntype(CHILD(tree, pos), STRING);
2417 break;
2418 default:
2419 res = 0;
2420 break;
2421 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002422 }
2423 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002424}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002425
2426
Nick Coghlan650f0d02007-04-15 12:05:43 +00002427/* testlist_comp:
2428 * test ( comp_for | (',' test)* [','] )
Fred Drake85bf3bb2000-08-23 15:35:26 +00002429 */
Fred Drakecff283c2000-08-21 22:24:43 +00002430static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002431validate_testlist_comp(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00002432{
2433 int nch = NCH(tree);
2434 int ok = nch;
2435
2436 if (nch == 0)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002437 err_string("missing child nodes of testlist_comp");
2438 else {
Benjamin Peterson4905e802009-09-27 02:43:28 +00002439 ok = validate_test_or_star_expr(CHILD(tree, 0));
Nick Coghlan650f0d02007-04-15 12:05:43 +00002440 }
Fred Drakecff283c2000-08-21 22:24:43 +00002441
2442 /*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002443 * comp_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002444 */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002445 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2446 ok = validate_comp_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002447 else {
2448 /* (',' test)* [','] */
2449 int i = 1;
2450 while (ok && nch - i >= 2) {
2451 ok = (validate_comma(CHILD(tree, i))
Benjamin Peterson4905e802009-09-27 02:43:28 +00002452 && validate_test_or_star_expr(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002453 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002454 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002455 if (ok && i == nch-1)
2456 ok = validate_comma(CHILD(tree, i));
2457 else if (i != nch) {
2458 ok = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002459 err_string("illegal trailing nodes for testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002460 }
2461 }
2462 return ok;
2463}
Fred Drakecff283c2000-08-21 22:24:43 +00002464
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002465/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002466 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002467 */
2468static int
2469validate_decorator(node *tree)
2470{
2471 int ok;
2472 int nch = NCH(tree);
2473 ok = (validate_ntype(tree, decorator) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 (nch == 3 || nch == 5 || nch == 6) &&
2475 validate_at(CHILD(tree, 0)) &&
2476 validate_dotted_name(CHILD(tree, 1)) &&
2477 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002478
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002479 if (ok && nch != 3) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 ok = (validate_lparen(CHILD(tree, 2)) &&
2481 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 if (ok && nch == 6)
2484 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002485 }
2486
2487 return ok;
2488}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002489
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002490/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002491 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002492 */
2493static int
2494validate_decorators(node *tree)
2495{
Guido van Rossumfc158e22007-11-15 19:17:28 +00002496 int i, nch, ok;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002497 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002498 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002499
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002500 for (i = 0; ok && i < nch; ++i)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002502
2503 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002504}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002505
Georg Brandl0c315622009-05-25 21:10:36 +00002506/* with_item:
2507 * test ['as' expr]
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002508 */
2509static int
Georg Brandl0c315622009-05-25 21:10:36 +00002510validate_with_item(node *tree)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002511{
2512 int nch = NCH(tree);
Georg Brandl0c315622009-05-25 21:10:36 +00002513 int ok = (validate_ntype(tree, with_item)
2514 && (nch == 1 || nch == 3)
2515 && validate_test(CHILD(tree, 0)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 if (ok && nch == 3)
Georg Brandl0c315622009-05-25 21:10:36 +00002517 ok = (validate_name(CHILD(tree, 1), "as")
2518 && validate_expr(CHILD(tree, 2)));
2519 return ok;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002520}
2521
Georg Brandl0c315622009-05-25 21:10:36 +00002522/* with_stmt:
2523 * 0 1 ... -2 -1
2524 * 'with' with_item (',' with_item)* ':' suite
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002525 */
2526static int
2527validate_with_stmt(node *tree)
2528{
Georg Brandl0c315622009-05-25 21:10:36 +00002529 int i;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002530 int nch = NCH(tree);
2531 int ok = (validate_ntype(tree, with_stmt)
Georg Brandl0c315622009-05-25 21:10:36 +00002532 && (nch % 2 == 0)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002533 && validate_name(CHILD(tree, 0), "with")
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002534 && validate_colon(RCHILD(tree, -2))
2535 && validate_suite(RCHILD(tree, -1)));
Georg Brandl0c315622009-05-25 21:10:36 +00002536 for (i = 1; ok && i < nch - 2; i += 2)
2537 ok = validate_with_item(CHILD(tree, i));
2538 return ok;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002539}
2540
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01002541/* funcdef: 'def' NAME parameters ['->' test] ':' suite */
2542
Guido van Rossum47478871996-08-21 14:32:37 +00002543static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002544validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002545{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002546 int nch = NCH(tree);
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01002547 int res = validate_ntype(tree, funcdef);
2548 if (res) {
2549 if (nch == 5) {
2550 res = (validate_name(CHILD(tree, 0), "def")
2551 && validate_ntype(CHILD(tree, 1), NAME)
2552 && validate_parameters(CHILD(tree, 2))
2553 && validate_colon(CHILD(tree, 3))
2554 && validate_suite(CHILD(tree, 4)));
2555 }
2556 else if (nch == 7) {
2557 res = (validate_name(CHILD(tree, 0), "def")
2558 && validate_ntype(CHILD(tree, 1), NAME)
2559 && validate_parameters(CHILD(tree, 2))
2560 && validate_rarrow(CHILD(tree, 3))
2561 && validate_test(CHILD(tree, 4))
2562 && validate_colon(CHILD(tree, 5))
2563 && validate_suite(CHILD(tree, 6)));
2564 }
2565 else {
2566 res = 0;
2567 err_string("illegal number of children for funcdef");
2568 }
2569 }
2570 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00002571}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002572
2573
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002574/* decorated
2575 * decorators (classdef | funcdef)
2576 */
2577static int
2578validate_decorated(node *tree)
2579{
Mark Dickinson2bd61a92010-07-04 16:37:31 +00002580 int nch = NCH(tree);
2581 int ok = (validate_ntype(tree, decorated)
2582 && (nch == 2)
2583 && validate_decorators(RCHILD(tree, -2)));
2584 if (TYPE(RCHILD(tree, -1)) == funcdef)
2585 ok = ok && validate_funcdef(RCHILD(tree, -1));
2586 else
2587 ok = ok && validate_class(RCHILD(tree, -1));
2588 return ok;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002589}
2590
Guido van Rossum47478871996-08-21 14:32:37 +00002591static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002592validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002593{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002594 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002595 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002596 && ((nch == 3) || (nch == 4))
2597 && validate_name(CHILD(tree, 0), "lambda")
2598 && validate_colon(CHILD(tree, nch - 2))
2599 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002600
Guido van Rossum3d602e31996-07-21 02:33:56 +00002601 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002602 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002603 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002604 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002605
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002606 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002607}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002608
2609
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002610static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002611validate_lambdef_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002612{
2613 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002614 int res = (validate_ntype(tree, lambdef_nocond)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002615 && ((nch == 3) || (nch == 4))
2616 && validate_name(CHILD(tree, 0), "lambda")
2617 && validate_colon(CHILD(tree, nch - 2))
2618 && validate_test(CHILD(tree, nch - 1)));
2619
2620 if (res && (nch == 4))
2621 res = validate_varargslist(CHILD(tree, 1));
2622 else if (!res && !PyErr_Occurred())
Nick Coghlan650f0d02007-04-15 12:05:43 +00002623 (void) validate_numnodes(tree, 3, "lambdef_nocond");
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002624
2625 return (res);
2626}
2627
2628
Guido van Rossum3d602e31996-07-21 02:33:56 +00002629/* arglist:
2630 *
Fred Drakecff283c2000-08-21 22:24:43 +00002631 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002632 */
Guido van Rossum47478871996-08-21 14:32:37 +00002633static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002634validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002635{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002636 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002637 int i = 0;
2638 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002639
2640 if (nch <= 0)
2641 /* raise the right error from having an invalid number of children */
2642 return validate_numnodes(tree, nch + 1, "arglist");
2643
Raymond Hettinger354433a2004-05-19 08:20:33 +00002644 if (nch > 1) {
2645 for (i=0; i<nch; i++) {
2646 if (TYPE(CHILD(tree, i)) == argument) {
2647 node *ch = CHILD(tree, i);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002648 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
Raymond Hettinger354433a2004-05-19 08:20:33 +00002649 err_string("need '(', ')' for generator expression");
2650 return 0;
2651 }
2652 }
2653 }
2654 }
2655
Fred Drakecff283c2000-08-21 22:24:43 +00002656 while (ok && nch-i >= 2) {
2657 /* skip leading (argument ',') */
2658 ok = (validate_argument(CHILD(tree, i))
2659 && validate_comma(CHILD(tree, i+1)));
2660 if (ok)
2661 i += 2;
2662 else
2663 PyErr_Clear();
2664 }
2665 ok = 1;
2666 if (nch-i > 0) {
2667 /*
2668 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002669 */
Fred Drakecff283c2000-08-21 22:24:43 +00002670 int sym = TYPE(CHILD(tree, i));
2671
2672 if (sym == argument) {
2673 ok = validate_argument(CHILD(tree, i));
2674 if (ok && i+1 != nch) {
2675 err_string("illegal arglist specification"
2676 " (extra stuff on end)");
2677 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002678 }
Fred Drakecff283c2000-08-21 22:24:43 +00002679 }
2680 else if (sym == STAR) {
2681 ok = validate_star(CHILD(tree, i));
2682 if (ok && (nch-i == 2))
2683 ok = validate_test(CHILD(tree, i+1));
2684 else if (ok && (nch-i == 5))
2685 ok = (validate_test(CHILD(tree, i+1))
2686 && validate_comma(CHILD(tree, i+2))
2687 && validate_doublestar(CHILD(tree, i+3))
2688 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002689 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002690 err_string("illegal use of '*' in arglist");
2691 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002692 }
2693 }
Fred Drakecff283c2000-08-21 22:24:43 +00002694 else if (sym == DOUBLESTAR) {
2695 if (nch-i == 2)
2696 ok = (validate_doublestar(CHILD(tree, i))
2697 && validate_test(CHILD(tree, i+1)));
2698 else {
2699 err_string("illegal use of '**' in arglist");
2700 ok = 0;
2701 }
2702 }
2703 else {
2704 err_string("illegal arglist specification");
2705 ok = 0;
2706 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002707 }
2708 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002709}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002710
2711
2712
2713/* argument:
2714 *
Nick Coghlan650f0d02007-04-15 12:05:43 +00002715 * [test '='] test [comp_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002716 */
Guido van Rossum47478871996-08-21 14:32:37 +00002717static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002718validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002719{
2720 int nch = NCH(tree);
2721 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002722 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002723 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002724
Raymond Hettinger354433a2004-05-19 08:20:33 +00002725 if (res && (nch == 2))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002726 res = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002727 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002728 res = (validate_equal(CHILD(tree, 1))
2729 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002730
2731 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002732}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002733
2734
2735
2736/* trailer:
2737 *
Guido van Rossum47478871996-08-21 14:32:37 +00002738 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002739 */
Guido van Rossum47478871996-08-21 14:32:37 +00002740static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002741validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002742{
2743 int nch = NCH(tree);
2744 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002745
2746 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002747 switch (TYPE(CHILD(tree, 0))) {
2748 case LPAR:
2749 res = validate_rparen(CHILD(tree, nch - 1));
2750 if (res && (nch == 3))
2751 res = validate_arglist(CHILD(tree, 1));
2752 break;
2753 case LSQB:
2754 res = (validate_numnodes(tree, 3, "trailer")
2755 && validate_subscriptlist(CHILD(tree, 1))
2756 && validate_ntype(CHILD(tree, 2), RSQB));
2757 break;
2758 case DOT:
2759 res = (validate_numnodes(tree, 2, "trailer")
2760 && validate_ntype(CHILD(tree, 1), NAME));
2761 break;
2762 default:
2763 res = 0;
2764 break;
2765 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002766 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002767 else {
2768 (void) validate_numnodes(tree, 2, "trailer");
2769 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002770 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002771}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002772
2773
Guido van Rossum47478871996-08-21 14:32:37 +00002774/* subscriptlist:
2775 *
2776 * subscript (',' subscript)* [',']
2777 */
2778static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002779validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002780{
2781 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002782 validate_subscript, "subscriptlist"));
2783}
Guido van Rossum47478871996-08-21 14:32:37 +00002784
2785
Guido van Rossum3d602e31996-07-21 02:33:56 +00002786/* subscript:
2787 *
Guido van Rossum47478871996-08-21 14:32:37 +00002788 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002789 */
Guido van Rossum47478871996-08-21 14:32:37 +00002790static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002791validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002792{
Guido van Rossum47478871996-08-21 14:32:37 +00002793 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002794 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002795 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002796
Guido van Rossum47478871996-08-21 14:32:37 +00002797 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002798 if (!PyErr_Occurred())
2799 err_string("invalid number of arguments for subscript node");
2800 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002801 }
Guido van Rossum47478871996-08-21 14:32:37 +00002802 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002803 /* take care of ('.' '.' '.') possibility */
2804 return (validate_numnodes(tree, 3, "subscript")
2805 && validate_dot(CHILD(tree, 0))
2806 && validate_dot(CHILD(tree, 1))
2807 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002808 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002809 if (TYPE(CHILD(tree, 0)) == test)
2810 res = validate_test(CHILD(tree, 0));
2811 else
2812 res = validate_colon(CHILD(tree, 0));
2813 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002814 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002815 /* Must be [test] ':' [test] [sliceop],
2816 * but at least one of the optional components will
2817 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002818 */
2819 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002820 res = validate_test(CHILD(tree, 0));
2821 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002822 }
2823 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002824 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002825 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002826 int rem = nch - ++offset;
2827 if (rem) {
2828 if (TYPE(CHILD(tree, offset)) == test) {
2829 res = validate_test(CHILD(tree, offset));
2830 ++offset;
2831 --rem;
2832 }
2833 if (res && rem)
2834 res = validate_sliceop(CHILD(tree, offset));
2835 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002836 }
2837 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002838}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002839
2840
Guido van Rossum47478871996-08-21 14:32:37 +00002841static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002842validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002843{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002844 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002845 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002846 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002847 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002848 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002849 }
Guido van Rossum47478871996-08-21 14:32:37 +00002850 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002851 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002852 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002853 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002854
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002855 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002856}
Guido van Rossum47478871996-08-21 14:32:37 +00002857
2858
2859static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00002860validate_test_or_star_expr(node *n)
2861{
2862 if (TYPE(n) == test)
2863 return validate_test(n);
2864 return validate_star_expr(n);
2865}
2866
2867static int
2868validate_expr_or_star_expr(node *n)
2869{
2870 if (TYPE(n) == expr)
2871 return validate_expr(n);
2872 return validate_star_expr(n);
2873}
2874
2875
2876static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002877validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002878{
2879 return (validate_repeating_list(tree, exprlist,
Benjamin Peterson4905e802009-09-27 02:43:28 +00002880 validate_expr_or_star_expr, "exprlist"));
Fred Drakeff9ea482000-04-19 13:54:15 +00002881}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002882
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002883/*
2884 * dictorsetmaker:
2885 *
2886 * (test ':' test (comp_for | (',' test ':' test)* [','])) |
2887 * (test (comp_for | (',' test)* [',']))
2888 */
Guido van Rossum47478871996-08-21 14:32:37 +00002889static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002890validate_dictorsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002891{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002892 int nch = NCH(tree);
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002893 int res;
2894 int i = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002895
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002896 res = validate_ntype(tree, dictorsetmaker);
2897 if (!res)
2898 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002899
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002900 if (nch - i < 1) {
2901 (void) validate_numnodes(tree, 1, "dictorsetmaker");
2902 return 0;
2903 }
2904
2905 res = validate_test(CHILD(tree, i++));
2906 if (!res)
2907 return 0;
2908
2909 if (nch - i >= 2 && TYPE(CHILD(tree, i)) == COLON) {
2910 /* Dictionary display or dictionary comprehension. */
2911 res = (validate_colon(CHILD(tree, i++))
2912 && validate_test(CHILD(tree, i++)));
2913 if (!res)
2914 return 0;
2915
2916 if (nch - i >= 1 && TYPE(CHILD(tree, i)) == comp_for) {
2917 /* Dictionary comprehension. */
2918 res = validate_comp_for(CHILD(tree, i++));
2919 if (!res)
2920 return 0;
2921 }
2922 else {
2923 /* Dictionary display. */
2924 while (nch - i >= 4) {
2925 res = (validate_comma(CHILD(tree, i++))
2926 && validate_test(CHILD(tree, i++))
2927 && validate_colon(CHILD(tree, i++))
2928 && validate_test(CHILD(tree, i++)));
2929 if (!res)
2930 return 0;
2931 }
2932 if (nch - i == 1) {
2933 res = validate_comma(CHILD(tree, i++));
2934 if (!res)
2935 return 0;
2936 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002937 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002938 }
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002939 else {
2940 /* Set display or set comprehension. */
2941 if (nch - i >= 1 && TYPE(CHILD(tree, i)) == comp_for) {
2942 /* Set comprehension. */
2943 res = validate_comp_for(CHILD(tree, i++));
2944 if (!res)
2945 return 0;
2946 }
2947 else {
2948 /* Set display. */
2949 while (nch - i >= 2) {
2950 res = (validate_comma(CHILD(tree, i++))
2951 && validate_test(CHILD(tree, i++)));
2952 if (!res)
2953 return 0;
2954 }
2955 if (nch - i == 1) {
2956 res = validate_comma(CHILD(tree, i++));
2957 if (!res)
2958 return 0;
2959 }
2960 }
2961 }
2962
2963 if (nch - i > 0) {
2964 err_string("Illegal trailing nodes for dictorsetmaker.");
2965 return 0;
2966 }
2967
2968 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00002969}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002970
2971
Guido van Rossum47478871996-08-21 14:32:37 +00002972static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002973validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002974{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002975 int pos;
2976 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002977 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002978 && (nch >= 2)
2979 && validate_testlist(CHILD(tree, 0))
2980 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002981
Guido van Rossum3d602e31996-07-21 02:33:56 +00002982 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002983 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002984
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002985 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002986}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002987
2988
Guido van Rossum47478871996-08-21 14:32:37 +00002989static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002990validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002991{
Fred Drakeff9ea482000-04-19 13:54:15 +00002992 int nch = 0; /* num. children on current node */
2993 int res = 1; /* result value */
2994 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002995
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002996 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002997 nch = NCH(tree);
2998 next = 0;
2999 switch (TYPE(tree)) {
3000 /*
3001 * Definition nodes.
3002 */
3003 case funcdef:
3004 res = validate_funcdef(tree);
3005 break;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00003006 case with_stmt:
3007 res = validate_with_stmt(tree);
3008 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003009 case classdef:
3010 res = validate_class(tree);
3011 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 case decorated:
3013 res = validate_decorated(tree);
3014 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003015 /*
3016 * "Trivial" parse tree nodes.
3017 * (Why did I call these trivial?)
3018 */
3019 case stmt:
3020 res = validate_stmt(tree);
3021 break;
3022 case small_stmt:
3023 /*
Mark Dickinson407b3bd2012-04-29 22:18:31 +01003024 * expr_stmt | del_stmt | pass_stmt | flow_stmt |
3025 * import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00003026 */
3027 res = validate_small_stmt(tree);
3028 break;
3029 case flow_stmt:
3030 res = (validate_numnodes(tree, 1, "flow_stmt")
3031 && ((TYPE(CHILD(tree, 0)) == break_stmt)
3032 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00003033 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00003034 || (TYPE(CHILD(tree, 0)) == return_stmt)
3035 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
3036 if (res)
3037 next = CHILD(tree, 0);
3038 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00003039 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003040 break;
Fred Drake02126f22001-07-17 02:59:15 +00003041 case yield_stmt:
3042 res = validate_yield_stmt(tree);
3043 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003044 /*
3045 * Compound statements.
3046 */
3047 case simple_stmt:
3048 res = validate_simple_stmt(tree);
3049 break;
3050 case compound_stmt:
3051 res = validate_compound_stmt(tree);
3052 break;
3053 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00003054 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00003055 */
3056 case expr_stmt:
3057 res = validate_expr_stmt(tree);
3058 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003059 case del_stmt:
3060 res = validate_del_stmt(tree);
3061 break;
3062 case pass_stmt:
3063 res = (validate_numnodes(tree, 1, "pass")
3064 && validate_name(CHILD(tree, 0), "pass"));
3065 break;
3066 case break_stmt:
3067 res = (validate_numnodes(tree, 1, "break")
3068 && validate_name(CHILD(tree, 0), "break"));
3069 break;
3070 case continue_stmt:
3071 res = (validate_numnodes(tree, 1, "continue")
3072 && validate_name(CHILD(tree, 0), "continue"));
3073 break;
3074 case return_stmt:
3075 res = validate_return_stmt(tree);
3076 break;
3077 case raise_stmt:
3078 res = validate_raise_stmt(tree);
3079 break;
3080 case import_stmt:
3081 res = validate_import_stmt(tree);
3082 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 case import_name:
3084 res = validate_import_name(tree);
3085 break;
3086 case import_from:
3087 res = validate_import_from(tree);
3088 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003089 case global_stmt:
3090 res = validate_global_stmt(tree);
3091 break;
Mark Dickinson407b3bd2012-04-29 22:18:31 +01003092 case nonlocal_stmt:
3093 res = validate_nonlocal_stmt(tree);
3094 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003095 case assert_stmt:
3096 res = validate_assert_stmt(tree);
3097 break;
3098 case if_stmt:
3099 res = validate_if(tree);
3100 break;
3101 case while_stmt:
3102 res = validate_while(tree);
3103 break;
3104 case for_stmt:
3105 res = validate_for(tree);
3106 break;
3107 case try_stmt:
3108 res = validate_try(tree);
3109 break;
3110 case suite:
3111 res = validate_suite(tree);
3112 break;
3113 /*
3114 * Expression nodes.
3115 */
3116 case testlist:
3117 res = validate_testlist(tree);
3118 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003119 case yield_expr:
3120 res = validate_yield_expr(tree);
3121 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003122 case test:
3123 res = validate_test(tree);
3124 break;
3125 case and_test:
3126 res = validate_and_test(tree);
3127 break;
3128 case not_test:
3129 res = validate_not_test(tree);
3130 break;
3131 case comparison:
3132 res = validate_comparison(tree);
3133 break;
3134 case exprlist:
3135 res = validate_exprlist(tree);
3136 break;
3137 case comp_op:
3138 res = validate_comp_op(tree);
3139 break;
3140 case expr:
3141 res = validate_expr(tree);
3142 break;
3143 case xor_expr:
3144 res = validate_xor_expr(tree);
3145 break;
3146 case and_expr:
3147 res = validate_and_expr(tree);
3148 break;
3149 case shift_expr:
3150 res = validate_shift_expr(tree);
3151 break;
3152 case arith_expr:
3153 res = validate_arith_expr(tree);
3154 break;
3155 case term:
3156 res = validate_term(tree);
3157 break;
3158 case factor:
3159 res = validate_factor(tree);
3160 break;
3161 case power:
3162 res = validate_power(tree);
3163 break;
3164 case atom:
3165 res = validate_atom(tree);
3166 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003167
Fred Drakeff9ea482000-04-19 13:54:15 +00003168 default:
3169 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00003170 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003171 res = 0;
3172 break;
3173 }
3174 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003175 }
3176 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003177}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003178
3179
Guido van Rossum47478871996-08-21 14:32:37 +00003180static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003181validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003182{
3183 int res = validate_eval_input(tree);
3184
3185 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003186 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003187
3188 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003189}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003190
3191
Guido van Rossum3d602e31996-07-21 02:33:56 +00003192/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003193 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003194 */
Guido van Rossum47478871996-08-21 14:32:37 +00003195static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003196validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003197{
Fred Drakec2683dd2001-07-17 19:32:05 +00003198 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003199 int nch = NCH(tree) - 1;
3200 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003201 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003202
Fred Drakec2683dd2001-07-17 19:32:05 +00003203 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003204 if (TYPE(CHILD(tree, j)) == stmt)
3205 res = validate_stmt(CHILD(tree, j));
3206 else
3207 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003208 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003209 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003210 * user. Hopefully, this won't be needed. If a user reports getting
3211 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003212 */
3213 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003214 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003215
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003216 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003217}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003218
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003219static int
3220validate_encoding_decl(node *tree)
3221{
3222 int nch = NCH(tree);
3223 int res = ((nch == 1)
3224 && validate_file_input(CHILD(tree, 0)));
3225
3226 if (!res && !PyErr_Occurred())
3227 err_string("Error Parsing encoding_decl");
3228
3229 return res;
3230}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003231
Fred Drake43f8f9b1998-04-13 16:25:46 +00003232static PyObject*
3233pickle_constructor = NULL;
3234
3235
3236static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003237parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003238{
Fred Drake268397f1998-04-29 14:16:32 +00003239 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003240 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003241 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003242 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003243
Fred Drakec2683dd2001-07-17 19:32:05 +00003244 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003245 PyObject *newargs;
3246 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003247
Fred Drake2a6875e1999-09-20 22:32:18 +00003248 if ((empty_dict = PyDict_New()) == NULL)
3249 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003250 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003251 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003252 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003253 if (tuple != NULL) {
3254 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3255 Py_DECREF(tuple);
3256 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003257 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003258 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003259 }
3260 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003261 Py_XDECREF(empty_dict);
3262
Fred Drake43f8f9b1998-04-13 16:25:46 +00003263 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003264}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003265
3266
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003267/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003268 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003269 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003270 * We'd really have to write a wrapper around it all anyway to allow
3271 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003272 */
3273static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003274 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003275 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003276 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003277 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003278 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003279 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003280 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003281 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003282 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003283 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003284 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003285 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003286 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003287 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003288 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003289 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003290 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003291 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003292
Fred Drake43f8f9b1998-04-13 16:25:46 +00003293 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003294 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003295 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003296
Fred Drake268397f1998-04-29 14:16:32 +00003297 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003298 };
3299
3300
Martin v. Löwis1a214512008-06-11 05:26:20 +00003301
3302static struct PyModuleDef parsermodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 PyModuleDef_HEAD_INIT,
3304 "parser",
3305 NULL,
3306 -1,
3307 parser_functions,
3308 NULL,
3309 NULL,
3310 NULL,
3311 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00003312};
3313
3314PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003315
Mark Hammond62b1ab12002-07-23 06:31:15 +00003316PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003317PyInit_parser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003318{
Fred Drake13130bc2001-08-15 16:44:56 +00003319 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003320
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00003321 if (PyType_Ready(&PyST_Type) < 0)
3322 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00003323 module = PyModule_Create(&parsermodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003324 if (module == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 return NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003326
Fred Drake7a15ba51999-09-09 14:21:52 +00003327 if (parser_error == 0)
3328 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003329
Tim Peters6a627252003-07-21 14:25:23 +00003330 if (parser_error == 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003331 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003332 /* CAUTION: The code next used to skip bumping the refcount on
Martin v. Löwis1a214512008-06-11 05:26:20 +00003333 * parser_error. That's a disaster if PyInit_parser() gets called more
Tim Peters6a627252003-07-21 14:25:23 +00003334 * than once. By incref'ing, we ensure that each module dict that
3335 * gets created owns its reference to the shared parser_error object,
3336 * and the file static parser_error vrbl owns a reference too.
3337 */
3338 Py_INCREF(parser_error);
3339 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003340 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003341
Fred Drakec2683dd2001-07-17 19:32:05 +00003342 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003343 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003344
Fred Drake13130bc2001-08-15 16:44:56 +00003345 PyModule_AddStringConstant(module, "__copyright__",
3346 parser_copyright_string);
3347 PyModule_AddStringConstant(module, "__doc__",
3348 parser_doc_string);
3349 PyModule_AddStringConstant(module, "__version__",
3350 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003351
Fred Drake78bdb9b2001-07-19 20:17:15 +00003352 /* Register to support pickling.
3353 * If this fails, the import of this module will fail because an
3354 * exception will be raised here; should we clear the exception?
3355 */
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003356 copyreg = PyImport_ImportModuleNoBlock("copyreg");
Fred Drake13130bc2001-08-15 16:44:56 +00003357 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003358 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003359
Fred Drake13130bc2001-08-15 16:44:56 +00003360 func = PyObject_GetAttrString(copyreg, "pickle");
3361 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3362 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003363 Py_XINCREF(pickle_constructor);
3364 if ((func != NULL) && (pickle_constructor != NULL)
3365 && (pickler != NULL)) {
3366 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003367
Thomas Wouters477c8d52006-05-27 19:21:47 +00003368 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3369 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003370 Py_XDECREF(res);
3371 }
3372 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003373 Py_XDECREF(pickle_constructor);
3374 Py_XDECREF(pickler);
3375 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003376 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00003377 return module;
Fred Drakeff9ea482000-04-19 13:54:15 +00003378}