blob: 8c57f867c9d57b810d7095f15e5b6e8fb13b3637 [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#include "compile.h"
38#undef Yield
39#include "ast.h"
40#include "pyarena.h"
41
42extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000043
Fred Drake268397f1998-04-29 14:16:32 +000044#ifdef lint
45#include <note.h>
46#else
47#define NOTE(x)
48#endif
49
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000050/* String constants used to initialize module attributes.
51 *
52 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000053static char parser_copyright_string[] =
54"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000055University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
56Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
57Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000058
59
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000060PyDoc_STRVAR(parser_doc_string,
61"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000062
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000063static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000064
65
Martin v. Löwis18e16552006-02-15 17:27:45 +000066typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000067typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000068 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000069 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000070
Thomas Wouters7e474022000-07-16 12:04:32 +000071/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000072 * original copyright statement is included below, and continues to apply
73 * in full to the function immediately following. All other material is
74 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
75 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000076 * new naming conventions. Added arguments to provide support for creating
77 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000078 */
79
Guido van Rossum52f2c051993-11-10 12:53:24 +000080
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000081static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000082node2tuple(node *n, /* node to convert */
83 SeqMaker mkseq, /* create sequence */
84 SeqInserter addelem, /* func. to add elem. in seq. */
Thomas Wouters89f507f2006-12-13 04:49:30 +000085 int lineno, /* include line numbers? */
86 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000087{
Guido van Rossum3d602e31996-07-21 02:33:56 +000088 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000089 Py_INCREF(Py_None);
90 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000091 }
92 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000093 int i;
94 PyObject *v;
95 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000096
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000097 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000098 if (v == NULL)
99 return (v);
Christian Heimes217cfd12007-12-02 14:31:20 +0000100 w = PyLong_FromLong(TYPE(n));
Fred Drakeff9ea482000-04-19 13:54:15 +0000101 if (w == NULL) {
102 Py_DECREF(v);
103 return ((PyObject*) NULL);
104 }
105 (void) addelem(v, 0, w);
106 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000107 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000108 if (w == NULL) {
109 Py_DECREF(v);
110 return ((PyObject*) NULL);
111 }
112 (void) addelem(v, i+1, w);
113 }
Tim Peters6a627252003-07-21 14:25:23 +0000114
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000115 if (TYPE(n) == encoding_decl)
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000116 (void) addelem(v, i+1, PyUnicode_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000117 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000118 }
119 else if (ISTERMINAL(TYPE(n))) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000120 PyObject *result = mkseq(2 + lineno + col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000121 if (result != NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000122 (void) addelem(result, 0, PyLong_FromLong(TYPE(n)));
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000123 (void) addelem(result, 1, PyUnicode_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000124 if (lineno == 1)
Christian Heimes217cfd12007-12-02 14:31:20 +0000125 (void) addelem(result, 2, PyLong_FromLong(n->n_lineno));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000126 if (col_offset == 1)
Christian Heimes217cfd12007-12-02 14:31:20 +0000127 (void) addelem(result, 3, PyLong_FromLong(n->n_col_offset));
Fred Drakeff9ea482000-04-19 13:54:15 +0000128 }
129 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000130 }
131 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000132 PyErr_SetString(PyExc_SystemError,
133 "unrecognized parse tree node type");
134 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000135 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000136}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000137/*
138 * End of material copyrighted by Stichting Mathematisch Centrum.
139 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000140
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000141
142
143/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000144 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000145 * field of the object type to identify which any given object represents.
146 * These should probably go in an external header to allow other extensions
147 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000148 */
149
Fred Drakec2683dd2001-07-17 19:32:05 +0000150#define PyST_EXPR 1
151#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000152
153
154/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000155 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000156 * naming style, but the code uses the new naming convention.
157 */
158
159static PyObject*
160parser_error = 0;
161
162
Fred Drakec2683dd2001-07-17 19:32:05 +0000163typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000164 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000165 node* st_node; /* the node* returned by the parser */
166 int st_type; /* EXPR or SUITE ? */
Benjamin Petersonf216c942008-10-31 02:28:05 +0000167 PyCompilerFlags st_flags; /* Parser and compiler flags */
Fred Drakec2683dd2001-07-17 19:32:05 +0000168} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000169
170
Jeremy Hylton938ace62002-07-17 16:30:39 +0000171static void parser_free(PyST_Object *st);
Mark Dickinson211c6252009-02-01 10:28:51 +0000172static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op);
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000173static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
174static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
175static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
176static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
177static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
Fred Drake503d8d61998-04-13 18:45:18 +0000178
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000179#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
180
181static PyMethodDef parser_methods[] = {
182 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
183 PyDoc_STR("Compile this ST object into a code object.")},
184 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
185 PyDoc_STR("Determines if this ST object was created from an expression.")},
186 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
187 PyDoc_STR("Determines if this ST object was created from a suite.")},
188 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
189 PyDoc_STR("Creates a list-tree representation of this ST.")},
190 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
191 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
192
193 {NULL, NULL, 0, NULL}
194};
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000195
Fred Drake268397f1998-04-29 14:16:32 +0000196static
Fred Drakec2683dd2001-07-17 19:32:05 +0000197PyTypeObject PyST_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000198 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000199 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000200 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000201 0, /* tp_itemsize */
202 (destructor)parser_free, /* tp_dealloc */
203 0, /* tp_print */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000204 0, /* tp_getattr */
Fred Drakeff9ea482000-04-19 13:54:15 +0000205 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000206 0, /* tp_reserved */
Fred Drakeff9ea482000-04-19 13:54:15 +0000207 0, /* tp_repr */
208 0, /* tp_as_number */
209 0, /* tp_as_sequence */
210 0, /* tp_as_mapping */
211 0, /* tp_hash */
212 0, /* tp_call */
213 0, /* tp_str */
214 0, /* tp_getattro */
215 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000216
217 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000218 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000219
Fred Drakeff9ea482000-04-19 13:54:15 +0000220 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000221
222 /* __doc__ */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000223 "Intermediate representation of a Python parse tree.",
224 0, /* tp_traverse */
225 0, /* tp_clear */
Mark Dickinson211c6252009-02-01 10:28:51 +0000226 parser_richcompare, /* tp_richcompare */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000227 0, /* tp_weaklistoffset */
228 0, /* tp_iter */
229 0, /* tp_iternext */
230 parser_methods, /* tp_methods */
Fred Drakec2683dd2001-07-17 19:32:05 +0000231}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000232
233
Mark Dickinson211c6252009-02-01 10:28:51 +0000234/* PyST_Type isn't subclassable, so just check ob_type */
235#define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type)
236
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000237static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000238parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000239{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000240 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000241
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000242 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000243 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000244
245 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000246 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247
248 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000249 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000250
251 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000252 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000253
254 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000255 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000256
257 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000258 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000259
Fred Drakeff9ea482000-04-19 13:54:15 +0000260 if (v != 0)
261 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000262 }
263 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000264}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265
Mark Dickinson211c6252009-02-01 10:28:51 +0000266/* parser_richcompare(PyObject* left, PyObject* right, int op)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000267 *
268 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
269 * This really just wraps a call to parser_compare_nodes() with some easy
270 * checks and protection code.
271 *
272 */
Mark Dickinson211c6252009-02-01 10:28:51 +0000273
274#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
275
276static PyObject *
277parser_richcompare(PyObject *left, PyObject *right, int op)
Guido van Rossum47478871996-08-21 14:32:37 +0000278{
Mark Dickinson211c6252009-02-01 10:28:51 +0000279 int result;
280 PyObject *v;
281
282 /* neither argument should be NULL, unless something's gone wrong */
283 if (left == NULL || right == NULL) {
284 PyErr_BadInternalCall();
285 return NULL;
286 }
287
288 /* both arguments should be instances of PyST_Object */
289 if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) {
290 v = Py_NotImplemented;
291 goto finished;
292 }
293
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000294 if (left == right)
Mark Dickinson211c6252009-02-01 10:28:51 +0000295 /* if arguments are identical, they're equal */
296 result = 0;
297 else
298 result = parser_compare_nodes(((PyST_Object *)left)->st_node,
299 ((PyST_Object *)right)->st_node);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000300
Mark Dickinson211c6252009-02-01 10:28:51 +0000301 /* Convert return value to a Boolean */
302 switch (op) {
303 case Py_EQ:
304 v = TEST_COND(result == 0);
305 break;
306 case Py_NE:
307 v = TEST_COND(result != 0);
308 break;
309 case Py_LE:
310 v = TEST_COND(result <= 0);
311 break;
312 case Py_GE:
313 v = TEST_COND(result >= 0);
314 break;
315 case Py_LT:
316 v = TEST_COND(result < 0);
317 break;
318 case Py_GT:
319 v = TEST_COND(result > 0);
320 break;
321 default:
322 PyErr_BadArgument();
323 return NULL;
324 }
325 finished:
326 Py_INCREF(v);
327 return v;
Fred Drakeff9ea482000-04-19 13:54:15 +0000328}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000329
Fred Drakec2683dd2001-07-17 19:32:05 +0000330/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000331 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000332 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000333 * 'wrapper' object that holds a node* and allows it to be passed around in
334 * Python code.
335 *
336 */
337static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000338parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000339{
Fred Drakec2683dd2001-07-17 19:32:05 +0000340 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000341
342 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000343 o->st_node = st;
344 o->st_type = type;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000345 o->st_flags.cf_flags = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000346 }
Fred Drake268397f1998-04-29 14:16:32 +0000347 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000348 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000349 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000350 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000351}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000352
353
Fred Drakec2683dd2001-07-17 19:32:05 +0000354/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000355 *
356 * This is called by a del statement that reduces the reference count to 0.
357 *
358 */
359static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000360parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000361{
Fred Drakec2683dd2001-07-17 19:32:05 +0000362 PyNode_Free(st->st_node);
363 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000364}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000365
366
Fred Drakec2683dd2001-07-17 19:32:05 +0000367/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000368 *
369 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000370 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000371 *
372 */
373static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000374parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000375{
Guido van Rossum47478871996-08-21 14:32:37 +0000376 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000377 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000378 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000379 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000380
Georg Brandl30704ea02008-07-23 15:07:12 +0000381 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000382
Martin v. Löwis1a214512008-06-11 05:26:20 +0000383 if (self == NULL || PyModule_Check(self)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000384 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
385 &PyST_Type, &self, &line_option,
386 &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000387 }
Fred Drake503d8d61998-04-13 18:45:18 +0000388 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000389 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
390 &line_option, &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000391 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000392 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000393 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000394 if (line_option != NULL) {
395 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
396 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000397 if (col_option != NULL) {
398 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
399 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000400 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000401 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000402 * since it's known to work already.
403 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000404 res = node2tuple(((PyST_Object*)self)->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000405 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000406 }
407 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000408}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000409
410
Fred Drakec2683dd2001-07-17 19:32:05 +0000411/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000412 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000413 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000414 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000415 *
416 */
417static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000418parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000419{
Guido van Rossum47478871996-08-21 14:32:37 +0000420 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000421 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000422 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000423 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000424
Georg Brandl30704ea02008-07-23 15:07:12 +0000425 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000426
Martin v. Löwis1a214512008-06-11 05:26:20 +0000427 if (self == NULL || PyModule_Check(self))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000428 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
429 &PyST_Type, &self, &line_option,
430 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000431 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000432 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
433 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000434 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000435 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000436 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000437 if (line_option != 0) {
438 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
439 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000440 if (col_option != NULL) {
441 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
442 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000443 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000444 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000445 * since it's known to work already.
446 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000447 res = node2tuple(self->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000448 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000449 }
450 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000451}
Guido van Rossum47478871996-08-21 14:32:37 +0000452
453
Fred Drakec2683dd2001-07-17 19:32:05 +0000454/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000455 *
456 * This function creates code objects from the parse tree represented by
457 * the passed-in data object. An optional file name is passed in as well.
458 *
459 */
460static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000461parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000462{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000463 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000464 PyArena* arena;
465 mod_ty mod;
Fred Drakec2683dd2001-07-17 19:32:05 +0000466 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000467 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000468
Georg Brandl30704ea02008-07-23 15:07:12 +0000469 static char *keywords[] = {"st", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000470
Martin v. Löwis1a214512008-06-11 05:26:20 +0000471 if (self == NULL || PyModule_Check(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000472 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
473 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000474 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000475 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000476 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000477
Benjamin Petersonf216c942008-10-31 02:28:05 +0000478 if (ok) {
479 arena = PyArena_New();
480 if (arena) {
481 mod = PyAST_FromNode(self->st_node, &(self->st_flags), str, arena);
482 if (mod) {
483 res = (PyObject *)PyAST_Compile(mod, str, &(self->st_flags), arena);
484 }
485 PyArena_Free(arena);
486 }
487 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000488
489 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000490}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000491
492
493/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
494 * PyObject* parser_issuite(PyObject* self, PyObject* args)
495 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000496 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000497 * a statement suite, respectively. The return is a Python truth value.
498 *
499 */
500static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000501parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000502{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000503 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000504 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000505
Georg Brandl30704ea02008-07-23 15:07:12 +0000506 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000507
Martin v. Löwis1a214512008-06-11 05:26:20 +0000508 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000509 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000510 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000511 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000512 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000513
514 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000515 /* Check to see if the ST represents an expression or not. */
516 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000517 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000518 }
519 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000520}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000521
522
523static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000524parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000525{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000526 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000527 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000528
Georg Brandl30704ea02008-07-23 15:07:12 +0000529 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000530
Martin v. Löwis1a214512008-06-11 05:26:20 +0000531 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000532 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000533 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000534 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000535 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000536
537 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000538 /* Check to see if the ST represents an expression or not. */
539 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000540 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000541 }
542 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000543}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000544
545
Guido van Rossum3d602e31996-07-21 02:33:56 +0000546/* err_string(char* message)
547 *
548 * Sets the error string for an exception of type ParserError.
549 *
550 */
551static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000552err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000553{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000554 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000555}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000556
557
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000558/* PyObject* parser_do_parse(PyObject* args, int type)
559 *
560 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000561 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000562 *
563 */
564static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000565parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000566{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000567 char* string = 0;
568 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000569 int flags = 0;
570 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000571
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000572 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000573
574 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000575 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
576 &_PyParser_Grammar,
577 (type == PyST_EXPR)
578 ? eval_input : file_input,
579 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 if (n) {
582 res = parser_newstobject(n, type);
Benjamin Petersonf216c942008-10-31 02:28:05 +0000583 if (res)
584 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
585 }
586 else
587 PyParser_SetError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000588 }
589 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000590}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000591
592
593/* PyObject* parser_expr(PyObject* self, PyObject* args)
594 * PyObject* parser_suite(PyObject* self, PyObject* args)
595 *
596 * External interfaces to the parser itself. Which is called determines if
597 * the parser attempts to recognize an expression ('eval' form) or statement
598 * suite ('exec' form). The real work is done by parser_do_parse() above.
599 *
600 */
601static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000602parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000603{
Fred Drake268397f1998-04-29 14:16:32 +0000604 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000605 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000606}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000607
608
609static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000610parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000611{
Fred Drake268397f1998-04-29 14:16:32 +0000612 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000613 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000614}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000615
616
617
Fred Drakec2683dd2001-07-17 19:32:05 +0000618/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000619 * object requires that the input tuple be valid without having to rely on
620 * catching an exception from the compiler. This is done to allow the
621 * compiler itself to remain fast, since most of its input will come from
622 * the parser directly, and therefore be known to be syntactically correct.
623 * This validation is done to ensure that we don't core dump the compile
624 * phase, returning an exception instead.
625 *
626 * Two aspects can be broken out in this code: creating a node tree from
627 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000628 * advantageous to expand the number of ST types to include funcdefs and
629 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000630 * here. They are not necessary, and not quite as useful in a raw form.
631 * For now, let's get expressions and suites working reliably.
632 */
633
634
Jeremy Hylton938ace62002-07-17 16:30:39 +0000635static node* build_node_tree(PyObject *tuple);
636static int validate_expr_tree(node *tree);
637static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000638static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000639
Fred Drakec2683dd2001-07-17 19:32:05 +0000640/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000641 *
642 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000643 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000644 * tuple can be validated. It does this by checking the first code of the
645 * tuple, and, if acceptable, builds the internal representation. If this
646 * step succeeds, the internal representation is validated as fully as
647 * possible with the various validate_*() routines defined below.
648 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000649 * This function must be changed if support is to be added for PyST_FRAGMENT
650 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000651 *
652 */
653static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000654parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000655{
Fred Drake268397f1998-04-29 14:16:32 +0000656 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000657 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000658 PyObject *tuple;
659 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000660
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000661 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000662
Fred Drakec2683dd2001-07-17 19:32:05 +0000663 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000664 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000665 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000666 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000667 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000668 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000669 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000670 }
671 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000672 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000673 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000674 tree = build_node_tree(tuple);
675 if (tree != 0) {
676 int start_sym = TYPE(tree);
677 if (start_sym == eval_input) {
678 /* Might be an eval form. */
679 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000680 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000681 else
682 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000683 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000684 else if (start_sym == file_input) {
685 /* This looks like an exec form so far. */
686 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000687 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000688 else
689 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000690 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000691 else if (start_sym == encoding_decl) {
692 /* This looks like an encoding_decl so far. */
693 if (validate_encoding_decl(tree))
694 st = parser_newstobject(tree, PyST_SUITE);
695 else
696 PyNode_Free(tree);
697 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000698 else {
699 /* This is a fragment, at best. */
700 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000701 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000702 }
Guido van Rossum47478871996-08-21 14:32:37 +0000703 }
Guido van Rossum47478871996-08-21 14:32:37 +0000704 /* Make sure we throw an exception on all errors. We should never
705 * get this, but we'd do well to be sure something is done.
706 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000707 if (st == NULL && !PyErr_Occurred())
708 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000709
Fred Drakec2683dd2001-07-17 19:32:05 +0000710 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000711}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000712
713
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000714/* node* build_node_children()
715 *
716 * Iterate across the children of the current non-terminal node and build
717 * their structures. If successful, return the root of this portion of
718 * the tree, otherwise, 0. Any required exception will be specified already,
719 * and no memory will have been deallocated.
720 *
721 */
722static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000723build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000724{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000725 Py_ssize_t len = PyObject_Size(tuple);
726 Py_ssize_t i;
727 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000728
729 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000730 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000731 PyObject* elem = PySequence_GetItem(tuple, i);
732 int ok = elem != NULL;
733 long type = 0;
734 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000735
Fred Drakeff9ea482000-04-19 13:54:15 +0000736 if (ok)
737 ok = PySequence_Check(elem);
738 if (ok) {
739 PyObject *temp = PySequence_GetItem(elem, 0);
740 if (temp == NULL)
741 ok = 0;
742 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000743 ok = PyLong_Check(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000744 if (ok)
Christian Heimes217cfd12007-12-02 14:31:20 +0000745 type = PyLong_AS_LONG(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000746 Py_DECREF(temp);
747 }
748 }
749 if (!ok) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000750 PyObject *err = Py_BuildValue("os", elem,
751 "Illegal node construct.");
752 PyErr_SetObject(parser_error, err);
753 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000754 Py_XDECREF(elem);
755 return (0);
756 }
757 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000758 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000759 PyObject *temp;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000760 const char *temp_str;
Guido van Rossum47478871996-08-21 14:32:37 +0000761
Fred Drake0ac9b072000-09-12 21:58:06 +0000762 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000763 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000764 return 0;
765 }
766 temp = PySequence_GetItem(elem, 1);
767 if (temp == NULL)
768 return 0;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000769 if (!PyUnicode_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000770 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000771 "second item in terminal node must be a string,"
772 " found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000773 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000774 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000775 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000776 return 0;
777 }
778 if (len == 3) {
779 PyObject *o = PySequence_GetItem(elem, 2);
780 if (o != NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000781 if (PyLong_Check(o))
782 *line_num = PyLong_AS_LONG(o);
Fred Drake0ac9b072000-09-12 21:58:06 +0000783 else {
784 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000785 "third item in terminal node must be an"
786 " integer, found %s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 Py_TYPE(temp)->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000788 Py_DECREF(o);
789 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000790 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000791 return 0;
792 }
793 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000794 }
795 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000796 temp_str = _PyUnicode_AsStringAndSize(temp, &len);
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000797 strn = (char *)PyObject_MALLOC(len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000798 if (strn != NULL)
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000799 (void) memcpy(strn, temp_str, len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000800 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000801 }
802 else if (!ISNONTERMINAL(type)) {
803 /*
804 * It has to be one or the other; this is an error.
805 * Throw an exception.
806 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000807 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
808 PyErr_SetObject(parser_error, err);
809 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000810 Py_XDECREF(elem);
811 return (0);
812 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000813 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000814 if (err == E_NOMEM) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000815 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000816 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000817 return (node *) PyErr_NoMemory();
818 }
819 if (err == E_OVERFLOW) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000820 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000821 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000822 PyErr_SetString(PyExc_ValueError,
823 "unsupported number of child nodes");
824 return NULL;
825 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000826
Fred Drakeff9ea482000-04-19 13:54:15 +0000827 if (ISNONTERMINAL(type)) {
828 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000829
Fred Drakeff9ea482000-04-19 13:54:15 +0000830 if (new_child != build_node_children(elem, new_child, line_num)) {
831 Py_XDECREF(elem);
832 return (0);
833 }
834 }
835 else if (type == NEWLINE) { /* It's true: we increment the */
836 ++(*line_num); /* line number *after* the newline! */
837 }
838 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000839 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000840 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000841}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000842
843
844static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000845build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000846{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000847 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000848 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000849 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000850
Guido van Rossum47478871996-08-21 14:32:37 +0000851 if (temp != NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000852 num = PyLong_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000853 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000854 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000855 /*
856 * The tuple is simple, but it doesn't start with a start symbol.
857 * Throw an exception now and be done with it.
858 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000859 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000860 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000861 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000862 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000863 }
864 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000865 /*
866 * Not efficient, but that can be handled later.
867 */
868 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000869 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000870
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000871 if (num == encoding_decl) {
872 encoding = PySequence_GetItem(tuple, 2);
873 /* tuple isn't borrowed anymore here, need to DECREF */
874 tuple = PySequence_GetSlice(tuple, 0, 2);
875 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000876 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000877 if (res != NULL) {
878 if (res != build_node_children(tuple, res, &line_num)) {
879 PyNode_Free(res);
880 res = NULL;
881 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000882 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000883 Py_ssize_t len;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000884 const char *temp;
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000885 temp = _PyUnicode_AsStringAndSize(encoding, &len);
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000886 res->n_str = (char *)PyObject_MALLOC(len + 1);
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000887 if (res->n_str != NULL && temp != NULL)
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000888 (void) memcpy(res->n_str, temp, len + 1);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000889 Py_DECREF(encoding);
890 Py_DECREF(tuple);
891 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000892 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000893 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000894 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000895 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000896 * NONTERMINAL, we can't use it. Not sure the implementation
897 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000898 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000899 PyObject *err = Py_BuildValue("os", tuple,
900 "Illegal component tuple.");
901 PyErr_SetObject(parser_error, err);
902 Py_XDECREF(err);
903 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000904
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000905 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000906}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000907
908
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000909/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000910 * Validation routines used within the validation section:
911 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000912static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000913
Fred Drakeff9ea482000-04-19 13:54:15 +0000914#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
915#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
916#define validate_colon(ch) validate_terminal(ch, COLON, ":")
917#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
918#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
919#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
920#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
921#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
922#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
923#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
924#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
925#define validate_star(ch) validate_terminal(ch, STAR, "*")
926#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
927#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
928#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000929#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000930#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000931
Fred Drake0ac9b072000-09-12 21:58:06 +0000932#define VALIDATER(n) static int validate_##n(node *tree)
933
Fred Drakeff9ea482000-04-19 13:54:15 +0000934VALIDATER(node); VALIDATER(small_stmt);
935VALIDATER(class); VALIDATER(node);
936VALIDATER(parameters); VALIDATER(suite);
937VALIDATER(testlist); VALIDATER(varargslist);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000938VALIDATER(vfpdef);
Fred Drakeff9ea482000-04-19 13:54:15 +0000939VALIDATER(stmt); VALIDATER(simple_stmt);
940VALIDATER(expr_stmt); VALIDATER(power);
Guido van Rossum452bf512007-02-09 05:32:43 +0000941VALIDATER(del_stmt);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000942VALIDATER(return_stmt); VALIDATER(raise_stmt);
943VALIDATER(import_stmt); VALIDATER(import_stmt);
944VALIDATER(import_name); VALIDATER(yield_stmt);
945VALIDATER(global_stmt); VALIDATER(assert_stmt);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000946VALIDATER(compound_stmt); VALIDATER(test_or_star_expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000947VALIDATER(while); VALIDATER(for);
948VALIDATER(try); VALIDATER(except_clause);
949VALIDATER(test); VALIDATER(and_test);
950VALIDATER(not_test); VALIDATER(comparison);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000951VALIDATER(comp_op);
Guido van Rossum0368b722007-05-11 16:50:42 +0000952VALIDATER(star_expr); VALIDATER(expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000953VALIDATER(xor_expr); VALIDATER(and_expr);
954VALIDATER(shift_expr); VALIDATER(arith_expr);
955VALIDATER(term); VALIDATER(factor);
956VALIDATER(atom); VALIDATER(lambdef);
957VALIDATER(trailer); VALIDATER(subscript);
958VALIDATER(subscriptlist); VALIDATER(sliceop);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000959VALIDATER(exprlist); VALIDATER(dictorsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000960VALIDATER(arglist); VALIDATER(argument);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000961VALIDATER(comp_for);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000962VALIDATER(comp_iter); VALIDATER(comp_if);
963VALIDATER(testlist_comp); VALIDATER(yield_expr);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000964VALIDATER(or_test);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000965VALIDATER(test_nocond); VALIDATER(lambdef_nocond);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000966
Fred Drake0ac9b072000-09-12 21:58:06 +0000967#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000968
Fred Drakeff9ea482000-04-19 13:54:15 +0000969#define is_even(n) (((n) & 1) == 0)
970#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000971
972
973static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000974validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000975{
Fred Drake0ac9b072000-09-12 21:58:06 +0000976 if (TYPE(n) != t) {
977 PyErr_Format(parser_error, "Expected node type %d, got %d.",
978 t, TYPE(n));
979 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000980 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000981 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000982}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000983
984
Fred Drakee7ab64e2000-04-25 04:14:46 +0000985/* Verifies that the number of child nodes is exactly 'num', raising
986 * an exception if it isn't. The exception message does not indicate
987 * the exact number of nodes, allowing this to be used to raise the
988 * "right" exception when the wrong number of nodes is present in a
989 * specific variant of a statement's syntax. This is commonly used
990 * in that fashion.
991 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000992static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000993validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000994{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000995 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000996 PyErr_Format(parser_error,
997 "Illegal number of children for %s node.", name);
998 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000999 }
Fred Drake0ac9b072000-09-12 21:58:06 +00001000 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00001001}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001002
1003
1004static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001005validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +00001006{
Guido van Rossum3d602e31996-07-21 02:33:56 +00001007 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +00001008 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001009
1010 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001011 PyErr_Format(parser_error,
1012 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001013 }
1014 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001015}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001016
1017
Guido van Rossum47478871996-08-21 14:32:37 +00001018/* X (',' X) [',']
1019 */
1020static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +00001021validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +00001022 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001023{
1024 int nch = NCH(tree);
1025 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +00001026 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +00001027
1028 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001029 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +00001030 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001031 if (is_even(nch))
1032 res = validate_comma(CHILD(tree, --nch));
1033 if (res && nch > 1) {
1034 int pos = 1;
1035 for ( ; res && pos < nch; pos += 2)
1036 res = (validate_comma(CHILD(tree, pos))
1037 && vfunc(CHILD(tree, pos + 1)));
1038 }
Guido van Rossum47478871996-08-21 14:32:37 +00001039 }
1040 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001041}
Guido van Rossum47478871996-08-21 14:32:37 +00001042
1043
Fred Drakecff283c2000-08-21 22:24:43 +00001044/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001045 *
1046 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001047 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00001048 */
Guido van Rossum47478871996-08-21 14:32:37 +00001049static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001050validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001051{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001052 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +00001053 int res = (validate_ntype(tree, classdef) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001055
Guido van Rossum3d602e31996-07-21 02:33:56 +00001056 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001057 res = (validate_name(CHILD(tree, 0), "class")
1058 && validate_ntype(CHILD(tree, 1), NAME)
1059 && validate_colon(CHILD(tree, nch - 2))
1060 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001061 }
Brett Cannonf4189912005-04-09 02:30:16 +00001062 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001063 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +00001064 }
Guido van Rossumfc158e22007-11-15 19:17:28 +00001065
Brett Cannonf4189912005-04-09 02:30:16 +00001066 if (res) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 if (nch == 7) {
1068 res = ((validate_lparen(CHILD(tree, 2)) &&
1069 validate_arglist(CHILD(tree, 3)) &&
1070 validate_rparen(CHILD(tree, 4))));
1071 }
1072 else if (nch == 6) {
1073 res = (validate_lparen(CHILD(tree,2)) &&
1074 validate_rparen(CHILD(tree,3)));
1075 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001076 }
1077 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001078}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001079
1080
Guido van Rossum3d602e31996-07-21 02:33:56 +00001081/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001082 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001083 */
Guido van Rossum47478871996-08-21 14:32:37 +00001084static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001085validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001086{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001087 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001088 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001089 && (nch >= 4)
1090 && validate_name(CHILD(tree, 0), "if")
1091 && validate_test(CHILD(tree, 1))
1092 && validate_colon(CHILD(tree, 2))
1093 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001094
1095 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001096 /* ... 'else' ':' suite */
1097 res = (validate_name(CHILD(tree, nch - 3), "else")
1098 && validate_colon(CHILD(tree, nch - 2))
1099 && validate_suite(CHILD(tree, nch - 1)));
1100 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001101 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001102 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001103 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001104 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001105 /* Will catch the case for nch < 4 */
1106 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001107 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001108 /* ... ('elif' test ':' suite)+ ... */
1109 int j = 4;
1110 while ((j < nch) && res) {
1111 res = (validate_name(CHILD(tree, j), "elif")
1112 && validate_colon(CHILD(tree, j + 2))
1113 && validate_test(CHILD(tree, j + 1))
1114 && validate_suite(CHILD(tree, j + 3)));
1115 j += 4;
1116 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001117 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001118 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001119}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001120
1121
Guido van Rossum3d602e31996-07-21 02:33:56 +00001122/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001123 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001124 *
1125 */
Guido van Rossum47478871996-08-21 14:32:37 +00001126static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001127validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001128{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001129 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001130 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001131
Guido van Rossum3d602e31996-07-21 02:33:56 +00001132 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001133 res = (validate_lparen(CHILD(tree, 0))
1134 && validate_rparen(CHILD(tree, nch - 1)));
1135 if (res && (nch == 3))
1136 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001137 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001138 else {
1139 (void) validate_numnodes(tree, 2, "parameters");
1140 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001141 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001142}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001143
1144
Fred Drakecff283c2000-08-21 22:24:43 +00001145/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001146 *
1147 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001148 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001149 * | NEWLINE INDENT stmt+ DEDENT
1150 */
Guido van Rossum47478871996-08-21 14:32:37 +00001151static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001152validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001153{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001154 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001155 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001156
Guido van Rossum3d602e31996-07-21 02:33:56 +00001157 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001158 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001159 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001160 /* NEWLINE INDENT stmt+ DEDENT */
1161 res = (validate_newline(CHILD(tree, 0))
1162 && validate_indent(CHILD(tree, 1))
1163 && validate_stmt(CHILD(tree, 2))
1164 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001165
Fred Drakeff9ea482000-04-19 13:54:15 +00001166 if (res && (nch > 4)) {
1167 int i = 3;
1168 --nch; /* forget the DEDENT */
1169 for ( ; res && (i < nch); ++i)
1170 res = validate_stmt(CHILD(tree, i));
1171 }
1172 else if (nch < 4)
1173 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001174 }
1175 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001176}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001177
1178
Guido van Rossum47478871996-08-21 14:32:37 +00001179static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001180validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001181{
Guido van Rossum47478871996-08-21 14:32:37 +00001182 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001183 validate_test, "testlist"));
1184}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001185
Guido van Rossum1c917072001-10-15 15:44:05 +00001186static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00001187validate_testlist_star_expr(node *tl)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001188{
Benjamin Peterson4905e802009-09-27 02:43:28 +00001189 return (validate_repeating_list(tl, testlist_star_expr, validate_test_or_star_expr,
1190 "testlist"));
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001191}
1192
1193
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001194/* validate either vfpdef or tfpdef.
1195 * vfpdef: NAME
1196 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001197 */
1198static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001199validate_vfpdef(node *tree)
Neal Norwitzc1505362006-12-28 06:47:50 +00001200{
1201 int nch = NCH(tree);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001202 if (TYPE(tree) == vfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001203 return nch == 1 && validate_name(CHILD(tree, 0), NULL);
1204 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001205 else if (TYPE(tree) == tfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001206 if (nch == 1) {
1207 return validate_name(CHILD(tree, 0), NULL);
1208 }
1209 else if (nch == 3) {
1210 return validate_name(CHILD(tree, 0), NULL) &&
1211 validate_colon(CHILD(tree, 1)) &&
1212 validate_test(CHILD(tree, 2));
1213 }
1214 }
1215 return 0;
1216}
1217
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001218/* '*' vfpdef (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
1219 * ..or tfpdef in place of vfpdef. vfpdef: NAME; tfpdef: NAME [':' test]
Fred Drakecff283c2000-08-21 22:24:43 +00001220 */
1221static int
1222validate_varargslist_trailer(node *tree, int start)
1223{
1224 int nch = NCH(tree);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001225 int res = 0, i;
Fred Drakecff283c2000-08-21 22:24:43 +00001226 int sym;
1227
1228 if (nch <= start) {
1229 err_string("expected variable argument trailer for varargslist");
1230 return 0;
1231 }
1232 sym = TYPE(CHILD(tree, start));
1233 if (sym == STAR) {
1234 /*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001235 * '*' vfpdef (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
Fred Drakecff283c2000-08-21 22:24:43 +00001236 */
1237 if (nch-start == 2)
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001238 res = validate_vfpdef(CHILD(tree, start+1));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001239 else if (nch-start == 5 && TYPE(CHILD(tree, start+2)) == COMMA)
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001240 res = (validate_vfpdef(CHILD(tree, start+1))
Fred Drakecff283c2000-08-21 22:24:43 +00001241 && validate_comma(CHILD(tree, start+2))
1242 && validate_doublestar(CHILD(tree, start+3))
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001243 && validate_vfpdef(CHILD(tree, start+4)));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001244 else {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001245 /* skip over vfpdef (',' vfpdef ['=' test])* */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001246 i = start + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 if (TYPE(CHILD(tree, i)) == vfpdef ||
1248 TYPE(CHILD(tree, i)) == tfpdef) { /* skip over vfpdef or tfpdef */
1249 i += 1;
1250 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001251 while (res && i+1 < nch) { /* validate (',' vfpdef ['=' test])* */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001252 res = validate_comma(CHILD(tree, i));
Guido van Rossumfc158e22007-11-15 19:17:28 +00001253 if (TYPE(CHILD(tree, i+1)) == DOUBLESTAR)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001254 break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001255 res = res && validate_vfpdef(CHILD(tree, i+1));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001256 if (res && i+2 < nch && TYPE(CHILD(tree, i+2)) == EQUAL) {
Guido van Rossumfc158e22007-11-15 19:17:28 +00001257 res = res && (i+3 < nch)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001258 && validate_test(CHILD(tree, i+3));
1259 i += 4;
1260 }
1261 else {
1262 i += 2;
1263 }
1264 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001265 /* [',' '**' vfpdef] */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001266 if (res && i+1 < nch && TYPE(CHILD(tree, i+1)) == DOUBLESTAR) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001267 res = validate_vfpdef(CHILD(tree, i+2));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001268 }
1269 }
Fred Drakecff283c2000-08-21 22:24:43 +00001270 }
1271 else if (sym == DOUBLESTAR) {
1272 /*
1273 * '**' NAME
1274 */
1275 if (nch-start == 2)
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001276 res = validate_vfpdef(CHILD(tree, start+1));
Fred Drakecff283c2000-08-21 22:24:43 +00001277 }
1278 if (!res)
1279 err_string("illegal variable argument trailer for varargslist");
1280 return res;
1281}
1282
1283
Neal Norwitzc1505362006-12-28 06:47:50 +00001284/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001285 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001286 * Validate typedargslist or varargslist.
1287 *
1288 * typedargslist: ((tfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001289 * ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
1290 * '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001291 * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001292 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001293 * varargslist: ((vfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001294 * ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
1295 * '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001296 * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001297 * vfpdef: NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001298 *
1299 */
Guido van Rossum47478871996-08-21 14:32:37 +00001300static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001301validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001302{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001303 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001304 int res = (TYPE(tree) == varargslist ||
1305 TYPE(tree) == typedargslist) &&
1306 (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001307 int sym;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001308 node *ch;
1309 int i = 0;
Guido van Rossumfc158e22007-11-15 19:17:28 +00001310
Fred Drakeb6429a22000-12-11 22:08:27 +00001311 if (!res)
1312 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001313 if (nch < 1) {
1314 err_string("varargslist missing child nodes");
1315 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001316 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001317 while (i < nch) {
Guido van Rossumfc158e22007-11-15 19:17:28 +00001318 ch = CHILD(tree, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001319 sym = TYPE(ch);
1320 if (sym == vfpdef || sym == tfpdef) {
1321 /* validate (vfpdef ['=' test] ',')+ */
1322 res = validate_vfpdef(ch);
Fred Drakecff283c2000-08-21 22:24:43 +00001323 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001324 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1325 res = (validate_equal(CHILD(tree, i))
1326 && validate_test(CHILD(tree, i+1)));
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001327 if (res)
1328 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001329 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001330 if (res && i < nch) {
1331 res = validate_comma(CHILD(tree, i));
1332 ++i;
Fred Drakecff283c2000-08-21 22:24:43 +00001333 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001334 } else if (sym == DOUBLESTAR || sym == STAR) {
1335 res = validate_varargslist_trailer(tree, i);
1336 break;
1337 } else {
1338 res = 0;
1339 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001340 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001341 }
Fred Drakecff283c2000-08-21 22:24:43 +00001342 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001343}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001344
1345
Nick Coghlan650f0d02007-04-15 12:05:43 +00001346/* comp_iter: comp_for | comp_if
Fred Drakecff283c2000-08-21 22:24:43 +00001347 */
1348static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001349validate_comp_iter(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001350{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001351 int res = (validate_ntype(tree, comp_iter)
1352 && validate_numnodes(tree, 1, "comp_iter"));
1353 if (res && TYPE(CHILD(tree, 0)) == comp_for)
1354 res = validate_comp_for(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001355 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001356 res = validate_comp_if(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001357
1358 return res;
1359}
1360
Nick Coghlan650f0d02007-04-15 12:05:43 +00001361/* comp_for: 'for' exprlist 'in' test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001362 */
1363static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001364validate_comp_for(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001365{
1366 int nch = NCH(tree);
1367 int res;
1368
1369 if (nch == 5)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001370 res = validate_comp_iter(CHILD(tree, 4));
Fred Drakecff283c2000-08-21 22:24:43 +00001371 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001372 res = validate_numnodes(tree, 4, "comp_for");
Raymond Hettinger354433a2004-05-19 08:20:33 +00001373
1374 if (res)
1375 res = (validate_name(CHILD(tree, 0), "for")
1376 && validate_exprlist(CHILD(tree, 1))
1377 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001378 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001379
1380 return res;
1381}
1382
Nick Coghlan650f0d02007-04-15 12:05:43 +00001383/* comp_if: 'if' test_nocond [comp_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001384 */
1385static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001386validate_comp_if(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001387{
1388 int nch = NCH(tree);
1389 int res;
1390
1391 if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001392 res = validate_comp_iter(CHILD(tree, 2));
Fred Drakecff283c2000-08-21 22:24:43 +00001393 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001394 res = validate_numnodes(tree, 2, "comp_if");
Fred Drakecff283c2000-08-21 22:24:43 +00001395
1396 if (res)
1397 res = (validate_name(CHILD(tree, 0), "if")
Nick Coghlan650f0d02007-04-15 12:05:43 +00001398 && validate_test_nocond(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001399
1400 return res;
1401}
1402
1403
Guido van Rossum3d602e31996-07-21 02:33:56 +00001404/* simple_stmt | compound_stmt
1405 *
1406 */
Guido van Rossum47478871996-08-21 14:32:37 +00001407static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001408validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001409{
1410 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001411 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001412
Guido van Rossum3d602e31996-07-21 02:33:56 +00001413 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001414 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001415
Fred Drakeff9ea482000-04-19 13:54:15 +00001416 if (TYPE(tree) == simple_stmt)
1417 res = validate_simple_stmt(tree);
1418 else
1419 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001420 }
1421 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001422}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001423
1424
Guido van Rossum3d602e31996-07-21 02:33:56 +00001425/* small_stmt (';' small_stmt)* [';'] NEWLINE
1426 *
1427 */
Guido van Rossum47478871996-08-21 14:32:37 +00001428static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001429validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001430{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001431 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001432 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001433 && (nch >= 2)
1434 && validate_small_stmt(CHILD(tree, 0))
1435 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001436
Guido van Rossum3d602e31996-07-21 02:33:56 +00001437 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001438 res = validate_numnodes(tree, 2, "simple_stmt");
1439 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001440 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001441 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001442 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001443 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001444
Fred Drakeff9ea482000-04-19 13:54:15 +00001445 for (i = 1; res && (i < nch); i += 2)
1446 res = (validate_semi(CHILD(tree, i))
1447 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001448 }
1449 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001450}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001451
1452
Guido van Rossum47478871996-08-21 14:32:37 +00001453static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001454validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001455{
1456 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001457 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001458
Fred Drake0ac9b072000-09-12 21:58:06 +00001459 if (res) {
1460 int ntype = TYPE(CHILD(tree, 0));
1461
1462 if ( (ntype == expr_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001463 || (ntype == del_stmt)
1464 || (ntype == pass_stmt)
1465 || (ntype == flow_stmt)
1466 || (ntype == import_stmt)
1467 || (ntype == global_stmt)
Georg Brandl7cae87c2006-09-06 06:51:57 +00001468 || (ntype == assert_stmt))
Fred Drake0ac9b072000-09-12 21:58:06 +00001469 res = validate_node(CHILD(tree, 0));
1470 else {
1471 res = 0;
1472 err_string("illegal small_stmt child type");
1473 }
1474 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001475 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001476 res = 0;
1477 PyErr_Format(parser_error,
1478 "Unrecognized child node of small_stmt: %d.",
1479 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001480 }
1481 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001482}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001483
1484
1485/* compound_stmt:
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00001486 * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
Guido van Rossum3d602e31996-07-21 02:33:56 +00001487 */
Guido van Rossum47478871996-08-21 14:32:37 +00001488static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001489validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001490{
1491 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001492 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001493 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001494
1495 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001496 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001497
1498 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001499 ntype = TYPE(tree);
1500 if ( (ntype == if_stmt)
1501 || (ntype == while_stmt)
1502 || (ntype == for_stmt)
1503 || (ntype == try_stmt)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00001504 || (ntype == with_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001505 || (ntype == funcdef)
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001506 || (ntype == classdef)
1507 || (ntype == decorated))
Fred Drakeff9ea482000-04-19 13:54:15 +00001508 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001509 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001510 res = 0;
1511 PyErr_Format(parser_error,
1512 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001513 }
1514 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001515}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001516
Guido van Rossum47478871996-08-21 14:32:37 +00001517static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00001518validate_yield_or_testlist(node *tree, int tse)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001519{
Benjamin Peterson4905e802009-09-27 02:43:28 +00001520 if (TYPE(tree) == yield_expr) {
1521 return validate_yield_expr(tree);
1522 }
1523 else {
1524 if (tse)
1525 return validate_testlist_star_expr(tree);
1526 else
1527 return validate_testlist(tree);
1528 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001529}
1530
1531static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001532validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001533{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001534 int j;
1535 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001536 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001537 && is_odd(nch)
Benjamin Peterson4905e802009-09-27 02:43:28 +00001538 && validate_testlist_star_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001539
Fred Drake28f739a2000-08-25 22:42:40 +00001540 if (res && nch == 3
1541 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001542 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
Benjamin Peterson4905e802009-09-27 02:43:28 +00001543 && validate_yield_or_testlist(CHILD(tree, 2), 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001544
Fred Drake28f739a2000-08-25 22:42:40 +00001545 if (res) {
1546 char *s = STR(CHILD(CHILD(tree, 1), 0));
1547
1548 res = (strcmp(s, "+=") == 0
1549 || strcmp(s, "-=") == 0
1550 || strcmp(s, "*=") == 0
1551 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001552 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001553 || strcmp(s, "%=") == 0
1554 || strcmp(s, "&=") == 0
1555 || strcmp(s, "|=") == 0
1556 || strcmp(s, "^=") == 0
1557 || strcmp(s, "<<=") == 0
1558 || strcmp(s, ">>=") == 0
1559 || strcmp(s, "**=") == 0);
1560 if (!res)
1561 err_string("illegal augmmented assignment operator");
1562 }
1563 }
1564 else {
1565 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001566 res = validate_equal(CHILD(tree, j))
Benjamin Peterson4905e802009-09-27 02:43:28 +00001567 && validate_yield_or_testlist(CHILD(tree, j + 1), 1);
Fred Drake28f739a2000-08-25 22:42:40 +00001568 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001569 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001570}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001571
1572
Guido van Rossum47478871996-08-21 14:32:37 +00001573static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001574validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001575{
1576 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001577 && validate_name(CHILD(tree, 0), "del")
1578 && validate_exprlist(CHILD(tree, 1)));
1579}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001580
1581
Guido van Rossum47478871996-08-21 14:32:37 +00001582static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001583validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001584{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001585 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001586 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001587 && ((nch == 1) || (nch == 2))
1588 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001589
Guido van Rossum3d602e31996-07-21 02:33:56 +00001590 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001591 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001592
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001593 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001594}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001595
1596
Guido van Rossum47478871996-08-21 14:32:37 +00001597static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001598validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001599{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001600 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001601 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001602 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001603
Guido van Rossum3d602e31996-07-21 02:33:56 +00001604 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001605 res = validate_name(CHILD(tree, 0), "raise");
1606 if (res && (nch >= 2))
1607 res = validate_test(CHILD(tree, 1));
1608 if (res && nch > 2) {
1609 res = (validate_comma(CHILD(tree, 2))
1610 && validate_test(CHILD(tree, 3)));
1611 if (res && (nch > 4))
1612 res = (validate_comma(CHILD(tree, 4))
1613 && validate_test(CHILD(tree, 5)));
1614 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001615 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001616 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001617 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001618 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001619 res = (validate_comma(CHILD(tree, 2))
1620 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001621
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001622 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001623}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001624
1625
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001626/* yield_expr: 'yield' [testlist]
1627 */
1628static int
1629validate_yield_expr(node *tree)
1630{
1631 int nch = NCH(tree);
1632 int res = (validate_ntype(tree, yield_expr)
1633 && ((nch == 1) || (nch == 2))
1634 && validate_name(CHILD(tree, 0), "yield"));
1635
1636 if (res && (nch == 2))
1637 res = validate_testlist(CHILD(tree, 1));
1638
1639 return (res);
1640}
1641
1642
1643/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001644 */
1645static int
1646validate_yield_stmt(node *tree)
1647{
1648 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001649 && validate_numnodes(tree, 1, "yield_stmt")
1650 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001651}
1652
1653
Fred Drakecff283c2000-08-21 22:24:43 +00001654static int
1655validate_import_as_name(node *tree)
1656{
1657 int nch = NCH(tree);
1658 int ok = validate_ntype(tree, import_as_name);
1659
1660 if (ok) {
1661 if (nch == 1)
1662 ok = validate_name(CHILD(tree, 0), NULL);
1663 else if (nch == 3)
1664 ok = (validate_name(CHILD(tree, 0), NULL)
1665 && validate_name(CHILD(tree, 1), "as")
1666 && validate_name(CHILD(tree, 2), NULL));
1667 else
1668 ok = validate_numnodes(tree, 3, "import_as_name");
1669 }
1670 return ok;
1671}
1672
1673
Fred Drake71137082001-01-07 05:59:59 +00001674/* dotted_name: NAME ("." NAME)*
1675 */
1676static int
1677validate_dotted_name(node *tree)
1678{
1679 int nch = NCH(tree);
1680 int res = (validate_ntype(tree, dotted_name)
1681 && is_odd(nch)
1682 && validate_name(CHILD(tree, 0), NULL));
1683 int i;
1684
1685 for (i = 1; res && (i < nch); i += 2) {
1686 res = (validate_dot(CHILD(tree, i))
1687 && validate_name(CHILD(tree, i+1), NULL));
1688 }
1689 return res;
1690}
1691
1692
Fred Drakecff283c2000-08-21 22:24:43 +00001693/* dotted_as_name: dotted_name [NAME NAME]
1694 */
1695static int
1696validate_dotted_as_name(node *tree)
1697{
1698 int nch = NCH(tree);
1699 int res = validate_ntype(tree, dotted_as_name);
1700
1701 if (res) {
1702 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001703 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001704 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001705 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001706 && validate_name(CHILD(tree, 1), "as")
1707 && validate_name(CHILD(tree, 2), NULL));
1708 else {
1709 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001710 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001711 }
1712 }
1713 return res;
1714}
1715
1716
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001717/* dotted_as_name (',' dotted_as_name)* */
1718static int
1719validate_dotted_as_names(node *tree)
1720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 int nch = NCH(tree);
1722 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1723 int i;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 for (i = 1; res && (i < nch); i += 2)
1726 res = (validate_comma(CHILD(tree, i))
1727 && validate_dotted_as_name(CHILD(tree, i + 1)));
1728 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001729}
1730
1731
1732/* import_as_name (',' import_as_name)* [','] */
1733static int
1734validate_import_as_names(node *tree)
1735{
1736 int nch = NCH(tree);
1737 int res = validate_import_as_name(CHILD(tree, 0));
1738 int i;
1739
1740 for (i = 1; res && (i + 1 < nch); i += 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 res = (validate_comma(CHILD(tree, i))
1742 && validate_import_as_name(CHILD(tree, i + 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001743 return (res);
1744}
1745
1746
1747/* 'import' dotted_as_names */
1748static int
1749validate_import_name(node *tree)
1750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 return (validate_ntype(tree, import_name)
1752 && validate_numnodes(tree, 2, "import_name")
1753 && validate_name(CHILD(tree, 0), "import")
1754 && validate_dotted_as_names(CHILD(tree, 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001755}
1756
Mark Dickinsonfeb3b752010-07-04 18:38:57 +00001757/* Helper function to count the number of leading dots (or ellipsis tokens) in
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001758 * 'from ...module import name'
1759 */
1760static int
1761count_from_dots(node *tree)
1762{
Mark Dickinsonfeb3b752010-07-04 18:38:57 +00001763 int i;
1764 for (i = 1; i < NCH(tree); i++)
1765 if (TYPE(CHILD(tree, i)) != DOT && TYPE(CHILD(tree, i)) != ELLIPSIS)
1766 break;
1767 return i - 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001768}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001769
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +00001770/* import_from: ('from' ('.'* dotted_name | '.'+)
1771 * 'import' ('*' | '(' import_as_names ')' | import_as_names))
Guido van Rossum3d602e31996-07-21 02:33:56 +00001772 */
Guido van Rossum47478871996-08-21 14:32:37 +00001773static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001774validate_import_from(node *tree)
1775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 int nch = NCH(tree);
1777 int ndots = count_from_dots(tree);
1778 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1779 int offset = ndots + havename;
1780 int res = validate_ntype(tree, import_from)
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +00001781 && (offset >= 1)
1782 && (nch >= 3 + offset)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 && validate_name(CHILD(tree, 0), "from")
1784 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1785 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1788 res = ((nch == offset + 5)
1789 && validate_lparen(CHILD(tree, offset + 2))
1790 && validate_import_as_names(CHILD(tree, offset + 3))
1791 && validate_rparen(CHILD(tree, offset + 4)));
1792 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1793 res = validate_import_as_names(CHILD(tree, offset + 2));
1794 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001795}
1796
1797
1798/* import_stmt: import_name | import_from */
1799static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001800validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001801{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001802 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001803 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001804
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001805 if (res) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 if (ntype == import_name || ntype == import_from)
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001809 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001810 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001811 res = 0;
1812 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001813 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001814 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001815 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001816 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001817 PyErr_Format(parser_error,
1818 "Unrecognized child node of import_stmt: %d.",
1819 TYPE(CHILD(tree, 0)));
1820 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001821 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001822}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001823
1824
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001825
1826
Guido van Rossum47478871996-08-21 14:32:37 +00001827static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001828validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001829{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001830 int j;
1831 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001832 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001833 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001834
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001835 if (!res && !PyErr_Occurred())
1836 err_string("illegal global statement");
1837
Guido van Rossum3d602e31996-07-21 02:33:56 +00001838 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001839 res = (validate_name(CHILD(tree, 0), "global")
1840 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001841 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001842 res = (validate_comma(CHILD(tree, j))
1843 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001844
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001845 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001846}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001847
1848
Guido van Rossum925e5471997-04-02 05:32:13 +00001849/* assert_stmt:
1850 *
1851 * 'assert' test [',' test]
1852 */
1853static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001854validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001855{
1856 int nch = NCH(tree);
1857 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001858 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001859 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001860 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001861
1862 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001863 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001864 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001865 res = (validate_comma(CHILD(tree, 2))
1866 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001867
1868 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001869}
Guido van Rossum925e5471997-04-02 05:32:13 +00001870
1871
Guido van Rossum47478871996-08-21 14:32:37 +00001872static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001873validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001874{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001875 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001876 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001877 && ((nch == 4) || (nch == 7))
1878 && validate_name(CHILD(tree, 0), "while")
1879 && validate_test(CHILD(tree, 1))
1880 && validate_colon(CHILD(tree, 2))
1881 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001882
Guido van Rossum3d602e31996-07-21 02:33:56 +00001883 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001884 res = (validate_name(CHILD(tree, 4), "else")
1885 && validate_colon(CHILD(tree, 5))
1886 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001887
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001888 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001889}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001890
1891
Guido van Rossum47478871996-08-21 14:32:37 +00001892static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001893validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001894{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001895 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001896 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001897 && ((nch == 6) || (nch == 9))
1898 && validate_name(CHILD(tree, 0), "for")
1899 && validate_exprlist(CHILD(tree, 1))
1900 && validate_name(CHILD(tree, 2), "in")
1901 && validate_testlist(CHILD(tree, 3))
1902 && validate_colon(CHILD(tree, 4))
1903 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001904
Guido van Rossum3d602e31996-07-21 02:33:56 +00001905 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001906 res = (validate_name(CHILD(tree, 6), "else")
1907 && validate_colon(CHILD(tree, 7))
1908 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001909
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001910 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001911}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001912
1913
Guido van Rossum3d602e31996-07-21 02:33:56 +00001914/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001915 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Georg Brandleee31162008-12-07 15:15:22 +00001916 ['finally' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001917 * | 'try' ':' suite 'finally' ':' suite
1918 *
1919 */
Guido van Rossum47478871996-08-21 14:32:37 +00001920static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001921validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001922{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001923 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001924 int pos = 3;
1925 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001926 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001927
1928 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001929 res = (validate_name(CHILD(tree, 0), "try")
1930 && validate_colon(CHILD(tree, 1))
1931 && validate_suite(CHILD(tree, 2))
1932 && validate_colon(CHILD(tree, nch - 2))
1933 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001934 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001935 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001936 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1937 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001938
1939 PyErr_Format(parser_error,
1940 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001941 }
Georg Brandleee31162008-12-07 15:15:22 +00001942 /* Handle try/finally statement */
1943 if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
1944 (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
1945 res = (validate_numnodes(tree, 6, "try/finally")
1946 && validate_colon(CHILD(tree, 4))
1947 && validate_suite(CHILD(tree, 5)));
1948 return (res);
1949 }
1950 /* try/except statement: skip past except_clause sections */
Antoine Pitrou37d1c182009-05-14 21:54:00 +00001951 while (res && pos < nch && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001952 res = (validate_except_clause(CHILD(tree, pos))
1953 && validate_colon(CHILD(tree, pos + 1))
1954 && validate_suite(CHILD(tree, pos + 2)));
1955 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001956 }
Georg Brandleee31162008-12-07 15:15:22 +00001957 /* skip else clause */
Antoine Pitrou37d1c182009-05-14 21:54:00 +00001958 if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) &&
Georg Brandleee31162008-12-07 15:15:22 +00001959 (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
1960 res = (validate_colon(CHILD(tree, pos + 1))
1961 && validate_suite(CHILD(tree, pos + 2)));
1962 pos += 3;
1963 }
1964 if (res && pos < nch) {
1965 /* last clause must be a finally */
1966 res = (validate_name(CHILD(tree, pos), "finally")
1967 && validate_numnodes(tree, pos + 3, "try/except/finally")
1968 && validate_colon(CHILD(tree, pos + 1))
1969 && validate_suite(CHILD(tree, pos + 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001970 }
1971 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001972}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001973
1974
Guido van Rossum47478871996-08-21 14:32:37 +00001975static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001976validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001977{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001978 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001979 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001980 && ((nch == 1) || (nch == 2) || (nch == 4))
1981 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001982
Guido van Rossum3d602e31996-07-21 02:33:56 +00001983 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001984 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001985 if (res && (nch == 4))
Guido van Rossumb940e112007-01-10 16:19:56 +00001986 res = (validate_name(CHILD(tree, 2), "as")
1987 && validate_ntype(CHILD(tree, 3), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001988
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001989 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001990}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001991
1992
Guido van Rossum47478871996-08-21 14:32:37 +00001993static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001994validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001995{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001996 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001997 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001998
Guido van Rossum3d602e31996-07-21 02:33:56 +00001999 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002000 res = ((nch == 1)
2001 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002002 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002003 res = validate_or_test(CHILD(tree, 0));
2004 res = (res && (nch == 1 || (nch == 5 &&
2005 validate_name(CHILD(tree, 1), "if") &&
2006 validate_or_test(CHILD(tree, 2)) &&
2007 validate_name(CHILD(tree, 3), "else") &&
2008 validate_test(CHILD(tree, 4)))));
2009 }
2010 return (res);
2011}
2012
2013static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002014validate_test_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002015{
2016 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002017 int res = validate_ntype(tree, test_nocond) && (nch == 1);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002018
Nick Coghlan650f0d02007-04-15 12:05:43 +00002019 if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
2020 res = (validate_lambdef_nocond(CHILD(tree, 0)));
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002021 else if (res) {
2022 res = (validate_or_test(CHILD(tree, 0)));
2023 }
2024 return (res);
2025}
2026
2027static int
2028validate_or_test(node *tree)
2029{
2030 int nch = NCH(tree);
2031 int res = validate_ntype(tree, or_test) && is_odd(nch);
2032
2033 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002034 int pos;
2035 res = validate_and_test(CHILD(tree, 0));
2036 for (pos = 1; res && (pos < nch); pos += 2)
2037 res = (validate_name(CHILD(tree, pos), "or")
2038 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002039 }
2040 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002041}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002042
2043
Guido van Rossum47478871996-08-21 14:32:37 +00002044static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002045validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002046{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002047 int pos;
2048 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002049 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002050 && is_odd(nch)
2051 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002052
Guido van Rossum3d602e31996-07-21 02:33:56 +00002053 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002054 res = (validate_name(CHILD(tree, pos), "and")
2055 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002056
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002057 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002058}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002059
2060
Guido van Rossum47478871996-08-21 14:32:37 +00002061static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002062validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002063{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002064 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002065 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002066
Guido van Rossum3d602e31996-07-21 02:33:56 +00002067 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002068 if (nch == 2)
2069 res = (validate_name(CHILD(tree, 0), "not")
2070 && validate_not_test(CHILD(tree, 1)));
2071 else if (nch == 1)
2072 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002073 }
2074 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002075}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002076
2077
Guido van Rossum47478871996-08-21 14:32:37 +00002078static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002079validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002080{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002081 int pos;
2082 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002083 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002084 && is_odd(nch)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002085 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002086
Guido van Rossum3d602e31996-07-21 02:33:56 +00002087 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002088 res = (validate_comp_op(CHILD(tree, pos))
Benjamin Peterson4905e802009-09-27 02:43:28 +00002089 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002090
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002091 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002092}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002093
2094
Guido van Rossum47478871996-08-21 14:32:37 +00002095static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002096validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002097{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002098 int res = 0;
2099 int nch = NCH(tree);
2100
Guido van Rossum3d602e31996-07-21 02:33:56 +00002101 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002102 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002103 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002104 /*
2105 * Only child will be a terminal with a well-defined symbolic name
2106 * or a NAME with a string of either 'is' or 'in'
2107 */
2108 tree = CHILD(tree, 0);
2109 switch (TYPE(tree)) {
2110 case LESS:
2111 case GREATER:
2112 case EQEQUAL:
2113 case EQUAL:
2114 case LESSEQUAL:
2115 case GREATEREQUAL:
2116 case NOTEQUAL:
2117 res = 1;
2118 break;
2119 case NAME:
2120 res = ((strcmp(STR(tree), "in") == 0)
2121 || (strcmp(STR(tree), "is") == 0));
2122 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002123 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002124 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002125 }
2126 break;
2127 default:
Fred Drake661ea262000-10-24 19:57:45 +00002128 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002129 break;
2130 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002131 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002132 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002133 res = (validate_ntype(CHILD(tree, 0), NAME)
2134 && validate_ntype(CHILD(tree, 1), NAME)
2135 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2136 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2137 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2138 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2139 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002140 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002141 }
2142 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002143}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002144
2145
Guido van Rossum47478871996-08-21 14:32:37 +00002146static int
Guido van Rossum0368b722007-05-11 16:50:42 +00002147validate_star_expr(node *tree)
2148{
2149 int res = validate_ntype(tree, star_expr);
2150 if (!res) return res;
Benjamin Peterson4905e802009-09-27 02:43:28 +00002151 if (!validate_numnodes(tree, 2, "star_expr"))
2152 return 0;
2153 return validate_ntype(CHILD(tree, 0), STAR) && \
2154 validate_expr(CHILD(tree, 1));
Guido van Rossum0368b722007-05-11 16:50:42 +00002155}
2156
2157
2158static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002159validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002160{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002161 int j;
2162 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002163 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002164 && is_odd(nch)
2165 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002166
Guido van Rossum3d602e31996-07-21 02:33:56 +00002167 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002168 res = (validate_xor_expr(CHILD(tree, j))
2169 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002170
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002171 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002172}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002173
2174
Guido van Rossum47478871996-08-21 14:32:37 +00002175static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002176validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002177{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002178 int j;
2179 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002180 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002181 && is_odd(nch)
2182 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002183
Guido van Rossum3d602e31996-07-21 02:33:56 +00002184 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002185 res = (validate_circumflex(CHILD(tree, j - 1))
2186 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002187
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002188 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002189}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002190
2191
Guido van Rossum47478871996-08-21 14:32:37 +00002192static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002193validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002194{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002195 int pos;
2196 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002197 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002198 && is_odd(nch)
2199 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002200
Guido van Rossum3d602e31996-07-21 02:33:56 +00002201 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002202 res = (validate_ampersand(CHILD(tree, pos))
2203 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002204
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002205 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002206}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002207
2208
2209static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002210validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002211 {
2212 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002213 int nch = NCH(tree);
2214 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002215 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002216
Guido van Rossum3d602e31996-07-21 02:33:56 +00002217 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002218 if (TYPE(CHILD(tree, pos)) != op1)
2219 res = validate_ntype(CHILD(tree, pos), op2);
2220 if (res)
2221 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002222 }
2223 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002224}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002225
2226
Guido van Rossum47478871996-08-21 14:32:37 +00002227static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002228validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002229{
2230 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002231 && validate_chain_two_ops(tree, validate_arith_expr,
2232 LEFTSHIFT, RIGHTSHIFT));
2233}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002234
2235
Guido van Rossum47478871996-08-21 14:32:37 +00002236static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002237validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002238{
2239 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002240 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2241}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002242
2243
Guido van Rossum47478871996-08-21 14:32:37 +00002244static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002245validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002246{
2247 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002248 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002249 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002250 && is_odd(nch)
2251 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002252
Guido van Rossum3d602e31996-07-21 02:33:56 +00002253 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002254 res = (((TYPE(CHILD(tree, pos)) == STAR)
2255 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002256 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002257 || (TYPE(CHILD(tree, pos)) == PERCENT))
2258 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002259
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002260 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002261}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002262
2263
Guido van Rossum3d602e31996-07-21 02:33:56 +00002264/* factor:
2265 *
2266 * factor: ('+'|'-'|'~') factor | power
2267 */
Guido van Rossum47478871996-08-21 14:32:37 +00002268static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002269validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002270{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002271 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002272 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002273 && (((nch == 2)
2274 && ((TYPE(CHILD(tree, 0)) == PLUS)
2275 || (TYPE(CHILD(tree, 0)) == MINUS)
2276 || (TYPE(CHILD(tree, 0)) == TILDE))
2277 && validate_factor(CHILD(tree, 1)))
2278 || ((nch == 1)
2279 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002280 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002281}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002282
2283
Guido van Rossum3d602e31996-07-21 02:33:56 +00002284/* power:
2285 *
2286 * power: atom trailer* ('**' factor)*
2287 */
Guido van Rossum47478871996-08-21 14:32:37 +00002288static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002289validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002290{
2291 int pos = 1;
2292 int nch = NCH(tree);
2293 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002294 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002295
2296 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002297 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002298 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002299 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002300 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002301 return (0);
2302 }
2303 for ( ; res && (pos < (nch - 1)); pos += 2)
2304 res = (validate_doublestar(CHILD(tree, pos))
2305 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002306 }
2307 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002308}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002309
2310
Guido van Rossum47478871996-08-21 14:32:37 +00002311static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002312validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002313{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002314 int pos;
2315 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002316 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002317
Fred Drakecff283c2000-08-21 22:24:43 +00002318 if (res && nch < 1)
2319 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002320 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002321 switch (TYPE(CHILD(tree, 0))) {
2322 case LPAR:
2323 res = ((nch <= 3)
2324 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002325
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002326 if (res && (nch == 3)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 if (TYPE(CHILD(tree, 1))==yield_expr)
2328 res = validate_yield_expr(CHILD(tree, 1));
2329 else
2330 res = validate_testlist_comp(CHILD(tree, 1));
2331 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002332 break;
2333 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002334 if (nch == 2)
2335 res = validate_ntype(CHILD(tree, 1), RSQB);
2336 else if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002337 res = (validate_testlist_comp(CHILD(tree, 1))
Fred Drakecff283c2000-08-21 22:24:43 +00002338 && validate_ntype(CHILD(tree, 2), RSQB));
2339 else {
2340 res = 0;
2341 err_string("illegal list display atom");
2342 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002343 break;
2344 case LBRACE:
2345 res = ((nch <= 3)
2346 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002347
Fred Drakeff9ea482000-04-19 13:54:15 +00002348 if (res && (nch == 3))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002349 res = validate_dictorsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002350 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002351 case NAME:
2352 case NUMBER:
2353 res = (nch == 1);
2354 break;
2355 case STRING:
2356 for (pos = 1; res && (pos < nch); ++pos)
2357 res = validate_ntype(CHILD(tree, pos), STRING);
2358 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 case DOT:
2360 res = (nch == 3 &&
2361 validate_ntype(CHILD(tree, 1), DOT) &&
2362 validate_ntype(CHILD(tree, 2), DOT));
2363 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002364 default:
2365 res = 0;
2366 break;
2367 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002368 }
2369 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002370}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002371
2372
Nick Coghlan650f0d02007-04-15 12:05:43 +00002373/* testlist_comp:
2374 * test ( comp_for | (',' test)* [','] )
Fred Drake85bf3bb2000-08-23 15:35:26 +00002375 */
Fred Drakecff283c2000-08-21 22:24:43 +00002376static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002377validate_testlist_comp(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00002378{
2379 int nch = NCH(tree);
2380 int ok = nch;
2381
2382 if (nch == 0)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002383 err_string("missing child nodes of testlist_comp");
2384 else {
Benjamin Peterson4905e802009-09-27 02:43:28 +00002385 ok = validate_test_or_star_expr(CHILD(tree, 0));
Nick Coghlan650f0d02007-04-15 12:05:43 +00002386 }
Fred Drakecff283c2000-08-21 22:24:43 +00002387
2388 /*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002389 * comp_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002390 */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002391 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2392 ok = validate_comp_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002393 else {
2394 /* (',' test)* [','] */
2395 int i = 1;
2396 while (ok && nch - i >= 2) {
2397 ok = (validate_comma(CHILD(tree, i))
Benjamin Peterson4905e802009-09-27 02:43:28 +00002398 && validate_test_or_star_expr(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002399 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002400 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002401 if (ok && i == nch-1)
2402 ok = validate_comma(CHILD(tree, i));
2403 else if (i != nch) {
2404 ok = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002405 err_string("illegal trailing nodes for testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002406 }
2407 }
2408 return ok;
2409}
Fred Drakecff283c2000-08-21 22:24:43 +00002410
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002411/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002412 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002413 */
2414static int
2415validate_decorator(node *tree)
2416{
2417 int ok;
2418 int nch = NCH(tree);
2419 ok = (validate_ntype(tree, decorator) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 (nch == 3 || nch == 5 || nch == 6) &&
2421 validate_at(CHILD(tree, 0)) &&
2422 validate_dotted_name(CHILD(tree, 1)) &&
2423 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002424
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002425 if (ok && nch != 3) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 ok = (validate_lparen(CHILD(tree, 2)) &&
2427 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 if (ok && nch == 6)
2430 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002431 }
2432
2433 return ok;
2434}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002435
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002436/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002437 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002438 */
2439static int
2440validate_decorators(node *tree)
2441{
Guido van Rossumfc158e22007-11-15 19:17:28 +00002442 int i, nch, ok;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002443 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002444 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002445
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002446 for (i = 0; ok && i < nch; ++i)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002448
2449 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002450}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002451
Georg Brandl0c315622009-05-25 21:10:36 +00002452/* with_item:
2453 * test ['as' expr]
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002454 */
2455static int
Georg Brandl0c315622009-05-25 21:10:36 +00002456validate_with_item(node *tree)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002457{
2458 int nch = NCH(tree);
Georg Brandl0c315622009-05-25 21:10:36 +00002459 int ok = (validate_ntype(tree, with_item)
2460 && (nch == 1 || nch == 3)
2461 && validate_test(CHILD(tree, 0)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 if (ok && nch == 3)
Georg Brandl0c315622009-05-25 21:10:36 +00002463 ok = (validate_name(CHILD(tree, 1), "as")
2464 && validate_expr(CHILD(tree, 2)));
2465 return ok;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002466}
2467
Georg Brandl0c315622009-05-25 21:10:36 +00002468/* with_stmt:
2469 * 0 1 ... -2 -1
2470 * 'with' with_item (',' with_item)* ':' suite
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002471 */
2472static int
2473validate_with_stmt(node *tree)
2474{
Georg Brandl0c315622009-05-25 21:10:36 +00002475 int i;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002476 int nch = NCH(tree);
2477 int ok = (validate_ntype(tree, with_stmt)
Georg Brandl0c315622009-05-25 21:10:36 +00002478 && (nch % 2 == 0)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002479 && validate_name(CHILD(tree, 0), "with")
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002480 && validate_colon(RCHILD(tree, -2))
2481 && validate_suite(RCHILD(tree, -1)));
Georg Brandl0c315622009-05-25 21:10:36 +00002482 for (i = 1; ok && i < nch - 2; i += 2)
2483 ok = validate_with_item(CHILD(tree, i));
2484 return ok;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002485}
2486
Guido van Rossum3d602e31996-07-21 02:33:56 +00002487/* funcdef:
Guido van Rossumfc158e22007-11-15 19:17:28 +00002488 *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002489 * -5 -4 -3 -2 -1
2490 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002491 */
Guido van Rossum47478871996-08-21 14:32:37 +00002492static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002493validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002494{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002495 int nch = NCH(tree);
2496 int ok = (validate_ntype(tree, funcdef)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 && (nch == 5)
2498 && validate_name(RCHILD(tree, -5), "def")
2499 && validate_ntype(RCHILD(tree, -4), NAME)
2500 && validate_colon(RCHILD(tree, -2))
2501 && validate_parameters(RCHILD(tree, -3))
2502 && validate_suite(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002503 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002504}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002505
2506
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002507/* decorated
2508 * decorators (classdef | funcdef)
2509 */
2510static int
2511validate_decorated(node *tree)
2512{
Mark Dickinson2bd61a92010-07-04 16:37:31 +00002513 int nch = NCH(tree);
2514 int ok = (validate_ntype(tree, decorated)
2515 && (nch == 2)
2516 && validate_decorators(RCHILD(tree, -2)));
2517 if (TYPE(RCHILD(tree, -1)) == funcdef)
2518 ok = ok && validate_funcdef(RCHILD(tree, -1));
2519 else
2520 ok = ok && validate_class(RCHILD(tree, -1));
2521 return ok;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002522}
2523
Guido van Rossum47478871996-08-21 14:32:37 +00002524static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002525validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002526{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002527 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002528 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002529 && ((nch == 3) || (nch == 4))
2530 && validate_name(CHILD(tree, 0), "lambda")
2531 && validate_colon(CHILD(tree, nch - 2))
2532 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002533
Guido van Rossum3d602e31996-07-21 02:33:56 +00002534 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002535 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002536 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002537 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002538
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002539 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002540}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002541
2542
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002543static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002544validate_lambdef_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002545{
2546 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002547 int res = (validate_ntype(tree, lambdef_nocond)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002548 && ((nch == 3) || (nch == 4))
2549 && validate_name(CHILD(tree, 0), "lambda")
2550 && validate_colon(CHILD(tree, nch - 2))
2551 && validate_test(CHILD(tree, nch - 1)));
2552
2553 if (res && (nch == 4))
2554 res = validate_varargslist(CHILD(tree, 1));
2555 else if (!res && !PyErr_Occurred())
Nick Coghlan650f0d02007-04-15 12:05:43 +00002556 (void) validate_numnodes(tree, 3, "lambdef_nocond");
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002557
2558 return (res);
2559}
2560
2561
Guido van Rossum3d602e31996-07-21 02:33:56 +00002562/* arglist:
2563 *
Fred Drakecff283c2000-08-21 22:24:43 +00002564 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002565 */
Guido van Rossum47478871996-08-21 14:32:37 +00002566static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002567validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002568{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002569 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002570 int i = 0;
2571 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002572
2573 if (nch <= 0)
2574 /* raise the right error from having an invalid number of children */
2575 return validate_numnodes(tree, nch + 1, "arglist");
2576
Raymond Hettinger354433a2004-05-19 08:20:33 +00002577 if (nch > 1) {
2578 for (i=0; i<nch; i++) {
2579 if (TYPE(CHILD(tree, i)) == argument) {
2580 node *ch = CHILD(tree, i);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002581 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
Raymond Hettinger354433a2004-05-19 08:20:33 +00002582 err_string("need '(', ')' for generator expression");
2583 return 0;
2584 }
2585 }
2586 }
2587 }
2588
Fred Drakecff283c2000-08-21 22:24:43 +00002589 while (ok && nch-i >= 2) {
2590 /* skip leading (argument ',') */
2591 ok = (validate_argument(CHILD(tree, i))
2592 && validate_comma(CHILD(tree, i+1)));
2593 if (ok)
2594 i += 2;
2595 else
2596 PyErr_Clear();
2597 }
2598 ok = 1;
2599 if (nch-i > 0) {
2600 /*
2601 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002602 */
Fred Drakecff283c2000-08-21 22:24:43 +00002603 int sym = TYPE(CHILD(tree, i));
2604
2605 if (sym == argument) {
2606 ok = validate_argument(CHILD(tree, i));
2607 if (ok && i+1 != nch) {
2608 err_string("illegal arglist specification"
2609 " (extra stuff on end)");
2610 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002611 }
Fred Drakecff283c2000-08-21 22:24:43 +00002612 }
2613 else if (sym == STAR) {
2614 ok = validate_star(CHILD(tree, i));
2615 if (ok && (nch-i == 2))
2616 ok = validate_test(CHILD(tree, i+1));
2617 else if (ok && (nch-i == 5))
2618 ok = (validate_test(CHILD(tree, i+1))
2619 && validate_comma(CHILD(tree, i+2))
2620 && validate_doublestar(CHILD(tree, i+3))
2621 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002622 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002623 err_string("illegal use of '*' in arglist");
2624 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002625 }
2626 }
Fred Drakecff283c2000-08-21 22:24:43 +00002627 else if (sym == DOUBLESTAR) {
2628 if (nch-i == 2)
2629 ok = (validate_doublestar(CHILD(tree, i))
2630 && validate_test(CHILD(tree, i+1)));
2631 else {
2632 err_string("illegal use of '**' in arglist");
2633 ok = 0;
2634 }
2635 }
2636 else {
2637 err_string("illegal arglist specification");
2638 ok = 0;
2639 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002640 }
2641 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002642}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002643
2644
2645
2646/* argument:
2647 *
Nick Coghlan650f0d02007-04-15 12:05:43 +00002648 * [test '='] test [comp_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002649 */
Guido van Rossum47478871996-08-21 14:32:37 +00002650static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002651validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002652{
2653 int nch = NCH(tree);
2654 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002655 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002656 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002657
Raymond Hettinger354433a2004-05-19 08:20:33 +00002658 if (res && (nch == 2))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002659 res = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002660 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002661 res = (validate_equal(CHILD(tree, 1))
2662 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002663
2664 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002665}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002666
2667
2668
2669/* trailer:
2670 *
Guido van Rossum47478871996-08-21 14:32:37 +00002671 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002672 */
Guido van Rossum47478871996-08-21 14:32:37 +00002673static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002674validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002675{
2676 int nch = NCH(tree);
2677 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002678
2679 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002680 switch (TYPE(CHILD(tree, 0))) {
2681 case LPAR:
2682 res = validate_rparen(CHILD(tree, nch - 1));
2683 if (res && (nch == 3))
2684 res = validate_arglist(CHILD(tree, 1));
2685 break;
2686 case LSQB:
2687 res = (validate_numnodes(tree, 3, "trailer")
2688 && validate_subscriptlist(CHILD(tree, 1))
2689 && validate_ntype(CHILD(tree, 2), RSQB));
2690 break;
2691 case DOT:
2692 res = (validate_numnodes(tree, 2, "trailer")
2693 && validate_ntype(CHILD(tree, 1), NAME));
2694 break;
2695 default:
2696 res = 0;
2697 break;
2698 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002699 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002700 else {
2701 (void) validate_numnodes(tree, 2, "trailer");
2702 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002703 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002704}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002705
2706
Guido van Rossum47478871996-08-21 14:32:37 +00002707/* subscriptlist:
2708 *
2709 * subscript (',' subscript)* [',']
2710 */
2711static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002712validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002713{
2714 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002715 validate_subscript, "subscriptlist"));
2716}
Guido van Rossum47478871996-08-21 14:32:37 +00002717
2718
Guido van Rossum3d602e31996-07-21 02:33:56 +00002719/* subscript:
2720 *
Guido van Rossum47478871996-08-21 14:32:37 +00002721 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002722 */
Guido van Rossum47478871996-08-21 14:32:37 +00002723static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002724validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002725{
Guido van Rossum47478871996-08-21 14:32:37 +00002726 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002727 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002728 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002729
Guido van Rossum47478871996-08-21 14:32:37 +00002730 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002731 if (!PyErr_Occurred())
2732 err_string("invalid number of arguments for subscript node");
2733 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002734 }
Guido van Rossum47478871996-08-21 14:32:37 +00002735 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002736 /* take care of ('.' '.' '.') possibility */
2737 return (validate_numnodes(tree, 3, "subscript")
2738 && validate_dot(CHILD(tree, 0))
2739 && validate_dot(CHILD(tree, 1))
2740 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002741 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002742 if (TYPE(CHILD(tree, 0)) == test)
2743 res = validate_test(CHILD(tree, 0));
2744 else
2745 res = validate_colon(CHILD(tree, 0));
2746 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002747 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002748 /* Must be [test] ':' [test] [sliceop],
2749 * but at least one of the optional components will
2750 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002751 */
2752 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002753 res = validate_test(CHILD(tree, 0));
2754 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002755 }
2756 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002757 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002758 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002759 int rem = nch - ++offset;
2760 if (rem) {
2761 if (TYPE(CHILD(tree, offset)) == test) {
2762 res = validate_test(CHILD(tree, offset));
2763 ++offset;
2764 --rem;
2765 }
2766 if (res && rem)
2767 res = validate_sliceop(CHILD(tree, offset));
2768 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002769 }
2770 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002771}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002772
2773
Guido van Rossum47478871996-08-21 14:32:37 +00002774static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002775validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002776{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002777 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002778 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002779 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002780 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002781 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002782 }
Guido van Rossum47478871996-08-21 14:32:37 +00002783 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002784 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002785 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002786 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002787
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002788 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002789}
Guido van Rossum47478871996-08-21 14:32:37 +00002790
2791
2792static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00002793validate_test_or_star_expr(node *n)
2794{
2795 if (TYPE(n) == test)
2796 return validate_test(n);
2797 return validate_star_expr(n);
2798}
2799
2800static int
2801validate_expr_or_star_expr(node *n)
2802{
2803 if (TYPE(n) == expr)
2804 return validate_expr(n);
2805 return validate_star_expr(n);
2806}
2807
2808
2809static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002810validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002811{
2812 return (validate_repeating_list(tree, exprlist,
Benjamin Peterson4905e802009-09-27 02:43:28 +00002813 validate_expr_or_star_expr, "exprlist"));
Fred Drakeff9ea482000-04-19 13:54:15 +00002814}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002815
2816
Guido van Rossum47478871996-08-21 14:32:37 +00002817static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002818validate_dictorsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002819{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002820 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002821 int res = (validate_ntype(tree, dictorsetmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002822 && (nch >= 3)
2823 && validate_test(CHILD(tree, 0))
2824 && validate_colon(CHILD(tree, 1))
2825 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002826
Guido van Rossum3d602e31996-07-21 02:33:56 +00002827 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002828 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002829 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002830 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002831
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002832 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002833 int pos = 3;
2834 /* ( ',' test ':' test )* */
2835 while (res && (pos < nch)) {
2836 res = (validate_comma(CHILD(tree, pos))
2837 && validate_test(CHILD(tree, pos + 1))
2838 && validate_colon(CHILD(tree, pos + 2))
2839 && validate_test(CHILD(tree, pos + 3)));
2840 pos += 4;
2841 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002842 }
2843 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002844}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002845
2846
Guido van Rossum47478871996-08-21 14:32:37 +00002847static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002848validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002849{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002850 int pos;
2851 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002852 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002853 && (nch >= 2)
2854 && validate_testlist(CHILD(tree, 0))
2855 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002856
Guido van Rossum3d602e31996-07-21 02:33:56 +00002857 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002858 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002859
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002860 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002861}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002862
2863
Guido van Rossum47478871996-08-21 14:32:37 +00002864static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002865validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002866{
Fred Drakeff9ea482000-04-19 13:54:15 +00002867 int nch = 0; /* num. children on current node */
2868 int res = 1; /* result value */
2869 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002870
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002871 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002872 nch = NCH(tree);
2873 next = 0;
2874 switch (TYPE(tree)) {
2875 /*
2876 * Definition nodes.
2877 */
2878 case funcdef:
2879 res = validate_funcdef(tree);
2880 break;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002881 case with_stmt:
2882 res = validate_with_stmt(tree);
2883 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002884 case classdef:
2885 res = validate_class(tree);
2886 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 case decorated:
2888 res = validate_decorated(tree);
2889 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002890 /*
2891 * "Trivial" parse tree nodes.
2892 * (Why did I call these trivial?)
2893 */
2894 case stmt:
2895 res = validate_stmt(tree);
2896 break;
2897 case small_stmt:
2898 /*
Guido van Rossum452bf512007-02-09 05:32:43 +00002899 * expr_stmt | del_stmt | pass_stmt | flow_stmt
Georg Brandl7cae87c2006-09-06 06:51:57 +00002900 * | import_stmt | global_stmt | assert_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002901 */
2902 res = validate_small_stmt(tree);
2903 break;
2904 case flow_stmt:
2905 res = (validate_numnodes(tree, 1, "flow_stmt")
2906 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2907 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002908 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002909 || (TYPE(CHILD(tree, 0)) == return_stmt)
2910 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2911 if (res)
2912 next = CHILD(tree, 0);
2913 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002914 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002915 break;
Fred Drake02126f22001-07-17 02:59:15 +00002916 case yield_stmt:
2917 res = validate_yield_stmt(tree);
2918 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002919 /*
2920 * Compound statements.
2921 */
2922 case simple_stmt:
2923 res = validate_simple_stmt(tree);
2924 break;
2925 case compound_stmt:
2926 res = validate_compound_stmt(tree);
2927 break;
2928 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002929 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002930 */
2931 case expr_stmt:
2932 res = validate_expr_stmt(tree);
2933 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002934 case del_stmt:
2935 res = validate_del_stmt(tree);
2936 break;
2937 case pass_stmt:
2938 res = (validate_numnodes(tree, 1, "pass")
2939 && validate_name(CHILD(tree, 0), "pass"));
2940 break;
2941 case break_stmt:
2942 res = (validate_numnodes(tree, 1, "break")
2943 && validate_name(CHILD(tree, 0), "break"));
2944 break;
2945 case continue_stmt:
2946 res = (validate_numnodes(tree, 1, "continue")
2947 && validate_name(CHILD(tree, 0), "continue"));
2948 break;
2949 case return_stmt:
2950 res = validate_return_stmt(tree);
2951 break;
2952 case raise_stmt:
2953 res = validate_raise_stmt(tree);
2954 break;
2955 case import_stmt:
2956 res = validate_import_stmt(tree);
2957 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 case import_name:
2959 res = validate_import_name(tree);
2960 break;
2961 case import_from:
2962 res = validate_import_from(tree);
2963 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002964 case global_stmt:
2965 res = validate_global_stmt(tree);
2966 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002967 case assert_stmt:
2968 res = validate_assert_stmt(tree);
2969 break;
2970 case if_stmt:
2971 res = validate_if(tree);
2972 break;
2973 case while_stmt:
2974 res = validate_while(tree);
2975 break;
2976 case for_stmt:
2977 res = validate_for(tree);
2978 break;
2979 case try_stmt:
2980 res = validate_try(tree);
2981 break;
2982 case suite:
2983 res = validate_suite(tree);
2984 break;
2985 /*
2986 * Expression nodes.
2987 */
2988 case testlist:
2989 res = validate_testlist(tree);
2990 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002991 case yield_expr:
2992 res = validate_yield_expr(tree);
2993 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002994 case test:
2995 res = validate_test(tree);
2996 break;
2997 case and_test:
2998 res = validate_and_test(tree);
2999 break;
3000 case not_test:
3001 res = validate_not_test(tree);
3002 break;
3003 case comparison:
3004 res = validate_comparison(tree);
3005 break;
3006 case exprlist:
3007 res = validate_exprlist(tree);
3008 break;
3009 case comp_op:
3010 res = validate_comp_op(tree);
3011 break;
3012 case expr:
3013 res = validate_expr(tree);
3014 break;
3015 case xor_expr:
3016 res = validate_xor_expr(tree);
3017 break;
3018 case and_expr:
3019 res = validate_and_expr(tree);
3020 break;
3021 case shift_expr:
3022 res = validate_shift_expr(tree);
3023 break;
3024 case arith_expr:
3025 res = validate_arith_expr(tree);
3026 break;
3027 case term:
3028 res = validate_term(tree);
3029 break;
3030 case factor:
3031 res = validate_factor(tree);
3032 break;
3033 case power:
3034 res = validate_power(tree);
3035 break;
3036 case atom:
3037 res = validate_atom(tree);
3038 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003039
Fred Drakeff9ea482000-04-19 13:54:15 +00003040 default:
3041 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00003042 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003043 res = 0;
3044 break;
3045 }
3046 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003047 }
3048 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003049}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003050
3051
Guido van Rossum47478871996-08-21 14:32:37 +00003052static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003053validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003054{
3055 int res = validate_eval_input(tree);
3056
3057 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003058 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003059
3060 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003061}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003062
3063
Guido van Rossum3d602e31996-07-21 02:33:56 +00003064/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003065 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003066 */
Guido van Rossum47478871996-08-21 14:32:37 +00003067static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003068validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003069{
Fred Drakec2683dd2001-07-17 19:32:05 +00003070 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003071 int nch = NCH(tree) - 1;
3072 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003073 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003074
Fred Drakec2683dd2001-07-17 19:32:05 +00003075 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003076 if (TYPE(CHILD(tree, j)) == stmt)
3077 res = validate_stmt(CHILD(tree, j));
3078 else
3079 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003080 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003081 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003082 * user. Hopefully, this won't be needed. If a user reports getting
3083 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003084 */
3085 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003086 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003087
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003088 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003089}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003090
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003091static int
3092validate_encoding_decl(node *tree)
3093{
3094 int nch = NCH(tree);
3095 int res = ((nch == 1)
3096 && validate_file_input(CHILD(tree, 0)));
3097
3098 if (!res && !PyErr_Occurred())
3099 err_string("Error Parsing encoding_decl");
3100
3101 return res;
3102}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003103
Fred Drake43f8f9b1998-04-13 16:25:46 +00003104static PyObject*
3105pickle_constructor = NULL;
3106
3107
3108static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003109parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003110{
Fred Drake268397f1998-04-29 14:16:32 +00003111 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003112 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003113 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003114 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003115
Fred Drakec2683dd2001-07-17 19:32:05 +00003116 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003117 PyObject *newargs;
3118 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003119
Fred Drake2a6875e1999-09-20 22:32:18 +00003120 if ((empty_dict = PyDict_New()) == NULL)
3121 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003122 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003123 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003124 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003125 if (tuple != NULL) {
3126 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3127 Py_DECREF(tuple);
3128 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003129 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003130 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003131 }
3132 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003133 Py_XDECREF(empty_dict);
3134
Fred Drake43f8f9b1998-04-13 16:25:46 +00003135 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003136}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003137
3138
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003139/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003140 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003141 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003142 * We'd really have to write a wrapper around it all anyway to allow
3143 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003144 */
3145static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003146 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003147 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003148 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003149 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003150 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003151 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003152 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003153 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003154 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003155 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003156 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003157 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003158 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003159 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003160 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003161 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003162 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003163 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003164
Fred Drake43f8f9b1998-04-13 16:25:46 +00003165 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003166 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003167 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003168
Fred Drake268397f1998-04-29 14:16:32 +00003169 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003170 };
3171
3172
Martin v. Löwis1a214512008-06-11 05:26:20 +00003173
3174static struct PyModuleDef parsermodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 PyModuleDef_HEAD_INIT,
3176 "parser",
3177 NULL,
3178 -1,
3179 parser_functions,
3180 NULL,
3181 NULL,
3182 NULL,
3183 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00003184};
3185
3186PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003187
Mark Hammond62b1ab12002-07-23 06:31:15 +00003188PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003189PyInit_parser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003190{
Fred Drake13130bc2001-08-15 16:44:56 +00003191 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003192
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00003193 if (PyType_Ready(&PyST_Type) < 0)
3194 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00003195 module = PyModule_Create(&parsermodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003196 if (module == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 return NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003198
Fred Drake7a15ba51999-09-09 14:21:52 +00003199 if (parser_error == 0)
3200 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003201
Tim Peters6a627252003-07-21 14:25:23 +00003202 if (parser_error == 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003203 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003204 /* CAUTION: The code next used to skip bumping the refcount on
Martin v. Löwis1a214512008-06-11 05:26:20 +00003205 * parser_error. That's a disaster if PyInit_parser() gets called more
Tim Peters6a627252003-07-21 14:25:23 +00003206 * than once. By incref'ing, we ensure that each module dict that
3207 * gets created owns its reference to the shared parser_error object,
3208 * and the file static parser_error vrbl owns a reference too.
3209 */
3210 Py_INCREF(parser_error);
3211 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003212 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003213
Fred Drakec2683dd2001-07-17 19:32:05 +00003214 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003215 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003216
Fred Drake13130bc2001-08-15 16:44:56 +00003217 PyModule_AddStringConstant(module, "__copyright__",
3218 parser_copyright_string);
3219 PyModule_AddStringConstant(module, "__doc__",
3220 parser_doc_string);
3221 PyModule_AddStringConstant(module, "__version__",
3222 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003223
Fred Drake78bdb9b2001-07-19 20:17:15 +00003224 /* Register to support pickling.
3225 * If this fails, the import of this module will fail because an
3226 * exception will be raised here; should we clear the exception?
3227 */
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003228 copyreg = PyImport_ImportModuleNoBlock("copyreg");
Fred Drake13130bc2001-08-15 16:44:56 +00003229 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003230 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003231
Fred Drake13130bc2001-08-15 16:44:56 +00003232 func = PyObject_GetAttrString(copyreg, "pickle");
3233 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3234 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003235 Py_XINCREF(pickle_constructor);
3236 if ((func != NULL) && (pickle_constructor != NULL)
3237 && (pickler != NULL)) {
3238 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003239
Thomas Wouters477c8d52006-05-27 19:21:47 +00003240 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3241 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003242 Py_XDECREF(res);
3243 }
3244 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003245 Py_XDECREF(pickle_constructor);
3246 Py_XDECREF(pickler);
3247 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003248 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00003249 return module;
Fred Drakeff9ea482000-04-19 13:54:15 +00003250}