blob: bc4e74bfa1d026be00d99a9b06dfdce27dc97632 [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);
Mark Dickinson211c6252009-02-01 10:28:51 +0000170static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op);
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000171static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
172static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
173static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
174static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
175static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
Fred Drake503d8d61998-04-13 18:45:18 +0000176
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000177#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
178
179static PyMethodDef parser_methods[] = {
180 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
181 PyDoc_STR("Compile this ST object into a code object.")},
182 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
183 PyDoc_STR("Determines if this ST object was created from an expression.")},
184 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
185 PyDoc_STR("Determines if this ST object was created from a suite.")},
186 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
187 PyDoc_STR("Creates a list-tree representation of this ST.")},
188 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
189 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
190
191 {NULL, NULL, 0, NULL}
192};
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000193
Fred Drake268397f1998-04-29 14:16:32 +0000194static
Fred Drakec2683dd2001-07-17 19:32:05 +0000195PyTypeObject PyST_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000196 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000197 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000198 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000199 0, /* tp_itemsize */
200 (destructor)parser_free, /* tp_dealloc */
201 0, /* tp_print */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000202 0, /* tp_getattr */
Fred Drakeff9ea482000-04-19 13:54:15 +0000203 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000204 0, /* tp_reserved */
Fred Drakeff9ea482000-04-19 13:54:15 +0000205 0, /* tp_repr */
206 0, /* tp_as_number */
207 0, /* tp_as_sequence */
208 0, /* tp_as_mapping */
209 0, /* tp_hash */
210 0, /* tp_call */
211 0, /* tp_str */
212 0, /* tp_getattro */
213 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000214
215 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000216 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000217
Fred Drakeff9ea482000-04-19 13:54:15 +0000218 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000219
220 /* __doc__ */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000221 "Intermediate representation of a Python parse tree.",
222 0, /* tp_traverse */
223 0, /* tp_clear */
Mark Dickinson211c6252009-02-01 10:28:51 +0000224 parser_richcompare, /* tp_richcompare */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000225 0, /* tp_weaklistoffset */
226 0, /* tp_iter */
227 0, /* tp_iternext */
228 parser_methods, /* tp_methods */
Fred Drakec2683dd2001-07-17 19:32:05 +0000229}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000230
231
Mark Dickinson211c6252009-02-01 10:28:51 +0000232/* PyST_Type isn't subclassable, so just check ob_type */
233#define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type)
234
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000235static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000236parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000237{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000238 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000239
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000240 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000241 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000242
243 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000244 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000245
246 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000247 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000248
249 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000250 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000251
252 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000253 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000254
255 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000256 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000257
Fred Drakeff9ea482000-04-19 13:54:15 +0000258 if (v != 0)
259 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000260 }
261 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000262}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000263
Mark Dickinson211c6252009-02-01 10:28:51 +0000264/* parser_richcompare(PyObject* left, PyObject* right, int op)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265 *
266 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
267 * This really just wraps a call to parser_compare_nodes() with some easy
268 * checks and protection code.
269 *
270 */
Mark Dickinson211c6252009-02-01 10:28:51 +0000271
272#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
273
274static PyObject *
275parser_richcompare(PyObject *left, PyObject *right, int op)
Guido van Rossum47478871996-08-21 14:32:37 +0000276{
Mark Dickinson211c6252009-02-01 10:28:51 +0000277 int result;
278 PyObject *v;
279
280 /* neither argument should be NULL, unless something's gone wrong */
281 if (left == NULL || right == NULL) {
282 PyErr_BadInternalCall();
283 return NULL;
284 }
285
286 /* both arguments should be instances of PyST_Object */
287 if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) {
288 v = Py_NotImplemented;
289 goto finished;
290 }
291
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000292 if (left == right)
Mark Dickinson211c6252009-02-01 10:28:51 +0000293 /* if arguments are identical, they're equal */
294 result = 0;
295 else
296 result = parser_compare_nodes(((PyST_Object *)left)->st_node,
297 ((PyST_Object *)right)->st_node);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000298
Mark Dickinson211c6252009-02-01 10:28:51 +0000299 /* Convert return value to a Boolean */
300 switch (op) {
301 case Py_EQ:
302 v = TEST_COND(result == 0);
303 break;
304 case Py_NE:
305 v = TEST_COND(result != 0);
306 break;
307 case Py_LE:
308 v = TEST_COND(result <= 0);
309 break;
310 case Py_GE:
311 v = TEST_COND(result >= 0);
312 break;
313 case Py_LT:
314 v = TEST_COND(result < 0);
315 break;
316 case Py_GT:
317 v = TEST_COND(result > 0);
318 break;
319 default:
320 PyErr_BadArgument();
321 return NULL;
322 }
323 finished:
324 Py_INCREF(v);
325 return v;
Fred Drakeff9ea482000-04-19 13:54:15 +0000326}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000327
Fred Drakec2683dd2001-07-17 19:32:05 +0000328/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000329 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000330 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000331 * 'wrapper' object that holds a node* and allows it to be passed around in
332 * Python code.
333 *
334 */
335static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000336parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000337{
Fred Drakec2683dd2001-07-17 19:32:05 +0000338 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000339
340 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000341 o->st_node = st;
342 o->st_type = type;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000343 o->st_flags.cf_flags = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000344 }
Fred Drake268397f1998-04-29 14:16:32 +0000345 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000346 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000347 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000348 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000349}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000350
351
Fred Drakec2683dd2001-07-17 19:32:05 +0000352/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000353 *
354 * This is called by a del statement that reduces the reference count to 0.
355 *
356 */
357static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000358parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000359{
Fred Drakec2683dd2001-07-17 19:32:05 +0000360 PyNode_Free(st->st_node);
361 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000362}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000363
364
Fred Drakec2683dd2001-07-17 19:32:05 +0000365/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000366 *
367 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000368 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000369 *
370 */
371static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000372parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000373{
Guido van Rossum47478871996-08-21 14:32:37 +0000374 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000375 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000376 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000377 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000378
Georg Brandl30704ea2008-07-23 15:07:12 +0000379 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000380
Martin v. Löwis1a214512008-06-11 05:26:20 +0000381 if (self == NULL || PyModule_Check(self)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000382 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
383 &PyST_Type, &self, &line_option,
384 &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000385 }
Fred Drake503d8d61998-04-13 18:45:18 +0000386 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000387 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
388 &line_option, &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000389 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000390 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000391 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000392 if (line_option != NULL) {
393 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
394 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000395 if (col_option != NULL) {
396 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
397 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000398 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000399 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000400 * since it's known to work already.
401 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000402 res = node2tuple(((PyST_Object*)self)->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000403 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000404 }
405 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000406}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000407
408
Fred Drakec2683dd2001-07-17 19:32:05 +0000409/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000410 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000411 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000412 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000413 *
414 */
415static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000416parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000417{
Guido van Rossum47478871996-08-21 14:32:37 +0000418 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000419 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000420 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000421 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000422
Georg Brandl30704ea2008-07-23 15:07:12 +0000423 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000424
Martin v. Löwis1a214512008-06-11 05:26:20 +0000425 if (self == NULL || PyModule_Check(self))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000426 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
427 &PyST_Type, &self, &line_option,
428 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000429 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000430 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
431 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000432 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000433 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000434 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000435 if (line_option != 0) {
436 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
437 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000438 if (col_option != NULL) {
439 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
440 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000441 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000442 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000443 * since it's known to work already.
444 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000445 res = node2tuple(self->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000446 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000447 }
448 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000449}
Guido van Rossum47478871996-08-21 14:32:37 +0000450
451
Fred Drakec2683dd2001-07-17 19:32:05 +0000452/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000453 *
454 * This function creates code objects from the parse tree represented by
455 * the passed-in data object. An optional file name is passed in as well.
456 *
457 */
458static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000459parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000460{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000461 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000462 PyArena* arena;
463 mod_ty mod;
Fred Drakec2683dd2001-07-17 19:32:05 +0000464 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000465 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000466
Georg Brandl30704ea2008-07-23 15:07:12 +0000467 static char *keywords[] = {"st", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000468
Martin v. Löwis1a214512008-06-11 05:26:20 +0000469 if (self == NULL || PyModule_Check(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000470 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
471 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000472 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000473 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000474 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000475
Benjamin Petersonf216c942008-10-31 02:28:05 +0000476 if (ok) {
477 arena = PyArena_New();
478 if (arena) {
479 mod = PyAST_FromNode(self->st_node, &(self->st_flags), str, arena);
480 if (mod) {
481 res = (PyObject *)PyAST_Compile(mod, str, &(self->st_flags), arena);
482 }
483 PyArena_Free(arena);
484 }
485 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000486
487 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000488}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000489
490
491/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
492 * PyObject* parser_issuite(PyObject* self, PyObject* args)
493 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000494 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000495 * a statement suite, respectively. The return is a Python truth value.
496 *
497 */
498static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000499parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000500{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000501 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000502 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000503
Georg Brandl30704ea2008-07-23 15:07:12 +0000504 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000505
Martin v. Löwis1a214512008-06-11 05:26:20 +0000506 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000507 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000508 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000509 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000510 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000511
512 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000513 /* Check to see if the ST represents an expression or not. */
514 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000515 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000516 }
517 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000518}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000519
520
521static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000522parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000523{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000524 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000525 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000526
Georg Brandl30704ea2008-07-23 15:07:12 +0000527 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000528
Martin v. Löwis1a214512008-06-11 05:26:20 +0000529 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000530 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000531 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000532 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000533 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000534
535 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000536 /* Check to see if the ST represents an expression or not. */
537 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000538 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000539 }
540 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000541}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000542
543
Guido van Rossum3d602e31996-07-21 02:33:56 +0000544/* err_string(char* message)
545 *
546 * Sets the error string for an exception of type ParserError.
547 *
548 */
549static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000550err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000551{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000552 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000553}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000554
555
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000556/* PyObject* parser_do_parse(PyObject* args, int type)
557 *
558 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000559 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000560 *
561 */
562static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000563parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000564{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000565 char* string = 0;
566 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000567 int flags = 0;
568 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000569
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000570 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000571
572 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000573 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
574 &_PyParser_Grammar,
575 (type == PyST_EXPR)
576 ? eval_input : file_input,
577 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (n) {
580 res = parser_newstobject(n, type);
Benjamin Petersonf216c942008-10-31 02:28:05 +0000581 if (res)
582 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
583 }
584 else
585 PyParser_SetError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000586 }
587 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000588}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000589
590
591/* PyObject* parser_expr(PyObject* self, PyObject* args)
592 * PyObject* parser_suite(PyObject* self, PyObject* args)
593 *
594 * External interfaces to the parser itself. Which is called determines if
595 * the parser attempts to recognize an expression ('eval' form) or statement
596 * suite ('exec' form). The real work is done by parser_do_parse() above.
597 *
598 */
599static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000600parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000601{
Fred Drake268397f1998-04-29 14:16:32 +0000602 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000603 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000604}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000605
606
607static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000608parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000609{
Fred Drake268397f1998-04-29 14:16:32 +0000610 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000611 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000612}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000613
614
615
Fred Drakec2683dd2001-07-17 19:32:05 +0000616/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000617 * object requires that the input tuple be valid without having to rely on
618 * catching an exception from the compiler. This is done to allow the
619 * compiler itself to remain fast, since most of its input will come from
620 * the parser directly, and therefore be known to be syntactically correct.
621 * This validation is done to ensure that we don't core dump the compile
622 * phase, returning an exception instead.
623 *
624 * Two aspects can be broken out in this code: creating a node tree from
625 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000626 * advantageous to expand the number of ST types to include funcdefs and
627 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000628 * here. They are not necessary, and not quite as useful in a raw form.
629 * For now, let's get expressions and suites working reliably.
630 */
631
632
Jeremy Hylton938ace62002-07-17 16:30:39 +0000633static node* build_node_tree(PyObject *tuple);
634static int validate_expr_tree(node *tree);
635static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000636static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000637
Fred Drakec2683dd2001-07-17 19:32:05 +0000638/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000639 *
640 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000641 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000642 * tuple can be validated. It does this by checking the first code of the
643 * tuple, and, if acceptable, builds the internal representation. If this
644 * step succeeds, the internal representation is validated as fully as
645 * possible with the various validate_*() routines defined below.
646 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000647 * This function must be changed if support is to be added for PyST_FRAGMENT
648 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000649 *
650 */
651static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000652parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000653{
Fred Drake268397f1998-04-29 14:16:32 +0000654 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000655 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000656 PyObject *tuple;
657 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000658
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000659 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000660
Fred Drakec2683dd2001-07-17 19:32:05 +0000661 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000662 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000663 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000664 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000665 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000666 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000667 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000668 }
669 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000670 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000671 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000672 tree = build_node_tree(tuple);
673 if (tree != 0) {
674 int start_sym = TYPE(tree);
675 if (start_sym == eval_input) {
676 /* Might be an eval form. */
677 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000678 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000679 else
680 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000681 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000682 else if (start_sym == file_input) {
683 /* This looks like an exec form so far. */
684 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000685 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000686 else
687 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000688 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000689 else if (start_sym == encoding_decl) {
690 /* This looks like an encoding_decl so far. */
691 if (validate_encoding_decl(tree))
692 st = parser_newstobject(tree, PyST_SUITE);
693 else
694 PyNode_Free(tree);
695 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000696 else {
697 /* This is a fragment, at best. */
698 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000699 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000700 }
Guido van Rossum47478871996-08-21 14:32:37 +0000701 }
Guido van Rossum47478871996-08-21 14:32:37 +0000702 /* Make sure we throw an exception on all errors. We should never
703 * get this, but we'd do well to be sure something is done.
704 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000705 if (st == NULL && !PyErr_Occurred())
706 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000707
Fred Drakec2683dd2001-07-17 19:32:05 +0000708 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000709}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000710
711
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000712/* node* build_node_children()
713 *
714 * Iterate across the children of the current non-terminal node and build
715 * their structures. If successful, return the root of this portion of
716 * the tree, otherwise, 0. Any required exception will be specified already,
717 * and no memory will have been deallocated.
718 *
719 */
720static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000721build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000722{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000723 Py_ssize_t len = PyObject_Size(tuple);
724 Py_ssize_t i;
725 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000726
727 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000728 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000729 PyObject* elem = PySequence_GetItem(tuple, i);
730 int ok = elem != NULL;
731 long type = 0;
732 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000733
Fred Drakeff9ea482000-04-19 13:54:15 +0000734 if (ok)
735 ok = PySequence_Check(elem);
736 if (ok) {
737 PyObject *temp = PySequence_GetItem(elem, 0);
738 if (temp == NULL)
739 ok = 0;
740 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000741 ok = PyLong_Check(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000742 if (ok)
Christian Heimes217cfd12007-12-02 14:31:20 +0000743 type = PyLong_AS_LONG(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000744 Py_DECREF(temp);
745 }
746 }
747 if (!ok) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000748 PyObject *err = Py_BuildValue("os", elem,
749 "Illegal node construct.");
750 PyErr_SetObject(parser_error, err);
751 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000752 Py_XDECREF(elem);
753 return (0);
754 }
755 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000756 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000757 PyObject *temp;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000758 const char *temp_str;
Guido van Rossum47478871996-08-21 14:32:37 +0000759
Fred Drake0ac9b072000-09-12 21:58:06 +0000760 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000761 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000762 return 0;
763 }
764 temp = PySequence_GetItem(elem, 1);
765 if (temp == NULL)
766 return 0;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000767 if (!PyUnicode_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000768 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000769 "second item in terminal node must be a string,"
770 " found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000771 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000772 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000773 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000774 return 0;
775 }
776 if (len == 3) {
777 PyObject *o = PySequence_GetItem(elem, 2);
778 if (o != NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000779 if (PyLong_Check(o))
780 *line_num = PyLong_AS_LONG(o);
Fred Drake0ac9b072000-09-12 21:58:06 +0000781 else {
782 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000783 "third item in terminal node must be an"
784 " integer, found %s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 Py_TYPE(temp)->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000786 Py_DECREF(o);
787 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000788 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000789 return 0;
790 }
791 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000792 }
793 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000794 temp_str = _PyUnicode_AsStringAndSize(temp, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000795 if (temp_str == NULL) {
796 Py_DECREF(temp);
797 Py_XDECREF(elem);
798 return 0;
799 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000800 strn = (char *)PyObject_MALLOC(len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000801 if (strn != NULL)
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000802 (void) memcpy(strn, temp_str, len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000803 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000804 }
805 else if (!ISNONTERMINAL(type)) {
806 /*
807 * It has to be one or the other; this is an error.
808 * Throw an exception.
809 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000810 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
811 PyErr_SetObject(parser_error, err);
812 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000813 Py_XDECREF(elem);
814 return (0);
815 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000816 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000817 if (err == E_NOMEM) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000818 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000819 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000820 return (node *) PyErr_NoMemory();
821 }
822 if (err == E_OVERFLOW) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000823 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000824 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000825 PyErr_SetString(PyExc_ValueError,
826 "unsupported number of child nodes");
827 return NULL;
828 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000829
Fred Drakeff9ea482000-04-19 13:54:15 +0000830 if (ISNONTERMINAL(type)) {
831 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000832
Fred Drakeff9ea482000-04-19 13:54:15 +0000833 if (new_child != build_node_children(elem, new_child, line_num)) {
834 Py_XDECREF(elem);
835 return (0);
836 }
837 }
838 else if (type == NEWLINE) { /* It's true: we increment the */
839 ++(*line_num); /* line number *after* the newline! */
840 }
841 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000842 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000843 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000844}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000845
846
847static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000848build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000849{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000850 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000851 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000852 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000853
Guido van Rossum47478871996-08-21 14:32:37 +0000854 if (temp != NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000855 num = PyLong_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000856 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000857 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000858 /*
859 * The tuple is simple, but it doesn't start with a start symbol.
860 * Throw an exception now and be done with it.
861 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000862 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000863 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000864 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000865 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000866 }
867 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000868 /*
869 * Not efficient, but that can be handled later.
870 */
871 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000872 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000873
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000874 if (num == encoding_decl) {
875 encoding = PySequence_GetItem(tuple, 2);
876 /* tuple isn't borrowed anymore here, need to DECREF */
877 tuple = PySequence_GetSlice(tuple, 0, 2);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000878 if (tuple == NULL)
879 return NULL;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000880 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000881 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000882 if (res != NULL) {
883 if (res != build_node_children(tuple, res, &line_num)) {
884 PyNode_Free(res);
885 res = NULL;
886 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000887 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000888 Py_ssize_t len;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000889 const char *temp;
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000890 temp = _PyUnicode_AsStringAndSize(encoding, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000891 if (temp == NULL) {
892 Py_DECREF(res);
893 Py_DECREF(encoding);
894 Py_DECREF(tuple);
895 return NULL;
896 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000897 res->n_str = (char *)PyObject_MALLOC(len + 1);
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000898 if (res->n_str != NULL && temp != NULL)
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000899 (void) memcpy(res->n_str, temp, len + 1);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000900 Py_DECREF(encoding);
901 Py_DECREF(tuple);
902 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000903 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000904 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000905 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000906 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000907 * NONTERMINAL, we can't use it. Not sure the implementation
908 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000909 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000910 PyObject *err = Py_BuildValue("os", tuple,
911 "Illegal component tuple.");
912 PyErr_SetObject(parser_error, err);
913 Py_XDECREF(err);
914 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000915
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000916 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000917}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000918
919
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000920/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000921 * Validation routines used within the validation section:
922 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000923static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000924
Fred Drakeff9ea482000-04-19 13:54:15 +0000925#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
926#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
927#define validate_colon(ch) validate_terminal(ch, COLON, ":")
928#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
929#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
930#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
931#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
932#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
933#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
934#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
935#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
936#define validate_star(ch) validate_terminal(ch, STAR, "*")
937#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
938#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
939#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000940#define validate_at(ch) validate_terminal(ch, AT, "@")
Mark Dickinsonea7e9f92012-04-29 18:34:40 +0100941#define validate_rarrow(ch) validate_terminal(ch, RARROW, "->")
Fred Drakeff9ea482000-04-19 13:54:15 +0000942#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000943
Fred Drake0ac9b072000-09-12 21:58:06 +0000944#define VALIDATER(n) static int validate_##n(node *tree)
945
Fred Drakeff9ea482000-04-19 13:54:15 +0000946VALIDATER(node); VALIDATER(small_stmt);
947VALIDATER(class); VALIDATER(node);
948VALIDATER(parameters); VALIDATER(suite);
949VALIDATER(testlist); VALIDATER(varargslist);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000950VALIDATER(vfpdef);
Fred Drakeff9ea482000-04-19 13:54:15 +0000951VALIDATER(stmt); VALIDATER(simple_stmt);
952VALIDATER(expr_stmt); VALIDATER(power);
Guido van Rossum452bf512007-02-09 05:32:43 +0000953VALIDATER(del_stmt);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000954VALIDATER(return_stmt); VALIDATER(raise_stmt);
955VALIDATER(import_stmt); VALIDATER(import_stmt);
956VALIDATER(import_name); VALIDATER(yield_stmt);
957VALIDATER(global_stmt); VALIDATER(assert_stmt);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000958VALIDATER(compound_stmt); VALIDATER(test_or_star_expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000959VALIDATER(while); VALIDATER(for);
960VALIDATER(try); VALIDATER(except_clause);
961VALIDATER(test); VALIDATER(and_test);
962VALIDATER(not_test); VALIDATER(comparison);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000963VALIDATER(comp_op);
Guido van Rossum0368b722007-05-11 16:50:42 +0000964VALIDATER(star_expr); VALIDATER(expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000965VALIDATER(xor_expr); VALIDATER(and_expr);
966VALIDATER(shift_expr); VALIDATER(arith_expr);
967VALIDATER(term); VALIDATER(factor);
968VALIDATER(atom); VALIDATER(lambdef);
969VALIDATER(trailer); VALIDATER(subscript);
970VALIDATER(subscriptlist); VALIDATER(sliceop);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000971VALIDATER(exprlist); VALIDATER(dictorsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000972VALIDATER(arglist); VALIDATER(argument);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000973VALIDATER(comp_for);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000974VALIDATER(comp_iter); VALIDATER(comp_if);
975VALIDATER(testlist_comp); VALIDATER(yield_expr);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000976VALIDATER(or_test);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000977VALIDATER(test_nocond); VALIDATER(lambdef_nocond);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000978
Fred Drake0ac9b072000-09-12 21:58:06 +0000979#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000980
Fred Drakeff9ea482000-04-19 13:54:15 +0000981#define is_even(n) (((n) & 1) == 0)
982#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000983
984
985static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000986validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000987{
Fred Drake0ac9b072000-09-12 21:58:06 +0000988 if (TYPE(n) != t) {
989 PyErr_Format(parser_error, "Expected node type %d, got %d.",
990 t, TYPE(n));
991 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000992 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000993 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000994}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000995
996
Fred Drakee7ab64e2000-04-25 04:14:46 +0000997/* Verifies that the number of child nodes is exactly 'num', raising
998 * an exception if it isn't. The exception message does not indicate
999 * the exact number of nodes, allowing this to be used to raise the
1000 * "right" exception when the wrong number of nodes is present in a
1001 * specific variant of a statement's syntax. This is commonly used
1002 * in that fashion.
1003 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001004static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001005validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001006{
Guido van Rossum3d602e31996-07-21 02:33:56 +00001007 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001008 PyErr_Format(parser_error,
1009 "Illegal number of children for %s node.", name);
1010 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001011 }
Fred Drake0ac9b072000-09-12 21:58:06 +00001012 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00001013}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001014
1015
1016static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001017validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +00001018{
Guido van Rossum3d602e31996-07-21 02:33:56 +00001019 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +00001020 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001021
1022 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001023 PyErr_Format(parser_error,
1024 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001025 }
1026 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001027}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001028
1029
Guido van Rossum47478871996-08-21 14:32:37 +00001030/* X (',' X) [',']
1031 */
1032static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +00001033validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +00001034 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001035{
1036 int nch = NCH(tree);
1037 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +00001038 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +00001039
1040 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001041 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +00001042 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001043 if (is_even(nch))
1044 res = validate_comma(CHILD(tree, --nch));
1045 if (res && nch > 1) {
1046 int pos = 1;
1047 for ( ; res && pos < nch; pos += 2)
1048 res = (validate_comma(CHILD(tree, pos))
1049 && vfunc(CHILD(tree, pos + 1)));
1050 }
Guido van Rossum47478871996-08-21 14:32:37 +00001051 }
1052 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001053}
Guido van Rossum47478871996-08-21 14:32:37 +00001054
1055
Fred Drakecff283c2000-08-21 22:24:43 +00001056/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001057 *
1058 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001059 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00001060 */
Guido van Rossum47478871996-08-21 14:32:37 +00001061static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001062validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001063{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001064 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +00001065 int res = (validate_ntype(tree, classdef) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001067
Guido van Rossum3d602e31996-07-21 02:33:56 +00001068 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001069 res = (validate_name(CHILD(tree, 0), "class")
1070 && validate_ntype(CHILD(tree, 1), NAME)
1071 && validate_colon(CHILD(tree, nch - 2))
1072 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001073 }
Brett Cannonf4189912005-04-09 02:30:16 +00001074 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001075 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +00001076 }
Guido van Rossumfc158e22007-11-15 19:17:28 +00001077
Brett Cannonf4189912005-04-09 02:30:16 +00001078 if (res) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 if (nch == 7) {
1080 res = ((validate_lparen(CHILD(tree, 2)) &&
1081 validate_arglist(CHILD(tree, 3)) &&
1082 validate_rparen(CHILD(tree, 4))));
1083 }
1084 else if (nch == 6) {
1085 res = (validate_lparen(CHILD(tree,2)) &&
1086 validate_rparen(CHILD(tree,3)));
1087 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001088 }
1089 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001090}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001091
1092
Guido van Rossum3d602e31996-07-21 02:33:56 +00001093/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001094 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001095 */
Guido van Rossum47478871996-08-21 14:32:37 +00001096static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001097validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001098{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001099 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001100 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001101 && (nch >= 4)
1102 && validate_name(CHILD(tree, 0), "if")
1103 && validate_test(CHILD(tree, 1))
1104 && validate_colon(CHILD(tree, 2))
1105 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001106
1107 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001108 /* ... 'else' ':' suite */
1109 res = (validate_name(CHILD(tree, nch - 3), "else")
1110 && validate_colon(CHILD(tree, nch - 2))
1111 && validate_suite(CHILD(tree, nch - 1)));
1112 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001113 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001114 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001115 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001116 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001117 /* Will catch the case for nch < 4 */
1118 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001119 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001120 /* ... ('elif' test ':' suite)+ ... */
1121 int j = 4;
1122 while ((j < nch) && res) {
1123 res = (validate_name(CHILD(tree, j), "elif")
1124 && validate_colon(CHILD(tree, j + 2))
1125 && validate_test(CHILD(tree, j + 1))
1126 && validate_suite(CHILD(tree, j + 3)));
1127 j += 4;
1128 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001129 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001130 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001131}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001132
1133
Guido van Rossum3d602e31996-07-21 02:33:56 +00001134/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001135 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001136 *
1137 */
Guido van Rossum47478871996-08-21 14:32:37 +00001138static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001139validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001140{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001141 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001142 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001143
Guido van Rossum3d602e31996-07-21 02:33:56 +00001144 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001145 res = (validate_lparen(CHILD(tree, 0))
1146 && validate_rparen(CHILD(tree, nch - 1)));
1147 if (res && (nch == 3))
1148 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001149 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001150 else {
1151 (void) validate_numnodes(tree, 2, "parameters");
1152 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001153 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001154}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001155
1156
Fred Drakecff283c2000-08-21 22:24:43 +00001157/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001158 *
1159 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001160 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001161 * | NEWLINE INDENT stmt+ DEDENT
1162 */
Guido van Rossum47478871996-08-21 14:32:37 +00001163static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001164validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001165{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001166 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001167 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001168
Guido van Rossum3d602e31996-07-21 02:33:56 +00001169 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001170 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001171 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001172 /* NEWLINE INDENT stmt+ DEDENT */
1173 res = (validate_newline(CHILD(tree, 0))
1174 && validate_indent(CHILD(tree, 1))
1175 && validate_stmt(CHILD(tree, 2))
1176 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001177
Fred Drakeff9ea482000-04-19 13:54:15 +00001178 if (res && (nch > 4)) {
1179 int i = 3;
1180 --nch; /* forget the DEDENT */
1181 for ( ; res && (i < nch); ++i)
1182 res = validate_stmt(CHILD(tree, i));
1183 }
1184 else if (nch < 4)
1185 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001186 }
1187 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001188}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001189
1190
Guido van Rossum47478871996-08-21 14:32:37 +00001191static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001192validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001193{
Guido van Rossum47478871996-08-21 14:32:37 +00001194 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001195 validate_test, "testlist"));
1196}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001197
Guido van Rossum1c917072001-10-15 15:44:05 +00001198static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00001199validate_testlist_star_expr(node *tl)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001200{
Benjamin Peterson4905e802009-09-27 02:43:28 +00001201 return (validate_repeating_list(tl, testlist_star_expr, validate_test_or_star_expr,
1202 "testlist"));
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001203}
1204
1205
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001206/* validate either vfpdef or tfpdef.
1207 * vfpdef: NAME
1208 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001209 */
1210static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001211validate_vfpdef(node *tree)
Neal Norwitzc1505362006-12-28 06:47:50 +00001212{
1213 int nch = NCH(tree);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001214 if (TYPE(tree) == vfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001215 return nch == 1 && validate_name(CHILD(tree, 0), NULL);
1216 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001217 else if (TYPE(tree) == tfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001218 if (nch == 1) {
1219 return validate_name(CHILD(tree, 0), NULL);
1220 }
1221 else if (nch == 3) {
1222 return validate_name(CHILD(tree, 0), NULL) &&
1223 validate_colon(CHILD(tree, 1)) &&
1224 validate_test(CHILD(tree, 2));
1225 }
1226 }
1227 return 0;
1228}
1229
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001230/* '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001231 * ..or tfpdef in place of vfpdef. vfpdef: NAME; tfpdef: NAME [':' test]
Fred Drakecff283c2000-08-21 22:24:43 +00001232 */
1233static int
1234validate_varargslist_trailer(node *tree, int start)
1235{
1236 int nch = NCH(tree);
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001237 int res = 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001238
1239 if (nch <= start) {
1240 err_string("expected variable argument trailer for varargslist");
1241 return 0;
1242 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001243 if (TYPE(CHILD(tree, start)) == STAR) {
Fred Drakecff283c2000-08-21 22:24:43 +00001244 /*
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001245 * '*' [vfpdef]
Fred Drakecff283c2000-08-21 22:24:43 +00001246 */
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001247 res = validate_star(CHILD(tree, start++));
1248 if (res && start < nch && (TYPE(CHILD(tree, start)) == vfpdef ||
1249 TYPE(CHILD(tree, start)) == tfpdef))
1250 res = validate_vfpdef(CHILD(tree, start++));
1251 /*
1252 * (',' vfpdef ['=' test])*
1253 */
1254 while (res && start + 1 < nch && (
1255 TYPE(CHILD(tree, start + 1)) == vfpdef ||
1256 TYPE(CHILD(tree, start + 1)) == tfpdef)) {
1257 res = (validate_comma(CHILD(tree, start++))
1258 && validate_vfpdef(CHILD(tree, start++)));
1259 if (res && start + 1 < nch && TYPE(CHILD(tree, start)) == EQUAL)
1260 res = (validate_equal(CHILD(tree, start++))
1261 && validate_test(CHILD(tree, start++)));
1262 }
1263 /*
1264 * [',' '**' vfpdef]
1265 */
1266 if (res && start + 2 < nch && TYPE(CHILD(tree, start+1)) == DOUBLESTAR)
1267 res = (validate_comma(CHILD(tree, start++))
1268 && validate_doublestar(CHILD(tree, start++))
1269 && validate_vfpdef(CHILD(tree, start++)));
1270 }
1271 else if (TYPE(CHILD(tree, start)) == DOUBLESTAR) {
1272 /*
1273 * '**' vfpdef
1274 */
1275 if (start + 1 < nch)
1276 res = (validate_doublestar(CHILD(tree, start++))
1277 && validate_vfpdef(CHILD(tree, start++)));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001278 else {
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001279 res = 0;
1280 err_string("expected vfpdef after ** in varargslist trailer");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001281 }
Fred Drakecff283c2000-08-21 22:24:43 +00001282 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001283 else {
1284 res = 0;
1285 err_string("expected * or ** in varargslist trailer");
Fred Drakecff283c2000-08-21 22:24:43 +00001286 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001287
1288 if (res && start != nch) {
1289 res = 0;
1290 err_string("unexpected extra children in varargslist trailer");
1291 }
Fred Drakecff283c2000-08-21 22:24:43 +00001292 return res;
1293}
1294
1295
Neal Norwitzc1505362006-12-28 06:47:50 +00001296/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001297 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001298 * Validate typedargslist or varargslist.
1299 *
1300 * typedargslist: ((tfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001301 * ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
1302 * '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001303 * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001304 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001305 * varargslist: ((vfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001306 * ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
1307 * '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001308 * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001309 * vfpdef: NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001310 *
1311 */
Guido van Rossum47478871996-08-21 14:32:37 +00001312static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001313validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001314{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001315 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001316 int res = (TYPE(tree) == varargslist ||
1317 TYPE(tree) == typedargslist) &&
1318 (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001319 int sym;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001320 node *ch;
1321 int i = 0;
Guido van Rossumfc158e22007-11-15 19:17:28 +00001322
Fred Drakeb6429a22000-12-11 22:08:27 +00001323 if (!res)
1324 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001325 if (nch < 1) {
1326 err_string("varargslist missing child nodes");
1327 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001328 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001329 while (i < nch) {
Guido van Rossumfc158e22007-11-15 19:17:28 +00001330 ch = CHILD(tree, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001331 sym = TYPE(ch);
1332 if (sym == vfpdef || sym == tfpdef) {
1333 /* validate (vfpdef ['=' test] ',')+ */
1334 res = validate_vfpdef(ch);
Fred Drakecff283c2000-08-21 22:24:43 +00001335 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001336 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1337 res = (validate_equal(CHILD(tree, i))
1338 && validate_test(CHILD(tree, i+1)));
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001339 if (res)
1340 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001341 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001342 if (res && i < nch) {
1343 res = validate_comma(CHILD(tree, i));
1344 ++i;
Fred Drakecff283c2000-08-21 22:24:43 +00001345 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001346 } else if (sym == DOUBLESTAR || sym == STAR) {
1347 res = validate_varargslist_trailer(tree, i);
1348 break;
1349 } else {
1350 res = 0;
1351 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001352 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001353 }
Fred Drakecff283c2000-08-21 22:24:43 +00001354 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001355}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001356
1357
Nick Coghlan650f0d02007-04-15 12:05:43 +00001358/* comp_iter: comp_for | comp_if
Fred Drakecff283c2000-08-21 22:24:43 +00001359 */
1360static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001361validate_comp_iter(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001362{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001363 int res = (validate_ntype(tree, comp_iter)
1364 && validate_numnodes(tree, 1, "comp_iter"));
1365 if (res && TYPE(CHILD(tree, 0)) == comp_for)
1366 res = validate_comp_for(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001367 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001368 res = validate_comp_if(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001369
1370 return res;
1371}
1372
Nick Coghlan650f0d02007-04-15 12:05:43 +00001373/* comp_for: 'for' exprlist 'in' test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001374 */
1375static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001376validate_comp_for(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001377{
1378 int nch = NCH(tree);
1379 int res;
1380
1381 if (nch == 5)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001382 res = validate_comp_iter(CHILD(tree, 4));
Fred Drakecff283c2000-08-21 22:24:43 +00001383 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001384 res = validate_numnodes(tree, 4, "comp_for");
Raymond Hettinger354433a2004-05-19 08:20:33 +00001385
1386 if (res)
1387 res = (validate_name(CHILD(tree, 0), "for")
1388 && validate_exprlist(CHILD(tree, 1))
1389 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001390 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001391
1392 return res;
1393}
1394
Nick Coghlan650f0d02007-04-15 12:05:43 +00001395/* comp_if: 'if' test_nocond [comp_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001396 */
1397static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001398validate_comp_if(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001399{
1400 int nch = NCH(tree);
1401 int res;
1402
1403 if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001404 res = validate_comp_iter(CHILD(tree, 2));
Fred Drakecff283c2000-08-21 22:24:43 +00001405 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001406 res = validate_numnodes(tree, 2, "comp_if");
Fred Drakecff283c2000-08-21 22:24:43 +00001407
1408 if (res)
1409 res = (validate_name(CHILD(tree, 0), "if")
Nick Coghlan650f0d02007-04-15 12:05:43 +00001410 && validate_test_nocond(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001411
1412 return res;
1413}
1414
1415
Guido van Rossum3d602e31996-07-21 02:33:56 +00001416/* simple_stmt | compound_stmt
1417 *
1418 */
Guido van Rossum47478871996-08-21 14:32:37 +00001419static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001420validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001421{
1422 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001423 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001424
Guido van Rossum3d602e31996-07-21 02:33:56 +00001425 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001426 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001427
Fred Drakeff9ea482000-04-19 13:54:15 +00001428 if (TYPE(tree) == simple_stmt)
1429 res = validate_simple_stmt(tree);
1430 else
1431 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001432 }
1433 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001434}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001435
1436
Guido van Rossum3d602e31996-07-21 02:33:56 +00001437/* small_stmt (';' small_stmt)* [';'] NEWLINE
1438 *
1439 */
Guido van Rossum47478871996-08-21 14:32:37 +00001440static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001441validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001442{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001443 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001444 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001445 && (nch >= 2)
1446 && validate_small_stmt(CHILD(tree, 0))
1447 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001448
Guido van Rossum3d602e31996-07-21 02:33:56 +00001449 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001450 res = validate_numnodes(tree, 2, "simple_stmt");
1451 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001452 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001453 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001454 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001455 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001456
Fred Drakeff9ea482000-04-19 13:54:15 +00001457 for (i = 1; res && (i < nch); i += 2)
1458 res = (validate_semi(CHILD(tree, i))
1459 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001460 }
1461 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001462}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001463
1464
Guido van Rossum47478871996-08-21 14:32:37 +00001465static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001466validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001467{
1468 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001469 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001470
Fred Drake0ac9b072000-09-12 21:58:06 +00001471 if (res) {
1472 int ntype = TYPE(CHILD(tree, 0));
1473
1474 if ( (ntype == expr_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001475 || (ntype == del_stmt)
1476 || (ntype == pass_stmt)
1477 || (ntype == flow_stmt)
1478 || (ntype == import_stmt)
1479 || (ntype == global_stmt)
Georg Brandl7cae87c2006-09-06 06:51:57 +00001480 || (ntype == assert_stmt))
Fred Drake0ac9b072000-09-12 21:58:06 +00001481 res = validate_node(CHILD(tree, 0));
1482 else {
1483 res = 0;
1484 err_string("illegal small_stmt child type");
1485 }
1486 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001487 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001488 res = 0;
1489 PyErr_Format(parser_error,
1490 "Unrecognized child node of small_stmt: %d.",
1491 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001492 }
1493 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001494}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001495
1496
1497/* compound_stmt:
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00001498 * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
Guido van Rossum3d602e31996-07-21 02:33:56 +00001499 */
Guido van Rossum47478871996-08-21 14:32:37 +00001500static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001501validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001502{
1503 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001504 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001505 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001506
1507 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001508 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001509
1510 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001511 ntype = TYPE(tree);
1512 if ( (ntype == if_stmt)
1513 || (ntype == while_stmt)
1514 || (ntype == for_stmt)
1515 || (ntype == try_stmt)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00001516 || (ntype == with_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001517 || (ntype == funcdef)
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001518 || (ntype == classdef)
1519 || (ntype == decorated))
Fred Drakeff9ea482000-04-19 13:54:15 +00001520 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001521 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001522 res = 0;
1523 PyErr_Format(parser_error,
1524 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001525 }
1526 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001527}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001528
Guido van Rossum47478871996-08-21 14:32:37 +00001529static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00001530validate_yield_or_testlist(node *tree, int tse)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001531{
Benjamin Peterson4905e802009-09-27 02:43:28 +00001532 if (TYPE(tree) == yield_expr) {
1533 return validate_yield_expr(tree);
1534 }
1535 else {
1536 if (tse)
1537 return validate_testlist_star_expr(tree);
1538 else
1539 return validate_testlist(tree);
1540 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001541}
1542
1543static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001544validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001545{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001546 int j;
1547 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001548 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001549 && is_odd(nch)
Benjamin Peterson4905e802009-09-27 02:43:28 +00001550 && validate_testlist_star_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001551
Fred Drake28f739a2000-08-25 22:42:40 +00001552 if (res && nch == 3
1553 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001554 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
Benjamin Peterson4905e802009-09-27 02:43:28 +00001555 && validate_yield_or_testlist(CHILD(tree, 2), 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001556
Fred Drake28f739a2000-08-25 22:42:40 +00001557 if (res) {
1558 char *s = STR(CHILD(CHILD(tree, 1), 0));
1559
1560 res = (strcmp(s, "+=") == 0
1561 || strcmp(s, "-=") == 0
1562 || strcmp(s, "*=") == 0
1563 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001564 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001565 || strcmp(s, "%=") == 0
1566 || strcmp(s, "&=") == 0
1567 || strcmp(s, "|=") == 0
1568 || strcmp(s, "^=") == 0
1569 || strcmp(s, "<<=") == 0
1570 || strcmp(s, ">>=") == 0
1571 || strcmp(s, "**=") == 0);
1572 if (!res)
Ezio Melotti42da6632011-03-15 05:18:48 +02001573 err_string("illegal augmented assignment operator");
Fred Drake28f739a2000-08-25 22:42:40 +00001574 }
1575 }
1576 else {
1577 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001578 res = validate_equal(CHILD(tree, j))
Benjamin Peterson4905e802009-09-27 02:43:28 +00001579 && validate_yield_or_testlist(CHILD(tree, j + 1), 1);
Fred Drake28f739a2000-08-25 22:42:40 +00001580 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001581 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001582}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001583
1584
Guido van Rossum47478871996-08-21 14:32:37 +00001585static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001586validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001587{
1588 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001589 && validate_name(CHILD(tree, 0), "del")
1590 && validate_exprlist(CHILD(tree, 1)));
1591}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001592
1593
Guido van Rossum47478871996-08-21 14:32:37 +00001594static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001595validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001596{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001597 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001598 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001599 && ((nch == 1) || (nch == 2))
1600 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001601
Guido van Rossum3d602e31996-07-21 02:33:56 +00001602 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001603 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001604
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001605 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001606}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001607
1608
Guido van Rossum47478871996-08-21 14:32:37 +00001609static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001610validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001611{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001612 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001613 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001614 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001615
Guido van Rossum3d602e31996-07-21 02:33:56 +00001616 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001617 res = validate_name(CHILD(tree, 0), "raise");
1618 if (res && (nch >= 2))
1619 res = validate_test(CHILD(tree, 1));
1620 if (res && nch > 2) {
1621 res = (validate_comma(CHILD(tree, 2))
1622 && validate_test(CHILD(tree, 3)));
1623 if (res && (nch > 4))
1624 res = (validate_comma(CHILD(tree, 4))
1625 && validate_test(CHILD(tree, 5)));
1626 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001627 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001628 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001629 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001630 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001631 res = (validate_comma(CHILD(tree, 2))
1632 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001633
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001634 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001635}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001636
1637
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001638/* yield_expr: 'yield' [testlist]
1639 */
1640static int
1641validate_yield_expr(node *tree)
1642{
1643 int nch = NCH(tree);
1644 int res = (validate_ntype(tree, yield_expr)
1645 && ((nch == 1) || (nch == 2))
1646 && validate_name(CHILD(tree, 0), "yield"));
1647
1648 if (res && (nch == 2))
1649 res = validate_testlist(CHILD(tree, 1));
1650
1651 return (res);
1652}
1653
1654
1655/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001656 */
1657static int
1658validate_yield_stmt(node *tree)
1659{
1660 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001661 && validate_numnodes(tree, 1, "yield_stmt")
1662 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001663}
1664
1665
Fred Drakecff283c2000-08-21 22:24:43 +00001666static int
1667validate_import_as_name(node *tree)
1668{
1669 int nch = NCH(tree);
1670 int ok = validate_ntype(tree, import_as_name);
1671
1672 if (ok) {
1673 if (nch == 1)
1674 ok = validate_name(CHILD(tree, 0), NULL);
1675 else if (nch == 3)
1676 ok = (validate_name(CHILD(tree, 0), NULL)
1677 && validate_name(CHILD(tree, 1), "as")
1678 && validate_name(CHILD(tree, 2), NULL));
1679 else
1680 ok = validate_numnodes(tree, 3, "import_as_name");
1681 }
1682 return ok;
1683}
1684
1685
Fred Drake71137082001-01-07 05:59:59 +00001686/* dotted_name: NAME ("." NAME)*
1687 */
1688static int
1689validate_dotted_name(node *tree)
1690{
1691 int nch = NCH(tree);
1692 int res = (validate_ntype(tree, dotted_name)
1693 && is_odd(nch)
1694 && validate_name(CHILD(tree, 0), NULL));
1695 int i;
1696
1697 for (i = 1; res && (i < nch); i += 2) {
1698 res = (validate_dot(CHILD(tree, i))
1699 && validate_name(CHILD(tree, i+1), NULL));
1700 }
1701 return res;
1702}
1703
1704
Fred Drakecff283c2000-08-21 22:24:43 +00001705/* dotted_as_name: dotted_name [NAME NAME]
1706 */
1707static int
1708validate_dotted_as_name(node *tree)
1709{
1710 int nch = NCH(tree);
1711 int res = validate_ntype(tree, dotted_as_name);
1712
1713 if (res) {
1714 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001715 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001716 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001717 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001718 && validate_name(CHILD(tree, 1), "as")
1719 && validate_name(CHILD(tree, 2), NULL));
1720 else {
1721 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001722 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001723 }
1724 }
1725 return res;
1726}
1727
1728
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001729/* dotted_as_name (',' dotted_as_name)* */
1730static int
1731validate_dotted_as_names(node *tree)
1732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 int nch = NCH(tree);
1734 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1735 int i;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 for (i = 1; res && (i < nch); i += 2)
1738 res = (validate_comma(CHILD(tree, i))
1739 && validate_dotted_as_name(CHILD(tree, i + 1)));
1740 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001741}
1742
1743
1744/* import_as_name (',' import_as_name)* [','] */
1745static int
1746validate_import_as_names(node *tree)
1747{
1748 int nch = NCH(tree);
1749 int res = validate_import_as_name(CHILD(tree, 0));
1750 int i;
1751
1752 for (i = 1; res && (i + 1 < nch); i += 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 res = (validate_comma(CHILD(tree, i))
1754 && validate_import_as_name(CHILD(tree, i + 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001755 return (res);
1756}
1757
1758
1759/* 'import' dotted_as_names */
1760static int
1761validate_import_name(node *tree)
1762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 return (validate_ntype(tree, import_name)
1764 && validate_numnodes(tree, 2, "import_name")
1765 && validate_name(CHILD(tree, 0), "import")
1766 && validate_dotted_as_names(CHILD(tree, 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001767}
1768
Mark Dickinsonfeb3b752010-07-04 18:38:57 +00001769/* Helper function to count the number of leading dots (or ellipsis tokens) in
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001770 * 'from ...module import name'
1771 */
1772static int
1773count_from_dots(node *tree)
1774{
Mark Dickinsonfeb3b752010-07-04 18:38:57 +00001775 int i;
1776 for (i = 1; i < NCH(tree); i++)
1777 if (TYPE(CHILD(tree, i)) != DOT && TYPE(CHILD(tree, i)) != ELLIPSIS)
1778 break;
1779 return i - 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001780}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001781
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +00001782/* import_from: ('from' ('.'* dotted_name | '.'+)
1783 * 'import' ('*' | '(' import_as_names ')' | import_as_names))
Guido van Rossum3d602e31996-07-21 02:33:56 +00001784 */
Guido van Rossum47478871996-08-21 14:32:37 +00001785static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001786validate_import_from(node *tree)
1787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 int nch = NCH(tree);
1789 int ndots = count_from_dots(tree);
1790 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1791 int offset = ndots + havename;
1792 int res = validate_ntype(tree, import_from)
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +00001793 && (offset >= 1)
1794 && (nch >= 3 + offset)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 && validate_name(CHILD(tree, 0), "from")
1796 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1797 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1800 res = ((nch == offset + 5)
1801 && validate_lparen(CHILD(tree, offset + 2))
1802 && validate_import_as_names(CHILD(tree, offset + 3))
1803 && validate_rparen(CHILD(tree, offset + 4)));
1804 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1805 res = validate_import_as_names(CHILD(tree, offset + 2));
1806 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001807}
1808
1809
1810/* import_stmt: import_name | import_from */
1811static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001812validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001813{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001814 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001815 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001816
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001817 if (res) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 if (ntype == import_name || ntype == import_from)
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001821 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001822 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001823 res = 0;
1824 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001825 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001826 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001827 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001828 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001829 PyErr_Format(parser_error,
1830 "Unrecognized child node of import_stmt: %d.",
1831 TYPE(CHILD(tree, 0)));
1832 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001833 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001834}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001835
1836
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001837
1838
Guido van Rossum47478871996-08-21 14:32:37 +00001839static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001840validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001841{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001842 int j;
1843 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001844 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001845 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001846
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001847 if (!res && !PyErr_Occurred())
1848 err_string("illegal global statement");
1849
Guido van Rossum3d602e31996-07-21 02:33:56 +00001850 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001851 res = (validate_name(CHILD(tree, 0), "global")
1852 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001853 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001854 res = (validate_comma(CHILD(tree, j))
1855 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001856
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001857 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001858}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001859
1860
Guido van Rossum925e5471997-04-02 05:32:13 +00001861/* assert_stmt:
1862 *
1863 * 'assert' test [',' test]
1864 */
1865static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001866validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001867{
1868 int nch = NCH(tree);
1869 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001870 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001871 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001872 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001873
1874 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001875 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001876 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001877 res = (validate_comma(CHILD(tree, 2))
1878 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001879
1880 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001881}
Guido van Rossum925e5471997-04-02 05:32:13 +00001882
1883
Guido van Rossum47478871996-08-21 14:32:37 +00001884static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001885validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001886{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001887 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001888 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001889 && ((nch == 4) || (nch == 7))
1890 && validate_name(CHILD(tree, 0), "while")
1891 && validate_test(CHILD(tree, 1))
1892 && validate_colon(CHILD(tree, 2))
1893 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001894
Guido van Rossum3d602e31996-07-21 02:33:56 +00001895 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001896 res = (validate_name(CHILD(tree, 4), "else")
1897 && validate_colon(CHILD(tree, 5))
1898 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001899
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001900 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001901}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001902
1903
Guido van Rossum47478871996-08-21 14:32:37 +00001904static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001905validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001906{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001907 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001908 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001909 && ((nch == 6) || (nch == 9))
1910 && validate_name(CHILD(tree, 0), "for")
1911 && validate_exprlist(CHILD(tree, 1))
1912 && validate_name(CHILD(tree, 2), "in")
1913 && validate_testlist(CHILD(tree, 3))
1914 && validate_colon(CHILD(tree, 4))
1915 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001916
Guido van Rossum3d602e31996-07-21 02:33:56 +00001917 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001918 res = (validate_name(CHILD(tree, 6), "else")
1919 && validate_colon(CHILD(tree, 7))
1920 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001921
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001922 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001923}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001924
1925
Guido van Rossum3d602e31996-07-21 02:33:56 +00001926/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001927 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Georg Brandleee31162008-12-07 15:15:22 +00001928 ['finally' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001929 * | 'try' ':' suite 'finally' ':' suite
1930 *
1931 */
Guido van Rossum47478871996-08-21 14:32:37 +00001932static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001933validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001934{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001935 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001936 int pos = 3;
1937 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001938 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001939
1940 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001941 res = (validate_name(CHILD(tree, 0), "try")
1942 && validate_colon(CHILD(tree, 1))
1943 && validate_suite(CHILD(tree, 2))
1944 && validate_colon(CHILD(tree, nch - 2))
1945 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001946 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001947 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001948 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1949 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001950
1951 PyErr_Format(parser_error,
1952 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001953 }
Georg Brandleee31162008-12-07 15:15:22 +00001954 /* Handle try/finally statement */
1955 if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
1956 (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
1957 res = (validate_numnodes(tree, 6, "try/finally")
1958 && validate_colon(CHILD(tree, 4))
1959 && validate_suite(CHILD(tree, 5)));
1960 return (res);
1961 }
1962 /* try/except statement: skip past except_clause sections */
Antoine Pitrou37d1c182009-05-14 21:54:00 +00001963 while (res && pos < nch && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001964 res = (validate_except_clause(CHILD(tree, pos))
1965 && validate_colon(CHILD(tree, pos + 1))
1966 && validate_suite(CHILD(tree, pos + 2)));
1967 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001968 }
Georg Brandleee31162008-12-07 15:15:22 +00001969 /* skip else clause */
Antoine Pitrou37d1c182009-05-14 21:54:00 +00001970 if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) &&
Georg Brandleee31162008-12-07 15:15:22 +00001971 (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
1972 res = (validate_colon(CHILD(tree, pos + 1))
1973 && validate_suite(CHILD(tree, pos + 2)));
1974 pos += 3;
1975 }
1976 if (res && pos < nch) {
1977 /* last clause must be a finally */
1978 res = (validate_name(CHILD(tree, pos), "finally")
1979 && validate_numnodes(tree, pos + 3, "try/except/finally")
1980 && validate_colon(CHILD(tree, pos + 1))
1981 && validate_suite(CHILD(tree, pos + 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001982 }
1983 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001984}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001985
1986
Guido van Rossum47478871996-08-21 14:32:37 +00001987static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001988validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001989{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001990 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001991 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001992 && ((nch == 1) || (nch == 2) || (nch == 4))
1993 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001994
Guido van Rossum3d602e31996-07-21 02:33:56 +00001995 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001996 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001997 if (res && (nch == 4))
Guido van Rossumb940e112007-01-10 16:19:56 +00001998 res = (validate_name(CHILD(tree, 2), "as")
1999 && validate_ntype(CHILD(tree, 3), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002000
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002001 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002002}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002003
2004
Guido van Rossum47478871996-08-21 14:32:37 +00002005static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002006validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002007{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002008 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002009 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002010
Guido van Rossum3d602e31996-07-21 02:33:56 +00002011 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002012 res = ((nch == 1)
2013 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002014 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002015 res = validate_or_test(CHILD(tree, 0));
2016 res = (res && (nch == 1 || (nch == 5 &&
2017 validate_name(CHILD(tree, 1), "if") &&
2018 validate_or_test(CHILD(tree, 2)) &&
2019 validate_name(CHILD(tree, 3), "else") &&
2020 validate_test(CHILD(tree, 4)))));
2021 }
2022 return (res);
2023}
2024
2025static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002026validate_test_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002027{
2028 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002029 int res = validate_ntype(tree, test_nocond) && (nch == 1);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002030
Nick Coghlan650f0d02007-04-15 12:05:43 +00002031 if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
2032 res = (validate_lambdef_nocond(CHILD(tree, 0)));
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002033 else if (res) {
2034 res = (validate_or_test(CHILD(tree, 0)));
2035 }
2036 return (res);
2037}
2038
2039static int
2040validate_or_test(node *tree)
2041{
2042 int nch = NCH(tree);
2043 int res = validate_ntype(tree, or_test) && is_odd(nch);
2044
2045 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002046 int pos;
2047 res = validate_and_test(CHILD(tree, 0));
2048 for (pos = 1; res && (pos < nch); pos += 2)
2049 res = (validate_name(CHILD(tree, pos), "or")
2050 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002051 }
2052 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002053}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002054
2055
Guido van Rossum47478871996-08-21 14:32:37 +00002056static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002057validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002058{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002059 int pos;
2060 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002061 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002062 && is_odd(nch)
2063 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002064
Guido van Rossum3d602e31996-07-21 02:33:56 +00002065 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002066 res = (validate_name(CHILD(tree, pos), "and")
2067 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002068
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002069 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002070}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002071
2072
Guido van Rossum47478871996-08-21 14:32:37 +00002073static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002074validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002075{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002076 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002077 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002078
Guido van Rossum3d602e31996-07-21 02:33:56 +00002079 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002080 if (nch == 2)
2081 res = (validate_name(CHILD(tree, 0), "not")
2082 && validate_not_test(CHILD(tree, 1)));
2083 else if (nch == 1)
2084 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002085 }
2086 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002087}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002088
2089
Guido van Rossum47478871996-08-21 14:32:37 +00002090static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002091validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002092{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002093 int pos;
2094 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002095 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002096 && is_odd(nch)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002097 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002098
Guido van Rossum3d602e31996-07-21 02:33:56 +00002099 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002100 res = (validate_comp_op(CHILD(tree, pos))
Benjamin Peterson4905e802009-09-27 02:43:28 +00002101 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002102
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002103 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002104}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002105
2106
Guido van Rossum47478871996-08-21 14:32:37 +00002107static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002108validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002109{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002110 int res = 0;
2111 int nch = NCH(tree);
2112
Guido van Rossum3d602e31996-07-21 02:33:56 +00002113 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002114 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002115 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002116 /*
2117 * Only child will be a terminal with a well-defined symbolic name
2118 * or a NAME with a string of either 'is' or 'in'
2119 */
2120 tree = CHILD(tree, 0);
2121 switch (TYPE(tree)) {
2122 case LESS:
2123 case GREATER:
2124 case EQEQUAL:
2125 case EQUAL:
2126 case LESSEQUAL:
2127 case GREATEREQUAL:
2128 case NOTEQUAL:
2129 res = 1;
2130 break;
2131 case NAME:
2132 res = ((strcmp(STR(tree), "in") == 0)
2133 || (strcmp(STR(tree), "is") == 0));
2134 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002135 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002136 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002137 }
2138 break;
2139 default:
Fred Drake661ea262000-10-24 19:57:45 +00002140 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002141 break;
2142 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002143 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002144 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002145 res = (validate_ntype(CHILD(tree, 0), NAME)
2146 && validate_ntype(CHILD(tree, 1), NAME)
2147 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2148 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2149 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2150 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2151 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002152 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002153 }
2154 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002155}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002156
2157
Guido van Rossum47478871996-08-21 14:32:37 +00002158static int
Guido van Rossum0368b722007-05-11 16:50:42 +00002159validate_star_expr(node *tree)
2160{
2161 int res = validate_ntype(tree, star_expr);
2162 if (!res) return res;
Benjamin Peterson4905e802009-09-27 02:43:28 +00002163 if (!validate_numnodes(tree, 2, "star_expr"))
2164 return 0;
2165 return validate_ntype(CHILD(tree, 0), STAR) && \
2166 validate_expr(CHILD(tree, 1));
Guido van Rossum0368b722007-05-11 16:50:42 +00002167}
2168
2169
2170static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002171validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002172{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002173 int j;
2174 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002175 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002176 && is_odd(nch)
2177 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002178
Guido van Rossum3d602e31996-07-21 02:33:56 +00002179 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002180 res = (validate_xor_expr(CHILD(tree, j))
2181 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002182
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002183 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002184}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002185
2186
Guido van Rossum47478871996-08-21 14:32:37 +00002187static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002188validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002189{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002190 int j;
2191 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002192 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002193 && is_odd(nch)
2194 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002195
Guido van Rossum3d602e31996-07-21 02:33:56 +00002196 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002197 res = (validate_circumflex(CHILD(tree, j - 1))
2198 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002199
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002200 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
Fred Drakeff9ea482000-04-19 13:54:15 +00002205validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002206{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002207 int pos;
2208 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002209 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002210 && is_odd(nch)
2211 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002212
Guido van Rossum3d602e31996-07-21 02:33:56 +00002213 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002214 res = (validate_ampersand(CHILD(tree, pos))
2215 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002216
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002217 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002218}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002219
2220
2221static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002222validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002223 {
2224 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002225 int nch = NCH(tree);
2226 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002227 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002228
Guido van Rossum3d602e31996-07-21 02:33:56 +00002229 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002230 if (TYPE(CHILD(tree, pos)) != op1)
2231 res = validate_ntype(CHILD(tree, pos), op2);
2232 if (res)
2233 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002234 }
2235 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002236}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002237
2238
Guido van Rossum47478871996-08-21 14:32:37 +00002239static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002240validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002241{
2242 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002243 && validate_chain_two_ops(tree, validate_arith_expr,
2244 LEFTSHIFT, RIGHTSHIFT));
2245}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002246
2247
Guido van Rossum47478871996-08-21 14:32:37 +00002248static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002249validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002250{
2251 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002252 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2253}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002254
2255
Guido van Rossum47478871996-08-21 14:32:37 +00002256static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002257validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002258{
2259 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002260 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002261 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002262 && is_odd(nch)
2263 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002264
Guido van Rossum3d602e31996-07-21 02:33:56 +00002265 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002266 res = (((TYPE(CHILD(tree, pos)) == STAR)
2267 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002268 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002269 || (TYPE(CHILD(tree, pos)) == PERCENT))
2270 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002271
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002272 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002273}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002274
2275
Guido van Rossum3d602e31996-07-21 02:33:56 +00002276/* factor:
2277 *
2278 * factor: ('+'|'-'|'~') factor | power
2279 */
Guido van Rossum47478871996-08-21 14:32:37 +00002280static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002281validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002282{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002283 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002284 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002285 && (((nch == 2)
2286 && ((TYPE(CHILD(tree, 0)) == PLUS)
2287 || (TYPE(CHILD(tree, 0)) == MINUS)
2288 || (TYPE(CHILD(tree, 0)) == TILDE))
2289 && validate_factor(CHILD(tree, 1)))
2290 || ((nch == 1)
2291 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002292 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002293}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002294
2295
Guido van Rossum3d602e31996-07-21 02:33:56 +00002296/* power:
2297 *
2298 * power: atom trailer* ('**' factor)*
2299 */
Guido van Rossum47478871996-08-21 14:32:37 +00002300static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002301validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002302{
2303 int pos = 1;
2304 int nch = NCH(tree);
2305 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002306 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002307
2308 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002309 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002310 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002311 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002312 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002313 return (0);
2314 }
2315 for ( ; res && (pos < (nch - 1)); pos += 2)
2316 res = (validate_doublestar(CHILD(tree, pos))
2317 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002318 }
2319 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002320}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002321
2322
Guido van Rossum47478871996-08-21 14:32:37 +00002323static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002324validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002325{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002326 int pos;
2327 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002328 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002329
Fred Drakecff283c2000-08-21 22:24:43 +00002330 if (res && nch < 1)
2331 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002332 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002333 switch (TYPE(CHILD(tree, 0))) {
2334 case LPAR:
2335 res = ((nch <= 3)
2336 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002337
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002338 if (res && (nch == 3)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 if (TYPE(CHILD(tree, 1))==yield_expr)
2340 res = validate_yield_expr(CHILD(tree, 1));
2341 else
2342 res = validate_testlist_comp(CHILD(tree, 1));
2343 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002344 break;
2345 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002346 if (nch == 2)
2347 res = validate_ntype(CHILD(tree, 1), RSQB);
2348 else if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002349 res = (validate_testlist_comp(CHILD(tree, 1))
Fred Drakecff283c2000-08-21 22:24:43 +00002350 && validate_ntype(CHILD(tree, 2), RSQB));
2351 else {
2352 res = 0;
2353 err_string("illegal list display atom");
2354 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002355 break;
2356 case LBRACE:
2357 res = ((nch <= 3)
2358 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002359
Fred Drakeff9ea482000-04-19 13:54:15 +00002360 if (res && (nch == 3))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002361 res = validate_dictorsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002362 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002363 case NAME:
2364 case NUMBER:
2365 res = (nch == 1);
2366 break;
2367 case STRING:
2368 for (pos = 1; res && (pos < nch); ++pos)
2369 res = validate_ntype(CHILD(tree, pos), STRING);
2370 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 case DOT:
2372 res = (nch == 3 &&
2373 validate_ntype(CHILD(tree, 1), DOT) &&
2374 validate_ntype(CHILD(tree, 2), DOT));
2375 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002376 default:
2377 res = 0;
2378 break;
2379 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002380 }
2381 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002382}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002383
2384
Nick Coghlan650f0d02007-04-15 12:05:43 +00002385/* testlist_comp:
2386 * test ( comp_for | (',' test)* [','] )
Fred Drake85bf3bb2000-08-23 15:35:26 +00002387 */
Fred Drakecff283c2000-08-21 22:24:43 +00002388static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002389validate_testlist_comp(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00002390{
2391 int nch = NCH(tree);
2392 int ok = nch;
2393
2394 if (nch == 0)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002395 err_string("missing child nodes of testlist_comp");
2396 else {
Benjamin Peterson4905e802009-09-27 02:43:28 +00002397 ok = validate_test_or_star_expr(CHILD(tree, 0));
Nick Coghlan650f0d02007-04-15 12:05:43 +00002398 }
Fred Drakecff283c2000-08-21 22:24:43 +00002399
2400 /*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002401 * comp_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002402 */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002403 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2404 ok = validate_comp_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002405 else {
2406 /* (',' test)* [','] */
2407 int i = 1;
2408 while (ok && nch - i >= 2) {
2409 ok = (validate_comma(CHILD(tree, i))
Benjamin Peterson4905e802009-09-27 02:43:28 +00002410 && validate_test_or_star_expr(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002411 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002412 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002413 if (ok && i == nch-1)
2414 ok = validate_comma(CHILD(tree, i));
2415 else if (i != nch) {
2416 ok = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002417 err_string("illegal trailing nodes for testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002418 }
2419 }
2420 return ok;
2421}
Fred Drakecff283c2000-08-21 22:24:43 +00002422
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002423/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002424 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002425 */
2426static int
2427validate_decorator(node *tree)
2428{
2429 int ok;
2430 int nch = NCH(tree);
2431 ok = (validate_ntype(tree, decorator) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 (nch == 3 || nch == 5 || nch == 6) &&
2433 validate_at(CHILD(tree, 0)) &&
2434 validate_dotted_name(CHILD(tree, 1)) &&
2435 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002436
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002437 if (ok && nch != 3) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 ok = (validate_lparen(CHILD(tree, 2)) &&
2439 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 if (ok && nch == 6)
2442 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002443 }
2444
2445 return ok;
2446}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002447
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002448/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002449 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002450 */
2451static int
2452validate_decorators(node *tree)
2453{
Guido van Rossumfc158e22007-11-15 19:17:28 +00002454 int i, nch, ok;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002455 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002456 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002457
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002458 for (i = 0; ok && i < nch; ++i)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002460
2461 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002462}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002463
Georg Brandl0c315622009-05-25 21:10:36 +00002464/* with_item:
2465 * test ['as' expr]
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002466 */
2467static int
Georg Brandl0c315622009-05-25 21:10:36 +00002468validate_with_item(node *tree)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002469{
2470 int nch = NCH(tree);
Georg Brandl0c315622009-05-25 21:10:36 +00002471 int ok = (validate_ntype(tree, with_item)
2472 && (nch == 1 || nch == 3)
2473 && validate_test(CHILD(tree, 0)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 if (ok && nch == 3)
Georg Brandl0c315622009-05-25 21:10:36 +00002475 ok = (validate_name(CHILD(tree, 1), "as")
2476 && validate_expr(CHILD(tree, 2)));
2477 return ok;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002478}
2479
Georg Brandl0c315622009-05-25 21:10:36 +00002480/* with_stmt:
2481 * 0 1 ... -2 -1
2482 * 'with' with_item (',' with_item)* ':' suite
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002483 */
2484static int
2485validate_with_stmt(node *tree)
2486{
Georg Brandl0c315622009-05-25 21:10:36 +00002487 int i;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002488 int nch = NCH(tree);
2489 int ok = (validate_ntype(tree, with_stmt)
Georg Brandl0c315622009-05-25 21:10:36 +00002490 && (nch % 2 == 0)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002491 && validate_name(CHILD(tree, 0), "with")
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002492 && validate_colon(RCHILD(tree, -2))
2493 && validate_suite(RCHILD(tree, -1)));
Georg Brandl0c315622009-05-25 21:10:36 +00002494 for (i = 1; ok && i < nch - 2; i += 2)
2495 ok = validate_with_item(CHILD(tree, i));
2496 return ok;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002497}
2498
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01002499/* funcdef: 'def' NAME parameters ['->' test] ':' suite */
2500
Guido van Rossum47478871996-08-21 14:32:37 +00002501static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002502validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002503{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002504 int nch = NCH(tree);
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01002505 int res = validate_ntype(tree, funcdef);
2506 if (res) {
2507 if (nch == 5) {
2508 res = (validate_name(CHILD(tree, 0), "def")
2509 && validate_ntype(CHILD(tree, 1), NAME)
2510 && validate_parameters(CHILD(tree, 2))
2511 && validate_colon(CHILD(tree, 3))
2512 && validate_suite(CHILD(tree, 4)));
2513 }
2514 else if (nch == 7) {
2515 res = (validate_name(CHILD(tree, 0), "def")
2516 && validate_ntype(CHILD(tree, 1), NAME)
2517 && validate_parameters(CHILD(tree, 2))
2518 && validate_rarrow(CHILD(tree, 3))
2519 && validate_test(CHILD(tree, 4))
2520 && validate_colon(CHILD(tree, 5))
2521 && validate_suite(CHILD(tree, 6)));
2522 }
2523 else {
2524 res = 0;
2525 err_string("illegal number of children for funcdef");
2526 }
2527 }
2528 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00002529}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002530
2531
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002532/* decorated
2533 * decorators (classdef | funcdef)
2534 */
2535static int
2536validate_decorated(node *tree)
2537{
Mark Dickinson2bd61a92010-07-04 16:37:31 +00002538 int nch = NCH(tree);
2539 int ok = (validate_ntype(tree, decorated)
2540 && (nch == 2)
2541 && validate_decorators(RCHILD(tree, -2)));
2542 if (TYPE(RCHILD(tree, -1)) == funcdef)
2543 ok = ok && validate_funcdef(RCHILD(tree, -1));
2544 else
2545 ok = ok && validate_class(RCHILD(tree, -1));
2546 return ok;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002547}
2548
Guido van Rossum47478871996-08-21 14:32:37 +00002549static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002550validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002551{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002552 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002553 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002554 && ((nch == 3) || (nch == 4))
2555 && validate_name(CHILD(tree, 0), "lambda")
2556 && validate_colon(CHILD(tree, nch - 2))
2557 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002558
Guido van Rossum3d602e31996-07-21 02:33:56 +00002559 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002560 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002561 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002562 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002563
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002564 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002565}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002566
2567
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002568static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002569validate_lambdef_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002570{
2571 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002572 int res = (validate_ntype(tree, lambdef_nocond)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002573 && ((nch == 3) || (nch == 4))
2574 && validate_name(CHILD(tree, 0), "lambda")
2575 && validate_colon(CHILD(tree, nch - 2))
2576 && validate_test(CHILD(tree, nch - 1)));
2577
2578 if (res && (nch == 4))
2579 res = validate_varargslist(CHILD(tree, 1));
2580 else if (!res && !PyErr_Occurred())
Nick Coghlan650f0d02007-04-15 12:05:43 +00002581 (void) validate_numnodes(tree, 3, "lambdef_nocond");
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002582
2583 return (res);
2584}
2585
2586
Guido van Rossum3d602e31996-07-21 02:33:56 +00002587/* arglist:
2588 *
Fred Drakecff283c2000-08-21 22:24:43 +00002589 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002590 */
Guido van Rossum47478871996-08-21 14:32:37 +00002591static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002592validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002593{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002594 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002595 int i = 0;
2596 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002597
2598 if (nch <= 0)
2599 /* raise the right error from having an invalid number of children */
2600 return validate_numnodes(tree, nch + 1, "arglist");
2601
Raymond Hettinger354433a2004-05-19 08:20:33 +00002602 if (nch > 1) {
2603 for (i=0; i<nch; i++) {
2604 if (TYPE(CHILD(tree, i)) == argument) {
2605 node *ch = CHILD(tree, i);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002606 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
Raymond Hettinger354433a2004-05-19 08:20:33 +00002607 err_string("need '(', ')' for generator expression");
2608 return 0;
2609 }
2610 }
2611 }
2612 }
2613
Fred Drakecff283c2000-08-21 22:24:43 +00002614 while (ok && nch-i >= 2) {
2615 /* skip leading (argument ',') */
2616 ok = (validate_argument(CHILD(tree, i))
2617 && validate_comma(CHILD(tree, i+1)));
2618 if (ok)
2619 i += 2;
2620 else
2621 PyErr_Clear();
2622 }
2623 ok = 1;
2624 if (nch-i > 0) {
2625 /*
2626 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002627 */
Fred Drakecff283c2000-08-21 22:24:43 +00002628 int sym = TYPE(CHILD(tree, i));
2629
2630 if (sym == argument) {
2631 ok = validate_argument(CHILD(tree, i));
2632 if (ok && i+1 != nch) {
2633 err_string("illegal arglist specification"
2634 " (extra stuff on end)");
2635 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002636 }
Fred Drakecff283c2000-08-21 22:24:43 +00002637 }
2638 else if (sym == STAR) {
2639 ok = validate_star(CHILD(tree, i));
2640 if (ok && (nch-i == 2))
2641 ok = validate_test(CHILD(tree, i+1));
2642 else if (ok && (nch-i == 5))
2643 ok = (validate_test(CHILD(tree, i+1))
2644 && validate_comma(CHILD(tree, i+2))
2645 && validate_doublestar(CHILD(tree, i+3))
2646 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002647 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002648 err_string("illegal use of '*' in arglist");
2649 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002650 }
2651 }
Fred Drakecff283c2000-08-21 22:24:43 +00002652 else if (sym == DOUBLESTAR) {
2653 if (nch-i == 2)
2654 ok = (validate_doublestar(CHILD(tree, i))
2655 && validate_test(CHILD(tree, i+1)));
2656 else {
2657 err_string("illegal use of '**' in arglist");
2658 ok = 0;
2659 }
2660 }
2661 else {
2662 err_string("illegal arglist specification");
2663 ok = 0;
2664 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002665 }
2666 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002667}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002668
2669
2670
2671/* argument:
2672 *
Nick Coghlan650f0d02007-04-15 12:05:43 +00002673 * [test '='] test [comp_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002674 */
Guido van Rossum47478871996-08-21 14:32:37 +00002675static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002676validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002677{
2678 int nch = NCH(tree);
2679 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002680 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002681 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002682
Raymond Hettinger354433a2004-05-19 08:20:33 +00002683 if (res && (nch == 2))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002684 res = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002685 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002686 res = (validate_equal(CHILD(tree, 1))
2687 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002688
2689 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002690}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002691
2692
2693
2694/* trailer:
2695 *
Guido van Rossum47478871996-08-21 14:32:37 +00002696 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002697 */
Guido van Rossum47478871996-08-21 14:32:37 +00002698static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002699validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002700{
2701 int nch = NCH(tree);
2702 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002703
2704 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002705 switch (TYPE(CHILD(tree, 0))) {
2706 case LPAR:
2707 res = validate_rparen(CHILD(tree, nch - 1));
2708 if (res && (nch == 3))
2709 res = validate_arglist(CHILD(tree, 1));
2710 break;
2711 case LSQB:
2712 res = (validate_numnodes(tree, 3, "trailer")
2713 && validate_subscriptlist(CHILD(tree, 1))
2714 && validate_ntype(CHILD(tree, 2), RSQB));
2715 break;
2716 case DOT:
2717 res = (validate_numnodes(tree, 2, "trailer")
2718 && validate_ntype(CHILD(tree, 1), NAME));
2719 break;
2720 default:
2721 res = 0;
2722 break;
2723 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002724 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002725 else {
2726 (void) validate_numnodes(tree, 2, "trailer");
2727 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002728 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002729}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002730
2731
Guido van Rossum47478871996-08-21 14:32:37 +00002732/* subscriptlist:
2733 *
2734 * subscript (',' subscript)* [',']
2735 */
2736static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002737validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002738{
2739 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002740 validate_subscript, "subscriptlist"));
2741}
Guido van Rossum47478871996-08-21 14:32:37 +00002742
2743
Guido van Rossum3d602e31996-07-21 02:33:56 +00002744/* subscript:
2745 *
Guido van Rossum47478871996-08-21 14:32:37 +00002746 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002747 */
Guido van Rossum47478871996-08-21 14:32:37 +00002748static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002749validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002750{
Guido van Rossum47478871996-08-21 14:32:37 +00002751 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002752 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002753 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002754
Guido van Rossum47478871996-08-21 14:32:37 +00002755 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002756 if (!PyErr_Occurred())
2757 err_string("invalid number of arguments for subscript node");
2758 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002759 }
Guido van Rossum47478871996-08-21 14:32:37 +00002760 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002761 /* take care of ('.' '.' '.') possibility */
2762 return (validate_numnodes(tree, 3, "subscript")
2763 && validate_dot(CHILD(tree, 0))
2764 && validate_dot(CHILD(tree, 1))
2765 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002766 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002767 if (TYPE(CHILD(tree, 0)) == test)
2768 res = validate_test(CHILD(tree, 0));
2769 else
2770 res = validate_colon(CHILD(tree, 0));
2771 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002772 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002773 /* Must be [test] ':' [test] [sliceop],
2774 * but at least one of the optional components will
2775 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002776 */
2777 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002778 res = validate_test(CHILD(tree, 0));
2779 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002780 }
2781 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002782 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002783 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002784 int rem = nch - ++offset;
2785 if (rem) {
2786 if (TYPE(CHILD(tree, offset)) == test) {
2787 res = validate_test(CHILD(tree, offset));
2788 ++offset;
2789 --rem;
2790 }
2791 if (res && rem)
2792 res = validate_sliceop(CHILD(tree, offset));
2793 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002794 }
2795 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002796}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002797
2798
Guido van Rossum47478871996-08-21 14:32:37 +00002799static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002800validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002801{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002802 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002803 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002804 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002805 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002806 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002807 }
Guido van Rossum47478871996-08-21 14:32:37 +00002808 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002809 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002810 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002811 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002812
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002813 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002814}
Guido van Rossum47478871996-08-21 14:32:37 +00002815
2816
2817static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00002818validate_test_or_star_expr(node *n)
2819{
2820 if (TYPE(n) == test)
2821 return validate_test(n);
2822 return validate_star_expr(n);
2823}
2824
2825static int
2826validate_expr_or_star_expr(node *n)
2827{
2828 if (TYPE(n) == expr)
2829 return validate_expr(n);
2830 return validate_star_expr(n);
2831}
2832
2833
2834static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002835validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002836{
2837 return (validate_repeating_list(tree, exprlist,
Benjamin Peterson4905e802009-09-27 02:43:28 +00002838 validate_expr_or_star_expr, "exprlist"));
Fred Drakeff9ea482000-04-19 13:54:15 +00002839}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002840
2841
Guido van Rossum47478871996-08-21 14:32:37 +00002842static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002843validate_dictorsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002844{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002845 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002846 int res = (validate_ntype(tree, dictorsetmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002847 && (nch >= 3)
2848 && validate_test(CHILD(tree, 0))
2849 && validate_colon(CHILD(tree, 1))
2850 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002851
Guido van Rossum3d602e31996-07-21 02:33:56 +00002852 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002853 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002854 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002855 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002856
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002857 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002858 int pos = 3;
2859 /* ( ',' test ':' test )* */
2860 while (res && (pos < nch)) {
2861 res = (validate_comma(CHILD(tree, pos))
2862 && validate_test(CHILD(tree, pos + 1))
2863 && validate_colon(CHILD(tree, pos + 2))
2864 && validate_test(CHILD(tree, pos + 3)));
2865 pos += 4;
2866 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002867 }
2868 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002869}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002870
2871
Guido van Rossum47478871996-08-21 14:32:37 +00002872static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002873validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002874{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002875 int pos;
2876 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002877 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002878 && (nch >= 2)
2879 && validate_testlist(CHILD(tree, 0))
2880 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002881
Guido van Rossum3d602e31996-07-21 02:33:56 +00002882 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002883 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002884
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002885 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002886}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002887
2888
Guido van Rossum47478871996-08-21 14:32:37 +00002889static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002890validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002891{
Fred Drakeff9ea482000-04-19 13:54:15 +00002892 int nch = 0; /* num. children on current node */
2893 int res = 1; /* result value */
2894 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002895
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002896 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002897 nch = NCH(tree);
2898 next = 0;
2899 switch (TYPE(tree)) {
2900 /*
2901 * Definition nodes.
2902 */
2903 case funcdef:
2904 res = validate_funcdef(tree);
2905 break;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002906 case with_stmt:
2907 res = validate_with_stmt(tree);
2908 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002909 case classdef:
2910 res = validate_class(tree);
2911 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 case decorated:
2913 res = validate_decorated(tree);
2914 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002915 /*
2916 * "Trivial" parse tree nodes.
2917 * (Why did I call these trivial?)
2918 */
2919 case stmt:
2920 res = validate_stmt(tree);
2921 break;
2922 case small_stmt:
2923 /*
Guido van Rossum452bf512007-02-09 05:32:43 +00002924 * expr_stmt | del_stmt | pass_stmt | flow_stmt
Georg Brandl7cae87c2006-09-06 06:51:57 +00002925 * | import_stmt | global_stmt | assert_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002926 */
2927 res = validate_small_stmt(tree);
2928 break;
2929 case flow_stmt:
2930 res = (validate_numnodes(tree, 1, "flow_stmt")
2931 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2932 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002933 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002934 || (TYPE(CHILD(tree, 0)) == return_stmt)
2935 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2936 if (res)
2937 next = CHILD(tree, 0);
2938 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002939 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002940 break;
Fred Drake02126f22001-07-17 02:59:15 +00002941 case yield_stmt:
2942 res = validate_yield_stmt(tree);
2943 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002944 /*
2945 * Compound statements.
2946 */
2947 case simple_stmt:
2948 res = validate_simple_stmt(tree);
2949 break;
2950 case compound_stmt:
2951 res = validate_compound_stmt(tree);
2952 break;
2953 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002954 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002955 */
2956 case expr_stmt:
2957 res = validate_expr_stmt(tree);
2958 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002959 case del_stmt:
2960 res = validate_del_stmt(tree);
2961 break;
2962 case pass_stmt:
2963 res = (validate_numnodes(tree, 1, "pass")
2964 && validate_name(CHILD(tree, 0), "pass"));
2965 break;
2966 case break_stmt:
2967 res = (validate_numnodes(tree, 1, "break")
2968 && validate_name(CHILD(tree, 0), "break"));
2969 break;
2970 case continue_stmt:
2971 res = (validate_numnodes(tree, 1, "continue")
2972 && validate_name(CHILD(tree, 0), "continue"));
2973 break;
2974 case return_stmt:
2975 res = validate_return_stmt(tree);
2976 break;
2977 case raise_stmt:
2978 res = validate_raise_stmt(tree);
2979 break;
2980 case import_stmt:
2981 res = validate_import_stmt(tree);
2982 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 case import_name:
2984 res = validate_import_name(tree);
2985 break;
2986 case import_from:
2987 res = validate_import_from(tree);
2988 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002989 case global_stmt:
2990 res = validate_global_stmt(tree);
2991 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002992 case assert_stmt:
2993 res = validate_assert_stmt(tree);
2994 break;
2995 case if_stmt:
2996 res = validate_if(tree);
2997 break;
2998 case while_stmt:
2999 res = validate_while(tree);
3000 break;
3001 case for_stmt:
3002 res = validate_for(tree);
3003 break;
3004 case try_stmt:
3005 res = validate_try(tree);
3006 break;
3007 case suite:
3008 res = validate_suite(tree);
3009 break;
3010 /*
3011 * Expression nodes.
3012 */
3013 case testlist:
3014 res = validate_testlist(tree);
3015 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003016 case yield_expr:
3017 res = validate_yield_expr(tree);
3018 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003019 case test:
3020 res = validate_test(tree);
3021 break;
3022 case and_test:
3023 res = validate_and_test(tree);
3024 break;
3025 case not_test:
3026 res = validate_not_test(tree);
3027 break;
3028 case comparison:
3029 res = validate_comparison(tree);
3030 break;
3031 case exprlist:
3032 res = validate_exprlist(tree);
3033 break;
3034 case comp_op:
3035 res = validate_comp_op(tree);
3036 break;
3037 case expr:
3038 res = validate_expr(tree);
3039 break;
3040 case xor_expr:
3041 res = validate_xor_expr(tree);
3042 break;
3043 case and_expr:
3044 res = validate_and_expr(tree);
3045 break;
3046 case shift_expr:
3047 res = validate_shift_expr(tree);
3048 break;
3049 case arith_expr:
3050 res = validate_arith_expr(tree);
3051 break;
3052 case term:
3053 res = validate_term(tree);
3054 break;
3055 case factor:
3056 res = validate_factor(tree);
3057 break;
3058 case power:
3059 res = validate_power(tree);
3060 break;
3061 case atom:
3062 res = validate_atom(tree);
3063 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003064
Fred Drakeff9ea482000-04-19 13:54:15 +00003065 default:
3066 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00003067 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003068 res = 0;
3069 break;
3070 }
3071 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003072 }
3073 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003074}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003075
3076
Guido van Rossum47478871996-08-21 14:32:37 +00003077static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003078validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003079{
3080 int res = validate_eval_input(tree);
3081
3082 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003083 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003084
3085 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003086}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003087
3088
Guido van Rossum3d602e31996-07-21 02:33:56 +00003089/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003090 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003091 */
Guido van Rossum47478871996-08-21 14:32:37 +00003092static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003093validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003094{
Fred Drakec2683dd2001-07-17 19:32:05 +00003095 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003096 int nch = NCH(tree) - 1;
3097 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003098 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003099
Fred Drakec2683dd2001-07-17 19:32:05 +00003100 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003101 if (TYPE(CHILD(tree, j)) == stmt)
3102 res = validate_stmt(CHILD(tree, j));
3103 else
3104 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003105 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003106 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003107 * user. Hopefully, this won't be needed. If a user reports getting
3108 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003109 */
3110 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003111 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003112
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003113 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003114}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003115
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003116static int
3117validate_encoding_decl(node *tree)
3118{
3119 int nch = NCH(tree);
3120 int res = ((nch == 1)
3121 && validate_file_input(CHILD(tree, 0)));
3122
3123 if (!res && !PyErr_Occurred())
3124 err_string("Error Parsing encoding_decl");
3125
3126 return res;
3127}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003128
Fred Drake43f8f9b1998-04-13 16:25:46 +00003129static PyObject*
3130pickle_constructor = NULL;
3131
3132
3133static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003134parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003135{
Fred Drake268397f1998-04-29 14:16:32 +00003136 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003137 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003138 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003139 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003140
Fred Drakec2683dd2001-07-17 19:32:05 +00003141 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003142 PyObject *newargs;
3143 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003144
Fred Drake2a6875e1999-09-20 22:32:18 +00003145 if ((empty_dict = PyDict_New()) == NULL)
3146 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003147 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003148 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003149 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003150 if (tuple != NULL) {
3151 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3152 Py_DECREF(tuple);
3153 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003154 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003155 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003156 }
3157 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003158 Py_XDECREF(empty_dict);
3159
Fred Drake43f8f9b1998-04-13 16:25:46 +00003160 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003161}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003162
3163
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003164/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003165 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003166 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003167 * We'd really have to write a wrapper around it all anyway to allow
3168 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003169 */
3170static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003171 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003172 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003173 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003174 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003175 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003176 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003177 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003178 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003179 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003180 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003181 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003182 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003183 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003184 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003185 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003186 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003187 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003188 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003189
Fred Drake43f8f9b1998-04-13 16:25:46 +00003190 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003191 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003192 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003193
Fred Drake268397f1998-04-29 14:16:32 +00003194 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003195 };
3196
3197
Martin v. Löwis1a214512008-06-11 05:26:20 +00003198
3199static struct PyModuleDef parsermodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003200 PyModuleDef_HEAD_INIT,
3201 "parser",
3202 NULL,
3203 -1,
3204 parser_functions,
3205 NULL,
3206 NULL,
3207 NULL,
3208 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00003209};
3210
3211PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003212
Mark Hammond62b1ab12002-07-23 06:31:15 +00003213PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003214PyInit_parser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003215{
Fred Drake13130bc2001-08-15 16:44:56 +00003216 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003217
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00003218 if (PyType_Ready(&PyST_Type) < 0)
3219 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00003220 module = PyModule_Create(&parsermodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003221 if (module == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 return NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003223
Fred Drake7a15ba51999-09-09 14:21:52 +00003224 if (parser_error == 0)
3225 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003226
Tim Peters6a627252003-07-21 14:25:23 +00003227 if (parser_error == 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003228 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003229 /* CAUTION: The code next used to skip bumping the refcount on
Martin v. Löwis1a214512008-06-11 05:26:20 +00003230 * parser_error. That's a disaster if PyInit_parser() gets called more
Tim Peters6a627252003-07-21 14:25:23 +00003231 * than once. By incref'ing, we ensure that each module dict that
3232 * gets created owns its reference to the shared parser_error object,
3233 * and the file static parser_error vrbl owns a reference too.
3234 */
3235 Py_INCREF(parser_error);
3236 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003237 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003238
Fred Drakec2683dd2001-07-17 19:32:05 +00003239 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003240 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003241
Fred Drake13130bc2001-08-15 16:44:56 +00003242 PyModule_AddStringConstant(module, "__copyright__",
3243 parser_copyright_string);
3244 PyModule_AddStringConstant(module, "__doc__",
3245 parser_doc_string);
3246 PyModule_AddStringConstant(module, "__version__",
3247 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003248
Fred Drake78bdb9b2001-07-19 20:17:15 +00003249 /* Register to support pickling.
3250 * If this fails, the import of this module will fail because an
3251 * exception will be raised here; should we clear the exception?
3252 */
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003253 copyreg = PyImport_ImportModuleNoBlock("copyreg");
Fred Drake13130bc2001-08-15 16:44:56 +00003254 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003255 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003256
Fred Drake13130bc2001-08-15 16:44:56 +00003257 func = PyObject_GetAttrString(copyreg, "pickle");
3258 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3259 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003260 Py_XINCREF(pickle_constructor);
3261 if ((func != NULL) && (pickle_constructor != NULL)
3262 && (pickler != NULL)) {
3263 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003264
Thomas Wouters477c8d52006-05-27 19:21:47 +00003265 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3266 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003267 Py_XDECREF(res);
3268 }
3269 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003270 Py_XDECREF(pickle_constructor);
3271 Py_XDECREF(pickler);
3272 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003273 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00003274 return module;
Fred Drakeff9ea482000-04-19 13:54:15 +00003275}