blob: fd030678e6f5fea3ae1722bcf8453800e64ac0b5 [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 }
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000109
110 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);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000552
553
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 }
605 else {
606 /* This is a fragment, at best. */
607 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000608 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000609 }
Guido van Rossum47478871996-08-21 14:32:37 +0000610 }
Guido van Rossum47478871996-08-21 14:32:37 +0000611 /* Make sure we throw an exception on all errors. We should never
612 * get this, but we'd do well to be sure something is done.
613 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000614 if (st == NULL && !PyErr_Occurred())
615 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000616
Fred Drakec2683dd2001-07-17 19:32:05 +0000617 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000618}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000619
620
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000621/* node* build_node_children()
622 *
623 * Iterate across the children of the current non-terminal node and build
624 * their structures. If successful, return the root of this portion of
625 * the tree, otherwise, 0. Any required exception will be specified already,
626 * and no memory will have been deallocated.
627 *
628 */
629static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000630build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000631{
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000632 int len = PyObject_Size(tuple);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000633 int i, err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000634
635 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000636 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000637 PyObject* elem = PySequence_GetItem(tuple, i);
638 int ok = elem != NULL;
639 long type = 0;
640 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000641
Fred Drakeff9ea482000-04-19 13:54:15 +0000642 if (ok)
643 ok = PySequence_Check(elem);
644 if (ok) {
645 PyObject *temp = PySequence_GetItem(elem, 0);
646 if (temp == NULL)
647 ok = 0;
648 else {
649 ok = PyInt_Check(temp);
650 if (ok)
651 type = PyInt_AS_LONG(temp);
652 Py_DECREF(temp);
653 }
654 }
655 if (!ok) {
656 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000657 Py_BuildValue("os", elem,
Fred Drakeff9ea482000-04-19 13:54:15 +0000658 "Illegal node construct."));
659 Py_XDECREF(elem);
660 return (0);
661 }
662 if (ISTERMINAL(type)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000663 int len = PyObject_Size(elem);
664 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000665
Fred Drake0ac9b072000-09-12 21:58:06 +0000666 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000667 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000668 return 0;
669 }
670 temp = PySequence_GetItem(elem, 1);
671 if (temp == NULL)
672 return 0;
673 if (!PyString_Check(temp)) {
674 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000675 "second item in terminal node must be a string,"
676 " found %s",
Fred Drake0ac9b072000-09-12 21:58:06 +0000677 ((PyTypeObject*)PyObject_Type(temp))->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000678 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000679 return 0;
680 }
681 if (len == 3) {
682 PyObject *o = PySequence_GetItem(elem, 2);
683 if (o != NULL) {
684 if (PyInt_Check(o))
685 *line_num = PyInt_AS_LONG(o);
686 else {
687 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000688 "third item in terminal node must be an"
689 " integer, found %s",
Fred Drake0ac9b072000-09-12 21:58:06 +0000690 ((PyTypeObject*)PyObject_Type(temp))->tp_name);
691 Py_DECREF(o);
692 Py_DECREF(temp);
693 return 0;
694 }
695 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000696 }
697 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000698 len = PyString_GET_SIZE(temp) + 1;
699 strn = (char *)PyMem_MALLOC(len);
700 if (strn != NULL)
701 (void) memcpy(strn, PyString_AS_STRING(temp), len);
702 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000703 }
704 else if (!ISNONTERMINAL(type)) {
705 /*
706 * It has to be one or the other; this is an error.
707 * Throw an exception.
708 */
709 PyErr_SetObject(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000710 Py_BuildValue("os", elem, "unknown node type."));
Fred Drakeff9ea482000-04-19 13:54:15 +0000711 Py_XDECREF(elem);
712 return (0);
713 }
Fred Drake8b55b2d2001-12-05 22:10:44 +0000714 err = PyNode_AddChild(root, type, strn, *line_num);
715 if (err == E_NOMEM) {
716 PyMem_DEL(strn);
717 return (node *) PyErr_NoMemory();
718 }
719 if (err == E_OVERFLOW) {
720 PyMem_DEL(strn);
721 PyErr_SetString(PyExc_ValueError,
722 "unsupported number of child nodes");
723 return NULL;
724 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000725
Fred Drakeff9ea482000-04-19 13:54:15 +0000726 if (ISNONTERMINAL(type)) {
727 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000728
Fred Drakeff9ea482000-04-19 13:54:15 +0000729 if (new_child != build_node_children(elem, new_child, line_num)) {
730 Py_XDECREF(elem);
731 return (0);
732 }
733 }
734 else if (type == NEWLINE) { /* It's true: we increment the */
735 ++(*line_num); /* line number *after* the newline! */
736 }
737 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000738 }
739 return (root);
Fred Drakeff9ea482000-04-19 13:54:15 +0000740}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000741
742
743static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000744build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000745{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000746 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000747 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000748 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000749
Guido van Rossum47478871996-08-21 14:32:37 +0000750 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000751 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000752 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000753 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000754 /*
755 * The tuple is simple, but it doesn't start with a start symbol.
756 * Throw an exception now and be done with it.
757 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000758 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000759 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000760 PyErr_SetObject(parser_error, tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000761 }
762 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000763 /*
764 * Not efficient, but that can be handled later.
765 */
766 int line_num = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000767
Fred Drakeff9ea482000-04-19 13:54:15 +0000768 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000769 if (res != NULL) {
770 if (res != build_node_children(tuple, res, &line_num)) {
771 PyNode_Free(res);
772 res = NULL;
773 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000774 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000775 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000776 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000777 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000778 * NONTERMINAL, we can't use it. Not sure the implementation
779 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000780 */
781 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000782 Py_BuildValue("os", tuple,
Fred Drakeff9ea482000-04-19 13:54:15 +0000783 "Illegal component tuple."));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000784
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000785 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000786}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000787
788
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000789/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000790 * Validation routines used within the validation section:
791 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000792static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000793
Fred Drakeff9ea482000-04-19 13:54:15 +0000794#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
795#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
796#define validate_colon(ch) validate_terminal(ch, COLON, ":")
797#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
798#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
799#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
800#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
801#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
802#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
803#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
804#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
805#define validate_star(ch) validate_terminal(ch, STAR, "*")
806#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
807#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
808#define validate_dot(ch) validate_terminal(ch, DOT, ".")
809#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000810
Fred Drake0ac9b072000-09-12 21:58:06 +0000811#define VALIDATER(n) static int validate_##n(node *tree)
812
Fred Drakeff9ea482000-04-19 13:54:15 +0000813VALIDATER(node); VALIDATER(small_stmt);
814VALIDATER(class); VALIDATER(node);
815VALIDATER(parameters); VALIDATER(suite);
816VALIDATER(testlist); VALIDATER(varargslist);
817VALIDATER(fpdef); VALIDATER(fplist);
818VALIDATER(stmt); VALIDATER(simple_stmt);
819VALIDATER(expr_stmt); VALIDATER(power);
820VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000821VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000822VALIDATER(raise_stmt); VALIDATER(import_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000823VALIDATER(global_stmt); VALIDATER(list_if);
824VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000825VALIDATER(exec_stmt); VALIDATER(compound_stmt);
826VALIDATER(while); VALIDATER(for);
827VALIDATER(try); VALIDATER(except_clause);
828VALIDATER(test); VALIDATER(and_test);
829VALIDATER(not_test); VALIDATER(comparison);
830VALIDATER(comp_op); VALIDATER(expr);
831VALIDATER(xor_expr); VALIDATER(and_expr);
832VALIDATER(shift_expr); VALIDATER(arith_expr);
833VALIDATER(term); VALIDATER(factor);
834VALIDATER(atom); VALIDATER(lambdef);
835VALIDATER(trailer); VALIDATER(subscript);
836VALIDATER(subscriptlist); VALIDATER(sliceop);
837VALIDATER(exprlist); VALIDATER(dictmaker);
838VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000839VALIDATER(listmaker); VALIDATER(yield_stmt);
Guido van Rossum2d3b9862002-05-24 15:47:06 +0000840VALIDATER(testlist1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000841
Fred Drake0ac9b072000-09-12 21:58:06 +0000842#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000843
Fred Drakeff9ea482000-04-19 13:54:15 +0000844#define is_even(n) (((n) & 1) == 0)
845#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000846
847
848static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000849validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000850{
Fred Drake0ac9b072000-09-12 21:58:06 +0000851 if (TYPE(n) != t) {
852 PyErr_Format(parser_error, "Expected node type %d, got %d.",
853 t, TYPE(n));
854 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000855 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000856 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000857}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000858
859
Fred Drakee7ab64e2000-04-25 04:14:46 +0000860/* Verifies that the number of child nodes is exactly 'num', raising
861 * an exception if it isn't. The exception message does not indicate
862 * the exact number of nodes, allowing this to be used to raise the
863 * "right" exception when the wrong number of nodes is present in a
864 * specific variant of a statement's syntax. This is commonly used
865 * in that fashion.
866 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000867static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000868validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000869{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000870 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000871 PyErr_Format(parser_error,
872 "Illegal number of children for %s node.", name);
873 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000874 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000875 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000876}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000877
878
879static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000880validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000881{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000882 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000883 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000884
885 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000886 PyErr_Format(parser_error,
887 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000888 }
889 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000890}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000891
892
Guido van Rossum47478871996-08-21 14:32:37 +0000893/* X (',' X) [',']
894 */
895static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000896validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000897 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000898{
899 int nch = NCH(tree);
900 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000901 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000902
903 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000904 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000905 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000906 if (is_even(nch))
907 res = validate_comma(CHILD(tree, --nch));
908 if (res && nch > 1) {
909 int pos = 1;
910 for ( ; res && pos < nch; pos += 2)
911 res = (validate_comma(CHILD(tree, pos))
912 && vfunc(CHILD(tree, pos + 1)));
913 }
Guido van Rossum47478871996-08-21 14:32:37 +0000914 }
915 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000916}
Guido van Rossum47478871996-08-21 14:32:37 +0000917
918
Fred Drakecff283c2000-08-21 22:24:43 +0000919/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000920 *
921 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000922 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000923 */
Guido van Rossum47478871996-08-21 14:32:37 +0000924static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000925validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000926{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000927 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000928 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000929
Guido van Rossum3d602e31996-07-21 02:33:56 +0000930 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000931 res = (validate_name(CHILD(tree, 0), "class")
932 && validate_ntype(CHILD(tree, 1), NAME)
933 && validate_colon(CHILD(tree, nch - 2))
934 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000935 }
936 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000937 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000938 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000939 res = (validate_lparen(CHILD(tree, 2))
940 && validate_testlist(CHILD(tree, 3))
941 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000942 }
943 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000944}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000945
946
Guido van Rossum3d602e31996-07-21 02:33:56 +0000947/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +0000948 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +0000949 */
Guido van Rossum47478871996-08-21 14:32:37 +0000950static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000951validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000952{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000953 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000954 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +0000955 && (nch >= 4)
956 && validate_name(CHILD(tree, 0), "if")
957 && validate_test(CHILD(tree, 1))
958 && validate_colon(CHILD(tree, 2))
959 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000960
961 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000962 /* ... 'else' ':' suite */
963 res = (validate_name(CHILD(tree, nch - 3), "else")
964 && validate_colon(CHILD(tree, nch - 2))
965 && validate_suite(CHILD(tree, nch - 1)));
966 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000967 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000968 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000969 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000970 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +0000971 /* Will catch the case for nch < 4 */
972 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000973 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000974 /* ... ('elif' test ':' suite)+ ... */
975 int j = 4;
976 while ((j < nch) && res) {
977 res = (validate_name(CHILD(tree, j), "elif")
978 && validate_colon(CHILD(tree, j + 2))
979 && validate_test(CHILD(tree, j + 1))
980 && validate_suite(CHILD(tree, j + 3)));
981 j += 4;
982 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000983 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000984 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000985}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000986
987
Guido van Rossum3d602e31996-07-21 02:33:56 +0000988/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +0000989 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +0000990 *
991 */
Guido van Rossum47478871996-08-21 14:32:37 +0000992static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000993validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000994{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000995 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000996 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000997
Guido van Rossum3d602e31996-07-21 02:33:56 +0000998 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000999 res = (validate_lparen(CHILD(tree, 0))
1000 && validate_rparen(CHILD(tree, nch - 1)));
1001 if (res && (nch == 3))
1002 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001003 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001004 else {
1005 (void) validate_numnodes(tree, 2, "parameters");
1006 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001007 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001008}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001009
1010
Fred Drakecff283c2000-08-21 22:24:43 +00001011/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001012 *
1013 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001014 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001015 * | NEWLINE INDENT stmt+ DEDENT
1016 */
Guido van Rossum47478871996-08-21 14:32:37 +00001017static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001018validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001019{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001020 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001021 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001022
Guido van Rossum3d602e31996-07-21 02:33:56 +00001023 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001024 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001025 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001026 /* NEWLINE INDENT stmt+ DEDENT */
1027 res = (validate_newline(CHILD(tree, 0))
1028 && validate_indent(CHILD(tree, 1))
1029 && validate_stmt(CHILD(tree, 2))
1030 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001031
Fred Drakeff9ea482000-04-19 13:54:15 +00001032 if (res && (nch > 4)) {
1033 int i = 3;
1034 --nch; /* forget the DEDENT */
1035 for ( ; res && (i < nch); ++i)
1036 res = validate_stmt(CHILD(tree, i));
1037 }
1038 else if (nch < 4)
1039 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001040 }
1041 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001042}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001043
1044
Guido van Rossum47478871996-08-21 14:32:37 +00001045static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001046validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001047{
Guido van Rossum47478871996-08-21 14:32:37 +00001048 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001049 validate_test, "testlist"));
1050}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001051
1052
Guido van Rossum1c917072001-10-15 15:44:05 +00001053static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001054validate_testlist1(node *tree)
1055{
1056 return (validate_repeating_list(tree, testlist1,
1057 validate_test, "testlist1"));
1058}
1059
1060
1061static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001062validate_testlist_safe(node *tree)
1063{
1064 return (validate_repeating_list(tree, testlist_safe,
1065 validate_test, "testlist_safe"));
1066}
1067
1068
Fred Drakecff283c2000-08-21 22:24:43 +00001069/* '*' NAME [',' '**' NAME] | '**' NAME
1070 */
1071static int
1072validate_varargslist_trailer(node *tree, int start)
1073{
1074 int nch = NCH(tree);
1075 int res = 0;
1076 int sym;
1077
1078 if (nch <= start) {
1079 err_string("expected variable argument trailer for varargslist");
1080 return 0;
1081 }
1082 sym = TYPE(CHILD(tree, start));
1083 if (sym == STAR) {
1084 /*
1085 * ('*' NAME [',' '**' NAME]
1086 */
1087 if (nch-start == 2)
1088 res = validate_name(CHILD(tree, start+1), NULL);
1089 else if (nch-start == 5)
1090 res = (validate_name(CHILD(tree, start+1), NULL)
1091 && validate_comma(CHILD(tree, start+2))
1092 && validate_doublestar(CHILD(tree, start+3))
1093 && validate_name(CHILD(tree, start+4), NULL));
1094 }
1095 else if (sym == DOUBLESTAR) {
1096 /*
1097 * '**' NAME
1098 */
1099 if (nch-start == 2)
1100 res = validate_name(CHILD(tree, start+1), NULL);
1101 }
1102 if (!res)
1103 err_string("illegal variable argument trailer for varargslist");
1104 return res;
1105}
1106
1107
1108/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001109 *
1110 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001111 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001112 * ('*' NAME [',' '**' NAME]
1113 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001114 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1115 *
1116 */
Guido van Rossum47478871996-08-21 14:32:37 +00001117static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001118validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001119{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001120 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001121 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001122 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001123
Fred Drakeb6429a22000-12-11 22:08:27 +00001124 if (!res)
1125 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001126 if (nch < 1) {
1127 err_string("varargslist missing child nodes");
1128 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001129 }
Fred Drakecff283c2000-08-21 22:24:43 +00001130 sym = TYPE(CHILD(tree, 0));
1131 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001132 /* whole thing matches:
1133 * '*' NAME [',' '**' NAME] | '**' NAME
1134 */
Fred Drakecff283c2000-08-21 22:24:43 +00001135 res = validate_varargslist_trailer(tree, 0);
1136 else if (sym == fpdef) {
1137 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001138
Fred Drakecff283c2000-08-21 22:24:43 +00001139 sym = TYPE(CHILD(tree, nch-1));
1140 if (sym == NAME) {
1141 /*
1142 * (fpdef ['=' test] ',')+
1143 * ('*' NAME [',' '**' NAME]
1144 * | '**' NAME)
1145 */
1146 /* skip over (fpdef ['=' test] ',')+ */
1147 while (res && (i+2 <= nch)) {
1148 res = validate_fpdef(CHILD(tree, i));
1149 ++i;
1150 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1151 res = (validate_equal(CHILD(tree, i))
1152 && validate_test(CHILD(tree, i+1)));
1153 if (res)
1154 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001155 }
Fred Drakecff283c2000-08-21 22:24:43 +00001156 if (res && i < nch) {
1157 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001158 ++i;
1159 if (res && i < nch
1160 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1161 || TYPE(CHILD(tree, i)) == STAR))
1162 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001163 }
1164 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001165 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1166 * i --^^^
1167 */
Fred Drakecff283c2000-08-21 22:24:43 +00001168 if (res)
1169 res = validate_varargslist_trailer(tree, i);
1170 }
1171 else {
1172 /*
1173 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1174 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001175 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001176 if (sym == COMMA) {
1177 res = validate_comma(CHILD(tree, nch-1));
1178 if (!res)
1179 return 0;
1180 --nch;
1181 }
1182 /*
1183 * fpdef ['=' test] (',' fpdef ['=' test])*
1184 */
1185 res = validate_fpdef(CHILD(tree, 0));
1186 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001187 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1188 res = (validate_equal(CHILD(tree, i))
1189 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001190 i += 2;
1191 }
1192 /*
1193 * ... (',' fpdef ['=' test])*
1194 * i ---^^^
1195 */
1196 while (res && (nch - i) >= 2) {
1197 res = (validate_comma(CHILD(tree, i))
1198 && validate_fpdef(CHILD(tree, i+1)));
1199 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001200 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1201 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001202 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001203 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001204 }
1205 }
1206 if (res && nch - i != 0) {
1207 res = 0;
1208 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001209 }
1210 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001211 }
Fred Drakecff283c2000-08-21 22:24:43 +00001212 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001213}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001214
1215
Fred Drakecff283c2000-08-21 22:24:43 +00001216/* list_iter: list_for | list_if
1217 */
1218static int
1219validate_list_iter(node *tree)
1220{
1221 int res = (validate_ntype(tree, list_iter)
1222 && validate_numnodes(tree, 1, "list_iter"));
1223 if (res && TYPE(CHILD(tree, 0)) == list_for)
1224 res = validate_list_for(CHILD(tree, 0));
1225 else
1226 res = validate_list_if(CHILD(tree, 0));
1227
1228 return res;
1229}
1230
1231/* list_for: 'for' exprlist 'in' testlist [list_iter]
1232 */
1233static int
1234validate_list_for(node *tree)
1235{
1236 int nch = NCH(tree);
1237 int res;
1238
1239 if (nch == 5)
1240 res = validate_list_iter(CHILD(tree, 4));
1241 else
1242 res = validate_numnodes(tree, 4, "list_for");
1243
1244 if (res)
1245 res = (validate_name(CHILD(tree, 0), "for")
1246 && validate_exprlist(CHILD(tree, 1))
1247 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001248 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001249
1250 return res;
1251}
1252
1253/* list_if: 'if' test [list_iter]
1254 */
1255static int
1256validate_list_if(node *tree)
1257{
1258 int nch = NCH(tree);
1259 int res;
1260
1261 if (nch == 3)
1262 res = validate_list_iter(CHILD(tree, 2));
1263 else
1264 res = validate_numnodes(tree, 2, "list_if");
1265
1266 if (res)
1267 res = (validate_name(CHILD(tree, 0), "if")
1268 && validate_test(CHILD(tree, 1)));
1269
1270 return res;
1271}
1272
1273
1274/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001275 *
1276 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001277 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001278 * | '(' fplist ')'
1279 */
Guido van Rossum47478871996-08-21 14:32:37 +00001280static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001281validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001282{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001283 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001284 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001285
Guido van Rossum3d602e31996-07-21 02:33:56 +00001286 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001287 if (nch == 1)
1288 res = validate_ntype(CHILD(tree, 0), NAME);
1289 else if (nch == 3)
1290 res = (validate_lparen(CHILD(tree, 0))
1291 && validate_fplist(CHILD(tree, 1))
1292 && validate_rparen(CHILD(tree, 2)));
1293 else
1294 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001295 }
1296 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001297}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001298
1299
Guido van Rossum47478871996-08-21 14:32:37 +00001300static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001301validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001302{
Guido van Rossum47478871996-08-21 14:32:37 +00001303 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001304 validate_fpdef, "fplist"));
1305}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001306
1307
Guido van Rossum3d602e31996-07-21 02:33:56 +00001308/* simple_stmt | compound_stmt
1309 *
1310 */
Guido van Rossum47478871996-08-21 14:32:37 +00001311static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001312validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001313{
1314 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001315 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001316
Guido van Rossum3d602e31996-07-21 02:33:56 +00001317 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001318 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001319
Fred Drakeff9ea482000-04-19 13:54:15 +00001320 if (TYPE(tree) == simple_stmt)
1321 res = validate_simple_stmt(tree);
1322 else
1323 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001324 }
1325 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001326}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001327
1328
Guido van Rossum3d602e31996-07-21 02:33:56 +00001329/* small_stmt (';' small_stmt)* [';'] NEWLINE
1330 *
1331 */
Guido van Rossum47478871996-08-21 14:32:37 +00001332static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001333validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001334{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001335 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001336 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001337 && (nch >= 2)
1338 && validate_small_stmt(CHILD(tree, 0))
1339 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001340
Guido van Rossum3d602e31996-07-21 02:33:56 +00001341 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001342 res = validate_numnodes(tree, 2, "simple_stmt");
1343 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001344 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001345 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001346 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001347 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001348
Fred Drakeff9ea482000-04-19 13:54:15 +00001349 for (i = 1; res && (i < nch); i += 2)
1350 res = (validate_semi(CHILD(tree, i))
1351 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001352 }
1353 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001354}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001355
1356
Guido van Rossum47478871996-08-21 14:32:37 +00001357static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001358validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001359{
1360 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001361 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001362
Fred Drake0ac9b072000-09-12 21:58:06 +00001363 if (res) {
1364 int ntype = TYPE(CHILD(tree, 0));
1365
1366 if ( (ntype == expr_stmt)
1367 || (ntype == print_stmt)
1368 || (ntype == del_stmt)
1369 || (ntype == pass_stmt)
1370 || (ntype == flow_stmt)
1371 || (ntype == import_stmt)
1372 || (ntype == global_stmt)
1373 || (ntype == assert_stmt)
1374 || (ntype == exec_stmt))
1375 res = validate_node(CHILD(tree, 0));
1376 else {
1377 res = 0;
1378 err_string("illegal small_stmt child type");
1379 }
1380 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001381 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001382 res = 0;
1383 PyErr_Format(parser_error,
1384 "Unrecognized child node of small_stmt: %d.",
1385 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001386 }
1387 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001388}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001389
1390
1391/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001392 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001393 */
Guido van Rossum47478871996-08-21 14:32:37 +00001394static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001395validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001396{
1397 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001398 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001399 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001400
1401 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001402 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001403
1404 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001405 ntype = TYPE(tree);
1406 if ( (ntype == if_stmt)
1407 || (ntype == while_stmt)
1408 || (ntype == for_stmt)
1409 || (ntype == try_stmt)
1410 || (ntype == funcdef)
1411 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001412 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001413 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001414 res = 0;
1415 PyErr_Format(parser_error,
1416 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001417 }
1418 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001419}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001420
1421
Guido van Rossum47478871996-08-21 14:32:37 +00001422static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001423validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001424{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001425 int j;
1426 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001427 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001428 && is_odd(nch)
1429 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001430
Fred Drake28f739a2000-08-25 22:42:40 +00001431 if (res && nch == 3
1432 && TYPE(CHILD(tree, 1)) == augassign) {
1433 res = (validate_numnodes(CHILD(tree, 1), 1, "augassign")
1434 && validate_testlist(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001435
Fred Drake28f739a2000-08-25 22:42:40 +00001436 if (res) {
1437 char *s = STR(CHILD(CHILD(tree, 1), 0));
1438
1439 res = (strcmp(s, "+=") == 0
1440 || strcmp(s, "-=") == 0
1441 || strcmp(s, "*=") == 0
1442 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001443 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001444 || strcmp(s, "%=") == 0
1445 || strcmp(s, "&=") == 0
1446 || strcmp(s, "|=") == 0
1447 || strcmp(s, "^=") == 0
1448 || strcmp(s, "<<=") == 0
1449 || strcmp(s, ">>=") == 0
1450 || strcmp(s, "**=") == 0);
1451 if (!res)
1452 err_string("illegal augmmented assignment operator");
1453 }
1454 }
1455 else {
1456 for (j = 1; res && (j < nch); j += 2)
1457 res = (validate_equal(CHILD(tree, j))
1458 && validate_testlist(CHILD(tree, j + 1)));
1459 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001460 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001461}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001462
1463
Guido van Rossum3d602e31996-07-21 02:33:56 +00001464/* print_stmt:
1465 *
Fred Drakecff283c2000-08-21 22:24:43 +00001466 * 'print' ( [ test (',' test)* [','] ]
1467 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001468 */
Guido van Rossum47478871996-08-21 14:32:37 +00001469static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001470validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001471{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001472 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001473 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001474 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001475 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001476
Fred Drakecff283c2000-08-21 22:24:43 +00001477 if (res && nch > 1) {
1478 int sym = TYPE(CHILD(tree, 1));
1479 int i = 1;
1480 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001481
Fred Drakecff283c2000-08-21 22:24:43 +00001482 if (sym == test)
1483 res = validate_test(CHILD(tree, i++));
1484 else {
1485 if (nch < 3)
1486 res = validate_numnodes(tree, 3, "print_stmt");
1487 else {
1488 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1489 && validate_test(CHILD(tree, i+1)));
1490 i += 2;
1491 allow_trailing_comma = 0;
1492 }
1493 }
1494 if (res) {
1495 /* ... (',' test)* [','] */
1496 while (res && i+2 <= nch) {
1497 res = (validate_comma(CHILD(tree, i))
1498 && validate_test(CHILD(tree, i+1)));
1499 allow_trailing_comma = 1;
1500 i += 2;
1501 }
1502 if (res && !allow_trailing_comma)
1503 res = validate_numnodes(tree, i, "print_stmt");
1504 else if (res && i < nch)
1505 res = validate_comma(CHILD(tree, i));
1506 }
1507 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001508 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001509}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001510
1511
Guido van Rossum47478871996-08-21 14:32:37 +00001512static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001513validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001514{
1515 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001516 && validate_name(CHILD(tree, 0), "del")
1517 && validate_exprlist(CHILD(tree, 1)));
1518}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001519
1520
Guido van Rossum47478871996-08-21 14:32:37 +00001521static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001522validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001523{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001524 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001525 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001526 && ((nch == 1) || (nch == 2))
1527 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001528
Guido van Rossum3d602e31996-07-21 02:33:56 +00001529 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001530 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001531
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001532 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001533}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001534
1535
Guido van Rossum47478871996-08-21 14:32:37 +00001536static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001537validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001538{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001539 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001540 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001541 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001542
Guido van Rossum3d602e31996-07-21 02:33:56 +00001543 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001544 res = validate_name(CHILD(tree, 0), "raise");
1545 if (res && (nch >= 2))
1546 res = validate_test(CHILD(tree, 1));
1547 if (res && nch > 2) {
1548 res = (validate_comma(CHILD(tree, 2))
1549 && validate_test(CHILD(tree, 3)));
1550 if (res && (nch > 4))
1551 res = (validate_comma(CHILD(tree, 4))
1552 && validate_test(CHILD(tree, 5)));
1553 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001554 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001555 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001556 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001557 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001558 res = (validate_comma(CHILD(tree, 2))
1559 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001560
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001561 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001562}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001563
1564
Fred Drake02126f22001-07-17 02:59:15 +00001565/* yield_stmt: 'yield' testlist
1566 */
1567static int
1568validate_yield_stmt(node *tree)
1569{
1570 return (validate_ntype(tree, yield_stmt)
1571 && validate_numnodes(tree, 2, "yield_stmt")
1572 && validate_name(CHILD(tree, 0), "yield")
1573 && validate_testlist(CHILD(tree, 1)));
1574}
1575
1576
Fred Drakecff283c2000-08-21 22:24:43 +00001577static int
1578validate_import_as_name(node *tree)
1579{
1580 int nch = NCH(tree);
1581 int ok = validate_ntype(tree, import_as_name);
1582
1583 if (ok) {
1584 if (nch == 1)
1585 ok = validate_name(CHILD(tree, 0), NULL);
1586 else if (nch == 3)
1587 ok = (validate_name(CHILD(tree, 0), NULL)
1588 && validate_name(CHILD(tree, 1), "as")
1589 && validate_name(CHILD(tree, 2), NULL));
1590 else
1591 ok = validate_numnodes(tree, 3, "import_as_name");
1592 }
1593 return ok;
1594}
1595
1596
Fred Drake71137082001-01-07 05:59:59 +00001597/* dotted_name: NAME ("." NAME)*
1598 */
1599static int
1600validate_dotted_name(node *tree)
1601{
1602 int nch = NCH(tree);
1603 int res = (validate_ntype(tree, dotted_name)
1604 && is_odd(nch)
1605 && validate_name(CHILD(tree, 0), NULL));
1606 int i;
1607
1608 for (i = 1; res && (i < nch); i += 2) {
1609 res = (validate_dot(CHILD(tree, i))
1610 && validate_name(CHILD(tree, i+1), NULL));
1611 }
1612 return res;
1613}
1614
1615
Fred Drakecff283c2000-08-21 22:24:43 +00001616/* dotted_as_name: dotted_name [NAME NAME]
1617 */
1618static int
1619validate_dotted_as_name(node *tree)
1620{
1621 int nch = NCH(tree);
1622 int res = validate_ntype(tree, dotted_as_name);
1623
1624 if (res) {
1625 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001626 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001627 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001628 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001629 && validate_name(CHILD(tree, 1), "as")
1630 && validate_name(CHILD(tree, 2), NULL));
1631 else {
1632 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001633 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001634 }
1635 }
1636 return res;
1637}
1638
1639
Guido van Rossum3d602e31996-07-21 02:33:56 +00001640/* import_stmt:
1641 *
Fred Drakecff283c2000-08-21 22:24:43 +00001642 * 'import' dotted_as_name (',' dotted_as_name)*
1643 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001644 */
Guido van Rossum47478871996-08-21 14:32:37 +00001645static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001646validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001647{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001648 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001649 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001650 && (nch >= 2) && is_even(nch)
Fred Drakecff283c2000-08-21 22:24:43 +00001651 && validate_ntype(CHILD(tree, 0), NAME));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001652
1653 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001654 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001655
Fred Drakecff283c2000-08-21 22:24:43 +00001656 res = validate_dotted_as_name(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00001657 for (j = 2; res && (j < nch); j += 2)
1658 res = (validate_comma(CHILD(tree, j))
Fred Drake71137082001-01-07 05:59:59 +00001659 && validate_dotted_as_name(CHILD(tree, j + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001660 }
Fred Drakecff283c2000-08-21 22:24:43 +00001661 else if (res && (res = validate_name(CHILD(tree, 0), "from"))) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001662 res = ((nch >= 4) && is_even(nch)
Fred Drake71137082001-01-07 05:59:59 +00001663 && validate_dotted_name(CHILD(tree, 1))
1664 && validate_name(CHILD(tree, 2), "import"));
Fred Drakeff9ea482000-04-19 13:54:15 +00001665 if (nch == 4) {
Fred Drakecff283c2000-08-21 22:24:43 +00001666 if (TYPE(CHILD(tree, 3)) == import_as_name)
1667 res = validate_import_as_name(CHILD(tree, 3));
1668 else
1669 res = validate_star(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001670 }
1671 else {
Fred Drakecff283c2000-08-21 22:24:43 +00001672 /* 'from' dotted_name 'import' import_as_name
1673 * (',' import_as_name)+
1674 */
Fred Drakeff9ea482000-04-19 13:54:15 +00001675 int j;
Fred Drakecff283c2000-08-21 22:24:43 +00001676 res = validate_import_as_name(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001677 for (j = 4; res && (j < nch); j += 2)
1678 res = (validate_comma(CHILD(tree, j))
Fred Drakecff283c2000-08-21 22:24:43 +00001679 && validate_import_as_name(CHILD(tree, j + 1)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001680 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001681 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001682 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001683 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001684
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001685 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001686}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001687
1688
Guido van Rossum47478871996-08-21 14:32:37 +00001689static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001690validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001691{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001692 int j;
1693 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001694 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001695 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001696
Guido van Rossum3d602e31996-07-21 02:33:56 +00001697 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001698 res = (validate_name(CHILD(tree, 0), "global")
1699 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001700 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001701 res = (validate_comma(CHILD(tree, j))
1702 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001703
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001704 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001705}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001706
1707
Guido van Rossum3d602e31996-07-21 02:33:56 +00001708/* exec_stmt:
1709 *
1710 * 'exec' expr ['in' test [',' test]]
1711 */
Guido van Rossum47478871996-08-21 14:32:37 +00001712static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001713validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001714{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001715 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001716 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001717 && ((nch == 2) || (nch == 4) || (nch == 6))
1718 && validate_name(CHILD(tree, 0), "exec")
1719 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001720
Guido van Rossum3d602e31996-07-21 02:33:56 +00001721 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001722 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001723 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001724 res = (validate_name(CHILD(tree, 2), "in")
1725 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001726 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001727 res = (validate_comma(CHILD(tree, 4))
1728 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001729
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001730 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001731}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001732
1733
Guido van Rossum925e5471997-04-02 05:32:13 +00001734/* assert_stmt:
1735 *
1736 * 'assert' test [',' test]
1737 */
1738static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001739validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001740{
1741 int nch = NCH(tree);
1742 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001743 && ((nch == 2) || (nch == 4))
1744 && (validate_name(CHILD(tree, 0), "__assert__") ||
1745 validate_name(CHILD(tree, 0), "assert"))
1746 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001747
1748 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001749 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001750 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001751 res = (validate_comma(CHILD(tree, 2))
1752 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001753
1754 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001755}
Guido van Rossum925e5471997-04-02 05:32:13 +00001756
1757
Guido van Rossum47478871996-08-21 14:32:37 +00001758static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001759validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001760{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001761 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001762 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001763 && ((nch == 4) || (nch == 7))
1764 && validate_name(CHILD(tree, 0), "while")
1765 && validate_test(CHILD(tree, 1))
1766 && validate_colon(CHILD(tree, 2))
1767 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001768
Guido van Rossum3d602e31996-07-21 02:33:56 +00001769 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001770 res = (validate_name(CHILD(tree, 4), "else")
1771 && validate_colon(CHILD(tree, 5))
1772 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001773
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001774 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001775}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001776
1777
Guido van Rossum47478871996-08-21 14:32:37 +00001778static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001779validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001780{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001781 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001782 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001783 && ((nch == 6) || (nch == 9))
1784 && validate_name(CHILD(tree, 0), "for")
1785 && validate_exprlist(CHILD(tree, 1))
1786 && validate_name(CHILD(tree, 2), "in")
1787 && validate_testlist(CHILD(tree, 3))
1788 && validate_colon(CHILD(tree, 4))
1789 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001790
Guido van Rossum3d602e31996-07-21 02:33:56 +00001791 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001792 res = (validate_name(CHILD(tree, 6), "else")
1793 && validate_colon(CHILD(tree, 7))
1794 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001795
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001796 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001797}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001798
1799
Guido van Rossum3d602e31996-07-21 02:33:56 +00001800/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001801 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001802 * | 'try' ':' suite 'finally' ':' suite
1803 *
1804 */
Guido van Rossum47478871996-08-21 14:32:37 +00001805static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001806validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001807{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001808 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001809 int pos = 3;
1810 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001811 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001812
1813 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001814 res = (validate_name(CHILD(tree, 0), "try")
1815 && validate_colon(CHILD(tree, 1))
1816 && validate_suite(CHILD(tree, 2))
1817 && validate_colon(CHILD(tree, nch - 2))
1818 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001819 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001820 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001821 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1822 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001823
1824 PyErr_Format(parser_error,
1825 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001826 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001827 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001828 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001829 res = (validate_except_clause(CHILD(tree, pos))
1830 && validate_colon(CHILD(tree, pos + 1))
1831 && validate_suite(CHILD(tree, pos + 2)));
1832 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001833 }
1834 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001835 res = validate_ntype(CHILD(tree, pos), NAME);
1836 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1837 res = (validate_numnodes(tree, 6, "try/finally")
1838 && validate_colon(CHILD(tree, 4))
1839 && validate_suite(CHILD(tree, 5)));
1840 else if (res) {
1841 if (nch == (pos + 3)) {
1842 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1843 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1844 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001845 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001846 }
1847 else if (nch == (pos + 6)) {
1848 res = (validate_name(CHILD(tree, pos), "except")
1849 && validate_colon(CHILD(tree, pos + 1))
1850 && validate_suite(CHILD(tree, pos + 2))
1851 && validate_name(CHILD(tree, pos + 3), "else"));
1852 }
1853 else
1854 res = validate_numnodes(tree, pos + 3, "try/except");
1855 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001856 }
1857 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001858}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001859
1860
Guido van Rossum47478871996-08-21 14:32:37 +00001861static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001862validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001863{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001864 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001865 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001866 && ((nch == 1) || (nch == 2) || (nch == 4))
1867 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001868
Guido van Rossum3d602e31996-07-21 02:33:56 +00001869 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001870 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001871 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001872 res = (validate_comma(CHILD(tree, 2))
1873 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001874
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001875 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001876}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001877
1878
Guido van Rossum47478871996-08-21 14:32:37 +00001879static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001880validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001881{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001882 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001883 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001884
Guido van Rossum3d602e31996-07-21 02:33:56 +00001885 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001886 res = ((nch == 1)
1887 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001888 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001889 int pos;
1890 res = validate_and_test(CHILD(tree, 0));
1891 for (pos = 1; res && (pos < nch); pos += 2)
1892 res = (validate_name(CHILD(tree, pos), "or")
1893 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001894 }
1895 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001896}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001897
1898
Guido van Rossum47478871996-08-21 14:32:37 +00001899static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001900validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001901{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001902 int pos;
1903 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001904 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001905 && is_odd(nch)
1906 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001907
Guido van Rossum3d602e31996-07-21 02:33:56 +00001908 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001909 res = (validate_name(CHILD(tree, pos), "and")
1910 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001911
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001912 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001913}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001914
1915
Guido van Rossum47478871996-08-21 14:32:37 +00001916static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001917validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001918{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001919 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001920 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001921
Guido van Rossum3d602e31996-07-21 02:33:56 +00001922 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001923 if (nch == 2)
1924 res = (validate_name(CHILD(tree, 0), "not")
1925 && validate_not_test(CHILD(tree, 1)));
1926 else if (nch == 1)
1927 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001928 }
1929 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001930}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001931
1932
Guido van Rossum47478871996-08-21 14:32:37 +00001933static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001934validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001935{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001936 int pos;
1937 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001938 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001939 && is_odd(nch)
1940 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001941
Guido van Rossum3d602e31996-07-21 02:33:56 +00001942 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001943 res = (validate_comp_op(CHILD(tree, pos))
1944 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001945
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001946 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001947}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001948
1949
Guido van Rossum47478871996-08-21 14:32:37 +00001950static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001951validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001952{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001953 int res = 0;
1954 int nch = NCH(tree);
1955
Guido van Rossum3d602e31996-07-21 02:33:56 +00001956 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001957 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001958 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001959 /*
1960 * Only child will be a terminal with a well-defined symbolic name
1961 * or a NAME with a string of either 'is' or 'in'
1962 */
1963 tree = CHILD(tree, 0);
1964 switch (TYPE(tree)) {
1965 case LESS:
1966 case GREATER:
1967 case EQEQUAL:
1968 case EQUAL:
1969 case LESSEQUAL:
1970 case GREATEREQUAL:
1971 case NOTEQUAL:
1972 res = 1;
1973 break;
1974 case NAME:
1975 res = ((strcmp(STR(tree), "in") == 0)
1976 || (strcmp(STR(tree), "is") == 0));
1977 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001978 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00001979 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00001980 }
1981 break;
1982 default:
Fred Drake661ea262000-10-24 19:57:45 +00001983 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001984 break;
1985 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001986 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00001987 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001988 res = (validate_ntype(CHILD(tree, 0), NAME)
1989 && validate_ntype(CHILD(tree, 1), NAME)
1990 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
1991 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
1992 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
1993 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
1994 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001995 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001996 }
1997 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001998}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001999
2000
Guido van Rossum47478871996-08-21 14:32:37 +00002001static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002002validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002003{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002004 int j;
2005 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002006 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002007 && is_odd(nch)
2008 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002009
Guido van Rossum3d602e31996-07-21 02:33:56 +00002010 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002011 res = (validate_xor_expr(CHILD(tree, j))
2012 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002013
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002014 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002015}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002016
2017
Guido van Rossum47478871996-08-21 14:32:37 +00002018static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002019validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002020{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002021 int j;
2022 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002023 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002024 && is_odd(nch)
2025 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002026
Guido van Rossum3d602e31996-07-21 02:33:56 +00002027 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002028 res = (validate_circumflex(CHILD(tree, j - 1))
2029 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002030
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002031 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002032}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002033
2034
Guido van Rossum47478871996-08-21 14:32:37 +00002035static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002036validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002037{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002038 int pos;
2039 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002040 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002041 && is_odd(nch)
2042 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002043
Guido van Rossum3d602e31996-07-21 02:33:56 +00002044 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002045 res = (validate_ampersand(CHILD(tree, pos))
2046 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002047
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002048 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002049}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002050
2051
2052static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002053validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002054 {
2055 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002056 int nch = NCH(tree);
2057 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002058 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002059
Guido van Rossum3d602e31996-07-21 02:33:56 +00002060 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002061 if (TYPE(CHILD(tree, pos)) != op1)
2062 res = validate_ntype(CHILD(tree, pos), op2);
2063 if (res)
2064 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002065 }
2066 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002067}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002068
2069
Guido van Rossum47478871996-08-21 14:32:37 +00002070static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002071validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002072{
2073 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002074 && validate_chain_two_ops(tree, validate_arith_expr,
2075 LEFTSHIFT, RIGHTSHIFT));
2076}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002077
2078
Guido van Rossum47478871996-08-21 14:32:37 +00002079static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002080validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002081{
2082 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002083 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2084}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002085
2086
Guido van Rossum47478871996-08-21 14:32:37 +00002087static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002088validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002089{
2090 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002091 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002092 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002093 && is_odd(nch)
2094 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002095
Guido van Rossum3d602e31996-07-21 02:33:56 +00002096 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002097 res = (((TYPE(CHILD(tree, pos)) == STAR)
2098 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002099 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002100 || (TYPE(CHILD(tree, pos)) == PERCENT))
2101 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002102
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002103 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002104}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002105
2106
Guido van Rossum3d602e31996-07-21 02:33:56 +00002107/* factor:
2108 *
2109 * factor: ('+'|'-'|'~') factor | power
2110 */
Guido van Rossum47478871996-08-21 14:32:37 +00002111static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002112validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002113{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002114 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002115 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002116 && (((nch == 2)
2117 && ((TYPE(CHILD(tree, 0)) == PLUS)
2118 || (TYPE(CHILD(tree, 0)) == MINUS)
2119 || (TYPE(CHILD(tree, 0)) == TILDE))
2120 && validate_factor(CHILD(tree, 1)))
2121 || ((nch == 1)
2122 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002123 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002124}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002125
2126
Guido van Rossum3d602e31996-07-21 02:33:56 +00002127/* power:
2128 *
2129 * power: atom trailer* ('**' factor)*
2130 */
Guido van Rossum47478871996-08-21 14:32:37 +00002131static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002132validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002133{
2134 int pos = 1;
2135 int nch = NCH(tree);
2136 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002137 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002138
2139 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002140 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002141 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002142 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002143 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002144 return (0);
2145 }
2146 for ( ; res && (pos < (nch - 1)); pos += 2)
2147 res = (validate_doublestar(CHILD(tree, pos))
2148 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002149 }
2150 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002151}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002152
2153
Guido van Rossum47478871996-08-21 14:32:37 +00002154static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002155validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002156{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002157 int pos;
2158 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002159 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002160
Fred Drakecff283c2000-08-21 22:24:43 +00002161 if (res && nch < 1)
2162 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002163 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002164 switch (TYPE(CHILD(tree, 0))) {
2165 case LPAR:
2166 res = ((nch <= 3)
2167 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002168
Fred Drakeff9ea482000-04-19 13:54:15 +00002169 if (res && (nch == 3))
2170 res = validate_testlist(CHILD(tree, 1));
2171 break;
2172 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002173 if (nch == 2)
2174 res = validate_ntype(CHILD(tree, 1), RSQB);
2175 else if (nch == 3)
2176 res = (validate_listmaker(CHILD(tree, 1))
2177 && validate_ntype(CHILD(tree, 2), RSQB));
2178 else {
2179 res = 0;
2180 err_string("illegal list display atom");
2181 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002182 break;
2183 case LBRACE:
2184 res = ((nch <= 3)
2185 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002186
Fred Drakeff9ea482000-04-19 13:54:15 +00002187 if (res && (nch == 3))
2188 res = validate_dictmaker(CHILD(tree, 1));
2189 break;
2190 case BACKQUOTE:
2191 res = ((nch == 3)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002192 && validate_testlist1(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002193 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2194 break;
2195 case NAME:
2196 case NUMBER:
2197 res = (nch == 1);
2198 break;
2199 case STRING:
2200 for (pos = 1; res && (pos < nch); ++pos)
2201 res = validate_ntype(CHILD(tree, pos), STRING);
2202 break;
2203 default:
2204 res = 0;
2205 break;
2206 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002207 }
2208 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002209}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002210
2211
Fred Drake85bf3bb2000-08-23 15:35:26 +00002212/* listmaker:
2213 * test ( list_for | (',' test)* [','] )
2214 */
Fred Drakecff283c2000-08-21 22:24:43 +00002215static int
2216validate_listmaker(node *tree)
2217{
2218 int nch = NCH(tree);
2219 int ok = nch;
2220
2221 if (nch == 0)
2222 err_string("missing child nodes of listmaker");
2223 else
2224 ok = validate_test(CHILD(tree, 0));
2225
2226 /*
2227 * list_iter | (',' test)* [',']
2228 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002229 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2230 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002231 else {
2232 /* (',' test)* [','] */
2233 int i = 1;
2234 while (ok && nch - i >= 2) {
2235 ok = (validate_comma(CHILD(tree, i))
2236 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002237 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002238 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002239 if (ok && i == nch-1)
2240 ok = validate_comma(CHILD(tree, i));
2241 else if (i != nch) {
2242 ok = 0;
2243 err_string("illegal trailing nodes for listmaker");
2244 }
Fred Drakecff283c2000-08-21 22:24:43 +00002245 }
2246 return ok;
2247}
2248
2249
Guido van Rossum3d602e31996-07-21 02:33:56 +00002250/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002251 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002252 *
2253 */
Guido van Rossum47478871996-08-21 14:32:37 +00002254static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002255validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002256{
2257 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002258 && validate_numnodes(tree, 5, "funcdef")
2259 && validate_name(CHILD(tree, 0), "def")
2260 && validate_ntype(CHILD(tree, 1), NAME)
2261 && validate_colon(CHILD(tree, 3))
2262 && validate_parameters(CHILD(tree, 2))
2263 && validate_suite(CHILD(tree, 4)));
2264}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002265
2266
Guido van Rossum47478871996-08-21 14:32:37 +00002267static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002268validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002269{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002270 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002271 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002272 && ((nch == 3) || (nch == 4))
2273 && validate_name(CHILD(tree, 0), "lambda")
2274 && validate_colon(CHILD(tree, nch - 2))
2275 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002276
Guido van Rossum3d602e31996-07-21 02:33:56 +00002277 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002278 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002279 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002280 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002281
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002282 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002283}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002284
2285
Guido van Rossum3d602e31996-07-21 02:33:56 +00002286/* arglist:
2287 *
Fred Drakecff283c2000-08-21 22:24:43 +00002288 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002289 */
Guido van Rossum47478871996-08-21 14:32:37 +00002290static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002291validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002292{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002293 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002294 int i = 0;
2295 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002296
2297 if (nch <= 0)
2298 /* raise the right error from having an invalid number of children */
2299 return validate_numnodes(tree, nch + 1, "arglist");
2300
Fred Drakecff283c2000-08-21 22:24:43 +00002301 while (ok && nch-i >= 2) {
2302 /* skip leading (argument ',') */
2303 ok = (validate_argument(CHILD(tree, i))
2304 && validate_comma(CHILD(tree, i+1)));
2305 if (ok)
2306 i += 2;
2307 else
2308 PyErr_Clear();
2309 }
2310 ok = 1;
2311 if (nch-i > 0) {
2312 /*
2313 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002314 */
Fred Drakecff283c2000-08-21 22:24:43 +00002315 int sym = TYPE(CHILD(tree, i));
2316
2317 if (sym == argument) {
2318 ok = validate_argument(CHILD(tree, i));
2319 if (ok && i+1 != nch) {
2320 err_string("illegal arglist specification"
2321 " (extra stuff on end)");
2322 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002323 }
Fred Drakecff283c2000-08-21 22:24:43 +00002324 }
2325 else if (sym == STAR) {
2326 ok = validate_star(CHILD(tree, i));
2327 if (ok && (nch-i == 2))
2328 ok = validate_test(CHILD(tree, i+1));
2329 else if (ok && (nch-i == 5))
2330 ok = (validate_test(CHILD(tree, i+1))
2331 && validate_comma(CHILD(tree, i+2))
2332 && validate_doublestar(CHILD(tree, i+3))
2333 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002334 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002335 err_string("illegal use of '*' in arglist");
2336 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002337 }
2338 }
Fred Drakecff283c2000-08-21 22:24:43 +00002339 else if (sym == DOUBLESTAR) {
2340 if (nch-i == 2)
2341 ok = (validate_doublestar(CHILD(tree, i))
2342 && validate_test(CHILD(tree, i+1)));
2343 else {
2344 err_string("illegal use of '**' in arglist");
2345 ok = 0;
2346 }
2347 }
2348 else {
2349 err_string("illegal arglist specification");
2350 ok = 0;
2351 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002352 }
2353 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002354}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002355
2356
2357
2358/* argument:
2359 *
2360 * [test '='] test
2361 */
Guido van Rossum47478871996-08-21 14:32:37 +00002362static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002363validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002364{
2365 int nch = NCH(tree);
2366 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002367 && ((nch == 1) || (nch == 3))
2368 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002369
2370 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002371 res = (validate_equal(CHILD(tree, 1))
2372 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002373
2374 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002375}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002376
2377
2378
2379/* trailer:
2380 *
Guido van Rossum47478871996-08-21 14:32:37 +00002381 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002382 */
Guido van Rossum47478871996-08-21 14:32:37 +00002383static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002384validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002385{
2386 int nch = NCH(tree);
2387 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002388
2389 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002390 switch (TYPE(CHILD(tree, 0))) {
2391 case LPAR:
2392 res = validate_rparen(CHILD(tree, nch - 1));
2393 if (res && (nch == 3))
2394 res = validate_arglist(CHILD(tree, 1));
2395 break;
2396 case LSQB:
2397 res = (validate_numnodes(tree, 3, "trailer")
2398 && validate_subscriptlist(CHILD(tree, 1))
2399 && validate_ntype(CHILD(tree, 2), RSQB));
2400 break;
2401 case DOT:
2402 res = (validate_numnodes(tree, 2, "trailer")
2403 && validate_ntype(CHILD(tree, 1), NAME));
2404 break;
2405 default:
2406 res = 0;
2407 break;
2408 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002409 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002410 else {
2411 (void) validate_numnodes(tree, 2, "trailer");
2412 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002413 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002414}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002415
2416
Guido van Rossum47478871996-08-21 14:32:37 +00002417/* subscriptlist:
2418 *
2419 * subscript (',' subscript)* [',']
2420 */
2421static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002422validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002423{
2424 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002425 validate_subscript, "subscriptlist"));
2426}
Guido van Rossum47478871996-08-21 14:32:37 +00002427
2428
Guido van Rossum3d602e31996-07-21 02:33:56 +00002429/* subscript:
2430 *
Guido van Rossum47478871996-08-21 14:32:37 +00002431 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002432 */
Guido van Rossum47478871996-08-21 14:32:37 +00002433static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002434validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002435{
Guido van Rossum47478871996-08-21 14:32:37 +00002436 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002437 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002438 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002439
Guido van Rossum47478871996-08-21 14:32:37 +00002440 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002441 if (!PyErr_Occurred())
2442 err_string("invalid number of arguments for subscript node");
2443 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002444 }
Guido van Rossum47478871996-08-21 14:32:37 +00002445 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002446 /* take care of ('.' '.' '.') possibility */
2447 return (validate_numnodes(tree, 3, "subscript")
2448 && validate_dot(CHILD(tree, 0))
2449 && validate_dot(CHILD(tree, 1))
2450 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002451 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002452 if (TYPE(CHILD(tree, 0)) == test)
2453 res = validate_test(CHILD(tree, 0));
2454 else
2455 res = validate_colon(CHILD(tree, 0));
2456 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002457 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002458 /* Must be [test] ':' [test] [sliceop],
2459 * but at least one of the optional components will
2460 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002461 */
2462 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002463 res = validate_test(CHILD(tree, 0));
2464 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002465 }
2466 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002467 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002468 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002469 int rem = nch - ++offset;
2470 if (rem) {
2471 if (TYPE(CHILD(tree, offset)) == test) {
2472 res = validate_test(CHILD(tree, offset));
2473 ++offset;
2474 --rem;
2475 }
2476 if (res && rem)
2477 res = validate_sliceop(CHILD(tree, offset));
2478 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002479 }
2480 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002481}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002482
2483
Guido van Rossum47478871996-08-21 14:32:37 +00002484static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002485validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002486{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002487 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002488 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002489 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002490 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002491 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002492 }
Guido van Rossum47478871996-08-21 14:32:37 +00002493 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002494 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002495 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002496 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002497
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002498 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002499}
Guido van Rossum47478871996-08-21 14:32:37 +00002500
2501
2502static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002503validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002504{
2505 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002506 validate_expr, "exprlist"));
2507}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002508
2509
Guido van Rossum47478871996-08-21 14:32:37 +00002510static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002511validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002512{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002513 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002514 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002515 && (nch >= 3)
2516 && validate_test(CHILD(tree, 0))
2517 && validate_colon(CHILD(tree, 1))
2518 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002519
Guido van Rossum3d602e31996-07-21 02:33:56 +00002520 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002521 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002522 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002523 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002524
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002525 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002526 int pos = 3;
2527 /* ( ',' test ':' test )* */
2528 while (res && (pos < nch)) {
2529 res = (validate_comma(CHILD(tree, pos))
2530 && validate_test(CHILD(tree, pos + 1))
2531 && validate_colon(CHILD(tree, pos + 2))
2532 && validate_test(CHILD(tree, pos + 3)));
2533 pos += 4;
2534 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002535 }
2536 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002537}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002538
2539
Guido van Rossum47478871996-08-21 14:32:37 +00002540static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002541validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002542{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002543 int pos;
2544 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002545 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002546 && (nch >= 2)
2547 && validate_testlist(CHILD(tree, 0))
2548 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002549
Guido van Rossum3d602e31996-07-21 02:33:56 +00002550 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002551 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002552
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002553 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002554}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002555
2556
Guido van Rossum47478871996-08-21 14:32:37 +00002557static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002558validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002559{
Fred Drakeff9ea482000-04-19 13:54:15 +00002560 int nch = 0; /* num. children on current node */
2561 int res = 1; /* result value */
2562 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002563
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002564 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002565 nch = NCH(tree);
2566 next = 0;
2567 switch (TYPE(tree)) {
2568 /*
2569 * Definition nodes.
2570 */
2571 case funcdef:
2572 res = validate_funcdef(tree);
2573 break;
2574 case classdef:
2575 res = validate_class(tree);
2576 break;
2577 /*
2578 * "Trivial" parse tree nodes.
2579 * (Why did I call these trivial?)
2580 */
2581 case stmt:
2582 res = validate_stmt(tree);
2583 break;
2584 case small_stmt:
2585 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002586 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002587 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2588 */
2589 res = validate_small_stmt(tree);
2590 break;
2591 case flow_stmt:
2592 res = (validate_numnodes(tree, 1, "flow_stmt")
2593 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2594 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002595 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002596 || (TYPE(CHILD(tree, 0)) == return_stmt)
2597 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2598 if (res)
2599 next = CHILD(tree, 0);
2600 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002601 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002602 break;
Fred Drake02126f22001-07-17 02:59:15 +00002603 case yield_stmt:
2604 res = validate_yield_stmt(tree);
2605 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002606 /*
2607 * Compound statements.
2608 */
2609 case simple_stmt:
2610 res = validate_simple_stmt(tree);
2611 break;
2612 case compound_stmt:
2613 res = validate_compound_stmt(tree);
2614 break;
2615 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002616 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002617 */
2618 case expr_stmt:
2619 res = validate_expr_stmt(tree);
2620 break;
2621 case print_stmt:
2622 res = validate_print_stmt(tree);
2623 break;
2624 case del_stmt:
2625 res = validate_del_stmt(tree);
2626 break;
2627 case pass_stmt:
2628 res = (validate_numnodes(tree, 1, "pass")
2629 && validate_name(CHILD(tree, 0), "pass"));
2630 break;
2631 case break_stmt:
2632 res = (validate_numnodes(tree, 1, "break")
2633 && validate_name(CHILD(tree, 0), "break"));
2634 break;
2635 case continue_stmt:
2636 res = (validate_numnodes(tree, 1, "continue")
2637 && validate_name(CHILD(tree, 0), "continue"));
2638 break;
2639 case return_stmt:
2640 res = validate_return_stmt(tree);
2641 break;
2642 case raise_stmt:
2643 res = validate_raise_stmt(tree);
2644 break;
2645 case import_stmt:
2646 res = validate_import_stmt(tree);
2647 break;
2648 case global_stmt:
2649 res = validate_global_stmt(tree);
2650 break;
2651 case exec_stmt:
2652 res = validate_exec_stmt(tree);
2653 break;
2654 case assert_stmt:
2655 res = validate_assert_stmt(tree);
2656 break;
2657 case if_stmt:
2658 res = validate_if(tree);
2659 break;
2660 case while_stmt:
2661 res = validate_while(tree);
2662 break;
2663 case for_stmt:
2664 res = validate_for(tree);
2665 break;
2666 case try_stmt:
2667 res = validate_try(tree);
2668 break;
2669 case suite:
2670 res = validate_suite(tree);
2671 break;
2672 /*
2673 * Expression nodes.
2674 */
2675 case testlist:
2676 res = validate_testlist(tree);
2677 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002678 case testlist1:
2679 res = validate_testlist1(tree);
2680 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002681 case test:
2682 res = validate_test(tree);
2683 break;
2684 case and_test:
2685 res = validate_and_test(tree);
2686 break;
2687 case not_test:
2688 res = validate_not_test(tree);
2689 break;
2690 case comparison:
2691 res = validate_comparison(tree);
2692 break;
2693 case exprlist:
2694 res = validate_exprlist(tree);
2695 break;
2696 case comp_op:
2697 res = validate_comp_op(tree);
2698 break;
2699 case expr:
2700 res = validate_expr(tree);
2701 break;
2702 case xor_expr:
2703 res = validate_xor_expr(tree);
2704 break;
2705 case and_expr:
2706 res = validate_and_expr(tree);
2707 break;
2708 case shift_expr:
2709 res = validate_shift_expr(tree);
2710 break;
2711 case arith_expr:
2712 res = validate_arith_expr(tree);
2713 break;
2714 case term:
2715 res = validate_term(tree);
2716 break;
2717 case factor:
2718 res = validate_factor(tree);
2719 break;
2720 case power:
2721 res = validate_power(tree);
2722 break;
2723 case atom:
2724 res = validate_atom(tree);
2725 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002726
Fred Drakeff9ea482000-04-19 13:54:15 +00002727 default:
2728 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002729 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002730 res = 0;
2731 break;
2732 }
2733 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002734 }
2735 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002736}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002737
2738
Guido van Rossum47478871996-08-21 14:32:37 +00002739static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002740validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002741{
2742 int res = validate_eval_input(tree);
2743
2744 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002745 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002746
2747 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002748}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002749
2750
Guido van Rossum3d602e31996-07-21 02:33:56 +00002751/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002752 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002753 */
Guido van Rossum47478871996-08-21 14:32:37 +00002754static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002755validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002756{
Fred Drakec2683dd2001-07-17 19:32:05 +00002757 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002758 int nch = NCH(tree) - 1;
2759 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002760 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002761
Fred Drakec2683dd2001-07-17 19:32:05 +00002762 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002763 if (TYPE(CHILD(tree, j)) == stmt)
2764 res = validate_stmt(CHILD(tree, j));
2765 else
2766 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002767 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002768 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002769 * user. Hopefully, this won't be needed. If a user reports getting
2770 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002771 */
2772 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002773 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002774
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002775 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002776}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002777
2778
Fred Drake43f8f9b1998-04-13 16:25:46 +00002779static PyObject*
2780pickle_constructor = NULL;
2781
2782
2783static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002784parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002785{
Fred Drake268397f1998-04-29 14:16:32 +00002786 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002787 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00002788 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002789 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002790
Fred Drakec2683dd2001-07-17 19:32:05 +00002791 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002792 PyObject *newargs;
2793 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002794
Fred Drake2a6875e1999-09-20 22:32:18 +00002795 if ((empty_dict = PyDict_New()) == NULL)
2796 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002797 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002798 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002799 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002800 if (tuple != NULL) {
2801 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2802 Py_DECREF(tuple);
2803 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002804 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002805 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002806 }
2807 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002808 Py_XDECREF(empty_dict);
2809
Fred Drake43f8f9b1998-04-13 16:25:46 +00002810 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002811}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002812
2813
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002814/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00002815 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002816 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002817 * We'd really have to write a wrapper around it all anyway to allow
2818 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002819 */
2820static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00002821 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002822 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002823 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002824 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002825 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002826 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002827 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002828 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002829 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002830 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002831 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002832 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002833 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002834 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002835 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002836 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002837 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002838 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002839 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002840 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002841 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002842 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002843 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002844 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002845 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002846 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002847 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002848 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002849
Fred Drake43f8f9b1998-04-13 16:25:46 +00002850 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00002851 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002852 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00002853
Fred Drake268397f1998-04-29 14:16:32 +00002854 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002855 };
2856
2857
Mark Hammond62b1ab12002-07-23 06:31:15 +00002858PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00002859
Mark Hammond62b1ab12002-07-23 06:31:15 +00002860PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00002861initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00002862{
Fred Drake13130bc2001-08-15 16:44:56 +00002863 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00002864
2865 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002866 module = Py_InitModule("parser", parser_functions);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002867
Fred Drake7a15ba51999-09-09 14:21:52 +00002868 if (parser_error == 0)
2869 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002870
2871 if ((parser_error == 0)
Fred Drake13130bc2001-08-15 16:44:56 +00002872 || (PyModule_AddObject(module, "ParserError", parser_error) != 0)) {
Fred Drakec2683dd2001-07-17 19:32:05 +00002873 /* caller will check PyErr_Occurred() */
2874 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002875 }
Fred Drakec2683dd2001-07-17 19:32:05 +00002876 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002877 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00002878 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002879 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002880
Fred Drake13130bc2001-08-15 16:44:56 +00002881 PyModule_AddStringConstant(module, "__copyright__",
2882 parser_copyright_string);
2883 PyModule_AddStringConstant(module, "__doc__",
2884 parser_doc_string);
2885 PyModule_AddStringConstant(module, "__version__",
2886 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002887
Fred Drake78bdb9b2001-07-19 20:17:15 +00002888 /* Register to support pickling.
2889 * If this fails, the import of this module will fail because an
2890 * exception will be raised here; should we clear the exception?
2891 */
Fred Drake13130bc2001-08-15 16:44:56 +00002892 copyreg = PyImport_ImportModule("copy_reg");
2893 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002894 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002895
Fred Drake13130bc2001-08-15 16:44:56 +00002896 func = PyObject_GetAttrString(copyreg, "pickle");
2897 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
2898 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00002899 Py_XINCREF(pickle_constructor);
2900 if ((func != NULL) && (pickle_constructor != NULL)
2901 && (pickler != NULL)) {
2902 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002903
Fred Drakec2683dd2001-07-17 19:32:05 +00002904 res = PyObject_CallFunction(func, "OOO", &PyST_Type, pickler,
2905 pickle_constructor);
Fred Drakeff9ea482000-04-19 13:54:15 +00002906 Py_XDECREF(res);
2907 }
2908 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00002909 Py_XDECREF(pickle_constructor);
2910 Py_XDECREF(pickler);
2911 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002912 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002913}