blob: b9c1201a9d8a555137645123dc73b4dbd63c603c [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000026 */
27
Fred Drakeff9ea482000-04-19 13:54:15 +000028#include "Python.h" /* general Python API */
Benjamin Petersonf216c942008-10-31 02:28:05 +000029#include "Python-ast.h" /* mod_ty */
Fred Drakeff9ea482000-04-19 13:54:15 +000030#include "graminit.h" /* symbols defined in the grammar */
31#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000032#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000033#include "token.h" /* token definitions */
Benjamin Petersonf216c942008-10-31 02:28:05 +000034#include "grammar.h"
35#include "parsetok.h"
Fred Drakeff9ea482000-04-19 13:54:15 +000036 /* ISTERMINAL() / ISNONTERMINAL() */
Benjamin Petersonf216c942008-10-31 02:28:05 +000037#undef Yield
38#include "ast.h"
Benjamin Petersonf216c942008-10-31 02:28:05 +000039
40extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000041
Fred Drake268397f1998-04-29 14:16:32 +000042#ifdef lint
43#include <note.h>
44#else
45#define NOTE(x)
46#endif
47
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000048/* String constants used to initialize module attributes.
49 *
50 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000051static char parser_copyright_string[] =
52"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000053University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
54Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
55Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000056
57
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000058PyDoc_STRVAR(parser_doc_string,
59"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000060
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000061static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000062
63
Martin v. Löwis18e16552006-02-15 17:27:45 +000064typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000065typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000066 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000067 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000068
Thomas Wouters7e474022000-07-16 12:04:32 +000069/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000070 * original copyright statement is included below, and continues to apply
71 * in full to the function immediately following. All other material is
72 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
73 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000074 * new naming conventions. Added arguments to provide support for creating
75 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000076 */
77
Guido van Rossum52f2c051993-11-10 12:53:24 +000078
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000079static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000080node2tuple(node *n, /* node to convert */
81 SeqMaker mkseq, /* create sequence */
82 SeqInserter addelem, /* func. to add elem. in seq. */
Thomas Wouters89f507f2006-12-13 04:49:30 +000083 int lineno, /* include line numbers? */
84 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000085{
Guido van Rossum3d602e31996-07-21 02:33:56 +000086 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000087 Py_INCREF(Py_None);
88 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000089 }
90 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000091 int i;
92 PyObject *v;
93 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000094
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000095 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000096 if (v == NULL)
97 return (v);
Christian Heimes217cfd12007-12-02 14:31:20 +000098 w = PyLong_FromLong(TYPE(n));
Fred Drakeff9ea482000-04-19 13:54:15 +000099 if (w == NULL) {
100 Py_DECREF(v);
101 return ((PyObject*) NULL);
102 }
103 (void) addelem(v, 0, w);
104 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000105 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000106 if (w == NULL) {
107 Py_DECREF(v);
108 return ((PyObject*) NULL);
109 }
110 (void) addelem(v, i+1, w);
111 }
Tim Peters6a627252003-07-21 14:25:23 +0000112
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000113 if (TYPE(n) == encoding_decl)
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000114 (void) addelem(v, i+1, PyUnicode_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000115 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000116 }
117 else if (ISTERMINAL(TYPE(n))) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000118 PyObject *result = mkseq(2 + lineno + col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000119 if (result != NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000120 (void) addelem(result, 0, PyLong_FromLong(TYPE(n)));
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000121 (void) addelem(result, 1, PyUnicode_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000122 if (lineno == 1)
Christian Heimes217cfd12007-12-02 14:31:20 +0000123 (void) addelem(result, 2, PyLong_FromLong(n->n_lineno));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000124 if (col_offset == 1)
Christian Heimes217cfd12007-12-02 14:31:20 +0000125 (void) addelem(result, 3, PyLong_FromLong(n->n_col_offset));
Fred Drakeff9ea482000-04-19 13:54:15 +0000126 }
127 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000128 }
129 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000130 PyErr_SetString(PyExc_SystemError,
131 "unrecognized parse tree node type");
132 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000133 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000134}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000135/*
136 * End of material copyrighted by Stichting Mathematisch Centrum.
137 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000138
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000139
140
141/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000142 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000143 * field of the object type to identify which any given object represents.
144 * These should probably go in an external header to allow other extensions
145 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000146 */
147
Fred Drakec2683dd2001-07-17 19:32:05 +0000148#define PyST_EXPR 1
149#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000150
151
152/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000153 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000154 * naming style, but the code uses the new naming convention.
155 */
156
157static PyObject*
158parser_error = 0;
159
160
Fred Drakec2683dd2001-07-17 19:32:05 +0000161typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000162 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000163 node* st_node; /* the node* returned by the parser */
164 int st_type; /* EXPR or SUITE ? */
Benjamin Petersonf216c942008-10-31 02:28:05 +0000165 PyCompilerFlags st_flags; /* Parser and compiler flags */
Fred Drakec2683dd2001-07-17 19:32:05 +0000166} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000167
168
Jeremy Hylton938ace62002-07-17 16:30:39 +0000169static void parser_free(PyST_Object *st);
Mark Dickinson211c6252009-02-01 10:28:51 +0000170static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op);
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000171static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
172static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
173static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
174static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
175static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
Fred Drake503d8d61998-04-13 18:45:18 +0000176
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000177#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
178
179static PyMethodDef parser_methods[] = {
180 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
181 PyDoc_STR("Compile this ST object into a code object.")},
182 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
183 PyDoc_STR("Determines if this ST object was created from an expression.")},
184 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
185 PyDoc_STR("Determines if this ST object was created from a suite.")},
186 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
187 PyDoc_STR("Creates a list-tree representation of this ST.")},
188 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
189 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
190
191 {NULL, NULL, 0, NULL}
192};
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000193
Fred Drake268397f1998-04-29 14:16:32 +0000194static
Fred Drakec2683dd2001-07-17 19:32:05 +0000195PyTypeObject PyST_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000196 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000197 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000198 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000199 0, /* tp_itemsize */
200 (destructor)parser_free, /* tp_dealloc */
201 0, /* tp_print */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000202 0, /* tp_getattr */
Fred Drakeff9ea482000-04-19 13:54:15 +0000203 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000204 0, /* tp_reserved */
Fred Drakeff9ea482000-04-19 13:54:15 +0000205 0, /* tp_repr */
206 0, /* tp_as_number */
207 0, /* tp_as_sequence */
208 0, /* tp_as_mapping */
209 0, /* tp_hash */
210 0, /* tp_call */
211 0, /* tp_str */
212 0, /* tp_getattro */
213 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000214
215 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000216 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000217
Fred Drakeff9ea482000-04-19 13:54:15 +0000218 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000219
220 /* __doc__ */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000221 "Intermediate representation of a Python parse tree.",
222 0, /* tp_traverse */
223 0, /* tp_clear */
Mark Dickinson211c6252009-02-01 10:28:51 +0000224 parser_richcompare, /* tp_richcompare */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000225 0, /* tp_weaklistoffset */
226 0, /* tp_iter */
227 0, /* tp_iternext */
228 parser_methods, /* tp_methods */
Fred Drakec2683dd2001-07-17 19:32:05 +0000229}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000230
231
Mark Dickinson211c6252009-02-01 10:28:51 +0000232/* PyST_Type isn't subclassable, so just check ob_type */
233#define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type)
234
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000235static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000236parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000237{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000238 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000239
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000240 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000241 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000242
243 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000244 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000245
246 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000247 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000248
249 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000250 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000251
252 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000253 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000254
255 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000256 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000257
Fred Drakeff9ea482000-04-19 13:54:15 +0000258 if (v != 0)
259 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000260 }
261 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000262}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000263
Mark Dickinson211c6252009-02-01 10:28:51 +0000264/* parser_richcompare(PyObject* left, PyObject* right, int op)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265 *
266 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
267 * This really just wraps a call to parser_compare_nodes() with some easy
268 * checks and protection code.
269 *
270 */
Mark Dickinson211c6252009-02-01 10:28:51 +0000271
272#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
273
274static PyObject *
275parser_richcompare(PyObject *left, PyObject *right, int op)
Guido van Rossum47478871996-08-21 14:32:37 +0000276{
Mark Dickinson211c6252009-02-01 10:28:51 +0000277 int result;
278 PyObject *v;
279
280 /* neither argument should be NULL, unless something's gone wrong */
281 if (left == NULL || right == NULL) {
282 PyErr_BadInternalCall();
283 return NULL;
284 }
285
286 /* both arguments should be instances of PyST_Object */
287 if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) {
288 v = Py_NotImplemented;
289 goto finished;
290 }
291
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000292 if (left == right)
Mark Dickinson211c6252009-02-01 10:28:51 +0000293 /* if arguments are identical, they're equal */
294 result = 0;
295 else
296 result = parser_compare_nodes(((PyST_Object *)left)->st_node,
297 ((PyST_Object *)right)->st_node);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000298
Mark Dickinson211c6252009-02-01 10:28:51 +0000299 /* Convert return value to a Boolean */
300 switch (op) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000301 case Py_EQ:
Mark Dickinson211c6252009-02-01 10:28:51 +0000302 v = TEST_COND(result == 0);
303 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000304 case Py_NE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000305 v = TEST_COND(result != 0);
306 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000307 case Py_LE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000308 v = TEST_COND(result <= 0);
309 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000310 case Py_GE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000311 v = TEST_COND(result >= 0);
312 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000313 case Py_LT:
Mark Dickinson211c6252009-02-01 10:28:51 +0000314 v = TEST_COND(result < 0);
315 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000316 case Py_GT:
Mark Dickinson211c6252009-02-01 10:28:51 +0000317 v = TEST_COND(result > 0);
318 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000319 default:
Mark Dickinson211c6252009-02-01 10:28:51 +0000320 PyErr_BadArgument();
321 return NULL;
322 }
323 finished:
324 Py_INCREF(v);
325 return v;
Fred Drakeff9ea482000-04-19 13:54:15 +0000326}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000327
Fred Drakec2683dd2001-07-17 19:32:05 +0000328/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000329 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000330 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000331 * 'wrapper' object that holds a node* and allows it to be passed around in
332 * Python code.
333 *
334 */
335static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000336parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000337{
Fred Drakec2683dd2001-07-17 19:32:05 +0000338 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000339
340 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000341 o->st_node = st;
342 o->st_type = type;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000343 o->st_flags.cf_flags = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000344 }
Fred Drake268397f1998-04-29 14:16:32 +0000345 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000346 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000347 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000348 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000349}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000350
351
Fred Drakec2683dd2001-07-17 19:32:05 +0000352/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000353 *
354 * This is called by a del statement that reduces the reference count to 0.
355 *
356 */
357static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000358parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000359{
Fred Drakec2683dd2001-07-17 19:32:05 +0000360 PyNode_Free(st->st_node);
361 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000362}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000363
364
Fred Drakec2683dd2001-07-17 19:32:05 +0000365/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000366 *
367 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000368 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000369 *
370 */
371static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000372parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000373{
Guido van Rossum47478871996-08-21 14:32:37 +0000374 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000375 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000376 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000377 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000378
Georg Brandl30704ea02008-07-23 15:07:12 +0000379 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000380
Martin v. Löwis1a214512008-06-11 05:26:20 +0000381 if (self == NULL || PyModule_Check(self)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000382 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
383 &PyST_Type, &self, &line_option,
384 &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000385 }
Fred Drake503d8d61998-04-13 18:45:18 +0000386 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000387 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
388 &line_option, &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000389 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000390 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000391 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000392 if (line_option != NULL) {
393 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
394 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000395 if (col_option != NULL) {
396 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
397 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000398 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000399 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000400 * since it's known to work already.
401 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000402 res = node2tuple(((PyST_Object*)self)->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000403 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000404 }
405 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000406}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000407
408
Fred Drakec2683dd2001-07-17 19:32:05 +0000409/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000410 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000411 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000412 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000413 *
414 */
415static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000416parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000417{
Guido van Rossum47478871996-08-21 14:32:37 +0000418 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000419 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000420 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000421 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000422
Georg Brandl30704ea02008-07-23 15:07:12 +0000423 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000424
Martin v. Löwis1a214512008-06-11 05:26:20 +0000425 if (self == NULL || PyModule_Check(self))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000426 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
427 &PyST_Type, &self, &line_option,
428 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000429 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000430 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
431 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000432 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000433 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000434 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000435 if (line_option != 0) {
436 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
437 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000438 if (col_option != NULL) {
439 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
440 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000441 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000442 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000443 * since it's known to work already.
444 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000445 res = node2tuple(self->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000446 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000447 }
448 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000449}
Guido van Rossum47478871996-08-21 14:32:37 +0000450
451
Fred Drakec2683dd2001-07-17 19:32:05 +0000452/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000453 *
454 * This function creates code objects from the parse tree represented by
455 * the passed-in data object. An optional file name is passed in as well.
456 *
457 */
458static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000459parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000460{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000461 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000462 PyArena* arena;
463 mod_ty mod;
Fred Drakec2683dd2001-07-17 19:32:05 +0000464 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000465 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000466
Georg Brandl30704ea02008-07-23 15:07:12 +0000467 static char *keywords[] = {"st", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000468
Martin v. Löwis1a214512008-06-11 05:26:20 +0000469 if (self == NULL || PyModule_Check(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000470 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
471 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000472 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000473 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000474 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000475
Benjamin Petersonf216c942008-10-31 02:28:05 +0000476 if (ok) {
477 arena = PyArena_New();
478 if (arena) {
479 mod = PyAST_FromNode(self->st_node, &(self->st_flags), str, arena);
480 if (mod) {
481 res = (PyObject *)PyAST_Compile(mod, str, &(self->st_flags), arena);
482 }
483 PyArena_Free(arena);
484 }
485 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000486
487 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000488}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000489
490
491/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
492 * PyObject* parser_issuite(PyObject* self, PyObject* args)
493 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000494 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000495 * a statement suite, respectively. The return is a Python truth value.
496 *
497 */
498static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000499parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000500{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000501 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000502 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000503
Georg Brandl30704ea02008-07-23 15:07:12 +0000504 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000505
Martin v. Löwis1a214512008-06-11 05:26:20 +0000506 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000507 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000508 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000509 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000510 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000511
512 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000513 /* Check to see if the ST represents an expression or not. */
514 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000515 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000516 }
517 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000518}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000519
520
521static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000522parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000523{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000524 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000525 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000526
Georg Brandl30704ea02008-07-23 15:07:12 +0000527 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000528
Martin v. Löwis1a214512008-06-11 05:26:20 +0000529 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000530 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000531 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000532 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000533 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000534
535 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000536 /* Check to see if the ST represents an expression or not. */
537 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000538 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000539 }
540 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000541}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000542
543
Guido van Rossum3d602e31996-07-21 02:33:56 +0000544/* err_string(char* message)
545 *
546 * Sets the error string for an exception of type ParserError.
547 *
548 */
549static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000550err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000551{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000552 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000553}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000554
555
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000556/* PyObject* parser_do_parse(PyObject* args, int type)
557 *
558 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000559 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000560 *
561 */
562static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000563parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000564{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000565 char* string = 0;
566 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000567 int flags = 0;
568 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000569
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000570 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000571
572 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000573 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
574 &_PyParser_Grammar,
575 (type == PyST_EXPR)
576 ? eval_input : file_input,
577 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (n) {
580 res = parser_newstobject(n, type);
Benjamin Petersonf216c942008-10-31 02:28:05 +0000581 if (res)
582 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
583 }
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500584 else {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000585 PyParser_SetError(&err);
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500586 }
Benjamin Petersonf0cdbad2011-06-05 22:14:05 -0500587 PyParser_ClearError(&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);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000797 if (temp_str == NULL) {
798 Py_DECREF(temp);
799 Py_XDECREF(elem);
800 return 0;
801 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000802 strn = (char *)PyObject_MALLOC(len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000803 if (strn != NULL)
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000804 (void) memcpy(strn, temp_str, len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000805 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000806 }
807 else if (!ISNONTERMINAL(type)) {
808 /*
809 * It has to be one or the other; this is an error.
810 * Throw an exception.
811 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000812 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
813 PyErr_SetObject(parser_error, err);
814 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000815 Py_XDECREF(elem);
816 return (0);
817 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000818 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000819 if (err == E_NOMEM) {
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 return (node *) PyErr_NoMemory();
823 }
824 if (err == E_OVERFLOW) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000825 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000826 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000827 PyErr_SetString(PyExc_ValueError,
828 "unsupported number of child nodes");
829 return NULL;
830 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000831
Fred Drakeff9ea482000-04-19 13:54:15 +0000832 if (ISNONTERMINAL(type)) {
833 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000834
Fred Drakeff9ea482000-04-19 13:54:15 +0000835 if (new_child != build_node_children(elem, new_child, line_num)) {
836 Py_XDECREF(elem);
837 return (0);
838 }
839 }
840 else if (type == NEWLINE) { /* It's true: we increment the */
841 ++(*line_num); /* line number *after* the newline! */
842 }
843 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000844 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000845 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000846}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000847
848
849static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000850build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000851{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000852 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000853 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000854 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000855
Guido van Rossum47478871996-08-21 14:32:37 +0000856 if (temp != NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000857 num = PyLong_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000858 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000859 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000860 /*
861 * The tuple is simple, but it doesn't start with a start symbol.
862 * Throw an exception now and be done with it.
863 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000864 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000865 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000866 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000867 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000868 }
869 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000870 /*
871 * Not efficient, but that can be handled later.
872 */
873 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000874 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000875
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000876 if (num == encoding_decl) {
877 encoding = PySequence_GetItem(tuple, 2);
878 /* tuple isn't borrowed anymore here, need to DECREF */
879 tuple = PySequence_GetSlice(tuple, 0, 2);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000880 if (tuple == NULL)
881 return NULL;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000882 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000883 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000884 if (res != NULL) {
885 if (res != build_node_children(tuple, res, &line_num)) {
886 PyNode_Free(res);
887 res = NULL;
888 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000889 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000890 Py_ssize_t len;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000891 const char *temp;
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000892 temp = _PyUnicode_AsStringAndSize(encoding, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000893 if (temp == NULL) {
894 Py_DECREF(res);
895 Py_DECREF(encoding);
896 Py_DECREF(tuple);
897 return NULL;
898 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000899 res->n_str = (char *)PyObject_MALLOC(len + 1);
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000900 if (res->n_str != NULL && temp != NULL)
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000901 (void) memcpy(res->n_str, temp, len + 1);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000902 Py_DECREF(encoding);
903 Py_DECREF(tuple);
904 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000905 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000906 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000907 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000908 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000909 * NONTERMINAL, we can't use it. Not sure the implementation
910 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000911 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000912 PyObject *err = Py_BuildValue("os", tuple,
913 "Illegal component tuple.");
914 PyErr_SetObject(parser_error, err);
915 Py_XDECREF(err);
916 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000917
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000918 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000919}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000920
921
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000922/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000923 * Validation routines used within the validation section:
924 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000925static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000926
Fred Drakeff9ea482000-04-19 13:54:15 +0000927#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
928#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
929#define validate_colon(ch) validate_terminal(ch, COLON, ":")
930#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
931#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
932#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
933#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
934#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
935#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
936#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
937#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
938#define validate_star(ch) validate_terminal(ch, STAR, "*")
939#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
940#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
941#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000942#define validate_at(ch) validate_terminal(ch, AT, "@")
Mark Dickinsonea7e9f92012-04-29 18:34:40 +0100943#define validate_rarrow(ch) validate_terminal(ch, RARROW, "->")
Fred Drakeff9ea482000-04-19 13:54:15 +0000944#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000945
Fred Drake0ac9b072000-09-12 21:58:06 +0000946#define VALIDATER(n) static int validate_##n(node *tree)
947
Fred Drakeff9ea482000-04-19 13:54:15 +0000948VALIDATER(node); VALIDATER(small_stmt);
949VALIDATER(class); VALIDATER(node);
950VALIDATER(parameters); VALIDATER(suite);
951VALIDATER(testlist); VALIDATER(varargslist);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000952VALIDATER(vfpdef);
Fred Drakeff9ea482000-04-19 13:54:15 +0000953VALIDATER(stmt); VALIDATER(simple_stmt);
954VALIDATER(expr_stmt); VALIDATER(power);
Guido van Rossum452bf512007-02-09 05:32:43 +0000955VALIDATER(del_stmt);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000956VALIDATER(return_stmt); VALIDATER(raise_stmt);
957VALIDATER(import_stmt); VALIDATER(import_stmt);
958VALIDATER(import_name); VALIDATER(yield_stmt);
Mark Dickinson407b3bd2012-04-29 22:18:31 +0100959VALIDATER(global_stmt); VALIDATER(nonlocal_stmt);
960VALIDATER(assert_stmt);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000961VALIDATER(compound_stmt); VALIDATER(test_or_star_expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000962VALIDATER(while); VALIDATER(for);
963VALIDATER(try); VALIDATER(except_clause);
964VALIDATER(test); VALIDATER(and_test);
965VALIDATER(not_test); VALIDATER(comparison);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000966VALIDATER(comp_op);
Guido van Rossum0368b722007-05-11 16:50:42 +0000967VALIDATER(star_expr); VALIDATER(expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000968VALIDATER(xor_expr); VALIDATER(and_expr);
969VALIDATER(shift_expr); VALIDATER(arith_expr);
970VALIDATER(term); VALIDATER(factor);
971VALIDATER(atom); VALIDATER(lambdef);
972VALIDATER(trailer); VALIDATER(subscript);
973VALIDATER(subscriptlist); VALIDATER(sliceop);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000974VALIDATER(exprlist); VALIDATER(dictorsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000975VALIDATER(arglist); VALIDATER(argument);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000976VALIDATER(comp_for);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000977VALIDATER(comp_iter); VALIDATER(comp_if);
978VALIDATER(testlist_comp); VALIDATER(yield_expr);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000979VALIDATER(or_test);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000980VALIDATER(test_nocond); VALIDATER(lambdef_nocond);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000981VALIDATER(yield_arg);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000982
Fred Drake0ac9b072000-09-12 21:58:06 +0000983#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000984
Fred Drakeff9ea482000-04-19 13:54:15 +0000985#define is_even(n) (((n) & 1) == 0)
986#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000987
988
989static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000990validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000991{
Fred Drake0ac9b072000-09-12 21:58:06 +0000992 if (TYPE(n) != t) {
993 PyErr_Format(parser_error, "Expected node type %d, got %d.",
994 t, TYPE(n));
995 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000996 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000997 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000998}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000999
1000
Fred Drakee7ab64e2000-04-25 04:14:46 +00001001/* Verifies that the number of child nodes is exactly 'num', raising
1002 * an exception if it isn't. The exception message does not indicate
1003 * the exact number of nodes, allowing this to be used to raise the
1004 * "right" exception when the wrong number of nodes is present in a
1005 * specific variant of a statement's syntax. This is commonly used
1006 * in that fashion.
1007 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001008static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001009validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001010{
Guido van Rossum3d602e31996-07-21 02:33:56 +00001011 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001012 PyErr_Format(parser_error,
1013 "Illegal number of children for %s node.", name);
1014 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001015 }
Fred Drake0ac9b072000-09-12 21:58:06 +00001016 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00001017}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001018
1019
1020static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001021validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +00001022{
Guido van Rossum3d602e31996-07-21 02:33:56 +00001023 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +00001024 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001025
1026 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001027 PyErr_Format(parser_error,
1028 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001029 }
1030 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001031}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001032
1033
Guido van Rossum47478871996-08-21 14:32:37 +00001034/* X (',' X) [',']
1035 */
1036static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +00001037validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +00001038 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001039{
1040 int nch = NCH(tree);
1041 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +00001042 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +00001043
1044 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001045 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +00001046 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001047 if (is_even(nch))
1048 res = validate_comma(CHILD(tree, --nch));
1049 if (res && nch > 1) {
1050 int pos = 1;
1051 for ( ; res && pos < nch; pos += 2)
1052 res = (validate_comma(CHILD(tree, pos))
1053 && vfunc(CHILD(tree, pos + 1)));
1054 }
Guido van Rossum47478871996-08-21 14:32:37 +00001055 }
1056 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001057}
Guido van Rossum47478871996-08-21 14:32:37 +00001058
1059
Fred Drakecff283c2000-08-21 22:24:43 +00001060/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001061 *
1062 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001063 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00001064 */
Guido van Rossum47478871996-08-21 14:32:37 +00001065static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001066validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001067{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001068 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +00001069 int res = (validate_ntype(tree, classdef) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001071
Guido van Rossum3d602e31996-07-21 02:33:56 +00001072 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001073 res = (validate_name(CHILD(tree, 0), "class")
1074 && validate_ntype(CHILD(tree, 1), NAME)
1075 && validate_colon(CHILD(tree, nch - 2))
1076 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001077 }
Brett Cannonf4189912005-04-09 02:30:16 +00001078 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001079 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +00001080 }
Guido van Rossumfc158e22007-11-15 19:17:28 +00001081
Brett Cannonf4189912005-04-09 02:30:16 +00001082 if (res) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 if (nch == 7) {
1084 res = ((validate_lparen(CHILD(tree, 2)) &&
1085 validate_arglist(CHILD(tree, 3)) &&
1086 validate_rparen(CHILD(tree, 4))));
1087 }
1088 else if (nch == 6) {
1089 res = (validate_lparen(CHILD(tree,2)) &&
1090 validate_rparen(CHILD(tree,3)));
1091 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001092 }
1093 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001094}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001095
1096
Guido van Rossum3d602e31996-07-21 02:33:56 +00001097/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001098 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001099 */
Guido van Rossum47478871996-08-21 14:32:37 +00001100static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001101validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001102{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001103 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001104 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001105 && (nch >= 4)
1106 && validate_name(CHILD(tree, 0), "if")
1107 && validate_test(CHILD(tree, 1))
1108 && validate_colon(CHILD(tree, 2))
1109 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001110
1111 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001112 /* ... 'else' ':' suite */
1113 res = (validate_name(CHILD(tree, nch - 3), "else")
1114 && validate_colon(CHILD(tree, nch - 2))
1115 && validate_suite(CHILD(tree, nch - 1)));
1116 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001117 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001118 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001119 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001120 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001121 /* Will catch the case for nch < 4 */
1122 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001123 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001124 /* ... ('elif' test ':' suite)+ ... */
1125 int j = 4;
1126 while ((j < nch) && res) {
1127 res = (validate_name(CHILD(tree, j), "elif")
1128 && validate_colon(CHILD(tree, j + 2))
1129 && validate_test(CHILD(tree, j + 1))
1130 && validate_suite(CHILD(tree, j + 3)));
1131 j += 4;
1132 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001133 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001134 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001135}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001136
1137
Guido van Rossum3d602e31996-07-21 02:33:56 +00001138/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001139 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001140 *
1141 */
Guido van Rossum47478871996-08-21 14:32:37 +00001142static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001143validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001144{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001145 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001146 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001147
Guido van Rossum3d602e31996-07-21 02:33:56 +00001148 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001149 res = (validate_lparen(CHILD(tree, 0))
1150 && validate_rparen(CHILD(tree, nch - 1)));
1151 if (res && (nch == 3))
1152 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001153 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001154 else {
1155 (void) validate_numnodes(tree, 2, "parameters");
1156 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001157 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001158}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001159
1160
Fred Drakecff283c2000-08-21 22:24:43 +00001161/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001162 *
1163 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001164 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001165 * | NEWLINE INDENT stmt+ DEDENT
1166 */
Guido van Rossum47478871996-08-21 14:32:37 +00001167static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001168validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001169{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001170 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001171 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001172
Guido van Rossum3d602e31996-07-21 02:33:56 +00001173 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001174 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001175 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001176 /* NEWLINE INDENT stmt+ DEDENT */
1177 res = (validate_newline(CHILD(tree, 0))
1178 && validate_indent(CHILD(tree, 1))
1179 && validate_stmt(CHILD(tree, 2))
1180 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001181
Fred Drakeff9ea482000-04-19 13:54:15 +00001182 if (res && (nch > 4)) {
1183 int i = 3;
1184 --nch; /* forget the DEDENT */
1185 for ( ; res && (i < nch); ++i)
1186 res = validate_stmt(CHILD(tree, i));
1187 }
1188 else if (nch < 4)
1189 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001190 }
1191 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001192}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001193
1194
Guido van Rossum47478871996-08-21 14:32:37 +00001195static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001196validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001197{
Guido van Rossum47478871996-08-21 14:32:37 +00001198 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001199 validate_test, "testlist"));
1200}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001201
Guido van Rossum1c917072001-10-15 15:44:05 +00001202static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00001203validate_testlist_star_expr(node *tl)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001204{
Benjamin Peterson4905e802009-09-27 02:43:28 +00001205 return (validate_repeating_list(tl, testlist_star_expr, validate_test_or_star_expr,
1206 "testlist"));
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001207}
1208
1209
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001210/* validate either vfpdef or tfpdef.
1211 * vfpdef: NAME
1212 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001213 */
1214static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001215validate_vfpdef(node *tree)
Neal Norwitzc1505362006-12-28 06:47:50 +00001216{
1217 int nch = NCH(tree);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001218 if (TYPE(tree) == vfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001219 return nch == 1 && validate_name(CHILD(tree, 0), NULL);
1220 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001221 else if (TYPE(tree) == tfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001222 if (nch == 1) {
1223 return validate_name(CHILD(tree, 0), NULL);
1224 }
1225 else if (nch == 3) {
1226 return validate_name(CHILD(tree, 0), NULL) &&
1227 validate_colon(CHILD(tree, 1)) &&
1228 validate_test(CHILD(tree, 2));
1229 }
1230 }
1231 return 0;
1232}
1233
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001234/* '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001235 * ..or tfpdef in place of vfpdef. vfpdef: NAME; tfpdef: NAME [':' test]
Fred Drakecff283c2000-08-21 22:24:43 +00001236 */
1237static int
1238validate_varargslist_trailer(node *tree, int start)
1239{
1240 int nch = NCH(tree);
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001241 int res = 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001242
1243 if (nch <= start) {
1244 err_string("expected variable argument trailer for varargslist");
1245 return 0;
1246 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001247 if (TYPE(CHILD(tree, start)) == STAR) {
Fred Drakecff283c2000-08-21 22:24:43 +00001248 /*
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001249 * '*' [vfpdef]
Fred Drakecff283c2000-08-21 22:24:43 +00001250 */
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001251 res = validate_star(CHILD(tree, start++));
1252 if (res && start < nch && (TYPE(CHILD(tree, start)) == vfpdef ||
1253 TYPE(CHILD(tree, start)) == tfpdef))
1254 res = validate_vfpdef(CHILD(tree, start++));
1255 /*
1256 * (',' vfpdef ['=' test])*
1257 */
1258 while (res && start + 1 < nch && (
1259 TYPE(CHILD(tree, start + 1)) == vfpdef ||
1260 TYPE(CHILD(tree, start + 1)) == tfpdef)) {
1261 res = (validate_comma(CHILD(tree, start++))
1262 && validate_vfpdef(CHILD(tree, start++)));
1263 if (res && start + 1 < nch && TYPE(CHILD(tree, start)) == EQUAL)
1264 res = (validate_equal(CHILD(tree, start++))
1265 && validate_test(CHILD(tree, start++)));
1266 }
1267 /*
1268 * [',' '**' vfpdef]
1269 */
1270 if (res && start + 2 < nch && TYPE(CHILD(tree, start+1)) == DOUBLESTAR)
1271 res = (validate_comma(CHILD(tree, start++))
1272 && validate_doublestar(CHILD(tree, start++))
1273 && validate_vfpdef(CHILD(tree, start++)));
1274 }
1275 else if (TYPE(CHILD(tree, start)) == DOUBLESTAR) {
1276 /*
1277 * '**' vfpdef
1278 */
1279 if (start + 1 < nch)
1280 res = (validate_doublestar(CHILD(tree, start++))
1281 && validate_vfpdef(CHILD(tree, start++)));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001282 else {
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001283 res = 0;
1284 err_string("expected vfpdef after ** in varargslist trailer");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001285 }
Fred Drakecff283c2000-08-21 22:24:43 +00001286 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001287 else {
1288 res = 0;
1289 err_string("expected * or ** in varargslist trailer");
Fred Drakecff283c2000-08-21 22:24:43 +00001290 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001291
1292 if (res && start != nch) {
1293 res = 0;
1294 err_string("unexpected extra children in varargslist trailer");
1295 }
Fred Drakecff283c2000-08-21 22:24:43 +00001296 return res;
1297}
1298
1299
Neal Norwitzc1505362006-12-28 06:47:50 +00001300/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001301 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001302 * Validate typedargslist or varargslist.
1303 *
1304 * typedargslist: ((tfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001305 * ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
1306 * '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001307 * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001308 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001309 * varargslist: ((vfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001310 * ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
1311 * '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001312 * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001313 * vfpdef: NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001314 *
1315 */
Guido van Rossum47478871996-08-21 14:32:37 +00001316static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001317validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001318{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001319 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001320 int res = (TYPE(tree) == varargslist ||
1321 TYPE(tree) == typedargslist) &&
1322 (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001323 int sym;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001324 node *ch;
1325 int i = 0;
Guido van Rossumfc158e22007-11-15 19:17:28 +00001326
Fred Drakeb6429a22000-12-11 22:08:27 +00001327 if (!res)
1328 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001329 if (nch < 1) {
1330 err_string("varargslist missing child nodes");
1331 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001332 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001333 while (i < nch) {
Guido van Rossumfc158e22007-11-15 19:17:28 +00001334 ch = CHILD(tree, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001335 sym = TYPE(ch);
1336 if (sym == vfpdef || sym == tfpdef) {
1337 /* validate (vfpdef ['=' test] ',')+ */
1338 res = validate_vfpdef(ch);
Fred Drakecff283c2000-08-21 22:24:43 +00001339 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001340 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1341 res = (validate_equal(CHILD(tree, i))
1342 && validate_test(CHILD(tree, i+1)));
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001343 if (res)
1344 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001345 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001346 if (res && i < nch) {
1347 res = validate_comma(CHILD(tree, i));
1348 ++i;
Fred Drakecff283c2000-08-21 22:24:43 +00001349 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001350 } else if (sym == DOUBLESTAR || sym == STAR) {
1351 res = validate_varargslist_trailer(tree, i);
1352 break;
1353 } else {
1354 res = 0;
1355 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001356 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001357 }
Fred Drakecff283c2000-08-21 22:24:43 +00001358 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001359}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001360
1361
Nick Coghlan650f0d02007-04-15 12:05:43 +00001362/* comp_iter: comp_for | comp_if
Fred Drakecff283c2000-08-21 22:24:43 +00001363 */
1364static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001365validate_comp_iter(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001366{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001367 int res = (validate_ntype(tree, comp_iter)
1368 && validate_numnodes(tree, 1, "comp_iter"));
1369 if (res && TYPE(CHILD(tree, 0)) == comp_for)
1370 res = validate_comp_for(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001371 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001372 res = validate_comp_if(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001373
1374 return res;
1375}
1376
Nick Coghlan650f0d02007-04-15 12:05:43 +00001377/* comp_for: 'for' exprlist 'in' test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001378 */
1379static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001380validate_comp_for(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001381{
1382 int nch = NCH(tree);
1383 int res;
1384
1385 if (nch == 5)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001386 res = validate_comp_iter(CHILD(tree, 4));
Fred Drakecff283c2000-08-21 22:24:43 +00001387 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001388 res = validate_numnodes(tree, 4, "comp_for");
Raymond Hettinger354433a2004-05-19 08:20:33 +00001389
1390 if (res)
1391 res = (validate_name(CHILD(tree, 0), "for")
1392 && validate_exprlist(CHILD(tree, 1))
1393 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001394 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001395
1396 return res;
1397}
1398
Nick Coghlan650f0d02007-04-15 12:05:43 +00001399/* comp_if: 'if' test_nocond [comp_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001400 */
1401static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001402validate_comp_if(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001403{
1404 int nch = NCH(tree);
1405 int res;
1406
1407 if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001408 res = validate_comp_iter(CHILD(tree, 2));
Fred Drakecff283c2000-08-21 22:24:43 +00001409 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001410 res = validate_numnodes(tree, 2, "comp_if");
Fred Drakecff283c2000-08-21 22:24:43 +00001411
1412 if (res)
1413 res = (validate_name(CHILD(tree, 0), "if")
Nick Coghlan650f0d02007-04-15 12:05:43 +00001414 && validate_test_nocond(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001415
1416 return res;
1417}
1418
1419
Guido van Rossum3d602e31996-07-21 02:33:56 +00001420/* simple_stmt | compound_stmt
1421 *
1422 */
Guido van Rossum47478871996-08-21 14:32:37 +00001423static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001424validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001425{
1426 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001427 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001428
Guido van Rossum3d602e31996-07-21 02:33:56 +00001429 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001430 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001431
Fred Drakeff9ea482000-04-19 13:54:15 +00001432 if (TYPE(tree) == simple_stmt)
1433 res = validate_simple_stmt(tree);
1434 else
1435 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001436 }
1437 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001438}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001439
1440
Guido van Rossum3d602e31996-07-21 02:33:56 +00001441/* small_stmt (';' small_stmt)* [';'] NEWLINE
1442 *
1443 */
Guido van Rossum47478871996-08-21 14:32:37 +00001444static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001445validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001446{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001447 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001448 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001449 && (nch >= 2)
1450 && validate_small_stmt(CHILD(tree, 0))
1451 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001452
Guido van Rossum3d602e31996-07-21 02:33:56 +00001453 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001454 res = validate_numnodes(tree, 2, "simple_stmt");
1455 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001456 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001457 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001458 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001459 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001460
Fred Drakeff9ea482000-04-19 13:54:15 +00001461 for (i = 1; res && (i < nch); i += 2)
1462 res = (validate_semi(CHILD(tree, i))
1463 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001464 }
1465 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001466}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001467
1468
Guido van Rossum47478871996-08-21 14:32:37 +00001469static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001470validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001471{
1472 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001473 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001474
Fred Drake0ac9b072000-09-12 21:58:06 +00001475 if (res) {
1476 int ntype = TYPE(CHILD(tree, 0));
1477
1478 if ( (ntype == expr_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001479 || (ntype == del_stmt)
1480 || (ntype == pass_stmt)
1481 || (ntype == flow_stmt)
1482 || (ntype == import_stmt)
1483 || (ntype == global_stmt)
Mark Dickinson407b3bd2012-04-29 22:18:31 +01001484 || (ntype == nonlocal_stmt)
Georg Brandl7cae87c2006-09-06 06:51:57 +00001485 || (ntype == assert_stmt))
Fred Drake0ac9b072000-09-12 21:58:06 +00001486 res = validate_node(CHILD(tree, 0));
1487 else {
1488 res = 0;
1489 err_string("illegal small_stmt child type");
1490 }
1491 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001492 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001493 res = 0;
1494 PyErr_Format(parser_error,
1495 "Unrecognized child node of small_stmt: %d.",
1496 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001497 }
1498 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001499}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001500
1501
1502/* compound_stmt:
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00001503 * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
Guido van Rossum3d602e31996-07-21 02:33:56 +00001504 */
Guido van Rossum47478871996-08-21 14:32:37 +00001505static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001506validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001507{
1508 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001509 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001510 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001511
1512 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001513 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001514
1515 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001516 ntype = TYPE(tree);
1517 if ( (ntype == if_stmt)
1518 || (ntype == while_stmt)
1519 || (ntype == for_stmt)
1520 || (ntype == try_stmt)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00001521 || (ntype == with_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001522 || (ntype == funcdef)
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001523 || (ntype == classdef)
1524 || (ntype == decorated))
Fred Drakeff9ea482000-04-19 13:54:15 +00001525 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001526 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001527 res = 0;
1528 PyErr_Format(parser_error,
1529 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001530 }
1531 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001532}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001533
Guido van Rossum47478871996-08-21 14:32:37 +00001534static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00001535validate_yield_or_testlist(node *tree, int tse)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001536{
Benjamin Peterson4905e802009-09-27 02:43:28 +00001537 if (TYPE(tree) == yield_expr) {
1538 return validate_yield_expr(tree);
1539 }
1540 else {
1541 if (tse)
1542 return validate_testlist_star_expr(tree);
1543 else
1544 return validate_testlist(tree);
1545 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001546}
1547
1548static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001549validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001550{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001551 int j;
1552 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001553 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001554 && is_odd(nch)
Benjamin Peterson4905e802009-09-27 02:43:28 +00001555 && validate_testlist_star_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001556
Fred Drake28f739a2000-08-25 22:42:40 +00001557 if (res && nch == 3
1558 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001559 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
Benjamin Peterson4905e802009-09-27 02:43:28 +00001560 && validate_yield_or_testlist(CHILD(tree, 2), 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001561
Fred Drake28f739a2000-08-25 22:42:40 +00001562 if (res) {
1563 char *s = STR(CHILD(CHILD(tree, 1), 0));
1564
1565 res = (strcmp(s, "+=") == 0
1566 || strcmp(s, "-=") == 0
1567 || strcmp(s, "*=") == 0
1568 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001569 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001570 || strcmp(s, "%=") == 0
1571 || strcmp(s, "&=") == 0
1572 || strcmp(s, "|=") == 0
1573 || strcmp(s, "^=") == 0
1574 || strcmp(s, "<<=") == 0
1575 || strcmp(s, ">>=") == 0
1576 || strcmp(s, "**=") == 0);
1577 if (!res)
Ezio Melotti42da6632011-03-15 05:18:48 +02001578 err_string("illegal augmented assignment operator");
Fred Drake28f739a2000-08-25 22:42:40 +00001579 }
1580 }
1581 else {
1582 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001583 res = validate_equal(CHILD(tree, j))
Benjamin Peterson4905e802009-09-27 02:43:28 +00001584 && validate_yield_or_testlist(CHILD(tree, j + 1), 1);
Fred Drake28f739a2000-08-25 22:42:40 +00001585 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001586 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001587}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001588
1589
Guido van Rossum47478871996-08-21 14:32:37 +00001590static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001591validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001592{
1593 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001594 && validate_name(CHILD(tree, 0), "del")
1595 && validate_exprlist(CHILD(tree, 1)));
1596}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001597
1598
Guido van Rossum47478871996-08-21 14:32:37 +00001599static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001600validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001601{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001602 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001603 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001604 && ((nch == 1) || (nch == 2))
1605 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001606
Guido van Rossum3d602e31996-07-21 02:33:56 +00001607 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001608 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001609
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001610 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001611}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001612
1613
Mark Dickinsoncf360b92012-05-07 12:01:27 +01001614/*
1615 * raise_stmt:
1616 *
1617 * 'raise' [test ['from' test]]
1618 */
Guido van Rossum47478871996-08-21 14:32:37 +00001619static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001620validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001621{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001622 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001623 int res = (validate_ntype(tree, raise_stmt)
Mark Dickinsoncf360b92012-05-07 12:01:27 +01001624 && ((nch == 1) || (nch == 2) || (nch == 4)));
1625
1626 if (!res && !PyErr_Occurred())
1627 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001628
Guido van Rossum3d602e31996-07-21 02:33:56 +00001629 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001630 res = validate_name(CHILD(tree, 0), "raise");
1631 if (res && (nch >= 2))
1632 res = validate_test(CHILD(tree, 1));
Mark Dickinsoncf360b92012-05-07 12:01:27 +01001633 if (res && (nch == 4)) {
1634 res = (validate_name(CHILD(tree, 2), "from")
Fred Drakeff9ea482000-04-19 13:54:15 +00001635 && validate_test(CHILD(tree, 3)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001636 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001637 }
1638 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001639}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001640
1641
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001642/* yield_expr: 'yield' [yield_arg]
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001643 */
1644static int
1645validate_yield_expr(node *tree)
1646{
1647 int nch = NCH(tree);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001648 if (nch < 1 || nch > 2)
1649 return 0;
1650 if (!validate_ntype(tree, yield_expr))
1651 return 0;
1652 if (!validate_name(CHILD(tree, 0), "yield"))
1653 return 0;
1654 if (nch == 2) {
1655 if (!validate_yield_arg(CHILD(tree, 1)))
1656 return 0;
1657 }
1658 return 1;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001659}
1660
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001661/* yield_arg: 'from' test | testlist
1662 */
1663static int
1664validate_yield_arg(node *tree)
1665{
1666 int nch = NCH(tree);
1667 if (!validate_ntype(tree, yield_arg))
1668 return 0;
1669 switch (nch) {
1670 case 1:
1671 if (!validate_testlist(CHILD(tree, nch - 1)))
1672 return 0;
1673 break;
1674 case 2:
1675 if (!validate_name(CHILD(tree, 0), "from"))
1676 return 0;
1677 if (!validate_test(CHILD(tree, 1)))
1678 return 0;
1679 break;
1680 default:
1681 return 0;
1682 }
1683 return 1;
1684}
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001685
1686/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001687 */
1688static int
1689validate_yield_stmt(node *tree)
1690{
1691 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001692 && validate_numnodes(tree, 1, "yield_stmt")
1693 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001694}
1695
1696
Fred Drakecff283c2000-08-21 22:24:43 +00001697static int
1698validate_import_as_name(node *tree)
1699{
1700 int nch = NCH(tree);
1701 int ok = validate_ntype(tree, import_as_name);
1702
1703 if (ok) {
1704 if (nch == 1)
1705 ok = validate_name(CHILD(tree, 0), NULL);
1706 else if (nch == 3)
1707 ok = (validate_name(CHILD(tree, 0), NULL)
1708 && validate_name(CHILD(tree, 1), "as")
1709 && validate_name(CHILD(tree, 2), NULL));
1710 else
1711 ok = validate_numnodes(tree, 3, "import_as_name");
1712 }
1713 return ok;
1714}
1715
1716
Fred Drake71137082001-01-07 05:59:59 +00001717/* dotted_name: NAME ("." NAME)*
1718 */
1719static int
1720validate_dotted_name(node *tree)
1721{
1722 int nch = NCH(tree);
1723 int res = (validate_ntype(tree, dotted_name)
1724 && is_odd(nch)
1725 && validate_name(CHILD(tree, 0), NULL));
1726 int i;
1727
1728 for (i = 1; res && (i < nch); i += 2) {
1729 res = (validate_dot(CHILD(tree, i))
1730 && validate_name(CHILD(tree, i+1), NULL));
1731 }
1732 return res;
1733}
1734
1735
Fred Drakecff283c2000-08-21 22:24:43 +00001736/* dotted_as_name: dotted_name [NAME NAME]
1737 */
1738static int
1739validate_dotted_as_name(node *tree)
1740{
1741 int nch = NCH(tree);
1742 int res = validate_ntype(tree, dotted_as_name);
1743
1744 if (res) {
1745 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001746 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001747 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001748 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001749 && validate_name(CHILD(tree, 1), "as")
1750 && validate_name(CHILD(tree, 2), NULL));
1751 else {
1752 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001753 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001754 }
1755 }
1756 return res;
1757}
1758
1759
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001760/* dotted_as_name (',' dotted_as_name)* */
1761static int
1762validate_dotted_as_names(node *tree)
1763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 int nch = NCH(tree);
1765 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1766 int i;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 for (i = 1; res && (i < nch); i += 2)
1769 res = (validate_comma(CHILD(tree, i))
1770 && validate_dotted_as_name(CHILD(tree, i + 1)));
1771 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001772}
1773
1774
1775/* import_as_name (',' import_as_name)* [','] */
1776static int
1777validate_import_as_names(node *tree)
1778{
1779 int nch = NCH(tree);
1780 int res = validate_import_as_name(CHILD(tree, 0));
1781 int i;
1782
1783 for (i = 1; res && (i + 1 < nch); i += 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 res = (validate_comma(CHILD(tree, i))
1785 && validate_import_as_name(CHILD(tree, i + 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001786 return (res);
1787}
1788
1789
1790/* 'import' dotted_as_names */
1791static int
1792validate_import_name(node *tree)
1793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 return (validate_ntype(tree, import_name)
1795 && validate_numnodes(tree, 2, "import_name")
1796 && validate_name(CHILD(tree, 0), "import")
1797 && validate_dotted_as_names(CHILD(tree, 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001798}
1799
Mark Dickinsonfeb3b752010-07-04 18:38:57 +00001800/* Helper function to count the number of leading dots (or ellipsis tokens) in
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001801 * 'from ...module import name'
1802 */
1803static int
1804count_from_dots(node *tree)
1805{
Mark Dickinsonfeb3b752010-07-04 18:38:57 +00001806 int i;
1807 for (i = 1; i < NCH(tree); i++)
1808 if (TYPE(CHILD(tree, i)) != DOT && TYPE(CHILD(tree, i)) != ELLIPSIS)
1809 break;
1810 return i - 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001811}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001812
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +00001813/* import_from: ('from' ('.'* dotted_name | '.'+)
1814 * 'import' ('*' | '(' import_as_names ')' | import_as_names))
Guido van Rossum3d602e31996-07-21 02:33:56 +00001815 */
Guido van Rossum47478871996-08-21 14:32:37 +00001816static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001817validate_import_from(node *tree)
1818{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 int nch = NCH(tree);
1820 int ndots = count_from_dots(tree);
1821 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1822 int offset = ndots + havename;
1823 int res = validate_ntype(tree, import_from)
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +00001824 && (offset >= 1)
1825 && (nch >= 3 + offset)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 && validate_name(CHILD(tree, 0), "from")
1827 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1828 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1831 res = ((nch == offset + 5)
1832 && validate_lparen(CHILD(tree, offset + 2))
1833 && validate_import_as_names(CHILD(tree, offset + 3))
1834 && validate_rparen(CHILD(tree, offset + 4)));
1835 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1836 res = validate_import_as_names(CHILD(tree, offset + 2));
1837 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001838}
1839
1840
1841/* import_stmt: import_name | import_from */
1842static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001843validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001844{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001845 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001846 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001847
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001848 if (res) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 if (ntype == import_name || ntype == import_from)
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001852 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001853 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001854 res = 0;
1855 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001856 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001857 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001858 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001859 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001860 PyErr_Format(parser_error,
1861 "Unrecognized child node of import_stmt: %d.",
1862 TYPE(CHILD(tree, 0)));
1863 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001864 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001865}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001866
1867
Mark Dickinson407b3bd2012-04-29 22:18:31 +01001868/* global_stmt:
1869 *
1870 * 'global' NAME (',' NAME)*
1871 */
Guido van Rossum47478871996-08-21 14:32:37 +00001872static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001873validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001874{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001875 int j;
1876 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001877 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001878 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001879
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001880 if (!res && !PyErr_Occurred())
1881 err_string("illegal global statement");
1882
Guido van Rossum3d602e31996-07-21 02:33:56 +00001883 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001884 res = (validate_name(CHILD(tree, 0), "global")
1885 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001886 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001887 res = (validate_comma(CHILD(tree, j))
1888 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001889
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001890 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001891}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001892
Mark Dickinson407b3bd2012-04-29 22:18:31 +01001893/* nonlocal_stmt:
1894 *
1895 * 'nonlocal' NAME (',' NAME)*
1896 */
1897static int
1898validate_nonlocal_stmt(node *tree)
1899{
1900 int j;
1901 int nch = NCH(tree);
1902 int res = (validate_ntype(tree, nonlocal_stmt)
1903 && is_even(nch) && (nch >= 2));
1904
1905 if (!res && !PyErr_Occurred())
1906 err_string("illegal nonlocal statement");
1907
1908 if (res)
1909 res = (validate_name(CHILD(tree, 0), "nonlocal")
1910 && validate_ntype(CHILD(tree, 1), NAME));
1911 for (j = 2; res && (j < nch); j += 2)
1912 res = (validate_comma(CHILD(tree, j))
1913 && validate_ntype(CHILD(tree, j + 1), NAME));
1914
1915 return res;
1916}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001917
Guido van Rossum925e5471997-04-02 05:32:13 +00001918/* assert_stmt:
1919 *
1920 * 'assert' test [',' test]
1921 */
1922static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001923validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001924{
1925 int nch = NCH(tree);
1926 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001927 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001928 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001929 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001930
1931 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001932 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001933 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001934 res = (validate_comma(CHILD(tree, 2))
1935 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001936
1937 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001938}
Guido van Rossum925e5471997-04-02 05:32:13 +00001939
1940
Guido van Rossum47478871996-08-21 14:32:37 +00001941static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001942validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001943{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001944 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001945 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001946 && ((nch == 4) || (nch == 7))
1947 && validate_name(CHILD(tree, 0), "while")
1948 && validate_test(CHILD(tree, 1))
1949 && validate_colon(CHILD(tree, 2))
1950 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001951
Guido van Rossum3d602e31996-07-21 02:33:56 +00001952 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001953 res = (validate_name(CHILD(tree, 4), "else")
1954 && validate_colon(CHILD(tree, 5))
1955 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001956
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001957 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001958}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001959
1960
Guido van Rossum47478871996-08-21 14:32:37 +00001961static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001962validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001963{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001964 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001965 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001966 && ((nch == 6) || (nch == 9))
1967 && validate_name(CHILD(tree, 0), "for")
1968 && validate_exprlist(CHILD(tree, 1))
1969 && validate_name(CHILD(tree, 2), "in")
1970 && validate_testlist(CHILD(tree, 3))
1971 && validate_colon(CHILD(tree, 4))
1972 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001973
Guido van Rossum3d602e31996-07-21 02:33:56 +00001974 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001975 res = (validate_name(CHILD(tree, 6), "else")
1976 && validate_colon(CHILD(tree, 7))
1977 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001978
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001979 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001980}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001981
1982
Guido van Rossum3d602e31996-07-21 02:33:56 +00001983/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001984 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Georg Brandleee31162008-12-07 15:15:22 +00001985 ['finally' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001986 * | 'try' ':' suite 'finally' ':' suite
1987 *
1988 */
Guido van Rossum47478871996-08-21 14:32:37 +00001989static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001990validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001991{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001992 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001993 int pos = 3;
1994 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001995 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001996
1997 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001998 res = (validate_name(CHILD(tree, 0), "try")
1999 && validate_colon(CHILD(tree, 1))
2000 && validate_suite(CHILD(tree, 2))
2001 && validate_colon(CHILD(tree, nch - 2))
2002 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00002003 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00002004 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00002005 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
2006 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00002007
2008 PyErr_Format(parser_error,
2009 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002010 }
Georg Brandleee31162008-12-07 15:15:22 +00002011 /* Handle try/finally statement */
2012 if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
2013 (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
2014 res = (validate_numnodes(tree, 6, "try/finally")
2015 && validate_colon(CHILD(tree, 4))
2016 && validate_suite(CHILD(tree, 5)));
2017 return (res);
2018 }
2019 /* try/except statement: skip past except_clause sections */
Antoine Pitrou37d1c182009-05-14 21:54:00 +00002020 while (res && pos < nch && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002021 res = (validate_except_clause(CHILD(tree, pos))
2022 && validate_colon(CHILD(tree, pos + 1))
2023 && validate_suite(CHILD(tree, pos + 2)));
2024 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002025 }
Georg Brandleee31162008-12-07 15:15:22 +00002026 /* skip else clause */
Antoine Pitrou37d1c182009-05-14 21:54:00 +00002027 if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) &&
Georg Brandleee31162008-12-07 15:15:22 +00002028 (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
2029 res = (validate_colon(CHILD(tree, pos + 1))
2030 && validate_suite(CHILD(tree, pos + 2)));
2031 pos += 3;
2032 }
2033 if (res && pos < nch) {
2034 /* last clause must be a finally */
2035 res = (validate_name(CHILD(tree, pos), "finally")
2036 && validate_numnodes(tree, pos + 3, "try/except/finally")
2037 && validate_colon(CHILD(tree, pos + 1))
2038 && validate_suite(CHILD(tree, pos + 2)));
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_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002046{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002047 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002048 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00002049 && ((nch == 1) || (nch == 2) || (nch == 4))
2050 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002051
Guido van Rossum3d602e31996-07-21 02:33:56 +00002052 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002053 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002054 if (res && (nch == 4))
Guido van Rossumb940e112007-01-10 16:19:56 +00002055 res = (validate_name(CHILD(tree, 2), "as")
2056 && validate_ntype(CHILD(tree, 3), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002057
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002058 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002059}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002060
2061
Guido van Rossum47478871996-08-21 14:32:37 +00002062static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002063validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002064{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002065 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002066 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002067
Guido van Rossum3d602e31996-07-21 02:33:56 +00002068 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002069 res = ((nch == 1)
2070 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002071 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002072 res = validate_or_test(CHILD(tree, 0));
2073 res = (res && (nch == 1 || (nch == 5 &&
2074 validate_name(CHILD(tree, 1), "if") &&
2075 validate_or_test(CHILD(tree, 2)) &&
2076 validate_name(CHILD(tree, 3), "else") &&
2077 validate_test(CHILD(tree, 4)))));
2078 }
2079 return (res);
2080}
2081
2082static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002083validate_test_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002084{
2085 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002086 int res = validate_ntype(tree, test_nocond) && (nch == 1);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002087
Nick Coghlan650f0d02007-04-15 12:05:43 +00002088 if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
2089 res = (validate_lambdef_nocond(CHILD(tree, 0)));
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002090 else if (res) {
2091 res = (validate_or_test(CHILD(tree, 0)));
2092 }
2093 return (res);
2094}
2095
2096static int
2097validate_or_test(node *tree)
2098{
2099 int nch = NCH(tree);
2100 int res = validate_ntype(tree, or_test) && is_odd(nch);
2101
2102 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002103 int pos;
2104 res = validate_and_test(CHILD(tree, 0));
2105 for (pos = 1; res && (pos < nch); pos += 2)
2106 res = (validate_name(CHILD(tree, pos), "or")
2107 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002108 }
2109 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002110}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002111
2112
Guido van Rossum47478871996-08-21 14:32:37 +00002113static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002114validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002115{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002116 int pos;
2117 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002118 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002119 && is_odd(nch)
2120 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002121
Guido van Rossum3d602e31996-07-21 02:33:56 +00002122 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002123 res = (validate_name(CHILD(tree, pos), "and")
2124 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002125
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002126 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002127}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002128
2129
Guido van Rossum47478871996-08-21 14:32:37 +00002130static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002131validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002132{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002133 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002134 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002135
Guido van Rossum3d602e31996-07-21 02:33:56 +00002136 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002137 if (nch == 2)
2138 res = (validate_name(CHILD(tree, 0), "not")
2139 && validate_not_test(CHILD(tree, 1)));
2140 else if (nch == 1)
2141 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002142 }
2143 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002144}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002145
2146
Guido van Rossum47478871996-08-21 14:32:37 +00002147static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002148validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002149{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002150 int pos;
2151 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002152 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002153 && is_odd(nch)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002154 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002155
Guido van Rossum3d602e31996-07-21 02:33:56 +00002156 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002157 res = (validate_comp_op(CHILD(tree, pos))
Benjamin Peterson4905e802009-09-27 02:43:28 +00002158 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002159
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002160 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002161}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002162
2163
Guido van Rossum47478871996-08-21 14:32:37 +00002164static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002165validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002166{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002167 int res = 0;
2168 int nch = NCH(tree);
2169
Guido van Rossum3d602e31996-07-21 02:33:56 +00002170 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002171 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002172 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002173 /*
2174 * Only child will be a terminal with a well-defined symbolic name
2175 * or a NAME with a string of either 'is' or 'in'
2176 */
2177 tree = CHILD(tree, 0);
2178 switch (TYPE(tree)) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002179 case LESS:
2180 case GREATER:
2181 case EQEQUAL:
2182 case EQUAL:
2183 case LESSEQUAL:
2184 case GREATEREQUAL:
2185 case NOTEQUAL:
Fred Drakeff9ea482000-04-19 13:54:15 +00002186 res = 1;
2187 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002188 case NAME:
Fred Drakeff9ea482000-04-19 13:54:15 +00002189 res = ((strcmp(STR(tree), "in") == 0)
2190 || (strcmp(STR(tree), "is") == 0));
2191 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002192 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002193 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002194 }
2195 break;
2196 default:
Fred Drake661ea262000-10-24 19:57:45 +00002197 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002198 break;
2199 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002200 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002201 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002202 res = (validate_ntype(CHILD(tree, 0), NAME)
2203 && validate_ntype(CHILD(tree, 1), NAME)
2204 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2205 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2206 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2207 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2208 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002209 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002210 }
2211 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002212}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002213
2214
Guido van Rossum47478871996-08-21 14:32:37 +00002215static int
Guido van Rossum0368b722007-05-11 16:50:42 +00002216validate_star_expr(node *tree)
2217{
2218 int res = validate_ntype(tree, star_expr);
2219 if (!res) return res;
Benjamin Peterson4905e802009-09-27 02:43:28 +00002220 if (!validate_numnodes(tree, 2, "star_expr"))
2221 return 0;
2222 return validate_ntype(CHILD(tree, 0), STAR) && \
2223 validate_expr(CHILD(tree, 1));
Guido van Rossum0368b722007-05-11 16:50:42 +00002224}
2225
2226
2227static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002228validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002229{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002230 int j;
2231 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002232 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002233 && is_odd(nch)
2234 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002235
Guido van Rossum3d602e31996-07-21 02:33:56 +00002236 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002237 res = (validate_xor_expr(CHILD(tree, j))
2238 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002239
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002240 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002241}
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_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002246{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002247 int j;
2248 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002249 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002250 && is_odd(nch)
2251 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002252
Guido van Rossum3d602e31996-07-21 02:33:56 +00002253 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002254 res = (validate_circumflex(CHILD(tree, j - 1))
2255 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002256
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002257 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002258}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002259
2260
Guido van Rossum47478871996-08-21 14:32:37 +00002261static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002262validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002263{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002264 int pos;
2265 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002266 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002267 && is_odd(nch)
2268 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002269
Guido van Rossum3d602e31996-07-21 02:33:56 +00002270 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002271 res = (validate_ampersand(CHILD(tree, pos))
2272 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002273
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002274 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002275}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002276
2277
2278static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002279validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002280 {
2281 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002282 int nch = NCH(tree);
2283 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002284 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002285
Guido van Rossum3d602e31996-07-21 02:33:56 +00002286 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002287 if (TYPE(CHILD(tree, pos)) != op1)
2288 res = validate_ntype(CHILD(tree, pos), op2);
2289 if (res)
2290 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002291 }
2292 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002293}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002294
2295
Guido van Rossum47478871996-08-21 14:32:37 +00002296static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002297validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002298{
2299 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002300 && validate_chain_two_ops(tree, validate_arith_expr,
2301 LEFTSHIFT, RIGHTSHIFT));
2302}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002303
2304
Guido van Rossum47478871996-08-21 14:32:37 +00002305static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002306validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002307{
2308 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002309 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2310}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002311
2312
Guido van Rossum47478871996-08-21 14:32:37 +00002313static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002314validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002315{
2316 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002317 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002318 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002319 && is_odd(nch)
2320 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002321
Guido van Rossum3d602e31996-07-21 02:33:56 +00002322 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002323 res = (((TYPE(CHILD(tree, pos)) == STAR)
2324 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002325 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002326 || (TYPE(CHILD(tree, pos)) == PERCENT))
2327 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002328
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002329 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002330}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002331
2332
Guido van Rossum3d602e31996-07-21 02:33:56 +00002333/* factor:
2334 *
2335 * factor: ('+'|'-'|'~') factor | power
2336 */
Guido van Rossum47478871996-08-21 14:32:37 +00002337static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002338validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002339{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002340 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002341 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002342 && (((nch == 2)
2343 && ((TYPE(CHILD(tree, 0)) == PLUS)
2344 || (TYPE(CHILD(tree, 0)) == MINUS)
2345 || (TYPE(CHILD(tree, 0)) == TILDE))
2346 && validate_factor(CHILD(tree, 1)))
2347 || ((nch == 1)
2348 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002349 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002350}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002351
2352
Guido van Rossum3d602e31996-07-21 02:33:56 +00002353/* power:
2354 *
2355 * power: atom trailer* ('**' factor)*
2356 */
Guido van Rossum47478871996-08-21 14:32:37 +00002357static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002358validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002359{
2360 int pos = 1;
2361 int nch = NCH(tree);
2362 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002363 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002364
2365 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002366 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002367 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002368 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002369 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002370 return (0);
2371 }
2372 for ( ; res && (pos < (nch - 1)); pos += 2)
2373 res = (validate_doublestar(CHILD(tree, pos))
2374 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002375 }
2376 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002377}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002378
2379
Guido van Rossum47478871996-08-21 14:32:37 +00002380static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002381validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002382{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002383 int pos;
2384 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002385 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002386
Fred Drakecff283c2000-08-21 22:24:43 +00002387 if (res && nch < 1)
2388 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002389 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002390 switch (TYPE(CHILD(tree, 0))) {
2391 case LPAR:
2392 res = ((nch <= 3)
2393 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002394
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002395 if (res && (nch == 3)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 if (TYPE(CHILD(tree, 1))==yield_expr)
2397 res = validate_yield_expr(CHILD(tree, 1));
2398 else
2399 res = validate_testlist_comp(CHILD(tree, 1));
2400 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002401 break;
2402 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002403 if (nch == 2)
2404 res = validate_ntype(CHILD(tree, 1), RSQB);
2405 else if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002406 res = (validate_testlist_comp(CHILD(tree, 1))
Fred Drakecff283c2000-08-21 22:24:43 +00002407 && validate_ntype(CHILD(tree, 2), RSQB));
2408 else {
2409 res = 0;
2410 err_string("illegal list display atom");
2411 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002412 break;
2413 case LBRACE:
2414 res = ((nch <= 3)
2415 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002416
Fred Drakeff9ea482000-04-19 13:54:15 +00002417 if (res && (nch == 3))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002418 res = validate_dictorsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002419 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002420 case NAME:
2421 case NUMBER:
Mark Dickinsonda029fb2012-05-07 17:24:04 +01002422 case ELLIPSIS:
Fred Drakeff9ea482000-04-19 13:54:15 +00002423 res = (nch == 1);
2424 break;
2425 case STRING:
2426 for (pos = 1; res && (pos < nch); ++pos)
2427 res = validate_ntype(CHILD(tree, pos), STRING);
2428 break;
2429 default:
2430 res = 0;
2431 break;
2432 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002433 }
2434 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002435}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002436
2437
Nick Coghlan650f0d02007-04-15 12:05:43 +00002438/* testlist_comp:
2439 * test ( comp_for | (',' test)* [','] )
Fred Drake85bf3bb2000-08-23 15:35:26 +00002440 */
Fred Drakecff283c2000-08-21 22:24:43 +00002441static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002442validate_testlist_comp(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00002443{
2444 int nch = NCH(tree);
2445 int ok = nch;
2446
2447 if (nch == 0)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002448 err_string("missing child nodes of testlist_comp");
2449 else {
Benjamin Peterson4905e802009-09-27 02:43:28 +00002450 ok = validate_test_or_star_expr(CHILD(tree, 0));
Nick Coghlan650f0d02007-04-15 12:05:43 +00002451 }
Fred Drakecff283c2000-08-21 22:24:43 +00002452
2453 /*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002454 * comp_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002455 */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002456 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2457 ok = validate_comp_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002458 else {
2459 /* (',' test)* [','] */
2460 int i = 1;
2461 while (ok && nch - i >= 2) {
2462 ok = (validate_comma(CHILD(tree, i))
Benjamin Peterson4905e802009-09-27 02:43:28 +00002463 && validate_test_or_star_expr(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002464 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002465 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002466 if (ok && i == nch-1)
2467 ok = validate_comma(CHILD(tree, i));
2468 else if (i != nch) {
2469 ok = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002470 err_string("illegal trailing nodes for testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002471 }
2472 }
2473 return ok;
2474}
Fred Drakecff283c2000-08-21 22:24:43 +00002475
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002476/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002477 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002478 */
2479static int
2480validate_decorator(node *tree)
2481{
2482 int ok;
2483 int nch = NCH(tree);
2484 ok = (validate_ntype(tree, decorator) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 (nch == 3 || nch == 5 || nch == 6) &&
2486 validate_at(CHILD(tree, 0)) &&
2487 validate_dotted_name(CHILD(tree, 1)) &&
2488 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002489
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002490 if (ok && nch != 3) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 ok = (validate_lparen(CHILD(tree, 2)) &&
2492 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 if (ok && nch == 6)
2495 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002496 }
2497
2498 return ok;
2499}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002500
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002501/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002502 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002503 */
2504static int
2505validate_decorators(node *tree)
2506{
Guido van Rossumfc158e22007-11-15 19:17:28 +00002507 int i, nch, ok;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002508 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002509 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002510
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002511 for (i = 0; ok && i < nch; ++i)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002513
2514 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002515}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002516
Georg Brandl0c315622009-05-25 21:10:36 +00002517/* with_item:
2518 * test ['as' expr]
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002519 */
2520static int
Georg Brandl0c315622009-05-25 21:10:36 +00002521validate_with_item(node *tree)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002522{
2523 int nch = NCH(tree);
Georg Brandl0c315622009-05-25 21:10:36 +00002524 int ok = (validate_ntype(tree, with_item)
2525 && (nch == 1 || nch == 3)
2526 && validate_test(CHILD(tree, 0)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 if (ok && nch == 3)
Georg Brandl0c315622009-05-25 21:10:36 +00002528 ok = (validate_name(CHILD(tree, 1), "as")
2529 && validate_expr(CHILD(tree, 2)));
2530 return ok;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002531}
2532
Georg Brandl0c315622009-05-25 21:10:36 +00002533/* with_stmt:
2534 * 0 1 ... -2 -1
2535 * 'with' with_item (',' with_item)* ':' suite
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002536 */
2537static int
2538validate_with_stmt(node *tree)
2539{
Georg Brandl0c315622009-05-25 21:10:36 +00002540 int i;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002541 int nch = NCH(tree);
2542 int ok = (validate_ntype(tree, with_stmt)
Georg Brandl0c315622009-05-25 21:10:36 +00002543 && (nch % 2 == 0)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002544 && validate_name(CHILD(tree, 0), "with")
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002545 && validate_colon(RCHILD(tree, -2))
2546 && validate_suite(RCHILD(tree, -1)));
Georg Brandl0c315622009-05-25 21:10:36 +00002547 for (i = 1; ok && i < nch - 2; i += 2)
2548 ok = validate_with_item(CHILD(tree, i));
2549 return ok;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002550}
2551
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01002552/* funcdef: 'def' NAME parameters ['->' test] ':' suite */
2553
Guido van Rossum47478871996-08-21 14:32:37 +00002554static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002555validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002556{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002557 int nch = NCH(tree);
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01002558 int res = validate_ntype(tree, funcdef);
2559 if (res) {
2560 if (nch == 5) {
2561 res = (validate_name(CHILD(tree, 0), "def")
2562 && validate_ntype(CHILD(tree, 1), NAME)
2563 && validate_parameters(CHILD(tree, 2))
2564 && validate_colon(CHILD(tree, 3))
2565 && validate_suite(CHILD(tree, 4)));
2566 }
2567 else if (nch == 7) {
2568 res = (validate_name(CHILD(tree, 0), "def")
2569 && validate_ntype(CHILD(tree, 1), NAME)
2570 && validate_parameters(CHILD(tree, 2))
2571 && validate_rarrow(CHILD(tree, 3))
2572 && validate_test(CHILD(tree, 4))
2573 && validate_colon(CHILD(tree, 5))
2574 && validate_suite(CHILD(tree, 6)));
2575 }
2576 else {
2577 res = 0;
2578 err_string("illegal number of children for funcdef");
2579 }
2580 }
2581 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00002582}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002583
2584
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002585/* decorated
2586 * decorators (classdef | funcdef)
2587 */
2588static int
2589validate_decorated(node *tree)
2590{
Mark Dickinson2bd61a92010-07-04 16:37:31 +00002591 int nch = NCH(tree);
2592 int ok = (validate_ntype(tree, decorated)
2593 && (nch == 2)
2594 && validate_decorators(RCHILD(tree, -2)));
2595 if (TYPE(RCHILD(tree, -1)) == funcdef)
2596 ok = ok && validate_funcdef(RCHILD(tree, -1));
2597 else
2598 ok = ok && validate_class(RCHILD(tree, -1));
2599 return ok;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002600}
2601
Guido van Rossum47478871996-08-21 14:32:37 +00002602static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002603validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002604{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002605 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002606 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002607 && ((nch == 3) || (nch == 4))
2608 && validate_name(CHILD(tree, 0), "lambda")
2609 && validate_colon(CHILD(tree, nch - 2))
2610 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002611
Guido van Rossum3d602e31996-07-21 02:33:56 +00002612 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002613 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002614 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002615 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002616
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002617 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002618}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002619
2620
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002621static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002622validate_lambdef_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002623{
2624 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002625 int res = (validate_ntype(tree, lambdef_nocond)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002626 && ((nch == 3) || (nch == 4))
2627 && validate_name(CHILD(tree, 0), "lambda")
2628 && validate_colon(CHILD(tree, nch - 2))
2629 && validate_test(CHILD(tree, nch - 1)));
2630
2631 if (res && (nch == 4))
2632 res = validate_varargslist(CHILD(tree, 1));
2633 else if (!res && !PyErr_Occurred())
Nick Coghlan650f0d02007-04-15 12:05:43 +00002634 (void) validate_numnodes(tree, 3, "lambdef_nocond");
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002635
2636 return (res);
2637}
2638
2639
Guido van Rossum3d602e31996-07-21 02:33:56 +00002640/* arglist:
2641 *
Fred Drakecff283c2000-08-21 22:24:43 +00002642 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002643 */
Guido van Rossum47478871996-08-21 14:32:37 +00002644static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002645validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002646{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002647 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002648 int i = 0;
2649 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002650
2651 if (nch <= 0)
2652 /* raise the right error from having an invalid number of children */
2653 return validate_numnodes(tree, nch + 1, "arglist");
2654
Raymond Hettinger354433a2004-05-19 08:20:33 +00002655 if (nch > 1) {
2656 for (i=0; i<nch; i++) {
2657 if (TYPE(CHILD(tree, i)) == argument) {
2658 node *ch = CHILD(tree, i);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002659 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
Raymond Hettinger354433a2004-05-19 08:20:33 +00002660 err_string("need '(', ')' for generator expression");
2661 return 0;
2662 }
2663 }
2664 }
2665 }
2666
Fred Drakecff283c2000-08-21 22:24:43 +00002667 while (ok && nch-i >= 2) {
2668 /* skip leading (argument ',') */
2669 ok = (validate_argument(CHILD(tree, i))
2670 && validate_comma(CHILD(tree, i+1)));
2671 if (ok)
2672 i += 2;
2673 else
2674 PyErr_Clear();
2675 }
2676 ok = 1;
2677 if (nch-i > 0) {
2678 /*
2679 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002680 */
Fred Drakecff283c2000-08-21 22:24:43 +00002681 int sym = TYPE(CHILD(tree, i));
2682
2683 if (sym == argument) {
2684 ok = validate_argument(CHILD(tree, i));
2685 if (ok && i+1 != nch) {
2686 err_string("illegal arglist specification"
2687 " (extra stuff on end)");
2688 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002689 }
Fred Drakecff283c2000-08-21 22:24:43 +00002690 }
2691 else if (sym == STAR) {
2692 ok = validate_star(CHILD(tree, i));
2693 if (ok && (nch-i == 2))
2694 ok = validate_test(CHILD(tree, i+1));
2695 else if (ok && (nch-i == 5))
2696 ok = (validate_test(CHILD(tree, i+1))
2697 && validate_comma(CHILD(tree, i+2))
2698 && validate_doublestar(CHILD(tree, i+3))
2699 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002700 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002701 err_string("illegal use of '*' in arglist");
2702 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002703 }
2704 }
Fred Drakecff283c2000-08-21 22:24:43 +00002705 else if (sym == DOUBLESTAR) {
2706 if (nch-i == 2)
2707 ok = (validate_doublestar(CHILD(tree, i))
2708 && validate_test(CHILD(tree, i+1)));
2709 else {
2710 err_string("illegal use of '**' in arglist");
2711 ok = 0;
2712 }
2713 }
2714 else {
2715 err_string("illegal arglist specification");
2716 ok = 0;
2717 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002718 }
2719 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002720}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002721
2722
2723
2724/* argument:
2725 *
Nick Coghlan650f0d02007-04-15 12:05:43 +00002726 * [test '='] test [comp_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002727 */
Guido van Rossum47478871996-08-21 14:32:37 +00002728static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002729validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002730{
2731 int nch = NCH(tree);
2732 int res = (validate_ntype(tree, argument)
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002733 && ((nch == 1) || (nch == 2) || (nch == 3)));
2734 if (res)
2735 res = validate_test(CHILD(tree, 0));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002736 if (res && (nch == 2))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002737 res = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002738 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002739 res = (validate_equal(CHILD(tree, 1))
2740 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002741
2742 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002743}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002744
2745
2746
2747/* trailer:
2748 *
Guido van Rossum47478871996-08-21 14:32:37 +00002749 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002750 */
Guido van Rossum47478871996-08-21 14:32:37 +00002751static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002752validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002753{
2754 int nch = NCH(tree);
2755 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002756
2757 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002758 switch (TYPE(CHILD(tree, 0))) {
2759 case LPAR:
2760 res = validate_rparen(CHILD(tree, nch - 1));
2761 if (res && (nch == 3))
2762 res = validate_arglist(CHILD(tree, 1));
2763 break;
2764 case LSQB:
2765 res = (validate_numnodes(tree, 3, "trailer")
2766 && validate_subscriptlist(CHILD(tree, 1))
2767 && validate_ntype(CHILD(tree, 2), RSQB));
2768 break;
2769 case DOT:
2770 res = (validate_numnodes(tree, 2, "trailer")
2771 && validate_ntype(CHILD(tree, 1), NAME));
2772 break;
2773 default:
2774 res = 0;
2775 break;
2776 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002777 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002778 else {
2779 (void) validate_numnodes(tree, 2, "trailer");
2780 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002781 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002782}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002783
2784
Guido van Rossum47478871996-08-21 14:32:37 +00002785/* subscriptlist:
2786 *
2787 * subscript (',' subscript)* [',']
2788 */
2789static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002790validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002791{
2792 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002793 validate_subscript, "subscriptlist"));
2794}
Guido van Rossum47478871996-08-21 14:32:37 +00002795
2796
Guido van Rossum3d602e31996-07-21 02:33:56 +00002797/* subscript:
2798 *
Guido van Rossum47478871996-08-21 14:32:37 +00002799 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002800 */
Guido van Rossum47478871996-08-21 14:32:37 +00002801static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002802validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002803{
Guido van Rossum47478871996-08-21 14:32:37 +00002804 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002805 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002806 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002807
Guido van Rossum47478871996-08-21 14:32:37 +00002808 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002809 if (!PyErr_Occurred())
2810 err_string("invalid number of arguments for subscript node");
2811 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002812 }
Guido van Rossum47478871996-08-21 14:32:37 +00002813 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002814 /* take care of ('.' '.' '.') possibility */
2815 return (validate_numnodes(tree, 3, "subscript")
2816 && validate_dot(CHILD(tree, 0))
2817 && validate_dot(CHILD(tree, 1))
2818 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002819 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002820 if (TYPE(CHILD(tree, 0)) == test)
2821 res = validate_test(CHILD(tree, 0));
2822 else
2823 res = validate_colon(CHILD(tree, 0));
2824 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002825 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002826 /* Must be [test] ':' [test] [sliceop],
2827 * but at least one of the optional components will
2828 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002829 */
2830 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002831 res = validate_test(CHILD(tree, 0));
2832 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002833 }
2834 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002835 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002836 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002837 int rem = nch - ++offset;
2838 if (rem) {
2839 if (TYPE(CHILD(tree, offset)) == test) {
2840 res = validate_test(CHILD(tree, offset));
2841 ++offset;
2842 --rem;
2843 }
2844 if (res && rem)
2845 res = validate_sliceop(CHILD(tree, offset));
2846 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002847 }
2848 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002849}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002850
2851
Guido van Rossum47478871996-08-21 14:32:37 +00002852static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002853validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002854{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002855 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002856 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002857 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002858 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002859 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002860 }
Guido van Rossum47478871996-08-21 14:32:37 +00002861 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002862 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002863 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002864 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002865
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002866 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002867}
Guido van Rossum47478871996-08-21 14:32:37 +00002868
2869
2870static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00002871validate_test_or_star_expr(node *n)
2872{
2873 if (TYPE(n) == test)
2874 return validate_test(n);
2875 return validate_star_expr(n);
2876}
2877
2878static int
2879validate_expr_or_star_expr(node *n)
2880{
2881 if (TYPE(n) == expr)
2882 return validate_expr(n);
2883 return validate_star_expr(n);
2884}
2885
2886
2887static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002888validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002889{
2890 return (validate_repeating_list(tree, exprlist,
Benjamin Peterson4905e802009-09-27 02:43:28 +00002891 validate_expr_or_star_expr, "exprlist"));
Fred Drakeff9ea482000-04-19 13:54:15 +00002892}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002893
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002894/*
2895 * dictorsetmaker:
2896 *
2897 * (test ':' test (comp_for | (',' test ':' test)* [','])) |
2898 * (test (comp_for | (',' test)* [',']))
2899 */
Guido van Rossum47478871996-08-21 14:32:37 +00002900static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002901validate_dictorsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002902{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002903 int nch = NCH(tree);
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002904 int res;
2905 int i = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002906
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002907 res = validate_ntype(tree, dictorsetmaker);
2908 if (!res)
2909 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002910
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002911 if (nch - i < 1) {
2912 (void) validate_numnodes(tree, 1, "dictorsetmaker");
2913 return 0;
2914 }
2915
2916 res = validate_test(CHILD(tree, i++));
2917 if (!res)
2918 return 0;
2919
2920 if (nch - i >= 2 && TYPE(CHILD(tree, i)) == COLON) {
2921 /* Dictionary display or dictionary comprehension. */
2922 res = (validate_colon(CHILD(tree, i++))
2923 && validate_test(CHILD(tree, i++)));
2924 if (!res)
2925 return 0;
2926
2927 if (nch - i >= 1 && TYPE(CHILD(tree, i)) == comp_for) {
2928 /* Dictionary comprehension. */
2929 res = validate_comp_for(CHILD(tree, i++));
2930 if (!res)
2931 return 0;
2932 }
2933 else {
2934 /* Dictionary display. */
2935 while (nch - i >= 4) {
2936 res = (validate_comma(CHILD(tree, i++))
2937 && validate_test(CHILD(tree, i++))
2938 && validate_colon(CHILD(tree, i++))
2939 && validate_test(CHILD(tree, i++)));
2940 if (!res)
2941 return 0;
2942 }
2943 if (nch - i == 1) {
2944 res = validate_comma(CHILD(tree, i++));
2945 if (!res)
2946 return 0;
2947 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002948 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002949 }
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002950 else {
2951 /* Set display or set comprehension. */
2952 if (nch - i >= 1 && TYPE(CHILD(tree, i)) == comp_for) {
2953 /* Set comprehension. */
2954 res = validate_comp_for(CHILD(tree, i++));
2955 if (!res)
2956 return 0;
2957 }
2958 else {
2959 /* Set display. */
2960 while (nch - i >= 2) {
2961 res = (validate_comma(CHILD(tree, i++))
2962 && validate_test(CHILD(tree, i++)));
2963 if (!res)
2964 return 0;
2965 }
2966 if (nch - i == 1) {
2967 res = validate_comma(CHILD(tree, i++));
2968 if (!res)
2969 return 0;
2970 }
2971 }
2972 }
2973
2974 if (nch - i > 0) {
2975 err_string("Illegal trailing nodes for dictorsetmaker.");
2976 return 0;
2977 }
2978
2979 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00002980}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002981
2982
Guido van Rossum47478871996-08-21 14:32:37 +00002983static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002984validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002985{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002986 int pos;
2987 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002988 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002989 && (nch >= 2)
2990 && validate_testlist(CHILD(tree, 0))
2991 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002992
Guido van Rossum3d602e31996-07-21 02:33:56 +00002993 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002994 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002995
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002996 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002997}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002998
2999
Guido van Rossum47478871996-08-21 14:32:37 +00003000static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003001validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003002{
Fred Drakeff9ea482000-04-19 13:54:15 +00003003 int nch = 0; /* num. children on current node */
3004 int res = 1; /* result value */
3005 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00003006
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00003007 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003008 nch = NCH(tree);
3009 next = 0;
3010 switch (TYPE(tree)) {
3011 /*
3012 * Definition nodes.
3013 */
3014 case funcdef:
3015 res = validate_funcdef(tree);
3016 break;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00003017 case with_stmt:
3018 res = validate_with_stmt(tree);
3019 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003020 case classdef:
3021 res = validate_class(tree);
3022 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003023 case decorated:
3024 res = validate_decorated(tree);
3025 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003026 /*
3027 * "Trivial" parse tree nodes.
3028 * (Why did I call these trivial?)
3029 */
3030 case stmt:
3031 res = validate_stmt(tree);
3032 break;
3033 case small_stmt:
3034 /*
Mark Dickinson407b3bd2012-04-29 22:18:31 +01003035 * expr_stmt | del_stmt | pass_stmt | flow_stmt |
3036 * import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00003037 */
3038 res = validate_small_stmt(tree);
3039 break;
3040 case flow_stmt:
3041 res = (validate_numnodes(tree, 1, "flow_stmt")
3042 && ((TYPE(CHILD(tree, 0)) == break_stmt)
3043 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00003044 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00003045 || (TYPE(CHILD(tree, 0)) == return_stmt)
3046 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
3047 if (res)
3048 next = CHILD(tree, 0);
3049 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00003050 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003051 break;
Fred Drake02126f22001-07-17 02:59:15 +00003052 case yield_stmt:
3053 res = validate_yield_stmt(tree);
3054 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003055 /*
3056 * Compound statements.
3057 */
3058 case simple_stmt:
3059 res = validate_simple_stmt(tree);
3060 break;
3061 case compound_stmt:
3062 res = validate_compound_stmt(tree);
3063 break;
3064 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00003065 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00003066 */
3067 case expr_stmt:
3068 res = validate_expr_stmt(tree);
3069 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003070 case del_stmt:
3071 res = validate_del_stmt(tree);
3072 break;
3073 case pass_stmt:
3074 res = (validate_numnodes(tree, 1, "pass")
3075 && validate_name(CHILD(tree, 0), "pass"));
3076 break;
3077 case break_stmt:
3078 res = (validate_numnodes(tree, 1, "break")
3079 && validate_name(CHILD(tree, 0), "break"));
3080 break;
3081 case continue_stmt:
3082 res = (validate_numnodes(tree, 1, "continue")
3083 && validate_name(CHILD(tree, 0), "continue"));
3084 break;
3085 case return_stmt:
3086 res = validate_return_stmt(tree);
3087 break;
3088 case raise_stmt:
3089 res = validate_raise_stmt(tree);
3090 break;
3091 case import_stmt:
3092 res = validate_import_stmt(tree);
3093 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 case import_name:
3095 res = validate_import_name(tree);
3096 break;
3097 case import_from:
3098 res = validate_import_from(tree);
3099 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003100 case global_stmt:
3101 res = validate_global_stmt(tree);
3102 break;
Mark Dickinson407b3bd2012-04-29 22:18:31 +01003103 case nonlocal_stmt:
3104 res = validate_nonlocal_stmt(tree);
3105 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003106 case assert_stmt:
3107 res = validate_assert_stmt(tree);
3108 break;
3109 case if_stmt:
3110 res = validate_if(tree);
3111 break;
3112 case while_stmt:
3113 res = validate_while(tree);
3114 break;
3115 case for_stmt:
3116 res = validate_for(tree);
3117 break;
3118 case try_stmt:
3119 res = validate_try(tree);
3120 break;
3121 case suite:
3122 res = validate_suite(tree);
3123 break;
3124 /*
3125 * Expression nodes.
3126 */
3127 case testlist:
3128 res = validate_testlist(tree);
3129 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003130 case yield_expr:
3131 res = validate_yield_expr(tree);
3132 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003133 case test:
3134 res = validate_test(tree);
3135 break;
3136 case and_test:
3137 res = validate_and_test(tree);
3138 break;
3139 case not_test:
3140 res = validate_not_test(tree);
3141 break;
3142 case comparison:
3143 res = validate_comparison(tree);
3144 break;
3145 case exprlist:
3146 res = validate_exprlist(tree);
3147 break;
3148 case comp_op:
3149 res = validate_comp_op(tree);
3150 break;
3151 case expr:
3152 res = validate_expr(tree);
3153 break;
3154 case xor_expr:
3155 res = validate_xor_expr(tree);
3156 break;
3157 case and_expr:
3158 res = validate_and_expr(tree);
3159 break;
3160 case shift_expr:
3161 res = validate_shift_expr(tree);
3162 break;
3163 case arith_expr:
3164 res = validate_arith_expr(tree);
3165 break;
3166 case term:
3167 res = validate_term(tree);
3168 break;
3169 case factor:
3170 res = validate_factor(tree);
3171 break;
3172 case power:
3173 res = validate_power(tree);
3174 break;
3175 case atom:
3176 res = validate_atom(tree);
3177 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003178
Fred Drakeff9ea482000-04-19 13:54:15 +00003179 default:
3180 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00003181 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003182 res = 0;
3183 break;
3184 }
3185 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003186 }
3187 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003188}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003189
3190
Guido van Rossum47478871996-08-21 14:32:37 +00003191static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003192validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003193{
3194 int res = validate_eval_input(tree);
3195
3196 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003197 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003198
3199 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003200}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003201
3202
Guido van Rossum3d602e31996-07-21 02:33:56 +00003203/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003204 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003205 */
Guido van Rossum47478871996-08-21 14:32:37 +00003206static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003207validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003208{
Fred Drakec2683dd2001-07-17 19:32:05 +00003209 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003210 int nch = NCH(tree) - 1;
3211 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003212 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003213
Fred Drakec2683dd2001-07-17 19:32:05 +00003214 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003215 if (TYPE(CHILD(tree, j)) == stmt)
3216 res = validate_stmt(CHILD(tree, j));
3217 else
3218 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003219 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003220 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003221 * user. Hopefully, this won't be needed. If a user reports getting
3222 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003223 */
3224 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003225 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003226
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003227 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003228}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003229
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003230static int
3231validate_encoding_decl(node *tree)
3232{
3233 int nch = NCH(tree);
3234 int res = ((nch == 1)
3235 && validate_file_input(CHILD(tree, 0)));
3236
3237 if (!res && !PyErr_Occurred())
3238 err_string("Error Parsing encoding_decl");
3239
3240 return res;
3241}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003242
Fred Drake43f8f9b1998-04-13 16:25:46 +00003243static PyObject*
3244pickle_constructor = NULL;
3245
3246
3247static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003248parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003249{
Fred Drake268397f1998-04-29 14:16:32 +00003250 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003251 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003252 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003253 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003254
Fred Drakec2683dd2001-07-17 19:32:05 +00003255 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003256 PyObject *newargs;
3257 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003258
Fred Drake2a6875e1999-09-20 22:32:18 +00003259 if ((empty_dict = PyDict_New()) == NULL)
3260 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003261 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003262 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003263 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003264 if (tuple != NULL) {
3265 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3266 Py_DECREF(tuple);
3267 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003268 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003269 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003270 }
3271 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003272 Py_XDECREF(empty_dict);
3273
Fred Drake43f8f9b1998-04-13 16:25:46 +00003274 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003275}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003276
3277
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003278/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003279 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003280 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003281 * We'd really have to write a wrapper around it all anyway to allow
3282 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003283 */
3284static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003285 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003286 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003287 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003288 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003289 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003290 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003291 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003292 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003293 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003294 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003295 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003296 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003297 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003298 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003299 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003300 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003301 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003302 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003303
Fred Drake43f8f9b1998-04-13 16:25:46 +00003304 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003305 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003306 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003307
Fred Drake268397f1998-04-29 14:16:32 +00003308 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003309 };
3310
3311
Martin v. Löwis1a214512008-06-11 05:26:20 +00003312
3313static struct PyModuleDef parsermodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 PyModuleDef_HEAD_INIT,
3315 "parser",
3316 NULL,
3317 -1,
3318 parser_functions,
3319 NULL,
3320 NULL,
3321 NULL,
3322 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00003323};
3324
3325PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003326
Mark Hammond62b1ab12002-07-23 06:31:15 +00003327PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003328PyInit_parser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003329{
Fred Drake13130bc2001-08-15 16:44:56 +00003330 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003331
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00003332 if (PyType_Ready(&PyST_Type) < 0)
3333 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00003334 module = PyModule_Create(&parsermodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003335 if (module == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 return NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003337
Fred Drake7a15ba51999-09-09 14:21:52 +00003338 if (parser_error == 0)
3339 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003340
Tim Peters6a627252003-07-21 14:25:23 +00003341 if (parser_error == 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003342 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003343 /* CAUTION: The code next used to skip bumping the refcount on
Martin v. Löwis1a214512008-06-11 05:26:20 +00003344 * parser_error. That's a disaster if PyInit_parser() gets called more
Tim Peters6a627252003-07-21 14:25:23 +00003345 * than once. By incref'ing, we ensure that each module dict that
3346 * gets created owns its reference to the shared parser_error object,
3347 * and the file static parser_error vrbl owns a reference too.
3348 */
3349 Py_INCREF(parser_error);
3350 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003351 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003352
Fred Drakec2683dd2001-07-17 19:32:05 +00003353 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003354 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003355
Fred Drake13130bc2001-08-15 16:44:56 +00003356 PyModule_AddStringConstant(module, "__copyright__",
3357 parser_copyright_string);
3358 PyModule_AddStringConstant(module, "__doc__",
3359 parser_doc_string);
3360 PyModule_AddStringConstant(module, "__version__",
3361 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003362
Fred Drake78bdb9b2001-07-19 20:17:15 +00003363 /* Register to support pickling.
3364 * If this fails, the import of this module will fail because an
3365 * exception will be raised here; should we clear the exception?
3366 */
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003367 copyreg = PyImport_ImportModuleNoBlock("copyreg");
Fred Drake13130bc2001-08-15 16:44:56 +00003368 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003369 PyObject *func, *pickler;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003370 _Py_IDENTIFIER(pickle);
3371 _Py_IDENTIFIER(sequence2st);
3372 _Py_IDENTIFIER(_pickler);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003373
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02003374 func = _PyObject_GetAttrId(copyreg, &PyId_pickle);
3375 pickle_constructor = _PyObject_GetAttrId(module, &PyId_sequence2st);
3376 pickler = _PyObject_GetAttrId(module, &PyId__pickler);
Fred Drakeff9ea482000-04-19 13:54:15 +00003377 Py_XINCREF(pickle_constructor);
3378 if ((func != NULL) && (pickle_constructor != NULL)
3379 && (pickler != NULL)) {
3380 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003381
Thomas Wouters477c8d52006-05-27 19:21:47 +00003382 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3383 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003384 Py_XDECREF(res);
3385 }
3386 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003387 Py_XDECREF(pickle_constructor);
3388 Py_XDECREF(pickler);
3389 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003390 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00003391 return module;
Fred Drakeff9ea482000-04-19 13:54:15 +00003392}