blob: 4a795ed77432b485582009abd69eb2aa69dd4c1e [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000026 */
27
Fred Drakeff9ea482000-04-19 13:54:15 +000028#include "Python.h" /* general Python API */
29#include "graminit.h" /* symbols defined in the grammar */
30#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000031#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000032#include "token.h" /* token definitions */
33 /* ISTERMINAL() / ISNONTERMINAL() */
34#include "compile.h" /* PyNode_Compile() */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000035
Fred Drake268397f1998-04-29 14:16:32 +000036#ifdef lint
37#include <note.h>
38#else
39#define NOTE(x)
40#endif
41
Guido van Rossum19efc5f1998-04-28 16:10:19 +000042#ifdef macintosh
Fred Drakeff9ea482000-04-19 13:54:15 +000043char *strdup(char *);
Guido van Rossum19efc5f1998-04-28 16:10:19 +000044#endif
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000045
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000046/* String constants used to initialize module attributes.
47 *
48 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000049static char parser_copyright_string[] =
50"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000051University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
52Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
53Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000054
55
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000056PyDoc_STRVAR(parser_doc_string,
57"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000058
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000059static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000060
61
Fred Drakeff9ea482000-04-19 13:54:15 +000062typedef PyObject* (*SeqMaker) (int length);
63typedef int (*SeqInserter) (PyObject* sequence,
64 int index,
65 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000066
Thomas Wouters7e474022000-07-16 12:04:32 +000067/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000068 * original copyright statement is included below, and continues to apply
69 * in full to the function immediately following. All other material is
70 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
71 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000072 * new naming conventions. Added arguments to provide support for creating
73 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000074 */
75
Guido van Rossum52f2c051993-11-10 12:53:24 +000076
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000077static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000078node2tuple(node *n, /* node to convert */
79 SeqMaker mkseq, /* create sequence */
80 SeqInserter addelem, /* func. to add elem. in seq. */
81 int lineno) /* include line numbers? */
Guido van Rossum47478871996-08-21 14:32:37 +000082{
Guido van Rossum3d602e31996-07-21 02:33:56 +000083 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000084 Py_INCREF(Py_None);
85 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000086 }
87 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000088 int i;
89 PyObject *v;
90 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000091
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000092 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000093 if (v == NULL)
94 return (v);
95 w = PyInt_FromLong(TYPE(n));
96 if (w == NULL) {
97 Py_DECREF(v);
98 return ((PyObject*) NULL);
99 }
100 (void) addelem(v, 0, w);
101 for (i = 0; i < NCH(n); i++) {
102 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno);
103 if (w == NULL) {
104 Py_DECREF(v);
105 return ((PyObject*) NULL);
106 }
107 (void) addelem(v, i+1, w);
108 }
Tim Peters6a627252003-07-21 14:25:23 +0000109
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000110 if (TYPE(n) == encoding_decl)
111 (void) addelem(v, i+1, PyString_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000112 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000113 }
114 else if (ISTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000115 PyObject *result = mkseq(2 + lineno);
116 if (result != NULL) {
117 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
118 (void) addelem(result, 1, PyString_FromString(STR(n)));
119 if (lineno == 1)
120 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
121 }
122 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000123 }
124 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000125 PyErr_SetString(PyExc_SystemError,
126 "unrecognized parse tree node type");
127 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000128 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000129}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000130/*
131 * End of material copyrighted by Stichting Mathematisch Centrum.
132 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000133
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000134
135
136/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000137 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000138 * field of the object type to identify which any given object represents.
139 * These should probably go in an external header to allow other extensions
140 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000141 */
142
Fred Drakec2683dd2001-07-17 19:32:05 +0000143#define PyST_EXPR 1
144#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000145
146
147/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000148 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000149 * naming style, but the code uses the new naming convention.
150 */
151
152static PyObject*
153parser_error = 0;
154
155
Fred Drakec2683dd2001-07-17 19:32:05 +0000156typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000157 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000158 node* st_node; /* the node* returned by the parser */
159 int st_type; /* EXPR or SUITE ? */
160} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000161
162
Jeremy Hylton938ace62002-07-17 16:30:39 +0000163static void parser_free(PyST_Object *st);
164static int parser_compare(PyST_Object *left, PyST_Object *right);
165static PyObject *parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000166
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000167
Fred Drake268397f1998-04-29 14:16:32 +0000168static
Fred Drakec2683dd2001-07-17 19:32:05 +0000169PyTypeObject PyST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000170 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000171 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000172 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000173 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000174 0, /* tp_itemsize */
175 (destructor)parser_free, /* tp_dealloc */
176 0, /* tp_print */
177 parser_getattr, /* tp_getattr */
178 0, /* tp_setattr */
179 (cmpfunc)parser_compare, /* tp_compare */
180 0, /* tp_repr */
181 0, /* tp_as_number */
182 0, /* tp_as_sequence */
183 0, /* tp_as_mapping */
184 0, /* tp_hash */
185 0, /* tp_call */
186 0, /* tp_str */
187 0, /* tp_getattro */
188 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000189
190 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000191 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000192
Fred Drakeff9ea482000-04-19 13:54:15 +0000193 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000194
195 /* __doc__ */
196 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000197}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000198
199
200static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000201parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000202{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000203 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000204
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000205 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000206 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000207
208 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000209 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000210
211 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000212 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000213
214 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000215 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000216
217 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000218 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000219
220 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000221 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000222
Fred Drakeff9ea482000-04-19 13:54:15 +0000223 if (v != 0)
224 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000225 }
226 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000227}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000228
229
Fred Drakec2683dd2001-07-17 19:32:05 +0000230/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000231 *
232 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
233 * This really just wraps a call to parser_compare_nodes() with some easy
234 * checks and protection code.
235 *
236 */
237static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000238parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000239{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000240 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000241 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000242
243 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000244 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000245
Fred Drakec2683dd2001-07-17 19:32:05 +0000246 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000247}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000248
249
Fred Drakec2683dd2001-07-17 19:32:05 +0000250/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000251 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000252 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000253 * 'wrapper' object that holds a node* and allows it to be passed around in
254 * Python code.
255 *
256 */
257static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000258parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000259{
Fred Drakec2683dd2001-07-17 19:32:05 +0000260 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000261
262 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000263 o->st_node = st;
264 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265 }
Fred Drake268397f1998-04-29 14:16:32 +0000266 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000267 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000268 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000269 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000270}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000271
272
Fred Drakec2683dd2001-07-17 19:32:05 +0000273/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000274 *
275 * This is called by a del statement that reduces the reference count to 0.
276 *
277 */
278static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000279parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000280{
Fred Drakec2683dd2001-07-17 19:32:05 +0000281 PyNode_Free(st->st_node);
282 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000283}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000284
285
Fred Drakec2683dd2001-07-17 19:32:05 +0000286/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000287 *
288 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000289 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000290 *
291 */
292static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000293parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000294{
Guido van Rossum47478871996-08-21 14:32:37 +0000295 PyObject *line_option = 0;
296 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000297 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000298
Fred Drake7a15ba51999-09-09 14:21:52 +0000299 static char *keywords[] = {"ast", "line_info", NULL};
300
Fred Drake268397f1998-04-29 14:16:32 +0000301 if (self == NULL) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000302 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2tuple", keywords,
303 &PyST_Type, &self, &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000304 }
Fred Drake503d8d61998-04-13 18:45:18 +0000305 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000306 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:totuple", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000307 &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000308 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000309 int lineno = 0;
310 if (line_option != NULL) {
311 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
312 }
313 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000314 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000315 * since it's known to work already.
316 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000317 res = node2tuple(((PyST_Object*)self)->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000318 PyTuple_New, PyTuple_SetItem, lineno);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000319 }
320 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000321}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000322
323
Fred Drakec2683dd2001-07-17 19:32:05 +0000324/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000325 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000326 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000327 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000328 *
329 */
330static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000331parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000332{
Guido van Rossum47478871996-08-21 14:32:37 +0000333 PyObject *line_option = 0;
334 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000335 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000336
Fred Drake7a15ba51999-09-09 14:21:52 +0000337 static char *keywords[] = {"ast", "line_info", NULL};
338
Fred Drake503d8d61998-04-13 18:45:18 +0000339 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000340 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2list", keywords,
341 &PyST_Type, &self, &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000342 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000343 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:tolist", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000344 &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000345 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000346 int lineno = 0;
347 if (line_option != 0) {
348 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
349 }
350 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000351 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000352 * since it's known to work already.
353 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000354 res = node2tuple(self->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000355 PyList_New, PyList_SetItem, lineno);
Guido van Rossum47478871996-08-21 14:32:37 +0000356 }
357 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000358}
Guido van Rossum47478871996-08-21 14:32:37 +0000359
360
Fred Drakec2683dd2001-07-17 19:32:05 +0000361/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000362 *
363 * This function creates code objects from the parse tree represented by
364 * the passed-in data object. An optional file name is passed in as well.
365 *
366 */
367static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000368parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000369{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000370 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000371 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000372 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000373
Fred Drake7a15ba51999-09-09 14:21:52 +0000374 static char *keywords[] = {"ast", "filename", NULL};
375
Fred Drake503d8d61998-04-13 18:45:18 +0000376 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000377 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
378 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000379 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000380 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000381 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000382
383 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000384 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000385
386 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000387}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000388
389
390/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
391 * PyObject* parser_issuite(PyObject* self, PyObject* args)
392 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000393 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000394 * a statement suite, respectively. The return is a Python truth value.
395 *
396 */
397static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000398parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000399{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000400 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000401 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000402
Fred Drake7a15ba51999-09-09 14:21:52 +0000403 static char *keywords[] = {"ast", NULL};
404
Fred Drake503d8d61998-04-13 18:45:18 +0000405 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000406 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000407 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000408 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000409 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000410
411 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000412 /* Check to see if the ST represents an expression or not. */
413 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000414 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000415 }
416 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000417}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000418
419
420static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000421parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000422{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000423 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000424 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000425
Fred Drake7a15ba51999-09-09 14:21:52 +0000426 static char *keywords[] = {"ast", NULL};
427
Fred Drake503d8d61998-04-13 18:45:18 +0000428 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000429 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000430 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000431 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000432 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000433
434 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000435 /* Check to see if the ST represents an expression or not. */
436 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000437 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000438 }
439 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000440}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000441
442
Fred Drake7a15ba51999-09-09 14:21:52 +0000443#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
444
Fred Drake503d8d61998-04-13 18:45:18 +0000445static PyMethodDef
446parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000447 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000448 PyDoc_STR("Compile this ST object into a code object.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000449 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000450 PyDoc_STR("Determines if this ST object was created from an expression.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000451 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000452 PyDoc_STR("Determines if this ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000453 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000454 PyDoc_STR("Creates a list-tree representation of this ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000455 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000456 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Fred Drake503d8d61998-04-13 18:45:18 +0000457
Fred Drake268397f1998-04-29 14:16:32 +0000458 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000459};
460
Fred Drake503d8d61998-04-13 18:45:18 +0000461
462static PyObject*
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000463parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000464{
Fred Drake503d8d61998-04-13 18:45:18 +0000465 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000466}
Fred Drake503d8d61998-04-13 18:45:18 +0000467
468
Guido van Rossum3d602e31996-07-21 02:33:56 +0000469/* err_string(char* message)
470 *
471 * Sets the error string for an exception of type ParserError.
472 *
473 */
474static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000475err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000476{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000477 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000478}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000479
480
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000481/* PyObject* parser_do_parse(PyObject* args, int type)
482 *
483 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000484 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000485 *
486 */
487static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000488parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000489{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000490 char* string = 0;
491 PyObject* res = 0;
492
Fred Drake7a15ba51999-09-09 14:21:52 +0000493 static char *keywords[] = {"source", NULL};
494
495 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000496 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000497 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000498 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000499
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000500 if (n)
501 res = parser_newstobject(n, type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000502 }
503 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000504}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000505
506
507/* PyObject* parser_expr(PyObject* self, PyObject* args)
508 * PyObject* parser_suite(PyObject* self, PyObject* args)
509 *
510 * External interfaces to the parser itself. Which is called determines if
511 * the parser attempts to recognize an expression ('eval' form) or statement
512 * suite ('exec' form). The real work is done by parser_do_parse() above.
513 *
514 */
515static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000516parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000517{
Fred Drake268397f1998-04-29 14:16:32 +0000518 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000519 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000520}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000521
522
523static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000524parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000525{
Fred Drake268397f1998-04-29 14:16:32 +0000526 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000527 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000528}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000529
530
531
Fred Drakec2683dd2001-07-17 19:32:05 +0000532/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000533 * object requires that the input tuple be valid without having to rely on
534 * catching an exception from the compiler. This is done to allow the
535 * compiler itself to remain fast, since most of its input will come from
536 * the parser directly, and therefore be known to be syntactically correct.
537 * This validation is done to ensure that we don't core dump the compile
538 * phase, returning an exception instead.
539 *
540 * Two aspects can be broken out in this code: creating a node tree from
541 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000542 * advantageous to expand the number of ST types to include funcdefs and
543 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000544 * here. They are not necessary, and not quite as useful in a raw form.
545 * For now, let's get expressions and suites working reliably.
546 */
547
548
Jeremy Hylton938ace62002-07-17 16:30:39 +0000549static node* build_node_tree(PyObject *tuple);
550static int validate_expr_tree(node *tree);
551static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000552static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000553
Fred Drakec2683dd2001-07-17 19:32:05 +0000554/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000555 *
556 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000557 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000558 * tuple can be validated. It does this by checking the first code of the
559 * tuple, and, if acceptable, builds the internal representation. If this
560 * step succeeds, the internal representation is validated as fully as
561 * possible with the various validate_*() routines defined below.
562 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000563 * This function must be changed if support is to be added for PyST_FRAGMENT
564 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000565 *
566 */
567static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000568parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000569{
Fred Drake268397f1998-04-29 14:16:32 +0000570 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000571 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000572 PyObject *tuple;
573 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000574
Fred Drake7a15ba51999-09-09 14:21:52 +0000575 static char *keywords[] = {"sequence", NULL};
576
Fred Drakec2683dd2001-07-17 19:32:05 +0000577 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000578 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000579 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000580 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000581 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000582 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000583 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000584 }
585 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000586 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000587 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000588 tree = build_node_tree(tuple);
589 if (tree != 0) {
590 int start_sym = TYPE(tree);
591 if (start_sym == eval_input) {
592 /* Might be an eval form. */
593 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000594 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000595 else
596 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000597 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000598 else if (start_sym == file_input) {
599 /* This looks like an exec form so far. */
600 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000601 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000602 else
603 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000604 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000605 else if (start_sym == encoding_decl) {
606 /* This looks like an encoding_decl so far. */
607 if (validate_encoding_decl(tree))
608 st = parser_newstobject(tree, PyST_SUITE);
609 else
610 PyNode_Free(tree);
611 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000612 else {
613 /* This is a fragment, at best. */
614 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000615 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000616 }
Guido van Rossum47478871996-08-21 14:32:37 +0000617 }
Guido van Rossum47478871996-08-21 14:32:37 +0000618 /* Make sure we throw an exception on all errors. We should never
619 * get this, but we'd do well to be sure something is done.
620 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000621 if (st == NULL && !PyErr_Occurred())
622 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000623
Fred Drakec2683dd2001-07-17 19:32:05 +0000624 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000625}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000626
627
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000628/* node* build_node_children()
629 *
630 * Iterate across the children of the current non-terminal node and build
631 * their structures. If successful, return the root of this portion of
632 * the tree, otherwise, 0. Any required exception will be specified already,
633 * and no memory will have been deallocated.
634 *
635 */
636static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000637build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000638{
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000639 int len = PyObject_Size(tuple);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000640 int i, err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000641
642 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000643 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000644 PyObject* elem = PySequence_GetItem(tuple, i);
645 int ok = elem != NULL;
646 long type = 0;
647 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000648
Fred Drakeff9ea482000-04-19 13:54:15 +0000649 if (ok)
650 ok = PySequence_Check(elem);
651 if (ok) {
652 PyObject *temp = PySequence_GetItem(elem, 0);
653 if (temp == NULL)
654 ok = 0;
655 else {
656 ok = PyInt_Check(temp);
657 if (ok)
658 type = PyInt_AS_LONG(temp);
659 Py_DECREF(temp);
660 }
661 }
662 if (!ok) {
663 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000664 Py_BuildValue("os", elem,
Fred Drakeff9ea482000-04-19 13:54:15 +0000665 "Illegal node construct."));
666 Py_XDECREF(elem);
667 return (0);
668 }
669 if (ISTERMINAL(type)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000670 int len = PyObject_Size(elem);
671 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000672
Fred Drake0ac9b072000-09-12 21:58:06 +0000673 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000674 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000675 return 0;
676 }
677 temp = PySequence_GetItem(elem, 1);
678 if (temp == NULL)
679 return 0;
680 if (!PyString_Check(temp)) {
681 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000682 "second item in terminal node must be a string,"
683 " found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000684 temp->ob_type->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000685 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000686 return 0;
687 }
688 if (len == 3) {
689 PyObject *o = PySequence_GetItem(elem, 2);
690 if (o != NULL) {
691 if (PyInt_Check(o))
692 *line_num = PyInt_AS_LONG(o);
693 else {
694 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000695 "third item in terminal node must be an"
696 " integer, found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000697 temp->ob_type->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000698 Py_DECREF(o);
699 Py_DECREF(temp);
700 return 0;
701 }
702 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000703 }
704 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000705 len = PyString_GET_SIZE(temp) + 1;
706 strn = (char *)PyMem_MALLOC(len);
707 if (strn != NULL)
708 (void) memcpy(strn, PyString_AS_STRING(temp), len);
709 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000710 }
711 else if (!ISNONTERMINAL(type)) {
712 /*
713 * It has to be one or the other; this is an error.
714 * Throw an exception.
715 */
716 PyErr_SetObject(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000717 Py_BuildValue("os", elem, "unknown node type."));
Fred Drakeff9ea482000-04-19 13:54:15 +0000718 Py_XDECREF(elem);
719 return (0);
720 }
Fred Drake8b55b2d2001-12-05 22:10:44 +0000721 err = PyNode_AddChild(root, type, strn, *line_num);
722 if (err == E_NOMEM) {
723 PyMem_DEL(strn);
724 return (node *) PyErr_NoMemory();
725 }
726 if (err == E_OVERFLOW) {
727 PyMem_DEL(strn);
728 PyErr_SetString(PyExc_ValueError,
729 "unsupported number of child nodes");
730 return NULL;
731 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000732
Fred Drakeff9ea482000-04-19 13:54:15 +0000733 if (ISNONTERMINAL(type)) {
734 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000735
Fred Drakeff9ea482000-04-19 13:54:15 +0000736 if (new_child != build_node_children(elem, new_child, line_num)) {
737 Py_XDECREF(elem);
738 return (0);
739 }
740 }
741 else if (type == NEWLINE) { /* It's true: we increment the */
742 ++(*line_num); /* line number *after* the newline! */
743 }
744 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000745 }
746 return (root);
Fred Drakeff9ea482000-04-19 13:54:15 +0000747}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000748
749
750static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000751build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000752{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000753 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000754 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000755 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000756
Guido van Rossum47478871996-08-21 14:32:37 +0000757 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000758 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000759 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000760 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000761 /*
762 * The tuple is simple, but it doesn't start with a start symbol.
763 * Throw an exception now and be done with it.
764 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000765 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000766 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000767 PyErr_SetObject(parser_error, tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000768 }
769 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000770 /*
771 * Not efficient, but that can be handled later.
772 */
773 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000774 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000775
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000776 if (num == encoding_decl) {
777 encoding = PySequence_GetItem(tuple, 2);
778 /* tuple isn't borrowed anymore here, need to DECREF */
779 tuple = PySequence_GetSlice(tuple, 0, 2);
780 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000781 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000782 if (res != NULL) {
783 if (res != build_node_children(tuple, res, &line_num)) {
784 PyNode_Free(res);
785 res = NULL;
786 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000787 if (res && encoding) {
788 int len;
789 len = PyString_GET_SIZE(encoding) + 1;
790 res->n_str = (char *)PyMem_MALLOC(len);
791 if (res->n_str != NULL)
792 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
793 Py_DECREF(encoding);
794 Py_DECREF(tuple);
795 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000796 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000797 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000798 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000799 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000800 * NONTERMINAL, we can't use it. Not sure the implementation
801 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000802 */
803 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000804 Py_BuildValue("os", tuple,
Fred Drakeff9ea482000-04-19 13:54:15 +0000805 "Illegal component tuple."));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000806
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000807 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000808}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000809
810
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000811/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000812 * Validation routines used within the validation section:
813 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000814static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000815
Fred Drakeff9ea482000-04-19 13:54:15 +0000816#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
817#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
818#define validate_colon(ch) validate_terminal(ch, COLON, ":")
819#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
820#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
821#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
822#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
823#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
824#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
825#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
826#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
827#define validate_star(ch) validate_terminal(ch, STAR, "*")
828#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
829#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
830#define validate_dot(ch) validate_terminal(ch, DOT, ".")
831#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000832
Fred Drake0ac9b072000-09-12 21:58:06 +0000833#define VALIDATER(n) static int validate_##n(node *tree)
834
Fred Drakeff9ea482000-04-19 13:54:15 +0000835VALIDATER(node); VALIDATER(small_stmt);
836VALIDATER(class); VALIDATER(node);
837VALIDATER(parameters); VALIDATER(suite);
838VALIDATER(testlist); VALIDATER(varargslist);
839VALIDATER(fpdef); VALIDATER(fplist);
840VALIDATER(stmt); VALIDATER(simple_stmt);
841VALIDATER(expr_stmt); VALIDATER(power);
842VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000843VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000844VALIDATER(raise_stmt); VALIDATER(import_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000845VALIDATER(global_stmt); VALIDATER(list_if);
846VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000847VALIDATER(exec_stmt); VALIDATER(compound_stmt);
848VALIDATER(while); VALIDATER(for);
849VALIDATER(try); VALIDATER(except_clause);
850VALIDATER(test); VALIDATER(and_test);
851VALIDATER(not_test); VALIDATER(comparison);
852VALIDATER(comp_op); VALIDATER(expr);
853VALIDATER(xor_expr); VALIDATER(and_expr);
854VALIDATER(shift_expr); VALIDATER(arith_expr);
855VALIDATER(term); VALIDATER(factor);
856VALIDATER(atom); VALIDATER(lambdef);
857VALIDATER(trailer); VALIDATER(subscript);
858VALIDATER(subscriptlist); VALIDATER(sliceop);
859VALIDATER(exprlist); VALIDATER(dictmaker);
860VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000861VALIDATER(listmaker); VALIDATER(yield_stmt);
Guido van Rossum2d3b9862002-05-24 15:47:06 +0000862VALIDATER(testlist1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000863
Fred Drake0ac9b072000-09-12 21:58:06 +0000864#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000865
Fred Drakeff9ea482000-04-19 13:54:15 +0000866#define is_even(n) (((n) & 1) == 0)
867#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000868
869
870static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000871validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000872{
Fred Drake0ac9b072000-09-12 21:58:06 +0000873 if (TYPE(n) != t) {
874 PyErr_Format(parser_error, "Expected node type %d, got %d.",
875 t, TYPE(n));
876 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000877 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000878 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000879}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000880
881
Fred Drakee7ab64e2000-04-25 04:14:46 +0000882/* Verifies that the number of child nodes is exactly 'num', raising
883 * an exception if it isn't. The exception message does not indicate
884 * the exact number of nodes, allowing this to be used to raise the
885 * "right" exception when the wrong number of nodes is present in a
886 * specific variant of a statement's syntax. This is commonly used
887 * in that fashion.
888 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000889static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000890validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000891{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000892 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000893 PyErr_Format(parser_error,
894 "Illegal number of children for %s node.", name);
895 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000896 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000897 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000898}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000899
900
901static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000902validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000903{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000904 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000905 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000906
907 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000908 PyErr_Format(parser_error,
909 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000910 }
911 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000912}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000913
914
Guido van Rossum47478871996-08-21 14:32:37 +0000915/* X (',' X) [',']
916 */
917static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000918validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000919 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000920{
921 int nch = NCH(tree);
922 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000923 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000924
925 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000926 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000927 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000928 if (is_even(nch))
929 res = validate_comma(CHILD(tree, --nch));
930 if (res && nch > 1) {
931 int pos = 1;
932 for ( ; res && pos < nch; pos += 2)
933 res = (validate_comma(CHILD(tree, pos))
934 && vfunc(CHILD(tree, pos + 1)));
935 }
Guido van Rossum47478871996-08-21 14:32:37 +0000936 }
937 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000938}
Guido van Rossum47478871996-08-21 14:32:37 +0000939
940
Fred Drakecff283c2000-08-21 22:24:43 +0000941/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000942 *
943 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000944 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000945 */
Guido van Rossum47478871996-08-21 14:32:37 +0000946static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000947validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000948{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000949 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000950 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000951
Guido van Rossum3d602e31996-07-21 02:33:56 +0000952 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000953 res = (validate_name(CHILD(tree, 0), "class")
954 && validate_ntype(CHILD(tree, 1), NAME)
955 && validate_colon(CHILD(tree, nch - 2))
956 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000957 }
958 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000959 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000960 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000961 res = (validate_lparen(CHILD(tree, 2))
962 && validate_testlist(CHILD(tree, 3))
963 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000964 }
965 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000966}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000967
968
Guido van Rossum3d602e31996-07-21 02:33:56 +0000969/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +0000970 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +0000971 */
Guido van Rossum47478871996-08-21 14:32:37 +0000972static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000973validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000974{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000975 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000976 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +0000977 && (nch >= 4)
978 && validate_name(CHILD(tree, 0), "if")
979 && validate_test(CHILD(tree, 1))
980 && validate_colon(CHILD(tree, 2))
981 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000982
983 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000984 /* ... 'else' ':' suite */
985 res = (validate_name(CHILD(tree, nch - 3), "else")
986 && validate_colon(CHILD(tree, nch - 2))
987 && validate_suite(CHILD(tree, nch - 1)));
988 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000989 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000990 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000991 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000992 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +0000993 /* Will catch the case for nch < 4 */
994 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000995 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000996 /* ... ('elif' test ':' suite)+ ... */
997 int j = 4;
998 while ((j < nch) && res) {
999 res = (validate_name(CHILD(tree, j), "elif")
1000 && validate_colon(CHILD(tree, j + 2))
1001 && validate_test(CHILD(tree, j + 1))
1002 && validate_suite(CHILD(tree, j + 3)));
1003 j += 4;
1004 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001005 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001006 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001007}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001008
1009
Guido van Rossum3d602e31996-07-21 02:33:56 +00001010/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001011 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001012 *
1013 */
Guido van Rossum47478871996-08-21 14:32:37 +00001014static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001015validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001016{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001017 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001018 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001019
Guido van Rossum3d602e31996-07-21 02:33:56 +00001020 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001021 res = (validate_lparen(CHILD(tree, 0))
1022 && validate_rparen(CHILD(tree, nch - 1)));
1023 if (res && (nch == 3))
1024 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001025 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001026 else {
1027 (void) validate_numnodes(tree, 2, "parameters");
1028 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001029 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001030}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001031
1032
Fred Drakecff283c2000-08-21 22:24:43 +00001033/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001034 *
1035 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001036 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001037 * | NEWLINE INDENT stmt+ DEDENT
1038 */
Guido van Rossum47478871996-08-21 14:32:37 +00001039static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001040validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001041{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001042 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001043 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001044
Guido van Rossum3d602e31996-07-21 02:33:56 +00001045 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001046 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001047 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001048 /* NEWLINE INDENT stmt+ DEDENT */
1049 res = (validate_newline(CHILD(tree, 0))
1050 && validate_indent(CHILD(tree, 1))
1051 && validate_stmt(CHILD(tree, 2))
1052 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001053
Fred Drakeff9ea482000-04-19 13:54:15 +00001054 if (res && (nch > 4)) {
1055 int i = 3;
1056 --nch; /* forget the DEDENT */
1057 for ( ; res && (i < nch); ++i)
1058 res = validate_stmt(CHILD(tree, i));
1059 }
1060 else if (nch < 4)
1061 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001062 }
1063 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001064}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001065
1066
Guido van Rossum47478871996-08-21 14:32:37 +00001067static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001068validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001069{
Guido van Rossum47478871996-08-21 14:32:37 +00001070 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001071 validate_test, "testlist"));
1072}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001073
1074
Guido van Rossum1c917072001-10-15 15:44:05 +00001075static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001076validate_testlist1(node *tree)
1077{
1078 return (validate_repeating_list(tree, testlist1,
1079 validate_test, "testlist1"));
1080}
1081
1082
1083static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001084validate_testlist_safe(node *tree)
1085{
1086 return (validate_repeating_list(tree, testlist_safe,
1087 validate_test, "testlist_safe"));
1088}
1089
1090
Fred Drakecff283c2000-08-21 22:24:43 +00001091/* '*' NAME [',' '**' NAME] | '**' NAME
1092 */
1093static int
1094validate_varargslist_trailer(node *tree, int start)
1095{
1096 int nch = NCH(tree);
1097 int res = 0;
1098 int sym;
1099
1100 if (nch <= start) {
1101 err_string("expected variable argument trailer for varargslist");
1102 return 0;
1103 }
1104 sym = TYPE(CHILD(tree, start));
1105 if (sym == STAR) {
1106 /*
1107 * ('*' NAME [',' '**' NAME]
1108 */
1109 if (nch-start == 2)
1110 res = validate_name(CHILD(tree, start+1), NULL);
1111 else if (nch-start == 5)
1112 res = (validate_name(CHILD(tree, start+1), NULL)
1113 && validate_comma(CHILD(tree, start+2))
1114 && validate_doublestar(CHILD(tree, start+3))
1115 && validate_name(CHILD(tree, start+4), NULL));
1116 }
1117 else if (sym == DOUBLESTAR) {
1118 /*
1119 * '**' NAME
1120 */
1121 if (nch-start == 2)
1122 res = validate_name(CHILD(tree, start+1), NULL);
1123 }
1124 if (!res)
1125 err_string("illegal variable argument trailer for varargslist");
1126 return res;
1127}
1128
1129
1130/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001131 *
1132 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001133 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001134 * ('*' NAME [',' '**' NAME]
1135 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001136 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1137 *
1138 */
Guido van Rossum47478871996-08-21 14:32:37 +00001139static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001140validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001141{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001142 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001143 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001144 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001145
Fred Drakeb6429a22000-12-11 22:08:27 +00001146 if (!res)
1147 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001148 if (nch < 1) {
1149 err_string("varargslist missing child nodes");
1150 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001151 }
Fred Drakecff283c2000-08-21 22:24:43 +00001152 sym = TYPE(CHILD(tree, 0));
1153 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001154 /* whole thing matches:
1155 * '*' NAME [',' '**' NAME] | '**' NAME
1156 */
Fred Drakecff283c2000-08-21 22:24:43 +00001157 res = validate_varargslist_trailer(tree, 0);
1158 else if (sym == fpdef) {
1159 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001160
Fred Drakecff283c2000-08-21 22:24:43 +00001161 sym = TYPE(CHILD(tree, nch-1));
1162 if (sym == NAME) {
1163 /*
1164 * (fpdef ['=' test] ',')+
1165 * ('*' NAME [',' '**' NAME]
1166 * | '**' NAME)
1167 */
1168 /* skip over (fpdef ['=' test] ',')+ */
1169 while (res && (i+2 <= nch)) {
1170 res = validate_fpdef(CHILD(tree, i));
1171 ++i;
1172 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1173 res = (validate_equal(CHILD(tree, i))
1174 && validate_test(CHILD(tree, i+1)));
1175 if (res)
1176 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001177 }
Fred Drakecff283c2000-08-21 22:24:43 +00001178 if (res && i < nch) {
1179 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001180 ++i;
1181 if (res && i < nch
1182 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1183 || TYPE(CHILD(tree, i)) == STAR))
1184 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001185 }
1186 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001187 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1188 * i --^^^
1189 */
Fred Drakecff283c2000-08-21 22:24:43 +00001190 if (res)
1191 res = validate_varargslist_trailer(tree, i);
1192 }
1193 else {
1194 /*
1195 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1196 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001197 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001198 if (sym == COMMA) {
1199 res = validate_comma(CHILD(tree, nch-1));
1200 if (!res)
1201 return 0;
1202 --nch;
1203 }
1204 /*
1205 * fpdef ['=' test] (',' fpdef ['=' test])*
1206 */
1207 res = validate_fpdef(CHILD(tree, 0));
1208 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001209 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1210 res = (validate_equal(CHILD(tree, i))
1211 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001212 i += 2;
1213 }
1214 /*
1215 * ... (',' fpdef ['=' test])*
1216 * i ---^^^
1217 */
1218 while (res && (nch - i) >= 2) {
1219 res = (validate_comma(CHILD(tree, i))
1220 && validate_fpdef(CHILD(tree, i+1)));
1221 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001222 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1223 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001224 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001225 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001226 }
1227 }
1228 if (res && nch - i != 0) {
1229 res = 0;
1230 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001231 }
1232 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001233 }
Fred Drakecff283c2000-08-21 22:24:43 +00001234 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001235}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001236
1237
Fred Drakecff283c2000-08-21 22:24:43 +00001238/* list_iter: list_for | list_if
1239 */
1240static int
1241validate_list_iter(node *tree)
1242{
1243 int res = (validate_ntype(tree, list_iter)
1244 && validate_numnodes(tree, 1, "list_iter"));
1245 if (res && TYPE(CHILD(tree, 0)) == list_for)
1246 res = validate_list_for(CHILD(tree, 0));
1247 else
1248 res = validate_list_if(CHILD(tree, 0));
1249
1250 return res;
1251}
1252
1253/* list_for: 'for' exprlist 'in' testlist [list_iter]
1254 */
1255static int
1256validate_list_for(node *tree)
1257{
1258 int nch = NCH(tree);
1259 int res;
1260
1261 if (nch == 5)
1262 res = validate_list_iter(CHILD(tree, 4));
1263 else
1264 res = validate_numnodes(tree, 4, "list_for");
1265
1266 if (res)
1267 res = (validate_name(CHILD(tree, 0), "for")
1268 && validate_exprlist(CHILD(tree, 1))
1269 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001270 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001271
1272 return res;
1273}
1274
1275/* list_if: 'if' test [list_iter]
1276 */
1277static int
1278validate_list_if(node *tree)
1279{
1280 int nch = NCH(tree);
1281 int res;
1282
1283 if (nch == 3)
1284 res = validate_list_iter(CHILD(tree, 2));
1285 else
1286 res = validate_numnodes(tree, 2, "list_if");
1287
1288 if (res)
1289 res = (validate_name(CHILD(tree, 0), "if")
1290 && validate_test(CHILD(tree, 1)));
1291
1292 return res;
1293}
1294
1295
1296/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001297 *
1298 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001299 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001300 * | '(' fplist ')'
1301 */
Guido van Rossum47478871996-08-21 14:32:37 +00001302static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001303validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001304{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001305 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001306 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001307
Guido van Rossum3d602e31996-07-21 02:33:56 +00001308 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001309 if (nch == 1)
1310 res = validate_ntype(CHILD(tree, 0), NAME);
1311 else if (nch == 3)
1312 res = (validate_lparen(CHILD(tree, 0))
1313 && validate_fplist(CHILD(tree, 1))
1314 && validate_rparen(CHILD(tree, 2)));
1315 else
1316 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001317 }
1318 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001319}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001320
1321
Guido van Rossum47478871996-08-21 14:32:37 +00001322static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001323validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001324{
Guido van Rossum47478871996-08-21 14:32:37 +00001325 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001326 validate_fpdef, "fplist"));
1327}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001328
1329
Guido van Rossum3d602e31996-07-21 02:33:56 +00001330/* simple_stmt | compound_stmt
1331 *
1332 */
Guido van Rossum47478871996-08-21 14:32:37 +00001333static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001334validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001335{
1336 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001337 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001338
Guido van Rossum3d602e31996-07-21 02:33:56 +00001339 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001340 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001341
Fred Drakeff9ea482000-04-19 13:54:15 +00001342 if (TYPE(tree) == simple_stmt)
1343 res = validate_simple_stmt(tree);
1344 else
1345 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001346 }
1347 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001348}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001349
1350
Guido van Rossum3d602e31996-07-21 02:33:56 +00001351/* small_stmt (';' small_stmt)* [';'] NEWLINE
1352 *
1353 */
Guido van Rossum47478871996-08-21 14:32:37 +00001354static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001355validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001356{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001357 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001358 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001359 && (nch >= 2)
1360 && validate_small_stmt(CHILD(tree, 0))
1361 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001362
Guido van Rossum3d602e31996-07-21 02:33:56 +00001363 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001364 res = validate_numnodes(tree, 2, "simple_stmt");
1365 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001366 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001367 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001368 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001369 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001370
Fred Drakeff9ea482000-04-19 13:54:15 +00001371 for (i = 1; res && (i < nch); i += 2)
1372 res = (validate_semi(CHILD(tree, i))
1373 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001374 }
1375 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001376}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001377
1378
Guido van Rossum47478871996-08-21 14:32:37 +00001379static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001380validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001381{
1382 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001383 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001384
Fred Drake0ac9b072000-09-12 21:58:06 +00001385 if (res) {
1386 int ntype = TYPE(CHILD(tree, 0));
1387
1388 if ( (ntype == expr_stmt)
1389 || (ntype == print_stmt)
1390 || (ntype == del_stmt)
1391 || (ntype == pass_stmt)
1392 || (ntype == flow_stmt)
1393 || (ntype == import_stmt)
1394 || (ntype == global_stmt)
1395 || (ntype == assert_stmt)
1396 || (ntype == exec_stmt))
1397 res = validate_node(CHILD(tree, 0));
1398 else {
1399 res = 0;
1400 err_string("illegal small_stmt child type");
1401 }
1402 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001403 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001404 res = 0;
1405 PyErr_Format(parser_error,
1406 "Unrecognized child node of small_stmt: %d.",
1407 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001408 }
1409 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001410}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001411
1412
1413/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001414 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001415 */
Guido van Rossum47478871996-08-21 14:32:37 +00001416static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001417validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001418{
1419 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001420 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001421 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001422
1423 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001424 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001425
1426 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001427 ntype = TYPE(tree);
1428 if ( (ntype == if_stmt)
1429 || (ntype == while_stmt)
1430 || (ntype == for_stmt)
1431 || (ntype == try_stmt)
1432 || (ntype == funcdef)
1433 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001434 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001435 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001436 res = 0;
1437 PyErr_Format(parser_error,
1438 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001439 }
1440 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001441}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001442
1443
Guido van Rossum47478871996-08-21 14:32:37 +00001444static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001445validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001446{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001447 int j;
1448 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001449 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001450 && is_odd(nch)
1451 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001452
Fred Drake28f739a2000-08-25 22:42:40 +00001453 if (res && nch == 3
1454 && TYPE(CHILD(tree, 1)) == augassign) {
1455 res = (validate_numnodes(CHILD(tree, 1), 1, "augassign")
1456 && validate_testlist(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001457
Fred Drake28f739a2000-08-25 22:42:40 +00001458 if (res) {
1459 char *s = STR(CHILD(CHILD(tree, 1), 0));
1460
1461 res = (strcmp(s, "+=") == 0
1462 || strcmp(s, "-=") == 0
1463 || strcmp(s, "*=") == 0
1464 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001465 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001466 || strcmp(s, "%=") == 0
1467 || strcmp(s, "&=") == 0
1468 || strcmp(s, "|=") == 0
1469 || strcmp(s, "^=") == 0
1470 || strcmp(s, "<<=") == 0
1471 || strcmp(s, ">>=") == 0
1472 || strcmp(s, "**=") == 0);
1473 if (!res)
1474 err_string("illegal augmmented assignment operator");
1475 }
1476 }
1477 else {
1478 for (j = 1; res && (j < nch); j += 2)
1479 res = (validate_equal(CHILD(tree, j))
1480 && validate_testlist(CHILD(tree, j + 1)));
1481 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001482 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001483}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001484
1485
Guido van Rossum3d602e31996-07-21 02:33:56 +00001486/* print_stmt:
1487 *
Fred Drakecff283c2000-08-21 22:24:43 +00001488 * 'print' ( [ test (',' test)* [','] ]
1489 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001490 */
Guido van Rossum47478871996-08-21 14:32:37 +00001491static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001492validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001493{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001494 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001495 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001496 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001497 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001498
Fred Drakecff283c2000-08-21 22:24:43 +00001499 if (res && nch > 1) {
1500 int sym = TYPE(CHILD(tree, 1));
1501 int i = 1;
1502 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001503
Fred Drakecff283c2000-08-21 22:24:43 +00001504 if (sym == test)
1505 res = validate_test(CHILD(tree, i++));
1506 else {
1507 if (nch < 3)
1508 res = validate_numnodes(tree, 3, "print_stmt");
1509 else {
1510 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1511 && validate_test(CHILD(tree, i+1)));
1512 i += 2;
1513 allow_trailing_comma = 0;
1514 }
1515 }
1516 if (res) {
1517 /* ... (',' test)* [','] */
1518 while (res && i+2 <= nch) {
1519 res = (validate_comma(CHILD(tree, i))
1520 && validate_test(CHILD(tree, i+1)));
1521 allow_trailing_comma = 1;
1522 i += 2;
1523 }
1524 if (res && !allow_trailing_comma)
1525 res = validate_numnodes(tree, i, "print_stmt");
1526 else if (res && i < nch)
1527 res = validate_comma(CHILD(tree, i));
1528 }
1529 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001530 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001531}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001532
1533
Guido van Rossum47478871996-08-21 14:32:37 +00001534static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001535validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001536{
1537 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001538 && validate_name(CHILD(tree, 0), "del")
1539 && validate_exprlist(CHILD(tree, 1)));
1540}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001541
1542
Guido van Rossum47478871996-08-21 14:32:37 +00001543static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001544validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001545{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001546 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001547 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001548 && ((nch == 1) || (nch == 2))
1549 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001550
Guido van Rossum3d602e31996-07-21 02:33:56 +00001551 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001552 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001553
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001554 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001555}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001556
1557
Guido van Rossum47478871996-08-21 14:32:37 +00001558static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001559validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001560{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001561 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001562 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001563 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001564
Guido van Rossum3d602e31996-07-21 02:33:56 +00001565 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001566 res = validate_name(CHILD(tree, 0), "raise");
1567 if (res && (nch >= 2))
1568 res = validate_test(CHILD(tree, 1));
1569 if (res && nch > 2) {
1570 res = (validate_comma(CHILD(tree, 2))
1571 && validate_test(CHILD(tree, 3)));
1572 if (res && (nch > 4))
1573 res = (validate_comma(CHILD(tree, 4))
1574 && validate_test(CHILD(tree, 5)));
1575 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001576 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001577 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001578 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001579 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001580 res = (validate_comma(CHILD(tree, 2))
1581 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001582
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001583 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001584}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001585
1586
Fred Drake02126f22001-07-17 02:59:15 +00001587/* yield_stmt: 'yield' testlist
1588 */
1589static int
1590validate_yield_stmt(node *tree)
1591{
1592 return (validate_ntype(tree, yield_stmt)
1593 && validate_numnodes(tree, 2, "yield_stmt")
1594 && validate_name(CHILD(tree, 0), "yield")
1595 && validate_testlist(CHILD(tree, 1)));
1596}
1597
1598
Fred Drakecff283c2000-08-21 22:24:43 +00001599static int
1600validate_import_as_name(node *tree)
1601{
1602 int nch = NCH(tree);
1603 int ok = validate_ntype(tree, import_as_name);
1604
1605 if (ok) {
1606 if (nch == 1)
1607 ok = validate_name(CHILD(tree, 0), NULL);
1608 else if (nch == 3)
1609 ok = (validate_name(CHILD(tree, 0), NULL)
1610 && validate_name(CHILD(tree, 1), "as")
1611 && validate_name(CHILD(tree, 2), NULL));
1612 else
1613 ok = validate_numnodes(tree, 3, "import_as_name");
1614 }
1615 return ok;
1616}
1617
1618
Fred Drake71137082001-01-07 05:59:59 +00001619/* dotted_name: NAME ("." NAME)*
1620 */
1621static int
1622validate_dotted_name(node *tree)
1623{
1624 int nch = NCH(tree);
1625 int res = (validate_ntype(tree, dotted_name)
1626 && is_odd(nch)
1627 && validate_name(CHILD(tree, 0), NULL));
1628 int i;
1629
1630 for (i = 1; res && (i < nch); i += 2) {
1631 res = (validate_dot(CHILD(tree, i))
1632 && validate_name(CHILD(tree, i+1), NULL));
1633 }
1634 return res;
1635}
1636
1637
Fred Drakecff283c2000-08-21 22:24:43 +00001638/* dotted_as_name: dotted_name [NAME NAME]
1639 */
1640static int
1641validate_dotted_as_name(node *tree)
1642{
1643 int nch = NCH(tree);
1644 int res = validate_ntype(tree, dotted_as_name);
1645
1646 if (res) {
1647 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001648 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001649 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001650 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001651 && validate_name(CHILD(tree, 1), "as")
1652 && validate_name(CHILD(tree, 2), NULL));
1653 else {
1654 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001655 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001656 }
1657 }
1658 return res;
1659}
1660
1661
Guido van Rossum3d602e31996-07-21 02:33:56 +00001662/* import_stmt:
1663 *
Fred Drakecff283c2000-08-21 22:24:43 +00001664 * 'import' dotted_as_name (',' dotted_as_name)*
1665 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001666 */
Guido van Rossum47478871996-08-21 14:32:37 +00001667static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001668validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001669{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001670 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001671 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001672 && (nch >= 2) && is_even(nch)
Fred Drakecff283c2000-08-21 22:24:43 +00001673 && validate_ntype(CHILD(tree, 0), NAME));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001674
1675 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001676 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001677
Fred Drakecff283c2000-08-21 22:24:43 +00001678 res = validate_dotted_as_name(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00001679 for (j = 2; res && (j < nch); j += 2)
1680 res = (validate_comma(CHILD(tree, j))
Fred Drake71137082001-01-07 05:59:59 +00001681 && validate_dotted_as_name(CHILD(tree, j + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001682 }
Fred Drakecff283c2000-08-21 22:24:43 +00001683 else if (res && (res = validate_name(CHILD(tree, 0), "from"))) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001684 res = ((nch >= 4) && is_even(nch)
Fred Drake71137082001-01-07 05:59:59 +00001685 && validate_dotted_name(CHILD(tree, 1))
1686 && validate_name(CHILD(tree, 2), "import"));
Fred Drakeff9ea482000-04-19 13:54:15 +00001687 if (nch == 4) {
Fred Drakecff283c2000-08-21 22:24:43 +00001688 if (TYPE(CHILD(tree, 3)) == import_as_name)
1689 res = validate_import_as_name(CHILD(tree, 3));
1690 else
1691 res = validate_star(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001692 }
1693 else {
Fred Drakecff283c2000-08-21 22:24:43 +00001694 /* 'from' dotted_name 'import' import_as_name
1695 * (',' import_as_name)+
1696 */
Fred Drakeff9ea482000-04-19 13:54:15 +00001697 int j;
Fred Drakecff283c2000-08-21 22:24:43 +00001698 res = validate_import_as_name(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001699 for (j = 4; res && (j < nch); j += 2)
1700 res = (validate_comma(CHILD(tree, j))
Fred Drakecff283c2000-08-21 22:24:43 +00001701 && validate_import_as_name(CHILD(tree, j + 1)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001702 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001703 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001704 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001705 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001706
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001707 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001708}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001709
1710
Guido van Rossum47478871996-08-21 14:32:37 +00001711static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001712validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001713{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001714 int j;
1715 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001716 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001717 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001718
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001719 if (!res && !PyErr_Occurred())
1720 err_string("illegal global statement");
1721
Guido van Rossum3d602e31996-07-21 02:33:56 +00001722 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001723 res = (validate_name(CHILD(tree, 0), "global")
1724 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001725 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001726 res = (validate_comma(CHILD(tree, j))
1727 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001728
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001729 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001730}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001731
1732
Guido van Rossum3d602e31996-07-21 02:33:56 +00001733/* exec_stmt:
1734 *
1735 * 'exec' expr ['in' test [',' test]]
1736 */
Guido van Rossum47478871996-08-21 14:32:37 +00001737static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001738validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001739{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001740 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001741 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001742 && ((nch == 2) || (nch == 4) || (nch == 6))
1743 && validate_name(CHILD(tree, 0), "exec")
1744 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001745
Guido van Rossum3d602e31996-07-21 02:33:56 +00001746 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001747 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001748 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001749 res = (validate_name(CHILD(tree, 2), "in")
1750 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001751 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001752 res = (validate_comma(CHILD(tree, 4))
1753 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001754
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001755 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001756}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001757
1758
Guido van Rossum925e5471997-04-02 05:32:13 +00001759/* assert_stmt:
1760 *
1761 * 'assert' test [',' test]
1762 */
1763static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001764validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001765{
1766 int nch = NCH(tree);
1767 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001768 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001769 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001770 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001771
1772 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001773 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001774 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001775 res = (validate_comma(CHILD(tree, 2))
1776 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001777
1778 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001779}
Guido van Rossum925e5471997-04-02 05:32:13 +00001780
1781
Guido van Rossum47478871996-08-21 14:32:37 +00001782static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001783validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001784{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001785 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001786 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001787 && ((nch == 4) || (nch == 7))
1788 && validate_name(CHILD(tree, 0), "while")
1789 && validate_test(CHILD(tree, 1))
1790 && validate_colon(CHILD(tree, 2))
1791 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001792
Guido van Rossum3d602e31996-07-21 02:33:56 +00001793 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001794 res = (validate_name(CHILD(tree, 4), "else")
1795 && validate_colon(CHILD(tree, 5))
1796 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001797
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001798 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001799}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001800
1801
Guido van Rossum47478871996-08-21 14:32:37 +00001802static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001803validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001804{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001805 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001806 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001807 && ((nch == 6) || (nch == 9))
1808 && validate_name(CHILD(tree, 0), "for")
1809 && validate_exprlist(CHILD(tree, 1))
1810 && validate_name(CHILD(tree, 2), "in")
1811 && validate_testlist(CHILD(tree, 3))
1812 && validate_colon(CHILD(tree, 4))
1813 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001814
Guido van Rossum3d602e31996-07-21 02:33:56 +00001815 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001816 res = (validate_name(CHILD(tree, 6), "else")
1817 && validate_colon(CHILD(tree, 7))
1818 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001819
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001820 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001821}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001822
1823
Guido van Rossum3d602e31996-07-21 02:33:56 +00001824/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001825 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001826 * | 'try' ':' suite 'finally' ':' suite
1827 *
1828 */
Guido van Rossum47478871996-08-21 14:32:37 +00001829static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001830validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001831{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001832 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001833 int pos = 3;
1834 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001835 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001836
1837 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001838 res = (validate_name(CHILD(tree, 0), "try")
1839 && validate_colon(CHILD(tree, 1))
1840 && validate_suite(CHILD(tree, 2))
1841 && validate_colon(CHILD(tree, nch - 2))
1842 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001843 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001844 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001845 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1846 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001847
1848 PyErr_Format(parser_error,
1849 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001850 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001851 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001852 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001853 res = (validate_except_clause(CHILD(tree, pos))
1854 && validate_colon(CHILD(tree, pos + 1))
1855 && validate_suite(CHILD(tree, pos + 2)));
1856 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001857 }
1858 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001859 res = validate_ntype(CHILD(tree, pos), NAME);
1860 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1861 res = (validate_numnodes(tree, 6, "try/finally")
1862 && validate_colon(CHILD(tree, 4))
1863 && validate_suite(CHILD(tree, 5)));
1864 else if (res) {
1865 if (nch == (pos + 3)) {
1866 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1867 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1868 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001869 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001870 }
1871 else if (nch == (pos + 6)) {
1872 res = (validate_name(CHILD(tree, pos), "except")
1873 && validate_colon(CHILD(tree, pos + 1))
1874 && validate_suite(CHILD(tree, pos + 2))
1875 && validate_name(CHILD(tree, pos + 3), "else"));
1876 }
1877 else
1878 res = validate_numnodes(tree, pos + 3, "try/except");
1879 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001880 }
1881 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001882}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001883
1884
Guido van Rossum47478871996-08-21 14:32:37 +00001885static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001886validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001887{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001888 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001889 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001890 && ((nch == 1) || (nch == 2) || (nch == 4))
1891 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001892
Guido van Rossum3d602e31996-07-21 02:33:56 +00001893 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001894 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001895 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001896 res = (validate_comma(CHILD(tree, 2))
1897 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001898
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001899 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001900}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001901
1902
Guido van Rossum47478871996-08-21 14:32:37 +00001903static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001904validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001905{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001906 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001907 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001908
Guido van Rossum3d602e31996-07-21 02:33:56 +00001909 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001910 res = ((nch == 1)
1911 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001912 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001913 int pos;
1914 res = validate_and_test(CHILD(tree, 0));
1915 for (pos = 1; res && (pos < nch); pos += 2)
1916 res = (validate_name(CHILD(tree, pos), "or")
1917 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001918 }
1919 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001920}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001921
1922
Guido van Rossum47478871996-08-21 14:32:37 +00001923static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001924validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001925{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001926 int pos;
1927 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001928 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001929 && is_odd(nch)
1930 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001931
Guido van Rossum3d602e31996-07-21 02:33:56 +00001932 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001933 res = (validate_name(CHILD(tree, pos), "and")
1934 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001935
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001936 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001937}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001938
1939
Guido van Rossum47478871996-08-21 14:32:37 +00001940static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001941validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001942{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001943 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001944 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001945
Guido van Rossum3d602e31996-07-21 02:33:56 +00001946 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001947 if (nch == 2)
1948 res = (validate_name(CHILD(tree, 0), "not")
1949 && validate_not_test(CHILD(tree, 1)));
1950 else if (nch == 1)
1951 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001952 }
1953 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001954}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001955
1956
Guido van Rossum47478871996-08-21 14:32:37 +00001957static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001958validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001959{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001960 int pos;
1961 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001962 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001963 && is_odd(nch)
1964 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001965
Guido van Rossum3d602e31996-07-21 02:33:56 +00001966 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001967 res = (validate_comp_op(CHILD(tree, pos))
1968 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001969
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001970 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001971}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001972
1973
Guido van Rossum47478871996-08-21 14:32:37 +00001974static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001975validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001976{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001977 int res = 0;
1978 int nch = NCH(tree);
1979
Guido van Rossum3d602e31996-07-21 02:33:56 +00001980 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001981 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001982 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001983 /*
1984 * Only child will be a terminal with a well-defined symbolic name
1985 * or a NAME with a string of either 'is' or 'in'
1986 */
1987 tree = CHILD(tree, 0);
1988 switch (TYPE(tree)) {
1989 case LESS:
1990 case GREATER:
1991 case EQEQUAL:
1992 case EQUAL:
1993 case LESSEQUAL:
1994 case GREATEREQUAL:
1995 case NOTEQUAL:
1996 res = 1;
1997 break;
1998 case NAME:
1999 res = ((strcmp(STR(tree), "in") == 0)
2000 || (strcmp(STR(tree), "is") == 0));
2001 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002002 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002003 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002004 }
2005 break;
2006 default:
Fred Drake661ea262000-10-24 19:57:45 +00002007 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002008 break;
2009 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002010 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002011 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002012 res = (validate_ntype(CHILD(tree, 0), NAME)
2013 && validate_ntype(CHILD(tree, 1), NAME)
2014 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2015 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2016 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2017 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2018 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002019 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002020 }
2021 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002022}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002023
2024
Guido van Rossum47478871996-08-21 14:32:37 +00002025static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002026validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002027{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002028 int j;
2029 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002030 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002031 && is_odd(nch)
2032 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002033
Guido van Rossum3d602e31996-07-21 02:33:56 +00002034 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002035 res = (validate_xor_expr(CHILD(tree, j))
2036 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002037
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002038 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002039}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002040
2041
Guido van Rossum47478871996-08-21 14:32:37 +00002042static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002043validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002044{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002045 int j;
2046 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002047 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002048 && is_odd(nch)
2049 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002050
Guido van Rossum3d602e31996-07-21 02:33:56 +00002051 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002052 res = (validate_circumflex(CHILD(tree, j - 1))
2053 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002054
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002055 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002056}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002057
2058
Guido van Rossum47478871996-08-21 14:32:37 +00002059static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002060validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002061{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002062 int pos;
2063 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002064 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002065 && is_odd(nch)
2066 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002067
Guido van Rossum3d602e31996-07-21 02:33:56 +00002068 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002069 res = (validate_ampersand(CHILD(tree, pos))
2070 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002071
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002072 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002073}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002074
2075
2076static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002077validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002078 {
2079 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002080 int nch = NCH(tree);
2081 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002082 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002083
Guido van Rossum3d602e31996-07-21 02:33:56 +00002084 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002085 if (TYPE(CHILD(tree, pos)) != op1)
2086 res = validate_ntype(CHILD(tree, pos), op2);
2087 if (res)
2088 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002089 }
2090 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002091}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002092
2093
Guido van Rossum47478871996-08-21 14:32:37 +00002094static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002095validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002096{
2097 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002098 && validate_chain_two_ops(tree, validate_arith_expr,
2099 LEFTSHIFT, RIGHTSHIFT));
2100}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002101
2102
Guido van Rossum47478871996-08-21 14:32:37 +00002103static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002104validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002105{
2106 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002107 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2108}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002109
2110
Guido van Rossum47478871996-08-21 14:32:37 +00002111static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002112validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002113{
2114 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002115 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002116 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002117 && is_odd(nch)
2118 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002119
Guido van Rossum3d602e31996-07-21 02:33:56 +00002120 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002121 res = (((TYPE(CHILD(tree, pos)) == STAR)
2122 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002123 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002124 || (TYPE(CHILD(tree, pos)) == PERCENT))
2125 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002126
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002127 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002128}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002129
2130
Guido van Rossum3d602e31996-07-21 02:33:56 +00002131/* factor:
2132 *
2133 * factor: ('+'|'-'|'~') factor | power
2134 */
Guido van Rossum47478871996-08-21 14:32:37 +00002135static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002136validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002137{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002138 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002139 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002140 && (((nch == 2)
2141 && ((TYPE(CHILD(tree, 0)) == PLUS)
2142 || (TYPE(CHILD(tree, 0)) == MINUS)
2143 || (TYPE(CHILD(tree, 0)) == TILDE))
2144 && validate_factor(CHILD(tree, 1)))
2145 || ((nch == 1)
2146 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002147 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002148}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002149
2150
Guido van Rossum3d602e31996-07-21 02:33:56 +00002151/* power:
2152 *
2153 * power: atom trailer* ('**' factor)*
2154 */
Guido van Rossum47478871996-08-21 14:32:37 +00002155static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002156validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002157{
2158 int pos = 1;
2159 int nch = NCH(tree);
2160 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002161 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002162
2163 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002164 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002165 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002166 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002167 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002168 return (0);
2169 }
2170 for ( ; res && (pos < (nch - 1)); pos += 2)
2171 res = (validate_doublestar(CHILD(tree, pos))
2172 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002173 }
2174 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002175}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002176
2177
Guido van Rossum47478871996-08-21 14:32:37 +00002178static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002179validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002180{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002181 int pos;
2182 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002183 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002184
Fred Drakecff283c2000-08-21 22:24:43 +00002185 if (res && nch < 1)
2186 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002187 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002188 switch (TYPE(CHILD(tree, 0))) {
2189 case LPAR:
2190 res = ((nch <= 3)
2191 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002192
Fred Drakeff9ea482000-04-19 13:54:15 +00002193 if (res && (nch == 3))
2194 res = validate_testlist(CHILD(tree, 1));
2195 break;
2196 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002197 if (nch == 2)
2198 res = validate_ntype(CHILD(tree, 1), RSQB);
2199 else if (nch == 3)
2200 res = (validate_listmaker(CHILD(tree, 1))
2201 && validate_ntype(CHILD(tree, 2), RSQB));
2202 else {
2203 res = 0;
2204 err_string("illegal list display atom");
2205 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002206 break;
2207 case LBRACE:
2208 res = ((nch <= 3)
2209 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002210
Fred Drakeff9ea482000-04-19 13:54:15 +00002211 if (res && (nch == 3))
2212 res = validate_dictmaker(CHILD(tree, 1));
2213 break;
2214 case BACKQUOTE:
2215 res = ((nch == 3)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002216 && validate_testlist1(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002217 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2218 break;
2219 case NAME:
2220 case NUMBER:
2221 res = (nch == 1);
2222 break;
2223 case STRING:
2224 for (pos = 1; res && (pos < nch); ++pos)
2225 res = validate_ntype(CHILD(tree, pos), STRING);
2226 break;
2227 default:
2228 res = 0;
2229 break;
2230 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002231 }
2232 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002233}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002234
2235
Fred Drake85bf3bb2000-08-23 15:35:26 +00002236/* listmaker:
2237 * test ( list_for | (',' test)* [','] )
2238 */
Fred Drakecff283c2000-08-21 22:24:43 +00002239static int
2240validate_listmaker(node *tree)
2241{
2242 int nch = NCH(tree);
2243 int ok = nch;
2244
2245 if (nch == 0)
2246 err_string("missing child nodes of listmaker");
2247 else
2248 ok = validate_test(CHILD(tree, 0));
2249
2250 /*
2251 * list_iter | (',' test)* [',']
2252 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002253 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2254 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002255 else {
2256 /* (',' test)* [','] */
2257 int i = 1;
2258 while (ok && nch - i >= 2) {
2259 ok = (validate_comma(CHILD(tree, i))
2260 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002261 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002262 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002263 if (ok && i == nch-1)
2264 ok = validate_comma(CHILD(tree, i));
2265 else if (i != nch) {
2266 ok = 0;
2267 err_string("illegal trailing nodes for listmaker");
2268 }
Fred Drakecff283c2000-08-21 22:24:43 +00002269 }
2270 return ok;
2271}
2272
2273
Guido van Rossum3d602e31996-07-21 02:33:56 +00002274/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002275 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002276 *
2277 */
Guido van Rossum47478871996-08-21 14:32:37 +00002278static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002279validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002280{
2281 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002282 && validate_numnodes(tree, 5, "funcdef")
2283 && validate_name(CHILD(tree, 0), "def")
2284 && validate_ntype(CHILD(tree, 1), NAME)
2285 && validate_colon(CHILD(tree, 3))
2286 && validate_parameters(CHILD(tree, 2))
2287 && validate_suite(CHILD(tree, 4)));
2288}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002289
2290
Guido van Rossum47478871996-08-21 14:32:37 +00002291static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002292validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002293{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002294 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002295 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002296 && ((nch == 3) || (nch == 4))
2297 && validate_name(CHILD(tree, 0), "lambda")
2298 && validate_colon(CHILD(tree, nch - 2))
2299 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002300
Guido van Rossum3d602e31996-07-21 02:33:56 +00002301 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002302 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002303 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002304 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002305
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002306 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002307}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002308
2309
Guido van Rossum3d602e31996-07-21 02:33:56 +00002310/* arglist:
2311 *
Fred Drakecff283c2000-08-21 22:24:43 +00002312 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002313 */
Guido van Rossum47478871996-08-21 14:32:37 +00002314static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002315validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002316{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002317 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002318 int i = 0;
2319 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002320
2321 if (nch <= 0)
2322 /* raise the right error from having an invalid number of children */
2323 return validate_numnodes(tree, nch + 1, "arglist");
2324
Fred Drakecff283c2000-08-21 22:24:43 +00002325 while (ok && nch-i >= 2) {
2326 /* skip leading (argument ',') */
2327 ok = (validate_argument(CHILD(tree, i))
2328 && validate_comma(CHILD(tree, i+1)));
2329 if (ok)
2330 i += 2;
2331 else
2332 PyErr_Clear();
2333 }
2334 ok = 1;
2335 if (nch-i > 0) {
2336 /*
2337 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002338 */
Fred Drakecff283c2000-08-21 22:24:43 +00002339 int sym = TYPE(CHILD(tree, i));
2340
2341 if (sym == argument) {
2342 ok = validate_argument(CHILD(tree, i));
2343 if (ok && i+1 != nch) {
2344 err_string("illegal arglist specification"
2345 " (extra stuff on end)");
2346 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002347 }
Fred Drakecff283c2000-08-21 22:24:43 +00002348 }
2349 else if (sym == STAR) {
2350 ok = validate_star(CHILD(tree, i));
2351 if (ok && (nch-i == 2))
2352 ok = validate_test(CHILD(tree, i+1));
2353 else if (ok && (nch-i == 5))
2354 ok = (validate_test(CHILD(tree, i+1))
2355 && validate_comma(CHILD(tree, i+2))
2356 && validate_doublestar(CHILD(tree, i+3))
2357 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002358 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002359 err_string("illegal use of '*' in arglist");
2360 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002361 }
2362 }
Fred Drakecff283c2000-08-21 22:24:43 +00002363 else if (sym == DOUBLESTAR) {
2364 if (nch-i == 2)
2365 ok = (validate_doublestar(CHILD(tree, i))
2366 && validate_test(CHILD(tree, i+1)));
2367 else {
2368 err_string("illegal use of '**' in arglist");
2369 ok = 0;
2370 }
2371 }
2372 else {
2373 err_string("illegal arglist specification");
2374 ok = 0;
2375 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002376 }
2377 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002378}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002379
2380
2381
2382/* argument:
2383 *
2384 * [test '='] test
2385 */
Guido van Rossum47478871996-08-21 14:32:37 +00002386static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002387validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002388{
2389 int nch = NCH(tree);
2390 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002391 && ((nch == 1) || (nch == 3))
2392 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002393
2394 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002395 res = (validate_equal(CHILD(tree, 1))
2396 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002397
2398 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002399}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002400
2401
2402
2403/* trailer:
2404 *
Guido van Rossum47478871996-08-21 14:32:37 +00002405 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002406 */
Guido van Rossum47478871996-08-21 14:32:37 +00002407static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002408validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002409{
2410 int nch = NCH(tree);
2411 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002412
2413 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002414 switch (TYPE(CHILD(tree, 0))) {
2415 case LPAR:
2416 res = validate_rparen(CHILD(tree, nch - 1));
2417 if (res && (nch == 3))
2418 res = validate_arglist(CHILD(tree, 1));
2419 break;
2420 case LSQB:
2421 res = (validate_numnodes(tree, 3, "trailer")
2422 && validate_subscriptlist(CHILD(tree, 1))
2423 && validate_ntype(CHILD(tree, 2), RSQB));
2424 break;
2425 case DOT:
2426 res = (validate_numnodes(tree, 2, "trailer")
2427 && validate_ntype(CHILD(tree, 1), NAME));
2428 break;
2429 default:
2430 res = 0;
2431 break;
2432 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002433 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002434 else {
2435 (void) validate_numnodes(tree, 2, "trailer");
2436 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002437 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002438}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002439
2440
Guido van Rossum47478871996-08-21 14:32:37 +00002441/* subscriptlist:
2442 *
2443 * subscript (',' subscript)* [',']
2444 */
2445static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002446validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002447{
2448 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002449 validate_subscript, "subscriptlist"));
2450}
Guido van Rossum47478871996-08-21 14:32:37 +00002451
2452
Guido van Rossum3d602e31996-07-21 02:33:56 +00002453/* subscript:
2454 *
Guido van Rossum47478871996-08-21 14:32:37 +00002455 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002456 */
Guido van Rossum47478871996-08-21 14:32:37 +00002457static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002458validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002459{
Guido van Rossum47478871996-08-21 14:32:37 +00002460 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002461 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002462 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002463
Guido van Rossum47478871996-08-21 14:32:37 +00002464 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002465 if (!PyErr_Occurred())
2466 err_string("invalid number of arguments for subscript node");
2467 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002468 }
Guido van Rossum47478871996-08-21 14:32:37 +00002469 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002470 /* take care of ('.' '.' '.') possibility */
2471 return (validate_numnodes(tree, 3, "subscript")
2472 && validate_dot(CHILD(tree, 0))
2473 && validate_dot(CHILD(tree, 1))
2474 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002475 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002476 if (TYPE(CHILD(tree, 0)) == test)
2477 res = validate_test(CHILD(tree, 0));
2478 else
2479 res = validate_colon(CHILD(tree, 0));
2480 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002481 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002482 /* Must be [test] ':' [test] [sliceop],
2483 * but at least one of the optional components will
2484 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002485 */
2486 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002487 res = validate_test(CHILD(tree, 0));
2488 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002489 }
2490 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002491 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002492 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002493 int rem = nch - ++offset;
2494 if (rem) {
2495 if (TYPE(CHILD(tree, offset)) == test) {
2496 res = validate_test(CHILD(tree, offset));
2497 ++offset;
2498 --rem;
2499 }
2500 if (res && rem)
2501 res = validate_sliceop(CHILD(tree, offset));
2502 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002503 }
2504 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002505}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002506
2507
Guido van Rossum47478871996-08-21 14:32:37 +00002508static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002509validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002510{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002511 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002512 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002513 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002514 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002515 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002516 }
Guido van Rossum47478871996-08-21 14:32:37 +00002517 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002518 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002519 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002520 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002521
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002522 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002523}
Guido van Rossum47478871996-08-21 14:32:37 +00002524
2525
2526static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002527validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002528{
2529 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002530 validate_expr, "exprlist"));
2531}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002532
2533
Guido van Rossum47478871996-08-21 14:32:37 +00002534static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002535validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002536{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002537 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002538 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002539 && (nch >= 3)
2540 && validate_test(CHILD(tree, 0))
2541 && validate_colon(CHILD(tree, 1))
2542 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002543
Guido van Rossum3d602e31996-07-21 02:33:56 +00002544 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002545 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002546 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002547 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002548
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002549 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002550 int pos = 3;
2551 /* ( ',' test ':' test )* */
2552 while (res && (pos < nch)) {
2553 res = (validate_comma(CHILD(tree, pos))
2554 && validate_test(CHILD(tree, pos + 1))
2555 && validate_colon(CHILD(tree, pos + 2))
2556 && validate_test(CHILD(tree, pos + 3)));
2557 pos += 4;
2558 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002559 }
2560 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002561}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002562
2563
Guido van Rossum47478871996-08-21 14:32:37 +00002564static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002565validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002566{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002567 int pos;
2568 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002569 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002570 && (nch >= 2)
2571 && validate_testlist(CHILD(tree, 0))
2572 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002573
Guido van Rossum3d602e31996-07-21 02:33:56 +00002574 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002575 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002576
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002577 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002578}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002579
2580
Guido van Rossum47478871996-08-21 14:32:37 +00002581static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002582validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002583{
Fred Drakeff9ea482000-04-19 13:54:15 +00002584 int nch = 0; /* num. children on current node */
2585 int res = 1; /* result value */
2586 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002587
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002588 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002589 nch = NCH(tree);
2590 next = 0;
2591 switch (TYPE(tree)) {
2592 /*
2593 * Definition nodes.
2594 */
2595 case funcdef:
2596 res = validate_funcdef(tree);
2597 break;
2598 case classdef:
2599 res = validate_class(tree);
2600 break;
2601 /*
2602 * "Trivial" parse tree nodes.
2603 * (Why did I call these trivial?)
2604 */
2605 case stmt:
2606 res = validate_stmt(tree);
2607 break;
2608 case small_stmt:
2609 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002610 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002611 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2612 */
2613 res = validate_small_stmt(tree);
2614 break;
2615 case flow_stmt:
2616 res = (validate_numnodes(tree, 1, "flow_stmt")
2617 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2618 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002619 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002620 || (TYPE(CHILD(tree, 0)) == return_stmt)
2621 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2622 if (res)
2623 next = CHILD(tree, 0);
2624 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002625 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002626 break;
Fred Drake02126f22001-07-17 02:59:15 +00002627 case yield_stmt:
2628 res = validate_yield_stmt(tree);
2629 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002630 /*
2631 * Compound statements.
2632 */
2633 case simple_stmt:
2634 res = validate_simple_stmt(tree);
2635 break;
2636 case compound_stmt:
2637 res = validate_compound_stmt(tree);
2638 break;
2639 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002640 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002641 */
2642 case expr_stmt:
2643 res = validate_expr_stmt(tree);
2644 break;
2645 case print_stmt:
2646 res = validate_print_stmt(tree);
2647 break;
2648 case del_stmt:
2649 res = validate_del_stmt(tree);
2650 break;
2651 case pass_stmt:
2652 res = (validate_numnodes(tree, 1, "pass")
2653 && validate_name(CHILD(tree, 0), "pass"));
2654 break;
2655 case break_stmt:
2656 res = (validate_numnodes(tree, 1, "break")
2657 && validate_name(CHILD(tree, 0), "break"));
2658 break;
2659 case continue_stmt:
2660 res = (validate_numnodes(tree, 1, "continue")
2661 && validate_name(CHILD(tree, 0), "continue"));
2662 break;
2663 case return_stmt:
2664 res = validate_return_stmt(tree);
2665 break;
2666 case raise_stmt:
2667 res = validate_raise_stmt(tree);
2668 break;
2669 case import_stmt:
2670 res = validate_import_stmt(tree);
2671 break;
2672 case global_stmt:
2673 res = validate_global_stmt(tree);
2674 break;
2675 case exec_stmt:
2676 res = validate_exec_stmt(tree);
2677 break;
2678 case assert_stmt:
2679 res = validate_assert_stmt(tree);
2680 break;
2681 case if_stmt:
2682 res = validate_if(tree);
2683 break;
2684 case while_stmt:
2685 res = validate_while(tree);
2686 break;
2687 case for_stmt:
2688 res = validate_for(tree);
2689 break;
2690 case try_stmt:
2691 res = validate_try(tree);
2692 break;
2693 case suite:
2694 res = validate_suite(tree);
2695 break;
2696 /*
2697 * Expression nodes.
2698 */
2699 case testlist:
2700 res = validate_testlist(tree);
2701 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002702 case testlist1:
2703 res = validate_testlist1(tree);
2704 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002705 case test:
2706 res = validate_test(tree);
2707 break;
2708 case and_test:
2709 res = validate_and_test(tree);
2710 break;
2711 case not_test:
2712 res = validate_not_test(tree);
2713 break;
2714 case comparison:
2715 res = validate_comparison(tree);
2716 break;
2717 case exprlist:
2718 res = validate_exprlist(tree);
2719 break;
2720 case comp_op:
2721 res = validate_comp_op(tree);
2722 break;
2723 case expr:
2724 res = validate_expr(tree);
2725 break;
2726 case xor_expr:
2727 res = validate_xor_expr(tree);
2728 break;
2729 case and_expr:
2730 res = validate_and_expr(tree);
2731 break;
2732 case shift_expr:
2733 res = validate_shift_expr(tree);
2734 break;
2735 case arith_expr:
2736 res = validate_arith_expr(tree);
2737 break;
2738 case term:
2739 res = validate_term(tree);
2740 break;
2741 case factor:
2742 res = validate_factor(tree);
2743 break;
2744 case power:
2745 res = validate_power(tree);
2746 break;
2747 case atom:
2748 res = validate_atom(tree);
2749 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002750
Fred Drakeff9ea482000-04-19 13:54:15 +00002751 default:
2752 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002753 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002754 res = 0;
2755 break;
2756 }
2757 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002758 }
2759 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002760}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002761
2762
Guido van Rossum47478871996-08-21 14:32:37 +00002763static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002764validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002765{
2766 int res = validate_eval_input(tree);
2767
2768 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002769 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002770
2771 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002772}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002773
2774
Guido van Rossum3d602e31996-07-21 02:33:56 +00002775/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002776 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002777 */
Guido van Rossum47478871996-08-21 14:32:37 +00002778static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002779validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002780{
Fred Drakec2683dd2001-07-17 19:32:05 +00002781 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002782 int nch = NCH(tree) - 1;
2783 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002784 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002785
Fred Drakec2683dd2001-07-17 19:32:05 +00002786 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002787 if (TYPE(CHILD(tree, j)) == stmt)
2788 res = validate_stmt(CHILD(tree, j));
2789 else
2790 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002791 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002792 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002793 * user. Hopefully, this won't be needed. If a user reports getting
2794 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002795 */
2796 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002797 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002798
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002799 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002800}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002801
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00002802static int
2803validate_encoding_decl(node *tree)
2804{
2805 int nch = NCH(tree);
2806 int res = ((nch == 1)
2807 && validate_file_input(CHILD(tree, 0)));
2808
2809 if (!res && !PyErr_Occurred())
2810 err_string("Error Parsing encoding_decl");
2811
2812 return res;
2813}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002814
Fred Drake43f8f9b1998-04-13 16:25:46 +00002815static PyObject*
2816pickle_constructor = NULL;
2817
2818
2819static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002820parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002821{
Fred Drake268397f1998-04-29 14:16:32 +00002822 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002823 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00002824 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002825 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002826
Fred Drakec2683dd2001-07-17 19:32:05 +00002827 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002828 PyObject *newargs;
2829 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002830
Fred Drake2a6875e1999-09-20 22:32:18 +00002831 if ((empty_dict = PyDict_New()) == NULL)
2832 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002833 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002834 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002835 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002836 if (tuple != NULL) {
2837 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2838 Py_DECREF(tuple);
2839 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002840 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002841 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002842 }
2843 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002844 Py_XDECREF(empty_dict);
2845
Fred Drake43f8f9b1998-04-13 16:25:46 +00002846 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002847}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002848
2849
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002850/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00002851 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002852 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002853 * We'd really have to write a wrapper around it all anyway to allow
2854 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002855 */
2856static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00002857 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002858 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002859 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002860 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002861 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002862 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002863 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002864 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002865 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002866 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002867 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002868 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002869 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002870 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002871 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002872 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002873 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002874 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002875 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002876 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002877 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002878 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002879 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002880 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002881 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002882 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002883 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002884 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002885
Fred Drake43f8f9b1998-04-13 16:25:46 +00002886 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00002887 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002888 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00002889
Fred Drake268397f1998-04-29 14:16:32 +00002890 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002891 };
2892
2893
Mark Hammond62b1ab12002-07-23 06:31:15 +00002894PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00002895
Mark Hammond62b1ab12002-07-23 06:31:15 +00002896PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00002897initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00002898{
Fred Drake13130bc2001-08-15 16:44:56 +00002899 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00002900
2901 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002902 module = Py_InitModule("parser", parser_functions);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002903
Fred Drake7a15ba51999-09-09 14:21:52 +00002904 if (parser_error == 0)
2905 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002906
Tim Peters6a627252003-07-21 14:25:23 +00002907 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00002908 /* caller will check PyErr_Occurred() */
2909 return;
Tim Peters6a627252003-07-21 14:25:23 +00002910 /* CAUTION: The code next used to skip bumping the refcount on
2911 * parser_error. That's a disaster if initparser() gets called more
2912 * than once. By incref'ing, we ensure that each module dict that
2913 * gets created owns its reference to the shared parser_error object,
2914 * and the file static parser_error vrbl owns a reference too.
2915 */
2916 Py_INCREF(parser_error);
2917 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
2918 return;
2919
Fred Drakec2683dd2001-07-17 19:32:05 +00002920 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002921 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00002922 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002923 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002924
Fred Drake13130bc2001-08-15 16:44:56 +00002925 PyModule_AddStringConstant(module, "__copyright__",
2926 parser_copyright_string);
2927 PyModule_AddStringConstant(module, "__doc__",
2928 parser_doc_string);
2929 PyModule_AddStringConstant(module, "__version__",
2930 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002931
Fred Drake78bdb9b2001-07-19 20:17:15 +00002932 /* Register to support pickling.
2933 * If this fails, the import of this module will fail because an
2934 * exception will be raised here; should we clear the exception?
2935 */
Fred Drake13130bc2001-08-15 16:44:56 +00002936 copyreg = PyImport_ImportModule("copy_reg");
2937 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002938 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002939
Fred Drake13130bc2001-08-15 16:44:56 +00002940 func = PyObject_GetAttrString(copyreg, "pickle");
2941 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
2942 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00002943 Py_XINCREF(pickle_constructor);
2944 if ((func != NULL) && (pickle_constructor != NULL)
2945 && (pickler != NULL)) {
2946 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002947
Fred Drakec2683dd2001-07-17 19:32:05 +00002948 res = PyObject_CallFunction(func, "OOO", &PyST_Type, pickler,
2949 pickle_constructor);
Fred Drakeff9ea482000-04-19 13:54:15 +00002950 Py_XDECREF(res);
2951 }
2952 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00002953 Py_XDECREF(pickle_constructor);
2954 Py_XDECREF(pickler);
2955 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002956 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002957}