blob: 5f53982627d1cc38b1e9bc851b30113510869747 [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 */
29#include "graminit.h" /* symbols defined in the grammar */
30#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000031#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000032#include "token.h" /* token definitions */
33 /* ISTERMINAL() / ISNONTERMINAL() */
34#include "compile.h" /* PyNode_Compile() */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000035
Fred Drake268397f1998-04-29 14:16:32 +000036#ifdef lint
37#include <note.h>
38#else
39#define NOTE(x)
40#endif
41
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000042/* String constants used to initialize module attributes.
43 *
44 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045static char parser_copyright_string[] =
46"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000047University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
48Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
49Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000050
51
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000052PyDoc_STRVAR(parser_doc_string,
53"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000054
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000055static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000056
57
Fred Drakeff9ea482000-04-19 13:54:15 +000058typedef PyObject* (*SeqMaker) (int length);
59typedef int (*SeqInserter) (PyObject* sequence,
60 int index,
61 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000062
Thomas Wouters7e474022000-07-16 12:04:32 +000063/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000064 * original copyright statement is included below, and continues to apply
65 * in full to the function immediately following. All other material is
66 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
67 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000068 * new naming conventions. Added arguments to provide support for creating
69 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000070 */
71
Guido van Rossum52f2c051993-11-10 12:53:24 +000072
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000073static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000074node2tuple(node *n, /* node to convert */
75 SeqMaker mkseq, /* create sequence */
76 SeqInserter addelem, /* func. to add elem. in seq. */
77 int lineno) /* include line numbers? */
Guido van Rossum47478871996-08-21 14:32:37 +000078{
Guido van Rossum3d602e31996-07-21 02:33:56 +000079 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000080 Py_INCREF(Py_None);
81 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000082 }
83 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000084 int i;
85 PyObject *v;
86 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000087
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000088 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000089 if (v == NULL)
90 return (v);
91 w = PyInt_FromLong(TYPE(n));
92 if (w == NULL) {
93 Py_DECREF(v);
94 return ((PyObject*) NULL);
95 }
96 (void) addelem(v, 0, w);
97 for (i = 0; i < NCH(n); i++) {
98 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno);
99 if (w == NULL) {
100 Py_DECREF(v);
101 return ((PyObject*) NULL);
102 }
103 (void) addelem(v, i+1, w);
104 }
Tim Peters6a627252003-07-21 14:25:23 +0000105
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000106 if (TYPE(n) == encoding_decl)
107 (void) addelem(v, i+1, PyString_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000108 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000109 }
110 else if (ISTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000111 PyObject *result = mkseq(2 + lineno);
112 if (result != NULL) {
113 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
114 (void) addelem(result, 1, PyString_FromString(STR(n)));
115 if (lineno == 1)
116 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
117 }
118 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000119 }
120 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000121 PyErr_SetString(PyExc_SystemError,
122 "unrecognized parse tree node type");
123 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000124 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000125}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000126/*
127 * End of material copyrighted by Stichting Mathematisch Centrum.
128 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000129
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000130
131
132/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000133 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000134 * field of the object type to identify which any given object represents.
135 * These should probably go in an external header to allow other extensions
136 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000137 */
138
Fred Drakec2683dd2001-07-17 19:32:05 +0000139#define PyST_EXPR 1
140#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000141
142
143/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000144 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000145 * naming style, but the code uses the new naming convention.
146 */
147
148static PyObject*
149parser_error = 0;
150
151
Fred Drakec2683dd2001-07-17 19:32:05 +0000152typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000153 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000154 node* st_node; /* the node* returned by the parser */
155 int st_type; /* EXPR or SUITE ? */
156} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000157
158
Jeremy Hylton938ace62002-07-17 16:30:39 +0000159static void parser_free(PyST_Object *st);
160static int parser_compare(PyST_Object *left, PyST_Object *right);
161static PyObject *parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000162
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000163
Fred Drake268397f1998-04-29 14:16:32 +0000164static
Fred Drakec2683dd2001-07-17 19:32:05 +0000165PyTypeObject PyST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000166 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000167 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000168 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000169 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000170 0, /* tp_itemsize */
171 (destructor)parser_free, /* tp_dealloc */
172 0, /* tp_print */
173 parser_getattr, /* tp_getattr */
174 0, /* tp_setattr */
175 (cmpfunc)parser_compare, /* tp_compare */
176 0, /* tp_repr */
177 0, /* tp_as_number */
178 0, /* tp_as_sequence */
179 0, /* tp_as_mapping */
180 0, /* tp_hash */
181 0, /* tp_call */
182 0, /* tp_str */
183 0, /* tp_getattro */
184 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000185
186 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000187 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000188
Fred Drakeff9ea482000-04-19 13:54:15 +0000189 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000190
191 /* __doc__ */
192 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000193}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000194
195
196static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000197parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000198{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000199 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000200
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000201 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000202 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000203
204 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000205 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000206
207 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000208 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000209
210 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000211 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000212
213 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000214 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000215
216 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000217 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000218
Fred Drakeff9ea482000-04-19 13:54:15 +0000219 if (v != 0)
220 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000221 }
222 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000223}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000224
225
Fred Drakec2683dd2001-07-17 19:32:05 +0000226/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000227 *
228 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
229 * This really just wraps a call to parser_compare_nodes() with some easy
230 * checks and protection code.
231 *
232 */
233static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000234parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000235{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000236 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000237 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000238
239 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000240 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000241
Fred Drakec2683dd2001-07-17 19:32:05 +0000242 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000243}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000244
245
Fred Drakec2683dd2001-07-17 19:32:05 +0000246/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000248 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000249 * 'wrapper' object that holds a node* and allows it to be passed around in
250 * Python code.
251 *
252 */
253static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000254parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000255{
Fred Drakec2683dd2001-07-17 19:32:05 +0000256 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000257
258 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000259 o->st_node = st;
260 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000261 }
Fred Drake268397f1998-04-29 14:16:32 +0000262 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000263 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000264 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000266}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000267
268
Fred Drakec2683dd2001-07-17 19:32:05 +0000269/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000270 *
271 * This is called by a del statement that reduces the reference count to 0.
272 *
273 */
274static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000275parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000276{
Fred Drakec2683dd2001-07-17 19:32:05 +0000277 PyNode_Free(st->st_node);
278 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000279}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000280
281
Fred Drakec2683dd2001-07-17 19:32:05 +0000282/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000283 *
284 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000285 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000286 *
287 */
288static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000289parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000290{
Guido van Rossum47478871996-08-21 14:32:37 +0000291 PyObject *line_option = 0;
292 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000293 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000294
Fred Drake7a15ba51999-09-09 14:21:52 +0000295 static char *keywords[] = {"ast", "line_info", NULL};
296
Fred Drake268397f1998-04-29 14:16:32 +0000297 if (self == NULL) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000298 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2tuple", keywords,
299 &PyST_Type, &self, &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000300 }
Fred Drake503d8d61998-04-13 18:45:18 +0000301 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000302 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:totuple", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000303 &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000304 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000305 int lineno = 0;
306 if (line_option != NULL) {
307 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
308 }
309 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000310 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000311 * since it's known to work already.
312 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000313 res = node2tuple(((PyST_Object*)self)->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000314 PyTuple_New, PyTuple_SetItem, lineno);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000315 }
316 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000317}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000318
319
Fred Drakec2683dd2001-07-17 19:32:05 +0000320/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000321 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000322 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000323 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000324 *
325 */
326static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000327parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000328{
Guido van Rossum47478871996-08-21 14:32:37 +0000329 PyObject *line_option = 0;
330 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000331 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000332
Fred Drake7a15ba51999-09-09 14:21:52 +0000333 static char *keywords[] = {"ast", "line_info", NULL};
334
Fred Drake503d8d61998-04-13 18:45:18 +0000335 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000336 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2list", keywords,
337 &PyST_Type, &self, &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000338 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000339 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:tolist", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000340 &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000341 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000342 int lineno = 0;
343 if (line_option != 0) {
344 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
345 }
346 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000347 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000348 * since it's known to work already.
349 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000350 res = node2tuple(self->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000351 PyList_New, PyList_SetItem, lineno);
Guido van Rossum47478871996-08-21 14:32:37 +0000352 }
353 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000354}
Guido van Rossum47478871996-08-21 14:32:37 +0000355
356
Fred Drakec2683dd2001-07-17 19:32:05 +0000357/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000358 *
359 * This function creates code objects from the parse tree represented by
360 * the passed-in data object. An optional file name is passed in as well.
361 *
362 */
363static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000364parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000365{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000366 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000367 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000368 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000369
Fred Drake7a15ba51999-09-09 14:21:52 +0000370 static char *keywords[] = {"ast", "filename", NULL};
371
Fred Drake503d8d61998-04-13 18:45:18 +0000372 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000373 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
374 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000375 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000376 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000377 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000378
379 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000380 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000381
382 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000383}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000384
385
386/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
387 * PyObject* parser_issuite(PyObject* self, PyObject* args)
388 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000389 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000390 * a statement suite, respectively. The return is a Python truth value.
391 *
392 */
393static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000394parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000395{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000396 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000397 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000398
Fred Drake7a15ba51999-09-09 14:21:52 +0000399 static char *keywords[] = {"ast", NULL};
400
Fred Drake503d8d61998-04-13 18:45:18 +0000401 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000402 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000403 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000404 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000405 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000406
407 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000408 /* Check to see if the ST represents an expression or not. */
409 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000410 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000411 }
412 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000413}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000414
415
416static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000417parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000418{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000419 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000420 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000421
Fred Drake7a15ba51999-09-09 14:21:52 +0000422 static char *keywords[] = {"ast", NULL};
423
Fred Drake503d8d61998-04-13 18:45:18 +0000424 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000425 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000426 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000427 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000428 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000429
430 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000431 /* Check to see if the ST represents an expression or not. */
432 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000433 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000434 }
435 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000436}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000437
438
Fred Drake7a15ba51999-09-09 14:21:52 +0000439#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
440
Fred Drake503d8d61998-04-13 18:45:18 +0000441static PyMethodDef
442parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000443 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000444 PyDoc_STR("Compile this ST object into a code object.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000445 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000446 PyDoc_STR("Determines if this ST object was created from an expression.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000447 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000448 PyDoc_STR("Determines if this ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000449 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000450 PyDoc_STR("Creates a list-tree representation of this ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000451 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000452 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Fred Drake503d8d61998-04-13 18:45:18 +0000453
Fred Drake268397f1998-04-29 14:16:32 +0000454 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000455};
456
Fred Drake503d8d61998-04-13 18:45:18 +0000457
458static PyObject*
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000459parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000460{
Fred Drake503d8d61998-04-13 18:45:18 +0000461 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000462}
Fred Drake503d8d61998-04-13 18:45:18 +0000463
464
Guido van Rossum3d602e31996-07-21 02:33:56 +0000465/* err_string(char* message)
466 *
467 * Sets the error string for an exception of type ParserError.
468 *
469 */
470static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000471err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000472{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000473 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000474}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000475
476
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000477/* PyObject* parser_do_parse(PyObject* args, int type)
478 *
479 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000480 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000481 *
482 */
483static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000484parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000485{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000486 char* string = 0;
487 PyObject* res = 0;
488
Fred Drake7a15ba51999-09-09 14:21:52 +0000489 static char *keywords[] = {"source", NULL};
490
491 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000492 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000493 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000494 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000495
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000496 if (n)
497 res = parser_newstobject(n, type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000498 }
499 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000500}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000501
502
503/* PyObject* parser_expr(PyObject* self, PyObject* args)
504 * PyObject* parser_suite(PyObject* self, PyObject* args)
505 *
506 * External interfaces to the parser itself. Which is called determines if
507 * the parser attempts to recognize an expression ('eval' form) or statement
508 * suite ('exec' form). The real work is done by parser_do_parse() above.
509 *
510 */
511static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000512parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000513{
Fred Drake268397f1998-04-29 14:16:32 +0000514 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000515 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000516}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000517
518
519static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000520parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000521{
Fred Drake268397f1998-04-29 14:16:32 +0000522 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000523 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000524}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000525
526
527
Fred Drakec2683dd2001-07-17 19:32:05 +0000528/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000529 * object requires that the input tuple be valid without having to rely on
530 * catching an exception from the compiler. This is done to allow the
531 * compiler itself to remain fast, since most of its input will come from
532 * the parser directly, and therefore be known to be syntactically correct.
533 * This validation is done to ensure that we don't core dump the compile
534 * phase, returning an exception instead.
535 *
536 * Two aspects can be broken out in this code: creating a node tree from
537 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000538 * advantageous to expand the number of ST types to include funcdefs and
539 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000540 * here. They are not necessary, and not quite as useful in a raw form.
541 * For now, let's get expressions and suites working reliably.
542 */
543
544
Jeremy Hylton938ace62002-07-17 16:30:39 +0000545static node* build_node_tree(PyObject *tuple);
546static int validate_expr_tree(node *tree);
547static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000548static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000549
Fred Drakec2683dd2001-07-17 19:32:05 +0000550/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000551 *
552 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000553 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000554 * tuple can be validated. It does this by checking the first code of the
555 * tuple, and, if acceptable, builds the internal representation. If this
556 * step succeeds, the internal representation is validated as fully as
557 * possible with the various validate_*() routines defined below.
558 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000559 * This function must be changed if support is to be added for PyST_FRAGMENT
560 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000561 *
562 */
563static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000564parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000565{
Fred Drake268397f1998-04-29 14:16:32 +0000566 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000567 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000568 PyObject *tuple;
569 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000570
Fred Drake7a15ba51999-09-09 14:21:52 +0000571 static char *keywords[] = {"sequence", NULL};
572
Fred Drakec2683dd2001-07-17 19:32:05 +0000573 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000574 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000575 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000576 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000577 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000578 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000579 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000580 }
581 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000582 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000583 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000584 tree = build_node_tree(tuple);
585 if (tree != 0) {
586 int start_sym = TYPE(tree);
587 if (start_sym == eval_input) {
588 /* Might be an eval form. */
589 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000590 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000591 else
592 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000593 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000594 else if (start_sym == file_input) {
595 /* This looks like an exec form so far. */
596 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000597 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000598 else
599 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000600 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000601 else if (start_sym == encoding_decl) {
602 /* This looks like an encoding_decl so far. */
603 if (validate_encoding_decl(tree))
604 st = parser_newstobject(tree, PyST_SUITE);
605 else
606 PyNode_Free(tree);
607 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000608 else {
609 /* This is a fragment, at best. */
610 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000611 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000612 }
Guido van Rossum47478871996-08-21 14:32:37 +0000613 }
Guido van Rossum47478871996-08-21 14:32:37 +0000614 /* Make sure we throw an exception on all errors. We should never
615 * get this, but we'd do well to be sure something is done.
616 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000617 if (st == NULL && !PyErr_Occurred())
618 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000619
Fred Drakec2683dd2001-07-17 19:32:05 +0000620 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000621}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000622
623
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000624/* node* build_node_children()
625 *
626 * Iterate across the children of the current non-terminal node and build
627 * their structures. If successful, return the root of this portion of
628 * the tree, otherwise, 0. Any required exception will be specified already,
629 * and no memory will have been deallocated.
630 *
631 */
632static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000633build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000634{
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000635 int len = PyObject_Size(tuple);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000636 int i, err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000637
638 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000639 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000640 PyObject* elem = PySequence_GetItem(tuple, i);
641 int ok = elem != NULL;
642 long type = 0;
643 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000644
Fred Drakeff9ea482000-04-19 13:54:15 +0000645 if (ok)
646 ok = PySequence_Check(elem);
647 if (ok) {
648 PyObject *temp = PySequence_GetItem(elem, 0);
649 if (temp == NULL)
650 ok = 0;
651 else {
652 ok = PyInt_Check(temp);
653 if (ok)
654 type = PyInt_AS_LONG(temp);
655 Py_DECREF(temp);
656 }
657 }
658 if (!ok) {
659 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000660 Py_BuildValue("os", elem,
Fred Drakeff9ea482000-04-19 13:54:15 +0000661 "Illegal node construct."));
662 Py_XDECREF(elem);
663 return (0);
664 }
665 if (ISTERMINAL(type)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000666 int len = PyObject_Size(elem);
667 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000668
Fred Drake0ac9b072000-09-12 21:58:06 +0000669 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000670 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000671 return 0;
672 }
673 temp = PySequence_GetItem(elem, 1);
674 if (temp == NULL)
675 return 0;
676 if (!PyString_Check(temp)) {
677 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000678 "second item in terminal node must be a string,"
679 " found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000680 temp->ob_type->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000681 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000682 return 0;
683 }
684 if (len == 3) {
685 PyObject *o = PySequence_GetItem(elem, 2);
686 if (o != NULL) {
687 if (PyInt_Check(o))
688 *line_num = PyInt_AS_LONG(o);
689 else {
690 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000691 "third item in terminal node must be an"
692 " integer, found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000693 temp->ob_type->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000694 Py_DECREF(o);
695 Py_DECREF(temp);
696 return 0;
697 }
698 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000699 }
700 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000701 len = PyString_GET_SIZE(temp) + 1;
702 strn = (char *)PyMem_MALLOC(len);
703 if (strn != NULL)
704 (void) memcpy(strn, PyString_AS_STRING(temp), len);
705 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000706 }
707 else if (!ISNONTERMINAL(type)) {
708 /*
709 * It has to be one or the other; this is an error.
710 * Throw an exception.
711 */
712 PyErr_SetObject(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000713 Py_BuildValue("os", elem, "unknown node type."));
Fred Drakeff9ea482000-04-19 13:54:15 +0000714 Py_XDECREF(elem);
715 return (0);
716 }
Fred Drake8b55b2d2001-12-05 22:10:44 +0000717 err = PyNode_AddChild(root, type, strn, *line_num);
718 if (err == E_NOMEM) {
719 PyMem_DEL(strn);
720 return (node *) PyErr_NoMemory();
721 }
722 if (err == E_OVERFLOW) {
723 PyMem_DEL(strn);
724 PyErr_SetString(PyExc_ValueError,
725 "unsupported number of child nodes");
726 return NULL;
727 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000728
Fred Drakeff9ea482000-04-19 13:54:15 +0000729 if (ISNONTERMINAL(type)) {
730 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000731
Fred Drakeff9ea482000-04-19 13:54:15 +0000732 if (new_child != build_node_children(elem, new_child, line_num)) {
733 Py_XDECREF(elem);
734 return (0);
735 }
736 }
737 else if (type == NEWLINE) { /* It's true: we increment the */
738 ++(*line_num); /* line number *after* the newline! */
739 }
740 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000741 }
742 return (root);
Fred Drakeff9ea482000-04-19 13:54:15 +0000743}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000744
745
746static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000747build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000748{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000749 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000750 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000751 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000752
Guido van Rossum47478871996-08-21 14:32:37 +0000753 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000754 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000755 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000756 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000757 /*
758 * The tuple is simple, but it doesn't start with a start symbol.
759 * Throw an exception now and be done with it.
760 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000761 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000762 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000763 PyErr_SetObject(parser_error, tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000764 }
765 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000766 /*
767 * Not efficient, but that can be handled later.
768 */
769 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000770 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000771
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000772 if (num == encoding_decl) {
773 encoding = PySequence_GetItem(tuple, 2);
774 /* tuple isn't borrowed anymore here, need to DECREF */
775 tuple = PySequence_GetSlice(tuple, 0, 2);
776 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000777 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000778 if (res != NULL) {
779 if (res != build_node_children(tuple, res, &line_num)) {
780 PyNode_Free(res);
781 res = NULL;
782 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000783 if (res && encoding) {
784 int len;
785 len = PyString_GET_SIZE(encoding) + 1;
786 res->n_str = (char *)PyMem_MALLOC(len);
787 if (res->n_str != NULL)
788 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
789 Py_DECREF(encoding);
790 Py_DECREF(tuple);
791 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000792 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000793 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000794 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000795 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000796 * NONTERMINAL, we can't use it. Not sure the implementation
797 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000798 */
799 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000800 Py_BuildValue("os", tuple,
Fred Drakeff9ea482000-04-19 13:54:15 +0000801 "Illegal component tuple."));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000802
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000803 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000804}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000805
806
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000807/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000808 * Validation routines used within the validation section:
809 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000810static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000811
Fred Drakeff9ea482000-04-19 13:54:15 +0000812#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
813#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
814#define validate_colon(ch) validate_terminal(ch, COLON, ":")
815#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
816#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
817#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
818#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
819#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
820#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
821#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
822#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
823#define validate_star(ch) validate_terminal(ch, STAR, "*")
824#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
825#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
826#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000827#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000828#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000829
Fred Drake0ac9b072000-09-12 21:58:06 +0000830#define VALIDATER(n) static int validate_##n(node *tree)
831
Fred Drakeff9ea482000-04-19 13:54:15 +0000832VALIDATER(node); VALIDATER(small_stmt);
833VALIDATER(class); VALIDATER(node);
834VALIDATER(parameters); VALIDATER(suite);
835VALIDATER(testlist); VALIDATER(varargslist);
836VALIDATER(fpdef); VALIDATER(fplist);
837VALIDATER(stmt); VALIDATER(simple_stmt);
838VALIDATER(expr_stmt); VALIDATER(power);
839VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000840VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000841VALIDATER(raise_stmt); VALIDATER(import_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000842VALIDATER(global_stmt); VALIDATER(list_if);
843VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000844VALIDATER(exec_stmt); VALIDATER(compound_stmt);
845VALIDATER(while); VALIDATER(for);
846VALIDATER(try); VALIDATER(except_clause);
847VALIDATER(test); VALIDATER(and_test);
848VALIDATER(not_test); VALIDATER(comparison);
849VALIDATER(comp_op); VALIDATER(expr);
850VALIDATER(xor_expr); VALIDATER(and_expr);
851VALIDATER(shift_expr); VALIDATER(arith_expr);
852VALIDATER(term); VALIDATER(factor);
853VALIDATER(atom); VALIDATER(lambdef);
854VALIDATER(trailer); VALIDATER(subscript);
855VALIDATER(subscriptlist); VALIDATER(sliceop);
856VALIDATER(exprlist); VALIDATER(dictmaker);
857VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000858VALIDATER(listmaker); VALIDATER(yield_stmt);
Raymond Hettinger354433a2004-05-19 08:20:33 +0000859VALIDATER(testlist1); VALIDATER(gen_for);
860VALIDATER(gen_iter); VALIDATER(gen_if);
861VALIDATER(testlist_gexp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000862
Fred Drake0ac9b072000-09-12 21:58:06 +0000863#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000864
Fred Drakeff9ea482000-04-19 13:54:15 +0000865#define is_even(n) (((n) & 1) == 0)
866#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000867
868
869static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000870validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000871{
Fred Drake0ac9b072000-09-12 21:58:06 +0000872 if (TYPE(n) != t) {
873 PyErr_Format(parser_error, "Expected node type %d, got %d.",
874 t, TYPE(n));
875 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000876 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000877 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000878}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000879
880
Fred Drakee7ab64e2000-04-25 04:14:46 +0000881/* Verifies that the number of child nodes is exactly 'num', raising
882 * an exception if it isn't. The exception message does not indicate
883 * the exact number of nodes, allowing this to be used to raise the
884 * "right" exception when the wrong number of nodes is present in a
885 * specific variant of a statement's syntax. This is commonly used
886 * in that fashion.
887 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000888static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000889validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000890{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000891 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000892 PyErr_Format(parser_error,
893 "Illegal number of children for %s node.", name);
894 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000895 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000896 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000897}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000898
899
900static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000901validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000902{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000903 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000904 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000905
906 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000907 PyErr_Format(parser_error,
908 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000909 }
910 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000911}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000912
913
Guido van Rossum47478871996-08-21 14:32:37 +0000914/* X (',' X) [',']
915 */
916static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000917validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000918 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000919{
920 int nch = NCH(tree);
921 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000922 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000923
924 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000925 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000926 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000927 if (is_even(nch))
928 res = validate_comma(CHILD(tree, --nch));
929 if (res && nch > 1) {
930 int pos = 1;
931 for ( ; res && pos < nch; pos += 2)
932 res = (validate_comma(CHILD(tree, pos))
933 && vfunc(CHILD(tree, pos + 1)));
934 }
Guido van Rossum47478871996-08-21 14:32:37 +0000935 }
936 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000937}
Guido van Rossum47478871996-08-21 14:32:37 +0000938
939
Fred Drakecff283c2000-08-21 22:24:43 +0000940/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000941 *
942 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000943 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000944 */
Guido van Rossum47478871996-08-21 14:32:37 +0000945static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000946validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000947{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000948 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000949 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000950
Guido van Rossum3d602e31996-07-21 02:33:56 +0000951 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000952 res = (validate_name(CHILD(tree, 0), "class")
953 && validate_ntype(CHILD(tree, 1), NAME)
954 && validate_colon(CHILD(tree, nch - 2))
955 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000956 }
957 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000958 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000959 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000960 res = (validate_lparen(CHILD(tree, 2))
961 && validate_testlist(CHILD(tree, 3))
962 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000963 }
964 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000965}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000966
967
Guido van Rossum3d602e31996-07-21 02:33:56 +0000968/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +0000969 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +0000970 */
Guido van Rossum47478871996-08-21 14:32:37 +0000971static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000972validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000973{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000974 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000975 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +0000976 && (nch >= 4)
977 && validate_name(CHILD(tree, 0), "if")
978 && validate_test(CHILD(tree, 1))
979 && validate_colon(CHILD(tree, 2))
980 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000981
982 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000983 /* ... 'else' ':' suite */
984 res = (validate_name(CHILD(tree, nch - 3), "else")
985 && validate_colon(CHILD(tree, nch - 2))
986 && validate_suite(CHILD(tree, nch - 1)));
987 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000988 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000989 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000990 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000991 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +0000992 /* Will catch the case for nch < 4 */
993 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000994 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000995 /* ... ('elif' test ':' suite)+ ... */
996 int j = 4;
997 while ((j < nch) && res) {
998 res = (validate_name(CHILD(tree, j), "elif")
999 && validate_colon(CHILD(tree, j + 2))
1000 && validate_test(CHILD(tree, j + 1))
1001 && validate_suite(CHILD(tree, j + 3)));
1002 j += 4;
1003 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001004 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001005 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001006}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001007
1008
Guido van Rossum3d602e31996-07-21 02:33:56 +00001009/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001010 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001011 *
1012 */
Guido van Rossum47478871996-08-21 14:32:37 +00001013static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001014validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001015{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001016 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001017 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001018
Guido van Rossum3d602e31996-07-21 02:33:56 +00001019 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001020 res = (validate_lparen(CHILD(tree, 0))
1021 && validate_rparen(CHILD(tree, nch - 1)));
1022 if (res && (nch == 3))
1023 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001024 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001025 else {
1026 (void) validate_numnodes(tree, 2, "parameters");
1027 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001028 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001029}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001030
1031
Fred Drakecff283c2000-08-21 22:24:43 +00001032/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001033 *
1034 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001035 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001036 * | NEWLINE INDENT stmt+ DEDENT
1037 */
Guido van Rossum47478871996-08-21 14:32:37 +00001038static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001039validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001040{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001041 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001042 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001043
Guido van Rossum3d602e31996-07-21 02:33:56 +00001044 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001045 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001046 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001047 /* NEWLINE INDENT stmt+ DEDENT */
1048 res = (validate_newline(CHILD(tree, 0))
1049 && validate_indent(CHILD(tree, 1))
1050 && validate_stmt(CHILD(tree, 2))
1051 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001052
Fred Drakeff9ea482000-04-19 13:54:15 +00001053 if (res && (nch > 4)) {
1054 int i = 3;
1055 --nch; /* forget the DEDENT */
1056 for ( ; res && (i < nch); ++i)
1057 res = validate_stmt(CHILD(tree, i));
1058 }
1059 else if (nch < 4)
1060 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001061 }
1062 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001063}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001064
1065
Guido van Rossum47478871996-08-21 14:32:37 +00001066static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001067validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001068{
Guido van Rossum47478871996-08-21 14:32:37 +00001069 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001070 validate_test, "testlist"));
1071}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001072
1073
Guido van Rossum1c917072001-10-15 15:44:05 +00001074static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001075validate_testlist1(node *tree)
1076{
1077 return (validate_repeating_list(tree, testlist1,
1078 validate_test, "testlist1"));
1079}
1080
1081
1082static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001083validate_testlist_safe(node *tree)
1084{
1085 return (validate_repeating_list(tree, testlist_safe,
1086 validate_test, "testlist_safe"));
1087}
1088
1089
Fred Drakecff283c2000-08-21 22:24:43 +00001090/* '*' NAME [',' '**' NAME] | '**' NAME
1091 */
1092static int
1093validate_varargslist_trailer(node *tree, int start)
1094{
1095 int nch = NCH(tree);
1096 int res = 0;
1097 int sym;
1098
1099 if (nch <= start) {
1100 err_string("expected variable argument trailer for varargslist");
1101 return 0;
1102 }
1103 sym = TYPE(CHILD(tree, start));
1104 if (sym == STAR) {
1105 /*
1106 * ('*' NAME [',' '**' NAME]
1107 */
1108 if (nch-start == 2)
1109 res = validate_name(CHILD(tree, start+1), NULL);
1110 else if (nch-start == 5)
1111 res = (validate_name(CHILD(tree, start+1), NULL)
1112 && validate_comma(CHILD(tree, start+2))
1113 && validate_doublestar(CHILD(tree, start+3))
1114 && validate_name(CHILD(tree, start+4), NULL));
1115 }
1116 else if (sym == DOUBLESTAR) {
1117 /*
1118 * '**' NAME
1119 */
1120 if (nch-start == 2)
1121 res = validate_name(CHILD(tree, start+1), NULL);
1122 }
1123 if (!res)
1124 err_string("illegal variable argument trailer for varargslist");
1125 return res;
1126}
1127
1128
1129/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001130 *
1131 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001132 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001133 * ('*' NAME [',' '**' NAME]
1134 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001135 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1136 *
1137 */
Guido van Rossum47478871996-08-21 14:32:37 +00001138static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001139validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001140{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001141 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001142 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001143 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001144
Fred Drakeb6429a22000-12-11 22:08:27 +00001145 if (!res)
1146 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001147 if (nch < 1) {
1148 err_string("varargslist missing child nodes");
1149 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001150 }
Fred Drakecff283c2000-08-21 22:24:43 +00001151 sym = TYPE(CHILD(tree, 0));
1152 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001153 /* whole thing matches:
1154 * '*' NAME [',' '**' NAME] | '**' NAME
1155 */
Fred Drakecff283c2000-08-21 22:24:43 +00001156 res = validate_varargslist_trailer(tree, 0);
1157 else if (sym == fpdef) {
1158 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001159
Fred Drakecff283c2000-08-21 22:24:43 +00001160 sym = TYPE(CHILD(tree, nch-1));
1161 if (sym == NAME) {
1162 /*
1163 * (fpdef ['=' test] ',')+
1164 * ('*' NAME [',' '**' NAME]
1165 * | '**' NAME)
1166 */
1167 /* skip over (fpdef ['=' test] ',')+ */
1168 while (res && (i+2 <= nch)) {
1169 res = validate_fpdef(CHILD(tree, i));
1170 ++i;
1171 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1172 res = (validate_equal(CHILD(tree, i))
1173 && validate_test(CHILD(tree, i+1)));
1174 if (res)
1175 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001176 }
Fred Drakecff283c2000-08-21 22:24:43 +00001177 if (res && i < nch) {
1178 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001179 ++i;
1180 if (res && i < nch
1181 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1182 || TYPE(CHILD(tree, i)) == STAR))
1183 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001184 }
1185 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001186 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1187 * i --^^^
1188 */
Fred Drakecff283c2000-08-21 22:24:43 +00001189 if (res)
1190 res = validate_varargslist_trailer(tree, i);
1191 }
1192 else {
1193 /*
1194 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1195 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001196 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001197 if (sym == COMMA) {
1198 res = validate_comma(CHILD(tree, nch-1));
1199 if (!res)
1200 return 0;
1201 --nch;
1202 }
1203 /*
1204 * fpdef ['=' test] (',' fpdef ['=' test])*
1205 */
1206 res = validate_fpdef(CHILD(tree, 0));
1207 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001208 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1209 res = (validate_equal(CHILD(tree, i))
1210 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001211 i += 2;
1212 }
1213 /*
1214 * ... (',' fpdef ['=' test])*
1215 * i ---^^^
1216 */
1217 while (res && (nch - i) >= 2) {
1218 res = (validate_comma(CHILD(tree, i))
1219 && validate_fpdef(CHILD(tree, i+1)));
1220 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001221 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1222 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001223 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001224 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001225 }
1226 }
1227 if (res && nch - i != 0) {
1228 res = 0;
1229 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001230 }
1231 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001232 }
Fred Drakecff283c2000-08-21 22:24:43 +00001233 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001234}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001235
1236
Fred Drakecff283c2000-08-21 22:24:43 +00001237/* list_iter: list_for | list_if
1238 */
1239static int
1240validate_list_iter(node *tree)
1241{
1242 int res = (validate_ntype(tree, list_iter)
1243 && validate_numnodes(tree, 1, "list_iter"));
1244 if (res && TYPE(CHILD(tree, 0)) == list_for)
1245 res = validate_list_for(CHILD(tree, 0));
1246 else
1247 res = validate_list_if(CHILD(tree, 0));
1248
1249 return res;
1250}
1251
Raymond Hettinger354433a2004-05-19 08:20:33 +00001252/* gen_iter: gen_for | gen_if
1253 */
1254static int
1255validate_gen_iter(node *tree)
1256{
1257 int res = (validate_ntype(tree, gen_iter)
1258 && validate_numnodes(tree, 1, "gen_iter"));
1259 if (res && TYPE(CHILD(tree, 0)) == gen_for)
1260 res = validate_gen_for(CHILD(tree, 0));
1261 else
1262 res = validate_gen_if(CHILD(tree, 0));
1263
1264 return res;
1265}
1266
Fred Drakecff283c2000-08-21 22:24:43 +00001267/* list_for: 'for' exprlist 'in' testlist [list_iter]
1268 */
1269static int
1270validate_list_for(node *tree)
1271{
1272 int nch = NCH(tree);
1273 int res;
1274
1275 if (nch == 5)
1276 res = validate_list_iter(CHILD(tree, 4));
1277 else
1278 res = validate_numnodes(tree, 4, "list_for");
1279
1280 if (res)
1281 res = (validate_name(CHILD(tree, 0), "for")
1282 && validate_exprlist(CHILD(tree, 1))
1283 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001284 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001285
1286 return res;
1287}
1288
Raymond Hettinger354433a2004-05-19 08:20:33 +00001289/* gen_for: 'for' exprlist 'in' test [gen_iter]
1290 */
1291static int
1292validate_gen_for(node *tree)
1293{
1294 int nch = NCH(tree);
1295 int res;
1296
1297 if (nch == 5)
1298 res = validate_gen_iter(CHILD(tree, 4));
1299 else
1300 res = validate_numnodes(tree, 4, "gen_for");
1301
1302 if (res)
1303 res = (validate_name(CHILD(tree, 0), "for")
1304 && validate_exprlist(CHILD(tree, 1))
1305 && validate_name(CHILD(tree, 2), "in")
1306 && validate_test(CHILD(tree, 3)));
1307
1308 return res;
1309}
1310
Fred Drakecff283c2000-08-21 22:24:43 +00001311/* list_if: 'if' test [list_iter]
1312 */
1313static int
1314validate_list_if(node *tree)
1315{
1316 int nch = NCH(tree);
1317 int res;
1318
1319 if (nch == 3)
1320 res = validate_list_iter(CHILD(tree, 2));
1321 else
1322 res = validate_numnodes(tree, 2, "list_if");
1323
1324 if (res)
1325 res = (validate_name(CHILD(tree, 0), "if")
1326 && validate_test(CHILD(tree, 1)));
1327
1328 return res;
1329}
1330
Raymond Hettinger354433a2004-05-19 08:20:33 +00001331/* gen_if: 'if' test [gen_iter]
1332 */
1333static int
1334validate_gen_if(node *tree)
1335{
1336 int nch = NCH(tree);
1337 int res;
1338
1339 if (nch == 3)
1340 res = validate_gen_iter(CHILD(tree, 2));
1341 else
1342 res = validate_numnodes(tree, 2, "gen_if");
1343
1344 if (res)
1345 res = (validate_name(CHILD(tree, 0), "if")
1346 && validate_test(CHILD(tree, 1)));
1347
1348 return res;
1349}
Fred Drakecff283c2000-08-21 22:24:43 +00001350
1351/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001352 *
1353 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001354 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001355 * | '(' fplist ')'
1356 */
Guido van Rossum47478871996-08-21 14:32:37 +00001357static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001358validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001359{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001360 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001361 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001362
Guido van Rossum3d602e31996-07-21 02:33:56 +00001363 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001364 if (nch == 1)
1365 res = validate_ntype(CHILD(tree, 0), NAME);
1366 else if (nch == 3)
1367 res = (validate_lparen(CHILD(tree, 0))
1368 && validate_fplist(CHILD(tree, 1))
1369 && validate_rparen(CHILD(tree, 2)));
1370 else
1371 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001372 }
1373 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001374}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001375
1376
Guido van Rossum47478871996-08-21 14:32:37 +00001377static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001378validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001379{
Guido van Rossum47478871996-08-21 14:32:37 +00001380 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001381 validate_fpdef, "fplist"));
1382}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001383
1384
Guido van Rossum3d602e31996-07-21 02:33:56 +00001385/* simple_stmt | compound_stmt
1386 *
1387 */
Guido van Rossum47478871996-08-21 14:32:37 +00001388static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001389validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001390{
1391 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001392 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001393
Guido van Rossum3d602e31996-07-21 02:33:56 +00001394 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001395 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001396
Fred Drakeff9ea482000-04-19 13:54:15 +00001397 if (TYPE(tree) == simple_stmt)
1398 res = validate_simple_stmt(tree);
1399 else
1400 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001401 }
1402 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001403}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001404
1405
Guido van Rossum3d602e31996-07-21 02:33:56 +00001406/* small_stmt (';' small_stmt)* [';'] NEWLINE
1407 *
1408 */
Guido van Rossum47478871996-08-21 14:32:37 +00001409static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001410validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001411{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001412 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001413 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001414 && (nch >= 2)
1415 && validate_small_stmt(CHILD(tree, 0))
1416 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001417
Guido van Rossum3d602e31996-07-21 02:33:56 +00001418 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001419 res = validate_numnodes(tree, 2, "simple_stmt");
1420 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001421 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001422 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001423 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001424 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001425
Fred Drakeff9ea482000-04-19 13:54:15 +00001426 for (i = 1; res && (i < nch); i += 2)
1427 res = (validate_semi(CHILD(tree, i))
1428 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001429 }
1430 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001431}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001432
1433
Guido van Rossum47478871996-08-21 14:32:37 +00001434static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001435validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001436{
1437 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001438 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001439
Fred Drake0ac9b072000-09-12 21:58:06 +00001440 if (res) {
1441 int ntype = TYPE(CHILD(tree, 0));
1442
1443 if ( (ntype == expr_stmt)
1444 || (ntype == print_stmt)
1445 || (ntype == del_stmt)
1446 || (ntype == pass_stmt)
1447 || (ntype == flow_stmt)
1448 || (ntype == import_stmt)
1449 || (ntype == global_stmt)
1450 || (ntype == assert_stmt)
1451 || (ntype == exec_stmt))
1452 res = validate_node(CHILD(tree, 0));
1453 else {
1454 res = 0;
1455 err_string("illegal small_stmt child type");
1456 }
1457 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001458 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001459 res = 0;
1460 PyErr_Format(parser_error,
1461 "Unrecognized child node of small_stmt: %d.",
1462 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001463 }
1464 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001465}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001466
1467
1468/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001469 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001470 */
Guido van Rossum47478871996-08-21 14:32:37 +00001471static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001472validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001473{
1474 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001475 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001476 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001477
1478 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001479 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001480
1481 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001482 ntype = TYPE(tree);
1483 if ( (ntype == if_stmt)
1484 || (ntype == while_stmt)
1485 || (ntype == for_stmt)
1486 || (ntype == try_stmt)
1487 || (ntype == funcdef)
1488 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001489 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001490 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001491 res = 0;
1492 PyErr_Format(parser_error,
1493 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001494 }
1495 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001496}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001497
1498
Guido van Rossum47478871996-08-21 14:32:37 +00001499static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001500validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001501{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001502 int j;
1503 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001504 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001505 && is_odd(nch)
1506 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001507
Fred Drake28f739a2000-08-25 22:42:40 +00001508 if (res && nch == 3
1509 && TYPE(CHILD(tree, 1)) == augassign) {
1510 res = (validate_numnodes(CHILD(tree, 1), 1, "augassign")
1511 && validate_testlist(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001512
Fred Drake28f739a2000-08-25 22:42:40 +00001513 if (res) {
1514 char *s = STR(CHILD(CHILD(tree, 1), 0));
1515
1516 res = (strcmp(s, "+=") == 0
1517 || strcmp(s, "-=") == 0
1518 || strcmp(s, "*=") == 0
1519 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001520 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001521 || strcmp(s, "%=") == 0
1522 || strcmp(s, "&=") == 0
1523 || strcmp(s, "|=") == 0
1524 || strcmp(s, "^=") == 0
1525 || strcmp(s, "<<=") == 0
1526 || strcmp(s, ">>=") == 0
1527 || strcmp(s, "**=") == 0);
1528 if (!res)
1529 err_string("illegal augmmented assignment operator");
1530 }
1531 }
1532 else {
1533 for (j = 1; res && (j < nch); j += 2)
1534 res = (validate_equal(CHILD(tree, j))
1535 && validate_testlist(CHILD(tree, j + 1)));
1536 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001537 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001538}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001539
1540
Guido van Rossum3d602e31996-07-21 02:33:56 +00001541/* print_stmt:
1542 *
Fred Drakecff283c2000-08-21 22:24:43 +00001543 * 'print' ( [ test (',' test)* [','] ]
1544 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001545 */
Guido van Rossum47478871996-08-21 14:32:37 +00001546static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001547validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001548{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001549 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001550 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001551 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001552 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001553
Fred Drakecff283c2000-08-21 22:24:43 +00001554 if (res && nch > 1) {
1555 int sym = TYPE(CHILD(tree, 1));
1556 int i = 1;
1557 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001558
Fred Drakecff283c2000-08-21 22:24:43 +00001559 if (sym == test)
1560 res = validate_test(CHILD(tree, i++));
1561 else {
1562 if (nch < 3)
1563 res = validate_numnodes(tree, 3, "print_stmt");
1564 else {
1565 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1566 && validate_test(CHILD(tree, i+1)));
1567 i += 2;
1568 allow_trailing_comma = 0;
1569 }
1570 }
1571 if (res) {
1572 /* ... (',' test)* [','] */
1573 while (res && i+2 <= nch) {
1574 res = (validate_comma(CHILD(tree, i))
1575 && validate_test(CHILD(tree, i+1)));
1576 allow_trailing_comma = 1;
1577 i += 2;
1578 }
1579 if (res && !allow_trailing_comma)
1580 res = validate_numnodes(tree, i, "print_stmt");
1581 else if (res && i < nch)
1582 res = validate_comma(CHILD(tree, i));
1583 }
1584 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001585 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001586}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001587
1588
Guido van Rossum47478871996-08-21 14:32:37 +00001589static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001590validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001591{
1592 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001593 && validate_name(CHILD(tree, 0), "del")
1594 && validate_exprlist(CHILD(tree, 1)));
1595}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001596
1597
Guido van Rossum47478871996-08-21 14:32:37 +00001598static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001599validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001600{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001601 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001602 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001603 && ((nch == 1) || (nch == 2))
1604 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001605
Guido van Rossum3d602e31996-07-21 02:33:56 +00001606 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001607 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001608
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001609 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001610}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001611
1612
Guido van Rossum47478871996-08-21 14:32:37 +00001613static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001614validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001615{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001616 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001617 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001618 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001619
Guido van Rossum3d602e31996-07-21 02:33:56 +00001620 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001621 res = validate_name(CHILD(tree, 0), "raise");
1622 if (res && (nch >= 2))
1623 res = validate_test(CHILD(tree, 1));
1624 if (res && nch > 2) {
1625 res = (validate_comma(CHILD(tree, 2))
1626 && validate_test(CHILD(tree, 3)));
1627 if (res && (nch > 4))
1628 res = (validate_comma(CHILD(tree, 4))
1629 && validate_test(CHILD(tree, 5)));
1630 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001631 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001632 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001633 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001634 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001635 res = (validate_comma(CHILD(tree, 2))
1636 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001637
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001638 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001639}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001640
1641
Fred Drake02126f22001-07-17 02:59:15 +00001642/* yield_stmt: 'yield' testlist
1643 */
1644static int
1645validate_yield_stmt(node *tree)
1646{
1647 return (validate_ntype(tree, yield_stmt)
1648 && validate_numnodes(tree, 2, "yield_stmt")
1649 && validate_name(CHILD(tree, 0), "yield")
1650 && validate_testlist(CHILD(tree, 1)));
1651}
1652
1653
Fred Drakecff283c2000-08-21 22:24:43 +00001654static int
1655validate_import_as_name(node *tree)
1656{
1657 int nch = NCH(tree);
1658 int ok = validate_ntype(tree, import_as_name);
1659
1660 if (ok) {
1661 if (nch == 1)
1662 ok = validate_name(CHILD(tree, 0), NULL);
1663 else if (nch == 3)
1664 ok = (validate_name(CHILD(tree, 0), NULL)
1665 && validate_name(CHILD(tree, 1), "as")
1666 && validate_name(CHILD(tree, 2), NULL));
1667 else
1668 ok = validate_numnodes(tree, 3, "import_as_name");
1669 }
1670 return ok;
1671}
1672
1673
Fred Drake71137082001-01-07 05:59:59 +00001674/* dotted_name: NAME ("." NAME)*
1675 */
1676static int
1677validate_dotted_name(node *tree)
1678{
1679 int nch = NCH(tree);
1680 int res = (validate_ntype(tree, dotted_name)
1681 && is_odd(nch)
1682 && validate_name(CHILD(tree, 0), NULL));
1683 int i;
1684
1685 for (i = 1; res && (i < nch); i += 2) {
1686 res = (validate_dot(CHILD(tree, i))
1687 && validate_name(CHILD(tree, i+1), NULL));
1688 }
1689 return res;
1690}
1691
1692
Fred Drakecff283c2000-08-21 22:24:43 +00001693/* dotted_as_name: dotted_name [NAME NAME]
1694 */
1695static int
1696validate_dotted_as_name(node *tree)
1697{
1698 int nch = NCH(tree);
1699 int res = validate_ntype(tree, dotted_as_name);
1700
1701 if (res) {
1702 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001703 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001704 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001705 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001706 && validate_name(CHILD(tree, 1), "as")
1707 && validate_name(CHILD(tree, 2), NULL));
1708 else {
1709 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001710 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001711 }
1712 }
1713 return res;
1714}
1715
1716
Guido van Rossum3d602e31996-07-21 02:33:56 +00001717/* import_stmt:
1718 *
Fred Drakecff283c2000-08-21 22:24:43 +00001719 * 'import' dotted_as_name (',' dotted_as_name)*
1720 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001721 */
Guido van Rossum47478871996-08-21 14:32:37 +00001722static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001723validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001724{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001725 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001726 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001727 && (nch >= 2) && is_even(nch)
Fred Drakecff283c2000-08-21 22:24:43 +00001728 && validate_ntype(CHILD(tree, 0), NAME));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001729
1730 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001731 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001732
Fred Drakecff283c2000-08-21 22:24:43 +00001733 res = validate_dotted_as_name(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00001734 for (j = 2; res && (j < nch); j += 2)
1735 res = (validate_comma(CHILD(tree, j))
Fred Drake71137082001-01-07 05:59:59 +00001736 && validate_dotted_as_name(CHILD(tree, j + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001737 }
Fred Drakecff283c2000-08-21 22:24:43 +00001738 else if (res && (res = validate_name(CHILD(tree, 0), "from"))) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001739 res = ((nch >= 4) && is_even(nch)
Fred Drake71137082001-01-07 05:59:59 +00001740 && validate_dotted_name(CHILD(tree, 1))
1741 && validate_name(CHILD(tree, 2), "import"));
Fred Drakeff9ea482000-04-19 13:54:15 +00001742 if (nch == 4) {
Fred Drakecff283c2000-08-21 22:24:43 +00001743 if (TYPE(CHILD(tree, 3)) == import_as_name)
1744 res = validate_import_as_name(CHILD(tree, 3));
1745 else
1746 res = validate_star(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001747 }
1748 else {
Fred Drakecff283c2000-08-21 22:24:43 +00001749 /* 'from' dotted_name 'import' import_as_name
1750 * (',' import_as_name)+
1751 */
Fred Drakeff9ea482000-04-19 13:54:15 +00001752 int j;
Fred Drakecff283c2000-08-21 22:24:43 +00001753 res = validate_import_as_name(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001754 for (j = 4; res && (j < nch); j += 2)
1755 res = (validate_comma(CHILD(tree, j))
Fred Drakecff283c2000-08-21 22:24:43 +00001756 && validate_import_as_name(CHILD(tree, j + 1)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001757 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001758 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001759 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001760 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001761
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001762 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001763}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001764
1765
Guido van Rossum47478871996-08-21 14:32:37 +00001766static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001767validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001768{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001769 int j;
1770 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001771 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001772 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001773
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001774 if (!res && !PyErr_Occurred())
1775 err_string("illegal global statement");
1776
Guido van Rossum3d602e31996-07-21 02:33:56 +00001777 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001778 res = (validate_name(CHILD(tree, 0), "global")
1779 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001780 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001781 res = (validate_comma(CHILD(tree, j))
1782 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001783
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001784 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001785}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001786
1787
Guido van Rossum3d602e31996-07-21 02:33:56 +00001788/* exec_stmt:
1789 *
1790 * 'exec' expr ['in' test [',' test]]
1791 */
Guido van Rossum47478871996-08-21 14:32:37 +00001792static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001793validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001794{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001795 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001796 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001797 && ((nch == 2) || (nch == 4) || (nch == 6))
1798 && validate_name(CHILD(tree, 0), "exec")
1799 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001800
Guido van Rossum3d602e31996-07-21 02:33:56 +00001801 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001802 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001803 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001804 res = (validate_name(CHILD(tree, 2), "in")
1805 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001806 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001807 res = (validate_comma(CHILD(tree, 4))
1808 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001809
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001810 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001811}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001812
1813
Guido van Rossum925e5471997-04-02 05:32:13 +00001814/* assert_stmt:
1815 *
1816 * 'assert' test [',' test]
1817 */
1818static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001819validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001820{
1821 int nch = NCH(tree);
1822 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001823 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001824 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001825 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001826
1827 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001828 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001829 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001830 res = (validate_comma(CHILD(tree, 2))
1831 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001832
1833 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001834}
Guido van Rossum925e5471997-04-02 05:32:13 +00001835
1836
Guido van Rossum47478871996-08-21 14:32:37 +00001837static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001838validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001839{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001840 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001841 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001842 && ((nch == 4) || (nch == 7))
1843 && validate_name(CHILD(tree, 0), "while")
1844 && validate_test(CHILD(tree, 1))
1845 && validate_colon(CHILD(tree, 2))
1846 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001847
Guido van Rossum3d602e31996-07-21 02:33:56 +00001848 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001849 res = (validate_name(CHILD(tree, 4), "else")
1850 && validate_colon(CHILD(tree, 5))
1851 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001852
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001853 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001854}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001855
1856
Guido van Rossum47478871996-08-21 14:32:37 +00001857static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001858validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001859{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001860 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001861 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001862 && ((nch == 6) || (nch == 9))
1863 && validate_name(CHILD(tree, 0), "for")
1864 && validate_exprlist(CHILD(tree, 1))
1865 && validate_name(CHILD(tree, 2), "in")
1866 && validate_testlist(CHILD(tree, 3))
1867 && validate_colon(CHILD(tree, 4))
1868 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001869
Guido van Rossum3d602e31996-07-21 02:33:56 +00001870 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001871 res = (validate_name(CHILD(tree, 6), "else")
1872 && validate_colon(CHILD(tree, 7))
1873 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001874
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001875 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001876}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001877
1878
Guido van Rossum3d602e31996-07-21 02:33:56 +00001879/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001880 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001881 * | 'try' ':' suite 'finally' ':' suite
1882 *
1883 */
Guido van Rossum47478871996-08-21 14:32:37 +00001884static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001885validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001886{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001887 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001888 int pos = 3;
1889 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001890 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001891
1892 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001893 res = (validate_name(CHILD(tree, 0), "try")
1894 && validate_colon(CHILD(tree, 1))
1895 && validate_suite(CHILD(tree, 2))
1896 && validate_colon(CHILD(tree, nch - 2))
1897 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001898 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001899 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001900 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1901 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001902
1903 PyErr_Format(parser_error,
1904 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001905 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001906 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001907 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001908 res = (validate_except_clause(CHILD(tree, pos))
1909 && validate_colon(CHILD(tree, pos + 1))
1910 && validate_suite(CHILD(tree, pos + 2)));
1911 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001912 }
1913 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001914 res = validate_ntype(CHILD(tree, pos), NAME);
1915 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1916 res = (validate_numnodes(tree, 6, "try/finally")
1917 && validate_colon(CHILD(tree, 4))
1918 && validate_suite(CHILD(tree, 5)));
1919 else if (res) {
1920 if (nch == (pos + 3)) {
1921 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1922 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1923 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001924 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001925 }
1926 else if (nch == (pos + 6)) {
1927 res = (validate_name(CHILD(tree, pos), "except")
1928 && validate_colon(CHILD(tree, pos + 1))
1929 && validate_suite(CHILD(tree, pos + 2))
1930 && validate_name(CHILD(tree, pos + 3), "else"));
1931 }
1932 else
1933 res = validate_numnodes(tree, pos + 3, "try/except");
1934 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001935 }
1936 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001937}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001938
1939
Guido van Rossum47478871996-08-21 14:32:37 +00001940static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001941validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001942{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001943 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001944 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001945 && ((nch == 1) || (nch == 2) || (nch == 4))
1946 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001947
Guido van Rossum3d602e31996-07-21 02:33:56 +00001948 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001949 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001950 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001951 res = (validate_comma(CHILD(tree, 2))
1952 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001953
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001954 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001955}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001956
1957
Guido van Rossum47478871996-08-21 14:32:37 +00001958static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001959validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001960{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001961 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001962 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001963
Guido van Rossum3d602e31996-07-21 02:33:56 +00001964 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001965 res = ((nch == 1)
1966 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001967 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001968 int pos;
1969 res = validate_and_test(CHILD(tree, 0));
1970 for (pos = 1; res && (pos < nch); pos += 2)
1971 res = (validate_name(CHILD(tree, pos), "or")
1972 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001973 }
1974 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001975}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001976
1977
Guido van Rossum47478871996-08-21 14:32:37 +00001978static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001979validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001980{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001981 int pos;
1982 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001983 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001984 && is_odd(nch)
1985 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001986
Guido van Rossum3d602e31996-07-21 02:33:56 +00001987 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001988 res = (validate_name(CHILD(tree, pos), "and")
1989 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001990
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001991 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001992}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001993
1994
Guido van Rossum47478871996-08-21 14:32:37 +00001995static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001996validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001997{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001998 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001999 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002000
Guido van Rossum3d602e31996-07-21 02:33:56 +00002001 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002002 if (nch == 2)
2003 res = (validate_name(CHILD(tree, 0), "not")
2004 && validate_not_test(CHILD(tree, 1)));
2005 else if (nch == 1)
2006 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002007 }
2008 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002009}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002010
2011
Guido van Rossum47478871996-08-21 14:32:37 +00002012static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002013validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002014{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002015 int pos;
2016 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002017 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002018 && is_odd(nch)
2019 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002020
Guido van Rossum3d602e31996-07-21 02:33:56 +00002021 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002022 res = (validate_comp_op(CHILD(tree, pos))
2023 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002024
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002025 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002026}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002027
2028
Guido van Rossum47478871996-08-21 14:32:37 +00002029static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002030validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002031{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002032 int res = 0;
2033 int nch = NCH(tree);
2034
Guido van Rossum3d602e31996-07-21 02:33:56 +00002035 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002036 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002037 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002038 /*
2039 * Only child will be a terminal with a well-defined symbolic name
2040 * or a NAME with a string of either 'is' or 'in'
2041 */
2042 tree = CHILD(tree, 0);
2043 switch (TYPE(tree)) {
2044 case LESS:
2045 case GREATER:
2046 case EQEQUAL:
2047 case EQUAL:
2048 case LESSEQUAL:
2049 case GREATEREQUAL:
2050 case NOTEQUAL:
2051 res = 1;
2052 break;
2053 case NAME:
2054 res = ((strcmp(STR(tree), "in") == 0)
2055 || (strcmp(STR(tree), "is") == 0));
2056 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002057 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002058 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002059 }
2060 break;
2061 default:
Fred Drake661ea262000-10-24 19:57:45 +00002062 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002063 break;
2064 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002065 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002066 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002067 res = (validate_ntype(CHILD(tree, 0), NAME)
2068 && validate_ntype(CHILD(tree, 1), NAME)
2069 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2070 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2071 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2072 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2073 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002074 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002075 }
2076 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002077}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002078
2079
Guido van Rossum47478871996-08-21 14:32:37 +00002080static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002081validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002082{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002083 int j;
2084 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002085 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002086 && is_odd(nch)
2087 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002088
Guido van Rossum3d602e31996-07-21 02:33:56 +00002089 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002090 res = (validate_xor_expr(CHILD(tree, j))
2091 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002092
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002093 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002094}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002095
2096
Guido van Rossum47478871996-08-21 14:32:37 +00002097static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002098validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002099{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002100 int j;
2101 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002102 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002103 && is_odd(nch)
2104 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002105
Guido van Rossum3d602e31996-07-21 02:33:56 +00002106 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002107 res = (validate_circumflex(CHILD(tree, j - 1))
2108 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002109
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002110 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002111}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002112
2113
Guido van Rossum47478871996-08-21 14:32:37 +00002114static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002115validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002116{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002117 int pos;
2118 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002119 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002120 && is_odd(nch)
2121 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002122
Guido van Rossum3d602e31996-07-21 02:33:56 +00002123 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002124 res = (validate_ampersand(CHILD(tree, pos))
2125 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002126
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002127 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002128}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002129
2130
2131static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002132validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002133 {
2134 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002135 int nch = NCH(tree);
2136 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002137 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002138
Guido van Rossum3d602e31996-07-21 02:33:56 +00002139 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002140 if (TYPE(CHILD(tree, pos)) != op1)
2141 res = validate_ntype(CHILD(tree, pos), op2);
2142 if (res)
2143 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002144 }
2145 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002146}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002147
2148
Guido van Rossum47478871996-08-21 14:32:37 +00002149static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002150validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002151{
2152 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002153 && validate_chain_two_ops(tree, validate_arith_expr,
2154 LEFTSHIFT, RIGHTSHIFT));
2155}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002156
2157
Guido van Rossum47478871996-08-21 14:32:37 +00002158static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002159validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002160{
2161 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002162 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2163}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002164
2165
Guido van Rossum47478871996-08-21 14:32:37 +00002166static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002167validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002168{
2169 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002170 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002171 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002172 && is_odd(nch)
2173 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002174
Guido van Rossum3d602e31996-07-21 02:33:56 +00002175 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002176 res = (((TYPE(CHILD(tree, pos)) == STAR)
2177 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002178 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002179 || (TYPE(CHILD(tree, pos)) == PERCENT))
2180 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002181
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002182 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002183}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002184
2185
Guido van Rossum3d602e31996-07-21 02:33:56 +00002186/* factor:
2187 *
2188 * factor: ('+'|'-'|'~') factor | power
2189 */
Guido van Rossum47478871996-08-21 14:32:37 +00002190static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002191validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002192{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002193 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002194 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002195 && (((nch == 2)
2196 && ((TYPE(CHILD(tree, 0)) == PLUS)
2197 || (TYPE(CHILD(tree, 0)) == MINUS)
2198 || (TYPE(CHILD(tree, 0)) == TILDE))
2199 && validate_factor(CHILD(tree, 1)))
2200 || ((nch == 1)
2201 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002202 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002203}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002204
2205
Guido van Rossum3d602e31996-07-21 02:33:56 +00002206/* power:
2207 *
2208 * power: atom trailer* ('**' factor)*
2209 */
Guido van Rossum47478871996-08-21 14:32:37 +00002210static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002211validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002212{
2213 int pos = 1;
2214 int nch = NCH(tree);
2215 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002216 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002217
2218 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002219 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002220 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002221 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002222 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002223 return (0);
2224 }
2225 for ( ; res && (pos < (nch - 1)); pos += 2)
2226 res = (validate_doublestar(CHILD(tree, pos))
2227 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002228 }
2229 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002230}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002231
2232
Guido van Rossum47478871996-08-21 14:32:37 +00002233static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002234validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002235{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002236 int pos;
2237 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002238 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002239
Fred Drakecff283c2000-08-21 22:24:43 +00002240 if (res && nch < 1)
2241 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002242 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002243 switch (TYPE(CHILD(tree, 0))) {
2244 case LPAR:
2245 res = ((nch <= 3)
2246 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002247
Fred Drakeff9ea482000-04-19 13:54:15 +00002248 if (res && (nch == 3))
Raymond Hettinger354433a2004-05-19 08:20:33 +00002249 res = validate_testlist_gexp(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002250 break;
2251 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002252 if (nch == 2)
2253 res = validate_ntype(CHILD(tree, 1), RSQB);
2254 else if (nch == 3)
2255 res = (validate_listmaker(CHILD(tree, 1))
2256 && validate_ntype(CHILD(tree, 2), RSQB));
2257 else {
2258 res = 0;
2259 err_string("illegal list display atom");
2260 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002261 break;
2262 case LBRACE:
2263 res = ((nch <= 3)
2264 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002265
Fred Drakeff9ea482000-04-19 13:54:15 +00002266 if (res && (nch == 3))
2267 res = validate_dictmaker(CHILD(tree, 1));
2268 break;
2269 case BACKQUOTE:
2270 res = ((nch == 3)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002271 && validate_testlist1(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002272 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2273 break;
2274 case NAME:
2275 case NUMBER:
2276 res = (nch == 1);
2277 break;
2278 case STRING:
2279 for (pos = 1; res && (pos < nch); ++pos)
2280 res = validate_ntype(CHILD(tree, pos), STRING);
2281 break;
2282 default:
2283 res = 0;
2284 break;
2285 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002286 }
2287 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002288}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002289
2290
Fred Drake85bf3bb2000-08-23 15:35:26 +00002291/* listmaker:
2292 * test ( list_for | (',' test)* [','] )
2293 */
Fred Drakecff283c2000-08-21 22:24:43 +00002294static int
2295validate_listmaker(node *tree)
2296{
2297 int nch = NCH(tree);
2298 int ok = nch;
2299
2300 if (nch == 0)
2301 err_string("missing child nodes of listmaker");
2302 else
2303 ok = validate_test(CHILD(tree, 0));
2304
2305 /*
Raymond Hettinger354433a2004-05-19 08:20:33 +00002306 * list_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002307 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002308 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2309 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002310 else {
2311 /* (',' test)* [','] */
2312 int i = 1;
2313 while (ok && nch - i >= 2) {
2314 ok = (validate_comma(CHILD(tree, i))
2315 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002316 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002317 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002318 if (ok && i == nch-1)
2319 ok = validate_comma(CHILD(tree, i));
2320 else if (i != nch) {
2321 ok = 0;
2322 err_string("illegal trailing nodes for listmaker");
2323 }
Fred Drakecff283c2000-08-21 22:24:43 +00002324 }
2325 return ok;
2326}
2327
Raymond Hettinger354433a2004-05-19 08:20:33 +00002328/* testlist_gexp:
2329 * test ( gen_for | (',' test)* [','] )
2330 */
2331static int
2332validate_testlist_gexp(node *tree)
2333{
2334 int nch = NCH(tree);
2335 int ok = nch;
2336
2337 if (nch == 0)
2338 err_string("missing child nodes of testlist_gexp");
2339 else {
2340 ok = validate_test(CHILD(tree, 0));
2341 }
2342
2343 /*
2344 * gen_for | (',' test)* [',']
2345 */
2346 if (nch == 2 && TYPE(CHILD(tree, 1)) == gen_for)
2347 ok = validate_gen_for(CHILD(tree, 1));
2348 else {
2349 /* (',' test)* [','] */
2350 int i = 1;
2351 while (ok && nch - i >= 2) {
2352 ok = (validate_comma(CHILD(tree, i))
2353 && validate_test(CHILD(tree, i+1)));
2354 i += 2;
2355 }
2356 if (ok && i == nch-1)
2357 ok = validate_comma(CHILD(tree, i));
2358 else if (i != nch) {
2359 ok = 0;
2360 err_string("illegal trailing nodes for testlist_gexp");
2361 }
2362 }
2363 return ok;
2364}
Fred Drakecff283c2000-08-21 22:24:43 +00002365
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002366/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002367 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002368 */
2369static int
2370validate_decorator(node *tree)
2371{
2372 int ok;
2373 int nch = NCH(tree);
2374 ok = (validate_ntype(tree, decorator) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002375 (nch == 3 || nch == 5 || nch == 6) &&
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002376 validate_at(CHILD(tree, 0)) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002377 validate_dotted_name(CHILD(tree, 1)) &&
2378 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002379
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002380 if (ok && nch != 3) {
2381 ok = (validate_lparen(CHILD(tree, 2)) &&
2382 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002383
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002384 if (ok && nch == 6)
2385 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002386 }
2387
2388 return ok;
2389}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002390
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002391/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002392 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002393 */
2394static int
2395validate_decorators(node *tree)
2396{
2397 int i, nch, ok;
2398 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002399 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002400
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002401 for (i = 0; ok && i < nch; ++i)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002402 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002403
2404 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002405}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002406
Guido van Rossum3d602e31996-07-21 02:33:56 +00002407/* funcdef:
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002408 *
2409 * -6 -5 -4 -3 -2 -1
2410 * [decorators] 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002411 */
Guido van Rossum47478871996-08-21 14:32:37 +00002412static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002413validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002414{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002415 int nch = NCH(tree);
2416 int ok = (validate_ntype(tree, funcdef)
2417 && ((nch == 5) || (nch == 6))
2418 && validate_name(RCHILD(tree, -5), "def")
2419 && validate_ntype(RCHILD(tree, -4), NAME)
2420 && validate_colon(RCHILD(tree, -2))
2421 && validate_parameters(RCHILD(tree, -3))
2422 && validate_suite(RCHILD(tree, -1)));
2423
2424 if (ok && (nch == 6))
2425 ok = validate_decorators(CHILD(tree, 0));
2426
2427 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002428}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002429
2430
Guido van Rossum47478871996-08-21 14:32:37 +00002431static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002432validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002433{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002434 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002435 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002436 && ((nch == 3) || (nch == 4))
2437 && validate_name(CHILD(tree, 0), "lambda")
2438 && validate_colon(CHILD(tree, nch - 2))
2439 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002440
Guido van Rossum3d602e31996-07-21 02:33:56 +00002441 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002442 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002443 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002444 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002445
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002446 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002447}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002448
2449
Guido van Rossum3d602e31996-07-21 02:33:56 +00002450/* arglist:
2451 *
Fred Drakecff283c2000-08-21 22:24:43 +00002452 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002453 */
Guido van Rossum47478871996-08-21 14:32:37 +00002454static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002455validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002456{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002457 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002458 int i = 0;
2459 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002460
2461 if (nch <= 0)
2462 /* raise the right error from having an invalid number of children */
2463 return validate_numnodes(tree, nch + 1, "arglist");
2464
Raymond Hettinger354433a2004-05-19 08:20:33 +00002465 if (nch > 1) {
2466 for (i=0; i<nch; i++) {
2467 if (TYPE(CHILD(tree, i)) == argument) {
2468 node *ch = CHILD(tree, i);
2469 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == gen_for) {
2470 err_string("need '(', ')' for generator expression");
2471 return 0;
2472 }
2473 }
2474 }
2475 }
2476
Fred Drakecff283c2000-08-21 22:24:43 +00002477 while (ok && nch-i >= 2) {
2478 /* skip leading (argument ',') */
2479 ok = (validate_argument(CHILD(tree, i))
2480 && validate_comma(CHILD(tree, i+1)));
2481 if (ok)
2482 i += 2;
2483 else
2484 PyErr_Clear();
2485 }
2486 ok = 1;
2487 if (nch-i > 0) {
2488 /*
2489 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002490 */
Fred Drakecff283c2000-08-21 22:24:43 +00002491 int sym = TYPE(CHILD(tree, i));
2492
2493 if (sym == argument) {
2494 ok = validate_argument(CHILD(tree, i));
2495 if (ok && i+1 != nch) {
2496 err_string("illegal arglist specification"
2497 " (extra stuff on end)");
2498 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002499 }
Fred Drakecff283c2000-08-21 22:24:43 +00002500 }
2501 else if (sym == STAR) {
2502 ok = validate_star(CHILD(tree, i));
2503 if (ok && (nch-i == 2))
2504 ok = validate_test(CHILD(tree, i+1));
2505 else if (ok && (nch-i == 5))
2506 ok = (validate_test(CHILD(tree, i+1))
2507 && validate_comma(CHILD(tree, i+2))
2508 && validate_doublestar(CHILD(tree, i+3))
2509 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002510 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002511 err_string("illegal use of '*' in arglist");
2512 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002513 }
2514 }
Fred Drakecff283c2000-08-21 22:24:43 +00002515 else if (sym == DOUBLESTAR) {
2516 if (nch-i == 2)
2517 ok = (validate_doublestar(CHILD(tree, i))
2518 && validate_test(CHILD(tree, i+1)));
2519 else {
2520 err_string("illegal use of '**' in arglist");
2521 ok = 0;
2522 }
2523 }
2524 else {
2525 err_string("illegal arglist specification");
2526 ok = 0;
2527 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002528 }
2529 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002530}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002531
2532
2533
2534/* argument:
2535 *
Raymond Hettinger354433a2004-05-19 08:20:33 +00002536 * [test '='] test [gen_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002537 */
Guido van Rossum47478871996-08-21 14:32:37 +00002538static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002539validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002540{
2541 int nch = NCH(tree);
2542 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002543 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002544 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002545
Raymond Hettinger354433a2004-05-19 08:20:33 +00002546 if (res && (nch == 2))
2547 res = validate_gen_for(CHILD(tree, 1));
2548 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002549 res = (validate_equal(CHILD(tree, 1))
2550 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002551
2552 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002553}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002554
2555
2556
2557/* trailer:
2558 *
Guido van Rossum47478871996-08-21 14:32:37 +00002559 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002560 */
Guido van Rossum47478871996-08-21 14:32:37 +00002561static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002562validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002563{
2564 int nch = NCH(tree);
2565 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002566
2567 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002568 switch (TYPE(CHILD(tree, 0))) {
2569 case LPAR:
2570 res = validate_rparen(CHILD(tree, nch - 1));
2571 if (res && (nch == 3))
2572 res = validate_arglist(CHILD(tree, 1));
2573 break;
2574 case LSQB:
2575 res = (validate_numnodes(tree, 3, "trailer")
2576 && validate_subscriptlist(CHILD(tree, 1))
2577 && validate_ntype(CHILD(tree, 2), RSQB));
2578 break;
2579 case DOT:
2580 res = (validate_numnodes(tree, 2, "trailer")
2581 && validate_ntype(CHILD(tree, 1), NAME));
2582 break;
2583 default:
2584 res = 0;
2585 break;
2586 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002587 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002588 else {
2589 (void) validate_numnodes(tree, 2, "trailer");
2590 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002591 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002592}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002593
2594
Guido van Rossum47478871996-08-21 14:32:37 +00002595/* subscriptlist:
2596 *
2597 * subscript (',' subscript)* [',']
2598 */
2599static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002600validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002601{
2602 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002603 validate_subscript, "subscriptlist"));
2604}
Guido van Rossum47478871996-08-21 14:32:37 +00002605
2606
Guido van Rossum3d602e31996-07-21 02:33:56 +00002607/* subscript:
2608 *
Guido van Rossum47478871996-08-21 14:32:37 +00002609 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002610 */
Guido van Rossum47478871996-08-21 14:32:37 +00002611static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002612validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002613{
Guido van Rossum47478871996-08-21 14:32:37 +00002614 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002615 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002616 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002617
Guido van Rossum47478871996-08-21 14:32:37 +00002618 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002619 if (!PyErr_Occurred())
2620 err_string("invalid number of arguments for subscript node");
2621 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002622 }
Guido van Rossum47478871996-08-21 14:32:37 +00002623 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002624 /* take care of ('.' '.' '.') possibility */
2625 return (validate_numnodes(tree, 3, "subscript")
2626 && validate_dot(CHILD(tree, 0))
2627 && validate_dot(CHILD(tree, 1))
2628 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002629 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002630 if (TYPE(CHILD(tree, 0)) == test)
2631 res = validate_test(CHILD(tree, 0));
2632 else
2633 res = validate_colon(CHILD(tree, 0));
2634 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002635 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002636 /* Must be [test] ':' [test] [sliceop],
2637 * but at least one of the optional components will
2638 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002639 */
2640 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002641 res = validate_test(CHILD(tree, 0));
2642 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002643 }
2644 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002645 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002646 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002647 int rem = nch - ++offset;
2648 if (rem) {
2649 if (TYPE(CHILD(tree, offset)) == test) {
2650 res = validate_test(CHILD(tree, offset));
2651 ++offset;
2652 --rem;
2653 }
2654 if (res && rem)
2655 res = validate_sliceop(CHILD(tree, offset));
2656 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002657 }
2658 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002659}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002660
2661
Guido van Rossum47478871996-08-21 14:32:37 +00002662static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002663validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002664{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002665 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002666 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002667 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002668 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002669 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002670 }
Guido van Rossum47478871996-08-21 14:32:37 +00002671 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002672 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002673 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002674 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002675
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002676 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002677}
Guido van Rossum47478871996-08-21 14:32:37 +00002678
2679
2680static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002681validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002682{
2683 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002684 validate_expr, "exprlist"));
2685}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002686
2687
Guido van Rossum47478871996-08-21 14:32:37 +00002688static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002689validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002690{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002691 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002692 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002693 && (nch >= 3)
2694 && validate_test(CHILD(tree, 0))
2695 && validate_colon(CHILD(tree, 1))
2696 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002697
Guido van Rossum3d602e31996-07-21 02:33:56 +00002698 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002699 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002700 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002701 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002702
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002703 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002704 int pos = 3;
2705 /* ( ',' test ':' test )* */
2706 while (res && (pos < nch)) {
2707 res = (validate_comma(CHILD(tree, pos))
2708 && validate_test(CHILD(tree, pos + 1))
2709 && validate_colon(CHILD(tree, pos + 2))
2710 && validate_test(CHILD(tree, pos + 3)));
2711 pos += 4;
2712 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002713 }
2714 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002715}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002716
2717
Guido van Rossum47478871996-08-21 14:32:37 +00002718static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002719validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002720{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002721 int pos;
2722 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002723 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002724 && (nch >= 2)
2725 && validate_testlist(CHILD(tree, 0))
2726 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002727
Guido van Rossum3d602e31996-07-21 02:33:56 +00002728 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002729 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002730
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002731 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002732}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002733
2734
Guido van Rossum47478871996-08-21 14:32:37 +00002735static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002736validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002737{
Fred Drakeff9ea482000-04-19 13:54:15 +00002738 int nch = 0; /* num. children on current node */
2739 int res = 1; /* result value */
2740 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002741
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002742 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002743 nch = NCH(tree);
2744 next = 0;
2745 switch (TYPE(tree)) {
2746 /*
2747 * Definition nodes.
2748 */
2749 case funcdef:
2750 res = validate_funcdef(tree);
2751 break;
2752 case classdef:
2753 res = validate_class(tree);
2754 break;
2755 /*
2756 * "Trivial" parse tree nodes.
2757 * (Why did I call these trivial?)
2758 */
2759 case stmt:
2760 res = validate_stmt(tree);
2761 break;
2762 case small_stmt:
2763 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002764 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002765 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2766 */
2767 res = validate_small_stmt(tree);
2768 break;
2769 case flow_stmt:
2770 res = (validate_numnodes(tree, 1, "flow_stmt")
2771 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2772 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002773 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002774 || (TYPE(CHILD(tree, 0)) == return_stmt)
2775 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2776 if (res)
2777 next = CHILD(tree, 0);
2778 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002779 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002780 break;
Fred Drake02126f22001-07-17 02:59:15 +00002781 case yield_stmt:
2782 res = validate_yield_stmt(tree);
2783 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002784 /*
2785 * Compound statements.
2786 */
2787 case simple_stmt:
2788 res = validate_simple_stmt(tree);
2789 break;
2790 case compound_stmt:
2791 res = validate_compound_stmt(tree);
2792 break;
2793 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002794 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002795 */
2796 case expr_stmt:
2797 res = validate_expr_stmt(tree);
2798 break;
2799 case print_stmt:
2800 res = validate_print_stmt(tree);
2801 break;
2802 case del_stmt:
2803 res = validate_del_stmt(tree);
2804 break;
2805 case pass_stmt:
2806 res = (validate_numnodes(tree, 1, "pass")
2807 && validate_name(CHILD(tree, 0), "pass"));
2808 break;
2809 case break_stmt:
2810 res = (validate_numnodes(tree, 1, "break")
2811 && validate_name(CHILD(tree, 0), "break"));
2812 break;
2813 case continue_stmt:
2814 res = (validate_numnodes(tree, 1, "continue")
2815 && validate_name(CHILD(tree, 0), "continue"));
2816 break;
2817 case return_stmt:
2818 res = validate_return_stmt(tree);
2819 break;
2820 case raise_stmt:
2821 res = validate_raise_stmt(tree);
2822 break;
2823 case import_stmt:
2824 res = validate_import_stmt(tree);
2825 break;
2826 case global_stmt:
2827 res = validate_global_stmt(tree);
2828 break;
2829 case exec_stmt:
2830 res = validate_exec_stmt(tree);
2831 break;
2832 case assert_stmt:
2833 res = validate_assert_stmt(tree);
2834 break;
2835 case if_stmt:
2836 res = validate_if(tree);
2837 break;
2838 case while_stmt:
2839 res = validate_while(tree);
2840 break;
2841 case for_stmt:
2842 res = validate_for(tree);
2843 break;
2844 case try_stmt:
2845 res = validate_try(tree);
2846 break;
2847 case suite:
2848 res = validate_suite(tree);
2849 break;
2850 /*
2851 * Expression nodes.
2852 */
2853 case testlist:
2854 res = validate_testlist(tree);
2855 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002856 case testlist1:
2857 res = validate_testlist1(tree);
2858 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002859 case test:
2860 res = validate_test(tree);
2861 break;
2862 case and_test:
2863 res = validate_and_test(tree);
2864 break;
2865 case not_test:
2866 res = validate_not_test(tree);
2867 break;
2868 case comparison:
2869 res = validate_comparison(tree);
2870 break;
2871 case exprlist:
2872 res = validate_exprlist(tree);
2873 break;
2874 case comp_op:
2875 res = validate_comp_op(tree);
2876 break;
2877 case expr:
2878 res = validate_expr(tree);
2879 break;
2880 case xor_expr:
2881 res = validate_xor_expr(tree);
2882 break;
2883 case and_expr:
2884 res = validate_and_expr(tree);
2885 break;
2886 case shift_expr:
2887 res = validate_shift_expr(tree);
2888 break;
2889 case arith_expr:
2890 res = validate_arith_expr(tree);
2891 break;
2892 case term:
2893 res = validate_term(tree);
2894 break;
2895 case factor:
2896 res = validate_factor(tree);
2897 break;
2898 case power:
2899 res = validate_power(tree);
2900 break;
2901 case atom:
2902 res = validate_atom(tree);
2903 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002904
Fred Drakeff9ea482000-04-19 13:54:15 +00002905 default:
2906 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002907 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002908 res = 0;
2909 break;
2910 }
2911 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002912 }
2913 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002914}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002915
2916
Guido van Rossum47478871996-08-21 14:32:37 +00002917static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002918validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002919{
2920 int res = validate_eval_input(tree);
2921
2922 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002923 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002924
2925 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002926}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002927
2928
Guido van Rossum3d602e31996-07-21 02:33:56 +00002929/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002930 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002931 */
Guido van Rossum47478871996-08-21 14:32:37 +00002932static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002933validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002934{
Fred Drakec2683dd2001-07-17 19:32:05 +00002935 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002936 int nch = NCH(tree) - 1;
2937 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002938 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002939
Fred Drakec2683dd2001-07-17 19:32:05 +00002940 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002941 if (TYPE(CHILD(tree, j)) == stmt)
2942 res = validate_stmt(CHILD(tree, j));
2943 else
2944 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002945 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002946 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002947 * user. Hopefully, this won't be needed. If a user reports getting
2948 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002949 */
2950 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002951 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002952
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002953 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002954}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002955
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00002956static int
2957validate_encoding_decl(node *tree)
2958{
2959 int nch = NCH(tree);
2960 int res = ((nch == 1)
2961 && validate_file_input(CHILD(tree, 0)));
2962
2963 if (!res && !PyErr_Occurred())
2964 err_string("Error Parsing encoding_decl");
2965
2966 return res;
2967}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002968
Fred Drake43f8f9b1998-04-13 16:25:46 +00002969static PyObject*
2970pickle_constructor = NULL;
2971
2972
2973static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002974parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002975{
Fred Drake268397f1998-04-29 14:16:32 +00002976 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002977 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00002978 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002979 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002980
Fred Drakec2683dd2001-07-17 19:32:05 +00002981 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002982 PyObject *newargs;
2983 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002984
Fred Drake2a6875e1999-09-20 22:32:18 +00002985 if ((empty_dict = PyDict_New()) == NULL)
2986 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002987 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002988 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002989 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002990 if (tuple != NULL) {
2991 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2992 Py_DECREF(tuple);
2993 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002994 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002995 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002996 }
2997 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002998 Py_XDECREF(empty_dict);
2999
Fred Drake43f8f9b1998-04-13 16:25:46 +00003000 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003001}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003002
3003
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003004/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003005 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003006 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003007 * We'd really have to write a wrapper around it all anyway to allow
3008 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003009 */
3010static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003011 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003012 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003013 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003014 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003015 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003016 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003017 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003018 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003019 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003020 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003021 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003022 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003023 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003024 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003025 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003026 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003027 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003028 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003029 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003030 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003031 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003032 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003033 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003034 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003035 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003036 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003037 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003038 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003039
Fred Drake43f8f9b1998-04-13 16:25:46 +00003040 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003041 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003042 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003043
Fred Drake268397f1998-04-29 14:16:32 +00003044 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003045 };
3046
3047
Mark Hammond62b1ab12002-07-23 06:31:15 +00003048PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003049
Mark Hammond62b1ab12002-07-23 06:31:15 +00003050PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00003051initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003052{
Fred Drake13130bc2001-08-15 16:44:56 +00003053 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003054
3055 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00003056 module = Py_InitModule("parser", parser_functions);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003057
Fred Drake7a15ba51999-09-09 14:21:52 +00003058 if (parser_error == 0)
3059 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003060
Tim Peters6a627252003-07-21 14:25:23 +00003061 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00003062 /* caller will check PyErr_Occurred() */
3063 return;
Tim Peters6a627252003-07-21 14:25:23 +00003064 /* CAUTION: The code next used to skip bumping the refcount on
3065 * parser_error. That's a disaster if initparser() gets called more
3066 * than once. By incref'ing, we ensure that each module dict that
3067 * gets created owns its reference to the shared parser_error object,
3068 * and the file static parser_error vrbl owns a reference too.
3069 */
3070 Py_INCREF(parser_error);
3071 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3072 return;
3073
Fred Drakec2683dd2001-07-17 19:32:05 +00003074 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003075 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00003076 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003077 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003078
Fred Drake13130bc2001-08-15 16:44:56 +00003079 PyModule_AddStringConstant(module, "__copyright__",
3080 parser_copyright_string);
3081 PyModule_AddStringConstant(module, "__doc__",
3082 parser_doc_string);
3083 PyModule_AddStringConstant(module, "__version__",
3084 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003085
Fred Drake78bdb9b2001-07-19 20:17:15 +00003086 /* Register to support pickling.
3087 * If this fails, the import of this module will fail because an
3088 * exception will be raised here; should we clear the exception?
3089 */
Fred Drake13130bc2001-08-15 16:44:56 +00003090 copyreg = PyImport_ImportModule("copy_reg");
3091 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003092 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003093
Fred Drake13130bc2001-08-15 16:44:56 +00003094 func = PyObject_GetAttrString(copyreg, "pickle");
3095 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3096 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003097 Py_XINCREF(pickle_constructor);
3098 if ((func != NULL) && (pickle_constructor != NULL)
3099 && (pickler != NULL)) {
3100 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003101
Fred Drakec2683dd2001-07-17 19:32:05 +00003102 res = PyObject_CallFunction(func, "OOO", &PyST_Type, pickler,
3103 pickle_constructor);
Fred Drakeff9ea482000-04-19 13:54:15 +00003104 Py_XDECREF(res);
3105 }
3106 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003107 Py_XDECREF(pickle_constructor);
3108 Py_XDECREF(pickler);
3109 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003110 }
Fred Drakeff9ea482000-04-19 13:54:15 +00003111}