blob: 24dec1f208170d2289fd8563bdec58dd09d8f748 [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 */
49static char*
50parser_copyright_string
Guido van Rossum2a288461996-08-21 21:55:43 +000051= "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
52University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
53Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
54Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000055
56
57static char*
58parser_doc_string
59= "This is an interface to Python's internal parser.";
60
61static char*
Fred Drakecff283c2000-08-21 22:24:43 +000062parser_version_string = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000063
64
Fred Drakeff9ea482000-04-19 13:54:15 +000065typedef PyObject* (*SeqMaker) (int length);
66typedef int (*SeqInserter) (PyObject* sequence,
67 int index,
68 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000069
Thomas Wouters7e474022000-07-16 12:04:32 +000070/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000071 * original copyright statement is included below, and continues to apply
72 * in full to the function immediately following. All other material is
73 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
74 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000075 * new naming conventions. Added arguments to provide support for creating
76 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000077 */
78
Guido van Rossum52f2c051993-11-10 12:53:24 +000079
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000080static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000081node2tuple(node *n, /* node to convert */
82 SeqMaker mkseq, /* create sequence */
83 SeqInserter addelem, /* func. to add elem. in seq. */
84 int lineno) /* include line numbers? */
Guido van Rossum47478871996-08-21 14:32:37 +000085{
Guido van Rossum3d602e31996-07-21 02:33:56 +000086 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000087 Py_INCREF(Py_None);
88 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000089 }
90 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000091 int i;
92 PyObject *v;
93 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000094
Fred Drakeff9ea482000-04-19 13:54:15 +000095 v = mkseq(1 + NCH(n));
96 if (v == NULL)
97 return (v);
98 w = PyInt_FromLong(TYPE(n));
99 if (w == NULL) {
100 Py_DECREF(v);
101 return ((PyObject*) NULL);
102 }
103 (void) addelem(v, 0, w);
104 for (i = 0; i < NCH(n); i++) {
105 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno);
106 if (w == NULL) {
107 Py_DECREF(v);
108 return ((PyObject*) NULL);
109 }
110 (void) addelem(v, i+1, w);
111 }
112 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
Fred Drake268397f1998-04-29 14:16:32 +0000163staticforward void
Fred Drakec2683dd2001-07-17 19:32:05 +0000164parser_free(PyST_Object *st);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000165
Fred Drake268397f1998-04-29 14:16:32 +0000166staticforward int
Fred Drakec2683dd2001-07-17 19:32:05 +0000167parser_compare(PyST_Object *left, PyST_Object *right);
Fred Drake268397f1998-04-29 14:16:32 +0000168
169staticforward PyObject *
Fred Drakeff9ea482000-04-19 13:54:15 +0000170parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000171
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000172
Fred Drake268397f1998-04-29 14:16:32 +0000173static
Fred Drakec2683dd2001-07-17 19:32:05 +0000174PyTypeObject PyST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000175 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000176 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000177 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000178 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000179 0, /* tp_itemsize */
180 (destructor)parser_free, /* tp_dealloc */
181 0, /* tp_print */
182 parser_getattr, /* tp_getattr */
183 0, /* tp_setattr */
184 (cmpfunc)parser_compare, /* tp_compare */
185 0, /* tp_repr */
186 0, /* tp_as_number */
187 0, /* tp_as_sequence */
188 0, /* tp_as_mapping */
189 0, /* tp_hash */
190 0, /* tp_call */
191 0, /* tp_str */
192 0, /* tp_getattro */
193 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000194
195 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000196 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000197
Fred Drakeff9ea482000-04-19 13:54:15 +0000198 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000199
200 /* __doc__ */
201 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000202}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000203
204
205static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000206parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000207{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000208 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000209
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000210 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000211 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000212
213 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000214 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000215
216 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000217 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000218
219 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000220 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000221
222 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000223 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000224
225 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000226 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000227
Fred Drakeff9ea482000-04-19 13:54:15 +0000228 if (v != 0)
229 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000230 }
231 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000232}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000233
234
Fred Drakec2683dd2001-07-17 19:32:05 +0000235/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000236 *
237 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
238 * This really just wraps a call to parser_compare_nodes() with some easy
239 * checks and protection code.
240 *
241 */
242static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000243parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000244{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000245 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000246 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247
248 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000249 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000250
Fred Drakec2683dd2001-07-17 19:32:05 +0000251 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000252}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000253
254
Fred Drakec2683dd2001-07-17 19:32:05 +0000255/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000256 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000257 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000258 * 'wrapper' object that holds a node* and allows it to be passed around in
259 * Python code.
260 *
261 */
262static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000263parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000264{
Fred Drakec2683dd2001-07-17 19:32:05 +0000265 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000266
267 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000268 o->st_node = st;
269 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000270 }
Fred Drake268397f1998-04-29 14:16:32 +0000271 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000272 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000273 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000274 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000275}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000276
277
Fred Drakec2683dd2001-07-17 19:32:05 +0000278/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000279 *
280 * This is called by a del statement that reduces the reference count to 0.
281 *
282 */
283static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000284parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000285{
Fred Drakec2683dd2001-07-17 19:32:05 +0000286 PyNode_Free(st->st_node);
287 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000288}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000289
290
Fred Drakec2683dd2001-07-17 19:32:05 +0000291/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000292 *
293 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000294 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000295 *
296 */
297static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000298parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000299{
Guido van Rossum47478871996-08-21 14:32:37 +0000300 PyObject *line_option = 0;
301 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000302 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000303
Fred Drake7a15ba51999-09-09 14:21:52 +0000304 static char *keywords[] = {"ast", "line_info", NULL};
305
Fred Drake268397f1998-04-29 14:16:32 +0000306 if (self == NULL) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000307 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2tuple", keywords,
308 &PyST_Type, &self, &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000309 }
Fred Drake503d8d61998-04-13 18:45:18 +0000310 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000311 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:totuple", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000312 &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000313 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000314 int lineno = 0;
315 if (line_option != NULL) {
316 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
317 }
318 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000319 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000320 * since it's known to work already.
321 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000322 res = node2tuple(((PyST_Object*)self)->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000323 PyTuple_New, PyTuple_SetItem, lineno);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000324 }
325 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000326}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000327
328
Fred Drakec2683dd2001-07-17 19:32:05 +0000329/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000330 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000331 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000332 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000333 *
334 */
335static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000336parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000337{
Guido van Rossum47478871996-08-21 14:32:37 +0000338 PyObject *line_option = 0;
339 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000340 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000341
Fred Drake7a15ba51999-09-09 14:21:52 +0000342 static char *keywords[] = {"ast", "line_info", NULL};
343
Fred Drake503d8d61998-04-13 18:45:18 +0000344 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000345 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2list", keywords,
346 &PyST_Type, &self, &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000347 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000348 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:tolist", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000349 &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000350 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000351 int lineno = 0;
352 if (line_option != 0) {
353 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
354 }
355 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000356 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000357 * since it's known to work already.
358 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000359 res = node2tuple(self->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000360 PyList_New, PyList_SetItem, lineno);
Guido van Rossum47478871996-08-21 14:32:37 +0000361 }
362 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000363}
Guido van Rossum47478871996-08-21 14:32:37 +0000364
365
Fred Drakec2683dd2001-07-17 19:32:05 +0000366/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000367 *
368 * This function creates code objects from the parse tree represented by
369 * the passed-in data object. An optional file name is passed in as well.
370 *
371 */
372static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000373parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000374{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000375 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000376 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000377 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000378
Fred Drake7a15ba51999-09-09 14:21:52 +0000379 static char *keywords[] = {"ast", "filename", NULL};
380
Fred Drake503d8d61998-04-13 18:45:18 +0000381 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000382 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
383 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000384 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000385 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000386 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000387
388 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000389 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000390
391 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000392}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000393
394
395/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
396 * PyObject* parser_issuite(PyObject* self, PyObject* args)
397 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000398 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000399 * a statement suite, respectively. The return is a Python truth value.
400 *
401 */
402static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000403parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000404{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000405 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000406 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000407
Fred Drake7a15ba51999-09-09 14:21:52 +0000408 static char *keywords[] = {"ast", NULL};
409
Fred Drake503d8d61998-04-13 18:45:18 +0000410 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000411 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000412 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000413 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000414 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000415
416 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000417 /* Check to see if the ST represents an expression or not. */
418 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000419 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000420 }
421 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000422}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000423
424
425static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000426parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000427{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000428 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000429 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000430
Fred Drake7a15ba51999-09-09 14:21:52 +0000431 static char *keywords[] = {"ast", NULL};
432
Fred Drake503d8d61998-04-13 18:45:18 +0000433 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000434 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000435 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000436 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000437 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000438
439 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000440 /* Check to see if the ST represents an expression or not. */
441 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000442 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000443 }
444 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000445}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000446
447
Fred Drake7a15ba51999-09-09 14:21:52 +0000448#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
449
Fred Drake503d8d61998-04-13 18:45:18 +0000450static PyMethodDef
451parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000452 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
453 "Compile this ST object into a code object."},
Fred Drakeff9ea482000-04-19 13:54:15 +0000454 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Fred Drakec2683dd2001-07-17 19:32:05 +0000455 "Determines if this ST object was created from an expression."},
Fred Drakeff9ea482000-04-19 13:54:15 +0000456 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Fred Drakec2683dd2001-07-17 19:32:05 +0000457 "Determines if this ST object was created from a suite."},
458 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
459 "Creates a list-tree representation of this ST."},
460 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
461 "Creates a tuple-tree representation of this ST."},
Fred Drake503d8d61998-04-13 18:45:18 +0000462
Fred Drake268397f1998-04-29 14:16:32 +0000463 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000464};
465
Fred Drake503d8d61998-04-13 18:45:18 +0000466
467static PyObject*
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000468parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000469{
Fred Drake503d8d61998-04-13 18:45:18 +0000470 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000471}
Fred Drake503d8d61998-04-13 18:45:18 +0000472
473
Guido van Rossum3d602e31996-07-21 02:33:56 +0000474/* err_string(char* message)
475 *
476 * Sets the error string for an exception of type ParserError.
477 *
478 */
479static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000480err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000481{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000482 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000483}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000484
485
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000486/* PyObject* parser_do_parse(PyObject* args, int type)
487 *
488 * Internal function to actually execute the parse and return the result if
489 * successful, or set an exception if not.
490 *
491 */
492static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000493parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000494{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000495 char* string = 0;
496 PyObject* res = 0;
497
Fred Drake7a15ba51999-09-09 14:21:52 +0000498 static char *keywords[] = {"source", NULL};
499
500 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000501 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000502 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000503 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000504
Fred Drakeff9ea482000-04-19 13:54:15 +0000505 if (n != 0)
Fred Drakec2683dd2001-07-17 19:32:05 +0000506 res = parser_newstobject(n, type);
Fred Drakeff9ea482000-04-19 13:54:15 +0000507 else
Fred Drake661ea262000-10-24 19:57:45 +0000508 err_string("could not parse string");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000509 }
510 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000511}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000512
513
514/* PyObject* parser_expr(PyObject* self, PyObject* args)
515 * PyObject* parser_suite(PyObject* self, PyObject* args)
516 *
517 * External interfaces to the parser itself. Which is called determines if
518 * the parser attempts to recognize an expression ('eval' form) or statement
519 * suite ('exec' form). The real work is done by parser_do_parse() above.
520 *
521 */
522static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000523parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000524{
Fred Drake268397f1998-04-29 14:16:32 +0000525 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000526 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000527}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000528
529
530static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000531parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000532{
Fred Drake268397f1998-04-29 14:16:32 +0000533 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000534 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000535}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000536
537
538
Fred Drakec2683dd2001-07-17 19:32:05 +0000539/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000540 * object requires that the input tuple be valid without having to rely on
541 * catching an exception from the compiler. This is done to allow the
542 * compiler itself to remain fast, since most of its input will come from
543 * the parser directly, and therefore be known to be syntactically correct.
544 * This validation is done to ensure that we don't core dump the compile
545 * phase, returning an exception instead.
546 *
547 * Two aspects can be broken out in this code: creating a node tree from
548 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000549 * advantageous to expand the number of ST types to include funcdefs and
550 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000551 * here. They are not necessary, and not quite as useful in a raw form.
552 * For now, let's get expressions and suites working reliably.
553 */
554
555
Fred Drakeff9ea482000-04-19 13:54:15 +0000556staticforward node* build_node_tree(PyObject *tuple);
557staticforward int validate_expr_tree(node *tree);
558staticforward int validate_file_input(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000559
560
Fred Drakec2683dd2001-07-17 19:32:05 +0000561/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000562 *
563 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000564 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000565 * tuple can be validated. It does this by checking the first code of the
566 * tuple, and, if acceptable, builds the internal representation. If this
567 * step succeeds, the internal representation is validated as fully as
568 * possible with the various validate_*() routines defined below.
569 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000570 * This function must be changed if support is to be added for PyST_FRAGMENT
571 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000572 *
573 */
574static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000575parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000576{
Fred Drake268397f1998-04-29 14:16:32 +0000577 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000578 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000579 PyObject *tuple;
580 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000581
Fred Drake7a15ba51999-09-09 14:21:52 +0000582 static char *keywords[] = {"sequence", NULL};
583
Fred Drakec2683dd2001-07-17 19:32:05 +0000584 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000585 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000586 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000587 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000588 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000589 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000590 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000591 }
592 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000593 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000594 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000595 tree = build_node_tree(tuple);
596 if (tree != 0) {
597 int start_sym = TYPE(tree);
598 if (start_sym == eval_input) {
599 /* Might be an eval form. */
600 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000601 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000602 else
603 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000604 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000605 else if (start_sym == file_input) {
606 /* This looks like an exec form so far. */
607 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000608 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000609 else
610 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000611 }
612 else {
613 /* This is a fragment, at best. */
614 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000615 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000616 }
Guido van Rossum47478871996-08-21 14:32:37 +0000617 }
Guido van Rossum47478871996-08-21 14:32:37 +0000618 /* Make sure we throw an exception on all errors. We should never
619 * get this, but we'd do well to be sure something is done.
620 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000621 if (st == NULL && !PyErr_Occurred())
622 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000623
Fred Drakec2683dd2001-07-17 19:32:05 +0000624 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000625}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000626
627
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000628/* node* build_node_children()
629 *
630 * Iterate across the children of the current non-terminal node and build
631 * their structures. If successful, return the root of this portion of
632 * the tree, otherwise, 0. Any required exception will be specified already,
633 * and no memory will have been deallocated.
634 *
635 */
636static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000637build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000638{
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000639 int len = PyObject_Size(tuple);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000640 int i, err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000641
642 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000643 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000644 PyObject* elem = PySequence_GetItem(tuple, i);
645 int ok = elem != NULL;
646 long type = 0;
647 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000648
Fred Drakeff9ea482000-04-19 13:54:15 +0000649 if (ok)
650 ok = PySequence_Check(elem);
651 if (ok) {
652 PyObject *temp = PySequence_GetItem(elem, 0);
653 if (temp == NULL)
654 ok = 0;
655 else {
656 ok = PyInt_Check(temp);
657 if (ok)
658 type = PyInt_AS_LONG(temp);
659 Py_DECREF(temp);
660 }
661 }
662 if (!ok) {
663 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000664 Py_BuildValue("os", elem,
Fred Drakeff9ea482000-04-19 13:54:15 +0000665 "Illegal node construct."));
666 Py_XDECREF(elem);
667 return (0);
668 }
669 if (ISTERMINAL(type)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000670 int len = PyObject_Size(elem);
671 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000672
Fred Drake0ac9b072000-09-12 21:58:06 +0000673 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000674 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000675 return 0;
676 }
677 temp = PySequence_GetItem(elem, 1);
678 if (temp == NULL)
679 return 0;
680 if (!PyString_Check(temp)) {
681 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000682 "second item in terminal node must be a string,"
683 " found %s",
Fred Drake0ac9b072000-09-12 21:58:06 +0000684 ((PyTypeObject*)PyObject_Type(temp))->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000685 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000686 return 0;
687 }
688 if (len == 3) {
689 PyObject *o = PySequence_GetItem(elem, 2);
690 if (o != NULL) {
691 if (PyInt_Check(o))
692 *line_num = PyInt_AS_LONG(o);
693 else {
694 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000695 "third item in terminal node must be an"
696 " integer, found %s",
Fred Drake0ac9b072000-09-12 21:58:06 +0000697 ((PyTypeObject*)PyObject_Type(temp))->tp_name);
698 Py_DECREF(o);
699 Py_DECREF(temp);
700 return 0;
701 }
702 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000703 }
704 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000705 len = PyString_GET_SIZE(temp) + 1;
706 strn = (char *)PyMem_MALLOC(len);
707 if (strn != NULL)
708 (void) memcpy(strn, PyString_AS_STRING(temp), len);
709 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000710 }
711 else if (!ISNONTERMINAL(type)) {
712 /*
713 * It has to be one or the other; this is an error.
714 * Throw an exception.
715 */
716 PyErr_SetObject(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000717 Py_BuildValue("os", elem, "unknown node type."));
Fred Drakeff9ea482000-04-19 13:54:15 +0000718 Py_XDECREF(elem);
719 return (0);
720 }
Fred Drake8b55b2d2001-12-05 22:10:44 +0000721 err = PyNode_AddChild(root, type, strn, *line_num);
722 if (err == E_NOMEM) {
723 PyMem_DEL(strn);
724 return (node *) PyErr_NoMemory();
725 }
726 if (err == E_OVERFLOW) {
727 PyMem_DEL(strn);
728 PyErr_SetString(PyExc_ValueError,
729 "unsupported number of child nodes");
730 return NULL;
731 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000732
Fred Drakeff9ea482000-04-19 13:54:15 +0000733 if (ISNONTERMINAL(type)) {
734 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000735
Fred Drakeff9ea482000-04-19 13:54:15 +0000736 if (new_child != build_node_children(elem, new_child, line_num)) {
737 Py_XDECREF(elem);
738 return (0);
739 }
740 }
741 else if (type == NEWLINE) { /* It's true: we increment the */
742 ++(*line_num); /* line number *after* the newline! */
743 }
744 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000745 }
746 return (root);
Fred Drakeff9ea482000-04-19 13:54:15 +0000747}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000748
749
750static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000751build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000752{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000753 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000754 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000755 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000756
Guido van Rossum47478871996-08-21 14:32:37 +0000757 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000758 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000759 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000760 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000761 /*
762 * The tuple is simple, but it doesn't start with a start symbol.
763 * Throw an exception now and be done with it.
764 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000765 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000766 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000767 PyErr_SetObject(parser_error, tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000768 }
769 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000770 /*
771 * Not efficient, but that can be handled later.
772 */
773 int line_num = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000774
Fred Drakeff9ea482000-04-19 13:54:15 +0000775 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000776 if (res != NULL) {
777 if (res != build_node_children(tuple, res, &line_num)) {
778 PyNode_Free(res);
779 res = NULL;
780 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000781 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000782 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000783 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000784 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000785 * NONTERMINAL, we can't use it. Not sure the implementation
786 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000787 */
788 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000789 Py_BuildValue("os", tuple,
Fred Drakeff9ea482000-04-19 13:54:15 +0000790 "Illegal component tuple."));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000791
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000792 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000793}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000794
795
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000796/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000797 * Validation routines used within the validation section:
798 */
Fred Drakeff9ea482000-04-19 13:54:15 +0000799staticforward int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000800
Fred Drakeff9ea482000-04-19 13:54:15 +0000801#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
802#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
803#define validate_colon(ch) validate_terminal(ch, COLON, ":")
804#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
805#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
806#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
807#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
808#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
809#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
810#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
811#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
812#define validate_star(ch) validate_terminal(ch, STAR, "*")
813#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
814#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
815#define validate_dot(ch) validate_terminal(ch, DOT, ".")
816#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000817
Fred Drake0ac9b072000-09-12 21:58:06 +0000818#define VALIDATER(n) static int validate_##n(node *tree)
819
Fred Drakeff9ea482000-04-19 13:54:15 +0000820VALIDATER(node); VALIDATER(small_stmt);
821VALIDATER(class); VALIDATER(node);
822VALIDATER(parameters); VALIDATER(suite);
823VALIDATER(testlist); VALIDATER(varargslist);
824VALIDATER(fpdef); VALIDATER(fplist);
825VALIDATER(stmt); VALIDATER(simple_stmt);
826VALIDATER(expr_stmt); VALIDATER(power);
827VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000828VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000829VALIDATER(raise_stmt); VALIDATER(import_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000830VALIDATER(global_stmt); VALIDATER(list_if);
831VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000832VALIDATER(exec_stmt); VALIDATER(compound_stmt);
833VALIDATER(while); VALIDATER(for);
834VALIDATER(try); VALIDATER(except_clause);
835VALIDATER(test); VALIDATER(and_test);
836VALIDATER(not_test); VALIDATER(comparison);
837VALIDATER(comp_op); VALIDATER(expr);
838VALIDATER(xor_expr); VALIDATER(and_expr);
839VALIDATER(shift_expr); VALIDATER(arith_expr);
840VALIDATER(term); VALIDATER(factor);
841VALIDATER(atom); VALIDATER(lambdef);
842VALIDATER(trailer); VALIDATER(subscript);
843VALIDATER(subscriptlist); VALIDATER(sliceop);
844VALIDATER(exprlist); VALIDATER(dictmaker);
845VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000846VALIDATER(listmaker); VALIDATER(yield_stmt);
Guido van Rossum2d3b9862002-05-24 15:47:06 +0000847VALIDATER(testlist1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000848
Fred Drake0ac9b072000-09-12 21:58:06 +0000849#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000850
Fred Drakeff9ea482000-04-19 13:54:15 +0000851#define is_even(n) (((n) & 1) == 0)
852#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000853
854
855static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000856validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000857{
Fred Drake0ac9b072000-09-12 21:58:06 +0000858 if (TYPE(n) != t) {
859 PyErr_Format(parser_error, "Expected node type %d, got %d.",
860 t, TYPE(n));
861 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000862 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000863 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000864}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000865
866
Fred Drakee7ab64e2000-04-25 04:14:46 +0000867/* Verifies that the number of child nodes is exactly 'num', raising
868 * an exception if it isn't. The exception message does not indicate
869 * the exact number of nodes, allowing this to be used to raise the
870 * "right" exception when the wrong number of nodes is present in a
871 * specific variant of a statement's syntax. This is commonly used
872 * in that fashion.
873 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000874static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000875validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000876{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000877 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000878 PyErr_Format(parser_error,
879 "Illegal number of children for %s node.", name);
880 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000881 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000882 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000883}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000884
885
886static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000887validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000888{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000889 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000890 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000891
892 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000893 PyErr_Format(parser_error,
894 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000895 }
896 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000897}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000898
899
Guido van Rossum47478871996-08-21 14:32:37 +0000900/* X (',' X) [',']
901 */
902static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000903validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000904 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000905{
906 int nch = NCH(tree);
907 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000908 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000909
910 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000911 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000912 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000913 if (is_even(nch))
914 res = validate_comma(CHILD(tree, --nch));
915 if (res && nch > 1) {
916 int pos = 1;
917 for ( ; res && pos < nch; pos += 2)
918 res = (validate_comma(CHILD(tree, pos))
919 && vfunc(CHILD(tree, pos + 1)));
920 }
Guido van Rossum47478871996-08-21 14:32:37 +0000921 }
922 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000923}
Guido van Rossum47478871996-08-21 14:32:37 +0000924
925
Fred Drakecff283c2000-08-21 22:24:43 +0000926/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000927 *
928 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000929 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000930 */
Guido van Rossum47478871996-08-21 14:32:37 +0000931static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000932validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000933{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000934 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000935 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000936
Guido van Rossum3d602e31996-07-21 02:33:56 +0000937 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000938 res = (validate_name(CHILD(tree, 0), "class")
939 && validate_ntype(CHILD(tree, 1), NAME)
940 && validate_colon(CHILD(tree, nch - 2))
941 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000942 }
943 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000944 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000945 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000946 res = (validate_lparen(CHILD(tree, 2))
947 && validate_testlist(CHILD(tree, 3))
948 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000949 }
950 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000951}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000952
953
Guido van Rossum3d602e31996-07-21 02:33:56 +0000954/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +0000955 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +0000956 */
Guido van Rossum47478871996-08-21 14:32:37 +0000957static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000958validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000959{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000960 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000961 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +0000962 && (nch >= 4)
963 && validate_name(CHILD(tree, 0), "if")
964 && validate_test(CHILD(tree, 1))
965 && validate_colon(CHILD(tree, 2))
966 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000967
968 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000969 /* ... 'else' ':' suite */
970 res = (validate_name(CHILD(tree, nch - 3), "else")
971 && validate_colon(CHILD(tree, nch - 2))
972 && validate_suite(CHILD(tree, nch - 1)));
973 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000974 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000975 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000976 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000977 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +0000978 /* Will catch the case for nch < 4 */
979 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000980 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000981 /* ... ('elif' test ':' suite)+ ... */
982 int j = 4;
983 while ((j < nch) && res) {
984 res = (validate_name(CHILD(tree, j), "elif")
985 && validate_colon(CHILD(tree, j + 2))
986 && validate_test(CHILD(tree, j + 1))
987 && validate_suite(CHILD(tree, j + 3)));
988 j += 4;
989 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000990 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000991 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000992}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000993
994
Guido van Rossum3d602e31996-07-21 02:33:56 +0000995/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +0000996 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +0000997 *
998 */
Guido van Rossum47478871996-08-21 14:32:37 +0000999static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001000validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001001{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001002 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001003 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001004
Guido van Rossum3d602e31996-07-21 02:33:56 +00001005 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001006 res = (validate_lparen(CHILD(tree, 0))
1007 && validate_rparen(CHILD(tree, nch - 1)));
1008 if (res && (nch == 3))
1009 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001010 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001011 else {
1012 (void) validate_numnodes(tree, 2, "parameters");
1013 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001014 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001015}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001016
1017
Fred Drakecff283c2000-08-21 22:24:43 +00001018/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001019 *
1020 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001021 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001022 * | NEWLINE INDENT stmt+ DEDENT
1023 */
Guido van Rossum47478871996-08-21 14:32:37 +00001024static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001025validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001026{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001027 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001028 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001029
Guido van Rossum3d602e31996-07-21 02:33:56 +00001030 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001031 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001032 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001033 /* NEWLINE INDENT stmt+ DEDENT */
1034 res = (validate_newline(CHILD(tree, 0))
1035 && validate_indent(CHILD(tree, 1))
1036 && validate_stmt(CHILD(tree, 2))
1037 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001038
Fred Drakeff9ea482000-04-19 13:54:15 +00001039 if (res && (nch > 4)) {
1040 int i = 3;
1041 --nch; /* forget the DEDENT */
1042 for ( ; res && (i < nch); ++i)
1043 res = validate_stmt(CHILD(tree, i));
1044 }
1045 else if (nch < 4)
1046 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001047 }
1048 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001049}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001050
1051
Guido van Rossum47478871996-08-21 14:32:37 +00001052static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001053validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001054{
Guido van Rossum47478871996-08-21 14:32:37 +00001055 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001056 validate_test, "testlist"));
1057}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001058
1059
Guido van Rossum1c917072001-10-15 15:44:05 +00001060static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001061validate_testlist1(node *tree)
1062{
1063 return (validate_repeating_list(tree, testlist1,
1064 validate_test, "testlist1"));
1065}
1066
1067
1068static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001069validate_testlist_safe(node *tree)
1070{
1071 return (validate_repeating_list(tree, testlist_safe,
1072 validate_test, "testlist_safe"));
1073}
1074
1075
Fred Drakecff283c2000-08-21 22:24:43 +00001076/* '*' NAME [',' '**' NAME] | '**' NAME
1077 */
1078static int
1079validate_varargslist_trailer(node *tree, int start)
1080{
1081 int nch = NCH(tree);
1082 int res = 0;
1083 int sym;
1084
1085 if (nch <= start) {
1086 err_string("expected variable argument trailer for varargslist");
1087 return 0;
1088 }
1089 sym = TYPE(CHILD(tree, start));
1090 if (sym == STAR) {
1091 /*
1092 * ('*' NAME [',' '**' NAME]
1093 */
1094 if (nch-start == 2)
1095 res = validate_name(CHILD(tree, start+1), NULL);
1096 else if (nch-start == 5)
1097 res = (validate_name(CHILD(tree, start+1), NULL)
1098 && validate_comma(CHILD(tree, start+2))
1099 && validate_doublestar(CHILD(tree, start+3))
1100 && validate_name(CHILD(tree, start+4), NULL));
1101 }
1102 else if (sym == DOUBLESTAR) {
1103 /*
1104 * '**' NAME
1105 */
1106 if (nch-start == 2)
1107 res = validate_name(CHILD(tree, start+1), NULL);
1108 }
1109 if (!res)
1110 err_string("illegal variable argument trailer for varargslist");
1111 return res;
1112}
1113
1114
1115/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001116 *
1117 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001118 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001119 * ('*' NAME [',' '**' NAME]
1120 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001121 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1122 *
1123 */
Guido van Rossum47478871996-08-21 14:32:37 +00001124static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001125validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001126{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001127 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001128 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001129 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001130
Fred Drakeb6429a22000-12-11 22:08:27 +00001131 if (!res)
1132 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001133 if (nch < 1) {
1134 err_string("varargslist missing child nodes");
1135 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001136 }
Fred Drakecff283c2000-08-21 22:24:43 +00001137 sym = TYPE(CHILD(tree, 0));
1138 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001139 /* whole thing matches:
1140 * '*' NAME [',' '**' NAME] | '**' NAME
1141 */
Fred Drakecff283c2000-08-21 22:24:43 +00001142 res = validate_varargslist_trailer(tree, 0);
1143 else if (sym == fpdef) {
1144 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001145
Fred Drakecff283c2000-08-21 22:24:43 +00001146 sym = TYPE(CHILD(tree, nch-1));
1147 if (sym == NAME) {
1148 /*
1149 * (fpdef ['=' test] ',')+
1150 * ('*' NAME [',' '**' NAME]
1151 * | '**' NAME)
1152 */
1153 /* skip over (fpdef ['=' test] ',')+ */
1154 while (res && (i+2 <= nch)) {
1155 res = validate_fpdef(CHILD(tree, i));
1156 ++i;
1157 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1158 res = (validate_equal(CHILD(tree, i))
1159 && validate_test(CHILD(tree, i+1)));
1160 if (res)
1161 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001162 }
Fred Drakecff283c2000-08-21 22:24:43 +00001163 if (res && i < nch) {
1164 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001165 ++i;
1166 if (res && i < nch
1167 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1168 || TYPE(CHILD(tree, i)) == STAR))
1169 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001170 }
1171 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001172 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1173 * i --^^^
1174 */
Fred Drakecff283c2000-08-21 22:24:43 +00001175 if (res)
1176 res = validate_varargslist_trailer(tree, i);
1177 }
1178 else {
1179 /*
1180 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1181 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001182 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001183 if (sym == COMMA) {
1184 res = validate_comma(CHILD(tree, nch-1));
1185 if (!res)
1186 return 0;
1187 --nch;
1188 }
1189 /*
1190 * fpdef ['=' test] (',' fpdef ['=' test])*
1191 */
1192 res = validate_fpdef(CHILD(tree, 0));
1193 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001194 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1195 res = (validate_equal(CHILD(tree, i))
1196 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001197 i += 2;
1198 }
1199 /*
1200 * ... (',' fpdef ['=' test])*
1201 * i ---^^^
1202 */
1203 while (res && (nch - i) >= 2) {
1204 res = (validate_comma(CHILD(tree, i))
1205 && validate_fpdef(CHILD(tree, i+1)));
1206 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001207 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1208 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001209 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001210 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001211 }
1212 }
1213 if (res && nch - i != 0) {
1214 res = 0;
1215 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001216 }
1217 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001218 }
Fred Drakecff283c2000-08-21 22:24:43 +00001219 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001220}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001221
1222
Fred Drakecff283c2000-08-21 22:24:43 +00001223/* list_iter: list_for | list_if
1224 */
1225static int
1226validate_list_iter(node *tree)
1227{
1228 int res = (validate_ntype(tree, list_iter)
1229 && validate_numnodes(tree, 1, "list_iter"));
1230 if (res && TYPE(CHILD(tree, 0)) == list_for)
1231 res = validate_list_for(CHILD(tree, 0));
1232 else
1233 res = validate_list_if(CHILD(tree, 0));
1234
1235 return res;
1236}
1237
1238/* list_for: 'for' exprlist 'in' testlist [list_iter]
1239 */
1240static int
1241validate_list_for(node *tree)
1242{
1243 int nch = NCH(tree);
1244 int res;
1245
1246 if (nch == 5)
1247 res = validate_list_iter(CHILD(tree, 4));
1248 else
1249 res = validate_numnodes(tree, 4, "list_for");
1250
1251 if (res)
1252 res = (validate_name(CHILD(tree, 0), "for")
1253 && validate_exprlist(CHILD(tree, 1))
1254 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001255 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001256
1257 return res;
1258}
1259
1260/* list_if: 'if' test [list_iter]
1261 */
1262static int
1263validate_list_if(node *tree)
1264{
1265 int nch = NCH(tree);
1266 int res;
1267
1268 if (nch == 3)
1269 res = validate_list_iter(CHILD(tree, 2));
1270 else
1271 res = validate_numnodes(tree, 2, "list_if");
1272
1273 if (res)
1274 res = (validate_name(CHILD(tree, 0), "if")
1275 && validate_test(CHILD(tree, 1)));
1276
1277 return res;
1278}
1279
1280
1281/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001282 *
1283 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001284 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001285 * | '(' fplist ')'
1286 */
Guido van Rossum47478871996-08-21 14:32:37 +00001287static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001288validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001289{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001290 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001291 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001292
Guido van Rossum3d602e31996-07-21 02:33:56 +00001293 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001294 if (nch == 1)
1295 res = validate_ntype(CHILD(tree, 0), NAME);
1296 else if (nch == 3)
1297 res = (validate_lparen(CHILD(tree, 0))
1298 && validate_fplist(CHILD(tree, 1))
1299 && validate_rparen(CHILD(tree, 2)));
1300 else
1301 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001302 }
1303 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001304}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001305
1306
Guido van Rossum47478871996-08-21 14:32:37 +00001307static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001308validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001309{
Guido van Rossum47478871996-08-21 14:32:37 +00001310 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001311 validate_fpdef, "fplist"));
1312}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001313
1314
Guido van Rossum3d602e31996-07-21 02:33:56 +00001315/* simple_stmt | compound_stmt
1316 *
1317 */
Guido van Rossum47478871996-08-21 14:32:37 +00001318static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001319validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001320{
1321 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001322 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001323
Guido van Rossum3d602e31996-07-21 02:33:56 +00001324 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001325 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001326
Fred Drakeff9ea482000-04-19 13:54:15 +00001327 if (TYPE(tree) == simple_stmt)
1328 res = validate_simple_stmt(tree);
1329 else
1330 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001331 }
1332 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001333}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001334
1335
Guido van Rossum3d602e31996-07-21 02:33:56 +00001336/* small_stmt (';' small_stmt)* [';'] NEWLINE
1337 *
1338 */
Guido van Rossum47478871996-08-21 14:32:37 +00001339static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001340validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001341{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001342 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001343 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001344 && (nch >= 2)
1345 && validate_small_stmt(CHILD(tree, 0))
1346 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001347
Guido van Rossum3d602e31996-07-21 02:33:56 +00001348 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001349 res = validate_numnodes(tree, 2, "simple_stmt");
1350 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001351 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001352 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001353 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001354 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001355
Fred Drakeff9ea482000-04-19 13:54:15 +00001356 for (i = 1; res && (i < nch); i += 2)
1357 res = (validate_semi(CHILD(tree, i))
1358 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001359 }
1360 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001361}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001362
1363
Guido van Rossum47478871996-08-21 14:32:37 +00001364static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001365validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001366{
1367 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001368 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001369
Fred Drake0ac9b072000-09-12 21:58:06 +00001370 if (res) {
1371 int ntype = TYPE(CHILD(tree, 0));
1372
1373 if ( (ntype == expr_stmt)
1374 || (ntype == print_stmt)
1375 || (ntype == del_stmt)
1376 || (ntype == pass_stmt)
1377 || (ntype == flow_stmt)
1378 || (ntype == import_stmt)
1379 || (ntype == global_stmt)
1380 || (ntype == assert_stmt)
1381 || (ntype == exec_stmt))
1382 res = validate_node(CHILD(tree, 0));
1383 else {
1384 res = 0;
1385 err_string("illegal small_stmt child type");
1386 }
1387 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001388 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001389 res = 0;
1390 PyErr_Format(parser_error,
1391 "Unrecognized child node of small_stmt: %d.",
1392 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001393 }
1394 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001395}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001396
1397
1398/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001399 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001400 */
Guido van Rossum47478871996-08-21 14:32:37 +00001401static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001402validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001403{
1404 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001405 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001406 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001407
1408 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001409 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001410
1411 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001412 ntype = TYPE(tree);
1413 if ( (ntype == if_stmt)
1414 || (ntype == while_stmt)
1415 || (ntype == for_stmt)
1416 || (ntype == try_stmt)
1417 || (ntype == funcdef)
1418 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001419 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001420 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001421 res = 0;
1422 PyErr_Format(parser_error,
1423 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001424 }
1425 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001426}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001427
1428
Guido van Rossum47478871996-08-21 14:32:37 +00001429static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001430validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001431{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001432 int j;
1433 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001434 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001435 && is_odd(nch)
1436 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001437
Fred Drake28f739a2000-08-25 22:42:40 +00001438 if (res && nch == 3
1439 && TYPE(CHILD(tree, 1)) == augassign) {
1440 res = (validate_numnodes(CHILD(tree, 1), 1, "augassign")
1441 && validate_testlist(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001442
Fred Drake28f739a2000-08-25 22:42:40 +00001443 if (res) {
1444 char *s = STR(CHILD(CHILD(tree, 1), 0));
1445
1446 res = (strcmp(s, "+=") == 0
1447 || strcmp(s, "-=") == 0
1448 || strcmp(s, "*=") == 0
1449 || strcmp(s, "/=") == 0
1450 || strcmp(s, "%=") == 0
1451 || strcmp(s, "&=") == 0
1452 || strcmp(s, "|=") == 0
1453 || strcmp(s, "^=") == 0
1454 || strcmp(s, "<<=") == 0
1455 || strcmp(s, ">>=") == 0
1456 || strcmp(s, "**=") == 0);
1457 if (!res)
1458 err_string("illegal augmmented assignment operator");
1459 }
1460 }
1461 else {
1462 for (j = 1; res && (j < nch); j += 2)
1463 res = (validate_equal(CHILD(tree, j))
1464 && validate_testlist(CHILD(tree, j + 1)));
1465 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001466 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001467}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001468
1469
Guido van Rossum3d602e31996-07-21 02:33:56 +00001470/* print_stmt:
1471 *
Fred Drakecff283c2000-08-21 22:24:43 +00001472 * 'print' ( [ test (',' test)* [','] ]
1473 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001474 */
Guido van Rossum47478871996-08-21 14:32:37 +00001475static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001476validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001477{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001478 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001479 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001480 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001481 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001482
Fred Drakecff283c2000-08-21 22:24:43 +00001483 if (res && nch > 1) {
1484 int sym = TYPE(CHILD(tree, 1));
1485 int i = 1;
1486 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001487
Fred Drakecff283c2000-08-21 22:24:43 +00001488 if (sym == test)
1489 res = validate_test(CHILD(tree, i++));
1490 else {
1491 if (nch < 3)
1492 res = validate_numnodes(tree, 3, "print_stmt");
1493 else {
1494 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1495 && validate_test(CHILD(tree, i+1)));
1496 i += 2;
1497 allow_trailing_comma = 0;
1498 }
1499 }
1500 if (res) {
1501 /* ... (',' test)* [','] */
1502 while (res && i+2 <= nch) {
1503 res = (validate_comma(CHILD(tree, i))
1504 && validate_test(CHILD(tree, i+1)));
1505 allow_trailing_comma = 1;
1506 i += 2;
1507 }
1508 if (res && !allow_trailing_comma)
1509 res = validate_numnodes(tree, i, "print_stmt");
1510 else if (res && i < nch)
1511 res = validate_comma(CHILD(tree, i));
1512 }
1513 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001514 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001515}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001516
1517
Guido van Rossum47478871996-08-21 14:32:37 +00001518static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001519validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001520{
1521 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001522 && validate_name(CHILD(tree, 0), "del")
1523 && validate_exprlist(CHILD(tree, 1)));
1524}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001525
1526
Guido van Rossum47478871996-08-21 14:32:37 +00001527static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001528validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001529{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001530 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001531 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001532 && ((nch == 1) || (nch == 2))
1533 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001534
Guido van Rossum3d602e31996-07-21 02:33:56 +00001535 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001536 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001537
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001538 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001539}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001540
1541
Guido van Rossum47478871996-08-21 14:32:37 +00001542static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001543validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001544{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001545 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001546 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001547 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001548
Guido van Rossum3d602e31996-07-21 02:33:56 +00001549 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001550 res = validate_name(CHILD(tree, 0), "raise");
1551 if (res && (nch >= 2))
1552 res = validate_test(CHILD(tree, 1));
1553 if (res && nch > 2) {
1554 res = (validate_comma(CHILD(tree, 2))
1555 && validate_test(CHILD(tree, 3)));
1556 if (res && (nch > 4))
1557 res = (validate_comma(CHILD(tree, 4))
1558 && validate_test(CHILD(tree, 5)));
1559 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001560 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001561 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001562 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001563 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001564 res = (validate_comma(CHILD(tree, 2))
1565 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001566
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001567 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001568}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001569
1570
Fred Drake02126f22001-07-17 02:59:15 +00001571/* yield_stmt: 'yield' testlist
1572 */
1573static int
1574validate_yield_stmt(node *tree)
1575{
1576 return (validate_ntype(tree, yield_stmt)
1577 && validate_numnodes(tree, 2, "yield_stmt")
1578 && validate_name(CHILD(tree, 0), "yield")
1579 && validate_testlist(CHILD(tree, 1)));
1580}
1581
1582
Fred Drakecff283c2000-08-21 22:24:43 +00001583static int
1584validate_import_as_name(node *tree)
1585{
1586 int nch = NCH(tree);
1587 int ok = validate_ntype(tree, import_as_name);
1588
1589 if (ok) {
1590 if (nch == 1)
1591 ok = validate_name(CHILD(tree, 0), NULL);
1592 else if (nch == 3)
1593 ok = (validate_name(CHILD(tree, 0), NULL)
1594 && validate_name(CHILD(tree, 1), "as")
1595 && validate_name(CHILD(tree, 2), NULL));
1596 else
1597 ok = validate_numnodes(tree, 3, "import_as_name");
1598 }
1599 return ok;
1600}
1601
1602
Fred Drake71137082001-01-07 05:59:59 +00001603/* dotted_name: NAME ("." NAME)*
1604 */
1605static int
1606validate_dotted_name(node *tree)
1607{
1608 int nch = NCH(tree);
1609 int res = (validate_ntype(tree, dotted_name)
1610 && is_odd(nch)
1611 && validate_name(CHILD(tree, 0), NULL));
1612 int i;
1613
1614 for (i = 1; res && (i < nch); i += 2) {
1615 res = (validate_dot(CHILD(tree, i))
1616 && validate_name(CHILD(tree, i+1), NULL));
1617 }
1618 return res;
1619}
1620
1621
Fred Drakecff283c2000-08-21 22:24:43 +00001622/* dotted_as_name: dotted_name [NAME NAME]
1623 */
1624static int
1625validate_dotted_as_name(node *tree)
1626{
1627 int nch = NCH(tree);
1628 int res = validate_ntype(tree, dotted_as_name);
1629
1630 if (res) {
1631 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001632 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001633 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001634 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001635 && validate_name(CHILD(tree, 1), "as")
1636 && validate_name(CHILD(tree, 2), NULL));
1637 else {
1638 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001639 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001640 }
1641 }
1642 return res;
1643}
1644
1645
Guido van Rossum3d602e31996-07-21 02:33:56 +00001646/* import_stmt:
1647 *
Fred Drakecff283c2000-08-21 22:24:43 +00001648 * 'import' dotted_as_name (',' dotted_as_name)*
1649 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001650 */
Guido van Rossum47478871996-08-21 14:32:37 +00001651static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001652validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001653{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001654 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001655 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001656 && (nch >= 2) && is_even(nch)
Fred Drakecff283c2000-08-21 22:24:43 +00001657 && validate_ntype(CHILD(tree, 0), NAME));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001658
1659 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001660 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001661
Fred Drakecff283c2000-08-21 22:24:43 +00001662 res = validate_dotted_as_name(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00001663 for (j = 2; res && (j < nch); j += 2)
1664 res = (validate_comma(CHILD(tree, j))
Fred Drake71137082001-01-07 05:59:59 +00001665 && validate_dotted_as_name(CHILD(tree, j + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001666 }
Fred Drakecff283c2000-08-21 22:24:43 +00001667 else if (res && (res = validate_name(CHILD(tree, 0), "from"))) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001668 res = ((nch >= 4) && is_even(nch)
Fred Drake71137082001-01-07 05:59:59 +00001669 && validate_dotted_name(CHILD(tree, 1))
1670 && validate_name(CHILD(tree, 2), "import"));
Fred Drakeff9ea482000-04-19 13:54:15 +00001671 if (nch == 4) {
Fred Drakecff283c2000-08-21 22:24:43 +00001672 if (TYPE(CHILD(tree, 3)) == import_as_name)
1673 res = validate_import_as_name(CHILD(tree, 3));
1674 else
1675 res = validate_star(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001676 }
1677 else {
Fred Drakecff283c2000-08-21 22:24:43 +00001678 /* 'from' dotted_name 'import' import_as_name
1679 * (',' import_as_name)+
1680 */
Fred Drakeff9ea482000-04-19 13:54:15 +00001681 int j;
Fred Drakecff283c2000-08-21 22:24:43 +00001682 res = validate_import_as_name(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001683 for (j = 4; res && (j < nch); j += 2)
1684 res = (validate_comma(CHILD(tree, j))
Fred Drakecff283c2000-08-21 22:24:43 +00001685 && validate_import_as_name(CHILD(tree, j + 1)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001686 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001687 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001688 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001689 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001690
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001691 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001692}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001693
1694
Guido van Rossum47478871996-08-21 14:32:37 +00001695static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001696validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001697{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001698 int j;
1699 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001700 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001701 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001702
Guido van Rossum3d602e31996-07-21 02:33:56 +00001703 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001704 res = (validate_name(CHILD(tree, 0), "global")
1705 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001706 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001707 res = (validate_comma(CHILD(tree, j))
1708 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001709
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001710 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001711}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001712
1713
Guido van Rossum3d602e31996-07-21 02:33:56 +00001714/* exec_stmt:
1715 *
1716 * 'exec' expr ['in' test [',' test]]
1717 */
Guido van Rossum47478871996-08-21 14:32:37 +00001718static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001719validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001720{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001721 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001722 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001723 && ((nch == 2) || (nch == 4) || (nch == 6))
1724 && validate_name(CHILD(tree, 0), "exec")
1725 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001726
Guido van Rossum3d602e31996-07-21 02:33:56 +00001727 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001728 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001729 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001730 res = (validate_name(CHILD(tree, 2), "in")
1731 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001732 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001733 res = (validate_comma(CHILD(tree, 4))
1734 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001735
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001736 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001737}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001738
1739
Guido van Rossum925e5471997-04-02 05:32:13 +00001740/* assert_stmt:
1741 *
1742 * 'assert' test [',' test]
1743 */
1744static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001745validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001746{
1747 int nch = NCH(tree);
1748 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001749 && ((nch == 2) || (nch == 4))
1750 && (validate_name(CHILD(tree, 0), "__assert__") ||
1751 validate_name(CHILD(tree, 0), "assert"))
1752 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001753
1754 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001755 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001756 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001757 res = (validate_comma(CHILD(tree, 2))
1758 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001759
1760 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001761}
Guido van Rossum925e5471997-04-02 05:32:13 +00001762
1763
Guido van Rossum47478871996-08-21 14:32:37 +00001764static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001765validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001766{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001767 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001768 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001769 && ((nch == 4) || (nch == 7))
1770 && validate_name(CHILD(tree, 0), "while")
1771 && validate_test(CHILD(tree, 1))
1772 && validate_colon(CHILD(tree, 2))
1773 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001774
Guido van Rossum3d602e31996-07-21 02:33:56 +00001775 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001776 res = (validate_name(CHILD(tree, 4), "else")
1777 && validate_colon(CHILD(tree, 5))
1778 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001779
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001780 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001781}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001782
1783
Guido van Rossum47478871996-08-21 14:32:37 +00001784static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001785validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001786{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001787 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001788 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001789 && ((nch == 6) || (nch == 9))
1790 && validate_name(CHILD(tree, 0), "for")
1791 && validate_exprlist(CHILD(tree, 1))
1792 && validate_name(CHILD(tree, 2), "in")
1793 && validate_testlist(CHILD(tree, 3))
1794 && validate_colon(CHILD(tree, 4))
1795 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001796
Guido van Rossum3d602e31996-07-21 02:33:56 +00001797 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001798 res = (validate_name(CHILD(tree, 6), "else")
1799 && validate_colon(CHILD(tree, 7))
1800 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001801
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001802 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001803}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001804
1805
Guido van Rossum3d602e31996-07-21 02:33:56 +00001806/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001807 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001808 * | 'try' ':' suite 'finally' ':' suite
1809 *
1810 */
Guido van Rossum47478871996-08-21 14:32:37 +00001811static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001812validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001813{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001814 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001815 int pos = 3;
1816 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001817 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001818
1819 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001820 res = (validate_name(CHILD(tree, 0), "try")
1821 && validate_colon(CHILD(tree, 1))
1822 && validate_suite(CHILD(tree, 2))
1823 && validate_colon(CHILD(tree, nch - 2))
1824 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001825 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001826 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001827 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1828 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001829
1830 PyErr_Format(parser_error,
1831 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001832 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001833 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001834 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001835 res = (validate_except_clause(CHILD(tree, pos))
1836 && validate_colon(CHILD(tree, pos + 1))
1837 && validate_suite(CHILD(tree, pos + 2)));
1838 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001839 }
1840 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001841 res = validate_ntype(CHILD(tree, pos), NAME);
1842 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1843 res = (validate_numnodes(tree, 6, "try/finally")
1844 && validate_colon(CHILD(tree, 4))
1845 && validate_suite(CHILD(tree, 5)));
1846 else if (res) {
1847 if (nch == (pos + 3)) {
1848 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1849 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1850 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001851 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001852 }
1853 else if (nch == (pos + 6)) {
1854 res = (validate_name(CHILD(tree, pos), "except")
1855 && validate_colon(CHILD(tree, pos + 1))
1856 && validate_suite(CHILD(tree, pos + 2))
1857 && validate_name(CHILD(tree, pos + 3), "else"));
1858 }
1859 else
1860 res = validate_numnodes(tree, pos + 3, "try/except");
1861 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001862 }
1863 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001864}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001865
1866
Guido van Rossum47478871996-08-21 14:32:37 +00001867static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001868validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001869{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001870 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001871 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001872 && ((nch == 1) || (nch == 2) || (nch == 4))
1873 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001874
Guido van Rossum3d602e31996-07-21 02:33:56 +00001875 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001876 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001877 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001878 res = (validate_comma(CHILD(tree, 2))
1879 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001880
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001881 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001882}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001883
1884
Guido van Rossum47478871996-08-21 14:32:37 +00001885static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001886validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001887{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001888 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001889 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001890
Guido van Rossum3d602e31996-07-21 02:33:56 +00001891 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001892 res = ((nch == 1)
1893 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001894 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001895 int pos;
1896 res = validate_and_test(CHILD(tree, 0));
1897 for (pos = 1; res && (pos < nch); pos += 2)
1898 res = (validate_name(CHILD(tree, pos), "or")
1899 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001900 }
1901 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001902}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001903
1904
Guido van Rossum47478871996-08-21 14:32:37 +00001905static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001906validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001907{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001908 int pos;
1909 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001910 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001911 && is_odd(nch)
1912 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001913
Guido van Rossum3d602e31996-07-21 02:33:56 +00001914 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001915 res = (validate_name(CHILD(tree, pos), "and")
1916 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001917
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001918 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001919}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001920
1921
Guido van Rossum47478871996-08-21 14:32:37 +00001922static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001923validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001924{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001925 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001926 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001927
Guido van Rossum3d602e31996-07-21 02:33:56 +00001928 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001929 if (nch == 2)
1930 res = (validate_name(CHILD(tree, 0), "not")
1931 && validate_not_test(CHILD(tree, 1)));
1932 else if (nch == 1)
1933 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001934 }
1935 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001936}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001937
1938
Guido van Rossum47478871996-08-21 14:32:37 +00001939static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001940validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001941{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001942 int pos;
1943 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001944 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001945 && is_odd(nch)
1946 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001947
Guido van Rossum3d602e31996-07-21 02:33:56 +00001948 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001949 res = (validate_comp_op(CHILD(tree, pos))
1950 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001951
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001952 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001953}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001954
1955
Guido van Rossum47478871996-08-21 14:32:37 +00001956static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001957validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001958{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001959 int res = 0;
1960 int nch = NCH(tree);
1961
Guido van Rossum3d602e31996-07-21 02:33:56 +00001962 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001963 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001964 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001965 /*
1966 * Only child will be a terminal with a well-defined symbolic name
1967 * or a NAME with a string of either 'is' or 'in'
1968 */
1969 tree = CHILD(tree, 0);
1970 switch (TYPE(tree)) {
1971 case LESS:
1972 case GREATER:
1973 case EQEQUAL:
1974 case EQUAL:
1975 case LESSEQUAL:
1976 case GREATEREQUAL:
1977 case NOTEQUAL:
1978 res = 1;
1979 break;
1980 case NAME:
1981 res = ((strcmp(STR(tree), "in") == 0)
1982 || (strcmp(STR(tree), "is") == 0));
1983 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001984 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00001985 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00001986 }
1987 break;
1988 default:
Fred Drake661ea262000-10-24 19:57:45 +00001989 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001990 break;
1991 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001992 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00001993 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001994 res = (validate_ntype(CHILD(tree, 0), NAME)
1995 && validate_ntype(CHILD(tree, 1), NAME)
1996 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
1997 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
1998 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
1999 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2000 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002001 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002002 }
2003 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002004}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002005
2006
Guido van Rossum47478871996-08-21 14:32:37 +00002007static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002008validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002009{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002010 int j;
2011 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002012 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002013 && is_odd(nch)
2014 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002015
Guido van Rossum3d602e31996-07-21 02:33:56 +00002016 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002017 res = (validate_xor_expr(CHILD(tree, j))
2018 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002019
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002020 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002021}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002022
2023
Guido van Rossum47478871996-08-21 14:32:37 +00002024static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002025validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002026{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002027 int j;
2028 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002029 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002030 && is_odd(nch)
2031 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002032
Guido van Rossum3d602e31996-07-21 02:33:56 +00002033 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002034 res = (validate_circumflex(CHILD(tree, j - 1))
2035 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002036
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002037 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002038}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002039
2040
Guido van Rossum47478871996-08-21 14:32:37 +00002041static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002042validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002043{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002044 int pos;
2045 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002046 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002047 && is_odd(nch)
2048 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002049
Guido van Rossum3d602e31996-07-21 02:33:56 +00002050 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002051 res = (validate_ampersand(CHILD(tree, pos))
2052 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002053
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002054 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002055}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002056
2057
2058static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002059validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002060 {
2061 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002062 int nch = NCH(tree);
2063 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002064 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002065
Guido van Rossum3d602e31996-07-21 02:33:56 +00002066 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002067 if (TYPE(CHILD(tree, pos)) != op1)
2068 res = validate_ntype(CHILD(tree, pos), op2);
2069 if (res)
2070 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002071 }
2072 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002073}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002074
2075
Guido van Rossum47478871996-08-21 14:32:37 +00002076static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002077validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002078{
2079 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002080 && validate_chain_two_ops(tree, validate_arith_expr,
2081 LEFTSHIFT, RIGHTSHIFT));
2082}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002083
2084
Guido van Rossum47478871996-08-21 14:32:37 +00002085static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002086validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002087{
2088 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002089 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2090}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002091
2092
Guido van Rossum47478871996-08-21 14:32:37 +00002093static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002094validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002095{
2096 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002097 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002098 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002099 && is_odd(nch)
2100 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002101
Guido van Rossum3d602e31996-07-21 02:33:56 +00002102 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002103 res = (((TYPE(CHILD(tree, pos)) == STAR)
2104 || (TYPE(CHILD(tree, pos)) == SLASH)
2105 || (TYPE(CHILD(tree, pos)) == PERCENT))
2106 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002107
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002108 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002109}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002110
2111
Guido van Rossum3d602e31996-07-21 02:33:56 +00002112/* factor:
2113 *
2114 * factor: ('+'|'-'|'~') factor | power
2115 */
Guido van Rossum47478871996-08-21 14:32:37 +00002116static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002117validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002118{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002119 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002120 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002121 && (((nch == 2)
2122 && ((TYPE(CHILD(tree, 0)) == PLUS)
2123 || (TYPE(CHILD(tree, 0)) == MINUS)
2124 || (TYPE(CHILD(tree, 0)) == TILDE))
2125 && validate_factor(CHILD(tree, 1)))
2126 || ((nch == 1)
2127 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002128 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002129}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002130
2131
Guido van Rossum3d602e31996-07-21 02:33:56 +00002132/* power:
2133 *
2134 * power: atom trailer* ('**' factor)*
2135 */
Guido van Rossum47478871996-08-21 14:32:37 +00002136static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002137validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002138{
2139 int pos = 1;
2140 int nch = NCH(tree);
2141 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002142 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002143
2144 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002145 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002146 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002147 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002148 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002149 return (0);
2150 }
2151 for ( ; res && (pos < (nch - 1)); pos += 2)
2152 res = (validate_doublestar(CHILD(tree, pos))
2153 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002154 }
2155 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002156}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002157
2158
Guido van Rossum47478871996-08-21 14:32:37 +00002159static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002160validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002161{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002162 int pos;
2163 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002164 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002165
Fred Drakecff283c2000-08-21 22:24:43 +00002166 if (res && nch < 1)
2167 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002168 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002169 switch (TYPE(CHILD(tree, 0))) {
2170 case LPAR:
2171 res = ((nch <= 3)
2172 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002173
Fred Drakeff9ea482000-04-19 13:54:15 +00002174 if (res && (nch == 3))
2175 res = validate_testlist(CHILD(tree, 1));
2176 break;
2177 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002178 if (nch == 2)
2179 res = validate_ntype(CHILD(tree, 1), RSQB);
2180 else if (nch == 3)
2181 res = (validate_listmaker(CHILD(tree, 1))
2182 && validate_ntype(CHILD(tree, 2), RSQB));
2183 else {
2184 res = 0;
2185 err_string("illegal list display atom");
2186 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002187 break;
2188 case LBRACE:
2189 res = ((nch <= 3)
2190 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002191
Fred Drakeff9ea482000-04-19 13:54:15 +00002192 if (res && (nch == 3))
2193 res = validate_dictmaker(CHILD(tree, 1));
2194 break;
2195 case BACKQUOTE:
2196 res = ((nch == 3)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002197 && validate_testlist1(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002198 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2199 break;
2200 case NAME:
2201 case NUMBER:
2202 res = (nch == 1);
2203 break;
2204 case STRING:
2205 for (pos = 1; res && (pos < nch); ++pos)
2206 res = validate_ntype(CHILD(tree, pos), STRING);
2207 break;
2208 default:
2209 res = 0;
2210 break;
2211 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002212 }
2213 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002214}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002215
2216
Fred Drake85bf3bb2000-08-23 15:35:26 +00002217/* listmaker:
2218 * test ( list_for | (',' test)* [','] )
2219 */
Fred Drakecff283c2000-08-21 22:24:43 +00002220static int
2221validate_listmaker(node *tree)
2222{
2223 int nch = NCH(tree);
2224 int ok = nch;
2225
2226 if (nch == 0)
2227 err_string("missing child nodes of listmaker");
2228 else
2229 ok = validate_test(CHILD(tree, 0));
2230
2231 /*
2232 * list_iter | (',' test)* [',']
2233 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002234 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2235 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002236 else {
2237 /* (',' test)* [','] */
2238 int i = 1;
2239 while (ok && nch - i >= 2) {
2240 ok = (validate_comma(CHILD(tree, i))
2241 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002242 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002243 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002244 if (ok && i == nch-1)
2245 ok = validate_comma(CHILD(tree, i));
2246 else if (i != nch) {
2247 ok = 0;
2248 err_string("illegal trailing nodes for listmaker");
2249 }
Fred Drakecff283c2000-08-21 22:24:43 +00002250 }
2251 return ok;
2252}
2253
2254
Guido van Rossum3d602e31996-07-21 02:33:56 +00002255/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002256 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002257 *
2258 */
Guido van Rossum47478871996-08-21 14:32:37 +00002259static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002260validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002261{
2262 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002263 && validate_numnodes(tree, 5, "funcdef")
2264 && validate_name(CHILD(tree, 0), "def")
2265 && validate_ntype(CHILD(tree, 1), NAME)
2266 && validate_colon(CHILD(tree, 3))
2267 && validate_parameters(CHILD(tree, 2))
2268 && validate_suite(CHILD(tree, 4)));
2269}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002270
2271
Guido van Rossum47478871996-08-21 14:32:37 +00002272static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002273validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002274{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002275 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002276 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002277 && ((nch == 3) || (nch == 4))
2278 && validate_name(CHILD(tree, 0), "lambda")
2279 && validate_colon(CHILD(tree, nch - 2))
2280 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002281
Guido van Rossum3d602e31996-07-21 02:33:56 +00002282 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002283 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002284 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002285 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002286
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002287 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002288}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002289
2290
Guido van Rossum3d602e31996-07-21 02:33:56 +00002291/* arglist:
2292 *
Fred Drakecff283c2000-08-21 22:24:43 +00002293 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002294 */
Guido van Rossum47478871996-08-21 14:32:37 +00002295static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002296validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002297{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002298 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002299 int i = 0;
2300 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002301
2302 if (nch <= 0)
2303 /* raise the right error from having an invalid number of children */
2304 return validate_numnodes(tree, nch + 1, "arglist");
2305
Fred Drakecff283c2000-08-21 22:24:43 +00002306 while (ok && nch-i >= 2) {
2307 /* skip leading (argument ',') */
2308 ok = (validate_argument(CHILD(tree, i))
2309 && validate_comma(CHILD(tree, i+1)));
2310 if (ok)
2311 i += 2;
2312 else
2313 PyErr_Clear();
2314 }
2315 ok = 1;
2316 if (nch-i > 0) {
2317 /*
2318 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002319 */
Fred Drakecff283c2000-08-21 22:24:43 +00002320 int sym = TYPE(CHILD(tree, i));
2321
2322 if (sym == argument) {
2323 ok = validate_argument(CHILD(tree, i));
2324 if (ok && i+1 != nch) {
2325 err_string("illegal arglist specification"
2326 " (extra stuff on end)");
2327 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002328 }
Fred Drakecff283c2000-08-21 22:24:43 +00002329 }
2330 else if (sym == STAR) {
2331 ok = validate_star(CHILD(tree, i));
2332 if (ok && (nch-i == 2))
2333 ok = validate_test(CHILD(tree, i+1));
2334 else if (ok && (nch-i == 5))
2335 ok = (validate_test(CHILD(tree, i+1))
2336 && validate_comma(CHILD(tree, i+2))
2337 && validate_doublestar(CHILD(tree, i+3))
2338 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002339 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002340 err_string("illegal use of '*' in arglist");
2341 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002342 }
2343 }
Fred Drakecff283c2000-08-21 22:24:43 +00002344 else if (sym == DOUBLESTAR) {
2345 if (nch-i == 2)
2346 ok = (validate_doublestar(CHILD(tree, i))
2347 && validate_test(CHILD(tree, i+1)));
2348 else {
2349 err_string("illegal use of '**' in arglist");
2350 ok = 0;
2351 }
2352 }
2353 else {
2354 err_string("illegal arglist specification");
2355 ok = 0;
2356 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002357 }
2358 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002359}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002360
2361
2362
2363/* argument:
2364 *
2365 * [test '='] test
2366 */
Guido van Rossum47478871996-08-21 14:32:37 +00002367static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002368validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002369{
2370 int nch = NCH(tree);
2371 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002372 && ((nch == 1) || (nch == 3))
2373 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002374
2375 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002376 res = (validate_equal(CHILD(tree, 1))
2377 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002378
2379 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002380}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002381
2382
2383
2384/* trailer:
2385 *
Guido van Rossum47478871996-08-21 14:32:37 +00002386 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002387 */
Guido van Rossum47478871996-08-21 14:32:37 +00002388static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002389validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002390{
2391 int nch = NCH(tree);
2392 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002393
2394 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002395 switch (TYPE(CHILD(tree, 0))) {
2396 case LPAR:
2397 res = validate_rparen(CHILD(tree, nch - 1));
2398 if (res && (nch == 3))
2399 res = validate_arglist(CHILD(tree, 1));
2400 break;
2401 case LSQB:
2402 res = (validate_numnodes(tree, 3, "trailer")
2403 && validate_subscriptlist(CHILD(tree, 1))
2404 && validate_ntype(CHILD(tree, 2), RSQB));
2405 break;
2406 case DOT:
2407 res = (validate_numnodes(tree, 2, "trailer")
2408 && validate_ntype(CHILD(tree, 1), NAME));
2409 break;
2410 default:
2411 res = 0;
2412 break;
2413 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002414 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002415 else {
2416 (void) validate_numnodes(tree, 2, "trailer");
2417 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002418 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002419}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002420
2421
Guido van Rossum47478871996-08-21 14:32:37 +00002422/* subscriptlist:
2423 *
2424 * subscript (',' subscript)* [',']
2425 */
2426static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002427validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002428{
2429 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002430 validate_subscript, "subscriptlist"));
2431}
Guido van Rossum47478871996-08-21 14:32:37 +00002432
2433
Guido van Rossum3d602e31996-07-21 02:33:56 +00002434/* subscript:
2435 *
Guido van Rossum47478871996-08-21 14:32:37 +00002436 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002437 */
Guido van Rossum47478871996-08-21 14:32:37 +00002438static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002439validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002440{
Guido van Rossum47478871996-08-21 14:32:37 +00002441 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002442 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002443 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002444
Guido van Rossum47478871996-08-21 14:32:37 +00002445 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002446 if (!PyErr_Occurred())
2447 err_string("invalid number of arguments for subscript node");
2448 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002449 }
Guido van Rossum47478871996-08-21 14:32:37 +00002450 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002451 /* take care of ('.' '.' '.') possibility */
2452 return (validate_numnodes(tree, 3, "subscript")
2453 && validate_dot(CHILD(tree, 0))
2454 && validate_dot(CHILD(tree, 1))
2455 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002456 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002457 if (TYPE(CHILD(tree, 0)) == test)
2458 res = validate_test(CHILD(tree, 0));
2459 else
2460 res = validate_colon(CHILD(tree, 0));
2461 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002462 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002463 /* Must be [test] ':' [test] [sliceop],
2464 * but at least one of the optional components will
2465 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002466 */
2467 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002468 res = validate_test(CHILD(tree, 0));
2469 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002470 }
2471 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002472 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002473 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002474 int rem = nch - ++offset;
2475 if (rem) {
2476 if (TYPE(CHILD(tree, offset)) == test) {
2477 res = validate_test(CHILD(tree, offset));
2478 ++offset;
2479 --rem;
2480 }
2481 if (res && rem)
2482 res = validate_sliceop(CHILD(tree, offset));
2483 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002484 }
2485 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002486}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002487
2488
Guido van Rossum47478871996-08-21 14:32:37 +00002489static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002490validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002491{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002492 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002493 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002494 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002495 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002496 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002497 }
Guido van Rossum47478871996-08-21 14:32:37 +00002498 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002499 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002500 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002501 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002502
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002503 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002504}
Guido van Rossum47478871996-08-21 14:32:37 +00002505
2506
2507static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002508validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002509{
2510 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002511 validate_expr, "exprlist"));
2512}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002513
2514
Guido van Rossum47478871996-08-21 14:32:37 +00002515static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002516validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002517{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002518 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002519 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002520 && (nch >= 3)
2521 && validate_test(CHILD(tree, 0))
2522 && validate_colon(CHILD(tree, 1))
2523 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002524
Guido van Rossum3d602e31996-07-21 02:33:56 +00002525 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002526 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002527 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002528 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002529
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002530 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002531 int pos = 3;
2532 /* ( ',' test ':' test )* */
2533 while (res && (pos < nch)) {
2534 res = (validate_comma(CHILD(tree, pos))
2535 && validate_test(CHILD(tree, pos + 1))
2536 && validate_colon(CHILD(tree, pos + 2))
2537 && validate_test(CHILD(tree, pos + 3)));
2538 pos += 4;
2539 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002540 }
2541 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002542}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002543
2544
Guido van Rossum47478871996-08-21 14:32:37 +00002545static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002546validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002547{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002548 int pos;
2549 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002550 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002551 && (nch >= 2)
2552 && validate_testlist(CHILD(tree, 0))
2553 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002554
Guido van Rossum3d602e31996-07-21 02:33:56 +00002555 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002556 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002557
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002558 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002559}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002560
2561
Guido van Rossum47478871996-08-21 14:32:37 +00002562static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002563validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002564{
Fred Drakeff9ea482000-04-19 13:54:15 +00002565 int nch = 0; /* num. children on current node */
2566 int res = 1; /* result value */
2567 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002568
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002569 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002570 nch = NCH(tree);
2571 next = 0;
2572 switch (TYPE(tree)) {
2573 /*
2574 * Definition nodes.
2575 */
2576 case funcdef:
2577 res = validate_funcdef(tree);
2578 break;
2579 case classdef:
2580 res = validate_class(tree);
2581 break;
2582 /*
2583 * "Trivial" parse tree nodes.
2584 * (Why did I call these trivial?)
2585 */
2586 case stmt:
2587 res = validate_stmt(tree);
2588 break;
2589 case small_stmt:
2590 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002591 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002592 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2593 */
2594 res = validate_small_stmt(tree);
2595 break;
2596 case flow_stmt:
2597 res = (validate_numnodes(tree, 1, "flow_stmt")
2598 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2599 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002600 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002601 || (TYPE(CHILD(tree, 0)) == return_stmt)
2602 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2603 if (res)
2604 next = CHILD(tree, 0);
2605 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002606 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002607 break;
Fred Drake02126f22001-07-17 02:59:15 +00002608 case yield_stmt:
2609 res = validate_yield_stmt(tree);
2610 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002611 /*
2612 * Compound statements.
2613 */
2614 case simple_stmt:
2615 res = validate_simple_stmt(tree);
2616 break;
2617 case compound_stmt:
2618 res = validate_compound_stmt(tree);
2619 break;
2620 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002621 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002622 */
2623 case expr_stmt:
2624 res = validate_expr_stmt(tree);
2625 break;
2626 case print_stmt:
2627 res = validate_print_stmt(tree);
2628 break;
2629 case del_stmt:
2630 res = validate_del_stmt(tree);
2631 break;
2632 case pass_stmt:
2633 res = (validate_numnodes(tree, 1, "pass")
2634 && validate_name(CHILD(tree, 0), "pass"));
2635 break;
2636 case break_stmt:
2637 res = (validate_numnodes(tree, 1, "break")
2638 && validate_name(CHILD(tree, 0), "break"));
2639 break;
2640 case continue_stmt:
2641 res = (validate_numnodes(tree, 1, "continue")
2642 && validate_name(CHILD(tree, 0), "continue"));
2643 break;
2644 case return_stmt:
2645 res = validate_return_stmt(tree);
2646 break;
2647 case raise_stmt:
2648 res = validate_raise_stmt(tree);
2649 break;
2650 case import_stmt:
2651 res = validate_import_stmt(tree);
2652 break;
2653 case global_stmt:
2654 res = validate_global_stmt(tree);
2655 break;
2656 case exec_stmt:
2657 res = validate_exec_stmt(tree);
2658 break;
2659 case assert_stmt:
2660 res = validate_assert_stmt(tree);
2661 break;
2662 case if_stmt:
2663 res = validate_if(tree);
2664 break;
2665 case while_stmt:
2666 res = validate_while(tree);
2667 break;
2668 case for_stmt:
2669 res = validate_for(tree);
2670 break;
2671 case try_stmt:
2672 res = validate_try(tree);
2673 break;
2674 case suite:
2675 res = validate_suite(tree);
2676 break;
2677 /*
2678 * Expression nodes.
2679 */
2680 case testlist:
2681 res = validate_testlist(tree);
2682 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002683 case testlist1:
2684 res = validate_testlist1(tree);
2685 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002686 case test:
2687 res = validate_test(tree);
2688 break;
2689 case and_test:
2690 res = validate_and_test(tree);
2691 break;
2692 case not_test:
2693 res = validate_not_test(tree);
2694 break;
2695 case comparison:
2696 res = validate_comparison(tree);
2697 break;
2698 case exprlist:
2699 res = validate_exprlist(tree);
2700 break;
2701 case comp_op:
2702 res = validate_comp_op(tree);
2703 break;
2704 case expr:
2705 res = validate_expr(tree);
2706 break;
2707 case xor_expr:
2708 res = validate_xor_expr(tree);
2709 break;
2710 case and_expr:
2711 res = validate_and_expr(tree);
2712 break;
2713 case shift_expr:
2714 res = validate_shift_expr(tree);
2715 break;
2716 case arith_expr:
2717 res = validate_arith_expr(tree);
2718 break;
2719 case term:
2720 res = validate_term(tree);
2721 break;
2722 case factor:
2723 res = validate_factor(tree);
2724 break;
2725 case power:
2726 res = validate_power(tree);
2727 break;
2728 case atom:
2729 res = validate_atom(tree);
2730 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002731
Fred Drakeff9ea482000-04-19 13:54:15 +00002732 default:
2733 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002734 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002735 res = 0;
2736 break;
2737 }
2738 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002739 }
2740 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002741}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002742
2743
Guido van Rossum47478871996-08-21 14:32:37 +00002744static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002745validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002746{
2747 int res = validate_eval_input(tree);
2748
2749 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002750 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002751
2752 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002753}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002754
2755
Guido van Rossum3d602e31996-07-21 02:33:56 +00002756/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002757 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002758 */
Guido van Rossum47478871996-08-21 14:32:37 +00002759static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002760validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002761{
Fred Drakec2683dd2001-07-17 19:32:05 +00002762 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002763 int nch = NCH(tree) - 1;
2764 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002765 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002766
Fred Drakec2683dd2001-07-17 19:32:05 +00002767 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002768 if (TYPE(CHILD(tree, j)) == stmt)
2769 res = validate_stmt(CHILD(tree, j));
2770 else
2771 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002772 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002773 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002774 * user. Hopefully, this won't be needed. If a user reports getting
2775 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002776 */
2777 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002778 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002779
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002780 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002781}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002782
2783
Fred Drake43f8f9b1998-04-13 16:25:46 +00002784static PyObject*
2785pickle_constructor = NULL;
2786
2787
2788static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002789parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002790{
Fred Drake268397f1998-04-29 14:16:32 +00002791 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002792 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00002793 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002794 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002795
Fred Drakec2683dd2001-07-17 19:32:05 +00002796 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002797 PyObject *newargs;
2798 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002799
Fred Drake2a6875e1999-09-20 22:32:18 +00002800 if ((empty_dict = PyDict_New()) == NULL)
2801 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002802 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002803 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002804 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002805 if (tuple != NULL) {
2806 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2807 Py_DECREF(tuple);
2808 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002809 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002810 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002811 }
2812 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002813 Py_XDECREF(empty_dict);
2814
Fred Drake43f8f9b1998-04-13 16:25:46 +00002815 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002816}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002817
2818
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002819/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00002820 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002821 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002822 * We'd really have to write a wrapper around it all anyway to allow
2823 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002824 */
2825static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00002826 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
2827 "Creates a tuple-tree representation of an ST."},
2828 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
2829 "Creates a list-tree representation of an ST."},
2830 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
2831 "Compiles an ST object into a code object."},
2832 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
2833 "Compiles an ST object into a code object."},
2834 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
2835 "Creates an ST object from an expression."},
2836 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
2837 "Determines if an ST object was created from an expression."},
2838 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
2839 "Determines if an ST object was created from a suite."},
2840 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
2841 "Creates an ST object from a suite."},
2842 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2843 "Creates an ST object from a tree representation."},
2844 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2845 "Creates an ST object from a tree representation."},
2846 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
2847 "Creates a tuple-tree representation of an ST."},
2848 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
2849 "Creates a list-tree representation of an ST."},
2850 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2851 "Creates an ST object from a tree representation."},
2852 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2853 "Creates an ST object from a tree representation."},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002854
Fred Drake43f8f9b1998-04-13 16:25:46 +00002855 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00002856 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Fred Drakec2683dd2001-07-17 19:32:05 +00002857 "Returns the pickle magic to allow ST objects to be pickled."},
Fred Drake43f8f9b1998-04-13 16:25:46 +00002858
Fred Drake268397f1998-04-29 14:16:32 +00002859 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002860 };
2861
2862
Tim Peters6d7c4422000-08-26 07:38:06 +00002863DL_EXPORT(void) initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00002864
Guido van Rossum3886bb61998-12-04 18:50:17 +00002865DL_EXPORT(void)
Thomas Wouters5c669862000-07-24 15:49:08 +00002866initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00002867{
Fred Drake13130bc2001-08-15 16:44:56 +00002868 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00002869
2870 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002871 module = Py_InitModule("parser", parser_functions);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002872
Fred Drake7a15ba51999-09-09 14:21:52 +00002873 if (parser_error == 0)
2874 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002875
2876 if ((parser_error == 0)
Fred Drake13130bc2001-08-15 16:44:56 +00002877 || (PyModule_AddObject(module, "ParserError", parser_error) != 0)) {
Fred Drakec2683dd2001-07-17 19:32:05 +00002878 /* caller will check PyErr_Occurred() */
2879 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002880 }
Fred Drakec2683dd2001-07-17 19:32:05 +00002881 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002882 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00002883 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002884 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002885
Fred Drake13130bc2001-08-15 16:44:56 +00002886 PyModule_AddStringConstant(module, "__copyright__",
2887 parser_copyright_string);
2888 PyModule_AddStringConstant(module, "__doc__",
2889 parser_doc_string);
2890 PyModule_AddStringConstant(module, "__version__",
2891 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002892
Fred Drake78bdb9b2001-07-19 20:17:15 +00002893 /* Register to support pickling.
2894 * If this fails, the import of this module will fail because an
2895 * exception will be raised here; should we clear the exception?
2896 */
Fred Drake13130bc2001-08-15 16:44:56 +00002897 copyreg = PyImport_ImportModule("copy_reg");
2898 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002899 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002900
Fred Drake13130bc2001-08-15 16:44:56 +00002901 func = PyObject_GetAttrString(copyreg, "pickle");
2902 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
2903 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00002904 Py_XINCREF(pickle_constructor);
2905 if ((func != NULL) && (pickle_constructor != NULL)
2906 && (pickler != NULL)) {
2907 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002908
Fred Drakec2683dd2001-07-17 19:32:05 +00002909 res = PyObject_CallFunction(func, "OOO", &PyST_Type, pickler,
2910 pickle_constructor);
Fred Drakeff9ea482000-04-19 13:54:15 +00002911 Py_XDECREF(res);
2912 }
2913 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00002914 Py_XDECREF(pickle_constructor);
2915 Py_XDECREF(pickler);
2916 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002917 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002918}