blob: e86fe4d2eb58076a9cf944c3d72d8531852be83a [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000026 */
27
Fred Drakeff9ea482000-04-19 13:54:15 +000028#include "Python.h" /* general Python API */
Benjamin Petersonf216c942008-10-31 02:28:05 +000029#include "Python-ast.h" /* mod_ty */
Fred Drakeff9ea482000-04-19 13:54:15 +000030#include "graminit.h" /* symbols defined in the grammar */
31#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000032#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000033#include "token.h" /* token definitions */
Benjamin Petersonf216c942008-10-31 02:28:05 +000034#include "grammar.h"
35#include "parsetok.h"
Fred Drakeff9ea482000-04-19 13:54:15 +000036 /* ISTERMINAL() / ISNONTERMINAL() */
Benjamin Petersonf216c942008-10-31 02:28:05 +000037#undef Yield
38#include "ast.h"
Benjamin Petersonf216c942008-10-31 02:28:05 +000039
40extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000041
Fred Drake268397f1998-04-29 14:16:32 +000042#ifdef lint
43#include <note.h>
44#else
45#define NOTE(x)
46#endif
47
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000048/* String constants used to initialize module attributes.
49 *
50 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000051static char parser_copyright_string[] =
52"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000053University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
54Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
55Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000056
57
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000058PyDoc_STRVAR(parser_doc_string,
59"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000060
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000061static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000062
63
Martin v. Löwis18e16552006-02-15 17:27:45 +000064typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000065typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000066 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000067 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000068
Thomas Wouters7e474022000-07-16 12:04:32 +000069/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000070 * original copyright statement is included below, and continues to apply
71 * in full to the function immediately following. All other material is
72 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
73 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000074 * new naming conventions. Added arguments to provide support for creating
75 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000076 */
77
Guido van Rossum52f2c051993-11-10 12:53:24 +000078
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000079static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000080node2tuple(node *n, /* node to convert */
81 SeqMaker mkseq, /* create sequence */
82 SeqInserter addelem, /* func. to add elem. in seq. */
Thomas Wouters89f507f2006-12-13 04:49:30 +000083 int lineno, /* include line numbers? */
84 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000085{
Guido van Rossum3d602e31996-07-21 02:33:56 +000086 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000087 Py_INCREF(Py_None);
88 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000089 }
90 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000091 int i;
92 PyObject *v;
93 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000094
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000095 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000096 if (v == NULL)
97 return (v);
Christian Heimes217cfd12007-12-02 14:31:20 +000098 w = PyLong_FromLong(TYPE(n));
Fred Drakeff9ea482000-04-19 13:54:15 +000099 if (w == NULL) {
100 Py_DECREF(v);
101 return ((PyObject*) NULL);
102 }
103 (void) addelem(v, 0, w);
104 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000105 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000106 if (w == NULL) {
107 Py_DECREF(v);
108 return ((PyObject*) NULL);
109 }
110 (void) addelem(v, i+1, w);
111 }
Tim Peters6a627252003-07-21 14:25:23 +0000112
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000113 if (TYPE(n) == encoding_decl)
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000114 (void) addelem(v, i+1, PyUnicode_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000115 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000116 }
117 else if (ISTERMINAL(TYPE(n))) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000118 PyObject *result = mkseq(2 + lineno + col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000119 if (result != NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000120 (void) addelem(result, 0, PyLong_FromLong(TYPE(n)));
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000121 (void) addelem(result, 1, PyUnicode_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000122 if (lineno == 1)
Christian Heimes217cfd12007-12-02 14:31:20 +0000123 (void) addelem(result, 2, PyLong_FromLong(n->n_lineno));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000124 if (col_offset == 1)
Christian Heimes217cfd12007-12-02 14:31:20 +0000125 (void) addelem(result, 3, PyLong_FromLong(n->n_col_offset));
Fred Drakeff9ea482000-04-19 13:54:15 +0000126 }
127 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000128 }
129 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000130 PyErr_SetString(PyExc_SystemError,
131 "unrecognized parse tree node type");
132 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000133 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000134}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000135/*
136 * End of material copyrighted by Stichting Mathematisch Centrum.
137 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000138
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000139
140
141/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000142 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000143 * field of the object type to identify which any given object represents.
144 * These should probably go in an external header to allow other extensions
145 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000146 */
147
Fred Drakec2683dd2001-07-17 19:32:05 +0000148#define PyST_EXPR 1
149#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000150
151
152/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000153 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000154 * naming style, but the code uses the new naming convention.
155 */
156
157static PyObject*
158parser_error = 0;
159
160
Fred Drakec2683dd2001-07-17 19:32:05 +0000161typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000162 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000163 node* st_node; /* the node* returned by the parser */
164 int st_type; /* EXPR or SUITE ? */
Benjamin Petersonf216c942008-10-31 02:28:05 +0000165 PyCompilerFlags st_flags; /* Parser and compiler flags */
Fred Drakec2683dd2001-07-17 19:32:05 +0000166} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000167
168
Jeremy Hylton938ace62002-07-17 16:30:39 +0000169static void parser_free(PyST_Object *st);
Jesus Ceae9c53182012-08-03 14:28:37 +0200170static PyObject* parser_sizeof(PyST_Object *, void *);
Mark Dickinson211c6252009-02-01 10:28:51 +0000171static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op);
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000172static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
173static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
174static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
175static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
176static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
Fred Drake503d8d61998-04-13 18:45:18 +0000177
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000178#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
179
180static PyMethodDef parser_methods[] = {
181 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
182 PyDoc_STR("Compile this ST object into a code object.")},
183 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
184 PyDoc_STR("Determines if this ST object was created from an expression.")},
185 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
186 PyDoc_STR("Determines if this ST object was created from a suite.")},
187 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
188 PyDoc_STR("Creates a list-tree representation of this ST.")},
189 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
190 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Jesus Ceae9c53182012-08-03 14:28:37 +0200191 {"__sizeof__", (PyCFunction)parser_sizeof, METH_NOARGS,
192 PyDoc_STR("Returns size in memory, in bytes.")},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000193 {NULL, NULL, 0, NULL}
194};
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000195
Fred Drake268397f1998-04-29 14:16:32 +0000196static
Fred Drakec2683dd2001-07-17 19:32:05 +0000197PyTypeObject PyST_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000198 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000199 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000200 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000201 0, /* tp_itemsize */
202 (destructor)parser_free, /* tp_dealloc */
203 0, /* tp_print */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000204 0, /* tp_getattr */
Fred Drakeff9ea482000-04-19 13:54:15 +0000205 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000206 0, /* tp_reserved */
Fred Drakeff9ea482000-04-19 13:54:15 +0000207 0, /* tp_repr */
208 0, /* tp_as_number */
209 0, /* tp_as_sequence */
210 0, /* tp_as_mapping */
211 0, /* tp_hash */
212 0, /* tp_call */
213 0, /* tp_str */
214 0, /* tp_getattro */
215 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000216
217 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000218 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000219
Fred Drakeff9ea482000-04-19 13:54:15 +0000220 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000221
222 /* __doc__ */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000223 "Intermediate representation of a Python parse tree.",
224 0, /* tp_traverse */
225 0, /* tp_clear */
Mark Dickinson211c6252009-02-01 10:28:51 +0000226 parser_richcompare, /* tp_richcompare */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000227 0, /* tp_weaklistoffset */
228 0, /* tp_iter */
229 0, /* tp_iternext */
230 parser_methods, /* tp_methods */
Fred Drakec2683dd2001-07-17 19:32:05 +0000231}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000232
233
Mark Dickinson211c6252009-02-01 10:28:51 +0000234/* PyST_Type isn't subclassable, so just check ob_type */
235#define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type)
236
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000237static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000238parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000239{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000240 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000241
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000242 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000243 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000244
245 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000246 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247
248 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000249 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000250
251 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000252 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000253
254 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000255 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000256
257 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000258 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000259
Fred Drakeff9ea482000-04-19 13:54:15 +0000260 if (v != 0)
261 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000262 }
263 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000264}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265
Mark Dickinson211c6252009-02-01 10:28:51 +0000266/* parser_richcompare(PyObject* left, PyObject* right, int op)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000267 *
268 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
269 * This really just wraps a call to parser_compare_nodes() with some easy
270 * checks and protection code.
271 *
272 */
Mark Dickinson211c6252009-02-01 10:28:51 +0000273
274#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
275
276static PyObject *
277parser_richcompare(PyObject *left, PyObject *right, int op)
Guido van Rossum47478871996-08-21 14:32:37 +0000278{
Mark Dickinson211c6252009-02-01 10:28:51 +0000279 int result;
280 PyObject *v;
281
282 /* neither argument should be NULL, unless something's gone wrong */
283 if (left == NULL || right == NULL) {
284 PyErr_BadInternalCall();
285 return NULL;
286 }
287
288 /* both arguments should be instances of PyST_Object */
289 if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) {
290 v = Py_NotImplemented;
291 goto finished;
292 }
293
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000294 if (left == right)
Mark Dickinson211c6252009-02-01 10:28:51 +0000295 /* if arguments are identical, they're equal */
296 result = 0;
297 else
298 result = parser_compare_nodes(((PyST_Object *)left)->st_node,
299 ((PyST_Object *)right)->st_node);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000300
Mark Dickinson211c6252009-02-01 10:28:51 +0000301 /* Convert return value to a Boolean */
302 switch (op) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000303 case Py_EQ:
Mark Dickinson211c6252009-02-01 10:28:51 +0000304 v = TEST_COND(result == 0);
305 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000306 case Py_NE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000307 v = TEST_COND(result != 0);
308 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000309 case Py_LE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000310 v = TEST_COND(result <= 0);
311 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000312 case Py_GE:
Mark Dickinson211c6252009-02-01 10:28:51 +0000313 v = TEST_COND(result >= 0);
314 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000315 case Py_LT:
Mark Dickinson211c6252009-02-01 10:28:51 +0000316 v = TEST_COND(result < 0);
317 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000318 case Py_GT:
Mark Dickinson211c6252009-02-01 10:28:51 +0000319 v = TEST_COND(result > 0);
320 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000321 default:
Mark Dickinson211c6252009-02-01 10:28:51 +0000322 PyErr_BadArgument();
323 return NULL;
324 }
325 finished:
326 Py_INCREF(v);
327 return v;
Fred Drakeff9ea482000-04-19 13:54:15 +0000328}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000329
Fred Drakec2683dd2001-07-17 19:32:05 +0000330/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000331 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000332 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000333 * 'wrapper' object that holds a node* and allows it to be passed around in
334 * Python code.
335 *
336 */
337static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000338parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000339{
Fred Drakec2683dd2001-07-17 19:32:05 +0000340 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000341
342 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000343 o->st_node = st;
344 o->st_type = type;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000345 o->st_flags.cf_flags = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000346 }
Fred Drake268397f1998-04-29 14:16:32 +0000347 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000348 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000349 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000350 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000351}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000352
353
Fred Drakec2683dd2001-07-17 19:32:05 +0000354/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000355 *
356 * This is called by a del statement that reduces the reference count to 0.
357 *
358 */
359static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000360parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000361{
Fred Drakec2683dd2001-07-17 19:32:05 +0000362 PyNode_Free(st->st_node);
363 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000364}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000365
Jesus Ceae9c53182012-08-03 14:28:37 +0200366static PyObject *
367parser_sizeof(PyST_Object *st, void *unused)
368{
369 Py_ssize_t res;
370
371 res = sizeof(PyST_Object) + _PyNode_SizeOf(st->st_node);
372 return PyLong_FromSsize_t(res);
373}
374
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000375
Fred Drakec2683dd2001-07-17 19:32:05 +0000376/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000377 *
378 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000379 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000380 *
381 */
382static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000383parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000384{
Antoine Pitrou721738f2012-08-15 23:20:39 +0200385 int line_info = 0;
386 int col_info = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000387 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000388 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000389
Georg Brandl30704ea02008-07-23 15:07:12 +0000390 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000391
Martin v. Löwis1a214512008-06-11 05:26:20 +0000392 if (self == NULL || PyModule_Check(self)) {
Antoine Pitrou721738f2012-08-15 23:20:39 +0200393 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2tuple", keywords,
394 &PyST_Type, &self, &line_info,
395 &col_info);
Fred Drake268397f1998-04-29 14:16:32 +0000396 }
Fred Drake503d8d61998-04-13 18:45:18 +0000397 else
Antoine Pitrou721738f2012-08-15 23:20:39 +0200398 ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:totuple", &keywords[1],
399 &line_info, &col_info);
Fred Drake268397f1998-04-29 14:16:32 +0000400 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000401 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000402 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000403 * since it's known to work already.
404 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000405 res = node2tuple(((PyST_Object*)self)->st_node,
Antoine Pitrou721738f2012-08-15 23:20:39 +0200406 PyTuple_New, PyTuple_SetItem, line_info, col_info);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000407 }
408 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000409}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000410
411
Fred Drakec2683dd2001-07-17 19:32:05 +0000412/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000413 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000414 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000415 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000416 *
417 */
418static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000419parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000420{
Antoine Pitrou721738f2012-08-15 23:20:39 +0200421 int line_info = 0;
422 int col_info = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000423 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000424 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000425
Georg Brandl30704ea02008-07-23 15:07:12 +0000426 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000427
Martin v. Löwis1a214512008-06-11 05:26:20 +0000428 if (self == NULL || PyModule_Check(self))
Antoine Pitrou721738f2012-08-15 23:20:39 +0200429 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2list", keywords,
430 &PyST_Type, &self, &line_info,
431 &col_info);
Fred Drake503d8d61998-04-13 18:45:18 +0000432 else
Antoine Pitrou721738f2012-08-15 23:20:39 +0200433 ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:tolist", &keywords[1],
434 &line_info, &col_info);
Fred Drake503d8d61998-04-13 18:45:18 +0000435 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000436 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000437 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000438 * since it's known to work already.
439 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000440 res = node2tuple(self->st_node,
Antoine Pitrou721738f2012-08-15 23:20:39 +0200441 PyList_New, PyList_SetItem, line_info, col_info);
Guido van Rossum47478871996-08-21 14:32:37 +0000442 }
443 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000444}
Guido van Rossum47478871996-08-21 14:32:37 +0000445
446
Fred Drakec2683dd2001-07-17 19:32:05 +0000447/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000448 *
449 * This function creates code objects from the parse tree represented by
450 * the passed-in data object. An optional file name is passed in as well.
451 *
452 */
453static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000454parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000455{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000456 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000457 PyArena* arena;
458 mod_ty mod;
Fred Drakec2683dd2001-07-17 19:32:05 +0000459 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000460 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000461
Georg Brandl30704ea02008-07-23 15:07:12 +0000462 static char *keywords[] = {"st", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000463
Martin v. Löwis1a214512008-06-11 05:26:20 +0000464 if (self == NULL || PyModule_Check(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000465 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
466 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000467 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000468 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000469 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000470
Benjamin Petersonf216c942008-10-31 02:28:05 +0000471 if (ok) {
472 arena = PyArena_New();
473 if (arena) {
474 mod = PyAST_FromNode(self->st_node, &(self->st_flags), str, arena);
475 if (mod) {
476 res = (PyObject *)PyAST_Compile(mod, str, &(self->st_flags), arena);
477 }
478 PyArena_Free(arena);
479 }
480 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000481
482 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000483}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000484
485
486/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
487 * PyObject* parser_issuite(PyObject* self, PyObject* args)
488 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000489 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000490 * a statement suite, respectively. The return is a Python truth value.
491 *
492 */
493static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000494parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000495{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000496 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000497 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000498
Georg Brandl30704ea02008-07-23 15:07:12 +0000499 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000500
Martin v. Löwis1a214512008-06-11 05:26:20 +0000501 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000502 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000503 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000504 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000505 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000506
507 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000508 /* Check to see if the ST represents an expression or not. */
509 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000510 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000511 }
512 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000513}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000514
515
516static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000517parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000518{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000519 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000520 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000521
Georg Brandl30704ea02008-07-23 15:07:12 +0000522 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000523
Martin v. Löwis1a214512008-06-11 05:26:20 +0000524 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000525 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000526 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000527 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000528 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000529
530 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000531 /* Check to see if the ST represents an expression or not. */
532 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000533 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000534 }
535 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000536}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000537
538
Guido van Rossum3d602e31996-07-21 02:33:56 +0000539/* err_string(char* message)
540 *
541 * Sets the error string for an exception of type ParserError.
542 *
543 */
544static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000545err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000546{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000547 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000548}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000549
550
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000551/* PyObject* parser_do_parse(PyObject* args, int type)
552 *
553 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000554 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000555 *
556 */
557static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000558parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000559{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000560 char* string = 0;
561 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000562 int flags = 0;
563 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000564
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000565 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000566
567 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000568 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
569 &_PyParser_Grammar,
570 (type == PyST_EXPR)
571 ? eval_input : file_input,
572 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 if (n) {
575 res = parser_newstobject(n, type);
Benjamin Petersonf216c942008-10-31 02:28:05 +0000576 if (res)
577 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
578 }
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500579 else {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000580 PyParser_SetError(&err);
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500581 }
Benjamin Petersonf0cdbad2011-06-05 22:14:05 -0500582 PyParser_ClearError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000583 }
584 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000585}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000586
587
588/* PyObject* parser_expr(PyObject* self, PyObject* args)
589 * PyObject* parser_suite(PyObject* self, PyObject* args)
590 *
591 * External interfaces to the parser itself. Which is called determines if
592 * the parser attempts to recognize an expression ('eval' form) or statement
593 * suite ('exec' form). The real work is done by parser_do_parse() above.
594 *
595 */
596static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000597parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000598{
Fred Drake268397f1998-04-29 14:16:32 +0000599 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000600 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000601}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000602
603
604static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000605parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000606{
Fred Drake268397f1998-04-29 14:16:32 +0000607 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000608 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000609}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000610
611
612
Fred Drakec2683dd2001-07-17 19:32:05 +0000613/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000614 * object requires that the input tuple be valid without having to rely on
615 * catching an exception from the compiler. This is done to allow the
616 * compiler itself to remain fast, since most of its input will come from
617 * the parser directly, and therefore be known to be syntactically correct.
618 * This validation is done to ensure that we don't core dump the compile
619 * phase, returning an exception instead.
620 *
621 * Two aspects can be broken out in this code: creating a node tree from
622 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000623 * advantageous to expand the number of ST types to include funcdefs and
624 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000625 * here. They are not necessary, and not quite as useful in a raw form.
626 * For now, let's get expressions and suites working reliably.
627 */
628
629
Jeremy Hylton938ace62002-07-17 16:30:39 +0000630static node* build_node_tree(PyObject *tuple);
631static int validate_expr_tree(node *tree);
632static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000633static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000634
Fred Drakec2683dd2001-07-17 19:32:05 +0000635/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000636 *
637 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000638 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000639 * tuple can be validated. It does this by checking the first code of the
640 * tuple, and, if acceptable, builds the internal representation. If this
641 * step succeeds, the internal representation is validated as fully as
642 * possible with the various validate_*() routines defined below.
643 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000644 * This function must be changed if support is to be added for PyST_FRAGMENT
645 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000646 *
647 */
648static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000649parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000650{
Fred Drake268397f1998-04-29 14:16:32 +0000651 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000652 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000653 PyObject *tuple;
654 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000655
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000656 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000657
Fred Drakec2683dd2001-07-17 19:32:05 +0000658 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000659 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000660 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000661 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000662 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000663 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000664 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000665 }
666 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000667 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000668 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000669 tree = build_node_tree(tuple);
670 if (tree != 0) {
671 int start_sym = TYPE(tree);
672 if (start_sym == eval_input) {
673 /* Might be an eval form. */
674 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000675 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000676 else
677 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000678 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000679 else if (start_sym == file_input) {
680 /* This looks like an exec form so far. */
681 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000682 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000683 else
684 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000685 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000686 else if (start_sym == encoding_decl) {
687 /* This looks like an encoding_decl so far. */
688 if (validate_encoding_decl(tree))
689 st = parser_newstobject(tree, PyST_SUITE);
690 else
691 PyNode_Free(tree);
692 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000693 else {
694 /* This is a fragment, at best. */
695 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000696 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000697 }
Guido van Rossum47478871996-08-21 14:32:37 +0000698 }
Andrew Svetlov737fb892012-12-18 21:14:22 +0200699 /* Make sure we raise an exception on all errors. We should never
Guido van Rossum47478871996-08-21 14:32:37 +0000700 * get this, but we'd do well to be sure something is done.
701 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000702 if (st == NULL && !PyErr_Occurred())
703 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000704
Fred Drakec2683dd2001-07-17 19:32:05 +0000705 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000706}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000707
708
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000709/* node* build_node_children()
710 *
711 * Iterate across the children of the current non-terminal node and build
712 * their structures. If successful, return the root of this portion of
713 * the tree, otherwise, 0. Any required exception will be specified already,
714 * and no memory will have been deallocated.
715 *
716 */
717static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000718build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000719{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000720 Py_ssize_t len = PyObject_Size(tuple);
721 Py_ssize_t i;
722 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000723
724 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000725 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000726 PyObject* elem = PySequence_GetItem(tuple, i);
727 int ok = elem != NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200728 int type = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000729 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000730
Fred Drakeff9ea482000-04-19 13:54:15 +0000731 if (ok)
732 ok = PySequence_Check(elem);
733 if (ok) {
734 PyObject *temp = PySequence_GetItem(elem, 0);
735 if (temp == NULL)
736 ok = 0;
737 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000738 ok = PyLong_Check(temp);
Serhiy Storchaka78980432013-01-15 01:12:17 +0200739 if (ok) {
740 type = _PyLong_AsInt(temp);
741 if (type == -1 && PyErr_Occurred()) {
742 Py_DECREF(temp);
743 Py_DECREF(elem);
744 return 0;
745 }
746 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000747 Py_DECREF(temp);
748 }
749 }
750 if (!ok) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000751 PyObject *err = Py_BuildValue("os", elem,
752 "Illegal node construct.");
753 PyErr_SetObject(parser_error, err);
754 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000755 Py_XDECREF(elem);
756 return (0);
757 }
758 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000759 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000760 PyObject *temp;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000761 const char *temp_str;
Guido van Rossum47478871996-08-21 14:32:37 +0000762
Fred Drake0ac9b072000-09-12 21:58:06 +0000763 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000764 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000765 return 0;
766 }
767 temp = PySequence_GetItem(elem, 1);
768 if (temp == NULL)
769 return 0;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000770 if (!PyUnicode_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000771 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000772 "second item in terminal node must be a string,"
773 " found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000774 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000775 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000776 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000777 return 0;
778 }
779 if (len == 3) {
780 PyObject *o = PySequence_GetItem(elem, 2);
781 if (o != NULL) {
Serhiy Storchaka78980432013-01-15 01:12:17 +0200782 if (PyLong_Check(o)) {
783 int num = _PyLong_AsInt(o);
784 if (num == -1 && PyErr_Occurred()) {
785 Py_DECREF(o);
786 Py_DECREF(temp);
787 Py_DECREF(elem);
788 return 0;
789 }
790 *line_num = num;
791 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000792 else {
793 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000794 "third item in terminal node must be an"
795 " integer, found %s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 Py_TYPE(temp)->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000797 Py_DECREF(o);
798 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000799 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000800 return 0;
801 }
802 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000803 }
804 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000805 temp_str = _PyUnicode_AsStringAndSize(temp, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000806 if (temp_str == NULL) {
807 Py_DECREF(temp);
808 Py_XDECREF(elem);
809 return 0;
810 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000811 strn = (char *)PyObject_MALLOC(len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000812 if (strn != NULL)
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000813 (void) memcpy(strn, temp_str, len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000814 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000815 }
816 else if (!ISNONTERMINAL(type)) {
817 /*
818 * It has to be one or the other; this is an error.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200819 * Raise an exception.
Fred Drakeff9ea482000-04-19 13:54:15 +0000820 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000821 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
822 PyErr_SetObject(parser_error, err);
823 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000824 Py_XDECREF(elem);
825 return (0);
826 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000827 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000828 if (err == E_NOMEM) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000829 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000830 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000831 return (node *) PyErr_NoMemory();
832 }
833 if (err == E_OVERFLOW) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000834 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000835 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000836 PyErr_SetString(PyExc_ValueError,
837 "unsupported number of child nodes");
838 return NULL;
839 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000840
Fred Drakeff9ea482000-04-19 13:54:15 +0000841 if (ISNONTERMINAL(type)) {
842 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000843
Fred Drakeff9ea482000-04-19 13:54:15 +0000844 if (new_child != build_node_children(elem, new_child, line_num)) {
845 Py_XDECREF(elem);
846 return (0);
847 }
848 }
849 else if (type == NEWLINE) { /* It's true: we increment the */
850 ++(*line_num); /* line number *after* the newline! */
851 }
852 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000853 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000854 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000855}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000856
857
858static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000859build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000860{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000861 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000862 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000863 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000864
Guido van Rossum47478871996-08-21 14:32:37 +0000865 if (temp != NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000866 num = PyLong_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000867 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000868 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000869 /*
870 * The tuple is simple, but it doesn't start with a start symbol.
Andrew Svetlov737fb892012-12-18 21:14:22 +0200871 * Raise an exception now and be done with it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000872 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000873 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000874 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000875 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000876 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000877 }
878 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000879 /*
880 * Not efficient, but that can be handled later.
881 */
882 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000883 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000884
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000885 if (num == encoding_decl) {
886 encoding = PySequence_GetItem(tuple, 2);
887 /* tuple isn't borrowed anymore here, need to DECREF */
888 tuple = PySequence_GetSlice(tuple, 0, 2);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000889 if (tuple == NULL)
890 return NULL;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000891 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000892 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000893 if (res != NULL) {
894 if (res != build_node_children(tuple, res, &line_num)) {
895 PyNode_Free(res);
896 res = NULL;
897 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000898 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000899 Py_ssize_t len;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000900 const char *temp;
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000901 temp = _PyUnicode_AsStringAndSize(encoding, &len);
Alexander Belopolskye239d232010-12-08 23:31:48 +0000902 if (temp == NULL) {
903 Py_DECREF(res);
904 Py_DECREF(encoding);
905 Py_DECREF(tuple);
906 return NULL;
907 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000908 res->n_str = (char *)PyObject_MALLOC(len + 1);
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000909 if (res->n_str != NULL && temp != NULL)
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000910 (void) memcpy(res->n_str, temp, len + 1);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000911 Py_DECREF(encoding);
912 Py_DECREF(tuple);
913 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000914 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000915 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000916 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000917 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000918 * NONTERMINAL, we can't use it. Not sure the implementation
919 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000920 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000921 PyObject *err = Py_BuildValue("os", tuple,
922 "Illegal component tuple.");
923 PyErr_SetObject(parser_error, err);
924 Py_XDECREF(err);
925 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000926
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000927 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000928}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000929
930
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000931/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000932 * Validation routines used within the validation section:
933 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000934static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000935
Fred Drakeff9ea482000-04-19 13:54:15 +0000936#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
937#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
938#define validate_colon(ch) validate_terminal(ch, COLON, ":")
939#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
940#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
941#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
942#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
943#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
944#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
945#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
946#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
947#define validate_star(ch) validate_terminal(ch, STAR, "*")
948#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
949#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
950#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000951#define validate_at(ch) validate_terminal(ch, AT, "@")
Mark Dickinsonea7e9f92012-04-29 18:34:40 +0100952#define validate_rarrow(ch) validate_terminal(ch, RARROW, "->")
Fred Drakeff9ea482000-04-19 13:54:15 +0000953#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000954
Fred Drake0ac9b072000-09-12 21:58:06 +0000955#define VALIDATER(n) static int validate_##n(node *tree)
956
Fred Drakeff9ea482000-04-19 13:54:15 +0000957VALIDATER(node); VALIDATER(small_stmt);
958VALIDATER(class); VALIDATER(node);
959VALIDATER(parameters); VALIDATER(suite);
960VALIDATER(testlist); VALIDATER(varargslist);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000961VALIDATER(vfpdef);
Fred Drakeff9ea482000-04-19 13:54:15 +0000962VALIDATER(stmt); VALIDATER(simple_stmt);
963VALIDATER(expr_stmt); VALIDATER(power);
Guido van Rossum452bf512007-02-09 05:32:43 +0000964VALIDATER(del_stmt);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000965VALIDATER(return_stmt); VALIDATER(raise_stmt);
966VALIDATER(import_stmt); VALIDATER(import_stmt);
967VALIDATER(import_name); VALIDATER(yield_stmt);
Mark Dickinson407b3bd2012-04-29 22:18:31 +0100968VALIDATER(global_stmt); VALIDATER(nonlocal_stmt);
969VALIDATER(assert_stmt);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000970VALIDATER(compound_stmt); VALIDATER(test_or_star_expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000971VALIDATER(while); VALIDATER(for);
972VALIDATER(try); VALIDATER(except_clause);
973VALIDATER(test); VALIDATER(and_test);
974VALIDATER(not_test); VALIDATER(comparison);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000975VALIDATER(comp_op);
Guido van Rossum0368b722007-05-11 16:50:42 +0000976VALIDATER(star_expr); VALIDATER(expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000977VALIDATER(xor_expr); VALIDATER(and_expr);
978VALIDATER(shift_expr); VALIDATER(arith_expr);
979VALIDATER(term); VALIDATER(factor);
980VALIDATER(atom); VALIDATER(lambdef);
981VALIDATER(trailer); VALIDATER(subscript);
982VALIDATER(subscriptlist); VALIDATER(sliceop);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000983VALIDATER(exprlist); VALIDATER(dictorsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000984VALIDATER(arglist); VALIDATER(argument);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000985VALIDATER(comp_for);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000986VALIDATER(comp_iter); VALIDATER(comp_if);
987VALIDATER(testlist_comp); VALIDATER(yield_expr);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000988VALIDATER(or_test);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000989VALIDATER(test_nocond); VALIDATER(lambdef_nocond);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000990VALIDATER(yield_arg);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000991
Fred Drake0ac9b072000-09-12 21:58:06 +0000992#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000993
Fred Drakeff9ea482000-04-19 13:54:15 +0000994#define is_even(n) (((n) & 1) == 0)
995#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000996
997
998static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000999validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +00001000{
Fred Drake0ac9b072000-09-12 21:58:06 +00001001 if (TYPE(n) != t) {
1002 PyErr_Format(parser_error, "Expected node type %d, got %d.",
1003 t, TYPE(n));
1004 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001005 }
Fred Drake0ac9b072000-09-12 21:58:06 +00001006 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00001007}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001008
1009
Fred Drakee7ab64e2000-04-25 04:14:46 +00001010/* Verifies that the number of child nodes is exactly 'num', raising
1011 * an exception if it isn't. The exception message does not indicate
1012 * the exact number of nodes, allowing this to be used to raise the
1013 * "right" exception when the wrong number of nodes is present in a
1014 * specific variant of a statement's syntax. This is commonly used
1015 * in that fashion.
1016 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001017static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001018validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001019{
Guido van Rossum3d602e31996-07-21 02:33:56 +00001020 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001021 PyErr_Format(parser_error,
1022 "Illegal number of children for %s node.", name);
1023 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001024 }
Fred Drake0ac9b072000-09-12 21:58:06 +00001025 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00001026}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001027
1028
1029static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001030validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +00001031{
Guido van Rossum3d602e31996-07-21 02:33:56 +00001032 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +00001033 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001034
1035 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001036 PyErr_Format(parser_error,
1037 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001038 }
1039 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001040}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001041
1042
Guido van Rossum47478871996-08-21 14:32:37 +00001043/* X (',' X) [',']
1044 */
1045static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +00001046validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +00001047 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001048{
1049 int nch = NCH(tree);
1050 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +00001051 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +00001052
1053 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001054 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +00001055 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001056 if (is_even(nch))
1057 res = validate_comma(CHILD(tree, --nch));
1058 if (res && nch > 1) {
1059 int pos = 1;
1060 for ( ; res && pos < nch; pos += 2)
1061 res = (validate_comma(CHILD(tree, pos))
1062 && vfunc(CHILD(tree, pos + 1)));
1063 }
Guido van Rossum47478871996-08-21 14:32:37 +00001064 }
1065 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001066}
Guido van Rossum47478871996-08-21 14:32:37 +00001067
1068
Fred Drakecff283c2000-08-21 22:24:43 +00001069/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001070 *
1071 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001072 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00001073 */
Guido van Rossum47478871996-08-21 14:32:37 +00001074static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001075validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001076{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001077 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +00001078 int res = (validate_ntype(tree, classdef) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001080
Guido van Rossum3d602e31996-07-21 02:33:56 +00001081 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001082 res = (validate_name(CHILD(tree, 0), "class")
1083 && validate_ntype(CHILD(tree, 1), NAME)
1084 && validate_colon(CHILD(tree, nch - 2))
1085 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001086 }
Brett Cannonf4189912005-04-09 02:30:16 +00001087 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001088 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +00001089 }
Guido van Rossumfc158e22007-11-15 19:17:28 +00001090
Brett Cannonf4189912005-04-09 02:30:16 +00001091 if (res) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 if (nch == 7) {
1093 res = ((validate_lparen(CHILD(tree, 2)) &&
1094 validate_arglist(CHILD(tree, 3)) &&
1095 validate_rparen(CHILD(tree, 4))));
1096 }
1097 else if (nch == 6) {
1098 res = (validate_lparen(CHILD(tree,2)) &&
1099 validate_rparen(CHILD(tree,3)));
1100 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001101 }
1102 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001103}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001104
1105
Guido van Rossum3d602e31996-07-21 02:33:56 +00001106/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001107 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001108 */
Guido van Rossum47478871996-08-21 14:32:37 +00001109static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001110validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001111{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001112 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001113 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001114 && (nch >= 4)
1115 && validate_name(CHILD(tree, 0), "if")
1116 && validate_test(CHILD(tree, 1))
1117 && validate_colon(CHILD(tree, 2))
1118 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001119
1120 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001121 /* ... 'else' ':' suite */
1122 res = (validate_name(CHILD(tree, nch - 3), "else")
1123 && validate_colon(CHILD(tree, nch - 2))
1124 && validate_suite(CHILD(tree, nch - 1)));
1125 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001126 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001127 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001128 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001129 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001130 /* Will catch the case for nch < 4 */
1131 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001132 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001133 /* ... ('elif' test ':' suite)+ ... */
1134 int j = 4;
1135 while ((j < nch) && res) {
1136 res = (validate_name(CHILD(tree, j), "elif")
1137 && validate_colon(CHILD(tree, j + 2))
1138 && validate_test(CHILD(tree, j + 1))
1139 && validate_suite(CHILD(tree, j + 3)));
1140 j += 4;
1141 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001142 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001143 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001144}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001145
1146
Guido van Rossum3d602e31996-07-21 02:33:56 +00001147/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001148 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001149 *
1150 */
Guido van Rossum47478871996-08-21 14:32:37 +00001151static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001152validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001153{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001154 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001155 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001156
Guido van Rossum3d602e31996-07-21 02:33:56 +00001157 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001158 res = (validate_lparen(CHILD(tree, 0))
1159 && validate_rparen(CHILD(tree, nch - 1)));
1160 if (res && (nch == 3))
1161 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001162 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001163 else {
1164 (void) validate_numnodes(tree, 2, "parameters");
1165 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001166 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001167}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001168
1169
Fred Drakecff283c2000-08-21 22:24:43 +00001170/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001171 *
1172 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001173 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001174 * | NEWLINE INDENT stmt+ DEDENT
1175 */
Guido van Rossum47478871996-08-21 14:32:37 +00001176static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001177validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001178{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001179 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001180 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001181
Guido van Rossum3d602e31996-07-21 02:33:56 +00001182 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001183 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001184 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001185 /* NEWLINE INDENT stmt+ DEDENT */
1186 res = (validate_newline(CHILD(tree, 0))
1187 && validate_indent(CHILD(tree, 1))
1188 && validate_stmt(CHILD(tree, 2))
1189 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001190
Fred Drakeff9ea482000-04-19 13:54:15 +00001191 if (res && (nch > 4)) {
1192 int i = 3;
1193 --nch; /* forget the DEDENT */
1194 for ( ; res && (i < nch); ++i)
1195 res = validate_stmt(CHILD(tree, i));
1196 }
1197 else if (nch < 4)
1198 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001199 }
1200 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001201}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001202
1203
Guido van Rossum47478871996-08-21 14:32:37 +00001204static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001205validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001206{
Guido van Rossum47478871996-08-21 14:32:37 +00001207 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001208 validate_test, "testlist"));
1209}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001210
Guido van Rossum1c917072001-10-15 15:44:05 +00001211static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00001212validate_testlist_star_expr(node *tl)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001213{
Benjamin Peterson4905e802009-09-27 02:43:28 +00001214 return (validate_repeating_list(tl, testlist_star_expr, validate_test_or_star_expr,
1215 "testlist"));
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001216}
1217
1218
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001219/* validate either vfpdef or tfpdef.
1220 * vfpdef: NAME
1221 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001222 */
1223static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001224validate_vfpdef(node *tree)
Neal Norwitzc1505362006-12-28 06:47:50 +00001225{
1226 int nch = NCH(tree);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001227 if (TYPE(tree) == vfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001228 return nch == 1 && validate_name(CHILD(tree, 0), NULL);
1229 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001230 else if (TYPE(tree) == tfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001231 if (nch == 1) {
1232 return validate_name(CHILD(tree, 0), NULL);
1233 }
1234 else if (nch == 3) {
1235 return validate_name(CHILD(tree, 0), NULL) &&
1236 validate_colon(CHILD(tree, 1)) &&
1237 validate_test(CHILD(tree, 2));
1238 }
1239 }
1240 return 0;
1241}
1242
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001243/* '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001244 * ..or tfpdef in place of vfpdef. vfpdef: NAME; tfpdef: NAME [':' test]
Fred Drakecff283c2000-08-21 22:24:43 +00001245 */
1246static int
1247validate_varargslist_trailer(node *tree, int start)
1248{
1249 int nch = NCH(tree);
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001250 int res = 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001251
1252 if (nch <= start) {
1253 err_string("expected variable argument trailer for varargslist");
1254 return 0;
1255 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001256 if (TYPE(CHILD(tree, start)) == STAR) {
Fred Drakecff283c2000-08-21 22:24:43 +00001257 /*
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001258 * '*' [vfpdef]
Fred Drakecff283c2000-08-21 22:24:43 +00001259 */
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001260 res = validate_star(CHILD(tree, start++));
1261 if (res && start < nch && (TYPE(CHILD(tree, start)) == vfpdef ||
1262 TYPE(CHILD(tree, start)) == tfpdef))
1263 res = validate_vfpdef(CHILD(tree, start++));
1264 /*
1265 * (',' vfpdef ['=' test])*
1266 */
1267 while (res && start + 1 < nch && (
1268 TYPE(CHILD(tree, start + 1)) == vfpdef ||
1269 TYPE(CHILD(tree, start + 1)) == tfpdef)) {
1270 res = (validate_comma(CHILD(tree, start++))
1271 && validate_vfpdef(CHILD(tree, start++)));
1272 if (res && start + 1 < nch && TYPE(CHILD(tree, start)) == EQUAL)
1273 res = (validate_equal(CHILD(tree, start++))
1274 && validate_test(CHILD(tree, start++)));
1275 }
1276 /*
1277 * [',' '**' vfpdef]
1278 */
1279 if (res && start + 2 < nch && TYPE(CHILD(tree, start+1)) == DOUBLESTAR)
1280 res = (validate_comma(CHILD(tree, start++))
1281 && validate_doublestar(CHILD(tree, start++))
1282 && validate_vfpdef(CHILD(tree, start++)));
1283 }
1284 else if (TYPE(CHILD(tree, start)) == DOUBLESTAR) {
1285 /*
1286 * '**' vfpdef
1287 */
1288 if (start + 1 < nch)
1289 res = (validate_doublestar(CHILD(tree, start++))
1290 && validate_vfpdef(CHILD(tree, start++)));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001291 else {
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001292 res = 0;
1293 err_string("expected vfpdef after ** in varargslist trailer");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001294 }
Fred Drakecff283c2000-08-21 22:24:43 +00001295 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001296 else {
1297 res = 0;
1298 err_string("expected * or ** in varargslist trailer");
Fred Drakecff283c2000-08-21 22:24:43 +00001299 }
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01001300
1301 if (res && start != nch) {
1302 res = 0;
1303 err_string("unexpected extra children in varargslist trailer");
1304 }
Fred Drakecff283c2000-08-21 22:24:43 +00001305 return res;
1306}
1307
1308
Neal Norwitzc1505362006-12-28 06:47:50 +00001309/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001310 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001311 * Validate typedargslist or varargslist.
1312 *
1313 * typedargslist: ((tfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001314 * ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
1315 * '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001316 * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001317 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001318 * varargslist: ((vfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001319 * ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
1320 * '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001321 * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001322 * vfpdef: NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001323 *
1324 */
Guido van Rossum47478871996-08-21 14:32:37 +00001325static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001326validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001327{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001328 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001329 int res = (TYPE(tree) == varargslist ||
1330 TYPE(tree) == typedargslist) &&
1331 (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001332 int sym;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001333 node *ch;
1334 int i = 0;
Guido van Rossumfc158e22007-11-15 19:17:28 +00001335
Fred Drakeb6429a22000-12-11 22:08:27 +00001336 if (!res)
1337 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001338 if (nch < 1) {
1339 err_string("varargslist missing child nodes");
1340 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001341 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001342 while (i < nch) {
Guido van Rossumfc158e22007-11-15 19:17:28 +00001343 ch = CHILD(tree, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001344 sym = TYPE(ch);
1345 if (sym == vfpdef || sym == tfpdef) {
1346 /* validate (vfpdef ['=' test] ',')+ */
1347 res = validate_vfpdef(ch);
Fred Drakecff283c2000-08-21 22:24:43 +00001348 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001349 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1350 res = (validate_equal(CHILD(tree, i))
1351 && validate_test(CHILD(tree, i+1)));
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001352 if (res)
1353 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001354 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001355 if (res && i < nch) {
1356 res = validate_comma(CHILD(tree, i));
1357 ++i;
Fred Drakecff283c2000-08-21 22:24:43 +00001358 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001359 } else if (sym == DOUBLESTAR || sym == STAR) {
1360 res = validate_varargslist_trailer(tree, i);
1361 break;
1362 } else {
1363 res = 0;
1364 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001365 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001366 }
Fred Drakecff283c2000-08-21 22:24:43 +00001367 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001368}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001369
1370
Nick Coghlan650f0d02007-04-15 12:05:43 +00001371/* comp_iter: comp_for | comp_if
Fred Drakecff283c2000-08-21 22:24:43 +00001372 */
1373static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001374validate_comp_iter(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001375{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001376 int res = (validate_ntype(tree, comp_iter)
1377 && validate_numnodes(tree, 1, "comp_iter"));
1378 if (res && TYPE(CHILD(tree, 0)) == comp_for)
1379 res = validate_comp_for(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001380 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001381 res = validate_comp_if(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001382
1383 return res;
1384}
1385
Nick Coghlan650f0d02007-04-15 12:05:43 +00001386/* comp_for: 'for' exprlist 'in' test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001387 */
1388static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001389validate_comp_for(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001390{
1391 int nch = NCH(tree);
1392 int res;
1393
1394 if (nch == 5)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001395 res = validate_comp_iter(CHILD(tree, 4));
Fred Drakecff283c2000-08-21 22:24:43 +00001396 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001397 res = validate_numnodes(tree, 4, "comp_for");
Raymond Hettinger354433a2004-05-19 08:20:33 +00001398
1399 if (res)
1400 res = (validate_name(CHILD(tree, 0), "for")
1401 && validate_exprlist(CHILD(tree, 1))
1402 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001403 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001404
1405 return res;
1406}
1407
Nick Coghlan650f0d02007-04-15 12:05:43 +00001408/* comp_if: 'if' test_nocond [comp_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001409 */
1410static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001411validate_comp_if(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001412{
1413 int nch = NCH(tree);
1414 int res;
1415
1416 if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001417 res = validate_comp_iter(CHILD(tree, 2));
Fred Drakecff283c2000-08-21 22:24:43 +00001418 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001419 res = validate_numnodes(tree, 2, "comp_if");
Fred Drakecff283c2000-08-21 22:24:43 +00001420
1421 if (res)
1422 res = (validate_name(CHILD(tree, 0), "if")
Nick Coghlan650f0d02007-04-15 12:05:43 +00001423 && validate_test_nocond(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001424
1425 return res;
1426}
1427
1428
Guido van Rossum3d602e31996-07-21 02:33:56 +00001429/* simple_stmt | compound_stmt
1430 *
1431 */
Guido van Rossum47478871996-08-21 14:32:37 +00001432static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001433validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001434{
1435 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001436 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001437
Guido van Rossum3d602e31996-07-21 02:33:56 +00001438 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001439 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001440
Fred Drakeff9ea482000-04-19 13:54:15 +00001441 if (TYPE(tree) == simple_stmt)
1442 res = validate_simple_stmt(tree);
1443 else
1444 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001445 }
1446 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001447}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001448
1449
Guido van Rossum3d602e31996-07-21 02:33:56 +00001450/* small_stmt (';' small_stmt)* [';'] NEWLINE
1451 *
1452 */
Guido van Rossum47478871996-08-21 14:32:37 +00001453static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001454validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001455{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001456 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001457 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001458 && (nch >= 2)
1459 && validate_small_stmt(CHILD(tree, 0))
1460 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001461
Guido van Rossum3d602e31996-07-21 02:33:56 +00001462 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001463 res = validate_numnodes(tree, 2, "simple_stmt");
1464 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001465 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001466 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001467 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001468 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001469
Fred Drakeff9ea482000-04-19 13:54:15 +00001470 for (i = 1; res && (i < nch); i += 2)
1471 res = (validate_semi(CHILD(tree, i))
1472 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001473 }
1474 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001475}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001476
1477
Guido van Rossum47478871996-08-21 14:32:37 +00001478static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001479validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001480{
1481 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001482 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001483
Fred Drake0ac9b072000-09-12 21:58:06 +00001484 if (res) {
1485 int ntype = TYPE(CHILD(tree, 0));
1486
1487 if ( (ntype == expr_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001488 || (ntype == del_stmt)
1489 || (ntype == pass_stmt)
1490 || (ntype == flow_stmt)
1491 || (ntype == import_stmt)
1492 || (ntype == global_stmt)
Mark Dickinson407b3bd2012-04-29 22:18:31 +01001493 || (ntype == nonlocal_stmt)
Georg Brandl7cae87c2006-09-06 06:51:57 +00001494 || (ntype == assert_stmt))
Fred Drake0ac9b072000-09-12 21:58:06 +00001495 res = validate_node(CHILD(tree, 0));
1496 else {
1497 res = 0;
1498 err_string("illegal small_stmt child type");
1499 }
1500 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001501 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001502 res = 0;
1503 PyErr_Format(parser_error,
1504 "Unrecognized child node of small_stmt: %d.",
1505 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001506 }
1507 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001508}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001509
1510
1511/* compound_stmt:
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00001512 * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
Guido van Rossum3d602e31996-07-21 02:33:56 +00001513 */
Guido van Rossum47478871996-08-21 14:32:37 +00001514static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001515validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001516{
1517 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001518 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001519 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001520
1521 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001522 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001523
1524 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001525 ntype = TYPE(tree);
1526 if ( (ntype == if_stmt)
1527 || (ntype == while_stmt)
1528 || (ntype == for_stmt)
1529 || (ntype == try_stmt)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00001530 || (ntype == with_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001531 || (ntype == funcdef)
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001532 || (ntype == classdef)
1533 || (ntype == decorated))
Fred Drakeff9ea482000-04-19 13:54:15 +00001534 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001535 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001536 res = 0;
1537 PyErr_Format(parser_error,
1538 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001539 }
1540 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001541}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001542
Guido van Rossum47478871996-08-21 14:32:37 +00001543static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00001544validate_yield_or_testlist(node *tree, int tse)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001545{
Benjamin Peterson4905e802009-09-27 02:43:28 +00001546 if (TYPE(tree) == yield_expr) {
1547 return validate_yield_expr(tree);
1548 }
1549 else {
1550 if (tse)
1551 return validate_testlist_star_expr(tree);
1552 else
1553 return validate_testlist(tree);
1554 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001555}
1556
1557static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001558validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001559{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001560 int j;
1561 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001562 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001563 && is_odd(nch)
Benjamin Peterson4905e802009-09-27 02:43:28 +00001564 && validate_testlist_star_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001565
Fred Drake28f739a2000-08-25 22:42:40 +00001566 if (res && nch == 3
1567 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001568 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
Benjamin Peterson4905e802009-09-27 02:43:28 +00001569 && validate_yield_or_testlist(CHILD(tree, 2), 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001570
Fred Drake28f739a2000-08-25 22:42:40 +00001571 if (res) {
1572 char *s = STR(CHILD(CHILD(tree, 1), 0));
1573
1574 res = (strcmp(s, "+=") == 0
1575 || strcmp(s, "-=") == 0
1576 || strcmp(s, "*=") == 0
1577 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001578 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001579 || strcmp(s, "%=") == 0
1580 || strcmp(s, "&=") == 0
1581 || strcmp(s, "|=") == 0
1582 || strcmp(s, "^=") == 0
1583 || strcmp(s, "<<=") == 0
1584 || strcmp(s, ">>=") == 0
1585 || strcmp(s, "**=") == 0);
1586 if (!res)
Ezio Melotti42da6632011-03-15 05:18:48 +02001587 err_string("illegal augmented assignment operator");
Fred Drake28f739a2000-08-25 22:42:40 +00001588 }
1589 }
1590 else {
1591 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001592 res = validate_equal(CHILD(tree, j))
Benjamin Peterson4905e802009-09-27 02:43:28 +00001593 && validate_yield_or_testlist(CHILD(tree, j + 1), 1);
Fred Drake28f739a2000-08-25 22:42:40 +00001594 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001595 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001596}
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_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001601{
1602 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001603 && validate_name(CHILD(tree, 0), "del")
1604 && validate_exprlist(CHILD(tree, 1)));
1605}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001606
1607
Guido van Rossum47478871996-08-21 14:32:37 +00001608static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001609validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001610{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001611 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001612 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001613 && ((nch == 1) || (nch == 2))
1614 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001615
Guido van Rossum3d602e31996-07-21 02:33:56 +00001616 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001617 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001618
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001619 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001620}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001621
1622
Mark Dickinsoncf360b92012-05-07 12:01:27 +01001623/*
1624 * raise_stmt:
1625 *
1626 * 'raise' [test ['from' test]]
1627 */
Guido van Rossum47478871996-08-21 14:32:37 +00001628static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001629validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001630{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001631 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001632 int res = (validate_ntype(tree, raise_stmt)
Mark Dickinsoncf360b92012-05-07 12:01:27 +01001633 && ((nch == 1) || (nch == 2) || (nch == 4)));
1634
1635 if (!res && !PyErr_Occurred())
1636 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001637
Guido van Rossum3d602e31996-07-21 02:33:56 +00001638 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001639 res = validate_name(CHILD(tree, 0), "raise");
1640 if (res && (nch >= 2))
1641 res = validate_test(CHILD(tree, 1));
Mark Dickinsoncf360b92012-05-07 12:01:27 +01001642 if (res && (nch == 4)) {
1643 res = (validate_name(CHILD(tree, 2), "from")
Fred Drakeff9ea482000-04-19 13:54:15 +00001644 && validate_test(CHILD(tree, 3)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001645 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001646 }
1647 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001648}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001649
1650
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001651/* yield_expr: 'yield' [yield_arg]
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001652 */
1653static int
1654validate_yield_expr(node *tree)
1655{
1656 int nch = NCH(tree);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001657 if (nch < 1 || nch > 2)
1658 return 0;
1659 if (!validate_ntype(tree, yield_expr))
1660 return 0;
1661 if (!validate_name(CHILD(tree, 0), "yield"))
1662 return 0;
1663 if (nch == 2) {
1664 if (!validate_yield_arg(CHILD(tree, 1)))
1665 return 0;
1666 }
1667 return 1;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001668}
1669
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001670/* yield_arg: 'from' test | testlist
1671 */
1672static int
1673validate_yield_arg(node *tree)
1674{
1675 int nch = NCH(tree);
1676 if (!validate_ntype(tree, yield_arg))
1677 return 0;
1678 switch (nch) {
1679 case 1:
1680 if (!validate_testlist(CHILD(tree, nch - 1)))
1681 return 0;
1682 break;
1683 case 2:
1684 if (!validate_name(CHILD(tree, 0), "from"))
1685 return 0;
1686 if (!validate_test(CHILD(tree, 1)))
1687 return 0;
1688 break;
1689 default:
1690 return 0;
1691 }
1692 return 1;
1693}
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001694
1695/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001696 */
1697static int
1698validate_yield_stmt(node *tree)
1699{
1700 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001701 && validate_numnodes(tree, 1, "yield_stmt")
1702 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001703}
1704
1705
Fred Drakecff283c2000-08-21 22:24:43 +00001706static int
1707validate_import_as_name(node *tree)
1708{
1709 int nch = NCH(tree);
1710 int ok = validate_ntype(tree, import_as_name);
1711
1712 if (ok) {
1713 if (nch == 1)
1714 ok = validate_name(CHILD(tree, 0), NULL);
1715 else if (nch == 3)
1716 ok = (validate_name(CHILD(tree, 0), NULL)
1717 && validate_name(CHILD(tree, 1), "as")
1718 && validate_name(CHILD(tree, 2), NULL));
1719 else
1720 ok = validate_numnodes(tree, 3, "import_as_name");
1721 }
1722 return ok;
1723}
1724
1725
Fred Drake71137082001-01-07 05:59:59 +00001726/* dotted_name: NAME ("." NAME)*
1727 */
1728static int
1729validate_dotted_name(node *tree)
1730{
1731 int nch = NCH(tree);
1732 int res = (validate_ntype(tree, dotted_name)
1733 && is_odd(nch)
1734 && validate_name(CHILD(tree, 0), NULL));
1735 int i;
1736
1737 for (i = 1; res && (i < nch); i += 2) {
1738 res = (validate_dot(CHILD(tree, i))
1739 && validate_name(CHILD(tree, i+1), NULL));
1740 }
1741 return res;
1742}
1743
1744
Fred Drakecff283c2000-08-21 22:24:43 +00001745/* dotted_as_name: dotted_name [NAME NAME]
1746 */
1747static int
1748validate_dotted_as_name(node *tree)
1749{
1750 int nch = NCH(tree);
1751 int res = validate_ntype(tree, dotted_as_name);
1752
1753 if (res) {
1754 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001755 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001756 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001757 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001758 && validate_name(CHILD(tree, 1), "as")
1759 && validate_name(CHILD(tree, 2), NULL));
1760 else {
1761 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001762 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001763 }
1764 }
1765 return res;
1766}
1767
1768
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001769/* dotted_as_name (',' dotted_as_name)* */
1770static int
1771validate_dotted_as_names(node *tree)
1772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 int nch = NCH(tree);
1774 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1775 int i;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 for (i = 1; res && (i < nch); i += 2)
1778 res = (validate_comma(CHILD(tree, i))
1779 && validate_dotted_as_name(CHILD(tree, i + 1)));
1780 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001781}
1782
1783
1784/* import_as_name (',' import_as_name)* [','] */
1785static int
1786validate_import_as_names(node *tree)
1787{
1788 int nch = NCH(tree);
1789 int res = validate_import_as_name(CHILD(tree, 0));
1790 int i;
1791
1792 for (i = 1; res && (i + 1 < nch); i += 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 res = (validate_comma(CHILD(tree, i))
1794 && validate_import_as_name(CHILD(tree, i + 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001795 return (res);
1796}
1797
1798
1799/* 'import' dotted_as_names */
1800static int
1801validate_import_name(node *tree)
1802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 return (validate_ntype(tree, import_name)
1804 && validate_numnodes(tree, 2, "import_name")
1805 && validate_name(CHILD(tree, 0), "import")
1806 && validate_dotted_as_names(CHILD(tree, 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001807}
1808
Mark Dickinsonfeb3b752010-07-04 18:38:57 +00001809/* Helper function to count the number of leading dots (or ellipsis tokens) in
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001810 * 'from ...module import name'
1811 */
1812static int
1813count_from_dots(node *tree)
1814{
Mark Dickinsonfeb3b752010-07-04 18:38:57 +00001815 int i;
1816 for (i = 1; i < NCH(tree); i++)
1817 if (TYPE(CHILD(tree, i)) != DOT && TYPE(CHILD(tree, i)) != ELLIPSIS)
1818 break;
1819 return i - 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001820}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001821
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +00001822/* import_from: ('from' ('.'* dotted_name | '.'+)
1823 * 'import' ('*' | '(' import_as_names ')' | import_as_names))
Guido van Rossum3d602e31996-07-21 02:33:56 +00001824 */
Guido van Rossum47478871996-08-21 14:32:37 +00001825static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001826validate_import_from(node *tree)
1827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 int nch = NCH(tree);
1829 int ndots = count_from_dots(tree);
1830 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1831 int offset = ndots + havename;
1832 int res = validate_ntype(tree, import_from)
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +00001833 && (offset >= 1)
1834 && (nch >= 3 + offset)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 && validate_name(CHILD(tree, 0), "from")
1836 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1837 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1840 res = ((nch == offset + 5)
1841 && validate_lparen(CHILD(tree, offset + 2))
1842 && validate_import_as_names(CHILD(tree, offset + 3))
1843 && validate_rparen(CHILD(tree, offset + 4)));
1844 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1845 res = validate_import_as_names(CHILD(tree, offset + 2));
1846 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001847}
1848
1849
1850/* import_stmt: import_name | import_from */
1851static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001852validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001853{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001854 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001855 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001856
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001857 if (res) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 if (ntype == import_name || ntype == import_from)
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001861 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001862 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001863 res = 0;
1864 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001865 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001866 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001867 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001868 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001869 PyErr_Format(parser_error,
1870 "Unrecognized child node of import_stmt: %d.",
1871 TYPE(CHILD(tree, 0)));
1872 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001873 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001874}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001875
1876
Mark Dickinson407b3bd2012-04-29 22:18:31 +01001877/* global_stmt:
1878 *
1879 * 'global' NAME (',' NAME)*
1880 */
Guido van Rossum47478871996-08-21 14:32:37 +00001881static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001882validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001883{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001884 int j;
1885 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001886 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001887 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001888
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001889 if (!res && !PyErr_Occurred())
1890 err_string("illegal global statement");
1891
Guido van Rossum3d602e31996-07-21 02:33:56 +00001892 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001893 res = (validate_name(CHILD(tree, 0), "global")
1894 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001895 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001896 res = (validate_comma(CHILD(tree, j))
1897 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001898
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001899 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001900}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001901
Mark Dickinson407b3bd2012-04-29 22:18:31 +01001902/* nonlocal_stmt:
1903 *
1904 * 'nonlocal' NAME (',' NAME)*
1905 */
1906static int
1907validate_nonlocal_stmt(node *tree)
1908{
1909 int j;
1910 int nch = NCH(tree);
1911 int res = (validate_ntype(tree, nonlocal_stmt)
1912 && is_even(nch) && (nch >= 2));
1913
1914 if (!res && !PyErr_Occurred())
1915 err_string("illegal nonlocal statement");
1916
1917 if (res)
1918 res = (validate_name(CHILD(tree, 0), "nonlocal")
1919 && validate_ntype(CHILD(tree, 1), NAME));
1920 for (j = 2; res && (j < nch); j += 2)
1921 res = (validate_comma(CHILD(tree, j))
1922 && validate_ntype(CHILD(tree, j + 1), NAME));
1923
1924 return res;
1925}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001926
Guido van Rossum925e5471997-04-02 05:32:13 +00001927/* assert_stmt:
1928 *
1929 * 'assert' test [',' test]
1930 */
1931static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001932validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001933{
1934 int nch = NCH(tree);
1935 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001936 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001937 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001938 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001939
1940 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001941 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001942 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001943 res = (validate_comma(CHILD(tree, 2))
1944 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001945
1946 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001947}
Guido van Rossum925e5471997-04-02 05:32:13 +00001948
1949
Guido van Rossum47478871996-08-21 14:32:37 +00001950static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001951validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001952{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001953 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001954 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001955 && ((nch == 4) || (nch == 7))
1956 && validate_name(CHILD(tree, 0), "while")
1957 && validate_test(CHILD(tree, 1))
1958 && validate_colon(CHILD(tree, 2))
1959 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001960
Guido van Rossum3d602e31996-07-21 02:33:56 +00001961 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001962 res = (validate_name(CHILD(tree, 4), "else")
1963 && validate_colon(CHILD(tree, 5))
1964 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001965
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001966 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001967}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001968
1969
Guido van Rossum47478871996-08-21 14:32:37 +00001970static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001971validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001972{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001973 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001974 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001975 && ((nch == 6) || (nch == 9))
1976 && validate_name(CHILD(tree, 0), "for")
1977 && validate_exprlist(CHILD(tree, 1))
1978 && validate_name(CHILD(tree, 2), "in")
1979 && validate_testlist(CHILD(tree, 3))
1980 && validate_colon(CHILD(tree, 4))
1981 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001982
Guido van Rossum3d602e31996-07-21 02:33:56 +00001983 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001984 res = (validate_name(CHILD(tree, 6), "else")
1985 && validate_colon(CHILD(tree, 7))
1986 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001987
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001988 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001989}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001990
1991
Guido van Rossum3d602e31996-07-21 02:33:56 +00001992/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001993 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Georg Brandleee31162008-12-07 15:15:22 +00001994 ['finally' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001995 * | 'try' ':' suite 'finally' ':' suite
1996 *
1997 */
Guido van Rossum47478871996-08-21 14:32:37 +00001998static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001999validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002000{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002001 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002002 int pos = 3;
2003 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002004 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002005
2006 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002007 res = (validate_name(CHILD(tree, 0), "try")
2008 && validate_colon(CHILD(tree, 1))
2009 && validate_suite(CHILD(tree, 2))
2010 && validate_colon(CHILD(tree, nch - 2))
2011 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00002012 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00002013 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00002014 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
2015 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00002016
2017 PyErr_Format(parser_error,
2018 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002019 }
Georg Brandleee31162008-12-07 15:15:22 +00002020 /* Handle try/finally statement */
2021 if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
2022 (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
2023 res = (validate_numnodes(tree, 6, "try/finally")
2024 && validate_colon(CHILD(tree, 4))
2025 && validate_suite(CHILD(tree, 5)));
2026 return (res);
2027 }
2028 /* try/except statement: skip past except_clause sections */
Antoine Pitrou37d1c182009-05-14 21:54:00 +00002029 while (res && pos < nch && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002030 res = (validate_except_clause(CHILD(tree, pos))
2031 && validate_colon(CHILD(tree, pos + 1))
2032 && validate_suite(CHILD(tree, pos + 2)));
2033 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002034 }
Georg Brandleee31162008-12-07 15:15:22 +00002035 /* skip else clause */
Antoine Pitrou37d1c182009-05-14 21:54:00 +00002036 if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) &&
Georg Brandleee31162008-12-07 15:15:22 +00002037 (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
2038 res = (validate_colon(CHILD(tree, pos + 1))
2039 && validate_suite(CHILD(tree, pos + 2)));
2040 pos += 3;
2041 }
2042 if (res && pos < nch) {
2043 /* last clause must be a finally */
2044 res = (validate_name(CHILD(tree, pos), "finally")
2045 && validate_numnodes(tree, pos + 3, "try/except/finally")
2046 && validate_colon(CHILD(tree, pos + 1))
2047 && validate_suite(CHILD(tree, pos + 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002048 }
2049 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002050}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002051
2052
Guido van Rossum47478871996-08-21 14:32:37 +00002053static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002054validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002055{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002056 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002057 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00002058 && ((nch == 1) || (nch == 2) || (nch == 4))
2059 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002060
Guido van Rossum3d602e31996-07-21 02:33:56 +00002061 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002062 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002063 if (res && (nch == 4))
Guido van Rossumb940e112007-01-10 16:19:56 +00002064 res = (validate_name(CHILD(tree, 2), "as")
2065 && validate_ntype(CHILD(tree, 3), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002066
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002067 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002068}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002069
2070
Guido van Rossum47478871996-08-21 14:32:37 +00002071static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002072validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002073{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002074 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002075 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002076
Guido van Rossum3d602e31996-07-21 02:33:56 +00002077 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002078 res = ((nch == 1)
2079 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002080 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002081 res = validate_or_test(CHILD(tree, 0));
2082 res = (res && (nch == 1 || (nch == 5 &&
2083 validate_name(CHILD(tree, 1), "if") &&
2084 validate_or_test(CHILD(tree, 2)) &&
2085 validate_name(CHILD(tree, 3), "else") &&
2086 validate_test(CHILD(tree, 4)))));
2087 }
2088 return (res);
2089}
2090
2091static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002092validate_test_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002093{
2094 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002095 int res = validate_ntype(tree, test_nocond) && (nch == 1);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002096
Nick Coghlan650f0d02007-04-15 12:05:43 +00002097 if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
2098 res = (validate_lambdef_nocond(CHILD(tree, 0)));
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002099 else if (res) {
2100 res = (validate_or_test(CHILD(tree, 0)));
2101 }
2102 return (res);
2103}
2104
2105static int
2106validate_or_test(node *tree)
2107{
2108 int nch = NCH(tree);
2109 int res = validate_ntype(tree, or_test) && is_odd(nch);
2110
2111 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002112 int pos;
2113 res = validate_and_test(CHILD(tree, 0));
2114 for (pos = 1; res && (pos < nch); pos += 2)
2115 res = (validate_name(CHILD(tree, pos), "or")
2116 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002117 }
2118 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002119}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002120
2121
Guido van Rossum47478871996-08-21 14:32:37 +00002122static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002123validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002124{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002125 int pos;
2126 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002127 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002128 && is_odd(nch)
2129 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002130
Guido van Rossum3d602e31996-07-21 02:33:56 +00002131 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002132 res = (validate_name(CHILD(tree, pos), "and")
2133 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002134
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002135 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002136}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002137
2138
Guido van Rossum47478871996-08-21 14:32:37 +00002139static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002140validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002141{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002142 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002143 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002144
Guido van Rossum3d602e31996-07-21 02:33:56 +00002145 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002146 if (nch == 2)
2147 res = (validate_name(CHILD(tree, 0), "not")
2148 && validate_not_test(CHILD(tree, 1)));
2149 else if (nch == 1)
2150 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002151 }
2152 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002153}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002154
2155
Guido van Rossum47478871996-08-21 14:32:37 +00002156static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002157validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002158{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002159 int pos;
2160 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002161 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002162 && is_odd(nch)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002163 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002164
Guido van Rossum3d602e31996-07-21 02:33:56 +00002165 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002166 res = (validate_comp_op(CHILD(tree, pos))
Benjamin Peterson4905e802009-09-27 02:43:28 +00002167 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002168
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002169 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002170}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002171
2172
Guido van Rossum47478871996-08-21 14:32:37 +00002173static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002174validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002175{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002176 int res = 0;
2177 int nch = NCH(tree);
2178
Guido van Rossum3d602e31996-07-21 02:33:56 +00002179 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002180 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002181 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002182 /*
2183 * Only child will be a terminal with a well-defined symbolic name
2184 * or a NAME with a string of either 'is' or 'in'
2185 */
2186 tree = CHILD(tree, 0);
2187 switch (TYPE(tree)) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002188 case LESS:
2189 case GREATER:
2190 case EQEQUAL:
2191 case EQUAL:
2192 case LESSEQUAL:
2193 case GREATEREQUAL:
2194 case NOTEQUAL:
Fred Drakeff9ea482000-04-19 13:54:15 +00002195 res = 1;
2196 break;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002197 case NAME:
Fred Drakeff9ea482000-04-19 13:54:15 +00002198 res = ((strcmp(STR(tree), "in") == 0)
2199 || (strcmp(STR(tree), "is") == 0));
2200 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002201 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002202 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002203 }
2204 break;
2205 default:
Fred Drake661ea262000-10-24 19:57:45 +00002206 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002207 break;
2208 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002209 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002210 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002211 res = (validate_ntype(CHILD(tree, 0), NAME)
2212 && validate_ntype(CHILD(tree, 1), NAME)
2213 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2214 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2215 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2216 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2217 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002218 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002219 }
2220 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002221}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002222
2223
Guido van Rossum47478871996-08-21 14:32:37 +00002224static int
Guido van Rossum0368b722007-05-11 16:50:42 +00002225validate_star_expr(node *tree)
2226{
2227 int res = validate_ntype(tree, star_expr);
2228 if (!res) return res;
Benjamin Peterson4905e802009-09-27 02:43:28 +00002229 if (!validate_numnodes(tree, 2, "star_expr"))
2230 return 0;
2231 return validate_ntype(CHILD(tree, 0), STAR) && \
2232 validate_expr(CHILD(tree, 1));
Guido van Rossum0368b722007-05-11 16:50:42 +00002233}
2234
2235
2236static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002237validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002238{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002239 int j;
2240 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002241 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002242 && is_odd(nch)
2243 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002244
Guido van Rossum3d602e31996-07-21 02:33:56 +00002245 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002246 res = (validate_xor_expr(CHILD(tree, j))
2247 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002248
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002249 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002250}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002251
2252
Guido van Rossum47478871996-08-21 14:32:37 +00002253static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002254validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002255{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002256 int j;
2257 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002258 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002259 && is_odd(nch)
2260 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002261
Guido van Rossum3d602e31996-07-21 02:33:56 +00002262 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002263 res = (validate_circumflex(CHILD(tree, j - 1))
2264 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002265
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002266 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002267}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002268
2269
Guido van Rossum47478871996-08-21 14:32:37 +00002270static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002271validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002272{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002273 int pos;
2274 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002275 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002276 && is_odd(nch)
2277 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002278
Guido van Rossum3d602e31996-07-21 02:33:56 +00002279 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002280 res = (validate_ampersand(CHILD(tree, pos))
2281 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002282
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002283 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002284}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002285
2286
2287static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002288validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002289 {
2290 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002291 int nch = NCH(tree);
2292 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002293 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002294
Guido van Rossum3d602e31996-07-21 02:33:56 +00002295 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002296 if (TYPE(CHILD(tree, pos)) != op1)
2297 res = validate_ntype(CHILD(tree, pos), op2);
2298 if (res)
2299 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002300 }
2301 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002302}
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_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002307{
2308 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002309 && validate_chain_two_ops(tree, validate_arith_expr,
2310 LEFTSHIFT, RIGHTSHIFT));
2311}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002312
2313
Guido van Rossum47478871996-08-21 14:32:37 +00002314static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002315validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002316{
2317 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002318 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2319}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002320
2321
Guido van Rossum47478871996-08-21 14:32:37 +00002322static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002323validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002324{
2325 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002326 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002327 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002328 && is_odd(nch)
2329 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002330
Guido van Rossum3d602e31996-07-21 02:33:56 +00002331 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002332 res = (((TYPE(CHILD(tree, pos)) == STAR)
2333 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002334 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002335 || (TYPE(CHILD(tree, pos)) == PERCENT))
2336 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002337
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002338 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002339}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002340
2341
Guido van Rossum3d602e31996-07-21 02:33:56 +00002342/* factor:
2343 *
2344 * factor: ('+'|'-'|'~') factor | power
2345 */
Guido van Rossum47478871996-08-21 14:32:37 +00002346static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002347validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002348{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002349 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002350 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002351 && (((nch == 2)
2352 && ((TYPE(CHILD(tree, 0)) == PLUS)
2353 || (TYPE(CHILD(tree, 0)) == MINUS)
2354 || (TYPE(CHILD(tree, 0)) == TILDE))
2355 && validate_factor(CHILD(tree, 1)))
2356 || ((nch == 1)
2357 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002358 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002359}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002360
2361
Guido van Rossum3d602e31996-07-21 02:33:56 +00002362/* power:
2363 *
2364 * power: atom trailer* ('**' factor)*
2365 */
Guido van Rossum47478871996-08-21 14:32:37 +00002366static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002367validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002368{
2369 int pos = 1;
2370 int nch = NCH(tree);
2371 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002372 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002373
2374 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002375 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002376 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002377 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002378 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002379 return (0);
2380 }
2381 for ( ; res && (pos < (nch - 1)); pos += 2)
2382 res = (validate_doublestar(CHILD(tree, pos))
2383 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002384 }
2385 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002386}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002387
2388
Guido van Rossum47478871996-08-21 14:32:37 +00002389static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002390validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002391{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002392 int pos;
2393 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002394 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002395
Fred Drakecff283c2000-08-21 22:24:43 +00002396 if (res && nch < 1)
2397 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002398 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002399 switch (TYPE(CHILD(tree, 0))) {
2400 case LPAR:
2401 res = ((nch <= 3)
2402 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002403
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002404 if (res && (nch == 3)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 if (TYPE(CHILD(tree, 1))==yield_expr)
2406 res = validate_yield_expr(CHILD(tree, 1));
2407 else
2408 res = validate_testlist_comp(CHILD(tree, 1));
2409 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002410 break;
2411 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002412 if (nch == 2)
2413 res = validate_ntype(CHILD(tree, 1), RSQB);
2414 else if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002415 res = (validate_testlist_comp(CHILD(tree, 1))
Fred Drakecff283c2000-08-21 22:24:43 +00002416 && validate_ntype(CHILD(tree, 2), RSQB));
2417 else {
2418 res = 0;
2419 err_string("illegal list display atom");
2420 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002421 break;
2422 case LBRACE:
2423 res = ((nch <= 3)
2424 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002425
Fred Drakeff9ea482000-04-19 13:54:15 +00002426 if (res && (nch == 3))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002427 res = validate_dictorsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002428 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002429 case NAME:
2430 case NUMBER:
Mark Dickinsonda029fb2012-05-07 17:24:04 +01002431 case ELLIPSIS:
Fred Drakeff9ea482000-04-19 13:54:15 +00002432 res = (nch == 1);
2433 break;
2434 case STRING:
2435 for (pos = 1; res && (pos < nch); ++pos)
2436 res = validate_ntype(CHILD(tree, pos), STRING);
2437 break;
2438 default:
2439 res = 0;
2440 break;
2441 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002442 }
2443 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002444}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002445
2446
Nick Coghlan650f0d02007-04-15 12:05:43 +00002447/* testlist_comp:
2448 * test ( comp_for | (',' test)* [','] )
Fred Drake85bf3bb2000-08-23 15:35:26 +00002449 */
Fred Drakecff283c2000-08-21 22:24:43 +00002450static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002451validate_testlist_comp(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00002452{
2453 int nch = NCH(tree);
2454 int ok = nch;
2455
2456 if (nch == 0)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002457 err_string("missing child nodes of testlist_comp");
2458 else {
Benjamin Peterson4905e802009-09-27 02:43:28 +00002459 ok = validate_test_or_star_expr(CHILD(tree, 0));
Nick Coghlan650f0d02007-04-15 12:05:43 +00002460 }
Fred Drakecff283c2000-08-21 22:24:43 +00002461
2462 /*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002463 * comp_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002464 */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002465 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2466 ok = validate_comp_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002467 else {
2468 /* (',' test)* [','] */
2469 int i = 1;
2470 while (ok && nch - i >= 2) {
2471 ok = (validate_comma(CHILD(tree, i))
Benjamin Peterson4905e802009-09-27 02:43:28 +00002472 && validate_test_or_star_expr(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002473 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002474 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002475 if (ok && i == nch-1)
2476 ok = validate_comma(CHILD(tree, i));
2477 else if (i != nch) {
2478 ok = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002479 err_string("illegal trailing nodes for testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002480 }
2481 }
2482 return ok;
2483}
Fred Drakecff283c2000-08-21 22:24:43 +00002484
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002485/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002486 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002487 */
2488static int
2489validate_decorator(node *tree)
2490{
2491 int ok;
2492 int nch = NCH(tree);
2493 ok = (validate_ntype(tree, decorator) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 (nch == 3 || nch == 5 || nch == 6) &&
2495 validate_at(CHILD(tree, 0)) &&
2496 validate_dotted_name(CHILD(tree, 1)) &&
2497 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002498
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002499 if (ok && nch != 3) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 ok = (validate_lparen(CHILD(tree, 2)) &&
2501 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 if (ok && nch == 6)
2504 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002505 }
2506
2507 return ok;
2508}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002509
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002510/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002511 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002512 */
2513static int
2514validate_decorators(node *tree)
2515{
Guido van Rossumfc158e22007-11-15 19:17:28 +00002516 int i, nch, ok;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002517 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002518 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002519
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002520 for (i = 0; ok && i < nch; ++i)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002522
2523 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002524}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002525
Georg Brandl0c315622009-05-25 21:10:36 +00002526/* with_item:
2527 * test ['as' expr]
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002528 */
2529static int
Georg Brandl0c315622009-05-25 21:10:36 +00002530validate_with_item(node *tree)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002531{
2532 int nch = NCH(tree);
Georg Brandl0c315622009-05-25 21:10:36 +00002533 int ok = (validate_ntype(tree, with_item)
2534 && (nch == 1 || nch == 3)
2535 && validate_test(CHILD(tree, 0)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 if (ok && nch == 3)
Georg Brandl0c315622009-05-25 21:10:36 +00002537 ok = (validate_name(CHILD(tree, 1), "as")
2538 && validate_expr(CHILD(tree, 2)));
2539 return ok;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002540}
2541
Georg Brandl0c315622009-05-25 21:10:36 +00002542/* with_stmt:
2543 * 0 1 ... -2 -1
2544 * 'with' with_item (',' with_item)* ':' suite
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002545 */
2546static int
2547validate_with_stmt(node *tree)
2548{
Georg Brandl0c315622009-05-25 21:10:36 +00002549 int i;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002550 int nch = NCH(tree);
2551 int ok = (validate_ntype(tree, with_stmt)
Georg Brandl0c315622009-05-25 21:10:36 +00002552 && (nch % 2 == 0)
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002553 && validate_name(CHILD(tree, 0), "with")
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002554 && validate_colon(RCHILD(tree, -2))
2555 && validate_suite(RCHILD(tree, -1)));
Georg Brandl0c315622009-05-25 21:10:36 +00002556 for (i = 1; ok && i < nch - 2; i += 2)
2557 ok = validate_with_item(CHILD(tree, i));
2558 return ok;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00002559}
2560
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01002561/* funcdef: 'def' NAME parameters ['->' test] ':' suite */
2562
Guido van Rossum47478871996-08-21 14:32:37 +00002563static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002564validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002565{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002566 int nch = NCH(tree);
Mark Dickinsonea7e9f92012-04-29 18:34:40 +01002567 int res = validate_ntype(tree, funcdef);
2568 if (res) {
2569 if (nch == 5) {
2570 res = (validate_name(CHILD(tree, 0), "def")
2571 && validate_ntype(CHILD(tree, 1), NAME)
2572 && validate_parameters(CHILD(tree, 2))
2573 && validate_colon(CHILD(tree, 3))
2574 && validate_suite(CHILD(tree, 4)));
2575 }
2576 else if (nch == 7) {
2577 res = (validate_name(CHILD(tree, 0), "def")
2578 && validate_ntype(CHILD(tree, 1), NAME)
2579 && validate_parameters(CHILD(tree, 2))
2580 && validate_rarrow(CHILD(tree, 3))
2581 && validate_test(CHILD(tree, 4))
2582 && validate_colon(CHILD(tree, 5))
2583 && validate_suite(CHILD(tree, 6)));
2584 }
2585 else {
2586 res = 0;
2587 err_string("illegal number of children for funcdef");
2588 }
2589 }
2590 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00002591}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002592
2593
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002594/* decorated
2595 * decorators (classdef | funcdef)
2596 */
2597static int
2598validate_decorated(node *tree)
2599{
Mark Dickinson2bd61a92010-07-04 16:37:31 +00002600 int nch = NCH(tree);
2601 int ok = (validate_ntype(tree, decorated)
2602 && (nch == 2)
2603 && validate_decorators(RCHILD(tree, -2)));
2604 if (TYPE(RCHILD(tree, -1)) == funcdef)
2605 ok = ok && validate_funcdef(RCHILD(tree, -1));
2606 else
2607 ok = ok && validate_class(RCHILD(tree, -1));
2608 return ok;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002609}
2610
Guido van Rossum47478871996-08-21 14:32:37 +00002611static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002612validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002613{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002614 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002615 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002616 && ((nch == 3) || (nch == 4))
2617 && validate_name(CHILD(tree, 0), "lambda")
2618 && validate_colon(CHILD(tree, nch - 2))
2619 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002620
Guido van Rossum3d602e31996-07-21 02:33:56 +00002621 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002622 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002623 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002624 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002625
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002626 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002627}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002628
2629
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002630static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002631validate_lambdef_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002632{
2633 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002634 int res = (validate_ntype(tree, lambdef_nocond)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002635 && ((nch == 3) || (nch == 4))
2636 && validate_name(CHILD(tree, 0), "lambda")
2637 && validate_colon(CHILD(tree, nch - 2))
2638 && validate_test(CHILD(tree, nch - 1)));
2639
2640 if (res && (nch == 4))
2641 res = validate_varargslist(CHILD(tree, 1));
2642 else if (!res && !PyErr_Occurred())
Nick Coghlan650f0d02007-04-15 12:05:43 +00002643 (void) validate_numnodes(tree, 3, "lambdef_nocond");
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002644
2645 return (res);
2646}
2647
2648
Guido van Rossum3d602e31996-07-21 02:33:56 +00002649/* arglist:
2650 *
Fred Drakecff283c2000-08-21 22:24:43 +00002651 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002652 */
Guido van Rossum47478871996-08-21 14:32:37 +00002653static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002654validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002655{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002656 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002657 int i = 0;
2658 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002659
2660 if (nch <= 0)
2661 /* raise the right error from having an invalid number of children */
2662 return validate_numnodes(tree, nch + 1, "arglist");
2663
Raymond Hettinger354433a2004-05-19 08:20:33 +00002664 if (nch > 1) {
2665 for (i=0; i<nch; i++) {
2666 if (TYPE(CHILD(tree, i)) == argument) {
2667 node *ch = CHILD(tree, i);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002668 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
Raymond Hettinger354433a2004-05-19 08:20:33 +00002669 err_string("need '(', ')' for generator expression");
2670 return 0;
2671 }
2672 }
2673 }
2674 }
2675
Fred Drakecff283c2000-08-21 22:24:43 +00002676 while (ok && nch-i >= 2) {
2677 /* skip leading (argument ',') */
2678 ok = (validate_argument(CHILD(tree, i))
2679 && validate_comma(CHILD(tree, i+1)));
2680 if (ok)
2681 i += 2;
2682 else
2683 PyErr_Clear();
2684 }
2685 ok = 1;
2686 if (nch-i > 0) {
2687 /*
2688 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002689 */
Fred Drakecff283c2000-08-21 22:24:43 +00002690 int sym = TYPE(CHILD(tree, i));
2691
2692 if (sym == argument) {
2693 ok = validate_argument(CHILD(tree, i));
2694 if (ok && i+1 != nch) {
2695 err_string("illegal arglist specification"
2696 " (extra stuff on end)");
2697 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002698 }
Fred Drakecff283c2000-08-21 22:24:43 +00002699 }
2700 else if (sym == STAR) {
2701 ok = validate_star(CHILD(tree, i));
2702 if (ok && (nch-i == 2))
2703 ok = validate_test(CHILD(tree, i+1));
2704 else if (ok && (nch-i == 5))
2705 ok = (validate_test(CHILD(tree, i+1))
2706 && validate_comma(CHILD(tree, i+2))
2707 && validate_doublestar(CHILD(tree, i+3))
2708 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002709 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002710 err_string("illegal use of '*' in arglist");
2711 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002712 }
2713 }
Fred Drakecff283c2000-08-21 22:24:43 +00002714 else if (sym == DOUBLESTAR) {
2715 if (nch-i == 2)
2716 ok = (validate_doublestar(CHILD(tree, i))
2717 && validate_test(CHILD(tree, i+1)));
2718 else {
2719 err_string("illegal use of '**' in arglist");
2720 ok = 0;
2721 }
2722 }
2723 else {
2724 err_string("illegal arglist specification");
2725 ok = 0;
2726 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002727 }
2728 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002729}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002730
2731
2732
2733/* argument:
2734 *
Nick Coghlan650f0d02007-04-15 12:05:43 +00002735 * [test '='] test [comp_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002736 */
Guido van Rossum47478871996-08-21 14:32:37 +00002737static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002738validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002739{
2740 int nch = NCH(tree);
2741 int res = (validate_ntype(tree, argument)
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002742 && ((nch == 1) || (nch == 2) || (nch == 3)));
2743 if (res)
2744 res = validate_test(CHILD(tree, 0));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002745 if (res && (nch == 2))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002746 res = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002747 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002748 res = (validate_equal(CHILD(tree, 1))
2749 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002750
2751 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002752}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002753
2754
2755
2756/* trailer:
2757 *
Guido van Rossum47478871996-08-21 14:32:37 +00002758 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002759 */
Guido van Rossum47478871996-08-21 14:32:37 +00002760static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002761validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002762{
2763 int nch = NCH(tree);
2764 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002765
2766 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002767 switch (TYPE(CHILD(tree, 0))) {
2768 case LPAR:
2769 res = validate_rparen(CHILD(tree, nch - 1));
2770 if (res && (nch == 3))
2771 res = validate_arglist(CHILD(tree, 1));
2772 break;
2773 case LSQB:
2774 res = (validate_numnodes(tree, 3, "trailer")
2775 && validate_subscriptlist(CHILD(tree, 1))
2776 && validate_ntype(CHILD(tree, 2), RSQB));
2777 break;
2778 case DOT:
2779 res = (validate_numnodes(tree, 2, "trailer")
2780 && validate_ntype(CHILD(tree, 1), NAME));
2781 break;
2782 default:
2783 res = 0;
2784 break;
2785 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002786 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002787 else {
2788 (void) validate_numnodes(tree, 2, "trailer");
2789 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002790 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002791}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002792
2793
Guido van Rossum47478871996-08-21 14:32:37 +00002794/* subscriptlist:
2795 *
2796 * subscript (',' subscript)* [',']
2797 */
2798static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002799validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002800{
2801 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002802 validate_subscript, "subscriptlist"));
2803}
Guido van Rossum47478871996-08-21 14:32:37 +00002804
2805
Guido van Rossum3d602e31996-07-21 02:33:56 +00002806/* subscript:
2807 *
Guido van Rossum47478871996-08-21 14:32:37 +00002808 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002809 */
Guido van Rossum47478871996-08-21 14:32:37 +00002810static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002811validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002812{
Guido van Rossum47478871996-08-21 14:32:37 +00002813 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002814 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002815 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002816
Guido van Rossum47478871996-08-21 14:32:37 +00002817 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002818 if (!PyErr_Occurred())
2819 err_string("invalid number of arguments for subscript node");
2820 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002821 }
Guido van Rossum47478871996-08-21 14:32:37 +00002822 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002823 /* take care of ('.' '.' '.') possibility */
2824 return (validate_numnodes(tree, 3, "subscript")
2825 && validate_dot(CHILD(tree, 0))
2826 && validate_dot(CHILD(tree, 1))
2827 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002828 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002829 if (TYPE(CHILD(tree, 0)) == test)
2830 res = validate_test(CHILD(tree, 0));
2831 else
2832 res = validate_colon(CHILD(tree, 0));
2833 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002834 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002835 /* Must be [test] ':' [test] [sliceop],
2836 * but at least one of the optional components will
2837 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002838 */
2839 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002840 res = validate_test(CHILD(tree, 0));
2841 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002842 }
2843 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002844 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002845 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002846 int rem = nch - ++offset;
2847 if (rem) {
2848 if (TYPE(CHILD(tree, offset)) == test) {
2849 res = validate_test(CHILD(tree, offset));
2850 ++offset;
2851 --rem;
2852 }
2853 if (res && rem)
2854 res = validate_sliceop(CHILD(tree, offset));
2855 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002856 }
2857 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002858}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002859
2860
Guido van Rossum47478871996-08-21 14:32:37 +00002861static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002862validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002863{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002864 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002865 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002866 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002867 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002868 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002869 }
Guido van Rossum47478871996-08-21 14:32:37 +00002870 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002871 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002872 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002873 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002874
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002875 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002876}
Guido van Rossum47478871996-08-21 14:32:37 +00002877
2878
2879static int
Benjamin Peterson4905e802009-09-27 02:43:28 +00002880validate_test_or_star_expr(node *n)
2881{
2882 if (TYPE(n) == test)
2883 return validate_test(n);
2884 return validate_star_expr(n);
2885}
2886
2887static int
2888validate_expr_or_star_expr(node *n)
2889{
2890 if (TYPE(n) == expr)
2891 return validate_expr(n);
2892 return validate_star_expr(n);
2893}
2894
2895
2896static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002897validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002898{
2899 return (validate_repeating_list(tree, exprlist,
Benjamin Peterson4905e802009-09-27 02:43:28 +00002900 validate_expr_or_star_expr, "exprlist"));
Fred Drakeff9ea482000-04-19 13:54:15 +00002901}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002902
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002903/*
2904 * dictorsetmaker:
2905 *
2906 * (test ':' test (comp_for | (',' test ':' test)* [','])) |
2907 * (test (comp_for | (',' test)* [',']))
2908 */
Guido van Rossum47478871996-08-21 14:32:37 +00002909static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002910validate_dictorsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002911{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002912 int nch = NCH(tree);
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002913 int res;
2914 int i = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002915
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002916 res = validate_ntype(tree, dictorsetmaker);
2917 if (!res)
2918 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002919
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002920 if (nch - i < 1) {
2921 (void) validate_numnodes(tree, 1, "dictorsetmaker");
2922 return 0;
2923 }
2924
2925 res = validate_test(CHILD(tree, i++));
2926 if (!res)
2927 return 0;
2928
2929 if (nch - i >= 2 && TYPE(CHILD(tree, i)) == COLON) {
2930 /* Dictionary display or dictionary comprehension. */
2931 res = (validate_colon(CHILD(tree, i++))
2932 && validate_test(CHILD(tree, i++)));
2933 if (!res)
2934 return 0;
2935
2936 if (nch - i >= 1 && TYPE(CHILD(tree, i)) == comp_for) {
2937 /* Dictionary comprehension. */
2938 res = validate_comp_for(CHILD(tree, i++));
2939 if (!res)
2940 return 0;
2941 }
2942 else {
2943 /* Dictionary display. */
2944 while (nch - i >= 4) {
2945 res = (validate_comma(CHILD(tree, i++))
2946 && validate_test(CHILD(tree, i++))
2947 && validate_colon(CHILD(tree, i++))
2948 && validate_test(CHILD(tree, i++)));
2949 if (!res)
2950 return 0;
2951 }
2952 if (nch - i == 1) {
2953 res = validate_comma(CHILD(tree, i++));
2954 if (!res)
2955 return 0;
2956 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002957 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002958 }
Mark Dickinson11c1dee2012-05-07 16:34:34 +01002959 else {
2960 /* Set display or set comprehension. */
2961 if (nch - i >= 1 && TYPE(CHILD(tree, i)) == comp_for) {
2962 /* Set comprehension. */
2963 res = validate_comp_for(CHILD(tree, i++));
2964 if (!res)
2965 return 0;
2966 }
2967 else {
2968 /* Set display. */
2969 while (nch - i >= 2) {
2970 res = (validate_comma(CHILD(tree, i++))
2971 && validate_test(CHILD(tree, i++)));
2972 if (!res)
2973 return 0;
2974 }
2975 if (nch - i == 1) {
2976 res = validate_comma(CHILD(tree, i++));
2977 if (!res)
2978 return 0;
2979 }
2980 }
2981 }
2982
2983 if (nch - i > 0) {
2984 err_string("Illegal trailing nodes for dictorsetmaker.");
2985 return 0;
2986 }
2987
2988 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00002989}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002990
2991
Guido van Rossum47478871996-08-21 14:32:37 +00002992static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002993validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002994{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002995 int pos;
2996 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002997 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002998 && (nch >= 2)
2999 && validate_testlist(CHILD(tree, 0))
3000 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003001
Guido van Rossum3d602e31996-07-21 02:33:56 +00003002 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00003003 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003004
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003005 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003006}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003007
3008
Guido van Rossum47478871996-08-21 14:32:37 +00003009static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003010validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003011{
Fred Drakeff9ea482000-04-19 13:54:15 +00003012 int nch = 0; /* num. children on current node */
3013 int res = 1; /* result value */
3014 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00003015
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00003016 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003017 nch = NCH(tree);
3018 next = 0;
3019 switch (TYPE(tree)) {
3020 /*
3021 * Definition nodes.
3022 */
3023 case funcdef:
3024 res = validate_funcdef(tree);
3025 break;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00003026 case with_stmt:
3027 res = validate_with_stmt(tree);
3028 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003029 case classdef:
3030 res = validate_class(tree);
3031 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 case decorated:
3033 res = validate_decorated(tree);
3034 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003035 /*
3036 * "Trivial" parse tree nodes.
3037 * (Why did I call these trivial?)
3038 */
3039 case stmt:
3040 res = validate_stmt(tree);
3041 break;
3042 case small_stmt:
3043 /*
Mark Dickinson407b3bd2012-04-29 22:18:31 +01003044 * expr_stmt | del_stmt | pass_stmt | flow_stmt |
3045 * import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00003046 */
3047 res = validate_small_stmt(tree);
3048 break;
3049 case flow_stmt:
3050 res = (validate_numnodes(tree, 1, "flow_stmt")
3051 && ((TYPE(CHILD(tree, 0)) == break_stmt)
3052 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00003053 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00003054 || (TYPE(CHILD(tree, 0)) == return_stmt)
3055 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
3056 if (res)
3057 next = CHILD(tree, 0);
3058 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00003059 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003060 break;
Fred Drake02126f22001-07-17 02:59:15 +00003061 case yield_stmt:
3062 res = validate_yield_stmt(tree);
3063 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003064 /*
3065 * Compound statements.
3066 */
3067 case simple_stmt:
3068 res = validate_simple_stmt(tree);
3069 break;
3070 case compound_stmt:
3071 res = validate_compound_stmt(tree);
3072 break;
3073 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00003074 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00003075 */
3076 case expr_stmt:
3077 res = validate_expr_stmt(tree);
3078 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003079 case del_stmt:
3080 res = validate_del_stmt(tree);
3081 break;
3082 case pass_stmt:
3083 res = (validate_numnodes(tree, 1, "pass")
3084 && validate_name(CHILD(tree, 0), "pass"));
3085 break;
3086 case break_stmt:
3087 res = (validate_numnodes(tree, 1, "break")
3088 && validate_name(CHILD(tree, 0), "break"));
3089 break;
3090 case continue_stmt:
3091 res = (validate_numnodes(tree, 1, "continue")
3092 && validate_name(CHILD(tree, 0), "continue"));
3093 break;
3094 case return_stmt:
3095 res = validate_return_stmt(tree);
3096 break;
3097 case raise_stmt:
3098 res = validate_raise_stmt(tree);
3099 break;
3100 case import_stmt:
3101 res = validate_import_stmt(tree);
3102 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 case import_name:
3104 res = validate_import_name(tree);
3105 break;
3106 case import_from:
3107 res = validate_import_from(tree);
3108 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003109 case global_stmt:
3110 res = validate_global_stmt(tree);
3111 break;
Mark Dickinson407b3bd2012-04-29 22:18:31 +01003112 case nonlocal_stmt:
3113 res = validate_nonlocal_stmt(tree);
3114 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003115 case assert_stmt:
3116 res = validate_assert_stmt(tree);
3117 break;
3118 case if_stmt:
3119 res = validate_if(tree);
3120 break;
3121 case while_stmt:
3122 res = validate_while(tree);
3123 break;
3124 case for_stmt:
3125 res = validate_for(tree);
3126 break;
3127 case try_stmt:
3128 res = validate_try(tree);
3129 break;
3130 case suite:
3131 res = validate_suite(tree);
3132 break;
3133 /*
3134 * Expression nodes.
3135 */
3136 case testlist:
3137 res = validate_testlist(tree);
3138 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003139 case yield_expr:
3140 res = validate_yield_expr(tree);
3141 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003142 case test:
3143 res = validate_test(tree);
3144 break;
3145 case and_test:
3146 res = validate_and_test(tree);
3147 break;
3148 case not_test:
3149 res = validate_not_test(tree);
3150 break;
3151 case comparison:
3152 res = validate_comparison(tree);
3153 break;
3154 case exprlist:
3155 res = validate_exprlist(tree);
3156 break;
3157 case comp_op:
3158 res = validate_comp_op(tree);
3159 break;
3160 case expr:
3161 res = validate_expr(tree);
3162 break;
3163 case xor_expr:
3164 res = validate_xor_expr(tree);
3165 break;
3166 case and_expr:
3167 res = validate_and_expr(tree);
3168 break;
3169 case shift_expr:
3170 res = validate_shift_expr(tree);
3171 break;
3172 case arith_expr:
3173 res = validate_arith_expr(tree);
3174 break;
3175 case term:
3176 res = validate_term(tree);
3177 break;
3178 case factor:
3179 res = validate_factor(tree);
3180 break;
3181 case power:
3182 res = validate_power(tree);
3183 break;
3184 case atom:
3185 res = validate_atom(tree);
3186 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003187
Fred Drakeff9ea482000-04-19 13:54:15 +00003188 default:
3189 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00003190 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003191 res = 0;
3192 break;
3193 }
3194 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003195 }
3196 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003197}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003198
3199
Guido van Rossum47478871996-08-21 14:32:37 +00003200static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003201validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003202{
3203 int res = validate_eval_input(tree);
3204
3205 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003206 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003207
3208 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003209}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003210
3211
Guido van Rossum3d602e31996-07-21 02:33:56 +00003212/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003213 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003214 */
Guido van Rossum47478871996-08-21 14:32:37 +00003215static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003216validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003217{
Fred Drakec2683dd2001-07-17 19:32:05 +00003218 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003219 int nch = NCH(tree) - 1;
3220 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003221 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003222
Fred Drakec2683dd2001-07-17 19:32:05 +00003223 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003224 if (TYPE(CHILD(tree, j)) == stmt)
3225 res = validate_stmt(CHILD(tree, j));
3226 else
3227 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003228 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003229 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003230 * user. Hopefully, this won't be needed. If a user reports getting
3231 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003232 */
3233 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003234 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003235
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003236 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003237}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003238
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003239static int
3240validate_encoding_decl(node *tree)
3241{
3242 int nch = NCH(tree);
3243 int res = ((nch == 1)
3244 && validate_file_input(CHILD(tree, 0)));
3245
3246 if (!res && !PyErr_Occurred())
3247 err_string("Error Parsing encoding_decl");
3248
3249 return res;
3250}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003251
Fred Drake43f8f9b1998-04-13 16:25:46 +00003252static PyObject*
3253pickle_constructor = NULL;
3254
3255
3256static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003257parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003258{
Fred Drake268397f1998-04-29 14:16:32 +00003259 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003260 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003261 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003262 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003263
Fred Drakec2683dd2001-07-17 19:32:05 +00003264 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003265 PyObject *newargs;
3266 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003267
Fred Drake2a6875e1999-09-20 22:32:18 +00003268 if ((empty_dict = PyDict_New()) == NULL)
3269 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003270 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003271 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003272 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003273 if (tuple != NULL) {
3274 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3275 Py_DECREF(tuple);
3276 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003277 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003278 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003279 }
3280 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003281 Py_XDECREF(empty_dict);
3282
Fred Drake43f8f9b1998-04-13 16:25:46 +00003283 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003284}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003285
3286
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003287/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003288 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003289 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003290 * We'd really have to write a wrapper around it all anyway to allow
3291 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003292 */
3293static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003294 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003295 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003296 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003297 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003298 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003299 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003300 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003301 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003302 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003303 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003304 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003305 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003306 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003307 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003308 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003309 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003310 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003311 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003312
Fred Drake43f8f9b1998-04-13 16:25:46 +00003313 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003314 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003315 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003316
Fred Drake268397f1998-04-29 14:16:32 +00003317 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003318 };
3319
3320
Martin v. Löwis1a214512008-06-11 05:26:20 +00003321
3322static struct PyModuleDef parsermodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 PyModuleDef_HEAD_INIT,
3324 "parser",
3325 NULL,
3326 -1,
3327 parser_functions,
3328 NULL,
3329 NULL,
3330 NULL,
3331 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00003332};
3333
3334PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003335
Mark Hammond62b1ab12002-07-23 06:31:15 +00003336PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003337PyInit_parser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003338{
Fred Drake13130bc2001-08-15 16:44:56 +00003339 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003340
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00003341 if (PyType_Ready(&PyST_Type) < 0)
3342 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00003343 module = PyModule_Create(&parsermodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003344 if (module == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 return NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003346
Fred Drake7a15ba51999-09-09 14:21:52 +00003347 if (parser_error == 0)
3348 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003349
Tim Peters6a627252003-07-21 14:25:23 +00003350 if (parser_error == 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003351 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003352 /* CAUTION: The code next used to skip bumping the refcount on
Martin v. Löwis1a214512008-06-11 05:26:20 +00003353 * parser_error. That's a disaster if PyInit_parser() gets called more
Tim Peters6a627252003-07-21 14:25:23 +00003354 * than once. By incref'ing, we ensure that each module dict that
3355 * gets created owns its reference to the shared parser_error object,
3356 * and the file static parser_error vrbl owns a reference too.
3357 */
3358 Py_INCREF(parser_error);
3359 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003360 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003361
Fred Drakec2683dd2001-07-17 19:32:05 +00003362 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003363 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003364
Fred Drake13130bc2001-08-15 16:44:56 +00003365 PyModule_AddStringConstant(module, "__copyright__",
3366 parser_copyright_string);
3367 PyModule_AddStringConstant(module, "__doc__",
3368 parser_doc_string);
3369 PyModule_AddStringConstant(module, "__version__",
3370 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003371
Fred Drake78bdb9b2001-07-19 20:17:15 +00003372 /* Register to support pickling.
3373 * If this fails, the import of this module will fail because an
3374 * exception will be raised here; should we clear the exception?
3375 */
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003376 copyreg = PyImport_ImportModuleNoBlock("copyreg");
Fred Drake13130bc2001-08-15 16:44:56 +00003377 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003378 PyObject *func, *pickler;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003379 _Py_IDENTIFIER(pickle);
3380 _Py_IDENTIFIER(sequence2st);
3381 _Py_IDENTIFIER(_pickler);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003382
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02003383 func = _PyObject_GetAttrId(copyreg, &PyId_pickle);
3384 pickle_constructor = _PyObject_GetAttrId(module, &PyId_sequence2st);
3385 pickler = _PyObject_GetAttrId(module, &PyId__pickler);
Fred Drakeff9ea482000-04-19 13:54:15 +00003386 Py_XINCREF(pickle_constructor);
3387 if ((func != NULL) && (pickle_constructor != NULL)
3388 && (pickler != NULL)) {
3389 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003390
Thomas Wouters477c8d52006-05-27 19:21:47 +00003391 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3392 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003393 Py_XDECREF(res);
3394 }
3395 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003396 Py_XDECREF(pickle_constructor);
3397 Py_XDECREF(pickler);
3398 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003399 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00003400 return module;
Fred Drakeff9ea482000-04-19 13:54:15 +00003401}