blob: 854d986746f03b60343669581fb09b130b6b1f02 [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 Rossum19efc5f1998-04-28 16:10:19 +000042#ifdef macintosh
Fred Drakeff9ea482000-04-19 13:54:15 +000043char *strdup(char *);
Guido van Rossum19efc5f1998-04-28 16:10:19 +000044#endif
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000045
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000046/* String constants used to initialize module attributes.
47 *
48 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000049static char parser_copyright_string[] =
50"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000051University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
52Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
53Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000054
55
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000056PyDoc_STRVAR(parser_doc_string,
57"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000058
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000059static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000060
61
Fred Drakeff9ea482000-04-19 13:54:15 +000062typedef PyObject* (*SeqMaker) (int length);
63typedef int (*SeqInserter) (PyObject* sequence,
64 int index,
65 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000066
Thomas Wouters7e474022000-07-16 12:04:32 +000067/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000068 * original copyright statement is included below, and continues to apply
69 * in full to the function immediately following. All other material is
70 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
71 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000072 * new naming conventions. Added arguments to provide support for creating
73 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000074 */
75
Guido van Rossum52f2c051993-11-10 12:53:24 +000076
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000077static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000078node2tuple(node *n, /* node to convert */
79 SeqMaker mkseq, /* create sequence */
80 SeqInserter addelem, /* func. to add elem. in seq. */
81 int lineno) /* include line numbers? */
Guido van Rossum47478871996-08-21 14:32:37 +000082{
Guido van Rossum3d602e31996-07-21 02:33:56 +000083 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000084 Py_INCREF(Py_None);
85 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000086 }
87 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000088 int i;
89 PyObject *v;
90 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000091
Fred Drakeff9ea482000-04-19 13:54:15 +000092 v = mkseq(1 + NCH(n));
93 if (v == NULL)
94 return (v);
95 w = PyInt_FromLong(TYPE(n));
96 if (w == NULL) {
97 Py_DECREF(v);
98 return ((PyObject*) NULL);
99 }
100 (void) addelem(v, 0, w);
101 for (i = 0; i < NCH(n); i++) {
102 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno);
103 if (w == NULL) {
104 Py_DECREF(v);
105 return ((PyObject*) NULL);
106 }
107 (void) addelem(v, i+1, w);
108 }
109 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000110 }
111 else if (ISTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000112 PyObject *result = mkseq(2 + lineno);
113 if (result != NULL) {
114 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
115 (void) addelem(result, 1, PyString_FromString(STR(n)));
116 if (lineno == 1)
117 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
118 }
119 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000120 }
121 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000122 PyErr_SetString(PyExc_SystemError,
123 "unrecognized parse tree node type");
124 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000125 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000126}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000127/*
128 * End of material copyrighted by Stichting Mathematisch Centrum.
129 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000130
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000131
132
133/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000134 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000135 * field of the object type to identify which any given object represents.
136 * These should probably go in an external header to allow other extensions
137 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000138 */
139
Fred Drakec2683dd2001-07-17 19:32:05 +0000140#define PyST_EXPR 1
141#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000142
143
144/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000145 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000146 * naming style, but the code uses the new naming convention.
147 */
148
149static PyObject*
150parser_error = 0;
151
152
Fred Drakec2683dd2001-07-17 19:32:05 +0000153typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000154 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000155 node* st_node; /* the node* returned by the parser */
156 int st_type; /* EXPR or SUITE ? */
157} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000158
159
Fred Drake268397f1998-04-29 14:16:32 +0000160staticforward void
Fred Drakec2683dd2001-07-17 19:32:05 +0000161parser_free(PyST_Object *st);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000162
Fred Drake268397f1998-04-29 14:16:32 +0000163staticforward int
Fred Drakec2683dd2001-07-17 19:32:05 +0000164parser_compare(PyST_Object *left, PyST_Object *right);
Fred Drake268397f1998-04-29 14:16:32 +0000165
166staticforward PyObject *
Fred Drakeff9ea482000-04-19 13:54:15 +0000167parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000168
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000169
Fred Drake268397f1998-04-29 14:16:32 +0000170static
Fred Drakec2683dd2001-07-17 19:32:05 +0000171PyTypeObject PyST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000172 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000173 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000174 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000175 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000176 0, /* tp_itemsize */
177 (destructor)parser_free, /* tp_dealloc */
178 0, /* tp_print */
179 parser_getattr, /* tp_getattr */
180 0, /* tp_setattr */
181 (cmpfunc)parser_compare, /* tp_compare */
182 0, /* tp_repr */
183 0, /* tp_as_number */
184 0, /* tp_as_sequence */
185 0, /* tp_as_mapping */
186 0, /* tp_hash */
187 0, /* tp_call */
188 0, /* tp_str */
189 0, /* tp_getattro */
190 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000191
192 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000193 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000194
Fred Drakeff9ea482000-04-19 13:54:15 +0000195 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000196
197 /* __doc__ */
198 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000199}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000200
201
202static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000203parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000204{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000205 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000206
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000207 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000208 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000209
210 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000211 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000212
213 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000214 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000215
216 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000217 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000218
219 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000220 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000221
222 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000223 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000224
Fred Drakeff9ea482000-04-19 13:54:15 +0000225 if (v != 0)
226 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000227 }
228 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000229}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000230
231
Fred Drakec2683dd2001-07-17 19:32:05 +0000232/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000233 *
234 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
235 * This really just wraps a call to parser_compare_nodes() with some easy
236 * checks and protection code.
237 *
238 */
239static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000240parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000241{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000242 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000243 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000244
245 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000246 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247
Fred Drakec2683dd2001-07-17 19:32:05 +0000248 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000249}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000250
251
Fred Drakec2683dd2001-07-17 19:32:05 +0000252/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000253 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000254 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000255 * 'wrapper' object that holds a node* and allows it to be passed around in
256 * Python code.
257 *
258 */
259static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000260parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000261{
Fred Drakec2683dd2001-07-17 19:32:05 +0000262 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000263
264 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000265 o->st_node = st;
266 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000267 }
Fred Drake268397f1998-04-29 14:16:32 +0000268 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000269 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000270 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000271 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000272}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000273
274
Fred Drakec2683dd2001-07-17 19:32:05 +0000275/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000276 *
277 * This is called by a del statement that reduces the reference count to 0.
278 *
279 */
280static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000281parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000282{
Fred Drakec2683dd2001-07-17 19:32:05 +0000283 PyNode_Free(st->st_node);
284 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000285}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000286
287
Fred Drakec2683dd2001-07-17 19:32:05 +0000288/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000289 *
290 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000291 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000292 *
293 */
294static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000295parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000296{
Guido van Rossum47478871996-08-21 14:32:37 +0000297 PyObject *line_option = 0;
298 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000299 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000300
Fred Drake7a15ba51999-09-09 14:21:52 +0000301 static char *keywords[] = {"ast", "line_info", NULL};
302
Fred Drake268397f1998-04-29 14:16:32 +0000303 if (self == NULL) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000304 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2tuple", keywords,
305 &PyST_Type, &self, &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000306 }
Fred Drake503d8d61998-04-13 18:45:18 +0000307 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000308 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:totuple", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000309 &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000310 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000311 int lineno = 0;
312 if (line_option != NULL) {
313 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
314 }
315 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000316 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000317 * since it's known to work already.
318 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000319 res = node2tuple(((PyST_Object*)self)->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000320 PyTuple_New, PyTuple_SetItem, lineno);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000321 }
322 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000323}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000324
325
Fred Drakec2683dd2001-07-17 19:32:05 +0000326/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000327 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000328 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000329 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000330 *
331 */
332static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000333parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000334{
Guido van Rossum47478871996-08-21 14:32:37 +0000335 PyObject *line_option = 0;
336 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000337 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000338
Fred Drake7a15ba51999-09-09 14:21:52 +0000339 static char *keywords[] = {"ast", "line_info", NULL};
340
Fred Drake503d8d61998-04-13 18:45:18 +0000341 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000342 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2list", keywords,
343 &PyST_Type, &self, &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000344 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000345 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:tolist", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000346 &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000347 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000348 int lineno = 0;
349 if (line_option != 0) {
350 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
351 }
352 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000353 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000354 * since it's known to work already.
355 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000356 res = node2tuple(self->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000357 PyList_New, PyList_SetItem, lineno);
Guido van Rossum47478871996-08-21 14:32:37 +0000358 }
359 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000360}
Guido van Rossum47478871996-08-21 14:32:37 +0000361
362
Fred Drakec2683dd2001-07-17 19:32:05 +0000363/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000364 *
365 * This function creates code objects from the parse tree represented by
366 * the passed-in data object. An optional file name is passed in as well.
367 *
368 */
369static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000370parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000371{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000372 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000373 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000374 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000375
Fred Drake7a15ba51999-09-09 14:21:52 +0000376 static char *keywords[] = {"ast", "filename", NULL};
377
Fred Drake503d8d61998-04-13 18:45:18 +0000378 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000379 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
380 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000381 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000382 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000383 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000384
385 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000386 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000387
388 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000389}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000390
391
392/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
393 * PyObject* parser_issuite(PyObject* self, PyObject* args)
394 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000395 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000396 * a statement suite, respectively. The return is a Python truth value.
397 *
398 */
399static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000400parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000401{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000402 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000403 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000404
Fred Drake7a15ba51999-09-09 14:21:52 +0000405 static char *keywords[] = {"ast", NULL};
406
Fred Drake503d8d61998-04-13 18:45:18 +0000407 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000408 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000409 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000410 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000411 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000412
413 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000414 /* Check to see if the ST represents an expression or not. */
415 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000416 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000417 }
418 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000419}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000420
421
422static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000423parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000424{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000425 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000426 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000427
Fred Drake7a15ba51999-09-09 14:21:52 +0000428 static char *keywords[] = {"ast", NULL};
429
Fred Drake503d8d61998-04-13 18:45:18 +0000430 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000431 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000432 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000433 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000434 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000435
436 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000437 /* Check to see if the ST represents an expression or not. */
438 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000439 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000440 }
441 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000442}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000443
444
Fred Drake7a15ba51999-09-09 14:21:52 +0000445#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
446
Fred Drake503d8d61998-04-13 18:45:18 +0000447static PyMethodDef
448parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000449 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
450 "Compile this ST object into a code object."},
Fred Drakeff9ea482000-04-19 13:54:15 +0000451 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Fred Drakec2683dd2001-07-17 19:32:05 +0000452 "Determines if this ST object was created from an expression."},
Fred Drakeff9ea482000-04-19 13:54:15 +0000453 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Fred Drakec2683dd2001-07-17 19:32:05 +0000454 "Determines if this ST object was created from a suite."},
455 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
456 "Creates a list-tree representation of this ST."},
457 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
458 "Creates a tuple-tree representation of this ST."},
Fred Drake503d8d61998-04-13 18:45:18 +0000459
Fred Drake268397f1998-04-29 14:16:32 +0000460 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000461};
462
Fred Drake503d8d61998-04-13 18:45:18 +0000463
464static PyObject*
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000465parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000466{
Fred Drake503d8d61998-04-13 18:45:18 +0000467 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000468}
Fred Drake503d8d61998-04-13 18:45:18 +0000469
470
Guido van Rossum3d602e31996-07-21 02:33:56 +0000471/* err_string(char* message)
472 *
473 * Sets the error string for an exception of type ParserError.
474 *
475 */
476static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000477err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000478{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000479 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000480}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000481
482
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000483/* PyObject* parser_do_parse(PyObject* args, int type)
484 *
485 * Internal function to actually execute the parse and return the result if
486 * successful, or set an exception if not.
487 *
488 */
489static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000490parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000491{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000492 char* string = 0;
493 PyObject* res = 0;
494
Fred Drake7a15ba51999-09-09 14:21:52 +0000495 static char *keywords[] = {"source", NULL};
496
497 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000498 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000499 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000500 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000501
Fred Drakeff9ea482000-04-19 13:54:15 +0000502 if (n != 0)
Fred Drakec2683dd2001-07-17 19:32:05 +0000503 res = parser_newstobject(n, type);
Fred Drakeff9ea482000-04-19 13:54:15 +0000504 else
Fred Drake661ea262000-10-24 19:57:45 +0000505 err_string("could not parse string");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000506 }
507 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000508}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000509
510
511/* PyObject* parser_expr(PyObject* self, PyObject* args)
512 * PyObject* parser_suite(PyObject* self, PyObject* args)
513 *
514 * External interfaces to the parser itself. Which is called determines if
515 * the parser attempts to recognize an expression ('eval' form) or statement
516 * suite ('exec' form). The real work is done by parser_do_parse() above.
517 *
518 */
519static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000520parser_expr(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:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000524}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000525
526
527static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000528parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000529{
Fred Drake268397f1998-04-29 14:16:32 +0000530 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000531 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000532}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000533
534
535
Fred Drakec2683dd2001-07-17 19:32:05 +0000536/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000537 * object requires that the input tuple be valid without having to rely on
538 * catching an exception from the compiler. This is done to allow the
539 * compiler itself to remain fast, since most of its input will come from
540 * the parser directly, and therefore be known to be syntactically correct.
541 * This validation is done to ensure that we don't core dump the compile
542 * phase, returning an exception instead.
543 *
544 * Two aspects can be broken out in this code: creating a node tree from
545 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000546 * advantageous to expand the number of ST types to include funcdefs and
547 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000548 * here. They are not necessary, and not quite as useful in a raw form.
549 * For now, let's get expressions and suites working reliably.
550 */
551
552
Fred Drakeff9ea482000-04-19 13:54:15 +0000553staticforward node* build_node_tree(PyObject *tuple);
554staticforward int validate_expr_tree(node *tree);
555staticforward int validate_file_input(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000556
557
Fred Drakec2683dd2001-07-17 19:32:05 +0000558/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000559 *
560 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000561 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000562 * tuple can be validated. It does this by checking the first code of the
563 * tuple, and, if acceptable, builds the internal representation. If this
564 * step succeeds, the internal representation is validated as fully as
565 * possible with the various validate_*() routines defined below.
566 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000567 * This function must be changed if support is to be added for PyST_FRAGMENT
568 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000569 *
570 */
571static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000572parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000573{
Fred Drake268397f1998-04-29 14:16:32 +0000574 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000575 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000576 PyObject *tuple;
577 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000578
Fred Drake7a15ba51999-09-09 14:21:52 +0000579 static char *keywords[] = {"sequence", NULL};
580
Fred Drakec2683dd2001-07-17 19:32:05 +0000581 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000582 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000583 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000584 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000585 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000586 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000587 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000588 }
589 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000590 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000591 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000592 tree = build_node_tree(tuple);
593 if (tree != 0) {
594 int start_sym = TYPE(tree);
595 if (start_sym == eval_input) {
596 /* Might be an eval form. */
597 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000598 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000599 else
600 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000601 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000602 else if (start_sym == file_input) {
603 /* This looks like an exec form so far. */
604 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000605 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000606 else
607 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000608 }
609 else {
610 /* This is a fragment, at best. */
611 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000612 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000613 }
Guido van Rossum47478871996-08-21 14:32:37 +0000614 }
Guido van Rossum47478871996-08-21 14:32:37 +0000615 /* Make sure we throw an exception on all errors. We should never
616 * get this, but we'd do well to be sure something is done.
617 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000618 if (st == NULL && !PyErr_Occurred())
619 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000620
Fred Drakec2683dd2001-07-17 19:32:05 +0000621 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000622}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000623
624
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000625/* node* build_node_children()
626 *
627 * Iterate across the children of the current non-terminal node and build
628 * their structures. If successful, return the root of this portion of
629 * the tree, otherwise, 0. Any required exception will be specified already,
630 * and no memory will have been deallocated.
631 *
632 */
633static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000634build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000635{
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000636 int len = PyObject_Size(tuple);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000637 int i, err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000638
639 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000640 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000641 PyObject* elem = PySequence_GetItem(tuple, i);
642 int ok = elem != NULL;
643 long type = 0;
644 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000645
Fred Drakeff9ea482000-04-19 13:54:15 +0000646 if (ok)
647 ok = PySequence_Check(elem);
648 if (ok) {
649 PyObject *temp = PySequence_GetItem(elem, 0);
650 if (temp == NULL)
651 ok = 0;
652 else {
653 ok = PyInt_Check(temp);
654 if (ok)
655 type = PyInt_AS_LONG(temp);
656 Py_DECREF(temp);
657 }
658 }
659 if (!ok) {
660 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000661 Py_BuildValue("os", elem,
Fred Drakeff9ea482000-04-19 13:54:15 +0000662 "Illegal node construct."));
663 Py_XDECREF(elem);
664 return (0);
665 }
666 if (ISTERMINAL(type)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000667 int len = PyObject_Size(elem);
668 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000669
Fred Drake0ac9b072000-09-12 21:58:06 +0000670 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000671 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000672 return 0;
673 }
674 temp = PySequence_GetItem(elem, 1);
675 if (temp == NULL)
676 return 0;
677 if (!PyString_Check(temp)) {
678 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000679 "second item in terminal node must be a string,"
680 " found %s",
Fred Drake0ac9b072000-09-12 21:58:06 +0000681 ((PyTypeObject*)PyObject_Type(temp))->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000682 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000683 return 0;
684 }
685 if (len == 3) {
686 PyObject *o = PySequence_GetItem(elem, 2);
687 if (o != NULL) {
688 if (PyInt_Check(o))
689 *line_num = PyInt_AS_LONG(o);
690 else {
691 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000692 "third item in terminal node must be an"
693 " integer, found %s",
Fred Drake0ac9b072000-09-12 21:58:06 +0000694 ((PyTypeObject*)PyObject_Type(temp))->tp_name);
695 Py_DECREF(o);
696 Py_DECREF(temp);
697 return 0;
698 }
699 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000700 }
701 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000702 len = PyString_GET_SIZE(temp) + 1;
703 strn = (char *)PyMem_MALLOC(len);
704 if (strn != NULL)
705 (void) memcpy(strn, PyString_AS_STRING(temp), len);
706 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000707 }
708 else if (!ISNONTERMINAL(type)) {
709 /*
710 * It has to be one or the other; this is an error.
711 * Throw an exception.
712 */
713 PyErr_SetObject(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000714 Py_BuildValue("os", elem, "unknown node type."));
Fred Drakeff9ea482000-04-19 13:54:15 +0000715 Py_XDECREF(elem);
716 return (0);
717 }
Fred Drake8b55b2d2001-12-05 22:10:44 +0000718 err = PyNode_AddChild(root, type, strn, *line_num);
719 if (err == E_NOMEM) {
720 PyMem_DEL(strn);
721 return (node *) PyErr_NoMemory();
722 }
723 if (err == E_OVERFLOW) {
724 PyMem_DEL(strn);
725 PyErr_SetString(PyExc_ValueError,
726 "unsupported number of child nodes");
727 return NULL;
728 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000729
Fred Drakeff9ea482000-04-19 13:54:15 +0000730 if (ISNONTERMINAL(type)) {
731 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000732
Fred Drakeff9ea482000-04-19 13:54:15 +0000733 if (new_child != build_node_children(elem, new_child, line_num)) {
734 Py_XDECREF(elem);
735 return (0);
736 }
737 }
738 else if (type == NEWLINE) { /* It's true: we increment the */
739 ++(*line_num); /* line number *after* the newline! */
740 }
741 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000742 }
743 return (root);
Fred Drakeff9ea482000-04-19 13:54:15 +0000744}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000745
746
747static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000748build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000749{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000750 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000751 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000752 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000753
Guido van Rossum47478871996-08-21 14:32:37 +0000754 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000755 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000756 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000757 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000758 /*
759 * The tuple is simple, but it doesn't start with a start symbol.
760 * Throw an exception now and be done with it.
761 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000762 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000763 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000764 PyErr_SetObject(parser_error, tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000765 }
766 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000767 /*
768 * Not efficient, but that can be handled later.
769 */
770 int line_num = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000771
Fred Drakeff9ea482000-04-19 13:54:15 +0000772 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000773 if (res != NULL) {
774 if (res != build_node_children(tuple, res, &line_num)) {
775 PyNode_Free(res);
776 res = NULL;
777 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000778 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000779 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000780 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000781 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000782 * NONTERMINAL, we can't use it. Not sure the implementation
783 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000784 */
785 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000786 Py_BuildValue("os", tuple,
Fred Drakeff9ea482000-04-19 13:54:15 +0000787 "Illegal component tuple."));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000788
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000789 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000790}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000791
792
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000793/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000794 * Validation routines used within the validation section:
795 */
Fred Drakeff9ea482000-04-19 13:54:15 +0000796staticforward int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000797
Fred Drakeff9ea482000-04-19 13:54:15 +0000798#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
799#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
800#define validate_colon(ch) validate_terminal(ch, COLON, ":")
801#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
802#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
803#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
804#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
805#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
806#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
807#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
808#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
809#define validate_star(ch) validate_terminal(ch, STAR, "*")
810#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
811#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
812#define validate_dot(ch) validate_terminal(ch, DOT, ".")
813#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000814
Fred Drake0ac9b072000-09-12 21:58:06 +0000815#define VALIDATER(n) static int validate_##n(node *tree)
816
Fred Drakeff9ea482000-04-19 13:54:15 +0000817VALIDATER(node); VALIDATER(small_stmt);
818VALIDATER(class); VALIDATER(node);
819VALIDATER(parameters); VALIDATER(suite);
820VALIDATER(testlist); VALIDATER(varargslist);
821VALIDATER(fpdef); VALIDATER(fplist);
822VALIDATER(stmt); VALIDATER(simple_stmt);
823VALIDATER(expr_stmt); VALIDATER(power);
824VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000825VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000826VALIDATER(raise_stmt); VALIDATER(import_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000827VALIDATER(global_stmt); VALIDATER(list_if);
828VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000829VALIDATER(exec_stmt); VALIDATER(compound_stmt);
830VALIDATER(while); VALIDATER(for);
831VALIDATER(try); VALIDATER(except_clause);
832VALIDATER(test); VALIDATER(and_test);
833VALIDATER(not_test); VALIDATER(comparison);
834VALIDATER(comp_op); VALIDATER(expr);
835VALIDATER(xor_expr); VALIDATER(and_expr);
836VALIDATER(shift_expr); VALIDATER(arith_expr);
837VALIDATER(term); VALIDATER(factor);
838VALIDATER(atom); VALIDATER(lambdef);
839VALIDATER(trailer); VALIDATER(subscript);
840VALIDATER(subscriptlist); VALIDATER(sliceop);
841VALIDATER(exprlist); VALIDATER(dictmaker);
842VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000843VALIDATER(listmaker); VALIDATER(yield_stmt);
Guido van Rossum2d3b9862002-05-24 15:47:06 +0000844VALIDATER(testlist1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000845
Fred Drake0ac9b072000-09-12 21:58:06 +0000846#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000847
Fred Drakeff9ea482000-04-19 13:54:15 +0000848#define is_even(n) (((n) & 1) == 0)
849#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000850
851
852static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000853validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000854{
Fred Drake0ac9b072000-09-12 21:58:06 +0000855 if (TYPE(n) != t) {
856 PyErr_Format(parser_error, "Expected node type %d, got %d.",
857 t, TYPE(n));
858 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000859 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000860 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000861}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000862
863
Fred Drakee7ab64e2000-04-25 04:14:46 +0000864/* Verifies that the number of child nodes is exactly 'num', raising
865 * an exception if it isn't. The exception message does not indicate
866 * the exact number of nodes, allowing this to be used to raise the
867 * "right" exception when the wrong number of nodes is present in a
868 * specific variant of a statement's syntax. This is commonly used
869 * in that fashion.
870 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000871static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000872validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000873{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000874 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000875 PyErr_Format(parser_error,
876 "Illegal number of children for %s node.", name);
877 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000878 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000879 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000880}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000881
882
883static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000884validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000885{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000886 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000887 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000888
889 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000890 PyErr_Format(parser_error,
891 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000892 }
893 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000894}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000895
896
Guido van Rossum47478871996-08-21 14:32:37 +0000897/* X (',' X) [',']
898 */
899static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000900validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000901 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000902{
903 int nch = NCH(tree);
904 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000905 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000906
907 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000908 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000909 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000910 if (is_even(nch))
911 res = validate_comma(CHILD(tree, --nch));
912 if (res && nch > 1) {
913 int pos = 1;
914 for ( ; res && pos < nch; pos += 2)
915 res = (validate_comma(CHILD(tree, pos))
916 && vfunc(CHILD(tree, pos + 1)));
917 }
Guido van Rossum47478871996-08-21 14:32:37 +0000918 }
919 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000920}
Guido van Rossum47478871996-08-21 14:32:37 +0000921
922
Fred Drakecff283c2000-08-21 22:24:43 +0000923/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000924 *
925 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000926 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000927 */
Guido van Rossum47478871996-08-21 14:32:37 +0000928static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000929validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000930{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000931 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000932 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000933
Guido van Rossum3d602e31996-07-21 02:33:56 +0000934 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000935 res = (validate_name(CHILD(tree, 0), "class")
936 && validate_ntype(CHILD(tree, 1), NAME)
937 && validate_colon(CHILD(tree, nch - 2))
938 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000939 }
940 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000941 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000942 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000943 res = (validate_lparen(CHILD(tree, 2))
944 && validate_testlist(CHILD(tree, 3))
945 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000946 }
947 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000948}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000949
950
Guido van Rossum3d602e31996-07-21 02:33:56 +0000951/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +0000952 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +0000953 */
Guido van Rossum47478871996-08-21 14:32:37 +0000954static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000955validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000956{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000957 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000958 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +0000959 && (nch >= 4)
960 && validate_name(CHILD(tree, 0), "if")
961 && validate_test(CHILD(tree, 1))
962 && validate_colon(CHILD(tree, 2))
963 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000964
965 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000966 /* ... 'else' ':' suite */
967 res = (validate_name(CHILD(tree, nch - 3), "else")
968 && validate_colon(CHILD(tree, nch - 2))
969 && validate_suite(CHILD(tree, nch - 1)));
970 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000971 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000972 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000973 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000974 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +0000975 /* Will catch the case for nch < 4 */
976 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000977 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000978 /* ... ('elif' test ':' suite)+ ... */
979 int j = 4;
980 while ((j < nch) && res) {
981 res = (validate_name(CHILD(tree, j), "elif")
982 && validate_colon(CHILD(tree, j + 2))
983 && validate_test(CHILD(tree, j + 1))
984 && validate_suite(CHILD(tree, j + 3)));
985 j += 4;
986 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000987 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000988 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000989}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000990
991
Guido van Rossum3d602e31996-07-21 02:33:56 +0000992/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +0000993 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +0000994 *
995 */
Guido van Rossum47478871996-08-21 14:32:37 +0000996static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000997validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000998{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000999 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001000 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001001
Guido van Rossum3d602e31996-07-21 02:33:56 +00001002 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001003 res = (validate_lparen(CHILD(tree, 0))
1004 && validate_rparen(CHILD(tree, nch - 1)));
1005 if (res && (nch == 3))
1006 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001007 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001008 else {
1009 (void) validate_numnodes(tree, 2, "parameters");
1010 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001011 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001012}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001013
1014
Fred Drakecff283c2000-08-21 22:24:43 +00001015/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001016 *
1017 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001018 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001019 * | NEWLINE INDENT stmt+ DEDENT
1020 */
Guido van Rossum47478871996-08-21 14:32:37 +00001021static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001022validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001023{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001024 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001025 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001026
Guido van Rossum3d602e31996-07-21 02:33:56 +00001027 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001028 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001029 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001030 /* NEWLINE INDENT stmt+ DEDENT */
1031 res = (validate_newline(CHILD(tree, 0))
1032 && validate_indent(CHILD(tree, 1))
1033 && validate_stmt(CHILD(tree, 2))
1034 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001035
Fred Drakeff9ea482000-04-19 13:54:15 +00001036 if (res && (nch > 4)) {
1037 int i = 3;
1038 --nch; /* forget the DEDENT */
1039 for ( ; res && (i < nch); ++i)
1040 res = validate_stmt(CHILD(tree, i));
1041 }
1042 else if (nch < 4)
1043 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001044 }
1045 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001046}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001047
1048
Guido van Rossum47478871996-08-21 14:32:37 +00001049static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001050validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001051{
Guido van Rossum47478871996-08-21 14:32:37 +00001052 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001053 validate_test, "testlist"));
1054}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001055
1056
Guido van Rossum1c917072001-10-15 15:44:05 +00001057static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001058validate_testlist1(node *tree)
1059{
1060 return (validate_repeating_list(tree, testlist1,
1061 validate_test, "testlist1"));
1062}
1063
1064
1065static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001066validate_testlist_safe(node *tree)
1067{
1068 return (validate_repeating_list(tree, testlist_safe,
1069 validate_test, "testlist_safe"));
1070}
1071
1072
Fred Drakecff283c2000-08-21 22:24:43 +00001073/* '*' NAME [',' '**' NAME] | '**' NAME
1074 */
1075static int
1076validate_varargslist_trailer(node *tree, int start)
1077{
1078 int nch = NCH(tree);
1079 int res = 0;
1080 int sym;
1081
1082 if (nch <= start) {
1083 err_string("expected variable argument trailer for varargslist");
1084 return 0;
1085 }
1086 sym = TYPE(CHILD(tree, start));
1087 if (sym == STAR) {
1088 /*
1089 * ('*' NAME [',' '**' NAME]
1090 */
1091 if (nch-start == 2)
1092 res = validate_name(CHILD(tree, start+1), NULL);
1093 else if (nch-start == 5)
1094 res = (validate_name(CHILD(tree, start+1), NULL)
1095 && validate_comma(CHILD(tree, start+2))
1096 && validate_doublestar(CHILD(tree, start+3))
1097 && validate_name(CHILD(tree, start+4), NULL));
1098 }
1099 else if (sym == DOUBLESTAR) {
1100 /*
1101 * '**' NAME
1102 */
1103 if (nch-start == 2)
1104 res = validate_name(CHILD(tree, start+1), NULL);
1105 }
1106 if (!res)
1107 err_string("illegal variable argument trailer for varargslist");
1108 return res;
1109}
1110
1111
1112/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001113 *
1114 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001115 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001116 * ('*' NAME [',' '**' NAME]
1117 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001118 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1119 *
1120 */
Guido van Rossum47478871996-08-21 14:32:37 +00001121static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001122validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001123{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001124 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001125 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001126 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001127
Fred Drakeb6429a22000-12-11 22:08:27 +00001128 if (!res)
1129 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001130 if (nch < 1) {
1131 err_string("varargslist missing child nodes");
1132 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001133 }
Fred Drakecff283c2000-08-21 22:24:43 +00001134 sym = TYPE(CHILD(tree, 0));
1135 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001136 /* whole thing matches:
1137 * '*' NAME [',' '**' NAME] | '**' NAME
1138 */
Fred Drakecff283c2000-08-21 22:24:43 +00001139 res = validate_varargslist_trailer(tree, 0);
1140 else if (sym == fpdef) {
1141 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001142
Fred Drakecff283c2000-08-21 22:24:43 +00001143 sym = TYPE(CHILD(tree, nch-1));
1144 if (sym == NAME) {
1145 /*
1146 * (fpdef ['=' test] ',')+
1147 * ('*' NAME [',' '**' NAME]
1148 * | '**' NAME)
1149 */
1150 /* skip over (fpdef ['=' test] ',')+ */
1151 while (res && (i+2 <= nch)) {
1152 res = validate_fpdef(CHILD(tree, i));
1153 ++i;
1154 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1155 res = (validate_equal(CHILD(tree, i))
1156 && validate_test(CHILD(tree, i+1)));
1157 if (res)
1158 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001159 }
Fred Drakecff283c2000-08-21 22:24:43 +00001160 if (res && i < nch) {
1161 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001162 ++i;
1163 if (res && i < nch
1164 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1165 || TYPE(CHILD(tree, i)) == STAR))
1166 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001167 }
1168 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001169 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1170 * i --^^^
1171 */
Fred Drakecff283c2000-08-21 22:24:43 +00001172 if (res)
1173 res = validate_varargslist_trailer(tree, i);
1174 }
1175 else {
1176 /*
1177 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1178 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001179 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001180 if (sym == COMMA) {
1181 res = validate_comma(CHILD(tree, nch-1));
1182 if (!res)
1183 return 0;
1184 --nch;
1185 }
1186 /*
1187 * fpdef ['=' test] (',' fpdef ['=' test])*
1188 */
1189 res = validate_fpdef(CHILD(tree, 0));
1190 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001191 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1192 res = (validate_equal(CHILD(tree, i))
1193 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001194 i += 2;
1195 }
1196 /*
1197 * ... (',' fpdef ['=' test])*
1198 * i ---^^^
1199 */
1200 while (res && (nch - i) >= 2) {
1201 res = (validate_comma(CHILD(tree, i))
1202 && validate_fpdef(CHILD(tree, i+1)));
1203 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001204 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1205 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001206 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001207 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001208 }
1209 }
1210 if (res && nch - i != 0) {
1211 res = 0;
1212 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001213 }
1214 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001215 }
Fred Drakecff283c2000-08-21 22:24:43 +00001216 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001217}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001218
1219
Fred Drakecff283c2000-08-21 22:24:43 +00001220/* list_iter: list_for | list_if
1221 */
1222static int
1223validate_list_iter(node *tree)
1224{
1225 int res = (validate_ntype(tree, list_iter)
1226 && validate_numnodes(tree, 1, "list_iter"));
1227 if (res && TYPE(CHILD(tree, 0)) == list_for)
1228 res = validate_list_for(CHILD(tree, 0));
1229 else
1230 res = validate_list_if(CHILD(tree, 0));
1231
1232 return res;
1233}
1234
1235/* list_for: 'for' exprlist 'in' testlist [list_iter]
1236 */
1237static int
1238validate_list_for(node *tree)
1239{
1240 int nch = NCH(tree);
1241 int res;
1242
1243 if (nch == 5)
1244 res = validate_list_iter(CHILD(tree, 4));
1245 else
1246 res = validate_numnodes(tree, 4, "list_for");
1247
1248 if (res)
1249 res = (validate_name(CHILD(tree, 0), "for")
1250 && validate_exprlist(CHILD(tree, 1))
1251 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001252 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001253
1254 return res;
1255}
1256
1257/* list_if: 'if' test [list_iter]
1258 */
1259static int
1260validate_list_if(node *tree)
1261{
1262 int nch = NCH(tree);
1263 int res;
1264
1265 if (nch == 3)
1266 res = validate_list_iter(CHILD(tree, 2));
1267 else
1268 res = validate_numnodes(tree, 2, "list_if");
1269
1270 if (res)
1271 res = (validate_name(CHILD(tree, 0), "if")
1272 && validate_test(CHILD(tree, 1)));
1273
1274 return res;
1275}
1276
1277
1278/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001279 *
1280 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001281 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001282 * | '(' fplist ')'
1283 */
Guido van Rossum47478871996-08-21 14:32:37 +00001284static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001285validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001286{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001287 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001288 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001289
Guido van Rossum3d602e31996-07-21 02:33:56 +00001290 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001291 if (nch == 1)
1292 res = validate_ntype(CHILD(tree, 0), NAME);
1293 else if (nch == 3)
1294 res = (validate_lparen(CHILD(tree, 0))
1295 && validate_fplist(CHILD(tree, 1))
1296 && validate_rparen(CHILD(tree, 2)));
1297 else
1298 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001299 }
1300 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001301}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001302
1303
Guido van Rossum47478871996-08-21 14:32:37 +00001304static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001305validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001306{
Guido van Rossum47478871996-08-21 14:32:37 +00001307 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001308 validate_fpdef, "fplist"));
1309}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001310
1311
Guido van Rossum3d602e31996-07-21 02:33:56 +00001312/* simple_stmt | compound_stmt
1313 *
1314 */
Guido van Rossum47478871996-08-21 14:32:37 +00001315static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001316validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001317{
1318 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001319 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001320
Guido van Rossum3d602e31996-07-21 02:33:56 +00001321 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001322 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001323
Fred Drakeff9ea482000-04-19 13:54:15 +00001324 if (TYPE(tree) == simple_stmt)
1325 res = validate_simple_stmt(tree);
1326 else
1327 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001328 }
1329 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001330}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001331
1332
Guido van Rossum3d602e31996-07-21 02:33:56 +00001333/* small_stmt (';' small_stmt)* [';'] NEWLINE
1334 *
1335 */
Guido van Rossum47478871996-08-21 14:32:37 +00001336static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001337validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001338{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001339 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001340 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001341 && (nch >= 2)
1342 && validate_small_stmt(CHILD(tree, 0))
1343 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001344
Guido van Rossum3d602e31996-07-21 02:33:56 +00001345 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001346 res = validate_numnodes(tree, 2, "simple_stmt");
1347 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001348 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001349 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001350 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001351 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001352
Fred Drakeff9ea482000-04-19 13:54:15 +00001353 for (i = 1; res && (i < nch); i += 2)
1354 res = (validate_semi(CHILD(tree, i))
1355 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001356 }
1357 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001358}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001359
1360
Guido van Rossum47478871996-08-21 14:32:37 +00001361static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001362validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001363{
1364 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001365 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001366
Fred Drake0ac9b072000-09-12 21:58:06 +00001367 if (res) {
1368 int ntype = TYPE(CHILD(tree, 0));
1369
1370 if ( (ntype == expr_stmt)
1371 || (ntype == print_stmt)
1372 || (ntype == del_stmt)
1373 || (ntype == pass_stmt)
1374 || (ntype == flow_stmt)
1375 || (ntype == import_stmt)
1376 || (ntype == global_stmt)
1377 || (ntype == assert_stmt)
1378 || (ntype == exec_stmt))
1379 res = validate_node(CHILD(tree, 0));
1380 else {
1381 res = 0;
1382 err_string("illegal small_stmt child type");
1383 }
1384 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001385 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001386 res = 0;
1387 PyErr_Format(parser_error,
1388 "Unrecognized child node of small_stmt: %d.",
1389 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001390 }
1391 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001392}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001393
1394
1395/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001396 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001397 */
Guido van Rossum47478871996-08-21 14:32:37 +00001398static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001399validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001400{
1401 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001402 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001403 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001404
1405 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001406 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001407
1408 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001409 ntype = TYPE(tree);
1410 if ( (ntype == if_stmt)
1411 || (ntype == while_stmt)
1412 || (ntype == for_stmt)
1413 || (ntype == try_stmt)
1414 || (ntype == funcdef)
1415 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001416 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001417 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001418 res = 0;
1419 PyErr_Format(parser_error,
1420 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001421 }
1422 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001423}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001424
1425
Guido van Rossum47478871996-08-21 14:32:37 +00001426static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001427validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001428{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001429 int j;
1430 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001431 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001432 && is_odd(nch)
1433 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001434
Fred Drake28f739a2000-08-25 22:42:40 +00001435 if (res && nch == 3
1436 && TYPE(CHILD(tree, 1)) == augassign) {
1437 res = (validate_numnodes(CHILD(tree, 1), 1, "augassign")
1438 && validate_testlist(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001439
Fred Drake28f739a2000-08-25 22:42:40 +00001440 if (res) {
1441 char *s = STR(CHILD(CHILD(tree, 1), 0));
1442
1443 res = (strcmp(s, "+=") == 0
1444 || strcmp(s, "-=") == 0
1445 || strcmp(s, "*=") == 0
1446 || strcmp(s, "/=") == 0
1447 || strcmp(s, "%=") == 0
1448 || strcmp(s, "&=") == 0
1449 || strcmp(s, "|=") == 0
1450 || strcmp(s, "^=") == 0
1451 || strcmp(s, "<<=") == 0
1452 || strcmp(s, ">>=") == 0
1453 || strcmp(s, "**=") == 0);
1454 if (!res)
1455 err_string("illegal augmmented assignment operator");
1456 }
1457 }
1458 else {
1459 for (j = 1; res && (j < nch); j += 2)
1460 res = (validate_equal(CHILD(tree, j))
1461 && validate_testlist(CHILD(tree, j + 1)));
1462 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001463 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001464}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001465
1466
Guido van Rossum3d602e31996-07-21 02:33:56 +00001467/* print_stmt:
1468 *
Fred Drakecff283c2000-08-21 22:24:43 +00001469 * 'print' ( [ test (',' test)* [','] ]
1470 * | '>>' test [ (',' test)+ [','] ] )
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_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001474{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001475 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001476 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001477 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001478 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001479
Fred Drakecff283c2000-08-21 22:24:43 +00001480 if (res && nch > 1) {
1481 int sym = TYPE(CHILD(tree, 1));
1482 int i = 1;
1483 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001484
Fred Drakecff283c2000-08-21 22:24:43 +00001485 if (sym == test)
1486 res = validate_test(CHILD(tree, i++));
1487 else {
1488 if (nch < 3)
1489 res = validate_numnodes(tree, 3, "print_stmt");
1490 else {
1491 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1492 && validate_test(CHILD(tree, i+1)));
1493 i += 2;
1494 allow_trailing_comma = 0;
1495 }
1496 }
1497 if (res) {
1498 /* ... (',' test)* [','] */
1499 while (res && i+2 <= nch) {
1500 res = (validate_comma(CHILD(tree, i))
1501 && validate_test(CHILD(tree, i+1)));
1502 allow_trailing_comma = 1;
1503 i += 2;
1504 }
1505 if (res && !allow_trailing_comma)
1506 res = validate_numnodes(tree, i, "print_stmt");
1507 else if (res && i < nch)
1508 res = validate_comma(CHILD(tree, i));
1509 }
1510 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001511 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001512}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001513
1514
Guido van Rossum47478871996-08-21 14:32:37 +00001515static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001516validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001517{
1518 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001519 && validate_name(CHILD(tree, 0), "del")
1520 && validate_exprlist(CHILD(tree, 1)));
1521}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001522
1523
Guido van Rossum47478871996-08-21 14:32:37 +00001524static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001525validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001526{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001527 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001528 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001529 && ((nch == 1) || (nch == 2))
1530 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001531
Guido van Rossum3d602e31996-07-21 02:33:56 +00001532 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001533 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001534
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001535 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001536}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001537
1538
Guido van Rossum47478871996-08-21 14:32:37 +00001539static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001540validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001541{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001542 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001543 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001544 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001545
Guido van Rossum3d602e31996-07-21 02:33:56 +00001546 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001547 res = validate_name(CHILD(tree, 0), "raise");
1548 if (res && (nch >= 2))
1549 res = validate_test(CHILD(tree, 1));
1550 if (res && nch > 2) {
1551 res = (validate_comma(CHILD(tree, 2))
1552 && validate_test(CHILD(tree, 3)));
1553 if (res && (nch > 4))
1554 res = (validate_comma(CHILD(tree, 4))
1555 && validate_test(CHILD(tree, 5)));
1556 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001557 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001558 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001559 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001560 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001561 res = (validate_comma(CHILD(tree, 2))
1562 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001563
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001564 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001565}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001566
1567
Fred Drake02126f22001-07-17 02:59:15 +00001568/* yield_stmt: 'yield' testlist
1569 */
1570static int
1571validate_yield_stmt(node *tree)
1572{
1573 return (validate_ntype(tree, yield_stmt)
1574 && validate_numnodes(tree, 2, "yield_stmt")
1575 && validate_name(CHILD(tree, 0), "yield")
1576 && validate_testlist(CHILD(tree, 1)));
1577}
1578
1579
Fred Drakecff283c2000-08-21 22:24:43 +00001580static int
1581validate_import_as_name(node *tree)
1582{
1583 int nch = NCH(tree);
1584 int ok = validate_ntype(tree, import_as_name);
1585
1586 if (ok) {
1587 if (nch == 1)
1588 ok = validate_name(CHILD(tree, 0), NULL);
1589 else if (nch == 3)
1590 ok = (validate_name(CHILD(tree, 0), NULL)
1591 && validate_name(CHILD(tree, 1), "as")
1592 && validate_name(CHILD(tree, 2), NULL));
1593 else
1594 ok = validate_numnodes(tree, 3, "import_as_name");
1595 }
1596 return ok;
1597}
1598
1599
Fred Drake71137082001-01-07 05:59:59 +00001600/* dotted_name: NAME ("." NAME)*
1601 */
1602static int
1603validate_dotted_name(node *tree)
1604{
1605 int nch = NCH(tree);
1606 int res = (validate_ntype(tree, dotted_name)
1607 && is_odd(nch)
1608 && validate_name(CHILD(tree, 0), NULL));
1609 int i;
1610
1611 for (i = 1; res && (i < nch); i += 2) {
1612 res = (validate_dot(CHILD(tree, i))
1613 && validate_name(CHILD(tree, i+1), NULL));
1614 }
1615 return res;
1616}
1617
1618
Fred Drakecff283c2000-08-21 22:24:43 +00001619/* dotted_as_name: dotted_name [NAME NAME]
1620 */
1621static int
1622validate_dotted_as_name(node *tree)
1623{
1624 int nch = NCH(tree);
1625 int res = validate_ntype(tree, dotted_as_name);
1626
1627 if (res) {
1628 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001629 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001630 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001631 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001632 && validate_name(CHILD(tree, 1), "as")
1633 && validate_name(CHILD(tree, 2), NULL));
1634 else {
1635 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001636 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001637 }
1638 }
1639 return res;
1640}
1641
1642
Guido van Rossum3d602e31996-07-21 02:33:56 +00001643/* import_stmt:
1644 *
Fred Drakecff283c2000-08-21 22:24:43 +00001645 * 'import' dotted_as_name (',' dotted_as_name)*
1646 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001647 */
Guido van Rossum47478871996-08-21 14:32:37 +00001648static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001649validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001650{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001651 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001652 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001653 && (nch >= 2) && is_even(nch)
Fred Drakecff283c2000-08-21 22:24:43 +00001654 && validate_ntype(CHILD(tree, 0), NAME));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001655
1656 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001657 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001658
Fred Drakecff283c2000-08-21 22:24:43 +00001659 res = validate_dotted_as_name(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00001660 for (j = 2; res && (j < nch); j += 2)
1661 res = (validate_comma(CHILD(tree, j))
Fred Drake71137082001-01-07 05:59:59 +00001662 && validate_dotted_as_name(CHILD(tree, j + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001663 }
Fred Drakecff283c2000-08-21 22:24:43 +00001664 else if (res && (res = validate_name(CHILD(tree, 0), "from"))) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001665 res = ((nch >= 4) && is_even(nch)
Fred Drake71137082001-01-07 05:59:59 +00001666 && validate_dotted_name(CHILD(tree, 1))
1667 && validate_name(CHILD(tree, 2), "import"));
Fred Drakeff9ea482000-04-19 13:54:15 +00001668 if (nch == 4) {
Fred Drakecff283c2000-08-21 22:24:43 +00001669 if (TYPE(CHILD(tree, 3)) == import_as_name)
1670 res = validate_import_as_name(CHILD(tree, 3));
1671 else
1672 res = validate_star(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001673 }
1674 else {
Fred Drakecff283c2000-08-21 22:24:43 +00001675 /* 'from' dotted_name 'import' import_as_name
1676 * (',' import_as_name)+
1677 */
Fred Drakeff9ea482000-04-19 13:54:15 +00001678 int j;
Fred Drakecff283c2000-08-21 22:24:43 +00001679 res = validate_import_as_name(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001680 for (j = 4; res && (j < nch); j += 2)
1681 res = (validate_comma(CHILD(tree, j))
Fred Drakecff283c2000-08-21 22:24:43 +00001682 && validate_import_as_name(CHILD(tree, j + 1)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001683 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001684 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001685 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001686 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001687
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001688 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001689}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001690
1691
Guido van Rossum47478871996-08-21 14:32:37 +00001692static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001693validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001694{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001695 int j;
1696 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001697 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001698 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001699
Guido van Rossum3d602e31996-07-21 02:33:56 +00001700 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001701 res = (validate_name(CHILD(tree, 0), "global")
1702 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001703 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001704 res = (validate_comma(CHILD(tree, j))
1705 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001706
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001707 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001708}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001709
1710
Guido van Rossum3d602e31996-07-21 02:33:56 +00001711/* exec_stmt:
1712 *
1713 * 'exec' expr ['in' test [',' test]]
1714 */
Guido van Rossum47478871996-08-21 14:32:37 +00001715static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001716validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001717{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001718 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001719 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001720 && ((nch == 2) || (nch == 4) || (nch == 6))
1721 && validate_name(CHILD(tree, 0), "exec")
1722 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001723
Guido van Rossum3d602e31996-07-21 02:33:56 +00001724 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001725 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001726 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001727 res = (validate_name(CHILD(tree, 2), "in")
1728 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001729 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001730 res = (validate_comma(CHILD(tree, 4))
1731 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001732
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001733 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001734}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001735
1736
Guido van Rossum925e5471997-04-02 05:32:13 +00001737/* assert_stmt:
1738 *
1739 * 'assert' test [',' test]
1740 */
1741static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001742validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001743{
1744 int nch = NCH(tree);
1745 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001746 && ((nch == 2) || (nch == 4))
1747 && (validate_name(CHILD(tree, 0), "__assert__") ||
1748 validate_name(CHILD(tree, 0), "assert"))
1749 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001750
1751 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001752 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001753 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001754 res = (validate_comma(CHILD(tree, 2))
1755 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001756
1757 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001758}
Guido van Rossum925e5471997-04-02 05:32:13 +00001759
1760
Guido van Rossum47478871996-08-21 14:32:37 +00001761static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001762validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001763{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001764 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001765 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001766 && ((nch == 4) || (nch == 7))
1767 && validate_name(CHILD(tree, 0), "while")
1768 && validate_test(CHILD(tree, 1))
1769 && validate_colon(CHILD(tree, 2))
1770 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001771
Guido van Rossum3d602e31996-07-21 02:33:56 +00001772 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001773 res = (validate_name(CHILD(tree, 4), "else")
1774 && validate_colon(CHILD(tree, 5))
1775 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001776
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001777 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001778}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001779
1780
Guido van Rossum47478871996-08-21 14:32:37 +00001781static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001782validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001783{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001784 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001785 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001786 && ((nch == 6) || (nch == 9))
1787 && validate_name(CHILD(tree, 0), "for")
1788 && validate_exprlist(CHILD(tree, 1))
1789 && validate_name(CHILD(tree, 2), "in")
1790 && validate_testlist(CHILD(tree, 3))
1791 && validate_colon(CHILD(tree, 4))
1792 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001793
Guido van Rossum3d602e31996-07-21 02:33:56 +00001794 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001795 res = (validate_name(CHILD(tree, 6), "else")
1796 && validate_colon(CHILD(tree, 7))
1797 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001798
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001799 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001800}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001801
1802
Guido van Rossum3d602e31996-07-21 02:33:56 +00001803/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001804 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001805 * | 'try' ':' suite 'finally' ':' suite
1806 *
1807 */
Guido van Rossum47478871996-08-21 14:32:37 +00001808static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001809validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001810{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001811 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001812 int pos = 3;
1813 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001814 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001815
1816 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001817 res = (validate_name(CHILD(tree, 0), "try")
1818 && validate_colon(CHILD(tree, 1))
1819 && validate_suite(CHILD(tree, 2))
1820 && validate_colon(CHILD(tree, nch - 2))
1821 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001822 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001823 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001824 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1825 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001826
1827 PyErr_Format(parser_error,
1828 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001829 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001830 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001831 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001832 res = (validate_except_clause(CHILD(tree, pos))
1833 && validate_colon(CHILD(tree, pos + 1))
1834 && validate_suite(CHILD(tree, pos + 2)));
1835 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001836 }
1837 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001838 res = validate_ntype(CHILD(tree, pos), NAME);
1839 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1840 res = (validate_numnodes(tree, 6, "try/finally")
1841 && validate_colon(CHILD(tree, 4))
1842 && validate_suite(CHILD(tree, 5)));
1843 else if (res) {
1844 if (nch == (pos + 3)) {
1845 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1846 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1847 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001848 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001849 }
1850 else if (nch == (pos + 6)) {
1851 res = (validate_name(CHILD(tree, pos), "except")
1852 && validate_colon(CHILD(tree, pos + 1))
1853 && validate_suite(CHILD(tree, pos + 2))
1854 && validate_name(CHILD(tree, pos + 3), "else"));
1855 }
1856 else
1857 res = validate_numnodes(tree, pos + 3, "try/except");
1858 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001859 }
1860 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001861}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001862
1863
Guido van Rossum47478871996-08-21 14:32:37 +00001864static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001865validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001866{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001867 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001868 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001869 && ((nch == 1) || (nch == 2) || (nch == 4))
1870 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001871
Guido van Rossum3d602e31996-07-21 02:33:56 +00001872 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001873 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001874 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001875 res = (validate_comma(CHILD(tree, 2))
1876 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001877
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001878 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001879}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001880
1881
Guido van Rossum47478871996-08-21 14:32:37 +00001882static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001883validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001884{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001885 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001886 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001887
Guido van Rossum3d602e31996-07-21 02:33:56 +00001888 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001889 res = ((nch == 1)
1890 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001891 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001892 int pos;
1893 res = validate_and_test(CHILD(tree, 0));
1894 for (pos = 1; res && (pos < nch); pos += 2)
1895 res = (validate_name(CHILD(tree, pos), "or")
1896 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001897 }
1898 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001899}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001900
1901
Guido van Rossum47478871996-08-21 14:32:37 +00001902static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001903validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001904{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001905 int pos;
1906 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001907 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001908 && is_odd(nch)
1909 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001910
Guido van Rossum3d602e31996-07-21 02:33:56 +00001911 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001912 res = (validate_name(CHILD(tree, pos), "and")
1913 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001914
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001915 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001916}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001917
1918
Guido van Rossum47478871996-08-21 14:32:37 +00001919static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001920validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001921{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001922 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001923 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001924
Guido van Rossum3d602e31996-07-21 02:33:56 +00001925 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001926 if (nch == 2)
1927 res = (validate_name(CHILD(tree, 0), "not")
1928 && validate_not_test(CHILD(tree, 1)));
1929 else if (nch == 1)
1930 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001931 }
1932 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001933}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001934
1935
Guido van Rossum47478871996-08-21 14:32:37 +00001936static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001937validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001938{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001939 int pos;
1940 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001941 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001942 && is_odd(nch)
1943 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001944
Guido van Rossum3d602e31996-07-21 02:33:56 +00001945 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001946 res = (validate_comp_op(CHILD(tree, pos))
1947 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001948
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001949 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001950}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001951
1952
Guido van Rossum47478871996-08-21 14:32:37 +00001953static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001954validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001955{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001956 int res = 0;
1957 int nch = NCH(tree);
1958
Guido van Rossum3d602e31996-07-21 02:33:56 +00001959 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001960 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001961 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001962 /*
1963 * Only child will be a terminal with a well-defined symbolic name
1964 * or a NAME with a string of either 'is' or 'in'
1965 */
1966 tree = CHILD(tree, 0);
1967 switch (TYPE(tree)) {
1968 case LESS:
1969 case GREATER:
1970 case EQEQUAL:
1971 case EQUAL:
1972 case LESSEQUAL:
1973 case GREATEREQUAL:
1974 case NOTEQUAL:
1975 res = 1;
1976 break;
1977 case NAME:
1978 res = ((strcmp(STR(tree), "in") == 0)
1979 || (strcmp(STR(tree), "is") == 0));
1980 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001981 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00001982 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00001983 }
1984 break;
1985 default:
Fred Drake661ea262000-10-24 19:57:45 +00001986 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001987 break;
1988 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001989 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00001990 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001991 res = (validate_ntype(CHILD(tree, 0), NAME)
1992 && validate_ntype(CHILD(tree, 1), NAME)
1993 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
1994 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
1995 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
1996 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
1997 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001998 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001999 }
2000 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_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002006{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002007 int j;
2008 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002009 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002010 && is_odd(nch)
2011 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002012
Guido van Rossum3d602e31996-07-21 02:33:56 +00002013 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002014 res = (validate_xor_expr(CHILD(tree, j))
2015 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002016
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002017 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002018}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002019
2020
Guido van Rossum47478871996-08-21 14:32:37 +00002021static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002022validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002023{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002024 int j;
2025 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002026 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002027 && is_odd(nch)
2028 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002029
Guido van Rossum3d602e31996-07-21 02:33:56 +00002030 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002031 res = (validate_circumflex(CHILD(tree, j - 1))
2032 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002033
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002034 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002035}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002036
2037
Guido van Rossum47478871996-08-21 14:32:37 +00002038static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002039validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002040{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002041 int pos;
2042 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002043 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002044 && is_odd(nch)
2045 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002046
Guido van Rossum3d602e31996-07-21 02:33:56 +00002047 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002048 res = (validate_ampersand(CHILD(tree, pos))
2049 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002050
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002051 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002052}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002053
2054
2055static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002056validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002057 {
2058 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002059 int nch = NCH(tree);
2060 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002061 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002062
Guido van Rossum3d602e31996-07-21 02:33:56 +00002063 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002064 if (TYPE(CHILD(tree, pos)) != op1)
2065 res = validate_ntype(CHILD(tree, pos), op2);
2066 if (res)
2067 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002068 }
2069 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002070}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002071
2072
Guido van Rossum47478871996-08-21 14:32:37 +00002073static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002074validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002075{
2076 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002077 && validate_chain_two_ops(tree, validate_arith_expr,
2078 LEFTSHIFT, RIGHTSHIFT));
2079}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002080
2081
Guido van Rossum47478871996-08-21 14:32:37 +00002082static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002083validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002084{
2085 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002086 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2087}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002088
2089
Guido van Rossum47478871996-08-21 14:32:37 +00002090static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002091validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002092{
2093 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002094 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002095 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002096 && is_odd(nch)
2097 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002098
Guido van Rossum3d602e31996-07-21 02:33:56 +00002099 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002100 res = (((TYPE(CHILD(tree, pos)) == STAR)
2101 || (TYPE(CHILD(tree, pos)) == SLASH)
2102 || (TYPE(CHILD(tree, pos)) == PERCENT))
2103 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002104
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002105 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002106}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002107
2108
Guido van Rossum3d602e31996-07-21 02:33:56 +00002109/* factor:
2110 *
2111 * factor: ('+'|'-'|'~') factor | power
2112 */
Guido van Rossum47478871996-08-21 14:32:37 +00002113static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002114validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002115{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002116 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002117 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002118 && (((nch == 2)
2119 && ((TYPE(CHILD(tree, 0)) == PLUS)
2120 || (TYPE(CHILD(tree, 0)) == MINUS)
2121 || (TYPE(CHILD(tree, 0)) == TILDE))
2122 && validate_factor(CHILD(tree, 1)))
2123 || ((nch == 1)
2124 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002125 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002126}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002127
2128
Guido van Rossum3d602e31996-07-21 02:33:56 +00002129/* power:
2130 *
2131 * power: atom trailer* ('**' factor)*
2132 */
Guido van Rossum47478871996-08-21 14:32:37 +00002133static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002134validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002135{
2136 int pos = 1;
2137 int nch = NCH(tree);
2138 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002139 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002140
2141 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002142 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002143 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002144 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002145 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002146 return (0);
2147 }
2148 for ( ; res && (pos < (nch - 1)); pos += 2)
2149 res = (validate_doublestar(CHILD(tree, pos))
2150 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002151 }
2152 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002153}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002154
2155
Guido van Rossum47478871996-08-21 14:32:37 +00002156static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002157validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002158{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002159 int pos;
2160 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002161 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002162
Fred Drakecff283c2000-08-21 22:24:43 +00002163 if (res && nch < 1)
2164 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002165 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002166 switch (TYPE(CHILD(tree, 0))) {
2167 case LPAR:
2168 res = ((nch <= 3)
2169 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002170
Fred Drakeff9ea482000-04-19 13:54:15 +00002171 if (res && (nch == 3))
2172 res = validate_testlist(CHILD(tree, 1));
2173 break;
2174 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002175 if (nch == 2)
2176 res = validate_ntype(CHILD(tree, 1), RSQB);
2177 else if (nch == 3)
2178 res = (validate_listmaker(CHILD(tree, 1))
2179 && validate_ntype(CHILD(tree, 2), RSQB));
2180 else {
2181 res = 0;
2182 err_string("illegal list display atom");
2183 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002184 break;
2185 case LBRACE:
2186 res = ((nch <= 3)
2187 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002188
Fred Drakeff9ea482000-04-19 13:54:15 +00002189 if (res && (nch == 3))
2190 res = validate_dictmaker(CHILD(tree, 1));
2191 break;
2192 case BACKQUOTE:
2193 res = ((nch == 3)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002194 && validate_testlist1(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002195 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2196 break;
2197 case NAME:
2198 case NUMBER:
2199 res = (nch == 1);
2200 break;
2201 case STRING:
2202 for (pos = 1; res && (pos < nch); ++pos)
2203 res = validate_ntype(CHILD(tree, pos), STRING);
2204 break;
2205 default:
2206 res = 0;
2207 break;
2208 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002209 }
2210 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002211}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002212
2213
Fred Drake85bf3bb2000-08-23 15:35:26 +00002214/* listmaker:
2215 * test ( list_for | (',' test)* [','] )
2216 */
Fred Drakecff283c2000-08-21 22:24:43 +00002217static int
2218validate_listmaker(node *tree)
2219{
2220 int nch = NCH(tree);
2221 int ok = nch;
2222
2223 if (nch == 0)
2224 err_string("missing child nodes of listmaker");
2225 else
2226 ok = validate_test(CHILD(tree, 0));
2227
2228 /*
2229 * list_iter | (',' test)* [',']
2230 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002231 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2232 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002233 else {
2234 /* (',' test)* [','] */
2235 int i = 1;
2236 while (ok && nch - i >= 2) {
2237 ok = (validate_comma(CHILD(tree, i))
2238 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002239 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002240 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002241 if (ok && i == nch-1)
2242 ok = validate_comma(CHILD(tree, i));
2243 else if (i != nch) {
2244 ok = 0;
2245 err_string("illegal trailing nodes for listmaker");
2246 }
Fred Drakecff283c2000-08-21 22:24:43 +00002247 }
2248 return ok;
2249}
2250
2251
Guido van Rossum3d602e31996-07-21 02:33:56 +00002252/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002253 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002254 *
2255 */
Guido van Rossum47478871996-08-21 14:32:37 +00002256static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002257validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002258{
2259 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002260 && validate_numnodes(tree, 5, "funcdef")
2261 && validate_name(CHILD(tree, 0), "def")
2262 && validate_ntype(CHILD(tree, 1), NAME)
2263 && validate_colon(CHILD(tree, 3))
2264 && validate_parameters(CHILD(tree, 2))
2265 && validate_suite(CHILD(tree, 4)));
2266}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002267
2268
Guido van Rossum47478871996-08-21 14:32:37 +00002269static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002270validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002271{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002272 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002273 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002274 && ((nch == 3) || (nch == 4))
2275 && validate_name(CHILD(tree, 0), "lambda")
2276 && validate_colon(CHILD(tree, nch - 2))
2277 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002278
Guido van Rossum3d602e31996-07-21 02:33:56 +00002279 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002280 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002281 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002282 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002283
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002284 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002285}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002286
2287
Guido van Rossum3d602e31996-07-21 02:33:56 +00002288/* arglist:
2289 *
Fred Drakecff283c2000-08-21 22:24:43 +00002290 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002291 */
Guido van Rossum47478871996-08-21 14:32:37 +00002292static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002293validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002294{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002295 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002296 int i = 0;
2297 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002298
2299 if (nch <= 0)
2300 /* raise the right error from having an invalid number of children */
2301 return validate_numnodes(tree, nch + 1, "arglist");
2302
Fred Drakecff283c2000-08-21 22:24:43 +00002303 while (ok && nch-i >= 2) {
2304 /* skip leading (argument ',') */
2305 ok = (validate_argument(CHILD(tree, i))
2306 && validate_comma(CHILD(tree, i+1)));
2307 if (ok)
2308 i += 2;
2309 else
2310 PyErr_Clear();
2311 }
2312 ok = 1;
2313 if (nch-i > 0) {
2314 /*
2315 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002316 */
Fred Drakecff283c2000-08-21 22:24:43 +00002317 int sym = TYPE(CHILD(tree, i));
2318
2319 if (sym == argument) {
2320 ok = validate_argument(CHILD(tree, i));
2321 if (ok && i+1 != nch) {
2322 err_string("illegal arglist specification"
2323 " (extra stuff on end)");
2324 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002325 }
Fred Drakecff283c2000-08-21 22:24:43 +00002326 }
2327 else if (sym == STAR) {
2328 ok = validate_star(CHILD(tree, i));
2329 if (ok && (nch-i == 2))
2330 ok = validate_test(CHILD(tree, i+1));
2331 else if (ok && (nch-i == 5))
2332 ok = (validate_test(CHILD(tree, i+1))
2333 && validate_comma(CHILD(tree, i+2))
2334 && validate_doublestar(CHILD(tree, i+3))
2335 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002336 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002337 err_string("illegal use of '*' in arglist");
2338 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002339 }
2340 }
Fred Drakecff283c2000-08-21 22:24:43 +00002341 else if (sym == DOUBLESTAR) {
2342 if (nch-i == 2)
2343 ok = (validate_doublestar(CHILD(tree, i))
2344 && validate_test(CHILD(tree, i+1)));
2345 else {
2346 err_string("illegal use of '**' in arglist");
2347 ok = 0;
2348 }
2349 }
2350 else {
2351 err_string("illegal arglist specification");
2352 ok = 0;
2353 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002354 }
2355 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002356}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002357
2358
2359
2360/* argument:
2361 *
2362 * [test '='] test
2363 */
Guido van Rossum47478871996-08-21 14:32:37 +00002364static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002365validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002366{
2367 int nch = NCH(tree);
2368 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002369 && ((nch == 1) || (nch == 3))
2370 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002371
2372 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002373 res = (validate_equal(CHILD(tree, 1))
2374 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002375
2376 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002377}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002378
2379
2380
2381/* trailer:
2382 *
Guido van Rossum47478871996-08-21 14:32:37 +00002383 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002384 */
Guido van Rossum47478871996-08-21 14:32:37 +00002385static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002386validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002387{
2388 int nch = NCH(tree);
2389 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002390
2391 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002392 switch (TYPE(CHILD(tree, 0))) {
2393 case LPAR:
2394 res = validate_rparen(CHILD(tree, nch - 1));
2395 if (res && (nch == 3))
2396 res = validate_arglist(CHILD(tree, 1));
2397 break;
2398 case LSQB:
2399 res = (validate_numnodes(tree, 3, "trailer")
2400 && validate_subscriptlist(CHILD(tree, 1))
2401 && validate_ntype(CHILD(tree, 2), RSQB));
2402 break;
2403 case DOT:
2404 res = (validate_numnodes(tree, 2, "trailer")
2405 && validate_ntype(CHILD(tree, 1), NAME));
2406 break;
2407 default:
2408 res = 0;
2409 break;
2410 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002411 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002412 else {
2413 (void) validate_numnodes(tree, 2, "trailer");
2414 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002415 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002416}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002417
2418
Guido van Rossum47478871996-08-21 14:32:37 +00002419/* subscriptlist:
2420 *
2421 * subscript (',' subscript)* [',']
2422 */
2423static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002424validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002425{
2426 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002427 validate_subscript, "subscriptlist"));
2428}
Guido van Rossum47478871996-08-21 14:32:37 +00002429
2430
Guido van Rossum3d602e31996-07-21 02:33:56 +00002431/* subscript:
2432 *
Guido van Rossum47478871996-08-21 14:32:37 +00002433 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002434 */
Guido van Rossum47478871996-08-21 14:32:37 +00002435static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002436validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002437{
Guido van Rossum47478871996-08-21 14:32:37 +00002438 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002439 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002440 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002441
Guido van Rossum47478871996-08-21 14:32:37 +00002442 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002443 if (!PyErr_Occurred())
2444 err_string("invalid number of arguments for subscript node");
2445 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002446 }
Guido van Rossum47478871996-08-21 14:32:37 +00002447 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002448 /* take care of ('.' '.' '.') possibility */
2449 return (validate_numnodes(tree, 3, "subscript")
2450 && validate_dot(CHILD(tree, 0))
2451 && validate_dot(CHILD(tree, 1))
2452 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002453 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002454 if (TYPE(CHILD(tree, 0)) == test)
2455 res = validate_test(CHILD(tree, 0));
2456 else
2457 res = validate_colon(CHILD(tree, 0));
2458 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002459 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002460 /* Must be [test] ':' [test] [sliceop],
2461 * but at least one of the optional components will
2462 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002463 */
2464 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002465 res = validate_test(CHILD(tree, 0));
2466 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002467 }
2468 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002469 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002470 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002471 int rem = nch - ++offset;
2472 if (rem) {
2473 if (TYPE(CHILD(tree, offset)) == test) {
2474 res = validate_test(CHILD(tree, offset));
2475 ++offset;
2476 --rem;
2477 }
2478 if (res && rem)
2479 res = validate_sliceop(CHILD(tree, offset));
2480 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002481 }
2482 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002483}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002484
2485
Guido van Rossum47478871996-08-21 14:32:37 +00002486static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002487validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002488{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002489 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002490 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002491 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002492 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002493 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002494 }
Guido van Rossum47478871996-08-21 14:32:37 +00002495 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002496 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002497 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002498 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002499
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002500 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002501}
Guido van Rossum47478871996-08-21 14:32:37 +00002502
2503
2504static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002505validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002506{
2507 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002508 validate_expr, "exprlist"));
2509}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002510
2511
Guido van Rossum47478871996-08-21 14:32:37 +00002512static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002513validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002514{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002515 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002516 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002517 && (nch >= 3)
2518 && validate_test(CHILD(tree, 0))
2519 && validate_colon(CHILD(tree, 1))
2520 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002521
Guido van Rossum3d602e31996-07-21 02:33:56 +00002522 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002523 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002524 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002525 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002526
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002527 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002528 int pos = 3;
2529 /* ( ',' test ':' test )* */
2530 while (res && (pos < nch)) {
2531 res = (validate_comma(CHILD(tree, pos))
2532 && validate_test(CHILD(tree, pos + 1))
2533 && validate_colon(CHILD(tree, pos + 2))
2534 && validate_test(CHILD(tree, pos + 3)));
2535 pos += 4;
2536 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002537 }
2538 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002539}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002540
2541
Guido van Rossum47478871996-08-21 14:32:37 +00002542static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002543validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002544{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002545 int pos;
2546 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002547 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002548 && (nch >= 2)
2549 && validate_testlist(CHILD(tree, 0))
2550 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002551
Guido van Rossum3d602e31996-07-21 02:33:56 +00002552 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002553 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002554
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002555 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002556}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002557
2558
Guido van Rossum47478871996-08-21 14:32:37 +00002559static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002560validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002561{
Fred Drakeff9ea482000-04-19 13:54:15 +00002562 int nch = 0; /* num. children on current node */
2563 int res = 1; /* result value */
2564 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002565
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002566 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002567 nch = NCH(tree);
2568 next = 0;
2569 switch (TYPE(tree)) {
2570 /*
2571 * Definition nodes.
2572 */
2573 case funcdef:
2574 res = validate_funcdef(tree);
2575 break;
2576 case classdef:
2577 res = validate_class(tree);
2578 break;
2579 /*
2580 * "Trivial" parse tree nodes.
2581 * (Why did I call these trivial?)
2582 */
2583 case stmt:
2584 res = validate_stmt(tree);
2585 break;
2586 case small_stmt:
2587 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002588 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002589 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2590 */
2591 res = validate_small_stmt(tree);
2592 break;
2593 case flow_stmt:
2594 res = (validate_numnodes(tree, 1, "flow_stmt")
2595 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2596 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002597 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002598 || (TYPE(CHILD(tree, 0)) == return_stmt)
2599 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2600 if (res)
2601 next = CHILD(tree, 0);
2602 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002603 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002604 break;
Fred Drake02126f22001-07-17 02:59:15 +00002605 case yield_stmt:
2606 res = validate_yield_stmt(tree);
2607 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002608 /*
2609 * Compound statements.
2610 */
2611 case simple_stmt:
2612 res = validate_simple_stmt(tree);
2613 break;
2614 case compound_stmt:
2615 res = validate_compound_stmt(tree);
2616 break;
2617 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002618 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002619 */
2620 case expr_stmt:
2621 res = validate_expr_stmt(tree);
2622 break;
2623 case print_stmt:
2624 res = validate_print_stmt(tree);
2625 break;
2626 case del_stmt:
2627 res = validate_del_stmt(tree);
2628 break;
2629 case pass_stmt:
2630 res = (validate_numnodes(tree, 1, "pass")
2631 && validate_name(CHILD(tree, 0), "pass"));
2632 break;
2633 case break_stmt:
2634 res = (validate_numnodes(tree, 1, "break")
2635 && validate_name(CHILD(tree, 0), "break"));
2636 break;
2637 case continue_stmt:
2638 res = (validate_numnodes(tree, 1, "continue")
2639 && validate_name(CHILD(tree, 0), "continue"));
2640 break;
2641 case return_stmt:
2642 res = validate_return_stmt(tree);
2643 break;
2644 case raise_stmt:
2645 res = validate_raise_stmt(tree);
2646 break;
2647 case import_stmt:
2648 res = validate_import_stmt(tree);
2649 break;
2650 case global_stmt:
2651 res = validate_global_stmt(tree);
2652 break;
2653 case exec_stmt:
2654 res = validate_exec_stmt(tree);
2655 break;
2656 case assert_stmt:
2657 res = validate_assert_stmt(tree);
2658 break;
2659 case if_stmt:
2660 res = validate_if(tree);
2661 break;
2662 case while_stmt:
2663 res = validate_while(tree);
2664 break;
2665 case for_stmt:
2666 res = validate_for(tree);
2667 break;
2668 case try_stmt:
2669 res = validate_try(tree);
2670 break;
2671 case suite:
2672 res = validate_suite(tree);
2673 break;
2674 /*
2675 * Expression nodes.
2676 */
2677 case testlist:
2678 res = validate_testlist(tree);
2679 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002680 case testlist1:
2681 res = validate_testlist1(tree);
2682 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002683 case test:
2684 res = validate_test(tree);
2685 break;
2686 case and_test:
2687 res = validate_and_test(tree);
2688 break;
2689 case not_test:
2690 res = validate_not_test(tree);
2691 break;
2692 case comparison:
2693 res = validate_comparison(tree);
2694 break;
2695 case exprlist:
2696 res = validate_exprlist(tree);
2697 break;
2698 case comp_op:
2699 res = validate_comp_op(tree);
2700 break;
2701 case expr:
2702 res = validate_expr(tree);
2703 break;
2704 case xor_expr:
2705 res = validate_xor_expr(tree);
2706 break;
2707 case and_expr:
2708 res = validate_and_expr(tree);
2709 break;
2710 case shift_expr:
2711 res = validate_shift_expr(tree);
2712 break;
2713 case arith_expr:
2714 res = validate_arith_expr(tree);
2715 break;
2716 case term:
2717 res = validate_term(tree);
2718 break;
2719 case factor:
2720 res = validate_factor(tree);
2721 break;
2722 case power:
2723 res = validate_power(tree);
2724 break;
2725 case atom:
2726 res = validate_atom(tree);
2727 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002728
Fred Drakeff9ea482000-04-19 13:54:15 +00002729 default:
2730 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002731 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002732 res = 0;
2733 break;
2734 }
2735 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002736 }
2737 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002738}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002739
2740
Guido van Rossum47478871996-08-21 14:32:37 +00002741static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002742validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002743{
2744 int res = validate_eval_input(tree);
2745
2746 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002747 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002748
2749 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002750}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002751
2752
Guido van Rossum3d602e31996-07-21 02:33:56 +00002753/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002754 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002755 */
Guido van Rossum47478871996-08-21 14:32:37 +00002756static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002757validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002758{
Fred Drakec2683dd2001-07-17 19:32:05 +00002759 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002760 int nch = NCH(tree) - 1;
2761 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002762 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002763
Fred Drakec2683dd2001-07-17 19:32:05 +00002764 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002765 if (TYPE(CHILD(tree, j)) == stmt)
2766 res = validate_stmt(CHILD(tree, j));
2767 else
2768 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002769 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002770 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002771 * user. Hopefully, this won't be needed. If a user reports getting
2772 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002773 */
2774 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002775 err_string("VALIDATION FAILURE: report this to the maintainer!");
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
Fred Drake43f8f9b1998-04-13 16:25:46 +00002781static PyObject*
2782pickle_constructor = NULL;
2783
2784
2785static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002786parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002787{
Fred Drake268397f1998-04-29 14:16:32 +00002788 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002789 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00002790 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002791 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002792
Fred Drakec2683dd2001-07-17 19:32:05 +00002793 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002794 PyObject *newargs;
2795 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002796
Fred Drake2a6875e1999-09-20 22:32:18 +00002797 if ((empty_dict = PyDict_New()) == NULL)
2798 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002799 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002800 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002801 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002802 if (tuple != NULL) {
2803 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2804 Py_DECREF(tuple);
2805 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002806 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002807 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002808 }
2809 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002810 Py_XDECREF(empty_dict);
2811
Fred Drake43f8f9b1998-04-13 16:25:46 +00002812 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002813}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002814
2815
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002816/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00002817 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002818 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002819 * We'd really have to write a wrapper around it all anyway to allow
2820 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002821 */
2822static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00002823 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
2824 "Creates a tuple-tree representation of an ST."},
2825 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
2826 "Creates a list-tree representation of an ST."},
2827 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
2828 "Compiles an ST object into a code object."},
2829 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
2830 "Compiles an ST object into a code object."},
2831 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
2832 "Creates an ST object from an expression."},
2833 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
2834 "Determines if an ST object was created from an expression."},
2835 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
2836 "Determines if an ST object was created from a suite."},
2837 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
2838 "Creates an ST object from a suite."},
2839 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2840 "Creates an ST object from a tree representation."},
2841 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2842 "Creates an ST object from a tree representation."},
2843 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
2844 "Creates a tuple-tree representation of an ST."},
2845 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
2846 "Creates a list-tree representation of an ST."},
2847 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2848 "Creates an ST object from a tree representation."},
2849 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2850 "Creates an ST object from a tree representation."},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002851
Fred Drake43f8f9b1998-04-13 16:25:46 +00002852 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00002853 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Fred Drakec2683dd2001-07-17 19:32:05 +00002854 "Returns the pickle magic to allow ST objects to be pickled."},
Fred Drake43f8f9b1998-04-13 16:25:46 +00002855
Fred Drake268397f1998-04-29 14:16:32 +00002856 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002857 };
2858
2859
Tim Peters6d7c4422000-08-26 07:38:06 +00002860DL_EXPORT(void) initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00002861
Guido van Rossum3886bb61998-12-04 18:50:17 +00002862DL_EXPORT(void)
Thomas Wouters5c669862000-07-24 15:49:08 +00002863initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00002864{
Fred Drake13130bc2001-08-15 16:44:56 +00002865 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00002866
2867 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002868 module = Py_InitModule("parser", parser_functions);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002869
Fred Drake7a15ba51999-09-09 14:21:52 +00002870 if (parser_error == 0)
2871 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002872
2873 if ((parser_error == 0)
Fred Drake13130bc2001-08-15 16:44:56 +00002874 || (PyModule_AddObject(module, "ParserError", parser_error) != 0)) {
Fred Drakec2683dd2001-07-17 19:32:05 +00002875 /* caller will check PyErr_Occurred() */
2876 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002877 }
Fred Drakec2683dd2001-07-17 19:32:05 +00002878 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002879 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00002880 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002881 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002882
Fred Drake13130bc2001-08-15 16:44:56 +00002883 PyModule_AddStringConstant(module, "__copyright__",
2884 parser_copyright_string);
2885 PyModule_AddStringConstant(module, "__doc__",
2886 parser_doc_string);
2887 PyModule_AddStringConstant(module, "__version__",
2888 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002889
Fred Drake78bdb9b2001-07-19 20:17:15 +00002890 /* Register to support pickling.
2891 * If this fails, the import of this module will fail because an
2892 * exception will be raised here; should we clear the exception?
2893 */
Fred Drake13130bc2001-08-15 16:44:56 +00002894 copyreg = PyImport_ImportModule("copy_reg");
2895 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002896 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002897
Fred Drake13130bc2001-08-15 16:44:56 +00002898 func = PyObject_GetAttrString(copyreg, "pickle");
2899 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
2900 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00002901 Py_XINCREF(pickle_constructor);
2902 if ((func != NULL) && (pickle_constructor != NULL)
2903 && (pickler != NULL)) {
2904 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002905
Fred Drakec2683dd2001-07-17 19:32:05 +00002906 res = PyObject_CallFunction(func, "OOO", &PyST_Type, pickler,
2907 pickle_constructor);
Fred Drakeff9ea482000-04-19 13:54:15 +00002908 Py_XDECREF(res);
2909 }
2910 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00002911 Py_XDECREF(pickle_constructor);
2912 Py_XDECREF(pickler);
2913 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002914 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002915}