blob: eb23b585724f611e35a625f192bb6dfe4bd5a672 [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);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000842VALIDATER(import_name); VALIDATER(import_from);
Fred Drakecff283c2000-08-21 22:24:43 +0000843VALIDATER(global_stmt); VALIDATER(list_if);
844VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000845VALIDATER(exec_stmt); VALIDATER(compound_stmt);
846VALIDATER(while); VALIDATER(for);
847VALIDATER(try); VALIDATER(except_clause);
848VALIDATER(test); VALIDATER(and_test);
849VALIDATER(not_test); VALIDATER(comparison);
850VALIDATER(comp_op); VALIDATER(expr);
851VALIDATER(xor_expr); VALIDATER(and_expr);
852VALIDATER(shift_expr); VALIDATER(arith_expr);
853VALIDATER(term); VALIDATER(factor);
854VALIDATER(atom); VALIDATER(lambdef);
855VALIDATER(trailer); VALIDATER(subscript);
856VALIDATER(subscriptlist); VALIDATER(sliceop);
857VALIDATER(exprlist); VALIDATER(dictmaker);
858VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000859VALIDATER(listmaker); VALIDATER(yield_stmt);
Raymond Hettinger354433a2004-05-19 08:20:33 +0000860VALIDATER(testlist1); VALIDATER(gen_for);
861VALIDATER(gen_iter); VALIDATER(gen_if);
862VALIDATER(testlist_gexp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000863
Fred Drake0ac9b072000-09-12 21:58:06 +0000864#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000865
Fred Drakeff9ea482000-04-19 13:54:15 +0000866#define is_even(n) (((n) & 1) == 0)
867#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000868
869
870static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000871validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000872{
Fred Drake0ac9b072000-09-12 21:58:06 +0000873 if (TYPE(n) != t) {
874 PyErr_Format(parser_error, "Expected node type %d, got %d.",
875 t, TYPE(n));
876 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000877 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000878 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000879}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000880
881
Fred Drakee7ab64e2000-04-25 04:14:46 +0000882/* Verifies that the number of child nodes is exactly 'num', raising
883 * an exception if it isn't. The exception message does not indicate
884 * the exact number of nodes, allowing this to be used to raise the
885 * "right" exception when the wrong number of nodes is present in a
886 * specific variant of a statement's syntax. This is commonly used
887 * in that fashion.
888 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000889static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000890validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000891{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000892 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000893 PyErr_Format(parser_error,
894 "Illegal number of children for %s node.", name);
895 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000896 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000897 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000898}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000899
900
901static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000902validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000903{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000904 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000905 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000906
907 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000908 PyErr_Format(parser_error,
909 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000910 }
911 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000912}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000913
914
Guido van Rossum47478871996-08-21 14:32:37 +0000915/* X (',' X) [',']
916 */
917static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000918validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000919 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000920{
921 int nch = NCH(tree);
922 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000923 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000924
925 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000926 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000927 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000928 if (is_even(nch))
929 res = validate_comma(CHILD(tree, --nch));
930 if (res && nch > 1) {
931 int pos = 1;
932 for ( ; res && pos < nch; pos += 2)
933 res = (validate_comma(CHILD(tree, pos))
934 && vfunc(CHILD(tree, pos + 1)));
935 }
Guido van Rossum47478871996-08-21 14:32:37 +0000936 }
937 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000938}
Guido van Rossum47478871996-08-21 14:32:37 +0000939
940
Fred Drakecff283c2000-08-21 22:24:43 +0000941/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000942 *
943 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000944 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000945 */
Guido van Rossum47478871996-08-21 14:32:37 +0000946static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000947validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000948{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000949 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000950 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000951
Guido van Rossum3d602e31996-07-21 02:33:56 +0000952 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000953 res = (validate_name(CHILD(tree, 0), "class")
954 && validate_ntype(CHILD(tree, 1), NAME)
955 && validate_colon(CHILD(tree, nch - 2))
956 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000957 }
958 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000959 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000960 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000961 res = (validate_lparen(CHILD(tree, 2))
962 && validate_testlist(CHILD(tree, 3))
963 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000964 }
965 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000966}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000967
968
Guido van Rossum3d602e31996-07-21 02:33:56 +0000969/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +0000970 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +0000971 */
Guido van Rossum47478871996-08-21 14:32:37 +0000972static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000973validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000974{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000975 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000976 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +0000977 && (nch >= 4)
978 && validate_name(CHILD(tree, 0), "if")
979 && validate_test(CHILD(tree, 1))
980 && validate_colon(CHILD(tree, 2))
981 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000982
983 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000984 /* ... 'else' ':' suite */
985 res = (validate_name(CHILD(tree, nch - 3), "else")
986 && validate_colon(CHILD(tree, nch - 2))
987 && validate_suite(CHILD(tree, nch - 1)));
988 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000989 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000990 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000991 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000992 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +0000993 /* Will catch the case for nch < 4 */
994 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000995 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000996 /* ... ('elif' test ':' suite)+ ... */
997 int j = 4;
998 while ((j < nch) && res) {
999 res = (validate_name(CHILD(tree, j), "elif")
1000 && validate_colon(CHILD(tree, j + 2))
1001 && validate_test(CHILD(tree, j + 1))
1002 && validate_suite(CHILD(tree, j + 3)));
1003 j += 4;
1004 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001005 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001006 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001007}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001008
1009
Guido van Rossum3d602e31996-07-21 02:33:56 +00001010/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001011 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001012 *
1013 */
Guido van Rossum47478871996-08-21 14:32:37 +00001014static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001015validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001016{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001017 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001018 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001019
Guido van Rossum3d602e31996-07-21 02:33:56 +00001020 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001021 res = (validate_lparen(CHILD(tree, 0))
1022 && validate_rparen(CHILD(tree, nch - 1)));
1023 if (res && (nch == 3))
1024 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001025 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001026 else {
1027 (void) validate_numnodes(tree, 2, "parameters");
1028 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001029 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001030}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001031
1032
Fred Drakecff283c2000-08-21 22:24:43 +00001033/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001034 *
1035 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001036 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001037 * | NEWLINE INDENT stmt+ DEDENT
1038 */
Guido van Rossum47478871996-08-21 14:32:37 +00001039static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001040validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001041{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001042 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001043 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001044
Guido van Rossum3d602e31996-07-21 02:33:56 +00001045 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001046 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001047 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001048 /* NEWLINE INDENT stmt+ DEDENT */
1049 res = (validate_newline(CHILD(tree, 0))
1050 && validate_indent(CHILD(tree, 1))
1051 && validate_stmt(CHILD(tree, 2))
1052 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001053
Fred Drakeff9ea482000-04-19 13:54:15 +00001054 if (res && (nch > 4)) {
1055 int i = 3;
1056 --nch; /* forget the DEDENT */
1057 for ( ; res && (i < nch); ++i)
1058 res = validate_stmt(CHILD(tree, i));
1059 }
1060 else if (nch < 4)
1061 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001062 }
1063 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001064}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001065
1066
Guido van Rossum47478871996-08-21 14:32:37 +00001067static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001068validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001069{
Guido van Rossum47478871996-08-21 14:32:37 +00001070 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001071 validate_test, "testlist"));
1072}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001073
1074
Guido van Rossum1c917072001-10-15 15:44:05 +00001075static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001076validate_testlist1(node *tree)
1077{
1078 return (validate_repeating_list(tree, testlist1,
1079 validate_test, "testlist1"));
1080}
1081
1082
1083static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001084validate_testlist_safe(node *tree)
1085{
1086 return (validate_repeating_list(tree, testlist_safe,
1087 validate_test, "testlist_safe"));
1088}
1089
1090
Fred Drakecff283c2000-08-21 22:24:43 +00001091/* '*' NAME [',' '**' NAME] | '**' NAME
1092 */
1093static int
1094validate_varargslist_trailer(node *tree, int start)
1095{
1096 int nch = NCH(tree);
1097 int res = 0;
1098 int sym;
1099
1100 if (nch <= start) {
1101 err_string("expected variable argument trailer for varargslist");
1102 return 0;
1103 }
1104 sym = TYPE(CHILD(tree, start));
1105 if (sym == STAR) {
1106 /*
1107 * ('*' NAME [',' '**' NAME]
1108 */
1109 if (nch-start == 2)
1110 res = validate_name(CHILD(tree, start+1), NULL);
1111 else if (nch-start == 5)
1112 res = (validate_name(CHILD(tree, start+1), NULL)
1113 && validate_comma(CHILD(tree, start+2))
1114 && validate_doublestar(CHILD(tree, start+3))
1115 && validate_name(CHILD(tree, start+4), NULL));
1116 }
1117 else if (sym == DOUBLESTAR) {
1118 /*
1119 * '**' NAME
1120 */
1121 if (nch-start == 2)
1122 res = validate_name(CHILD(tree, start+1), NULL);
1123 }
1124 if (!res)
1125 err_string("illegal variable argument trailer for varargslist");
1126 return res;
1127}
1128
1129
1130/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001131 *
1132 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001133 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001134 * ('*' NAME [',' '**' NAME]
1135 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001136 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1137 *
1138 */
Guido van Rossum47478871996-08-21 14:32:37 +00001139static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001140validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001141{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001142 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001143 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001144 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001145
Fred Drakeb6429a22000-12-11 22:08:27 +00001146 if (!res)
1147 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001148 if (nch < 1) {
1149 err_string("varargslist missing child nodes");
1150 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001151 }
Fred Drakecff283c2000-08-21 22:24:43 +00001152 sym = TYPE(CHILD(tree, 0));
1153 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001154 /* whole thing matches:
1155 * '*' NAME [',' '**' NAME] | '**' NAME
1156 */
Fred Drakecff283c2000-08-21 22:24:43 +00001157 res = validate_varargslist_trailer(tree, 0);
1158 else if (sym == fpdef) {
1159 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001160
Fred Drakecff283c2000-08-21 22:24:43 +00001161 sym = TYPE(CHILD(tree, nch-1));
1162 if (sym == NAME) {
1163 /*
1164 * (fpdef ['=' test] ',')+
1165 * ('*' NAME [',' '**' NAME]
1166 * | '**' NAME)
1167 */
1168 /* skip over (fpdef ['=' test] ',')+ */
1169 while (res && (i+2 <= nch)) {
1170 res = validate_fpdef(CHILD(tree, i));
1171 ++i;
1172 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1173 res = (validate_equal(CHILD(tree, i))
1174 && validate_test(CHILD(tree, i+1)));
1175 if (res)
1176 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001177 }
Fred Drakecff283c2000-08-21 22:24:43 +00001178 if (res && i < nch) {
1179 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001180 ++i;
1181 if (res && i < nch
1182 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1183 || TYPE(CHILD(tree, i)) == STAR))
1184 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001185 }
1186 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001187 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1188 * i --^^^
1189 */
Fred Drakecff283c2000-08-21 22:24:43 +00001190 if (res)
1191 res = validate_varargslist_trailer(tree, i);
1192 }
1193 else {
1194 /*
1195 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1196 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001197 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001198 if (sym == COMMA) {
1199 res = validate_comma(CHILD(tree, nch-1));
1200 if (!res)
1201 return 0;
1202 --nch;
1203 }
1204 /*
1205 * fpdef ['=' test] (',' fpdef ['=' test])*
1206 */
1207 res = validate_fpdef(CHILD(tree, 0));
1208 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001209 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1210 res = (validate_equal(CHILD(tree, i))
1211 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001212 i += 2;
1213 }
1214 /*
1215 * ... (',' fpdef ['=' test])*
1216 * i ---^^^
1217 */
1218 while (res && (nch - i) >= 2) {
1219 res = (validate_comma(CHILD(tree, i))
1220 && validate_fpdef(CHILD(tree, i+1)));
1221 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001222 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1223 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001224 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001225 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001226 }
1227 }
1228 if (res && nch - i != 0) {
1229 res = 0;
1230 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001231 }
1232 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001233 }
Fred Drakecff283c2000-08-21 22:24:43 +00001234 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001235}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001236
1237
Fred Drakecff283c2000-08-21 22:24:43 +00001238/* list_iter: list_for | list_if
1239 */
1240static int
1241validate_list_iter(node *tree)
1242{
1243 int res = (validate_ntype(tree, list_iter)
1244 && validate_numnodes(tree, 1, "list_iter"));
1245 if (res && TYPE(CHILD(tree, 0)) == list_for)
1246 res = validate_list_for(CHILD(tree, 0));
1247 else
1248 res = validate_list_if(CHILD(tree, 0));
1249
1250 return res;
1251}
1252
Raymond Hettinger354433a2004-05-19 08:20:33 +00001253/* gen_iter: gen_for | gen_if
1254 */
1255static int
1256validate_gen_iter(node *tree)
1257{
1258 int res = (validate_ntype(tree, gen_iter)
1259 && validate_numnodes(tree, 1, "gen_iter"));
1260 if (res && TYPE(CHILD(tree, 0)) == gen_for)
1261 res = validate_gen_for(CHILD(tree, 0));
1262 else
1263 res = validate_gen_if(CHILD(tree, 0));
1264
1265 return res;
1266}
1267
Fred Drakecff283c2000-08-21 22:24:43 +00001268/* list_for: 'for' exprlist 'in' testlist [list_iter]
1269 */
1270static int
1271validate_list_for(node *tree)
1272{
1273 int nch = NCH(tree);
1274 int res;
1275
1276 if (nch == 5)
1277 res = validate_list_iter(CHILD(tree, 4));
1278 else
1279 res = validate_numnodes(tree, 4, "list_for");
1280
1281 if (res)
1282 res = (validate_name(CHILD(tree, 0), "for")
1283 && validate_exprlist(CHILD(tree, 1))
1284 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001285 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001286
1287 return res;
1288}
1289
Raymond Hettinger354433a2004-05-19 08:20:33 +00001290/* gen_for: 'for' exprlist 'in' test [gen_iter]
1291 */
1292static int
1293validate_gen_for(node *tree)
1294{
1295 int nch = NCH(tree);
1296 int res;
1297
1298 if (nch == 5)
1299 res = validate_gen_iter(CHILD(tree, 4));
1300 else
1301 res = validate_numnodes(tree, 4, "gen_for");
1302
1303 if (res)
1304 res = (validate_name(CHILD(tree, 0), "for")
1305 && validate_exprlist(CHILD(tree, 1))
1306 && validate_name(CHILD(tree, 2), "in")
1307 && validate_test(CHILD(tree, 3)));
1308
1309 return res;
1310}
1311
Fred Drakecff283c2000-08-21 22:24:43 +00001312/* list_if: 'if' test [list_iter]
1313 */
1314static int
1315validate_list_if(node *tree)
1316{
1317 int nch = NCH(tree);
1318 int res;
1319
1320 if (nch == 3)
1321 res = validate_list_iter(CHILD(tree, 2));
1322 else
1323 res = validate_numnodes(tree, 2, "list_if");
1324
1325 if (res)
1326 res = (validate_name(CHILD(tree, 0), "if")
1327 && validate_test(CHILD(tree, 1)));
1328
1329 return res;
1330}
1331
Raymond Hettinger354433a2004-05-19 08:20:33 +00001332/* gen_if: 'if' test [gen_iter]
1333 */
1334static int
1335validate_gen_if(node *tree)
1336{
1337 int nch = NCH(tree);
1338 int res;
1339
1340 if (nch == 3)
1341 res = validate_gen_iter(CHILD(tree, 2));
1342 else
1343 res = validate_numnodes(tree, 2, "gen_if");
1344
1345 if (res)
1346 res = (validate_name(CHILD(tree, 0), "if")
1347 && validate_test(CHILD(tree, 1)));
1348
1349 return res;
1350}
Fred Drakecff283c2000-08-21 22:24:43 +00001351
1352/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001353 *
1354 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001355 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001356 * | '(' fplist ')'
1357 */
Guido van Rossum47478871996-08-21 14:32:37 +00001358static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001359validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001360{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001361 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001362 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001363
Guido van Rossum3d602e31996-07-21 02:33:56 +00001364 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001365 if (nch == 1)
1366 res = validate_ntype(CHILD(tree, 0), NAME);
1367 else if (nch == 3)
1368 res = (validate_lparen(CHILD(tree, 0))
1369 && validate_fplist(CHILD(tree, 1))
1370 && validate_rparen(CHILD(tree, 2)));
1371 else
1372 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001373 }
1374 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001375}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001376
1377
Guido van Rossum47478871996-08-21 14:32:37 +00001378static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001379validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001380{
Guido van Rossum47478871996-08-21 14:32:37 +00001381 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001382 validate_fpdef, "fplist"));
1383}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001384
1385
Guido van Rossum3d602e31996-07-21 02:33:56 +00001386/* simple_stmt | compound_stmt
1387 *
1388 */
Guido van Rossum47478871996-08-21 14:32:37 +00001389static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001390validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001391{
1392 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001393 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001394
Guido van Rossum3d602e31996-07-21 02:33:56 +00001395 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001396 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001397
Fred Drakeff9ea482000-04-19 13:54:15 +00001398 if (TYPE(tree) == simple_stmt)
1399 res = validate_simple_stmt(tree);
1400 else
1401 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001402 }
1403 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001404}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001405
1406
Guido van Rossum3d602e31996-07-21 02:33:56 +00001407/* small_stmt (';' small_stmt)* [';'] NEWLINE
1408 *
1409 */
Guido van Rossum47478871996-08-21 14:32:37 +00001410static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001411validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001412{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001413 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001414 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001415 && (nch >= 2)
1416 && validate_small_stmt(CHILD(tree, 0))
1417 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001418
Guido van Rossum3d602e31996-07-21 02:33:56 +00001419 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001420 res = validate_numnodes(tree, 2, "simple_stmt");
1421 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001422 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001423 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001424 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001425 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001426
Fred Drakeff9ea482000-04-19 13:54:15 +00001427 for (i = 1; res && (i < nch); i += 2)
1428 res = (validate_semi(CHILD(tree, i))
1429 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001430 }
1431 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001432}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001433
1434
Guido van Rossum47478871996-08-21 14:32:37 +00001435static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001436validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001437{
1438 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001439 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001440
Fred Drake0ac9b072000-09-12 21:58:06 +00001441 if (res) {
1442 int ntype = TYPE(CHILD(tree, 0));
1443
1444 if ( (ntype == expr_stmt)
1445 || (ntype == print_stmt)
1446 || (ntype == del_stmt)
1447 || (ntype == pass_stmt)
1448 || (ntype == flow_stmt)
1449 || (ntype == import_stmt)
1450 || (ntype == global_stmt)
1451 || (ntype == assert_stmt)
1452 || (ntype == exec_stmt))
1453 res = validate_node(CHILD(tree, 0));
1454 else {
1455 res = 0;
1456 err_string("illegal small_stmt child type");
1457 }
1458 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001459 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001460 res = 0;
1461 PyErr_Format(parser_error,
1462 "Unrecognized child node of small_stmt: %d.",
1463 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001464 }
1465 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001466}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001467
1468
1469/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001470 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001471 */
Guido van Rossum47478871996-08-21 14:32:37 +00001472static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001473validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001474{
1475 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001476 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001477 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001478
1479 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001480 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001481
1482 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001483 ntype = TYPE(tree);
1484 if ( (ntype == if_stmt)
1485 || (ntype == while_stmt)
1486 || (ntype == for_stmt)
1487 || (ntype == try_stmt)
1488 || (ntype == funcdef)
1489 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001490 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001491 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001492 res = 0;
1493 PyErr_Format(parser_error,
1494 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001495 }
1496 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001497}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001498
1499
Guido van Rossum47478871996-08-21 14:32:37 +00001500static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001501validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001502{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001503 int j;
1504 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001505 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001506 && is_odd(nch)
1507 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001508
Fred Drake28f739a2000-08-25 22:42:40 +00001509 if (res && nch == 3
1510 && TYPE(CHILD(tree, 1)) == augassign) {
1511 res = (validate_numnodes(CHILD(tree, 1), 1, "augassign")
1512 && validate_testlist(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001513
Fred Drake28f739a2000-08-25 22:42:40 +00001514 if (res) {
1515 char *s = STR(CHILD(CHILD(tree, 1), 0));
1516
1517 res = (strcmp(s, "+=") == 0
1518 || strcmp(s, "-=") == 0
1519 || strcmp(s, "*=") == 0
1520 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001521 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001522 || strcmp(s, "%=") == 0
1523 || strcmp(s, "&=") == 0
1524 || strcmp(s, "|=") == 0
1525 || strcmp(s, "^=") == 0
1526 || strcmp(s, "<<=") == 0
1527 || strcmp(s, ">>=") == 0
1528 || strcmp(s, "**=") == 0);
1529 if (!res)
1530 err_string("illegal augmmented assignment operator");
1531 }
1532 }
1533 else {
1534 for (j = 1; res && (j < nch); j += 2)
1535 res = (validate_equal(CHILD(tree, j))
1536 && validate_testlist(CHILD(tree, j + 1)));
1537 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001538 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001539}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001540
1541
Guido van Rossum3d602e31996-07-21 02:33:56 +00001542/* print_stmt:
1543 *
Fred Drakecff283c2000-08-21 22:24:43 +00001544 * 'print' ( [ test (',' test)* [','] ]
1545 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001546 */
Guido van Rossum47478871996-08-21 14:32:37 +00001547static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001548validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001549{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001550 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001551 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001552 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001553 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001554
Fred Drakecff283c2000-08-21 22:24:43 +00001555 if (res && nch > 1) {
1556 int sym = TYPE(CHILD(tree, 1));
1557 int i = 1;
1558 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001559
Fred Drakecff283c2000-08-21 22:24:43 +00001560 if (sym == test)
1561 res = validate_test(CHILD(tree, i++));
1562 else {
1563 if (nch < 3)
1564 res = validate_numnodes(tree, 3, "print_stmt");
1565 else {
1566 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1567 && validate_test(CHILD(tree, i+1)));
1568 i += 2;
1569 allow_trailing_comma = 0;
1570 }
1571 }
1572 if (res) {
1573 /* ... (',' test)* [','] */
1574 while (res && i+2 <= nch) {
1575 res = (validate_comma(CHILD(tree, i))
1576 && validate_test(CHILD(tree, i+1)));
1577 allow_trailing_comma = 1;
1578 i += 2;
1579 }
1580 if (res && !allow_trailing_comma)
1581 res = validate_numnodes(tree, i, "print_stmt");
1582 else if (res && i < nch)
1583 res = validate_comma(CHILD(tree, i));
1584 }
1585 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001586 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001587}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001588
1589
Guido van Rossum47478871996-08-21 14:32:37 +00001590static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001591validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001592{
1593 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001594 && validate_name(CHILD(tree, 0), "del")
1595 && validate_exprlist(CHILD(tree, 1)));
1596}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001597
1598
Guido van Rossum47478871996-08-21 14:32:37 +00001599static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001600validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001601{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001602 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001603 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001604 && ((nch == 1) || (nch == 2))
1605 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001606
Guido van Rossum3d602e31996-07-21 02:33:56 +00001607 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001608 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001609
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001610 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001611}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001612
1613
Guido van Rossum47478871996-08-21 14:32:37 +00001614static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001615validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001616{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001617 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001618 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001619 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001620
Guido van Rossum3d602e31996-07-21 02:33:56 +00001621 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001622 res = validate_name(CHILD(tree, 0), "raise");
1623 if (res && (nch >= 2))
1624 res = validate_test(CHILD(tree, 1));
1625 if (res && nch > 2) {
1626 res = (validate_comma(CHILD(tree, 2))
1627 && validate_test(CHILD(tree, 3)));
1628 if (res && (nch > 4))
1629 res = (validate_comma(CHILD(tree, 4))
1630 && validate_test(CHILD(tree, 5)));
1631 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001632 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001633 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001634 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001635 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001636 res = (validate_comma(CHILD(tree, 2))
1637 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001638
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001639 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001640}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001641
1642
Fred Drake02126f22001-07-17 02:59:15 +00001643/* yield_stmt: 'yield' testlist
1644 */
1645static int
1646validate_yield_stmt(node *tree)
1647{
1648 return (validate_ntype(tree, yield_stmt)
1649 && validate_numnodes(tree, 2, "yield_stmt")
1650 && validate_name(CHILD(tree, 0), "yield")
1651 && validate_testlist(CHILD(tree, 1)));
1652}
1653
1654
Fred Drakecff283c2000-08-21 22:24:43 +00001655static int
1656validate_import_as_name(node *tree)
1657{
1658 int nch = NCH(tree);
1659 int ok = validate_ntype(tree, import_as_name);
1660
1661 if (ok) {
1662 if (nch == 1)
1663 ok = validate_name(CHILD(tree, 0), NULL);
1664 else if (nch == 3)
1665 ok = (validate_name(CHILD(tree, 0), NULL)
1666 && validate_name(CHILD(tree, 1), "as")
1667 && validate_name(CHILD(tree, 2), NULL));
1668 else
1669 ok = validate_numnodes(tree, 3, "import_as_name");
1670 }
1671 return ok;
1672}
1673
1674
Fred Drake71137082001-01-07 05:59:59 +00001675/* dotted_name: NAME ("." NAME)*
1676 */
1677static int
1678validate_dotted_name(node *tree)
1679{
1680 int nch = NCH(tree);
1681 int res = (validate_ntype(tree, dotted_name)
1682 && is_odd(nch)
1683 && validate_name(CHILD(tree, 0), NULL));
1684 int i;
1685
1686 for (i = 1; res && (i < nch); i += 2) {
1687 res = (validate_dot(CHILD(tree, i))
1688 && validate_name(CHILD(tree, i+1), NULL));
1689 }
1690 return res;
1691}
1692
1693
Fred Drakecff283c2000-08-21 22:24:43 +00001694/* dotted_as_name: dotted_name [NAME NAME]
1695 */
1696static int
1697validate_dotted_as_name(node *tree)
1698{
1699 int nch = NCH(tree);
1700 int res = validate_ntype(tree, dotted_as_name);
1701
1702 if (res) {
1703 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001704 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001705 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001706 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001707 && validate_name(CHILD(tree, 1), "as")
1708 && validate_name(CHILD(tree, 2), NULL));
1709 else {
1710 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001711 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001712 }
1713 }
1714 return res;
1715}
1716
1717
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001718/* dotted_as_name (',' dotted_as_name)* */
1719static int
1720validate_dotted_as_names(node *tree)
1721{
1722 int nch = NCH(tree);
1723 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1724 int i;
1725
1726 for (i = 1; res && (i < nch); i += 2)
1727 res = (validate_comma(CHILD(tree, i))
1728 && validate_dotted_as_name(CHILD(tree, i + 1)));
1729 return (res);
1730}
1731
1732
1733/* import_as_name (',' import_as_name)* [','] */
1734static int
1735validate_import_as_names(node *tree)
1736{
1737 int nch = NCH(tree);
1738 int res = validate_import_as_name(CHILD(tree, 0));
1739 int i;
1740
1741 for (i = 1; res && (i + 1 < nch); i += 2)
1742 res = (validate_comma(CHILD(tree, i))
1743 && validate_import_as_name(CHILD(tree, i + 1)));
1744 return (res);
1745}
1746
1747
1748/* 'import' dotted_as_names */
1749static int
1750validate_import_name(node *tree)
1751{
1752 return (validate_ntype(tree, import_name)
1753 && validate_numnodes(tree, 2, "import_name")
1754 && validate_name(CHILD(tree, 0), "import")
1755 && validate_dotted_as_names(CHILD(tree, 1)));
1756}
1757
1758
1759/* 'from' dotted_name 'import' ('*' | '(' import_as_names ')' |
1760 * import_as_names
Guido van Rossum3d602e31996-07-21 02:33:56 +00001761 */
Guido van Rossum47478871996-08-21 14:32:37 +00001762static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001763validate_import_from(node *tree)
1764{
1765 int nch = NCH(tree);
1766 int res = validate_ntype(tree, import_from)
1767 && (nch >= 4)
1768 && validate_name(CHILD(tree, 0), "from")
1769 && validate_dotted_name(CHILD(tree, 1))
1770 && validate_name(CHILD(tree, 2), "import");
1771
1772 if (res && TYPE(CHILD(tree, 3)) == LPAR)
1773 res = ((nch == 6)
1774 && validate_lparen(CHILD(tree, 3))
1775 && validate_import_as_names(CHILD(tree, 4))
1776 && validate_rparen(CHILD(tree, 5)));
1777 else if (res && TYPE(CHILD(tree, 3)) != STAR)
1778 res = validate_import_as_names(CHILD(tree, 3));
1779 return (res);
1780}
1781
1782
1783/* import_stmt: import_name | import_from */
1784static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001785validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001786{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001787 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001788 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001789
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001790 if (res) {
1791 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001792
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001793 if (ntype == import_name || ntype == import_from)
1794 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001795 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001796 res = 0;
1797 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001798 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001799 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001800 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001801 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001802 PyErr_Format(parser_error,
1803 "Unrecognized child node of import_stmt: %d.",
1804 TYPE(CHILD(tree, 0)));
1805 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001806 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001807}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001808
1809
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001810
1811
Guido van Rossum47478871996-08-21 14:32:37 +00001812static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001813validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001814{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001815 int j;
1816 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001817 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001818 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001819
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001820 if (!res && !PyErr_Occurred())
1821 err_string("illegal global statement");
1822
Guido van Rossum3d602e31996-07-21 02:33:56 +00001823 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001824 res = (validate_name(CHILD(tree, 0), "global")
1825 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001826 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001827 res = (validate_comma(CHILD(tree, j))
1828 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001829
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001830 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001831}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001832
1833
Guido van Rossum3d602e31996-07-21 02:33:56 +00001834/* exec_stmt:
1835 *
1836 * 'exec' expr ['in' test [',' test]]
1837 */
Guido van Rossum47478871996-08-21 14:32:37 +00001838static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001839validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001840{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001841 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001842 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001843 && ((nch == 2) || (nch == 4) || (nch == 6))
1844 && validate_name(CHILD(tree, 0), "exec")
1845 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001846
Guido van Rossum3d602e31996-07-21 02:33:56 +00001847 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001848 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001849 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001850 res = (validate_name(CHILD(tree, 2), "in")
1851 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001852 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001853 res = (validate_comma(CHILD(tree, 4))
1854 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001855
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001856 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001857}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001858
1859
Guido van Rossum925e5471997-04-02 05:32:13 +00001860/* assert_stmt:
1861 *
1862 * 'assert' test [',' test]
1863 */
1864static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001865validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001866{
1867 int nch = NCH(tree);
1868 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001869 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001870 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001871 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001872
1873 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001874 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001875 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001876 res = (validate_comma(CHILD(tree, 2))
1877 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001878
1879 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001880}
Guido van Rossum925e5471997-04-02 05:32:13 +00001881
1882
Guido van Rossum47478871996-08-21 14:32:37 +00001883static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001884validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001885{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001886 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001887 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001888 && ((nch == 4) || (nch == 7))
1889 && validate_name(CHILD(tree, 0), "while")
1890 && validate_test(CHILD(tree, 1))
1891 && validate_colon(CHILD(tree, 2))
1892 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001893
Guido van Rossum3d602e31996-07-21 02:33:56 +00001894 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001895 res = (validate_name(CHILD(tree, 4), "else")
1896 && validate_colon(CHILD(tree, 5))
1897 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001898
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001899 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001900}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001901
1902
Guido van Rossum47478871996-08-21 14:32:37 +00001903static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001904validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001905{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001906 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001907 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001908 && ((nch == 6) || (nch == 9))
1909 && validate_name(CHILD(tree, 0), "for")
1910 && validate_exprlist(CHILD(tree, 1))
1911 && validate_name(CHILD(tree, 2), "in")
1912 && validate_testlist(CHILD(tree, 3))
1913 && validate_colon(CHILD(tree, 4))
1914 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001915
Guido van Rossum3d602e31996-07-21 02:33:56 +00001916 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001917 res = (validate_name(CHILD(tree, 6), "else")
1918 && validate_colon(CHILD(tree, 7))
1919 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001920
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001921 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001922}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001923
1924
Guido van Rossum3d602e31996-07-21 02:33:56 +00001925/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001926 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001927 * | 'try' ':' suite 'finally' ':' suite
1928 *
1929 */
Guido van Rossum47478871996-08-21 14:32:37 +00001930static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001931validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001932{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001933 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001934 int pos = 3;
1935 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001936 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001937
1938 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001939 res = (validate_name(CHILD(tree, 0), "try")
1940 && validate_colon(CHILD(tree, 1))
1941 && validate_suite(CHILD(tree, 2))
1942 && validate_colon(CHILD(tree, nch - 2))
1943 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001944 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001945 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001946 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1947 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001948
1949 PyErr_Format(parser_error,
1950 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001951 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001952 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001953 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001954 res = (validate_except_clause(CHILD(tree, pos))
1955 && validate_colon(CHILD(tree, pos + 1))
1956 && validate_suite(CHILD(tree, pos + 2)));
1957 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001958 }
1959 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001960 res = validate_ntype(CHILD(tree, pos), NAME);
1961 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1962 res = (validate_numnodes(tree, 6, "try/finally")
1963 && validate_colon(CHILD(tree, 4))
1964 && validate_suite(CHILD(tree, 5)));
1965 else if (res) {
1966 if (nch == (pos + 3)) {
1967 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1968 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1969 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001970 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001971 }
1972 else if (nch == (pos + 6)) {
1973 res = (validate_name(CHILD(tree, pos), "except")
1974 && validate_colon(CHILD(tree, pos + 1))
1975 && validate_suite(CHILD(tree, pos + 2))
1976 && validate_name(CHILD(tree, pos + 3), "else"));
1977 }
1978 else
1979 res = validate_numnodes(tree, pos + 3, "try/except");
1980 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001981 }
1982 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001983}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001984
1985
Guido van Rossum47478871996-08-21 14:32:37 +00001986static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001987validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001988{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001989 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001990 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001991 && ((nch == 1) || (nch == 2) || (nch == 4))
1992 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001993
Guido van Rossum3d602e31996-07-21 02:33:56 +00001994 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001995 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001996 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001997 res = (validate_comma(CHILD(tree, 2))
1998 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001999
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002000 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002001}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002002
2003
Guido van Rossum47478871996-08-21 14:32:37 +00002004static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002005validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002006{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002007 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002008 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002009
Guido van Rossum3d602e31996-07-21 02:33:56 +00002010 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002011 res = ((nch == 1)
2012 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002013 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002014 int pos;
2015 res = validate_and_test(CHILD(tree, 0));
2016 for (pos = 1; res && (pos < nch); pos += 2)
2017 res = (validate_name(CHILD(tree, pos), "or")
2018 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002019 }
2020 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002021}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002022
2023
Guido van Rossum47478871996-08-21 14:32:37 +00002024static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002025validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002026{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002027 int pos;
2028 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002029 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002030 && is_odd(nch)
2031 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002032
Guido van Rossum3d602e31996-07-21 02:33:56 +00002033 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002034 res = (validate_name(CHILD(tree, pos), "and")
2035 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002036
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002037 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002038}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002039
2040
Guido van Rossum47478871996-08-21 14:32:37 +00002041static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002042validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002043{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002044 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002045 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002046
Guido van Rossum3d602e31996-07-21 02:33:56 +00002047 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002048 if (nch == 2)
2049 res = (validate_name(CHILD(tree, 0), "not")
2050 && validate_not_test(CHILD(tree, 1)));
2051 else if (nch == 1)
2052 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002053 }
2054 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002055}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002056
2057
Guido van Rossum47478871996-08-21 14:32:37 +00002058static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002059validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002060{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002061 int pos;
2062 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002063 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002064 && is_odd(nch)
2065 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002066
Guido van Rossum3d602e31996-07-21 02:33:56 +00002067 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002068 res = (validate_comp_op(CHILD(tree, pos))
2069 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002070
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002071 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002072}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002073
2074
Guido van Rossum47478871996-08-21 14:32:37 +00002075static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002076validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002077{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002078 int res = 0;
2079 int nch = NCH(tree);
2080
Guido van Rossum3d602e31996-07-21 02:33:56 +00002081 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002082 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002083 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002084 /*
2085 * Only child will be a terminal with a well-defined symbolic name
2086 * or a NAME with a string of either 'is' or 'in'
2087 */
2088 tree = CHILD(tree, 0);
2089 switch (TYPE(tree)) {
2090 case LESS:
2091 case GREATER:
2092 case EQEQUAL:
2093 case EQUAL:
2094 case LESSEQUAL:
2095 case GREATEREQUAL:
2096 case NOTEQUAL:
2097 res = 1;
2098 break;
2099 case NAME:
2100 res = ((strcmp(STR(tree), "in") == 0)
2101 || (strcmp(STR(tree), "is") == 0));
2102 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002103 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002104 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002105 }
2106 break;
2107 default:
Fred Drake661ea262000-10-24 19:57:45 +00002108 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002109 break;
2110 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002111 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002112 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002113 res = (validate_ntype(CHILD(tree, 0), NAME)
2114 && validate_ntype(CHILD(tree, 1), NAME)
2115 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2116 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2117 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2118 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2119 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002120 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002121 }
2122 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002123}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002124
2125
Guido van Rossum47478871996-08-21 14:32:37 +00002126static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002127validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002128{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002129 int j;
2130 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002131 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002132 && is_odd(nch)
2133 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002134
Guido van Rossum3d602e31996-07-21 02:33:56 +00002135 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002136 res = (validate_xor_expr(CHILD(tree, j))
2137 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002138
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002139 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002140}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002141
2142
Guido van Rossum47478871996-08-21 14:32:37 +00002143static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002144validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002145{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002146 int j;
2147 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002148 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002149 && is_odd(nch)
2150 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002151
Guido van Rossum3d602e31996-07-21 02:33:56 +00002152 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002153 res = (validate_circumflex(CHILD(tree, j - 1))
2154 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002155
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002156 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002157}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002158
2159
Guido van Rossum47478871996-08-21 14:32:37 +00002160static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002161validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002162{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002163 int pos;
2164 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002165 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002166 && is_odd(nch)
2167 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002168
Guido van Rossum3d602e31996-07-21 02:33:56 +00002169 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002170 res = (validate_ampersand(CHILD(tree, pos))
2171 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002172
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002173 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002174}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002175
2176
2177static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002178validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002179 {
2180 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002181 int nch = NCH(tree);
2182 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002183 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002184
Guido van Rossum3d602e31996-07-21 02:33:56 +00002185 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002186 if (TYPE(CHILD(tree, pos)) != op1)
2187 res = validate_ntype(CHILD(tree, pos), op2);
2188 if (res)
2189 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002190 }
2191 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002192}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002193
2194
Guido van Rossum47478871996-08-21 14:32:37 +00002195static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002196validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002197{
2198 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002199 && validate_chain_two_ops(tree, validate_arith_expr,
2200 LEFTSHIFT, RIGHTSHIFT));
2201}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002202
2203
Guido van Rossum47478871996-08-21 14:32:37 +00002204static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002205validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002206{
2207 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002208 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2209}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002210
2211
Guido van Rossum47478871996-08-21 14:32:37 +00002212static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002213validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002214{
2215 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002216 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002217 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002218 && is_odd(nch)
2219 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002220
Guido van Rossum3d602e31996-07-21 02:33:56 +00002221 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002222 res = (((TYPE(CHILD(tree, pos)) == STAR)
2223 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002224 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002225 || (TYPE(CHILD(tree, pos)) == PERCENT))
2226 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002227
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002228 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002229}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002230
2231
Guido van Rossum3d602e31996-07-21 02:33:56 +00002232/* factor:
2233 *
2234 * factor: ('+'|'-'|'~') factor | power
2235 */
Guido van Rossum47478871996-08-21 14:32:37 +00002236static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002237validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002238{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002239 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002240 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002241 && (((nch == 2)
2242 && ((TYPE(CHILD(tree, 0)) == PLUS)
2243 || (TYPE(CHILD(tree, 0)) == MINUS)
2244 || (TYPE(CHILD(tree, 0)) == TILDE))
2245 && validate_factor(CHILD(tree, 1)))
2246 || ((nch == 1)
2247 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002248 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002249}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002250
2251
Guido van Rossum3d602e31996-07-21 02:33:56 +00002252/* power:
2253 *
2254 * power: atom trailer* ('**' factor)*
2255 */
Guido van Rossum47478871996-08-21 14:32:37 +00002256static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002257validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002258{
2259 int pos = 1;
2260 int nch = NCH(tree);
2261 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002262 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002263
2264 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002265 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002266 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002267 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002268 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002269 return (0);
2270 }
2271 for ( ; res && (pos < (nch - 1)); pos += 2)
2272 res = (validate_doublestar(CHILD(tree, pos))
2273 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002274 }
2275 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002276}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002277
2278
Guido van Rossum47478871996-08-21 14:32:37 +00002279static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002280validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002281{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002282 int pos;
2283 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002284 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002285
Fred Drakecff283c2000-08-21 22:24:43 +00002286 if (res && nch < 1)
2287 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002288 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002289 switch (TYPE(CHILD(tree, 0))) {
2290 case LPAR:
2291 res = ((nch <= 3)
2292 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002293
Fred Drakeff9ea482000-04-19 13:54:15 +00002294 if (res && (nch == 3))
Raymond Hettinger354433a2004-05-19 08:20:33 +00002295 res = validate_testlist_gexp(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002296 break;
2297 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002298 if (nch == 2)
2299 res = validate_ntype(CHILD(tree, 1), RSQB);
2300 else if (nch == 3)
2301 res = (validate_listmaker(CHILD(tree, 1))
2302 && validate_ntype(CHILD(tree, 2), RSQB));
2303 else {
2304 res = 0;
2305 err_string("illegal list display atom");
2306 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002307 break;
2308 case LBRACE:
2309 res = ((nch <= 3)
2310 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002311
Fred Drakeff9ea482000-04-19 13:54:15 +00002312 if (res && (nch == 3))
2313 res = validate_dictmaker(CHILD(tree, 1));
2314 break;
2315 case BACKQUOTE:
2316 res = ((nch == 3)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002317 && validate_testlist1(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002318 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2319 break;
2320 case NAME:
2321 case NUMBER:
2322 res = (nch == 1);
2323 break;
2324 case STRING:
2325 for (pos = 1; res && (pos < nch); ++pos)
2326 res = validate_ntype(CHILD(tree, pos), STRING);
2327 break;
2328 default:
2329 res = 0;
2330 break;
2331 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002332 }
2333 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002334}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002335
2336
Fred Drake85bf3bb2000-08-23 15:35:26 +00002337/* listmaker:
2338 * test ( list_for | (',' test)* [','] )
2339 */
Fred Drakecff283c2000-08-21 22:24:43 +00002340static int
2341validate_listmaker(node *tree)
2342{
2343 int nch = NCH(tree);
2344 int ok = nch;
2345
2346 if (nch == 0)
2347 err_string("missing child nodes of listmaker");
2348 else
2349 ok = validate_test(CHILD(tree, 0));
2350
2351 /*
Raymond Hettinger354433a2004-05-19 08:20:33 +00002352 * list_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002353 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002354 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2355 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002356 else {
2357 /* (',' test)* [','] */
2358 int i = 1;
2359 while (ok && nch - i >= 2) {
2360 ok = (validate_comma(CHILD(tree, i))
2361 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002362 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002363 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002364 if (ok && i == nch-1)
2365 ok = validate_comma(CHILD(tree, i));
2366 else if (i != nch) {
2367 ok = 0;
2368 err_string("illegal trailing nodes for listmaker");
2369 }
Fred Drakecff283c2000-08-21 22:24:43 +00002370 }
2371 return ok;
2372}
2373
Raymond Hettinger354433a2004-05-19 08:20:33 +00002374/* testlist_gexp:
2375 * test ( gen_for | (',' test)* [','] )
2376 */
2377static int
2378validate_testlist_gexp(node *tree)
2379{
2380 int nch = NCH(tree);
2381 int ok = nch;
2382
2383 if (nch == 0)
2384 err_string("missing child nodes of testlist_gexp");
2385 else {
2386 ok = validate_test(CHILD(tree, 0));
2387 }
2388
2389 /*
2390 * gen_for | (',' test)* [',']
2391 */
2392 if (nch == 2 && TYPE(CHILD(tree, 1)) == gen_for)
2393 ok = validate_gen_for(CHILD(tree, 1));
2394 else {
2395 /* (',' test)* [','] */
2396 int i = 1;
2397 while (ok && nch - i >= 2) {
2398 ok = (validate_comma(CHILD(tree, i))
2399 && validate_test(CHILD(tree, i+1)));
2400 i += 2;
2401 }
2402 if (ok && i == nch-1)
2403 ok = validate_comma(CHILD(tree, i));
2404 else if (i != nch) {
2405 ok = 0;
2406 err_string("illegal trailing nodes for testlist_gexp");
2407 }
2408 }
2409 return ok;
2410}
Fred Drakecff283c2000-08-21 22:24:43 +00002411
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002412/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002413 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002414 */
2415static int
2416validate_decorator(node *tree)
2417{
2418 int ok;
2419 int nch = NCH(tree);
2420 ok = (validate_ntype(tree, decorator) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002421 (nch == 3 || nch == 5 || nch == 6) &&
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002422 validate_at(CHILD(tree, 0)) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002423 validate_dotted_name(CHILD(tree, 1)) &&
2424 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002425
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002426 if (ok && nch != 3) {
2427 ok = (validate_lparen(CHILD(tree, 2)) &&
2428 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002429
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002430 if (ok && nch == 6)
2431 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002432 }
2433
2434 return ok;
2435}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002436
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002437/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002438 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002439 */
2440static int
2441validate_decorators(node *tree)
2442{
2443 int i, nch, ok;
2444 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002445 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002446
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002447 for (i = 0; ok && i < nch; ++i)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002448 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002449
2450 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002451}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002452
Guido van Rossum3d602e31996-07-21 02:33:56 +00002453/* funcdef:
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002454 *
2455 * -6 -5 -4 -3 -2 -1
2456 * [decorators] 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002457 */
Guido van Rossum47478871996-08-21 14:32:37 +00002458static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002459validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002460{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002461 int nch = NCH(tree);
2462 int ok = (validate_ntype(tree, funcdef)
2463 && ((nch == 5) || (nch == 6))
2464 && validate_name(RCHILD(tree, -5), "def")
2465 && validate_ntype(RCHILD(tree, -4), NAME)
2466 && validate_colon(RCHILD(tree, -2))
2467 && validate_parameters(RCHILD(tree, -3))
2468 && validate_suite(RCHILD(tree, -1)));
2469
2470 if (ok && (nch == 6))
2471 ok = validate_decorators(CHILD(tree, 0));
2472
2473 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002474}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002475
2476
Guido van Rossum47478871996-08-21 14:32:37 +00002477static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002478validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002479{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002480 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002481 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002482 && ((nch == 3) || (nch == 4))
2483 && validate_name(CHILD(tree, 0), "lambda")
2484 && validate_colon(CHILD(tree, nch - 2))
2485 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002486
Guido van Rossum3d602e31996-07-21 02:33:56 +00002487 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002488 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002489 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002490 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002491
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002492 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002493}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002494
2495
Guido van Rossum3d602e31996-07-21 02:33:56 +00002496/* arglist:
2497 *
Fred Drakecff283c2000-08-21 22:24:43 +00002498 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002499 */
Guido van Rossum47478871996-08-21 14:32:37 +00002500static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002501validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002502{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002503 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002504 int i = 0;
2505 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002506
2507 if (nch <= 0)
2508 /* raise the right error from having an invalid number of children */
2509 return validate_numnodes(tree, nch + 1, "arglist");
2510
Raymond Hettinger354433a2004-05-19 08:20:33 +00002511 if (nch > 1) {
2512 for (i=0; i<nch; i++) {
2513 if (TYPE(CHILD(tree, i)) == argument) {
2514 node *ch = CHILD(tree, i);
2515 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == gen_for) {
2516 err_string("need '(', ')' for generator expression");
2517 return 0;
2518 }
2519 }
2520 }
2521 }
2522
Fred Drakecff283c2000-08-21 22:24:43 +00002523 while (ok && nch-i >= 2) {
2524 /* skip leading (argument ',') */
2525 ok = (validate_argument(CHILD(tree, i))
2526 && validate_comma(CHILD(tree, i+1)));
2527 if (ok)
2528 i += 2;
2529 else
2530 PyErr_Clear();
2531 }
2532 ok = 1;
2533 if (nch-i > 0) {
2534 /*
2535 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002536 */
Fred Drakecff283c2000-08-21 22:24:43 +00002537 int sym = TYPE(CHILD(tree, i));
2538
2539 if (sym == argument) {
2540 ok = validate_argument(CHILD(tree, i));
2541 if (ok && i+1 != nch) {
2542 err_string("illegal arglist specification"
2543 " (extra stuff on end)");
2544 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002545 }
Fred Drakecff283c2000-08-21 22:24:43 +00002546 }
2547 else if (sym == STAR) {
2548 ok = validate_star(CHILD(tree, i));
2549 if (ok && (nch-i == 2))
2550 ok = validate_test(CHILD(tree, i+1));
2551 else if (ok && (nch-i == 5))
2552 ok = (validate_test(CHILD(tree, i+1))
2553 && validate_comma(CHILD(tree, i+2))
2554 && validate_doublestar(CHILD(tree, i+3))
2555 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002556 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002557 err_string("illegal use of '*' in arglist");
2558 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002559 }
2560 }
Fred Drakecff283c2000-08-21 22:24:43 +00002561 else if (sym == DOUBLESTAR) {
2562 if (nch-i == 2)
2563 ok = (validate_doublestar(CHILD(tree, i))
2564 && validate_test(CHILD(tree, i+1)));
2565 else {
2566 err_string("illegal use of '**' in arglist");
2567 ok = 0;
2568 }
2569 }
2570 else {
2571 err_string("illegal arglist specification");
2572 ok = 0;
2573 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002574 }
2575 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002576}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002577
2578
2579
2580/* argument:
2581 *
Raymond Hettinger354433a2004-05-19 08:20:33 +00002582 * [test '='] test [gen_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002583 */
Guido van Rossum47478871996-08-21 14:32:37 +00002584static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002585validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002586{
2587 int nch = NCH(tree);
2588 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002589 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002590 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002591
Raymond Hettinger354433a2004-05-19 08:20:33 +00002592 if (res && (nch == 2))
2593 res = validate_gen_for(CHILD(tree, 1));
2594 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002595 res = (validate_equal(CHILD(tree, 1))
2596 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002597
2598 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002599}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002600
2601
2602
2603/* trailer:
2604 *
Guido van Rossum47478871996-08-21 14:32:37 +00002605 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002606 */
Guido van Rossum47478871996-08-21 14:32:37 +00002607static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002608validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002609{
2610 int nch = NCH(tree);
2611 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002612
2613 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002614 switch (TYPE(CHILD(tree, 0))) {
2615 case LPAR:
2616 res = validate_rparen(CHILD(tree, nch - 1));
2617 if (res && (nch == 3))
2618 res = validate_arglist(CHILD(tree, 1));
2619 break;
2620 case LSQB:
2621 res = (validate_numnodes(tree, 3, "trailer")
2622 && validate_subscriptlist(CHILD(tree, 1))
2623 && validate_ntype(CHILD(tree, 2), RSQB));
2624 break;
2625 case DOT:
2626 res = (validate_numnodes(tree, 2, "trailer")
2627 && validate_ntype(CHILD(tree, 1), NAME));
2628 break;
2629 default:
2630 res = 0;
2631 break;
2632 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002633 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002634 else {
2635 (void) validate_numnodes(tree, 2, "trailer");
2636 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002637 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002638}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002639
2640
Guido van Rossum47478871996-08-21 14:32:37 +00002641/* subscriptlist:
2642 *
2643 * subscript (',' subscript)* [',']
2644 */
2645static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002646validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002647{
2648 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002649 validate_subscript, "subscriptlist"));
2650}
Guido van Rossum47478871996-08-21 14:32:37 +00002651
2652
Guido van Rossum3d602e31996-07-21 02:33:56 +00002653/* subscript:
2654 *
Guido van Rossum47478871996-08-21 14:32:37 +00002655 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002656 */
Guido van Rossum47478871996-08-21 14:32:37 +00002657static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002658validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002659{
Guido van Rossum47478871996-08-21 14:32:37 +00002660 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002661 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002662 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002663
Guido van Rossum47478871996-08-21 14:32:37 +00002664 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002665 if (!PyErr_Occurred())
2666 err_string("invalid number of arguments for subscript node");
2667 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002668 }
Guido van Rossum47478871996-08-21 14:32:37 +00002669 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002670 /* take care of ('.' '.' '.') possibility */
2671 return (validate_numnodes(tree, 3, "subscript")
2672 && validate_dot(CHILD(tree, 0))
2673 && validate_dot(CHILD(tree, 1))
2674 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002675 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002676 if (TYPE(CHILD(tree, 0)) == test)
2677 res = validate_test(CHILD(tree, 0));
2678 else
2679 res = validate_colon(CHILD(tree, 0));
2680 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002681 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002682 /* Must be [test] ':' [test] [sliceop],
2683 * but at least one of the optional components will
2684 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002685 */
2686 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002687 res = validate_test(CHILD(tree, 0));
2688 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002689 }
2690 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002691 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002692 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002693 int rem = nch - ++offset;
2694 if (rem) {
2695 if (TYPE(CHILD(tree, offset)) == test) {
2696 res = validate_test(CHILD(tree, offset));
2697 ++offset;
2698 --rem;
2699 }
2700 if (res && rem)
2701 res = validate_sliceop(CHILD(tree, offset));
2702 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002703 }
2704 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002705}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002706
2707
Guido van Rossum47478871996-08-21 14:32:37 +00002708static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002709validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002710{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002711 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002712 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002713 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002714 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002715 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002716 }
Guido van Rossum47478871996-08-21 14:32:37 +00002717 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002718 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002719 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002720 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002721
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002722 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002723}
Guido van Rossum47478871996-08-21 14:32:37 +00002724
2725
2726static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002727validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002728{
2729 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002730 validate_expr, "exprlist"));
2731}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002732
2733
Guido van Rossum47478871996-08-21 14:32:37 +00002734static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002735validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002736{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002737 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002738 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002739 && (nch >= 3)
2740 && validate_test(CHILD(tree, 0))
2741 && validate_colon(CHILD(tree, 1))
2742 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002743
Guido van Rossum3d602e31996-07-21 02:33:56 +00002744 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002745 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002746 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002747 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002748
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002749 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002750 int pos = 3;
2751 /* ( ',' test ':' test )* */
2752 while (res && (pos < nch)) {
2753 res = (validate_comma(CHILD(tree, pos))
2754 && validate_test(CHILD(tree, pos + 1))
2755 && validate_colon(CHILD(tree, pos + 2))
2756 && validate_test(CHILD(tree, pos + 3)));
2757 pos += 4;
2758 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002759 }
2760 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002761}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002762
2763
Guido van Rossum47478871996-08-21 14:32:37 +00002764static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002765validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002766{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002767 int pos;
2768 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002769 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002770 && (nch >= 2)
2771 && validate_testlist(CHILD(tree, 0))
2772 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002773
Guido van Rossum3d602e31996-07-21 02:33:56 +00002774 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002775 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002776
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002777 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002778}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002779
2780
Guido van Rossum47478871996-08-21 14:32:37 +00002781static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002782validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002783{
Fred Drakeff9ea482000-04-19 13:54:15 +00002784 int nch = 0; /* num. children on current node */
2785 int res = 1; /* result value */
2786 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002787
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002788 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002789 nch = NCH(tree);
2790 next = 0;
2791 switch (TYPE(tree)) {
2792 /*
2793 * Definition nodes.
2794 */
2795 case funcdef:
2796 res = validate_funcdef(tree);
2797 break;
2798 case classdef:
2799 res = validate_class(tree);
2800 break;
2801 /*
2802 * "Trivial" parse tree nodes.
2803 * (Why did I call these trivial?)
2804 */
2805 case stmt:
2806 res = validate_stmt(tree);
2807 break;
2808 case small_stmt:
2809 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002810 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002811 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2812 */
2813 res = validate_small_stmt(tree);
2814 break;
2815 case flow_stmt:
2816 res = (validate_numnodes(tree, 1, "flow_stmt")
2817 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2818 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002819 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002820 || (TYPE(CHILD(tree, 0)) == return_stmt)
2821 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2822 if (res)
2823 next = CHILD(tree, 0);
2824 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002825 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002826 break;
Fred Drake02126f22001-07-17 02:59:15 +00002827 case yield_stmt:
2828 res = validate_yield_stmt(tree);
2829 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002830 /*
2831 * Compound statements.
2832 */
2833 case simple_stmt:
2834 res = validate_simple_stmt(tree);
2835 break;
2836 case compound_stmt:
2837 res = validate_compound_stmt(tree);
2838 break;
2839 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002840 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002841 */
2842 case expr_stmt:
2843 res = validate_expr_stmt(tree);
2844 break;
2845 case print_stmt:
2846 res = validate_print_stmt(tree);
2847 break;
2848 case del_stmt:
2849 res = validate_del_stmt(tree);
2850 break;
2851 case pass_stmt:
2852 res = (validate_numnodes(tree, 1, "pass")
2853 && validate_name(CHILD(tree, 0), "pass"));
2854 break;
2855 case break_stmt:
2856 res = (validate_numnodes(tree, 1, "break")
2857 && validate_name(CHILD(tree, 0), "break"));
2858 break;
2859 case continue_stmt:
2860 res = (validate_numnodes(tree, 1, "continue")
2861 && validate_name(CHILD(tree, 0), "continue"));
2862 break;
2863 case return_stmt:
2864 res = validate_return_stmt(tree);
2865 break;
2866 case raise_stmt:
2867 res = validate_raise_stmt(tree);
2868 break;
2869 case import_stmt:
2870 res = validate_import_stmt(tree);
2871 break;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00002872 case import_name:
2873 res = validate_import_name(tree);
2874 break;
2875 case import_from:
2876 res = validate_import_from(tree);
2877 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002878 case global_stmt:
2879 res = validate_global_stmt(tree);
2880 break;
2881 case exec_stmt:
2882 res = validate_exec_stmt(tree);
2883 break;
2884 case assert_stmt:
2885 res = validate_assert_stmt(tree);
2886 break;
2887 case if_stmt:
2888 res = validate_if(tree);
2889 break;
2890 case while_stmt:
2891 res = validate_while(tree);
2892 break;
2893 case for_stmt:
2894 res = validate_for(tree);
2895 break;
2896 case try_stmt:
2897 res = validate_try(tree);
2898 break;
2899 case suite:
2900 res = validate_suite(tree);
2901 break;
2902 /*
2903 * Expression nodes.
2904 */
2905 case testlist:
2906 res = validate_testlist(tree);
2907 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002908 case testlist1:
2909 res = validate_testlist1(tree);
2910 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002911 case test:
2912 res = validate_test(tree);
2913 break;
2914 case and_test:
2915 res = validate_and_test(tree);
2916 break;
2917 case not_test:
2918 res = validate_not_test(tree);
2919 break;
2920 case comparison:
2921 res = validate_comparison(tree);
2922 break;
2923 case exprlist:
2924 res = validate_exprlist(tree);
2925 break;
2926 case comp_op:
2927 res = validate_comp_op(tree);
2928 break;
2929 case expr:
2930 res = validate_expr(tree);
2931 break;
2932 case xor_expr:
2933 res = validate_xor_expr(tree);
2934 break;
2935 case and_expr:
2936 res = validate_and_expr(tree);
2937 break;
2938 case shift_expr:
2939 res = validate_shift_expr(tree);
2940 break;
2941 case arith_expr:
2942 res = validate_arith_expr(tree);
2943 break;
2944 case term:
2945 res = validate_term(tree);
2946 break;
2947 case factor:
2948 res = validate_factor(tree);
2949 break;
2950 case power:
2951 res = validate_power(tree);
2952 break;
2953 case atom:
2954 res = validate_atom(tree);
2955 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002956
Fred Drakeff9ea482000-04-19 13:54:15 +00002957 default:
2958 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002959 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002960 res = 0;
2961 break;
2962 }
2963 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002964 }
2965 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002966}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002967
2968
Guido van Rossum47478871996-08-21 14:32:37 +00002969static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002970validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002971{
2972 int res = validate_eval_input(tree);
2973
2974 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002975 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002976
2977 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002978}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002979
2980
Guido van Rossum3d602e31996-07-21 02:33:56 +00002981/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002982 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002983 */
Guido van Rossum47478871996-08-21 14:32:37 +00002984static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002985validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002986{
Fred Drakec2683dd2001-07-17 19:32:05 +00002987 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002988 int nch = NCH(tree) - 1;
2989 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002990 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002991
Fred Drakec2683dd2001-07-17 19:32:05 +00002992 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002993 if (TYPE(CHILD(tree, j)) == stmt)
2994 res = validate_stmt(CHILD(tree, j));
2995 else
2996 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002997 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002998 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002999 * user. Hopefully, this won't be needed. If a user reports getting
3000 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003001 */
3002 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003003 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003004
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003005 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003006}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003007
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003008static int
3009validate_encoding_decl(node *tree)
3010{
3011 int nch = NCH(tree);
3012 int res = ((nch == 1)
3013 && validate_file_input(CHILD(tree, 0)));
3014
3015 if (!res && !PyErr_Occurred())
3016 err_string("Error Parsing encoding_decl");
3017
3018 return res;
3019}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003020
Fred Drake43f8f9b1998-04-13 16:25:46 +00003021static PyObject*
3022pickle_constructor = NULL;
3023
3024
3025static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003026parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003027{
Fred Drake268397f1998-04-29 14:16:32 +00003028 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003029 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003030 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003031 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003032
Fred Drakec2683dd2001-07-17 19:32:05 +00003033 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003034 PyObject *newargs;
3035 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003036
Fred Drake2a6875e1999-09-20 22:32:18 +00003037 if ((empty_dict = PyDict_New()) == NULL)
3038 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003039 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003040 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003041 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003042 if (tuple != NULL) {
3043 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3044 Py_DECREF(tuple);
3045 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003046 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003047 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003048 }
3049 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003050 Py_XDECREF(empty_dict);
3051
Fred Drake43f8f9b1998-04-13 16:25:46 +00003052 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003053}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003054
3055
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003056/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003057 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003058 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003059 * We'd really have to write a wrapper around it all anyway to allow
3060 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003061 */
3062static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003063 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003064 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003065 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003066 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003067 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003068 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003069 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003070 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003071 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003072 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003073 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003074 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003075 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003076 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003077 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003078 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003079 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003080 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003081 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003082 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003083 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003084 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003085 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003086 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003087 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003088 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003089 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003090 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003091
Fred Drake43f8f9b1998-04-13 16:25:46 +00003092 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003093 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003094 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003095
Fred Drake268397f1998-04-29 14:16:32 +00003096 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003097 };
3098
3099
Mark Hammond62b1ab12002-07-23 06:31:15 +00003100PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003101
Mark Hammond62b1ab12002-07-23 06:31:15 +00003102PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00003103initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003104{
Fred Drake13130bc2001-08-15 16:44:56 +00003105 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003106
3107 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00003108 module = Py_InitModule("parser", parser_functions);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003109
Fred Drake7a15ba51999-09-09 14:21:52 +00003110 if (parser_error == 0)
3111 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003112
Tim Peters6a627252003-07-21 14:25:23 +00003113 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00003114 /* caller will check PyErr_Occurred() */
3115 return;
Tim Peters6a627252003-07-21 14:25:23 +00003116 /* CAUTION: The code next used to skip bumping the refcount on
3117 * parser_error. That's a disaster if initparser() gets called more
3118 * than once. By incref'ing, we ensure that each module dict that
3119 * gets created owns its reference to the shared parser_error object,
3120 * and the file static parser_error vrbl owns a reference too.
3121 */
3122 Py_INCREF(parser_error);
3123 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3124 return;
3125
Fred Drakec2683dd2001-07-17 19:32:05 +00003126 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003127 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00003128 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003129 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003130
Fred Drake13130bc2001-08-15 16:44:56 +00003131 PyModule_AddStringConstant(module, "__copyright__",
3132 parser_copyright_string);
3133 PyModule_AddStringConstant(module, "__doc__",
3134 parser_doc_string);
3135 PyModule_AddStringConstant(module, "__version__",
3136 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003137
Fred Drake78bdb9b2001-07-19 20:17:15 +00003138 /* Register to support pickling.
3139 * If this fails, the import of this module will fail because an
3140 * exception will be raised here; should we clear the exception?
3141 */
Fred Drake13130bc2001-08-15 16:44:56 +00003142 copyreg = PyImport_ImportModule("copy_reg");
3143 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003144 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003145
Fred Drake13130bc2001-08-15 16:44:56 +00003146 func = PyObject_GetAttrString(copyreg, "pickle");
3147 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3148 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003149 Py_XINCREF(pickle_constructor);
3150 if ((func != NULL) && (pickle_constructor != NULL)
3151 && (pickler != NULL)) {
3152 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003153
Fred Drakec2683dd2001-07-17 19:32:05 +00003154 res = PyObject_CallFunction(func, "OOO", &PyST_Type, pickler,
3155 pickle_constructor);
Fred Drakeff9ea482000-04-19 13:54:15 +00003156 Py_XDECREF(res);
3157 }
3158 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003159 Py_XDECREF(pickle_constructor);
3160 Py_XDECREF(pickler);
3161 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003162 }
Fred Drakeff9ea482000-04-19 13:54:15 +00003163}