blob: 203786eb195f513ca1199c4bff0b1212ecbf68f2 [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
Jeremy Hylton938ace62002-07-17 16:30:39 +0000160static void parser_free(PyST_Object *st);
161static int parser_compare(PyST_Object *left, PyST_Object *right);
162static PyObject *parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000163
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000164
Fred Drake268397f1998-04-29 14:16:32 +0000165static
Fred Drakec2683dd2001-07-17 19:32:05 +0000166PyTypeObject PyST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000167 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000168 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000169 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000170 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000171 0, /* tp_itemsize */
172 (destructor)parser_free, /* tp_dealloc */
173 0, /* tp_print */
174 parser_getattr, /* tp_getattr */
175 0, /* tp_setattr */
176 (cmpfunc)parser_compare, /* tp_compare */
177 0, /* tp_repr */
178 0, /* tp_as_number */
179 0, /* tp_as_sequence */
180 0, /* tp_as_mapping */
181 0, /* tp_hash */
182 0, /* tp_call */
183 0, /* tp_str */
184 0, /* tp_getattro */
185 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000186
187 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000188 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000189
Fred Drakeff9ea482000-04-19 13:54:15 +0000190 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000191
192 /* __doc__ */
193 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000194}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000195
196
197static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000198parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000199{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000200 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000201
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000202 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000203 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000204
205 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000206 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000207
208 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000209 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000210
211 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000212 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000213
214 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000215 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000216
217 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000218 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000219
Fred Drakeff9ea482000-04-19 13:54:15 +0000220 if (v != 0)
221 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000222 }
223 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000224}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000225
226
Fred Drakec2683dd2001-07-17 19:32:05 +0000227/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000228 *
229 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
230 * This really just wraps a call to parser_compare_nodes() with some easy
231 * checks and protection code.
232 *
233 */
234static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000235parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000236{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000237 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000238 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000239
240 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000241 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000242
Fred Drakec2683dd2001-07-17 19:32:05 +0000243 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000244}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000245
246
Fred Drakec2683dd2001-07-17 19:32:05 +0000247/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000248 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000249 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000250 * 'wrapper' object that holds a node* and allows it to be passed around in
251 * Python code.
252 *
253 */
254static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000255parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000256{
Fred Drakec2683dd2001-07-17 19:32:05 +0000257 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000258
259 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000260 o->st_node = st;
261 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000262 }
Fred Drake268397f1998-04-29 14:16:32 +0000263 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000264 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000265 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000266 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000267}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000268
269
Fred Drakec2683dd2001-07-17 19:32:05 +0000270/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000271 *
272 * This is called by a del statement that reduces the reference count to 0.
273 *
274 */
275static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000276parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000277{
Fred Drakec2683dd2001-07-17 19:32:05 +0000278 PyNode_Free(st->st_node);
279 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000280}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000281
282
Fred Drakec2683dd2001-07-17 19:32:05 +0000283/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000284 *
285 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000286 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000287 *
288 */
289static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000290parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000291{
Guido van Rossum47478871996-08-21 14:32:37 +0000292 PyObject *line_option = 0;
293 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000294 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000295
Fred Drake7a15ba51999-09-09 14:21:52 +0000296 static char *keywords[] = {"ast", "line_info", NULL};
297
Fred Drake268397f1998-04-29 14:16:32 +0000298 if (self == NULL) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000299 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2tuple", keywords,
300 &PyST_Type, &self, &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000301 }
Fred Drake503d8d61998-04-13 18:45:18 +0000302 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000303 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:totuple", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000304 &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000305 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000306 int lineno = 0;
307 if (line_option != NULL) {
308 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
309 }
310 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000311 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000312 * since it's known to work already.
313 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000314 res = node2tuple(((PyST_Object*)self)->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000315 PyTuple_New, PyTuple_SetItem, lineno);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000316 }
317 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000318}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000319
320
Fred Drakec2683dd2001-07-17 19:32:05 +0000321/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000322 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000323 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000324 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000325 *
326 */
327static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000328parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000329{
Guido van Rossum47478871996-08-21 14:32:37 +0000330 PyObject *line_option = 0;
331 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000332 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000333
Fred Drake7a15ba51999-09-09 14:21:52 +0000334 static char *keywords[] = {"ast", "line_info", NULL};
335
Fred Drake503d8d61998-04-13 18:45:18 +0000336 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000337 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2list", keywords,
338 &PyST_Type, &self, &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000339 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000340 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:tolist", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000341 &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000342 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000343 int lineno = 0;
344 if (line_option != 0) {
345 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
346 }
347 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000348 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000349 * since it's known to work already.
350 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000351 res = node2tuple(self->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000352 PyList_New, PyList_SetItem, lineno);
Guido van Rossum47478871996-08-21 14:32:37 +0000353 }
354 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000355}
Guido van Rossum47478871996-08-21 14:32:37 +0000356
357
Fred Drakec2683dd2001-07-17 19:32:05 +0000358/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000359 *
360 * This function creates code objects from the parse tree represented by
361 * the passed-in data object. An optional file name is passed in as well.
362 *
363 */
364static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000365parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000366{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000367 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000368 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000369 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000370
Fred Drake7a15ba51999-09-09 14:21:52 +0000371 static char *keywords[] = {"ast", "filename", NULL};
372
Fred Drake503d8d61998-04-13 18:45:18 +0000373 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000374 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
375 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000376 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000377 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000378 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000379
380 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000381 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000382
383 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000384}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000385
386
387/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
388 * PyObject* parser_issuite(PyObject* self, PyObject* args)
389 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000390 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000391 * a statement suite, respectively. The return is a Python truth value.
392 *
393 */
394static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000395parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000396{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000397 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000398 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000399
Fred Drake7a15ba51999-09-09 14:21:52 +0000400 static char *keywords[] = {"ast", NULL};
401
Fred Drake503d8d61998-04-13 18:45:18 +0000402 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000403 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000404 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000405 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000406 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000407
408 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000409 /* Check to see if the ST represents an expression or not. */
410 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000411 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000412 }
413 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000414}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000415
416
417static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000418parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000419{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000420 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000421 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000422
Fred Drake7a15ba51999-09-09 14:21:52 +0000423 static char *keywords[] = {"ast", NULL};
424
Fred Drake503d8d61998-04-13 18:45:18 +0000425 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000426 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000427 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000428 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000429 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000430
431 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000432 /* Check to see if the ST represents an expression or not. */
433 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000434 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000435 }
436 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000437}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000438
439
Fred Drake7a15ba51999-09-09 14:21:52 +0000440#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
441
Fred Drake503d8d61998-04-13 18:45:18 +0000442static PyMethodDef
443parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000444 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000445 PyDoc_STR("Compile this ST object into a code object.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000446 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000447 PyDoc_STR("Determines if this ST object was created from an expression.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000448 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000449 PyDoc_STR("Determines if this ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000450 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000451 PyDoc_STR("Creates a list-tree representation of this ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000452 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000453 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Fred Drake503d8d61998-04-13 18:45:18 +0000454
Fred Drake268397f1998-04-29 14:16:32 +0000455 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000456};
457
Fred Drake503d8d61998-04-13 18:45:18 +0000458
459static PyObject*
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000460parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000461{
Fred Drake503d8d61998-04-13 18:45:18 +0000462 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000463}
Fred Drake503d8d61998-04-13 18:45:18 +0000464
465
Guido van Rossum3d602e31996-07-21 02:33:56 +0000466/* err_string(char* message)
467 *
468 * Sets the error string for an exception of type ParserError.
469 *
470 */
471static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000472err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000473{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000474 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000475}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000476
477
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000478/* PyObject* parser_do_parse(PyObject* args, int type)
479 *
480 * Internal function to actually execute the parse and return the result if
481 * successful, or set an exception if not.
482 *
483 */
484static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000485parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000486{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000487 char* string = 0;
488 PyObject* res = 0;
489
Fred Drake7a15ba51999-09-09 14:21:52 +0000490 static char *keywords[] = {"source", NULL};
491
492 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000493 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000494 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000495 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000496
Fred Drakeff9ea482000-04-19 13:54:15 +0000497 if (n != 0)
Fred Drakec2683dd2001-07-17 19:32:05 +0000498 res = parser_newstobject(n, type);
Fred Drakeff9ea482000-04-19 13:54:15 +0000499 else
Fred Drake661ea262000-10-24 19:57:45 +0000500 err_string("could not parse string");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000501 }
502 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000503}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000504
505
506/* PyObject* parser_expr(PyObject* self, PyObject* args)
507 * PyObject* parser_suite(PyObject* self, PyObject* args)
508 *
509 * External interfaces to the parser itself. Which is called determines if
510 * the parser attempts to recognize an expression ('eval' form) or statement
511 * suite ('exec' form). The real work is done by parser_do_parse() above.
512 *
513 */
514static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000515parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000516{
Fred Drake268397f1998-04-29 14:16:32 +0000517 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000518 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000519}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000520
521
522static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000523parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000524{
Fred Drake268397f1998-04-29 14:16:32 +0000525 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000526 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000527}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000528
529
530
Fred Drakec2683dd2001-07-17 19:32:05 +0000531/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000532 * object requires that the input tuple be valid without having to rely on
533 * catching an exception from the compiler. This is done to allow the
534 * compiler itself to remain fast, since most of its input will come from
535 * the parser directly, and therefore be known to be syntactically correct.
536 * This validation is done to ensure that we don't core dump the compile
537 * phase, returning an exception instead.
538 *
539 * Two aspects can be broken out in this code: creating a node tree from
540 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000541 * advantageous to expand the number of ST types to include funcdefs and
542 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000543 * here. They are not necessary, and not quite as useful in a raw form.
544 * For now, let's get expressions and suites working reliably.
545 */
546
547
Jeremy Hylton938ace62002-07-17 16:30:39 +0000548static node* build_node_tree(PyObject *tuple);
549static int validate_expr_tree(node *tree);
550static int validate_file_input(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000551
552
Fred Drakec2683dd2001-07-17 19:32:05 +0000553/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000554 *
555 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000556 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000557 * tuple can be validated. It does this by checking the first code of the
558 * tuple, and, if acceptable, builds the internal representation. If this
559 * step succeeds, the internal representation is validated as fully as
560 * possible with the various validate_*() routines defined below.
561 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000562 * This function must be changed if support is to be added for PyST_FRAGMENT
563 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000564 *
565 */
566static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000567parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000568{
Fred Drake268397f1998-04-29 14:16:32 +0000569 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000570 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000571 PyObject *tuple;
572 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000573
Fred Drake7a15ba51999-09-09 14:21:52 +0000574 static char *keywords[] = {"sequence", NULL};
575
Fred Drakec2683dd2001-07-17 19:32:05 +0000576 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000577 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000578 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000579 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000580 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000581 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000582 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000583 }
584 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000585 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000586 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000587 tree = build_node_tree(tuple);
588 if (tree != 0) {
589 int start_sym = TYPE(tree);
590 if (start_sym == eval_input) {
591 /* Might be an eval form. */
592 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000593 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000594 else
595 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000596 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000597 else if (start_sym == file_input) {
598 /* This looks like an exec form so far. */
599 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000600 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000601 else
602 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000603 }
604 else {
605 /* This is a fragment, at best. */
606 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000607 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000608 }
Guido van Rossum47478871996-08-21 14:32:37 +0000609 }
Guido van Rossum47478871996-08-21 14:32:37 +0000610 /* Make sure we throw an exception on all errors. We should never
611 * get this, but we'd do well to be sure something is done.
612 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000613 if (st == NULL && !PyErr_Occurred())
614 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000615
Fred Drakec2683dd2001-07-17 19:32:05 +0000616 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000617}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000618
619
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000620/* node* build_node_children()
621 *
622 * Iterate across the children of the current non-terminal node and build
623 * their structures. If successful, return the root of this portion of
624 * the tree, otherwise, 0. Any required exception will be specified already,
625 * and no memory will have been deallocated.
626 *
627 */
628static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000629build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000630{
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000631 int len = PyObject_Size(tuple);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000632 int i, err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000633
634 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000635 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000636 PyObject* elem = PySequence_GetItem(tuple, i);
637 int ok = elem != NULL;
638 long type = 0;
639 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000640
Fred Drakeff9ea482000-04-19 13:54:15 +0000641 if (ok)
642 ok = PySequence_Check(elem);
643 if (ok) {
644 PyObject *temp = PySequence_GetItem(elem, 0);
645 if (temp == NULL)
646 ok = 0;
647 else {
648 ok = PyInt_Check(temp);
649 if (ok)
650 type = PyInt_AS_LONG(temp);
651 Py_DECREF(temp);
652 }
653 }
654 if (!ok) {
655 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000656 Py_BuildValue("os", elem,
Fred Drakeff9ea482000-04-19 13:54:15 +0000657 "Illegal node construct."));
658 Py_XDECREF(elem);
659 return (0);
660 }
661 if (ISTERMINAL(type)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000662 int len = PyObject_Size(elem);
663 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000664
Fred Drake0ac9b072000-09-12 21:58:06 +0000665 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000666 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000667 return 0;
668 }
669 temp = PySequence_GetItem(elem, 1);
670 if (temp == NULL)
671 return 0;
672 if (!PyString_Check(temp)) {
673 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000674 "second item in terminal node must be a string,"
675 " found %s",
Fred Drake0ac9b072000-09-12 21:58:06 +0000676 ((PyTypeObject*)PyObject_Type(temp))->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000677 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000678 return 0;
679 }
680 if (len == 3) {
681 PyObject *o = PySequence_GetItem(elem, 2);
682 if (o != NULL) {
683 if (PyInt_Check(o))
684 *line_num = PyInt_AS_LONG(o);
685 else {
686 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000687 "third item in terminal node must be an"
688 " integer, found %s",
Fred Drake0ac9b072000-09-12 21:58:06 +0000689 ((PyTypeObject*)PyObject_Type(temp))->tp_name);
690 Py_DECREF(o);
691 Py_DECREF(temp);
692 return 0;
693 }
694 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000695 }
696 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000697 len = PyString_GET_SIZE(temp) + 1;
698 strn = (char *)PyMem_MALLOC(len);
699 if (strn != NULL)
700 (void) memcpy(strn, PyString_AS_STRING(temp), len);
701 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000702 }
703 else if (!ISNONTERMINAL(type)) {
704 /*
705 * It has to be one or the other; this is an error.
706 * Throw an exception.
707 */
708 PyErr_SetObject(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000709 Py_BuildValue("os", elem, "unknown node type."));
Fred Drakeff9ea482000-04-19 13:54:15 +0000710 Py_XDECREF(elem);
711 return (0);
712 }
Fred Drake8b55b2d2001-12-05 22:10:44 +0000713 err = PyNode_AddChild(root, type, strn, *line_num);
714 if (err == E_NOMEM) {
715 PyMem_DEL(strn);
716 return (node *) PyErr_NoMemory();
717 }
718 if (err == E_OVERFLOW) {
719 PyMem_DEL(strn);
720 PyErr_SetString(PyExc_ValueError,
721 "unsupported number of child nodes");
722 return NULL;
723 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000724
Fred Drakeff9ea482000-04-19 13:54:15 +0000725 if (ISNONTERMINAL(type)) {
726 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000727
Fred Drakeff9ea482000-04-19 13:54:15 +0000728 if (new_child != build_node_children(elem, new_child, line_num)) {
729 Py_XDECREF(elem);
730 return (0);
731 }
732 }
733 else if (type == NEWLINE) { /* It's true: we increment the */
734 ++(*line_num); /* line number *after* the newline! */
735 }
736 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000737 }
738 return (root);
Fred Drakeff9ea482000-04-19 13:54:15 +0000739}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000740
741
742static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000743build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000744{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000745 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000746 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000747 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000748
Guido van Rossum47478871996-08-21 14:32:37 +0000749 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000750 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000751 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000752 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000753 /*
754 * The tuple is simple, but it doesn't start with a start symbol.
755 * Throw an exception now and be done with it.
756 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000757 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000758 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000759 PyErr_SetObject(parser_error, tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000760 }
761 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000762 /*
763 * Not efficient, but that can be handled later.
764 */
765 int line_num = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000766
Fred Drakeff9ea482000-04-19 13:54:15 +0000767 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000768 if (res != NULL) {
769 if (res != build_node_children(tuple, res, &line_num)) {
770 PyNode_Free(res);
771 res = NULL;
772 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000773 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000774 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000775 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000776 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000777 * NONTERMINAL, we can't use it. Not sure the implementation
778 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000779 */
780 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000781 Py_BuildValue("os", tuple,
Fred Drakeff9ea482000-04-19 13:54:15 +0000782 "Illegal component tuple."));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000783
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000784 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000785}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000786
787
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000788/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000789 * Validation routines used within the validation section:
790 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000791static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000792
Fred Drakeff9ea482000-04-19 13:54:15 +0000793#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
794#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
795#define validate_colon(ch) validate_terminal(ch, COLON, ":")
796#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
797#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
798#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
799#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
800#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
801#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
802#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
803#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
804#define validate_star(ch) validate_terminal(ch, STAR, "*")
805#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
806#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
807#define validate_dot(ch) validate_terminal(ch, DOT, ".")
808#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000809
Fred Drake0ac9b072000-09-12 21:58:06 +0000810#define VALIDATER(n) static int validate_##n(node *tree)
811
Fred Drakeff9ea482000-04-19 13:54:15 +0000812VALIDATER(node); VALIDATER(small_stmt);
813VALIDATER(class); VALIDATER(node);
814VALIDATER(parameters); VALIDATER(suite);
815VALIDATER(testlist); VALIDATER(varargslist);
816VALIDATER(fpdef); VALIDATER(fplist);
817VALIDATER(stmt); VALIDATER(simple_stmt);
818VALIDATER(expr_stmt); VALIDATER(power);
819VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000820VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000821VALIDATER(raise_stmt); VALIDATER(import_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000822VALIDATER(global_stmt); VALIDATER(list_if);
823VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000824VALIDATER(exec_stmt); VALIDATER(compound_stmt);
825VALIDATER(while); VALIDATER(for);
826VALIDATER(try); VALIDATER(except_clause);
827VALIDATER(test); VALIDATER(and_test);
828VALIDATER(not_test); VALIDATER(comparison);
829VALIDATER(comp_op); VALIDATER(expr);
830VALIDATER(xor_expr); VALIDATER(and_expr);
831VALIDATER(shift_expr); VALIDATER(arith_expr);
832VALIDATER(term); VALIDATER(factor);
833VALIDATER(atom); VALIDATER(lambdef);
834VALIDATER(trailer); VALIDATER(subscript);
835VALIDATER(subscriptlist); VALIDATER(sliceop);
836VALIDATER(exprlist); VALIDATER(dictmaker);
837VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000838VALIDATER(listmaker); VALIDATER(yield_stmt);
Guido van Rossum2d3b9862002-05-24 15:47:06 +0000839VALIDATER(testlist1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000840
Fred Drake0ac9b072000-09-12 21:58:06 +0000841#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000842
Fred Drakeff9ea482000-04-19 13:54:15 +0000843#define is_even(n) (((n) & 1) == 0)
844#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000845
846
847static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000848validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000849{
Fred Drake0ac9b072000-09-12 21:58:06 +0000850 if (TYPE(n) != t) {
851 PyErr_Format(parser_error, "Expected node type %d, got %d.",
852 t, TYPE(n));
853 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000854 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000855 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000856}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000857
858
Fred Drakee7ab64e2000-04-25 04:14:46 +0000859/* Verifies that the number of child nodes is exactly 'num', raising
860 * an exception if it isn't. The exception message does not indicate
861 * the exact number of nodes, allowing this to be used to raise the
862 * "right" exception when the wrong number of nodes is present in a
863 * specific variant of a statement's syntax. This is commonly used
864 * in that fashion.
865 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000866static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000867validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000868{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000869 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000870 PyErr_Format(parser_error,
871 "Illegal number of children for %s node.", name);
872 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000873 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000874 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000875}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000876
877
878static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000879validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000880{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000881 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000882 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000883
884 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000885 PyErr_Format(parser_error,
886 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000887 }
888 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000889}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000890
891
Guido van Rossum47478871996-08-21 14:32:37 +0000892/* X (',' X) [',']
893 */
894static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000895validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000896 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000897{
898 int nch = NCH(tree);
899 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000900 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000901
902 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000903 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000904 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000905 if (is_even(nch))
906 res = validate_comma(CHILD(tree, --nch));
907 if (res && nch > 1) {
908 int pos = 1;
909 for ( ; res && pos < nch; pos += 2)
910 res = (validate_comma(CHILD(tree, pos))
911 && vfunc(CHILD(tree, pos + 1)));
912 }
Guido van Rossum47478871996-08-21 14:32:37 +0000913 }
914 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000915}
Guido van Rossum47478871996-08-21 14:32:37 +0000916
917
Fred Drakecff283c2000-08-21 22:24:43 +0000918/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000919 *
920 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000921 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000922 */
Guido van Rossum47478871996-08-21 14:32:37 +0000923static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000924validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000925{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000926 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000927 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000928
Guido van Rossum3d602e31996-07-21 02:33:56 +0000929 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000930 res = (validate_name(CHILD(tree, 0), "class")
931 && validate_ntype(CHILD(tree, 1), NAME)
932 && validate_colon(CHILD(tree, nch - 2))
933 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000934 }
935 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000936 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000937 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000938 res = (validate_lparen(CHILD(tree, 2))
939 && validate_testlist(CHILD(tree, 3))
940 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000941 }
942 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000943}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000944
945
Guido van Rossum3d602e31996-07-21 02:33:56 +0000946/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +0000947 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +0000948 */
Guido van Rossum47478871996-08-21 14:32:37 +0000949static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000950validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000951{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000952 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000953 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +0000954 && (nch >= 4)
955 && validate_name(CHILD(tree, 0), "if")
956 && validate_test(CHILD(tree, 1))
957 && validate_colon(CHILD(tree, 2))
958 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000959
960 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000961 /* ... 'else' ':' suite */
962 res = (validate_name(CHILD(tree, nch - 3), "else")
963 && validate_colon(CHILD(tree, nch - 2))
964 && validate_suite(CHILD(tree, nch - 1)));
965 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000966 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000967 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000968 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000969 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +0000970 /* Will catch the case for nch < 4 */
971 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000972 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000973 /* ... ('elif' test ':' suite)+ ... */
974 int j = 4;
975 while ((j < nch) && res) {
976 res = (validate_name(CHILD(tree, j), "elif")
977 && validate_colon(CHILD(tree, j + 2))
978 && validate_test(CHILD(tree, j + 1))
979 && validate_suite(CHILD(tree, j + 3)));
980 j += 4;
981 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000982 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000983 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000984}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000985
986
Guido van Rossum3d602e31996-07-21 02:33:56 +0000987/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +0000988 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +0000989 *
990 */
Guido van Rossum47478871996-08-21 14:32:37 +0000991static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000992validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000993{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000994 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000995 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000996
Guido van Rossum3d602e31996-07-21 02:33:56 +0000997 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000998 res = (validate_lparen(CHILD(tree, 0))
999 && validate_rparen(CHILD(tree, nch - 1)));
1000 if (res && (nch == 3))
1001 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001002 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001003 else {
1004 (void) validate_numnodes(tree, 2, "parameters");
1005 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001006 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001007}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001008
1009
Fred Drakecff283c2000-08-21 22:24:43 +00001010/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001011 *
1012 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001013 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001014 * | NEWLINE INDENT stmt+ DEDENT
1015 */
Guido van Rossum47478871996-08-21 14:32:37 +00001016static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001017validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001018{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001019 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001020 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001021
Guido van Rossum3d602e31996-07-21 02:33:56 +00001022 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001023 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001024 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001025 /* NEWLINE INDENT stmt+ DEDENT */
1026 res = (validate_newline(CHILD(tree, 0))
1027 && validate_indent(CHILD(tree, 1))
1028 && validate_stmt(CHILD(tree, 2))
1029 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001030
Fred Drakeff9ea482000-04-19 13:54:15 +00001031 if (res && (nch > 4)) {
1032 int i = 3;
1033 --nch; /* forget the DEDENT */
1034 for ( ; res && (i < nch); ++i)
1035 res = validate_stmt(CHILD(tree, i));
1036 }
1037 else if (nch < 4)
1038 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001039 }
1040 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001041}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001042
1043
Guido van Rossum47478871996-08-21 14:32:37 +00001044static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001045validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001046{
Guido van Rossum47478871996-08-21 14:32:37 +00001047 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001048 validate_test, "testlist"));
1049}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001050
1051
Guido van Rossum1c917072001-10-15 15:44:05 +00001052static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001053validate_testlist1(node *tree)
1054{
1055 return (validate_repeating_list(tree, testlist1,
1056 validate_test, "testlist1"));
1057}
1058
1059
1060static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001061validate_testlist_safe(node *tree)
1062{
1063 return (validate_repeating_list(tree, testlist_safe,
1064 validate_test, "testlist_safe"));
1065}
1066
1067
Fred Drakecff283c2000-08-21 22:24:43 +00001068/* '*' NAME [',' '**' NAME] | '**' NAME
1069 */
1070static int
1071validate_varargslist_trailer(node *tree, int start)
1072{
1073 int nch = NCH(tree);
1074 int res = 0;
1075 int sym;
1076
1077 if (nch <= start) {
1078 err_string("expected variable argument trailer for varargslist");
1079 return 0;
1080 }
1081 sym = TYPE(CHILD(tree, start));
1082 if (sym == STAR) {
1083 /*
1084 * ('*' NAME [',' '**' NAME]
1085 */
1086 if (nch-start == 2)
1087 res = validate_name(CHILD(tree, start+1), NULL);
1088 else if (nch-start == 5)
1089 res = (validate_name(CHILD(tree, start+1), NULL)
1090 && validate_comma(CHILD(tree, start+2))
1091 && validate_doublestar(CHILD(tree, start+3))
1092 && validate_name(CHILD(tree, start+4), NULL));
1093 }
1094 else if (sym == DOUBLESTAR) {
1095 /*
1096 * '**' NAME
1097 */
1098 if (nch-start == 2)
1099 res = validate_name(CHILD(tree, start+1), NULL);
1100 }
1101 if (!res)
1102 err_string("illegal variable argument trailer for varargslist");
1103 return res;
1104}
1105
1106
1107/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001108 *
1109 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001110 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001111 * ('*' NAME [',' '**' NAME]
1112 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001113 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1114 *
1115 */
Guido van Rossum47478871996-08-21 14:32:37 +00001116static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001117validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001118{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001119 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001120 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001121 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001122
Fred Drakeb6429a22000-12-11 22:08:27 +00001123 if (!res)
1124 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001125 if (nch < 1) {
1126 err_string("varargslist missing child nodes");
1127 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001128 }
Fred Drakecff283c2000-08-21 22:24:43 +00001129 sym = TYPE(CHILD(tree, 0));
1130 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001131 /* whole thing matches:
1132 * '*' NAME [',' '**' NAME] | '**' NAME
1133 */
Fred Drakecff283c2000-08-21 22:24:43 +00001134 res = validate_varargslist_trailer(tree, 0);
1135 else if (sym == fpdef) {
1136 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001137
Fred Drakecff283c2000-08-21 22:24:43 +00001138 sym = TYPE(CHILD(tree, nch-1));
1139 if (sym == NAME) {
1140 /*
1141 * (fpdef ['=' test] ',')+
1142 * ('*' NAME [',' '**' NAME]
1143 * | '**' NAME)
1144 */
1145 /* skip over (fpdef ['=' test] ',')+ */
1146 while (res && (i+2 <= nch)) {
1147 res = validate_fpdef(CHILD(tree, i));
1148 ++i;
1149 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1150 res = (validate_equal(CHILD(tree, i))
1151 && validate_test(CHILD(tree, i+1)));
1152 if (res)
1153 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001154 }
Fred Drakecff283c2000-08-21 22:24:43 +00001155 if (res && i < nch) {
1156 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001157 ++i;
1158 if (res && i < nch
1159 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1160 || TYPE(CHILD(tree, i)) == STAR))
1161 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001162 }
1163 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001164 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1165 * i --^^^
1166 */
Fred Drakecff283c2000-08-21 22:24:43 +00001167 if (res)
1168 res = validate_varargslist_trailer(tree, i);
1169 }
1170 else {
1171 /*
1172 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1173 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001174 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001175 if (sym == COMMA) {
1176 res = validate_comma(CHILD(tree, nch-1));
1177 if (!res)
1178 return 0;
1179 --nch;
1180 }
1181 /*
1182 * fpdef ['=' test] (',' fpdef ['=' test])*
1183 */
1184 res = validate_fpdef(CHILD(tree, 0));
1185 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001186 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1187 res = (validate_equal(CHILD(tree, i))
1188 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001189 i += 2;
1190 }
1191 /*
1192 * ... (',' fpdef ['=' test])*
1193 * i ---^^^
1194 */
1195 while (res && (nch - i) >= 2) {
1196 res = (validate_comma(CHILD(tree, i))
1197 && validate_fpdef(CHILD(tree, i+1)));
1198 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001199 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1200 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001201 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001202 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001203 }
1204 }
1205 if (res && nch - i != 0) {
1206 res = 0;
1207 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001208 }
1209 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001210 }
Fred Drakecff283c2000-08-21 22:24:43 +00001211 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001212}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001213
1214
Fred Drakecff283c2000-08-21 22:24:43 +00001215/* list_iter: list_for | list_if
1216 */
1217static int
1218validate_list_iter(node *tree)
1219{
1220 int res = (validate_ntype(tree, list_iter)
1221 && validate_numnodes(tree, 1, "list_iter"));
1222 if (res && TYPE(CHILD(tree, 0)) == list_for)
1223 res = validate_list_for(CHILD(tree, 0));
1224 else
1225 res = validate_list_if(CHILD(tree, 0));
1226
1227 return res;
1228}
1229
1230/* list_for: 'for' exprlist 'in' testlist [list_iter]
1231 */
1232static int
1233validate_list_for(node *tree)
1234{
1235 int nch = NCH(tree);
1236 int res;
1237
1238 if (nch == 5)
1239 res = validate_list_iter(CHILD(tree, 4));
1240 else
1241 res = validate_numnodes(tree, 4, "list_for");
1242
1243 if (res)
1244 res = (validate_name(CHILD(tree, 0), "for")
1245 && validate_exprlist(CHILD(tree, 1))
1246 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001247 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001248
1249 return res;
1250}
1251
1252/* list_if: 'if' test [list_iter]
1253 */
1254static int
1255validate_list_if(node *tree)
1256{
1257 int nch = NCH(tree);
1258 int res;
1259
1260 if (nch == 3)
1261 res = validate_list_iter(CHILD(tree, 2));
1262 else
1263 res = validate_numnodes(tree, 2, "list_if");
1264
1265 if (res)
1266 res = (validate_name(CHILD(tree, 0), "if")
1267 && validate_test(CHILD(tree, 1)));
1268
1269 return res;
1270}
1271
1272
1273/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001274 *
1275 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001276 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001277 * | '(' fplist ')'
1278 */
Guido van Rossum47478871996-08-21 14:32:37 +00001279static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001280validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001281{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001282 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001283 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001284
Guido van Rossum3d602e31996-07-21 02:33:56 +00001285 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001286 if (nch == 1)
1287 res = validate_ntype(CHILD(tree, 0), NAME);
1288 else if (nch == 3)
1289 res = (validate_lparen(CHILD(tree, 0))
1290 && validate_fplist(CHILD(tree, 1))
1291 && validate_rparen(CHILD(tree, 2)));
1292 else
1293 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001294 }
1295 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001296}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001297
1298
Guido van Rossum47478871996-08-21 14:32:37 +00001299static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001300validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001301{
Guido van Rossum47478871996-08-21 14:32:37 +00001302 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001303 validate_fpdef, "fplist"));
1304}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001305
1306
Guido van Rossum3d602e31996-07-21 02:33:56 +00001307/* simple_stmt | compound_stmt
1308 *
1309 */
Guido van Rossum47478871996-08-21 14:32:37 +00001310static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001311validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001312{
1313 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001314 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001315
Guido van Rossum3d602e31996-07-21 02:33:56 +00001316 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001317 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001318
Fred Drakeff9ea482000-04-19 13:54:15 +00001319 if (TYPE(tree) == simple_stmt)
1320 res = validate_simple_stmt(tree);
1321 else
1322 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001323 }
1324 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001325}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001326
1327
Guido van Rossum3d602e31996-07-21 02:33:56 +00001328/* small_stmt (';' small_stmt)* [';'] NEWLINE
1329 *
1330 */
Guido van Rossum47478871996-08-21 14:32:37 +00001331static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001332validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001333{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001334 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001335 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001336 && (nch >= 2)
1337 && validate_small_stmt(CHILD(tree, 0))
1338 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001339
Guido van Rossum3d602e31996-07-21 02:33:56 +00001340 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001341 res = validate_numnodes(tree, 2, "simple_stmt");
1342 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001343 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001344 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001345 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001346 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001347
Fred Drakeff9ea482000-04-19 13:54:15 +00001348 for (i = 1; res && (i < nch); i += 2)
1349 res = (validate_semi(CHILD(tree, i))
1350 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001351 }
1352 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001353}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001354
1355
Guido van Rossum47478871996-08-21 14:32:37 +00001356static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001357validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001358{
1359 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001360 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001361
Fred Drake0ac9b072000-09-12 21:58:06 +00001362 if (res) {
1363 int ntype = TYPE(CHILD(tree, 0));
1364
1365 if ( (ntype == expr_stmt)
1366 || (ntype == print_stmt)
1367 || (ntype == del_stmt)
1368 || (ntype == pass_stmt)
1369 || (ntype == flow_stmt)
1370 || (ntype == import_stmt)
1371 || (ntype == global_stmt)
1372 || (ntype == assert_stmt)
1373 || (ntype == exec_stmt))
1374 res = validate_node(CHILD(tree, 0));
1375 else {
1376 res = 0;
1377 err_string("illegal small_stmt child type");
1378 }
1379 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001380 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001381 res = 0;
1382 PyErr_Format(parser_error,
1383 "Unrecognized child node of small_stmt: %d.",
1384 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001385 }
1386 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001387}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001388
1389
1390/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001391 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001392 */
Guido van Rossum47478871996-08-21 14:32:37 +00001393static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001394validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001395{
1396 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001397 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001398 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001399
1400 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001401 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001402
1403 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001404 ntype = TYPE(tree);
1405 if ( (ntype == if_stmt)
1406 || (ntype == while_stmt)
1407 || (ntype == for_stmt)
1408 || (ntype == try_stmt)
1409 || (ntype == funcdef)
1410 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001411 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001412 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001413 res = 0;
1414 PyErr_Format(parser_error,
1415 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001416 }
1417 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001418}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001419
1420
Guido van Rossum47478871996-08-21 14:32:37 +00001421static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001422validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001423{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001424 int j;
1425 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001426 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001427 && is_odd(nch)
1428 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001429
Fred Drake28f739a2000-08-25 22:42:40 +00001430 if (res && nch == 3
1431 && TYPE(CHILD(tree, 1)) == augassign) {
1432 res = (validate_numnodes(CHILD(tree, 1), 1, "augassign")
1433 && validate_testlist(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001434
Fred Drake28f739a2000-08-25 22:42:40 +00001435 if (res) {
1436 char *s = STR(CHILD(CHILD(tree, 1), 0));
1437
1438 res = (strcmp(s, "+=") == 0
1439 || strcmp(s, "-=") == 0
1440 || strcmp(s, "*=") == 0
1441 || strcmp(s, "/=") == 0
1442 || strcmp(s, "%=") == 0
1443 || strcmp(s, "&=") == 0
1444 || strcmp(s, "|=") == 0
1445 || strcmp(s, "^=") == 0
1446 || strcmp(s, "<<=") == 0
1447 || strcmp(s, ">>=") == 0
1448 || strcmp(s, "**=") == 0);
1449 if (!res)
1450 err_string("illegal augmmented assignment operator");
1451 }
1452 }
1453 else {
1454 for (j = 1; res && (j < nch); j += 2)
1455 res = (validate_equal(CHILD(tree, j))
1456 && validate_testlist(CHILD(tree, j + 1)));
1457 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001458 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001459}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001460
1461
Guido van Rossum3d602e31996-07-21 02:33:56 +00001462/* print_stmt:
1463 *
Fred Drakecff283c2000-08-21 22:24:43 +00001464 * 'print' ( [ test (',' test)* [','] ]
1465 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001466 */
Guido van Rossum47478871996-08-21 14:32:37 +00001467static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001468validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001469{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001470 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001471 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001472 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001473 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001474
Fred Drakecff283c2000-08-21 22:24:43 +00001475 if (res && nch > 1) {
1476 int sym = TYPE(CHILD(tree, 1));
1477 int i = 1;
1478 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001479
Fred Drakecff283c2000-08-21 22:24:43 +00001480 if (sym == test)
1481 res = validate_test(CHILD(tree, i++));
1482 else {
1483 if (nch < 3)
1484 res = validate_numnodes(tree, 3, "print_stmt");
1485 else {
1486 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1487 && validate_test(CHILD(tree, i+1)));
1488 i += 2;
1489 allow_trailing_comma = 0;
1490 }
1491 }
1492 if (res) {
1493 /* ... (',' test)* [','] */
1494 while (res && i+2 <= nch) {
1495 res = (validate_comma(CHILD(tree, i))
1496 && validate_test(CHILD(tree, i+1)));
1497 allow_trailing_comma = 1;
1498 i += 2;
1499 }
1500 if (res && !allow_trailing_comma)
1501 res = validate_numnodes(tree, i, "print_stmt");
1502 else if (res && i < nch)
1503 res = validate_comma(CHILD(tree, i));
1504 }
1505 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001506 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001507}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001508
1509
Guido van Rossum47478871996-08-21 14:32:37 +00001510static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001511validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001512{
1513 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001514 && validate_name(CHILD(tree, 0), "del")
1515 && validate_exprlist(CHILD(tree, 1)));
1516}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001517
1518
Guido van Rossum47478871996-08-21 14:32:37 +00001519static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001520validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001521{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001522 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001523 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001524 && ((nch == 1) || (nch == 2))
1525 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001526
Guido van Rossum3d602e31996-07-21 02:33:56 +00001527 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001528 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001529
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001530 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001531}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001532
1533
Guido van Rossum47478871996-08-21 14:32:37 +00001534static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001535validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001536{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001537 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001538 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001539 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001540
Guido van Rossum3d602e31996-07-21 02:33:56 +00001541 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001542 res = validate_name(CHILD(tree, 0), "raise");
1543 if (res && (nch >= 2))
1544 res = validate_test(CHILD(tree, 1));
1545 if (res && nch > 2) {
1546 res = (validate_comma(CHILD(tree, 2))
1547 && validate_test(CHILD(tree, 3)));
1548 if (res && (nch > 4))
1549 res = (validate_comma(CHILD(tree, 4))
1550 && validate_test(CHILD(tree, 5)));
1551 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001552 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001553 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001554 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001555 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001556 res = (validate_comma(CHILD(tree, 2))
1557 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001558
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001559 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001560}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001561
1562
Fred Drake02126f22001-07-17 02:59:15 +00001563/* yield_stmt: 'yield' testlist
1564 */
1565static int
1566validate_yield_stmt(node *tree)
1567{
1568 return (validate_ntype(tree, yield_stmt)
1569 && validate_numnodes(tree, 2, "yield_stmt")
1570 && validate_name(CHILD(tree, 0), "yield")
1571 && validate_testlist(CHILD(tree, 1)));
1572}
1573
1574
Fred Drakecff283c2000-08-21 22:24:43 +00001575static int
1576validate_import_as_name(node *tree)
1577{
1578 int nch = NCH(tree);
1579 int ok = validate_ntype(tree, import_as_name);
1580
1581 if (ok) {
1582 if (nch == 1)
1583 ok = validate_name(CHILD(tree, 0), NULL);
1584 else if (nch == 3)
1585 ok = (validate_name(CHILD(tree, 0), NULL)
1586 && validate_name(CHILD(tree, 1), "as")
1587 && validate_name(CHILD(tree, 2), NULL));
1588 else
1589 ok = validate_numnodes(tree, 3, "import_as_name");
1590 }
1591 return ok;
1592}
1593
1594
Fred Drake71137082001-01-07 05:59:59 +00001595/* dotted_name: NAME ("." NAME)*
1596 */
1597static int
1598validate_dotted_name(node *tree)
1599{
1600 int nch = NCH(tree);
1601 int res = (validate_ntype(tree, dotted_name)
1602 && is_odd(nch)
1603 && validate_name(CHILD(tree, 0), NULL));
1604 int i;
1605
1606 for (i = 1; res && (i < nch); i += 2) {
1607 res = (validate_dot(CHILD(tree, i))
1608 && validate_name(CHILD(tree, i+1), NULL));
1609 }
1610 return res;
1611}
1612
1613
Fred Drakecff283c2000-08-21 22:24:43 +00001614/* dotted_as_name: dotted_name [NAME NAME]
1615 */
1616static int
1617validate_dotted_as_name(node *tree)
1618{
1619 int nch = NCH(tree);
1620 int res = validate_ntype(tree, dotted_as_name);
1621
1622 if (res) {
1623 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001624 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001625 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001626 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001627 && validate_name(CHILD(tree, 1), "as")
1628 && validate_name(CHILD(tree, 2), NULL));
1629 else {
1630 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001631 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001632 }
1633 }
1634 return res;
1635}
1636
1637
Guido van Rossum3d602e31996-07-21 02:33:56 +00001638/* import_stmt:
1639 *
Fred Drakecff283c2000-08-21 22:24:43 +00001640 * 'import' dotted_as_name (',' dotted_as_name)*
1641 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001642 */
Guido van Rossum47478871996-08-21 14:32:37 +00001643static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001644validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001645{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001646 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001647 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001648 && (nch >= 2) && is_even(nch)
Fred Drakecff283c2000-08-21 22:24:43 +00001649 && validate_ntype(CHILD(tree, 0), NAME));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001650
1651 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001652 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001653
Fred Drakecff283c2000-08-21 22:24:43 +00001654 res = validate_dotted_as_name(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00001655 for (j = 2; res && (j < nch); j += 2)
1656 res = (validate_comma(CHILD(tree, j))
Fred Drake71137082001-01-07 05:59:59 +00001657 && validate_dotted_as_name(CHILD(tree, j + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001658 }
Fred Drakecff283c2000-08-21 22:24:43 +00001659 else if (res && (res = validate_name(CHILD(tree, 0), "from"))) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001660 res = ((nch >= 4) && is_even(nch)
Fred Drake71137082001-01-07 05:59:59 +00001661 && validate_dotted_name(CHILD(tree, 1))
1662 && validate_name(CHILD(tree, 2), "import"));
Fred Drakeff9ea482000-04-19 13:54:15 +00001663 if (nch == 4) {
Fred Drakecff283c2000-08-21 22:24:43 +00001664 if (TYPE(CHILD(tree, 3)) == import_as_name)
1665 res = validate_import_as_name(CHILD(tree, 3));
1666 else
1667 res = validate_star(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001668 }
1669 else {
Fred Drakecff283c2000-08-21 22:24:43 +00001670 /* 'from' dotted_name 'import' import_as_name
1671 * (',' import_as_name)+
1672 */
Fred Drakeff9ea482000-04-19 13:54:15 +00001673 int j;
Fred Drakecff283c2000-08-21 22:24:43 +00001674 res = validate_import_as_name(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001675 for (j = 4; res && (j < nch); j += 2)
1676 res = (validate_comma(CHILD(tree, j))
Fred Drakecff283c2000-08-21 22:24:43 +00001677 && validate_import_as_name(CHILD(tree, j + 1)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001678 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001679 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001680 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001681 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001682
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001683 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001684}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001685
1686
Guido van Rossum47478871996-08-21 14:32:37 +00001687static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001688validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001689{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001690 int j;
1691 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001692 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001693 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001694
Guido van Rossum3d602e31996-07-21 02:33:56 +00001695 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001696 res = (validate_name(CHILD(tree, 0), "global")
1697 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001698 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001699 res = (validate_comma(CHILD(tree, j))
1700 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001701
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001702 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001703}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001704
1705
Guido van Rossum3d602e31996-07-21 02:33:56 +00001706/* exec_stmt:
1707 *
1708 * 'exec' expr ['in' test [',' test]]
1709 */
Guido van Rossum47478871996-08-21 14:32:37 +00001710static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001711validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001712{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001713 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001714 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001715 && ((nch == 2) || (nch == 4) || (nch == 6))
1716 && validate_name(CHILD(tree, 0), "exec")
1717 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001718
Guido van Rossum3d602e31996-07-21 02:33:56 +00001719 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001720 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001721 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001722 res = (validate_name(CHILD(tree, 2), "in")
1723 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001724 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001725 res = (validate_comma(CHILD(tree, 4))
1726 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001727
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001728 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001729}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001730
1731
Guido van Rossum925e5471997-04-02 05:32:13 +00001732/* assert_stmt:
1733 *
1734 * 'assert' test [',' test]
1735 */
1736static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001737validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001738{
1739 int nch = NCH(tree);
1740 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001741 && ((nch == 2) || (nch == 4))
1742 && (validate_name(CHILD(tree, 0), "__assert__") ||
1743 validate_name(CHILD(tree, 0), "assert"))
1744 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001745
1746 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001747 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001748 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001749 res = (validate_comma(CHILD(tree, 2))
1750 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001751
1752 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001753}
Guido van Rossum925e5471997-04-02 05:32:13 +00001754
1755
Guido van Rossum47478871996-08-21 14:32:37 +00001756static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001757validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001758{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001759 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001760 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001761 && ((nch == 4) || (nch == 7))
1762 && validate_name(CHILD(tree, 0), "while")
1763 && validate_test(CHILD(tree, 1))
1764 && validate_colon(CHILD(tree, 2))
1765 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001766
Guido van Rossum3d602e31996-07-21 02:33:56 +00001767 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001768 res = (validate_name(CHILD(tree, 4), "else")
1769 && validate_colon(CHILD(tree, 5))
1770 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001771
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001772 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001773}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001774
1775
Guido van Rossum47478871996-08-21 14:32:37 +00001776static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001777validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001778{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001779 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001780 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001781 && ((nch == 6) || (nch == 9))
1782 && validate_name(CHILD(tree, 0), "for")
1783 && validate_exprlist(CHILD(tree, 1))
1784 && validate_name(CHILD(tree, 2), "in")
1785 && validate_testlist(CHILD(tree, 3))
1786 && validate_colon(CHILD(tree, 4))
1787 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001788
Guido van Rossum3d602e31996-07-21 02:33:56 +00001789 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001790 res = (validate_name(CHILD(tree, 6), "else")
1791 && validate_colon(CHILD(tree, 7))
1792 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001793
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001794 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001795}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001796
1797
Guido van Rossum3d602e31996-07-21 02:33:56 +00001798/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001799 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001800 * | 'try' ':' suite 'finally' ':' suite
1801 *
1802 */
Guido van Rossum47478871996-08-21 14:32:37 +00001803static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001804validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001805{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001806 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001807 int pos = 3;
1808 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001809 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001810
1811 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001812 res = (validate_name(CHILD(tree, 0), "try")
1813 && validate_colon(CHILD(tree, 1))
1814 && validate_suite(CHILD(tree, 2))
1815 && validate_colon(CHILD(tree, nch - 2))
1816 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001817 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001818 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001819 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1820 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001821
1822 PyErr_Format(parser_error,
1823 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001824 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001825 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001826 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001827 res = (validate_except_clause(CHILD(tree, pos))
1828 && validate_colon(CHILD(tree, pos + 1))
1829 && validate_suite(CHILD(tree, pos + 2)));
1830 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001831 }
1832 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001833 res = validate_ntype(CHILD(tree, pos), NAME);
1834 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1835 res = (validate_numnodes(tree, 6, "try/finally")
1836 && validate_colon(CHILD(tree, 4))
1837 && validate_suite(CHILD(tree, 5)));
1838 else if (res) {
1839 if (nch == (pos + 3)) {
1840 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1841 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1842 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001843 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001844 }
1845 else if (nch == (pos + 6)) {
1846 res = (validate_name(CHILD(tree, pos), "except")
1847 && validate_colon(CHILD(tree, pos + 1))
1848 && validate_suite(CHILD(tree, pos + 2))
1849 && validate_name(CHILD(tree, pos + 3), "else"));
1850 }
1851 else
1852 res = validate_numnodes(tree, pos + 3, "try/except");
1853 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001854 }
1855 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001856}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001857
1858
Guido van Rossum47478871996-08-21 14:32:37 +00001859static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001860validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001861{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001862 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001863 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001864 && ((nch == 1) || (nch == 2) || (nch == 4))
1865 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001866
Guido van Rossum3d602e31996-07-21 02:33:56 +00001867 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001868 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001869 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001870 res = (validate_comma(CHILD(tree, 2))
1871 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001872
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001873 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001874}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001875
1876
Guido van Rossum47478871996-08-21 14:32:37 +00001877static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001878validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001879{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001880 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001881 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001882
Guido van Rossum3d602e31996-07-21 02:33:56 +00001883 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001884 res = ((nch == 1)
1885 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001886 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001887 int pos;
1888 res = validate_and_test(CHILD(tree, 0));
1889 for (pos = 1; res && (pos < nch); pos += 2)
1890 res = (validate_name(CHILD(tree, pos), "or")
1891 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001892 }
1893 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001894}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001895
1896
Guido van Rossum47478871996-08-21 14:32:37 +00001897static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001898validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001899{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001900 int pos;
1901 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001902 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001903 && is_odd(nch)
1904 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001905
Guido van Rossum3d602e31996-07-21 02:33:56 +00001906 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001907 res = (validate_name(CHILD(tree, pos), "and")
1908 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001909
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001910 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001911}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001912
1913
Guido van Rossum47478871996-08-21 14:32:37 +00001914static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001915validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001916{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001917 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001918 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001919
Guido van Rossum3d602e31996-07-21 02:33:56 +00001920 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001921 if (nch == 2)
1922 res = (validate_name(CHILD(tree, 0), "not")
1923 && validate_not_test(CHILD(tree, 1)));
1924 else if (nch == 1)
1925 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001926 }
1927 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001928}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001929
1930
Guido van Rossum47478871996-08-21 14:32:37 +00001931static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001932validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001933{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001934 int pos;
1935 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001936 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001937 && is_odd(nch)
1938 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001939
Guido van Rossum3d602e31996-07-21 02:33:56 +00001940 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001941 res = (validate_comp_op(CHILD(tree, pos))
1942 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001943
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001944 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001945}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001946
1947
Guido van Rossum47478871996-08-21 14:32:37 +00001948static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001949validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001950{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001951 int res = 0;
1952 int nch = NCH(tree);
1953
Guido van Rossum3d602e31996-07-21 02:33:56 +00001954 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001955 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001956 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001957 /*
1958 * Only child will be a terminal with a well-defined symbolic name
1959 * or a NAME with a string of either 'is' or 'in'
1960 */
1961 tree = CHILD(tree, 0);
1962 switch (TYPE(tree)) {
1963 case LESS:
1964 case GREATER:
1965 case EQEQUAL:
1966 case EQUAL:
1967 case LESSEQUAL:
1968 case GREATEREQUAL:
1969 case NOTEQUAL:
1970 res = 1;
1971 break;
1972 case NAME:
1973 res = ((strcmp(STR(tree), "in") == 0)
1974 || (strcmp(STR(tree), "is") == 0));
1975 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001976 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00001977 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00001978 }
1979 break;
1980 default:
Fred Drake661ea262000-10-24 19:57:45 +00001981 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001982 break;
1983 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001984 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00001985 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001986 res = (validate_ntype(CHILD(tree, 0), NAME)
1987 && validate_ntype(CHILD(tree, 1), NAME)
1988 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
1989 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
1990 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
1991 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
1992 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001993 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001994 }
1995 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001996}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001997
1998
Guido van Rossum47478871996-08-21 14:32:37 +00001999static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002000validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002001{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002002 int j;
2003 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002004 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002005 && is_odd(nch)
2006 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002007
Guido van Rossum3d602e31996-07-21 02:33:56 +00002008 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002009 res = (validate_xor_expr(CHILD(tree, j))
2010 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002011
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002012 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002013}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002014
2015
Guido van Rossum47478871996-08-21 14:32:37 +00002016static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002017validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002018{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002019 int j;
2020 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002021 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002022 && is_odd(nch)
2023 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002024
Guido van Rossum3d602e31996-07-21 02:33:56 +00002025 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002026 res = (validate_circumflex(CHILD(tree, j - 1))
2027 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002028
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002029 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002030}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002031
2032
Guido van Rossum47478871996-08-21 14:32:37 +00002033static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002034validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002035{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002036 int pos;
2037 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002038 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002039 && is_odd(nch)
2040 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002041
Guido van Rossum3d602e31996-07-21 02:33:56 +00002042 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002043 res = (validate_ampersand(CHILD(tree, pos))
2044 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002045
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002046 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002047}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002048
2049
2050static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002051validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002052 {
2053 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002054 int nch = NCH(tree);
2055 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002056 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002057
Guido van Rossum3d602e31996-07-21 02:33:56 +00002058 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002059 if (TYPE(CHILD(tree, pos)) != op1)
2060 res = validate_ntype(CHILD(tree, pos), op2);
2061 if (res)
2062 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002063 }
2064 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002065}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002066
2067
Guido van Rossum47478871996-08-21 14:32:37 +00002068static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002069validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002070{
2071 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002072 && validate_chain_two_ops(tree, validate_arith_expr,
2073 LEFTSHIFT, RIGHTSHIFT));
2074}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002075
2076
Guido van Rossum47478871996-08-21 14:32:37 +00002077static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002078validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002079{
2080 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002081 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2082}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002083
2084
Guido van Rossum47478871996-08-21 14:32:37 +00002085static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002086validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002087{
2088 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002089 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002090 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002091 && is_odd(nch)
2092 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002093
Guido van Rossum3d602e31996-07-21 02:33:56 +00002094 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002095 res = (((TYPE(CHILD(tree, pos)) == STAR)
2096 || (TYPE(CHILD(tree, pos)) == SLASH)
2097 || (TYPE(CHILD(tree, pos)) == PERCENT))
2098 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002099
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002100 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002101}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002102
2103
Guido van Rossum3d602e31996-07-21 02:33:56 +00002104/* factor:
2105 *
2106 * factor: ('+'|'-'|'~') factor | power
2107 */
Guido van Rossum47478871996-08-21 14:32:37 +00002108static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002109validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002110{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002111 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002112 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002113 && (((nch == 2)
2114 && ((TYPE(CHILD(tree, 0)) == PLUS)
2115 || (TYPE(CHILD(tree, 0)) == MINUS)
2116 || (TYPE(CHILD(tree, 0)) == TILDE))
2117 && validate_factor(CHILD(tree, 1)))
2118 || ((nch == 1)
2119 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002120 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002121}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002122
2123
Guido van Rossum3d602e31996-07-21 02:33:56 +00002124/* power:
2125 *
2126 * power: atom trailer* ('**' factor)*
2127 */
Guido van Rossum47478871996-08-21 14:32:37 +00002128static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002129validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002130{
2131 int pos = 1;
2132 int nch = NCH(tree);
2133 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002134 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002135
2136 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002137 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002138 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002139 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002140 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002141 return (0);
2142 }
2143 for ( ; res && (pos < (nch - 1)); pos += 2)
2144 res = (validate_doublestar(CHILD(tree, pos))
2145 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002146 }
2147 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002148}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002149
2150
Guido van Rossum47478871996-08-21 14:32:37 +00002151static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002152validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002153{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002154 int pos;
2155 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002156 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002157
Fred Drakecff283c2000-08-21 22:24:43 +00002158 if (res && nch < 1)
2159 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002160 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002161 switch (TYPE(CHILD(tree, 0))) {
2162 case LPAR:
2163 res = ((nch <= 3)
2164 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002165
Fred Drakeff9ea482000-04-19 13:54:15 +00002166 if (res && (nch == 3))
2167 res = validate_testlist(CHILD(tree, 1));
2168 break;
2169 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002170 if (nch == 2)
2171 res = validate_ntype(CHILD(tree, 1), RSQB);
2172 else if (nch == 3)
2173 res = (validate_listmaker(CHILD(tree, 1))
2174 && validate_ntype(CHILD(tree, 2), RSQB));
2175 else {
2176 res = 0;
2177 err_string("illegal list display atom");
2178 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002179 break;
2180 case LBRACE:
2181 res = ((nch <= 3)
2182 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002183
Fred Drakeff9ea482000-04-19 13:54:15 +00002184 if (res && (nch == 3))
2185 res = validate_dictmaker(CHILD(tree, 1));
2186 break;
2187 case BACKQUOTE:
2188 res = ((nch == 3)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002189 && validate_testlist1(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002190 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2191 break;
2192 case NAME:
2193 case NUMBER:
2194 res = (nch == 1);
2195 break;
2196 case STRING:
2197 for (pos = 1; res && (pos < nch); ++pos)
2198 res = validate_ntype(CHILD(tree, pos), STRING);
2199 break;
2200 default:
2201 res = 0;
2202 break;
2203 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002204 }
2205 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002206}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002207
2208
Fred Drake85bf3bb2000-08-23 15:35:26 +00002209/* listmaker:
2210 * test ( list_for | (',' test)* [','] )
2211 */
Fred Drakecff283c2000-08-21 22:24:43 +00002212static int
2213validate_listmaker(node *tree)
2214{
2215 int nch = NCH(tree);
2216 int ok = nch;
2217
2218 if (nch == 0)
2219 err_string("missing child nodes of listmaker");
2220 else
2221 ok = validate_test(CHILD(tree, 0));
2222
2223 /*
2224 * list_iter | (',' test)* [',']
2225 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002226 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2227 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002228 else {
2229 /* (',' test)* [','] */
2230 int i = 1;
2231 while (ok && nch - i >= 2) {
2232 ok = (validate_comma(CHILD(tree, i))
2233 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002234 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002235 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002236 if (ok && i == nch-1)
2237 ok = validate_comma(CHILD(tree, i));
2238 else if (i != nch) {
2239 ok = 0;
2240 err_string("illegal trailing nodes for listmaker");
2241 }
Fred Drakecff283c2000-08-21 22:24:43 +00002242 }
2243 return ok;
2244}
2245
2246
Guido van Rossum3d602e31996-07-21 02:33:56 +00002247/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002248 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002249 *
2250 */
Guido van Rossum47478871996-08-21 14:32:37 +00002251static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002252validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002253{
2254 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002255 && validate_numnodes(tree, 5, "funcdef")
2256 && validate_name(CHILD(tree, 0), "def")
2257 && validate_ntype(CHILD(tree, 1), NAME)
2258 && validate_colon(CHILD(tree, 3))
2259 && validate_parameters(CHILD(tree, 2))
2260 && validate_suite(CHILD(tree, 4)));
2261}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002262
2263
Guido van Rossum47478871996-08-21 14:32:37 +00002264static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002265validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002266{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002267 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002268 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002269 && ((nch == 3) || (nch == 4))
2270 && validate_name(CHILD(tree, 0), "lambda")
2271 && validate_colon(CHILD(tree, nch - 2))
2272 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002273
Guido van Rossum3d602e31996-07-21 02:33:56 +00002274 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002275 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002276 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002277 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002278
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002279 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002280}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002281
2282
Guido van Rossum3d602e31996-07-21 02:33:56 +00002283/* arglist:
2284 *
Fred Drakecff283c2000-08-21 22:24:43 +00002285 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002286 */
Guido van Rossum47478871996-08-21 14:32:37 +00002287static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002288validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002289{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002290 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002291 int i = 0;
2292 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002293
2294 if (nch <= 0)
2295 /* raise the right error from having an invalid number of children */
2296 return validate_numnodes(tree, nch + 1, "arglist");
2297
Fred Drakecff283c2000-08-21 22:24:43 +00002298 while (ok && nch-i >= 2) {
2299 /* skip leading (argument ',') */
2300 ok = (validate_argument(CHILD(tree, i))
2301 && validate_comma(CHILD(tree, i+1)));
2302 if (ok)
2303 i += 2;
2304 else
2305 PyErr_Clear();
2306 }
2307 ok = 1;
2308 if (nch-i > 0) {
2309 /*
2310 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002311 */
Fred Drakecff283c2000-08-21 22:24:43 +00002312 int sym = TYPE(CHILD(tree, i));
2313
2314 if (sym == argument) {
2315 ok = validate_argument(CHILD(tree, i));
2316 if (ok && i+1 != nch) {
2317 err_string("illegal arglist specification"
2318 " (extra stuff on end)");
2319 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002320 }
Fred Drakecff283c2000-08-21 22:24:43 +00002321 }
2322 else if (sym == STAR) {
2323 ok = validate_star(CHILD(tree, i));
2324 if (ok && (nch-i == 2))
2325 ok = validate_test(CHILD(tree, i+1));
2326 else if (ok && (nch-i == 5))
2327 ok = (validate_test(CHILD(tree, i+1))
2328 && validate_comma(CHILD(tree, i+2))
2329 && validate_doublestar(CHILD(tree, i+3))
2330 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002331 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002332 err_string("illegal use of '*' in arglist");
2333 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002334 }
2335 }
Fred Drakecff283c2000-08-21 22:24:43 +00002336 else if (sym == DOUBLESTAR) {
2337 if (nch-i == 2)
2338 ok = (validate_doublestar(CHILD(tree, i))
2339 && validate_test(CHILD(tree, i+1)));
2340 else {
2341 err_string("illegal use of '**' in arglist");
2342 ok = 0;
2343 }
2344 }
2345 else {
2346 err_string("illegal arglist specification");
2347 ok = 0;
2348 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002349 }
2350 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002351}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002352
2353
2354
2355/* argument:
2356 *
2357 * [test '='] test
2358 */
Guido van Rossum47478871996-08-21 14:32:37 +00002359static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002360validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002361{
2362 int nch = NCH(tree);
2363 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002364 && ((nch == 1) || (nch == 3))
2365 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002366
2367 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002368 res = (validate_equal(CHILD(tree, 1))
2369 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002370
2371 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002372}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002373
2374
2375
2376/* trailer:
2377 *
Guido van Rossum47478871996-08-21 14:32:37 +00002378 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002379 */
Guido van Rossum47478871996-08-21 14:32:37 +00002380static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002381validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002382{
2383 int nch = NCH(tree);
2384 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002385
2386 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002387 switch (TYPE(CHILD(tree, 0))) {
2388 case LPAR:
2389 res = validate_rparen(CHILD(tree, nch - 1));
2390 if (res && (nch == 3))
2391 res = validate_arglist(CHILD(tree, 1));
2392 break;
2393 case LSQB:
2394 res = (validate_numnodes(tree, 3, "trailer")
2395 && validate_subscriptlist(CHILD(tree, 1))
2396 && validate_ntype(CHILD(tree, 2), RSQB));
2397 break;
2398 case DOT:
2399 res = (validate_numnodes(tree, 2, "trailer")
2400 && validate_ntype(CHILD(tree, 1), NAME));
2401 break;
2402 default:
2403 res = 0;
2404 break;
2405 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002406 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002407 else {
2408 (void) validate_numnodes(tree, 2, "trailer");
2409 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002410 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002411}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002412
2413
Guido van Rossum47478871996-08-21 14:32:37 +00002414/* subscriptlist:
2415 *
2416 * subscript (',' subscript)* [',']
2417 */
2418static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002419validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002420{
2421 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002422 validate_subscript, "subscriptlist"));
2423}
Guido van Rossum47478871996-08-21 14:32:37 +00002424
2425
Guido van Rossum3d602e31996-07-21 02:33:56 +00002426/* subscript:
2427 *
Guido van Rossum47478871996-08-21 14:32:37 +00002428 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002429 */
Guido van Rossum47478871996-08-21 14:32:37 +00002430static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002431validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002432{
Guido van Rossum47478871996-08-21 14:32:37 +00002433 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002434 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002435 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002436
Guido van Rossum47478871996-08-21 14:32:37 +00002437 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002438 if (!PyErr_Occurred())
2439 err_string("invalid number of arguments for subscript node");
2440 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002441 }
Guido van Rossum47478871996-08-21 14:32:37 +00002442 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002443 /* take care of ('.' '.' '.') possibility */
2444 return (validate_numnodes(tree, 3, "subscript")
2445 && validate_dot(CHILD(tree, 0))
2446 && validate_dot(CHILD(tree, 1))
2447 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002448 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002449 if (TYPE(CHILD(tree, 0)) == test)
2450 res = validate_test(CHILD(tree, 0));
2451 else
2452 res = validate_colon(CHILD(tree, 0));
2453 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002454 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002455 /* Must be [test] ':' [test] [sliceop],
2456 * but at least one of the optional components will
2457 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002458 */
2459 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002460 res = validate_test(CHILD(tree, 0));
2461 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002462 }
2463 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002464 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002465 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002466 int rem = nch - ++offset;
2467 if (rem) {
2468 if (TYPE(CHILD(tree, offset)) == test) {
2469 res = validate_test(CHILD(tree, offset));
2470 ++offset;
2471 --rem;
2472 }
2473 if (res && rem)
2474 res = validate_sliceop(CHILD(tree, offset));
2475 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002476 }
2477 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002478}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002479
2480
Guido van Rossum47478871996-08-21 14:32:37 +00002481static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002482validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002483{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002484 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002485 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002486 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002487 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002488 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002489 }
Guido van Rossum47478871996-08-21 14:32:37 +00002490 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002491 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002492 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002493 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002494
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002495 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002496}
Guido van Rossum47478871996-08-21 14:32:37 +00002497
2498
2499static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002500validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002501{
2502 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002503 validate_expr, "exprlist"));
2504}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002505
2506
Guido van Rossum47478871996-08-21 14:32:37 +00002507static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002508validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002509{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002510 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002511 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002512 && (nch >= 3)
2513 && validate_test(CHILD(tree, 0))
2514 && validate_colon(CHILD(tree, 1))
2515 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002516
Guido van Rossum3d602e31996-07-21 02:33:56 +00002517 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002518 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002519 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002520 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002521
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002522 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002523 int pos = 3;
2524 /* ( ',' test ':' test )* */
2525 while (res && (pos < nch)) {
2526 res = (validate_comma(CHILD(tree, pos))
2527 && validate_test(CHILD(tree, pos + 1))
2528 && validate_colon(CHILD(tree, pos + 2))
2529 && validate_test(CHILD(tree, pos + 3)));
2530 pos += 4;
2531 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002532 }
2533 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002534}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002535
2536
Guido van Rossum47478871996-08-21 14:32:37 +00002537static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002538validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002539{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002540 int pos;
2541 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002542 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002543 && (nch >= 2)
2544 && validate_testlist(CHILD(tree, 0))
2545 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002546
Guido van Rossum3d602e31996-07-21 02:33:56 +00002547 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002548 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002549
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002550 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002551}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002552
2553
Guido van Rossum47478871996-08-21 14:32:37 +00002554static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002555validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002556{
Fred Drakeff9ea482000-04-19 13:54:15 +00002557 int nch = 0; /* num. children on current node */
2558 int res = 1; /* result value */
2559 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002560
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002561 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002562 nch = NCH(tree);
2563 next = 0;
2564 switch (TYPE(tree)) {
2565 /*
2566 * Definition nodes.
2567 */
2568 case funcdef:
2569 res = validate_funcdef(tree);
2570 break;
2571 case classdef:
2572 res = validate_class(tree);
2573 break;
2574 /*
2575 * "Trivial" parse tree nodes.
2576 * (Why did I call these trivial?)
2577 */
2578 case stmt:
2579 res = validate_stmt(tree);
2580 break;
2581 case small_stmt:
2582 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002583 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002584 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2585 */
2586 res = validate_small_stmt(tree);
2587 break;
2588 case flow_stmt:
2589 res = (validate_numnodes(tree, 1, "flow_stmt")
2590 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2591 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002592 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002593 || (TYPE(CHILD(tree, 0)) == return_stmt)
2594 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2595 if (res)
2596 next = CHILD(tree, 0);
2597 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002598 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002599 break;
Fred Drake02126f22001-07-17 02:59:15 +00002600 case yield_stmt:
2601 res = validate_yield_stmt(tree);
2602 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002603 /*
2604 * Compound statements.
2605 */
2606 case simple_stmt:
2607 res = validate_simple_stmt(tree);
2608 break;
2609 case compound_stmt:
2610 res = validate_compound_stmt(tree);
2611 break;
2612 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002613 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002614 */
2615 case expr_stmt:
2616 res = validate_expr_stmt(tree);
2617 break;
2618 case print_stmt:
2619 res = validate_print_stmt(tree);
2620 break;
2621 case del_stmt:
2622 res = validate_del_stmt(tree);
2623 break;
2624 case pass_stmt:
2625 res = (validate_numnodes(tree, 1, "pass")
2626 && validate_name(CHILD(tree, 0), "pass"));
2627 break;
2628 case break_stmt:
2629 res = (validate_numnodes(tree, 1, "break")
2630 && validate_name(CHILD(tree, 0), "break"));
2631 break;
2632 case continue_stmt:
2633 res = (validate_numnodes(tree, 1, "continue")
2634 && validate_name(CHILD(tree, 0), "continue"));
2635 break;
2636 case return_stmt:
2637 res = validate_return_stmt(tree);
2638 break;
2639 case raise_stmt:
2640 res = validate_raise_stmt(tree);
2641 break;
2642 case import_stmt:
2643 res = validate_import_stmt(tree);
2644 break;
2645 case global_stmt:
2646 res = validate_global_stmt(tree);
2647 break;
2648 case exec_stmt:
2649 res = validate_exec_stmt(tree);
2650 break;
2651 case assert_stmt:
2652 res = validate_assert_stmt(tree);
2653 break;
2654 case if_stmt:
2655 res = validate_if(tree);
2656 break;
2657 case while_stmt:
2658 res = validate_while(tree);
2659 break;
2660 case for_stmt:
2661 res = validate_for(tree);
2662 break;
2663 case try_stmt:
2664 res = validate_try(tree);
2665 break;
2666 case suite:
2667 res = validate_suite(tree);
2668 break;
2669 /*
2670 * Expression nodes.
2671 */
2672 case testlist:
2673 res = validate_testlist(tree);
2674 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002675 case testlist1:
2676 res = validate_testlist1(tree);
2677 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002678 case test:
2679 res = validate_test(tree);
2680 break;
2681 case and_test:
2682 res = validate_and_test(tree);
2683 break;
2684 case not_test:
2685 res = validate_not_test(tree);
2686 break;
2687 case comparison:
2688 res = validate_comparison(tree);
2689 break;
2690 case exprlist:
2691 res = validate_exprlist(tree);
2692 break;
2693 case comp_op:
2694 res = validate_comp_op(tree);
2695 break;
2696 case expr:
2697 res = validate_expr(tree);
2698 break;
2699 case xor_expr:
2700 res = validate_xor_expr(tree);
2701 break;
2702 case and_expr:
2703 res = validate_and_expr(tree);
2704 break;
2705 case shift_expr:
2706 res = validate_shift_expr(tree);
2707 break;
2708 case arith_expr:
2709 res = validate_arith_expr(tree);
2710 break;
2711 case term:
2712 res = validate_term(tree);
2713 break;
2714 case factor:
2715 res = validate_factor(tree);
2716 break;
2717 case power:
2718 res = validate_power(tree);
2719 break;
2720 case atom:
2721 res = validate_atom(tree);
2722 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002723
Fred Drakeff9ea482000-04-19 13:54:15 +00002724 default:
2725 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002726 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002727 res = 0;
2728 break;
2729 }
2730 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002731 }
2732 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002733}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002734
2735
Guido van Rossum47478871996-08-21 14:32:37 +00002736static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002737validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002738{
2739 int res = validate_eval_input(tree);
2740
2741 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002742 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002743
2744 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002745}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002746
2747
Guido van Rossum3d602e31996-07-21 02:33:56 +00002748/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002749 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002750 */
Guido van Rossum47478871996-08-21 14:32:37 +00002751static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002752validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002753{
Fred Drakec2683dd2001-07-17 19:32:05 +00002754 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002755 int nch = NCH(tree) - 1;
2756 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002757 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002758
Fred Drakec2683dd2001-07-17 19:32:05 +00002759 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002760 if (TYPE(CHILD(tree, j)) == stmt)
2761 res = validate_stmt(CHILD(tree, j));
2762 else
2763 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002764 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002765 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002766 * user. Hopefully, this won't be needed. If a user reports getting
2767 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002768 */
2769 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002770 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002771
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002772 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002773}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002774
2775
Fred Drake43f8f9b1998-04-13 16:25:46 +00002776static PyObject*
2777pickle_constructor = NULL;
2778
2779
2780static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002781parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002782{
Fred Drake268397f1998-04-29 14:16:32 +00002783 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002784 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00002785 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002786 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002787
Fred Drakec2683dd2001-07-17 19:32:05 +00002788 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002789 PyObject *newargs;
2790 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002791
Fred Drake2a6875e1999-09-20 22:32:18 +00002792 if ((empty_dict = PyDict_New()) == NULL)
2793 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002794 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002795 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002796 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002797 if (tuple != NULL) {
2798 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2799 Py_DECREF(tuple);
2800 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002801 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002802 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002803 }
2804 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002805 Py_XDECREF(empty_dict);
2806
Fred Drake43f8f9b1998-04-13 16:25:46 +00002807 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002808}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002809
2810
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002811/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00002812 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002813 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002814 * We'd really have to write a wrapper around it all anyway to allow
2815 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002816 */
2817static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00002818 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002819 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002820 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002821 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002822 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002823 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002824 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002825 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002826 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002827 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002828 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002829 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002830 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002831 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002832 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002833 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002834 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002835 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002836 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002837 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002838 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002839 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002840 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002841 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002842 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002843 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002844 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002845 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002846
Fred Drake43f8f9b1998-04-13 16:25:46 +00002847 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00002848 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002849 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00002850
Fred Drake268397f1998-04-29 14:16:32 +00002851 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002852 };
2853
2854
Mark Hammond62b1ab12002-07-23 06:31:15 +00002855PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00002856
Mark Hammond62b1ab12002-07-23 06:31:15 +00002857PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00002858initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00002859{
Fred Drake13130bc2001-08-15 16:44:56 +00002860 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00002861
2862 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002863 module = Py_InitModule("parser", parser_functions);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002864
Fred Drake7a15ba51999-09-09 14:21:52 +00002865 if (parser_error == 0)
2866 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002867
2868 if ((parser_error == 0)
Fred Drake13130bc2001-08-15 16:44:56 +00002869 || (PyModule_AddObject(module, "ParserError", parser_error) != 0)) {
Fred Drakec2683dd2001-07-17 19:32:05 +00002870 /* caller will check PyErr_Occurred() */
2871 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002872 }
Fred Drakec2683dd2001-07-17 19:32:05 +00002873 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002874 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00002875 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002876 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002877
Fred Drake13130bc2001-08-15 16:44:56 +00002878 PyModule_AddStringConstant(module, "__copyright__",
2879 parser_copyright_string);
2880 PyModule_AddStringConstant(module, "__doc__",
2881 parser_doc_string);
2882 PyModule_AddStringConstant(module, "__version__",
2883 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002884
Fred Drake78bdb9b2001-07-19 20:17:15 +00002885 /* Register to support pickling.
2886 * If this fails, the import of this module will fail because an
2887 * exception will be raised here; should we clear the exception?
2888 */
Fred Drake13130bc2001-08-15 16:44:56 +00002889 copyreg = PyImport_ImportModule("copy_reg");
2890 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002891 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002892
Fred Drake13130bc2001-08-15 16:44:56 +00002893 func = PyObject_GetAttrString(copyreg, "pickle");
2894 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
2895 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00002896 Py_XINCREF(pickle_constructor);
2897 if ((func != NULL) && (pickle_constructor != NULL)
2898 && (pickler != NULL)) {
2899 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002900
Fred Drakec2683dd2001-07-17 19:32:05 +00002901 res = PyObject_CallFunction(func, "OOO", &PyST_Type, pickler,
2902 pickle_constructor);
Fred Drakeff9ea482000-04-19 13:54:15 +00002903 Py_XDECREF(res);
2904 }
2905 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00002906 Py_XDECREF(pickle_constructor);
2907 Py_XDECREF(pickler);
2908 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002909 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002910}