blob: bf6a360116aad2ffbd8c4fd5ca5a2a8eb5e9fdd2 [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000026 */
27
Fred Drakeff9ea482000-04-19 13:54:15 +000028#include "Python.h" /* general Python API */
29#include "graminit.h" /* symbols defined in the grammar */
30#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000031#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000032#include "token.h" /* token definitions */
33 /* ISTERMINAL() / ISNONTERMINAL() */
34#include "compile.h" /* PyNode_Compile() */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000035
Fred Drake268397f1998-04-29 14:16:32 +000036#ifdef lint
37#include <note.h>
38#else
39#define NOTE(x)
40#endif
41
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000042/* String constants used to initialize module attributes.
43 *
44 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045static char parser_copyright_string[] =
46"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000047University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
48Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
49Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000050
51
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000052PyDoc_STRVAR(parser_doc_string,
53"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000054
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000055static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000056
57
Fred Drakeff9ea482000-04-19 13:54:15 +000058typedef PyObject* (*SeqMaker) (int length);
59typedef int (*SeqInserter) (PyObject* sequence,
60 int index,
61 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000062
Thomas Wouters7e474022000-07-16 12:04:32 +000063/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000064 * original copyright statement is included below, and continues to apply
65 * in full to the function immediately following. All other material is
66 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
67 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000068 * new naming conventions. Added arguments to provide support for creating
69 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000070 */
71
Guido van Rossum52f2c051993-11-10 12:53:24 +000072
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000073static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000074node2tuple(node *n, /* node to convert */
75 SeqMaker mkseq, /* create sequence */
76 SeqInserter addelem, /* func. to add elem. in seq. */
77 int lineno) /* include line numbers? */
Guido van Rossum47478871996-08-21 14:32:37 +000078{
Guido van Rossum3d602e31996-07-21 02:33:56 +000079 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000080 Py_INCREF(Py_None);
81 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000082 }
83 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000084 int i;
85 PyObject *v;
86 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000087
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000088 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000089 if (v == NULL)
90 return (v);
91 w = PyInt_FromLong(TYPE(n));
92 if (w == NULL) {
93 Py_DECREF(v);
94 return ((PyObject*) NULL);
95 }
96 (void) addelem(v, 0, w);
97 for (i = 0; i < NCH(n); i++) {
98 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno);
99 if (w == NULL) {
100 Py_DECREF(v);
101 return ((PyObject*) NULL);
102 }
103 (void) addelem(v, i+1, w);
104 }
Tim Peters6a627252003-07-21 14:25:23 +0000105
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000106 if (TYPE(n) == encoding_decl)
107 (void) addelem(v, i+1, PyString_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000108 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000109 }
110 else if (ISTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000111 PyObject *result = mkseq(2 + lineno);
112 if (result != NULL) {
113 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
114 (void) addelem(result, 1, PyString_FromString(STR(n)));
115 if (lineno == 1)
116 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
117 }
118 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000119 }
120 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000121 PyErr_SetString(PyExc_SystemError,
122 "unrecognized parse tree node type");
123 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000124 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000125}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000126/*
127 * End of material copyrighted by Stichting Mathematisch Centrum.
128 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000129
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000130
131
132/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000133 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000134 * field of the object type to identify which any given object represents.
135 * These should probably go in an external header to allow other extensions
136 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000137 */
138
Fred Drakec2683dd2001-07-17 19:32:05 +0000139#define PyST_EXPR 1
140#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000141
142
143/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000144 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000145 * naming style, but the code uses the new naming convention.
146 */
147
148static PyObject*
149parser_error = 0;
150
151
Fred Drakec2683dd2001-07-17 19:32:05 +0000152typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000153 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000154 node* st_node; /* the node* returned by the parser */
155 int st_type; /* EXPR or SUITE ? */
156} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000157
158
Jeremy Hylton938ace62002-07-17 16:30:39 +0000159static void parser_free(PyST_Object *st);
160static int parser_compare(PyST_Object *left, PyST_Object *right);
161static PyObject *parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000162
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000163
Fred Drake268397f1998-04-29 14:16:32 +0000164static
Fred Drakec2683dd2001-07-17 19:32:05 +0000165PyTypeObject PyST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000166 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000167 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000168 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000169 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000170 0, /* tp_itemsize */
171 (destructor)parser_free, /* tp_dealloc */
172 0, /* tp_print */
173 parser_getattr, /* tp_getattr */
174 0, /* tp_setattr */
175 (cmpfunc)parser_compare, /* tp_compare */
176 0, /* tp_repr */
177 0, /* tp_as_number */
178 0, /* tp_as_sequence */
179 0, /* tp_as_mapping */
180 0, /* tp_hash */
181 0, /* tp_call */
182 0, /* tp_str */
183 0, /* tp_getattro */
184 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000185
186 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000187 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000188
Fred Drakeff9ea482000-04-19 13:54:15 +0000189 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000190
191 /* __doc__ */
192 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000193}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000194
195
196static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000197parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000198{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000199 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000200
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000201 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000202 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000203
204 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000205 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000206
207 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000208 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000209
210 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000211 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000212
213 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000214 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000215
216 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000217 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000218
Fred Drakeff9ea482000-04-19 13:54:15 +0000219 if (v != 0)
220 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000221 }
222 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000223}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000224
225
Fred Drakec2683dd2001-07-17 19:32:05 +0000226/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000227 *
228 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
229 * This really just wraps a call to parser_compare_nodes() with some easy
230 * checks and protection code.
231 *
232 */
233static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000234parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000235{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000236 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000237 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000238
239 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000240 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000241
Fred Drakec2683dd2001-07-17 19:32:05 +0000242 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000243}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000244
245
Fred Drakec2683dd2001-07-17 19:32:05 +0000246/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000248 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000249 * 'wrapper' object that holds a node* and allows it to be passed around in
250 * Python code.
251 *
252 */
253static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000254parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000255{
Fred Drakec2683dd2001-07-17 19:32:05 +0000256 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000257
258 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000259 o->st_node = st;
260 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000261 }
Fred Drake268397f1998-04-29 14:16:32 +0000262 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000263 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000264 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000266}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000267
268
Fred Drakec2683dd2001-07-17 19:32:05 +0000269/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000270 *
271 * This is called by a del statement that reduces the reference count to 0.
272 *
273 */
274static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000275parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000276{
Fred Drakec2683dd2001-07-17 19:32:05 +0000277 PyNode_Free(st->st_node);
278 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000279}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000280
281
Fred Drakec2683dd2001-07-17 19:32:05 +0000282/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000283 *
284 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000285 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000286 *
287 */
288static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000289parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000290{
Guido van Rossum47478871996-08-21 14:32:37 +0000291 PyObject *line_option = 0;
292 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000293 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000294
Fred Drake7a15ba51999-09-09 14:21:52 +0000295 static char *keywords[] = {"ast", "line_info", NULL};
296
Fred Drake268397f1998-04-29 14:16:32 +0000297 if (self == NULL) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000298 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2tuple", keywords,
299 &PyST_Type, &self, &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000300 }
Fred Drake503d8d61998-04-13 18:45:18 +0000301 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000302 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:totuple", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000303 &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000304 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000305 int lineno = 0;
306 if (line_option != NULL) {
307 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
308 }
309 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000310 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000311 * since it's known to work already.
312 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000313 res = node2tuple(((PyST_Object*)self)->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000314 PyTuple_New, PyTuple_SetItem, lineno);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000315 }
316 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000317}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000318
319
Fred Drakec2683dd2001-07-17 19:32:05 +0000320/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000321 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000322 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000323 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000324 *
325 */
326static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000327parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000328{
Guido van Rossum47478871996-08-21 14:32:37 +0000329 PyObject *line_option = 0;
330 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000331 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000332
Fred Drake7a15ba51999-09-09 14:21:52 +0000333 static char *keywords[] = {"ast", "line_info", NULL};
334
Fred Drake503d8d61998-04-13 18:45:18 +0000335 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000336 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2list", keywords,
337 &PyST_Type, &self, &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000338 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000339 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:tolist", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000340 &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000341 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000342 int lineno = 0;
343 if (line_option != 0) {
344 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
345 }
346 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000347 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000348 * since it's known to work already.
349 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000350 res = node2tuple(self->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000351 PyList_New, PyList_SetItem, lineno);
Guido van Rossum47478871996-08-21 14:32:37 +0000352 }
353 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000354}
Guido van Rossum47478871996-08-21 14:32:37 +0000355
356
Fred Drakec2683dd2001-07-17 19:32:05 +0000357/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000358 *
359 * This function creates code objects from the parse tree represented by
360 * the passed-in data object. An optional file name is passed in as well.
361 *
362 */
363static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000364parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000365{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000366 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000367 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000368 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000369
Fred Drake7a15ba51999-09-09 14:21:52 +0000370 static char *keywords[] = {"ast", "filename", NULL};
371
Fred Drake503d8d61998-04-13 18:45:18 +0000372 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000373 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
374 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000375 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000376 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000377 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000378
379 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000380 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000381
382 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000383}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000384
385
386/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
387 * PyObject* parser_issuite(PyObject* self, PyObject* args)
388 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000389 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000390 * a statement suite, respectively. The return is a Python truth value.
391 *
392 */
393static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000394parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000395{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000396 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000397 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000398
Fred Drake7a15ba51999-09-09 14:21:52 +0000399 static char *keywords[] = {"ast", NULL};
400
Fred Drake503d8d61998-04-13 18:45:18 +0000401 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000402 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000403 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000404 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000405 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000406
407 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000408 /* Check to see if the ST represents an expression or not. */
409 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000410 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000411 }
412 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000413}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000414
415
416static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000417parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000418{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000419 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000420 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000421
Fred Drake7a15ba51999-09-09 14:21:52 +0000422 static char *keywords[] = {"ast", NULL};
423
Fred Drake503d8d61998-04-13 18:45:18 +0000424 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000425 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000426 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000427 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000428 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000429
430 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000431 /* Check to see if the ST represents an expression or not. */
432 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000433 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000434 }
435 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000436}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000437
438
Fred Drake7a15ba51999-09-09 14:21:52 +0000439#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
440
Fred Drake503d8d61998-04-13 18:45:18 +0000441static PyMethodDef
442parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000443 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000444 PyDoc_STR("Compile this ST object into a code object.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000445 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000446 PyDoc_STR("Determines if this ST object was created from an expression.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000447 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000448 PyDoc_STR("Determines if this ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000449 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000450 PyDoc_STR("Creates a list-tree representation of this ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000451 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000452 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Fred Drake503d8d61998-04-13 18:45:18 +0000453
Fred Drake268397f1998-04-29 14:16:32 +0000454 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000455};
456
Fred Drake503d8d61998-04-13 18:45:18 +0000457
458static PyObject*
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000459parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000460{
Fred Drake503d8d61998-04-13 18:45:18 +0000461 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000462}
Fred Drake503d8d61998-04-13 18:45:18 +0000463
464
Guido van Rossum3d602e31996-07-21 02:33:56 +0000465/* err_string(char* message)
466 *
467 * Sets the error string for an exception of type ParserError.
468 *
469 */
470static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000471err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000472{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000473 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000474}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000475
476
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000477/* PyObject* parser_do_parse(PyObject* args, int type)
478 *
479 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000480 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000481 *
482 */
483static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000484parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000485{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000486 char* string = 0;
487 PyObject* res = 0;
488
Fred Drake7a15ba51999-09-09 14:21:52 +0000489 static char *keywords[] = {"source", NULL};
490
491 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000492 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000493 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000494 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000495
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000496 if (n)
497 res = parser_newstobject(n, type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000498 }
499 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000500}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000501
502
503/* PyObject* parser_expr(PyObject* self, PyObject* args)
504 * PyObject* parser_suite(PyObject* self, PyObject* args)
505 *
506 * External interfaces to the parser itself. Which is called determines if
507 * the parser attempts to recognize an expression ('eval' form) or statement
508 * suite ('exec' form). The real work is done by parser_do_parse() above.
509 *
510 */
511static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000512parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000513{
Fred Drake268397f1998-04-29 14:16:32 +0000514 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000515 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000516}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000517
518
519static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000520parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000521{
Fred Drake268397f1998-04-29 14:16:32 +0000522 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000523 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000524}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000525
526
527
Fred Drakec2683dd2001-07-17 19:32:05 +0000528/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000529 * object requires that the input tuple be valid without having to rely on
530 * catching an exception from the compiler. This is done to allow the
531 * compiler itself to remain fast, since most of its input will come from
532 * the parser directly, and therefore be known to be syntactically correct.
533 * This validation is done to ensure that we don't core dump the compile
534 * phase, returning an exception instead.
535 *
536 * Two aspects can be broken out in this code: creating a node tree from
537 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000538 * advantageous to expand the number of ST types to include funcdefs and
539 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000540 * here. They are not necessary, and not quite as useful in a raw form.
541 * For now, let's get expressions and suites working reliably.
542 */
543
544
Jeremy Hylton938ace62002-07-17 16:30:39 +0000545static node* build_node_tree(PyObject *tuple);
546static int validate_expr_tree(node *tree);
547static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000548static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000549
Fred Drakec2683dd2001-07-17 19:32:05 +0000550/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000551 *
552 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000553 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000554 * tuple can be validated. It does this by checking the first code of the
555 * tuple, and, if acceptable, builds the internal representation. If this
556 * step succeeds, the internal representation is validated as fully as
557 * possible with the various validate_*() routines defined below.
558 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000559 * This function must be changed if support is to be added for PyST_FRAGMENT
560 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000561 *
562 */
563static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000564parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000565{
Fred Drake268397f1998-04-29 14:16:32 +0000566 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000567 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000568 PyObject *tuple;
569 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000570
Fred Drake7a15ba51999-09-09 14:21:52 +0000571 static char *keywords[] = {"sequence", NULL};
572
Fred Drakec2683dd2001-07-17 19:32:05 +0000573 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000574 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000575 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000576 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000577 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000578 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000579 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000580 }
581 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000582 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000583 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000584 tree = build_node_tree(tuple);
585 if (tree != 0) {
586 int start_sym = TYPE(tree);
587 if (start_sym == eval_input) {
588 /* Might be an eval form. */
589 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000590 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000591 else
592 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000593 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000594 else if (start_sym == file_input) {
595 /* This looks like an exec form so far. */
596 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000597 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000598 else
599 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000600 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000601 else if (start_sym == encoding_decl) {
602 /* This looks like an encoding_decl so far. */
603 if (validate_encoding_decl(tree))
604 st = parser_newstobject(tree, PyST_SUITE);
605 else
606 PyNode_Free(tree);
607 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000608 else {
609 /* This is a fragment, at best. */
610 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000611 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000612 }
Guido van Rossum47478871996-08-21 14:32:37 +0000613 }
Guido van Rossum47478871996-08-21 14:32:37 +0000614 /* Make sure we throw an exception on all errors. We should never
615 * get this, but we'd do well to be sure something is done.
616 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000617 if (st == NULL && !PyErr_Occurred())
618 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000619
Fred Drakec2683dd2001-07-17 19:32:05 +0000620 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000621}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000622
623
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000624/* node* build_node_children()
625 *
626 * Iterate across the children of the current non-terminal node and build
627 * their structures. If successful, return the root of this portion of
628 * the tree, otherwise, 0. Any required exception will be specified already,
629 * and no memory will have been deallocated.
630 *
631 */
632static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000633build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000634{
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000635 int len = PyObject_Size(tuple);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000636 int i, err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000637
638 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000639 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000640 PyObject* elem = PySequence_GetItem(tuple, i);
641 int ok = elem != NULL;
642 long type = 0;
643 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000644
Fred Drakeff9ea482000-04-19 13:54:15 +0000645 if (ok)
646 ok = PySequence_Check(elem);
647 if (ok) {
648 PyObject *temp = PySequence_GetItem(elem, 0);
649 if (temp == NULL)
650 ok = 0;
651 else {
652 ok = PyInt_Check(temp);
653 if (ok)
654 type = PyInt_AS_LONG(temp);
655 Py_DECREF(temp);
656 }
657 }
658 if (!ok) {
659 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000660 Py_BuildValue("os", elem,
Fred Drakeff9ea482000-04-19 13:54:15 +0000661 "Illegal node construct."));
662 Py_XDECREF(elem);
663 return (0);
664 }
665 if (ISTERMINAL(type)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000666 int len = PyObject_Size(elem);
667 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000668
Fred Drake0ac9b072000-09-12 21:58:06 +0000669 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000670 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000671 return 0;
672 }
673 temp = PySequence_GetItem(elem, 1);
674 if (temp == NULL)
675 return 0;
676 if (!PyString_Check(temp)) {
677 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000678 "second item in terminal node must be a string,"
679 " found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000680 temp->ob_type->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000681 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000682 return 0;
683 }
684 if (len == 3) {
685 PyObject *o = PySequence_GetItem(elem, 2);
686 if (o != NULL) {
687 if (PyInt_Check(o))
688 *line_num = PyInt_AS_LONG(o);
689 else {
690 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000691 "third item in terminal node must be an"
692 " integer, found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000693 temp->ob_type->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000694 Py_DECREF(o);
695 Py_DECREF(temp);
696 return 0;
697 }
698 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000699 }
700 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000701 len = PyString_GET_SIZE(temp) + 1;
702 strn = (char *)PyMem_MALLOC(len);
703 if (strn != NULL)
704 (void) memcpy(strn, PyString_AS_STRING(temp), len);
705 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000706 }
707 else if (!ISNONTERMINAL(type)) {
708 /*
709 * It has to be one or the other; this is an error.
710 * Throw an exception.
711 */
712 PyErr_SetObject(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000713 Py_BuildValue("os", elem, "unknown node type."));
Fred Drakeff9ea482000-04-19 13:54:15 +0000714 Py_XDECREF(elem);
715 return (0);
716 }
Fred Drake8b55b2d2001-12-05 22:10:44 +0000717 err = PyNode_AddChild(root, type, strn, *line_num);
718 if (err == E_NOMEM) {
719 PyMem_DEL(strn);
720 return (node *) PyErr_NoMemory();
721 }
722 if (err == E_OVERFLOW) {
723 PyMem_DEL(strn);
724 PyErr_SetString(PyExc_ValueError,
725 "unsupported number of child nodes");
726 return NULL;
727 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000728
Fred Drakeff9ea482000-04-19 13:54:15 +0000729 if (ISNONTERMINAL(type)) {
730 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000731
Fred Drakeff9ea482000-04-19 13:54:15 +0000732 if (new_child != build_node_children(elem, new_child, line_num)) {
733 Py_XDECREF(elem);
734 return (0);
735 }
736 }
737 else if (type == NEWLINE) { /* It's true: we increment the */
738 ++(*line_num); /* line number *after* the newline! */
739 }
740 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000741 }
742 return (root);
Fred Drakeff9ea482000-04-19 13:54:15 +0000743}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000744
745
746static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000747build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000748{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000749 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000750 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000751 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000752
Guido van Rossum47478871996-08-21 14:32:37 +0000753 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000754 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000755 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000756 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000757 /*
758 * The tuple is simple, but it doesn't start with a start symbol.
759 * Throw an exception now and be done with it.
760 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000761 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000762 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000763 PyErr_SetObject(parser_error, tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000764 }
765 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000766 /*
767 * Not efficient, but that can be handled later.
768 */
769 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000770 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000771
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000772 if (num == encoding_decl) {
773 encoding = PySequence_GetItem(tuple, 2);
774 /* tuple isn't borrowed anymore here, need to DECREF */
775 tuple = PySequence_GetSlice(tuple, 0, 2);
776 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000777 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000778 if (res != NULL) {
779 if (res != build_node_children(tuple, res, &line_num)) {
780 PyNode_Free(res);
781 res = NULL;
782 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000783 if (res && encoding) {
784 int len;
785 len = PyString_GET_SIZE(encoding) + 1;
786 res->n_str = (char *)PyMem_MALLOC(len);
787 if (res->n_str != NULL)
788 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
789 Py_DECREF(encoding);
790 Py_DECREF(tuple);
791 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000792 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000793 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000794 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000795 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000796 * NONTERMINAL, we can't use it. Not sure the implementation
797 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000798 */
799 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000800 Py_BuildValue("os", tuple,
Fred Drakeff9ea482000-04-19 13:54:15 +0000801 "Illegal component tuple."));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000802
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000803 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000804}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000805
806
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000807/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000808 * Validation routines used within the validation section:
809 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000810static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000811
Fred Drakeff9ea482000-04-19 13:54:15 +0000812#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
813#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
814#define validate_colon(ch) validate_terminal(ch, COLON, ":")
815#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
816#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
817#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
818#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
819#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
820#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
821#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
822#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
823#define validate_star(ch) validate_terminal(ch, STAR, "*")
824#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
825#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
826#define validate_dot(ch) validate_terminal(ch, DOT, ".")
827#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000828
Fred Drake0ac9b072000-09-12 21:58:06 +0000829#define VALIDATER(n) static int validate_##n(node *tree)
830
Fred Drakeff9ea482000-04-19 13:54:15 +0000831VALIDATER(node); VALIDATER(small_stmt);
832VALIDATER(class); VALIDATER(node);
833VALIDATER(parameters); VALIDATER(suite);
834VALIDATER(testlist); VALIDATER(varargslist);
835VALIDATER(fpdef); VALIDATER(fplist);
836VALIDATER(stmt); VALIDATER(simple_stmt);
837VALIDATER(expr_stmt); VALIDATER(power);
838VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000839VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000840VALIDATER(raise_stmt); VALIDATER(import_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000841VALIDATER(global_stmt); VALIDATER(list_if);
842VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000843VALIDATER(exec_stmt); VALIDATER(compound_stmt);
844VALIDATER(while); VALIDATER(for);
845VALIDATER(try); VALIDATER(except_clause);
846VALIDATER(test); VALIDATER(and_test);
847VALIDATER(not_test); VALIDATER(comparison);
848VALIDATER(comp_op); VALIDATER(expr);
849VALIDATER(xor_expr); VALIDATER(and_expr);
850VALIDATER(shift_expr); VALIDATER(arith_expr);
851VALIDATER(term); VALIDATER(factor);
852VALIDATER(atom); VALIDATER(lambdef);
853VALIDATER(trailer); VALIDATER(subscript);
854VALIDATER(subscriptlist); VALIDATER(sliceop);
855VALIDATER(exprlist); VALIDATER(dictmaker);
856VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000857VALIDATER(listmaker); VALIDATER(yield_stmt);
Guido van Rossum2d3b9862002-05-24 15:47:06 +0000858VALIDATER(testlist1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000859
Fred Drake0ac9b072000-09-12 21:58:06 +0000860#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000861
Fred Drakeff9ea482000-04-19 13:54:15 +0000862#define is_even(n) (((n) & 1) == 0)
863#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000864
865
866static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000867validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000868{
Fred Drake0ac9b072000-09-12 21:58:06 +0000869 if (TYPE(n) != t) {
870 PyErr_Format(parser_error, "Expected node type %d, got %d.",
871 t, TYPE(n));
872 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000873 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000874 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000875}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000876
877
Fred Drakee7ab64e2000-04-25 04:14:46 +0000878/* Verifies that the number of child nodes is exactly 'num', raising
879 * an exception if it isn't. The exception message does not indicate
880 * the exact number of nodes, allowing this to be used to raise the
881 * "right" exception when the wrong number of nodes is present in a
882 * specific variant of a statement's syntax. This is commonly used
883 * in that fashion.
884 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000885static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000886validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000887{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000888 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000889 PyErr_Format(parser_error,
890 "Illegal number of children for %s node.", name);
891 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000892 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000893 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000894}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000895
896
897static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000898validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000899{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000900 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000901 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000902
903 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000904 PyErr_Format(parser_error,
905 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000906 }
907 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000908}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000909
910
Guido van Rossum47478871996-08-21 14:32:37 +0000911/* X (',' X) [',']
912 */
913static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000914validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000915 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000916{
917 int nch = NCH(tree);
918 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000919 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000920
921 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000922 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000923 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000924 if (is_even(nch))
925 res = validate_comma(CHILD(tree, --nch));
926 if (res && nch > 1) {
927 int pos = 1;
928 for ( ; res && pos < nch; pos += 2)
929 res = (validate_comma(CHILD(tree, pos))
930 && vfunc(CHILD(tree, pos + 1)));
931 }
Guido van Rossum47478871996-08-21 14:32:37 +0000932 }
933 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000934}
Guido van Rossum47478871996-08-21 14:32:37 +0000935
936
Fred Drakecff283c2000-08-21 22:24:43 +0000937/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000938 *
939 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000940 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000941 */
Guido van Rossum47478871996-08-21 14:32:37 +0000942static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000943validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000944{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000945 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000946 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000947
Guido van Rossum3d602e31996-07-21 02:33:56 +0000948 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000949 res = (validate_name(CHILD(tree, 0), "class")
950 && validate_ntype(CHILD(tree, 1), NAME)
951 && validate_colon(CHILD(tree, nch - 2))
952 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000953 }
954 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000955 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000956 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000957 res = (validate_lparen(CHILD(tree, 2))
958 && validate_testlist(CHILD(tree, 3))
959 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000960 }
961 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000962}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000963
964
Guido van Rossum3d602e31996-07-21 02:33:56 +0000965/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +0000966 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +0000967 */
Guido van Rossum47478871996-08-21 14:32:37 +0000968static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000969validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000970{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000971 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000972 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +0000973 && (nch >= 4)
974 && validate_name(CHILD(tree, 0), "if")
975 && validate_test(CHILD(tree, 1))
976 && validate_colon(CHILD(tree, 2))
977 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000978
979 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000980 /* ... 'else' ':' suite */
981 res = (validate_name(CHILD(tree, nch - 3), "else")
982 && validate_colon(CHILD(tree, nch - 2))
983 && validate_suite(CHILD(tree, nch - 1)));
984 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000985 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000986 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000987 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000988 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +0000989 /* Will catch the case for nch < 4 */
990 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000991 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000992 /* ... ('elif' test ':' suite)+ ... */
993 int j = 4;
994 while ((j < nch) && res) {
995 res = (validate_name(CHILD(tree, j), "elif")
996 && validate_colon(CHILD(tree, j + 2))
997 && validate_test(CHILD(tree, j + 1))
998 && validate_suite(CHILD(tree, j + 3)));
999 j += 4;
1000 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001001 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001002 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001003}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001004
1005
Guido van Rossum3d602e31996-07-21 02:33:56 +00001006/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001007 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001008 *
1009 */
Guido van Rossum47478871996-08-21 14:32:37 +00001010static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001011validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001012{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001013 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001014 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001015
Guido van Rossum3d602e31996-07-21 02:33:56 +00001016 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001017 res = (validate_lparen(CHILD(tree, 0))
1018 && validate_rparen(CHILD(tree, nch - 1)));
1019 if (res && (nch == 3))
1020 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001021 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001022 else {
1023 (void) validate_numnodes(tree, 2, "parameters");
1024 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001025 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001026}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001027
1028
Fred Drakecff283c2000-08-21 22:24:43 +00001029/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001030 *
1031 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001032 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001033 * | NEWLINE INDENT stmt+ DEDENT
1034 */
Guido van Rossum47478871996-08-21 14:32:37 +00001035static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001036validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001037{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001038 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001039 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001040
Guido van Rossum3d602e31996-07-21 02:33:56 +00001041 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001042 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001043 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001044 /* NEWLINE INDENT stmt+ DEDENT */
1045 res = (validate_newline(CHILD(tree, 0))
1046 && validate_indent(CHILD(tree, 1))
1047 && validate_stmt(CHILD(tree, 2))
1048 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001049
Fred Drakeff9ea482000-04-19 13:54:15 +00001050 if (res && (nch > 4)) {
1051 int i = 3;
1052 --nch; /* forget the DEDENT */
1053 for ( ; res && (i < nch); ++i)
1054 res = validate_stmt(CHILD(tree, i));
1055 }
1056 else if (nch < 4)
1057 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001058 }
1059 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001060}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001061
1062
Guido van Rossum47478871996-08-21 14:32:37 +00001063static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001064validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001065{
Guido van Rossum47478871996-08-21 14:32:37 +00001066 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001067 validate_test, "testlist"));
1068}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001069
1070
Guido van Rossum1c917072001-10-15 15:44:05 +00001071static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001072validate_testlist1(node *tree)
1073{
1074 return (validate_repeating_list(tree, testlist1,
1075 validate_test, "testlist1"));
1076}
1077
1078
1079static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001080validate_testlist_safe(node *tree)
1081{
1082 return (validate_repeating_list(tree, testlist_safe,
1083 validate_test, "testlist_safe"));
1084}
1085
1086
Fred Drakecff283c2000-08-21 22:24:43 +00001087/* '*' NAME [',' '**' NAME] | '**' NAME
1088 */
1089static int
1090validate_varargslist_trailer(node *tree, int start)
1091{
1092 int nch = NCH(tree);
1093 int res = 0;
1094 int sym;
1095
1096 if (nch <= start) {
1097 err_string("expected variable argument trailer for varargslist");
1098 return 0;
1099 }
1100 sym = TYPE(CHILD(tree, start));
1101 if (sym == STAR) {
1102 /*
1103 * ('*' NAME [',' '**' NAME]
1104 */
1105 if (nch-start == 2)
1106 res = validate_name(CHILD(tree, start+1), NULL);
1107 else if (nch-start == 5)
1108 res = (validate_name(CHILD(tree, start+1), NULL)
1109 && validate_comma(CHILD(tree, start+2))
1110 && validate_doublestar(CHILD(tree, start+3))
1111 && validate_name(CHILD(tree, start+4), NULL));
1112 }
1113 else if (sym == DOUBLESTAR) {
1114 /*
1115 * '**' NAME
1116 */
1117 if (nch-start == 2)
1118 res = validate_name(CHILD(tree, start+1), NULL);
1119 }
1120 if (!res)
1121 err_string("illegal variable argument trailer for varargslist");
1122 return res;
1123}
1124
1125
1126/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001127 *
1128 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001129 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001130 * ('*' NAME [',' '**' NAME]
1131 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001132 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1133 *
1134 */
Guido van Rossum47478871996-08-21 14:32:37 +00001135static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001136validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001137{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001138 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001139 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001140 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001141
Fred Drakeb6429a22000-12-11 22:08:27 +00001142 if (!res)
1143 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001144 if (nch < 1) {
1145 err_string("varargslist missing child nodes");
1146 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001147 }
Fred Drakecff283c2000-08-21 22:24:43 +00001148 sym = TYPE(CHILD(tree, 0));
1149 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001150 /* whole thing matches:
1151 * '*' NAME [',' '**' NAME] | '**' NAME
1152 */
Fred Drakecff283c2000-08-21 22:24:43 +00001153 res = validate_varargslist_trailer(tree, 0);
1154 else if (sym == fpdef) {
1155 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001156
Fred Drakecff283c2000-08-21 22:24:43 +00001157 sym = TYPE(CHILD(tree, nch-1));
1158 if (sym == NAME) {
1159 /*
1160 * (fpdef ['=' test] ',')+
1161 * ('*' NAME [',' '**' NAME]
1162 * | '**' NAME)
1163 */
1164 /* skip over (fpdef ['=' test] ',')+ */
1165 while (res && (i+2 <= nch)) {
1166 res = validate_fpdef(CHILD(tree, i));
1167 ++i;
1168 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1169 res = (validate_equal(CHILD(tree, i))
1170 && validate_test(CHILD(tree, i+1)));
1171 if (res)
1172 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001173 }
Fred Drakecff283c2000-08-21 22:24:43 +00001174 if (res && i < nch) {
1175 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001176 ++i;
1177 if (res && i < nch
1178 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1179 || TYPE(CHILD(tree, i)) == STAR))
1180 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001181 }
1182 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001183 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1184 * i --^^^
1185 */
Fred Drakecff283c2000-08-21 22:24:43 +00001186 if (res)
1187 res = validate_varargslist_trailer(tree, i);
1188 }
1189 else {
1190 /*
1191 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1192 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001193 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001194 if (sym == COMMA) {
1195 res = validate_comma(CHILD(tree, nch-1));
1196 if (!res)
1197 return 0;
1198 --nch;
1199 }
1200 /*
1201 * fpdef ['=' test] (',' fpdef ['=' test])*
1202 */
1203 res = validate_fpdef(CHILD(tree, 0));
1204 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001205 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1206 res = (validate_equal(CHILD(tree, i))
1207 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001208 i += 2;
1209 }
1210 /*
1211 * ... (',' fpdef ['=' test])*
1212 * i ---^^^
1213 */
1214 while (res && (nch - i) >= 2) {
1215 res = (validate_comma(CHILD(tree, i))
1216 && validate_fpdef(CHILD(tree, i+1)));
1217 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001218 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1219 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001220 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001221 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001222 }
1223 }
1224 if (res && nch - i != 0) {
1225 res = 0;
1226 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001227 }
1228 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001229 }
Fred Drakecff283c2000-08-21 22:24:43 +00001230 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001231}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001232
1233
Fred Drakecff283c2000-08-21 22:24:43 +00001234/* list_iter: list_for | list_if
1235 */
1236static int
1237validate_list_iter(node *tree)
1238{
1239 int res = (validate_ntype(tree, list_iter)
1240 && validate_numnodes(tree, 1, "list_iter"));
1241 if (res && TYPE(CHILD(tree, 0)) == list_for)
1242 res = validate_list_for(CHILD(tree, 0));
1243 else
1244 res = validate_list_if(CHILD(tree, 0));
1245
1246 return res;
1247}
1248
1249/* list_for: 'for' exprlist 'in' testlist [list_iter]
1250 */
1251static int
1252validate_list_for(node *tree)
1253{
1254 int nch = NCH(tree);
1255 int res;
1256
1257 if (nch == 5)
1258 res = validate_list_iter(CHILD(tree, 4));
1259 else
1260 res = validate_numnodes(tree, 4, "list_for");
1261
1262 if (res)
1263 res = (validate_name(CHILD(tree, 0), "for")
1264 && validate_exprlist(CHILD(tree, 1))
1265 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001266 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001267
1268 return res;
1269}
1270
1271/* list_if: 'if' test [list_iter]
1272 */
1273static int
1274validate_list_if(node *tree)
1275{
1276 int nch = NCH(tree);
1277 int res;
1278
1279 if (nch == 3)
1280 res = validate_list_iter(CHILD(tree, 2));
1281 else
1282 res = validate_numnodes(tree, 2, "list_if");
1283
1284 if (res)
1285 res = (validate_name(CHILD(tree, 0), "if")
1286 && validate_test(CHILD(tree, 1)));
1287
1288 return res;
1289}
1290
1291
1292/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001293 *
1294 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001295 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001296 * | '(' fplist ')'
1297 */
Guido van Rossum47478871996-08-21 14:32:37 +00001298static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001299validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001300{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001301 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001302 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001303
Guido van Rossum3d602e31996-07-21 02:33:56 +00001304 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001305 if (nch == 1)
1306 res = validate_ntype(CHILD(tree, 0), NAME);
1307 else if (nch == 3)
1308 res = (validate_lparen(CHILD(tree, 0))
1309 && validate_fplist(CHILD(tree, 1))
1310 && validate_rparen(CHILD(tree, 2)));
1311 else
1312 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001313 }
1314 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001315}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001316
1317
Guido van Rossum47478871996-08-21 14:32:37 +00001318static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001319validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001320{
Guido van Rossum47478871996-08-21 14:32:37 +00001321 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001322 validate_fpdef, "fplist"));
1323}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001324
1325
Guido van Rossum3d602e31996-07-21 02:33:56 +00001326/* simple_stmt | compound_stmt
1327 *
1328 */
Guido van Rossum47478871996-08-21 14:32:37 +00001329static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001330validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001331{
1332 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001333 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001334
Guido van Rossum3d602e31996-07-21 02:33:56 +00001335 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001336 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001337
Fred Drakeff9ea482000-04-19 13:54:15 +00001338 if (TYPE(tree) == simple_stmt)
1339 res = validate_simple_stmt(tree);
1340 else
1341 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001342 }
1343 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001344}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001345
1346
Guido van Rossum3d602e31996-07-21 02:33:56 +00001347/* small_stmt (';' small_stmt)* [';'] NEWLINE
1348 *
1349 */
Guido van Rossum47478871996-08-21 14:32:37 +00001350static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001351validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001352{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001353 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001354 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001355 && (nch >= 2)
1356 && validate_small_stmt(CHILD(tree, 0))
1357 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001358
Guido van Rossum3d602e31996-07-21 02:33:56 +00001359 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001360 res = validate_numnodes(tree, 2, "simple_stmt");
1361 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001362 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001363 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001364 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001365 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001366
Fred Drakeff9ea482000-04-19 13:54:15 +00001367 for (i = 1; res && (i < nch); i += 2)
1368 res = (validate_semi(CHILD(tree, i))
1369 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001370 }
1371 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001372}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001373
1374
Guido van Rossum47478871996-08-21 14:32:37 +00001375static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001376validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001377{
1378 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001379 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001380
Fred Drake0ac9b072000-09-12 21:58:06 +00001381 if (res) {
1382 int ntype = TYPE(CHILD(tree, 0));
1383
1384 if ( (ntype == expr_stmt)
1385 || (ntype == print_stmt)
1386 || (ntype == del_stmt)
1387 || (ntype == pass_stmt)
1388 || (ntype == flow_stmt)
1389 || (ntype == import_stmt)
1390 || (ntype == global_stmt)
1391 || (ntype == assert_stmt)
1392 || (ntype == exec_stmt))
1393 res = validate_node(CHILD(tree, 0));
1394 else {
1395 res = 0;
1396 err_string("illegal small_stmt child type");
1397 }
1398 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001399 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001400 res = 0;
1401 PyErr_Format(parser_error,
1402 "Unrecognized child node of small_stmt: %d.",
1403 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001404 }
1405 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001406}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001407
1408
1409/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001410 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001411 */
Guido van Rossum47478871996-08-21 14:32:37 +00001412static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001413validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001414{
1415 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001416 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001417 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001418
1419 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001420 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001421
1422 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001423 ntype = TYPE(tree);
1424 if ( (ntype == if_stmt)
1425 || (ntype == while_stmt)
1426 || (ntype == for_stmt)
1427 || (ntype == try_stmt)
1428 || (ntype == funcdef)
1429 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001430 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001431 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001432 res = 0;
1433 PyErr_Format(parser_error,
1434 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001435 }
1436 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001437}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001438
1439
Guido van Rossum47478871996-08-21 14:32:37 +00001440static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001441validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001442{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001443 int j;
1444 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001445 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001446 && is_odd(nch)
1447 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001448
Fred Drake28f739a2000-08-25 22:42:40 +00001449 if (res && nch == 3
1450 && TYPE(CHILD(tree, 1)) == augassign) {
1451 res = (validate_numnodes(CHILD(tree, 1), 1, "augassign")
1452 && validate_testlist(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001453
Fred Drake28f739a2000-08-25 22:42:40 +00001454 if (res) {
1455 char *s = STR(CHILD(CHILD(tree, 1), 0));
1456
1457 res = (strcmp(s, "+=") == 0
1458 || strcmp(s, "-=") == 0
1459 || strcmp(s, "*=") == 0
1460 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001461 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001462 || strcmp(s, "%=") == 0
1463 || strcmp(s, "&=") == 0
1464 || strcmp(s, "|=") == 0
1465 || strcmp(s, "^=") == 0
1466 || strcmp(s, "<<=") == 0
1467 || strcmp(s, ">>=") == 0
1468 || strcmp(s, "**=") == 0);
1469 if (!res)
1470 err_string("illegal augmmented assignment operator");
1471 }
1472 }
1473 else {
1474 for (j = 1; res && (j < nch); j += 2)
1475 res = (validate_equal(CHILD(tree, j))
1476 && validate_testlist(CHILD(tree, j + 1)));
1477 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001478 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001479}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001480
1481
Guido van Rossum3d602e31996-07-21 02:33:56 +00001482/* print_stmt:
1483 *
Fred Drakecff283c2000-08-21 22:24:43 +00001484 * 'print' ( [ test (',' test)* [','] ]
1485 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001486 */
Guido van Rossum47478871996-08-21 14:32:37 +00001487static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001488validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001489{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001490 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001491 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001492 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001493 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001494
Fred Drakecff283c2000-08-21 22:24:43 +00001495 if (res && nch > 1) {
1496 int sym = TYPE(CHILD(tree, 1));
1497 int i = 1;
1498 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001499
Fred Drakecff283c2000-08-21 22:24:43 +00001500 if (sym == test)
1501 res = validate_test(CHILD(tree, i++));
1502 else {
1503 if (nch < 3)
1504 res = validate_numnodes(tree, 3, "print_stmt");
1505 else {
1506 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1507 && validate_test(CHILD(tree, i+1)));
1508 i += 2;
1509 allow_trailing_comma = 0;
1510 }
1511 }
1512 if (res) {
1513 /* ... (',' test)* [','] */
1514 while (res && i+2 <= nch) {
1515 res = (validate_comma(CHILD(tree, i))
1516 && validate_test(CHILD(tree, i+1)));
1517 allow_trailing_comma = 1;
1518 i += 2;
1519 }
1520 if (res && !allow_trailing_comma)
1521 res = validate_numnodes(tree, i, "print_stmt");
1522 else if (res && i < nch)
1523 res = validate_comma(CHILD(tree, i));
1524 }
1525 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001526 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001527}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001528
1529
Guido van Rossum47478871996-08-21 14:32:37 +00001530static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001531validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001532{
1533 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001534 && validate_name(CHILD(tree, 0), "del")
1535 && validate_exprlist(CHILD(tree, 1)));
1536}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001537
1538
Guido van Rossum47478871996-08-21 14:32:37 +00001539static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001540validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001541{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001542 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001543 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001544 && ((nch == 1) || (nch == 2))
1545 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001546
Guido van Rossum3d602e31996-07-21 02:33:56 +00001547 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001548 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001549
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001550 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001551}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001552
1553
Guido van Rossum47478871996-08-21 14:32:37 +00001554static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001555validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001556{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001557 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001558 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001559 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001560
Guido van Rossum3d602e31996-07-21 02:33:56 +00001561 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001562 res = validate_name(CHILD(tree, 0), "raise");
1563 if (res && (nch >= 2))
1564 res = validate_test(CHILD(tree, 1));
1565 if (res && nch > 2) {
1566 res = (validate_comma(CHILD(tree, 2))
1567 && validate_test(CHILD(tree, 3)));
1568 if (res && (nch > 4))
1569 res = (validate_comma(CHILD(tree, 4))
1570 && validate_test(CHILD(tree, 5)));
1571 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001572 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001573 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001574 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001575 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001576 res = (validate_comma(CHILD(tree, 2))
1577 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001578
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001579 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001580}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001581
1582
Fred Drake02126f22001-07-17 02:59:15 +00001583/* yield_stmt: 'yield' testlist
1584 */
1585static int
1586validate_yield_stmt(node *tree)
1587{
1588 return (validate_ntype(tree, yield_stmt)
1589 && validate_numnodes(tree, 2, "yield_stmt")
1590 && validate_name(CHILD(tree, 0), "yield")
1591 && validate_testlist(CHILD(tree, 1)));
1592}
1593
1594
Fred Drakecff283c2000-08-21 22:24:43 +00001595static int
1596validate_import_as_name(node *tree)
1597{
1598 int nch = NCH(tree);
1599 int ok = validate_ntype(tree, import_as_name);
1600
1601 if (ok) {
1602 if (nch == 1)
1603 ok = validate_name(CHILD(tree, 0), NULL);
1604 else if (nch == 3)
1605 ok = (validate_name(CHILD(tree, 0), NULL)
1606 && validate_name(CHILD(tree, 1), "as")
1607 && validate_name(CHILD(tree, 2), NULL));
1608 else
1609 ok = validate_numnodes(tree, 3, "import_as_name");
1610 }
1611 return ok;
1612}
1613
1614
Fred Drake71137082001-01-07 05:59:59 +00001615/* dotted_name: NAME ("." NAME)*
1616 */
1617static int
1618validate_dotted_name(node *tree)
1619{
1620 int nch = NCH(tree);
1621 int res = (validate_ntype(tree, dotted_name)
1622 && is_odd(nch)
1623 && validate_name(CHILD(tree, 0), NULL));
1624 int i;
1625
1626 for (i = 1; res && (i < nch); i += 2) {
1627 res = (validate_dot(CHILD(tree, i))
1628 && validate_name(CHILD(tree, i+1), NULL));
1629 }
1630 return res;
1631}
1632
1633
Fred Drakecff283c2000-08-21 22:24:43 +00001634/* dotted_as_name: dotted_name [NAME NAME]
1635 */
1636static int
1637validate_dotted_as_name(node *tree)
1638{
1639 int nch = NCH(tree);
1640 int res = validate_ntype(tree, dotted_as_name);
1641
1642 if (res) {
1643 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001644 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001645 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001646 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001647 && validate_name(CHILD(tree, 1), "as")
1648 && validate_name(CHILD(tree, 2), NULL));
1649 else {
1650 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001651 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001652 }
1653 }
1654 return res;
1655}
1656
1657
Guido van Rossum3d602e31996-07-21 02:33:56 +00001658/* import_stmt:
1659 *
Fred Drakecff283c2000-08-21 22:24:43 +00001660 * 'import' dotted_as_name (',' dotted_as_name)*
1661 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001662 */
Guido van Rossum47478871996-08-21 14:32:37 +00001663static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001664validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001665{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001666 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001667 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001668 && (nch >= 2) && is_even(nch)
Fred Drakecff283c2000-08-21 22:24:43 +00001669 && validate_ntype(CHILD(tree, 0), NAME));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001670
1671 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001672 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001673
Fred Drakecff283c2000-08-21 22:24:43 +00001674 res = validate_dotted_as_name(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00001675 for (j = 2; res && (j < nch); j += 2)
1676 res = (validate_comma(CHILD(tree, j))
Fred Drake71137082001-01-07 05:59:59 +00001677 && validate_dotted_as_name(CHILD(tree, j + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001678 }
Fred Drakecff283c2000-08-21 22:24:43 +00001679 else if (res && (res = validate_name(CHILD(tree, 0), "from"))) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001680 res = ((nch >= 4) && is_even(nch)
Fred Drake71137082001-01-07 05:59:59 +00001681 && validate_dotted_name(CHILD(tree, 1))
1682 && validate_name(CHILD(tree, 2), "import"));
Fred Drakeff9ea482000-04-19 13:54:15 +00001683 if (nch == 4) {
Fred Drakecff283c2000-08-21 22:24:43 +00001684 if (TYPE(CHILD(tree, 3)) == import_as_name)
1685 res = validate_import_as_name(CHILD(tree, 3));
1686 else
1687 res = validate_star(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001688 }
1689 else {
Fred Drakecff283c2000-08-21 22:24:43 +00001690 /* 'from' dotted_name 'import' import_as_name
1691 * (',' import_as_name)+
1692 */
Fred Drakeff9ea482000-04-19 13:54:15 +00001693 int j;
Fred Drakecff283c2000-08-21 22:24:43 +00001694 res = validate_import_as_name(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001695 for (j = 4; res && (j < nch); j += 2)
1696 res = (validate_comma(CHILD(tree, j))
Fred Drakecff283c2000-08-21 22:24:43 +00001697 && validate_import_as_name(CHILD(tree, j + 1)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001698 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001699 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001700 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001701 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001702
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001703 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001704}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001705
1706
Guido van Rossum47478871996-08-21 14:32:37 +00001707static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001708validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001709{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001710 int j;
1711 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001712 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001713 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001714
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001715 if (!res && !PyErr_Occurred())
1716 err_string("illegal global statement");
1717
Guido van Rossum3d602e31996-07-21 02:33:56 +00001718 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001719 res = (validate_name(CHILD(tree, 0), "global")
1720 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001721 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001722 res = (validate_comma(CHILD(tree, j))
1723 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001724
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001725 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001726}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001727
1728
Guido van Rossum3d602e31996-07-21 02:33:56 +00001729/* exec_stmt:
1730 *
1731 * 'exec' expr ['in' test [',' test]]
1732 */
Guido van Rossum47478871996-08-21 14:32:37 +00001733static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001734validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001735{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001736 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001737 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001738 && ((nch == 2) || (nch == 4) || (nch == 6))
1739 && validate_name(CHILD(tree, 0), "exec")
1740 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001741
Guido van Rossum3d602e31996-07-21 02:33:56 +00001742 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001743 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001744 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001745 res = (validate_name(CHILD(tree, 2), "in")
1746 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001747 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001748 res = (validate_comma(CHILD(tree, 4))
1749 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001750
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001751 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001752}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001753
1754
Guido van Rossum925e5471997-04-02 05:32:13 +00001755/* assert_stmt:
1756 *
1757 * 'assert' test [',' test]
1758 */
1759static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001760validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001761{
1762 int nch = NCH(tree);
1763 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001764 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001765 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001766 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001767
1768 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001769 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001770 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001771 res = (validate_comma(CHILD(tree, 2))
1772 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001773
1774 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001775}
Guido van Rossum925e5471997-04-02 05:32:13 +00001776
1777
Guido van Rossum47478871996-08-21 14:32:37 +00001778static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001779validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001780{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001781 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001782 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001783 && ((nch == 4) || (nch == 7))
1784 && validate_name(CHILD(tree, 0), "while")
1785 && validate_test(CHILD(tree, 1))
1786 && validate_colon(CHILD(tree, 2))
1787 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001788
Guido van Rossum3d602e31996-07-21 02:33:56 +00001789 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001790 res = (validate_name(CHILD(tree, 4), "else")
1791 && validate_colon(CHILD(tree, 5))
1792 && validate_suite(CHILD(tree, 6)));
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 Rossum47478871996-08-21 14:32:37 +00001798static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001799validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001800{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001801 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001802 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001803 && ((nch == 6) || (nch == 9))
1804 && validate_name(CHILD(tree, 0), "for")
1805 && validate_exprlist(CHILD(tree, 1))
1806 && validate_name(CHILD(tree, 2), "in")
1807 && validate_testlist(CHILD(tree, 3))
1808 && validate_colon(CHILD(tree, 4))
1809 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001810
Guido van Rossum3d602e31996-07-21 02:33:56 +00001811 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001812 res = (validate_name(CHILD(tree, 6), "else")
1813 && validate_colon(CHILD(tree, 7))
1814 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001815
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001816 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001817}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001818
1819
Guido van Rossum3d602e31996-07-21 02:33:56 +00001820/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001821 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001822 * | 'try' ':' suite 'finally' ':' suite
1823 *
1824 */
Guido van Rossum47478871996-08-21 14:32:37 +00001825static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001826validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001827{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001828 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001829 int pos = 3;
1830 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001831 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001832
1833 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001834 res = (validate_name(CHILD(tree, 0), "try")
1835 && validate_colon(CHILD(tree, 1))
1836 && validate_suite(CHILD(tree, 2))
1837 && validate_colon(CHILD(tree, nch - 2))
1838 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001839 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001840 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001841 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1842 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001843
1844 PyErr_Format(parser_error,
1845 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001846 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001847 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001848 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001849 res = (validate_except_clause(CHILD(tree, pos))
1850 && validate_colon(CHILD(tree, pos + 1))
1851 && validate_suite(CHILD(tree, pos + 2)));
1852 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001853 }
1854 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001855 res = validate_ntype(CHILD(tree, pos), NAME);
1856 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1857 res = (validate_numnodes(tree, 6, "try/finally")
1858 && validate_colon(CHILD(tree, 4))
1859 && validate_suite(CHILD(tree, 5)));
1860 else if (res) {
1861 if (nch == (pos + 3)) {
1862 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1863 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1864 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001865 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001866 }
1867 else if (nch == (pos + 6)) {
1868 res = (validate_name(CHILD(tree, pos), "except")
1869 && validate_colon(CHILD(tree, pos + 1))
1870 && validate_suite(CHILD(tree, pos + 2))
1871 && validate_name(CHILD(tree, pos + 3), "else"));
1872 }
1873 else
1874 res = validate_numnodes(tree, pos + 3, "try/except");
1875 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001876 }
1877 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001878}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001879
1880
Guido van Rossum47478871996-08-21 14:32:37 +00001881static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001882validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001883{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001884 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001885 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001886 && ((nch == 1) || (nch == 2) || (nch == 4))
1887 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001888
Guido van Rossum3d602e31996-07-21 02:33:56 +00001889 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001890 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001891 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001892 res = (validate_comma(CHILD(tree, 2))
1893 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001894
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001895 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001896}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001897
1898
Guido van Rossum47478871996-08-21 14:32:37 +00001899static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001900validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001901{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001902 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001903 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001904
Guido van Rossum3d602e31996-07-21 02:33:56 +00001905 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001906 res = ((nch == 1)
1907 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001908 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001909 int pos;
1910 res = validate_and_test(CHILD(tree, 0));
1911 for (pos = 1; res && (pos < nch); pos += 2)
1912 res = (validate_name(CHILD(tree, pos), "or")
1913 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001914 }
1915 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001916}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001917
1918
Guido van Rossum47478871996-08-21 14:32:37 +00001919static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001920validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001921{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001922 int pos;
1923 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001924 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001925 && is_odd(nch)
1926 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001927
Guido van Rossum3d602e31996-07-21 02:33:56 +00001928 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001929 res = (validate_name(CHILD(tree, pos), "and")
1930 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001931
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001932 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001933}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001934
1935
Guido van Rossum47478871996-08-21 14:32:37 +00001936static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001937validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001938{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001939 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001940 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001941
Guido van Rossum3d602e31996-07-21 02:33:56 +00001942 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001943 if (nch == 2)
1944 res = (validate_name(CHILD(tree, 0), "not")
1945 && validate_not_test(CHILD(tree, 1)));
1946 else if (nch == 1)
1947 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001948 }
1949 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001950}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001951
1952
Guido van Rossum47478871996-08-21 14:32:37 +00001953static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001954validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001955{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001956 int pos;
1957 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001958 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001959 && is_odd(nch)
1960 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001961
Guido van Rossum3d602e31996-07-21 02:33:56 +00001962 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001963 res = (validate_comp_op(CHILD(tree, pos))
1964 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001965
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001966 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001967}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001968
1969
Guido van Rossum47478871996-08-21 14:32:37 +00001970static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001971validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001972{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001973 int res = 0;
1974 int nch = NCH(tree);
1975
Guido van Rossum3d602e31996-07-21 02:33:56 +00001976 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001977 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001978 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001979 /*
1980 * Only child will be a terminal with a well-defined symbolic name
1981 * or a NAME with a string of either 'is' or 'in'
1982 */
1983 tree = CHILD(tree, 0);
1984 switch (TYPE(tree)) {
1985 case LESS:
1986 case GREATER:
1987 case EQEQUAL:
1988 case EQUAL:
1989 case LESSEQUAL:
1990 case GREATEREQUAL:
1991 case NOTEQUAL:
1992 res = 1;
1993 break;
1994 case NAME:
1995 res = ((strcmp(STR(tree), "in") == 0)
1996 || (strcmp(STR(tree), "is") == 0));
1997 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001998 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00001999 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002000 }
2001 break;
2002 default:
Fred Drake661ea262000-10-24 19:57:45 +00002003 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002004 break;
2005 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002006 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002007 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002008 res = (validate_ntype(CHILD(tree, 0), NAME)
2009 && validate_ntype(CHILD(tree, 1), NAME)
2010 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2011 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2012 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2013 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2014 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002015 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002016 }
2017 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002018}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002019
2020
Guido van Rossum47478871996-08-21 14:32:37 +00002021static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002022validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002023{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002024 int j;
2025 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002026 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002027 && is_odd(nch)
2028 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002029
Guido van Rossum3d602e31996-07-21 02:33:56 +00002030 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002031 res = (validate_xor_expr(CHILD(tree, j))
2032 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002033
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002034 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002035}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002036
2037
Guido van Rossum47478871996-08-21 14:32:37 +00002038static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002039validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002040{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002041 int j;
2042 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002043 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002044 && is_odd(nch)
2045 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002046
Guido van Rossum3d602e31996-07-21 02:33:56 +00002047 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002048 res = (validate_circumflex(CHILD(tree, j - 1))
2049 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002050
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002051 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002052}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002053
2054
Guido van Rossum47478871996-08-21 14:32:37 +00002055static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002056validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002057{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002058 int pos;
2059 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002060 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002061 && is_odd(nch)
2062 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002063
Guido van Rossum3d602e31996-07-21 02:33:56 +00002064 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002065 res = (validate_ampersand(CHILD(tree, pos))
2066 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002067
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002068 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002069}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002070
2071
2072static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002073validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002074 {
2075 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002076 int nch = NCH(tree);
2077 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002078 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002079
Guido van Rossum3d602e31996-07-21 02:33:56 +00002080 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002081 if (TYPE(CHILD(tree, pos)) != op1)
2082 res = validate_ntype(CHILD(tree, pos), op2);
2083 if (res)
2084 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002085 }
2086 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002087}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002088
2089
Guido van Rossum47478871996-08-21 14:32:37 +00002090static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002091validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002092{
2093 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002094 && validate_chain_two_ops(tree, validate_arith_expr,
2095 LEFTSHIFT, RIGHTSHIFT));
2096}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002097
2098
Guido van Rossum47478871996-08-21 14:32:37 +00002099static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002100validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002101{
2102 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002103 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2104}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002105
2106
Guido van Rossum47478871996-08-21 14:32:37 +00002107static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002108validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002109{
2110 int pos = 1;
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, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002113 && is_odd(nch)
2114 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002115
Guido van Rossum3d602e31996-07-21 02:33:56 +00002116 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002117 res = (((TYPE(CHILD(tree, pos)) == STAR)
2118 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002119 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002120 || (TYPE(CHILD(tree, pos)) == PERCENT))
2121 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002122
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002123 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002124}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002125
2126
Guido van Rossum3d602e31996-07-21 02:33:56 +00002127/* factor:
2128 *
2129 * factor: ('+'|'-'|'~') factor | power
2130 */
Guido van Rossum47478871996-08-21 14:32:37 +00002131static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002132validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002133{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002134 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002135 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002136 && (((nch == 2)
2137 && ((TYPE(CHILD(tree, 0)) == PLUS)
2138 || (TYPE(CHILD(tree, 0)) == MINUS)
2139 || (TYPE(CHILD(tree, 0)) == TILDE))
2140 && validate_factor(CHILD(tree, 1)))
2141 || ((nch == 1)
2142 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002143 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002144}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002145
2146
Guido van Rossum3d602e31996-07-21 02:33:56 +00002147/* power:
2148 *
2149 * power: atom trailer* ('**' factor)*
2150 */
Guido van Rossum47478871996-08-21 14:32:37 +00002151static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002152validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002153{
2154 int pos = 1;
2155 int nch = NCH(tree);
2156 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002157 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002158
2159 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002160 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002161 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002162 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002163 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002164 return (0);
2165 }
2166 for ( ; res && (pos < (nch - 1)); pos += 2)
2167 res = (validate_doublestar(CHILD(tree, pos))
2168 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002169 }
2170 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002171}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002172
2173
Guido van Rossum47478871996-08-21 14:32:37 +00002174static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002175validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002176{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002177 int pos;
2178 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002179 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002180
Fred Drakecff283c2000-08-21 22:24:43 +00002181 if (res && nch < 1)
2182 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002183 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002184 switch (TYPE(CHILD(tree, 0))) {
2185 case LPAR:
2186 res = ((nch <= 3)
2187 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002188
Fred Drakeff9ea482000-04-19 13:54:15 +00002189 if (res && (nch == 3))
2190 res = validate_testlist(CHILD(tree, 1));
2191 break;
2192 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002193 if (nch == 2)
2194 res = validate_ntype(CHILD(tree, 1), RSQB);
2195 else if (nch == 3)
2196 res = (validate_listmaker(CHILD(tree, 1))
2197 && validate_ntype(CHILD(tree, 2), RSQB));
2198 else {
2199 res = 0;
2200 err_string("illegal list display atom");
2201 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002202 break;
2203 case LBRACE:
2204 res = ((nch <= 3)
2205 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002206
Fred Drakeff9ea482000-04-19 13:54:15 +00002207 if (res && (nch == 3))
2208 res = validate_dictmaker(CHILD(tree, 1));
2209 break;
2210 case BACKQUOTE:
2211 res = ((nch == 3)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002212 && validate_testlist1(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002213 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2214 break;
2215 case NAME:
2216 case NUMBER:
2217 res = (nch == 1);
2218 break;
2219 case STRING:
2220 for (pos = 1; res && (pos < nch); ++pos)
2221 res = validate_ntype(CHILD(tree, pos), STRING);
2222 break;
2223 default:
2224 res = 0;
2225 break;
2226 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002227 }
2228 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002229}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002230
2231
Fred Drake85bf3bb2000-08-23 15:35:26 +00002232/* listmaker:
2233 * test ( list_for | (',' test)* [','] )
2234 */
Fred Drakecff283c2000-08-21 22:24:43 +00002235static int
2236validate_listmaker(node *tree)
2237{
2238 int nch = NCH(tree);
2239 int ok = nch;
2240
2241 if (nch == 0)
2242 err_string("missing child nodes of listmaker");
2243 else
2244 ok = validate_test(CHILD(tree, 0));
2245
2246 /*
2247 * list_iter | (',' test)* [',']
2248 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002249 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2250 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002251 else {
2252 /* (',' test)* [','] */
2253 int i = 1;
2254 while (ok && nch - i >= 2) {
2255 ok = (validate_comma(CHILD(tree, i))
2256 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002257 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002258 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002259 if (ok && i == nch-1)
2260 ok = validate_comma(CHILD(tree, i));
2261 else if (i != nch) {
2262 ok = 0;
2263 err_string("illegal trailing nodes for listmaker");
2264 }
Fred Drakecff283c2000-08-21 22:24:43 +00002265 }
2266 return ok;
2267}
2268
2269
Guido van Rossum3d602e31996-07-21 02:33:56 +00002270/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002271 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002272 *
2273 */
Guido van Rossum47478871996-08-21 14:32:37 +00002274static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002275validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002276{
2277 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002278 && validate_numnodes(tree, 5, "funcdef")
2279 && validate_name(CHILD(tree, 0), "def")
2280 && validate_ntype(CHILD(tree, 1), NAME)
2281 && validate_colon(CHILD(tree, 3))
2282 && validate_parameters(CHILD(tree, 2))
2283 && validate_suite(CHILD(tree, 4)));
2284}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002285
2286
Guido van Rossum47478871996-08-21 14:32:37 +00002287static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002288validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002289{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002290 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002291 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002292 && ((nch == 3) || (nch == 4))
2293 && validate_name(CHILD(tree, 0), "lambda")
2294 && validate_colon(CHILD(tree, nch - 2))
2295 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002296
Guido van Rossum3d602e31996-07-21 02:33:56 +00002297 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002298 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002299 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002300 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002301
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002302 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002303}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002304
2305
Guido van Rossum3d602e31996-07-21 02:33:56 +00002306/* arglist:
2307 *
Fred Drakecff283c2000-08-21 22:24:43 +00002308 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002309 */
Guido van Rossum47478871996-08-21 14:32:37 +00002310static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002311validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002312{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002313 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002314 int i = 0;
2315 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002316
2317 if (nch <= 0)
2318 /* raise the right error from having an invalid number of children */
2319 return validate_numnodes(tree, nch + 1, "arglist");
2320
Fred Drakecff283c2000-08-21 22:24:43 +00002321 while (ok && nch-i >= 2) {
2322 /* skip leading (argument ',') */
2323 ok = (validate_argument(CHILD(tree, i))
2324 && validate_comma(CHILD(tree, i+1)));
2325 if (ok)
2326 i += 2;
2327 else
2328 PyErr_Clear();
2329 }
2330 ok = 1;
2331 if (nch-i > 0) {
2332 /*
2333 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002334 */
Fred Drakecff283c2000-08-21 22:24:43 +00002335 int sym = TYPE(CHILD(tree, i));
2336
2337 if (sym == argument) {
2338 ok = validate_argument(CHILD(tree, i));
2339 if (ok && i+1 != nch) {
2340 err_string("illegal arglist specification"
2341 " (extra stuff on end)");
2342 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002343 }
Fred Drakecff283c2000-08-21 22:24:43 +00002344 }
2345 else if (sym == STAR) {
2346 ok = validate_star(CHILD(tree, i));
2347 if (ok && (nch-i == 2))
2348 ok = validate_test(CHILD(tree, i+1));
2349 else if (ok && (nch-i == 5))
2350 ok = (validate_test(CHILD(tree, i+1))
2351 && validate_comma(CHILD(tree, i+2))
2352 && validate_doublestar(CHILD(tree, i+3))
2353 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002354 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002355 err_string("illegal use of '*' in arglist");
2356 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002357 }
2358 }
Fred Drakecff283c2000-08-21 22:24:43 +00002359 else if (sym == DOUBLESTAR) {
2360 if (nch-i == 2)
2361 ok = (validate_doublestar(CHILD(tree, i))
2362 && validate_test(CHILD(tree, i+1)));
2363 else {
2364 err_string("illegal use of '**' in arglist");
2365 ok = 0;
2366 }
2367 }
2368 else {
2369 err_string("illegal arglist specification");
2370 ok = 0;
2371 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002372 }
2373 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002374}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002375
2376
2377
2378/* argument:
2379 *
2380 * [test '='] test
2381 */
Guido van Rossum47478871996-08-21 14:32:37 +00002382static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002383validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002384{
2385 int nch = NCH(tree);
2386 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002387 && ((nch == 1) || (nch == 3))
2388 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002389
2390 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002391 res = (validate_equal(CHILD(tree, 1))
2392 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002393
2394 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002395}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002396
2397
2398
2399/* trailer:
2400 *
Guido van Rossum47478871996-08-21 14:32:37 +00002401 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002402 */
Guido van Rossum47478871996-08-21 14:32:37 +00002403static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002404validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002405{
2406 int nch = NCH(tree);
2407 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002408
2409 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002410 switch (TYPE(CHILD(tree, 0))) {
2411 case LPAR:
2412 res = validate_rparen(CHILD(tree, nch - 1));
2413 if (res && (nch == 3))
2414 res = validate_arglist(CHILD(tree, 1));
2415 break;
2416 case LSQB:
2417 res = (validate_numnodes(tree, 3, "trailer")
2418 && validate_subscriptlist(CHILD(tree, 1))
2419 && validate_ntype(CHILD(tree, 2), RSQB));
2420 break;
2421 case DOT:
2422 res = (validate_numnodes(tree, 2, "trailer")
2423 && validate_ntype(CHILD(tree, 1), NAME));
2424 break;
2425 default:
2426 res = 0;
2427 break;
2428 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002429 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002430 else {
2431 (void) validate_numnodes(tree, 2, "trailer");
2432 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002433 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002434}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002435
2436
Guido van Rossum47478871996-08-21 14:32:37 +00002437/* subscriptlist:
2438 *
2439 * subscript (',' subscript)* [',']
2440 */
2441static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002442validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002443{
2444 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002445 validate_subscript, "subscriptlist"));
2446}
Guido van Rossum47478871996-08-21 14:32:37 +00002447
2448
Guido van Rossum3d602e31996-07-21 02:33:56 +00002449/* subscript:
2450 *
Guido van Rossum47478871996-08-21 14:32:37 +00002451 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002452 */
Guido van Rossum47478871996-08-21 14:32:37 +00002453static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002454validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002455{
Guido van Rossum47478871996-08-21 14:32:37 +00002456 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002457 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002458 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002459
Guido van Rossum47478871996-08-21 14:32:37 +00002460 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002461 if (!PyErr_Occurred())
2462 err_string("invalid number of arguments for subscript node");
2463 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002464 }
Guido van Rossum47478871996-08-21 14:32:37 +00002465 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002466 /* take care of ('.' '.' '.') possibility */
2467 return (validate_numnodes(tree, 3, "subscript")
2468 && validate_dot(CHILD(tree, 0))
2469 && validate_dot(CHILD(tree, 1))
2470 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002471 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002472 if (TYPE(CHILD(tree, 0)) == test)
2473 res = validate_test(CHILD(tree, 0));
2474 else
2475 res = validate_colon(CHILD(tree, 0));
2476 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002477 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002478 /* Must be [test] ':' [test] [sliceop],
2479 * but at least one of the optional components will
2480 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002481 */
2482 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002483 res = validate_test(CHILD(tree, 0));
2484 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002485 }
2486 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002487 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002488 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002489 int rem = nch - ++offset;
2490 if (rem) {
2491 if (TYPE(CHILD(tree, offset)) == test) {
2492 res = validate_test(CHILD(tree, offset));
2493 ++offset;
2494 --rem;
2495 }
2496 if (res && rem)
2497 res = validate_sliceop(CHILD(tree, offset));
2498 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002499 }
2500 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002501}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002502
2503
Guido van Rossum47478871996-08-21 14:32:37 +00002504static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002505validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002506{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002507 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002508 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002509 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002510 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002511 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002512 }
Guido van Rossum47478871996-08-21 14:32:37 +00002513 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002514 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002515 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002516 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002517
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002518 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002519}
Guido van Rossum47478871996-08-21 14:32:37 +00002520
2521
2522static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002523validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002524{
2525 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002526 validate_expr, "exprlist"));
2527}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002528
2529
Guido van Rossum47478871996-08-21 14:32:37 +00002530static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002531validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002532{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002533 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002534 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002535 && (nch >= 3)
2536 && validate_test(CHILD(tree, 0))
2537 && validate_colon(CHILD(tree, 1))
2538 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002539
Guido van Rossum3d602e31996-07-21 02:33:56 +00002540 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002541 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002542 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002543 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002544
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002545 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002546 int pos = 3;
2547 /* ( ',' test ':' test )* */
2548 while (res && (pos < nch)) {
2549 res = (validate_comma(CHILD(tree, pos))
2550 && validate_test(CHILD(tree, pos + 1))
2551 && validate_colon(CHILD(tree, pos + 2))
2552 && validate_test(CHILD(tree, pos + 3)));
2553 pos += 4;
2554 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002555 }
2556 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002557}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002558
2559
Guido van Rossum47478871996-08-21 14:32:37 +00002560static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002561validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002562{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002563 int pos;
2564 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002565 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002566 && (nch >= 2)
2567 && validate_testlist(CHILD(tree, 0))
2568 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002569
Guido van Rossum3d602e31996-07-21 02:33:56 +00002570 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002571 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002572
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002573 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002574}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002575
2576
Guido van Rossum47478871996-08-21 14:32:37 +00002577static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002578validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002579{
Fred Drakeff9ea482000-04-19 13:54:15 +00002580 int nch = 0; /* num. children on current node */
2581 int res = 1; /* result value */
2582 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002583
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002584 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002585 nch = NCH(tree);
2586 next = 0;
2587 switch (TYPE(tree)) {
2588 /*
2589 * Definition nodes.
2590 */
2591 case funcdef:
2592 res = validate_funcdef(tree);
2593 break;
2594 case classdef:
2595 res = validate_class(tree);
2596 break;
2597 /*
2598 * "Trivial" parse tree nodes.
2599 * (Why did I call these trivial?)
2600 */
2601 case stmt:
2602 res = validate_stmt(tree);
2603 break;
2604 case small_stmt:
2605 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002606 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002607 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2608 */
2609 res = validate_small_stmt(tree);
2610 break;
2611 case flow_stmt:
2612 res = (validate_numnodes(tree, 1, "flow_stmt")
2613 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2614 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002615 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002616 || (TYPE(CHILD(tree, 0)) == return_stmt)
2617 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2618 if (res)
2619 next = CHILD(tree, 0);
2620 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002621 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002622 break;
Fred Drake02126f22001-07-17 02:59:15 +00002623 case yield_stmt:
2624 res = validate_yield_stmt(tree);
2625 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002626 /*
2627 * Compound statements.
2628 */
2629 case simple_stmt:
2630 res = validate_simple_stmt(tree);
2631 break;
2632 case compound_stmt:
2633 res = validate_compound_stmt(tree);
2634 break;
2635 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002636 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002637 */
2638 case expr_stmt:
2639 res = validate_expr_stmt(tree);
2640 break;
2641 case print_stmt:
2642 res = validate_print_stmt(tree);
2643 break;
2644 case del_stmt:
2645 res = validate_del_stmt(tree);
2646 break;
2647 case pass_stmt:
2648 res = (validate_numnodes(tree, 1, "pass")
2649 && validate_name(CHILD(tree, 0), "pass"));
2650 break;
2651 case break_stmt:
2652 res = (validate_numnodes(tree, 1, "break")
2653 && validate_name(CHILD(tree, 0), "break"));
2654 break;
2655 case continue_stmt:
2656 res = (validate_numnodes(tree, 1, "continue")
2657 && validate_name(CHILD(tree, 0), "continue"));
2658 break;
2659 case return_stmt:
2660 res = validate_return_stmt(tree);
2661 break;
2662 case raise_stmt:
2663 res = validate_raise_stmt(tree);
2664 break;
2665 case import_stmt:
2666 res = validate_import_stmt(tree);
2667 break;
2668 case global_stmt:
2669 res = validate_global_stmt(tree);
2670 break;
2671 case exec_stmt:
2672 res = validate_exec_stmt(tree);
2673 break;
2674 case assert_stmt:
2675 res = validate_assert_stmt(tree);
2676 break;
2677 case if_stmt:
2678 res = validate_if(tree);
2679 break;
2680 case while_stmt:
2681 res = validate_while(tree);
2682 break;
2683 case for_stmt:
2684 res = validate_for(tree);
2685 break;
2686 case try_stmt:
2687 res = validate_try(tree);
2688 break;
2689 case suite:
2690 res = validate_suite(tree);
2691 break;
2692 /*
2693 * Expression nodes.
2694 */
2695 case testlist:
2696 res = validate_testlist(tree);
2697 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002698 case testlist1:
2699 res = validate_testlist1(tree);
2700 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002701 case test:
2702 res = validate_test(tree);
2703 break;
2704 case and_test:
2705 res = validate_and_test(tree);
2706 break;
2707 case not_test:
2708 res = validate_not_test(tree);
2709 break;
2710 case comparison:
2711 res = validate_comparison(tree);
2712 break;
2713 case exprlist:
2714 res = validate_exprlist(tree);
2715 break;
2716 case comp_op:
2717 res = validate_comp_op(tree);
2718 break;
2719 case expr:
2720 res = validate_expr(tree);
2721 break;
2722 case xor_expr:
2723 res = validate_xor_expr(tree);
2724 break;
2725 case and_expr:
2726 res = validate_and_expr(tree);
2727 break;
2728 case shift_expr:
2729 res = validate_shift_expr(tree);
2730 break;
2731 case arith_expr:
2732 res = validate_arith_expr(tree);
2733 break;
2734 case term:
2735 res = validate_term(tree);
2736 break;
2737 case factor:
2738 res = validate_factor(tree);
2739 break;
2740 case power:
2741 res = validate_power(tree);
2742 break;
2743 case atom:
2744 res = validate_atom(tree);
2745 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002746
Fred Drakeff9ea482000-04-19 13:54:15 +00002747 default:
2748 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002749 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002750 res = 0;
2751 break;
2752 }
2753 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002754 }
2755 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002756}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002757
2758
Guido van Rossum47478871996-08-21 14:32:37 +00002759static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002760validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002761{
2762 int res = validate_eval_input(tree);
2763
2764 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002765 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002766
2767 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002768}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002769
2770
Guido van Rossum3d602e31996-07-21 02:33:56 +00002771/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002772 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002773 */
Guido van Rossum47478871996-08-21 14:32:37 +00002774static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002775validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002776{
Fred Drakec2683dd2001-07-17 19:32:05 +00002777 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002778 int nch = NCH(tree) - 1;
2779 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002780 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002781
Fred Drakec2683dd2001-07-17 19:32:05 +00002782 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002783 if (TYPE(CHILD(tree, j)) == stmt)
2784 res = validate_stmt(CHILD(tree, j));
2785 else
2786 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002787 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002788 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002789 * user. Hopefully, this won't be needed. If a user reports getting
2790 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002791 */
2792 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002793 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002794
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002795 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002796}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002797
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00002798static int
2799validate_encoding_decl(node *tree)
2800{
2801 int nch = NCH(tree);
2802 int res = ((nch == 1)
2803 && validate_file_input(CHILD(tree, 0)));
2804
2805 if (!res && !PyErr_Occurred())
2806 err_string("Error Parsing encoding_decl");
2807
2808 return res;
2809}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002810
Fred Drake43f8f9b1998-04-13 16:25:46 +00002811static PyObject*
2812pickle_constructor = NULL;
2813
2814
2815static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002816parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002817{
Fred Drake268397f1998-04-29 14:16:32 +00002818 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002819 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00002820 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002821 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002822
Fred Drakec2683dd2001-07-17 19:32:05 +00002823 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002824 PyObject *newargs;
2825 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002826
Fred Drake2a6875e1999-09-20 22:32:18 +00002827 if ((empty_dict = PyDict_New()) == NULL)
2828 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002829 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002830 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002831 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002832 if (tuple != NULL) {
2833 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2834 Py_DECREF(tuple);
2835 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002836 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002837 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002838 }
2839 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002840 Py_XDECREF(empty_dict);
2841
Fred Drake43f8f9b1998-04-13 16:25:46 +00002842 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002843}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002844
2845
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002846/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00002847 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002848 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002849 * We'd really have to write a wrapper around it all anyway to allow
2850 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002851 */
2852static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00002853 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002854 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002855 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002856 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002857 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002858 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002859 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002860 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002861 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002862 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002863 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002864 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002865 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002866 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002867 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002868 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002869 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002870 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002871 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002872 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002873 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002874 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002875 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002876 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002877 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002878 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002879 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002880 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002881
Fred Drake43f8f9b1998-04-13 16:25:46 +00002882 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00002883 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002884 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00002885
Fred Drake268397f1998-04-29 14:16:32 +00002886 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002887 };
2888
2889
Mark Hammond62b1ab12002-07-23 06:31:15 +00002890PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00002891
Mark Hammond62b1ab12002-07-23 06:31:15 +00002892PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00002893initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00002894{
Fred Drake13130bc2001-08-15 16:44:56 +00002895 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00002896
2897 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002898 module = Py_InitModule("parser", parser_functions);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002899
Fred Drake7a15ba51999-09-09 14:21:52 +00002900 if (parser_error == 0)
2901 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002902
Tim Peters6a627252003-07-21 14:25:23 +00002903 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00002904 /* caller will check PyErr_Occurred() */
2905 return;
Tim Peters6a627252003-07-21 14:25:23 +00002906 /* CAUTION: The code next used to skip bumping the refcount on
2907 * parser_error. That's a disaster if initparser() gets called more
2908 * than once. By incref'ing, we ensure that each module dict that
2909 * gets created owns its reference to the shared parser_error object,
2910 * and the file static parser_error vrbl owns a reference too.
2911 */
2912 Py_INCREF(parser_error);
2913 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
2914 return;
2915
Fred Drakec2683dd2001-07-17 19:32:05 +00002916 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002917 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00002918 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002919 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002920
Fred Drake13130bc2001-08-15 16:44:56 +00002921 PyModule_AddStringConstant(module, "__copyright__",
2922 parser_copyright_string);
2923 PyModule_AddStringConstant(module, "__doc__",
2924 parser_doc_string);
2925 PyModule_AddStringConstant(module, "__version__",
2926 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002927
Fred Drake78bdb9b2001-07-19 20:17:15 +00002928 /* Register to support pickling.
2929 * If this fails, the import of this module will fail because an
2930 * exception will be raised here; should we clear the exception?
2931 */
Fred Drake13130bc2001-08-15 16:44:56 +00002932 copyreg = PyImport_ImportModule("copy_reg");
2933 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002934 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002935
Fred Drake13130bc2001-08-15 16:44:56 +00002936 func = PyObject_GetAttrString(copyreg, "pickle");
2937 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
2938 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00002939 Py_XINCREF(pickle_constructor);
2940 if ((func != NULL) && (pickle_constructor != NULL)
2941 && (pickler != NULL)) {
2942 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002943
Fred Drakec2683dd2001-07-17 19:32:05 +00002944 res = PyObject_CallFunction(func, "OOO", &PyST_Type, pickler,
2945 pickle_constructor);
Fred Drakeff9ea482000-04-19 13:54:15 +00002946 Py_XDECREF(res);
2947 }
2948 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00002949 Py_XDECREF(pickle_constructor);
2950 Py_XDECREF(pickler);
2951 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002952 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002953}