blob: ca0531d6b0f6b53eddc69f12817b47811d706b31 [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 Rossumd9e9f9c1995-10-11 17:35:38 +0000847
Fred Drake0ac9b072000-09-12 21:58:06 +0000848#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000849
Fred Drakeff9ea482000-04-19 13:54:15 +0000850#define is_even(n) (((n) & 1) == 0)
851#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000852
853
854static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000855validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000856{
Fred Drake0ac9b072000-09-12 21:58:06 +0000857 if (TYPE(n) != t) {
858 PyErr_Format(parser_error, "Expected node type %d, got %d.",
859 t, TYPE(n));
860 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000861 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000862 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000863}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000864
865
Fred Drakee7ab64e2000-04-25 04:14:46 +0000866/* Verifies that the number of child nodes is exactly 'num', raising
867 * an exception if it isn't. The exception message does not indicate
868 * the exact number of nodes, allowing this to be used to raise the
869 * "right" exception when the wrong number of nodes is present in a
870 * specific variant of a statement's syntax. This is commonly used
871 * in that fashion.
872 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000873static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000874validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000875{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000876 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000877 PyErr_Format(parser_error,
878 "Illegal number of children for %s node.", name);
879 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000880 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000881 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000882}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000883
884
885static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000886validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000887{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000888 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000889 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000890
891 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000892 PyErr_Format(parser_error,
893 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000894 }
895 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000896}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000897
898
Guido van Rossum47478871996-08-21 14:32:37 +0000899/* X (',' X) [',']
900 */
901static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000902validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000903 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000904{
905 int nch = NCH(tree);
906 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000907 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000908
909 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000910 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000911 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000912 if (is_even(nch))
913 res = validate_comma(CHILD(tree, --nch));
914 if (res && nch > 1) {
915 int pos = 1;
916 for ( ; res && pos < nch; pos += 2)
917 res = (validate_comma(CHILD(tree, pos))
918 && vfunc(CHILD(tree, pos + 1)));
919 }
Guido van Rossum47478871996-08-21 14:32:37 +0000920 }
921 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000922}
Guido van Rossum47478871996-08-21 14:32:37 +0000923
924
Fred Drakecff283c2000-08-21 22:24:43 +0000925/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000926 *
927 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000928 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000929 */
Guido van Rossum47478871996-08-21 14:32:37 +0000930static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000931validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000932{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000933 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000934 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000935
Guido van Rossum3d602e31996-07-21 02:33:56 +0000936 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000937 res = (validate_name(CHILD(tree, 0), "class")
938 && validate_ntype(CHILD(tree, 1), NAME)
939 && validate_colon(CHILD(tree, nch - 2))
940 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000941 }
942 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000943 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000944 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000945 res = (validate_lparen(CHILD(tree, 2))
946 && validate_testlist(CHILD(tree, 3))
947 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000948 }
949 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000950}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000951
952
Guido van Rossum3d602e31996-07-21 02:33:56 +0000953/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +0000954 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +0000955 */
Guido van Rossum47478871996-08-21 14:32:37 +0000956static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000957validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000958{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000959 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000960 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +0000961 && (nch >= 4)
962 && validate_name(CHILD(tree, 0), "if")
963 && validate_test(CHILD(tree, 1))
964 && validate_colon(CHILD(tree, 2))
965 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000966
967 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000968 /* ... 'else' ':' suite */
969 res = (validate_name(CHILD(tree, nch - 3), "else")
970 && validate_colon(CHILD(tree, nch - 2))
971 && validate_suite(CHILD(tree, nch - 1)));
972 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000973 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000974 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000975 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000976 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +0000977 /* Will catch the case for nch < 4 */
978 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000979 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000980 /* ... ('elif' test ':' suite)+ ... */
981 int j = 4;
982 while ((j < nch) && res) {
983 res = (validate_name(CHILD(tree, j), "elif")
984 && validate_colon(CHILD(tree, j + 2))
985 && validate_test(CHILD(tree, j + 1))
986 && validate_suite(CHILD(tree, j + 3)));
987 j += 4;
988 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000989 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000990 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000991}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000992
993
Guido van Rossum3d602e31996-07-21 02:33:56 +0000994/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +0000995 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +0000996 *
997 */
Guido van Rossum47478871996-08-21 14:32:37 +0000998static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000999validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001000{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001001 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001002 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001003
Guido van Rossum3d602e31996-07-21 02:33:56 +00001004 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001005 res = (validate_lparen(CHILD(tree, 0))
1006 && validate_rparen(CHILD(tree, nch - 1)));
1007 if (res && (nch == 3))
1008 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001009 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001010 else {
1011 (void) validate_numnodes(tree, 2, "parameters");
1012 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001013 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001014}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001015
1016
Fred Drakecff283c2000-08-21 22:24:43 +00001017/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001018 *
1019 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001020 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001021 * | NEWLINE INDENT stmt+ DEDENT
1022 */
Guido van Rossum47478871996-08-21 14:32:37 +00001023static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001024validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001025{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001026 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001027 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001028
Guido van Rossum3d602e31996-07-21 02:33:56 +00001029 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001030 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001031 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001032 /* NEWLINE INDENT stmt+ DEDENT */
1033 res = (validate_newline(CHILD(tree, 0))
1034 && validate_indent(CHILD(tree, 1))
1035 && validate_stmt(CHILD(tree, 2))
1036 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001037
Fred Drakeff9ea482000-04-19 13:54:15 +00001038 if (res && (nch > 4)) {
1039 int i = 3;
1040 --nch; /* forget the DEDENT */
1041 for ( ; res && (i < nch); ++i)
1042 res = validate_stmt(CHILD(tree, i));
1043 }
1044 else if (nch < 4)
1045 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001046 }
1047 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001048}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001049
1050
Guido van Rossum47478871996-08-21 14:32:37 +00001051static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001052validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001053{
Guido van Rossum47478871996-08-21 14:32:37 +00001054 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001055 validate_test, "testlist"));
1056}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001057
1058
Guido van Rossum1c917072001-10-15 15:44:05 +00001059static int
1060validate_testlist_safe(node *tree)
1061{
1062 return (validate_repeating_list(tree, testlist_safe,
1063 validate_test, "testlist_safe"));
1064}
1065
1066
Fred Drakecff283c2000-08-21 22:24:43 +00001067/* '*' NAME [',' '**' NAME] | '**' NAME
1068 */
1069static int
1070validate_varargslist_trailer(node *tree, int start)
1071{
1072 int nch = NCH(tree);
1073 int res = 0;
1074 int sym;
1075
1076 if (nch <= start) {
1077 err_string("expected variable argument trailer for varargslist");
1078 return 0;
1079 }
1080 sym = TYPE(CHILD(tree, start));
1081 if (sym == STAR) {
1082 /*
1083 * ('*' NAME [',' '**' NAME]
1084 */
1085 if (nch-start == 2)
1086 res = validate_name(CHILD(tree, start+1), NULL);
1087 else if (nch-start == 5)
1088 res = (validate_name(CHILD(tree, start+1), NULL)
1089 && validate_comma(CHILD(tree, start+2))
1090 && validate_doublestar(CHILD(tree, start+3))
1091 && validate_name(CHILD(tree, start+4), NULL));
1092 }
1093 else if (sym == DOUBLESTAR) {
1094 /*
1095 * '**' NAME
1096 */
1097 if (nch-start == 2)
1098 res = validate_name(CHILD(tree, start+1), NULL);
1099 }
1100 if (!res)
1101 err_string("illegal variable argument trailer for varargslist");
1102 return res;
1103}
1104
1105
1106/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001107 *
1108 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001109 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001110 * ('*' NAME [',' '**' NAME]
1111 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001112 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1113 *
1114 */
Guido van Rossum47478871996-08-21 14:32:37 +00001115static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001116validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001117{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001118 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001119 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001120 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001121
Fred Drakeb6429a22000-12-11 22:08:27 +00001122 if (!res)
1123 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001124 if (nch < 1) {
1125 err_string("varargslist missing child nodes");
1126 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001127 }
Fred Drakecff283c2000-08-21 22:24:43 +00001128 sym = TYPE(CHILD(tree, 0));
1129 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001130 /* whole thing matches:
1131 * '*' NAME [',' '**' NAME] | '**' NAME
1132 */
Fred Drakecff283c2000-08-21 22:24:43 +00001133 res = validate_varargslist_trailer(tree, 0);
1134 else if (sym == fpdef) {
1135 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001136
Fred Drakecff283c2000-08-21 22:24:43 +00001137 sym = TYPE(CHILD(tree, nch-1));
1138 if (sym == NAME) {
1139 /*
1140 * (fpdef ['=' test] ',')+
1141 * ('*' NAME [',' '**' NAME]
1142 * | '**' NAME)
1143 */
1144 /* skip over (fpdef ['=' test] ',')+ */
1145 while (res && (i+2 <= nch)) {
1146 res = validate_fpdef(CHILD(tree, i));
1147 ++i;
1148 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1149 res = (validate_equal(CHILD(tree, i))
1150 && validate_test(CHILD(tree, i+1)));
1151 if (res)
1152 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001153 }
Fred Drakecff283c2000-08-21 22:24:43 +00001154 if (res && i < nch) {
1155 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001156 ++i;
1157 if (res && i < nch
1158 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1159 || TYPE(CHILD(tree, i)) == STAR))
1160 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001161 }
1162 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001163 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1164 * i --^^^
1165 */
Fred Drakecff283c2000-08-21 22:24:43 +00001166 if (res)
1167 res = validate_varargslist_trailer(tree, i);
1168 }
1169 else {
1170 /*
1171 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1172 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001173 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001174 if (sym == COMMA) {
1175 res = validate_comma(CHILD(tree, nch-1));
1176 if (!res)
1177 return 0;
1178 --nch;
1179 }
1180 /*
1181 * fpdef ['=' test] (',' fpdef ['=' test])*
1182 */
1183 res = validate_fpdef(CHILD(tree, 0));
1184 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001185 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1186 res = (validate_equal(CHILD(tree, i))
1187 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001188 i += 2;
1189 }
1190 /*
1191 * ... (',' fpdef ['=' test])*
1192 * i ---^^^
1193 */
1194 while (res && (nch - i) >= 2) {
1195 res = (validate_comma(CHILD(tree, i))
1196 && validate_fpdef(CHILD(tree, i+1)));
1197 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001198 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1199 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001200 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001201 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001202 }
1203 }
1204 if (res && nch - i != 0) {
1205 res = 0;
1206 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001207 }
1208 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001209 }
Fred Drakecff283c2000-08-21 22:24:43 +00001210 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001211}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001212
1213
Fred Drakecff283c2000-08-21 22:24:43 +00001214/* list_iter: list_for | list_if
1215 */
1216static int
1217validate_list_iter(node *tree)
1218{
1219 int res = (validate_ntype(tree, list_iter)
1220 && validate_numnodes(tree, 1, "list_iter"));
1221 if (res && TYPE(CHILD(tree, 0)) == list_for)
1222 res = validate_list_for(CHILD(tree, 0));
1223 else
1224 res = validate_list_if(CHILD(tree, 0));
1225
1226 return res;
1227}
1228
1229/* list_for: 'for' exprlist 'in' testlist [list_iter]
1230 */
1231static int
1232validate_list_for(node *tree)
1233{
1234 int nch = NCH(tree);
1235 int res;
1236
1237 if (nch == 5)
1238 res = validate_list_iter(CHILD(tree, 4));
1239 else
1240 res = validate_numnodes(tree, 4, "list_for");
1241
1242 if (res)
1243 res = (validate_name(CHILD(tree, 0), "for")
1244 && validate_exprlist(CHILD(tree, 1))
1245 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001246 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001247
1248 return res;
1249}
1250
1251/* list_if: 'if' test [list_iter]
1252 */
1253static int
1254validate_list_if(node *tree)
1255{
1256 int nch = NCH(tree);
1257 int res;
1258
1259 if (nch == 3)
1260 res = validate_list_iter(CHILD(tree, 2));
1261 else
1262 res = validate_numnodes(tree, 2, "list_if");
1263
1264 if (res)
1265 res = (validate_name(CHILD(tree, 0), "if")
1266 && validate_test(CHILD(tree, 1)));
1267
1268 return res;
1269}
1270
1271
1272/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001273 *
1274 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001275 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001276 * | '(' fplist ')'
1277 */
Guido van Rossum47478871996-08-21 14:32:37 +00001278static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001279validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001280{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001281 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001282 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001283
Guido van Rossum3d602e31996-07-21 02:33:56 +00001284 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001285 if (nch == 1)
1286 res = validate_ntype(CHILD(tree, 0), NAME);
1287 else if (nch == 3)
1288 res = (validate_lparen(CHILD(tree, 0))
1289 && validate_fplist(CHILD(tree, 1))
1290 && validate_rparen(CHILD(tree, 2)));
1291 else
1292 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001293 }
1294 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001295}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001296
1297
Guido van Rossum47478871996-08-21 14:32:37 +00001298static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001299validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001300{
Guido van Rossum47478871996-08-21 14:32:37 +00001301 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001302 validate_fpdef, "fplist"));
1303}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001304
1305
Guido van Rossum3d602e31996-07-21 02:33:56 +00001306/* simple_stmt | compound_stmt
1307 *
1308 */
Guido van Rossum47478871996-08-21 14:32:37 +00001309static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001310validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001311{
1312 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001313 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001314
Guido van Rossum3d602e31996-07-21 02:33:56 +00001315 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001316 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001317
Fred Drakeff9ea482000-04-19 13:54:15 +00001318 if (TYPE(tree) == simple_stmt)
1319 res = validate_simple_stmt(tree);
1320 else
1321 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001322 }
1323 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001324}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001325
1326
Guido van Rossum3d602e31996-07-21 02:33:56 +00001327/* small_stmt (';' small_stmt)* [';'] NEWLINE
1328 *
1329 */
Guido van Rossum47478871996-08-21 14:32:37 +00001330static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001331validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001332{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001333 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001334 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001335 && (nch >= 2)
1336 && validate_small_stmt(CHILD(tree, 0))
1337 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001338
Guido van Rossum3d602e31996-07-21 02:33:56 +00001339 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001340 res = validate_numnodes(tree, 2, "simple_stmt");
1341 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001342 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001343 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001344 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001345 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001346
Fred Drakeff9ea482000-04-19 13:54:15 +00001347 for (i = 1; res && (i < nch); i += 2)
1348 res = (validate_semi(CHILD(tree, i))
1349 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001350 }
1351 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001352}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001353
1354
Guido van Rossum47478871996-08-21 14:32:37 +00001355static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001356validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001357{
1358 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001359 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001360
Fred Drake0ac9b072000-09-12 21:58:06 +00001361 if (res) {
1362 int ntype = TYPE(CHILD(tree, 0));
1363
1364 if ( (ntype == expr_stmt)
1365 || (ntype == print_stmt)
1366 || (ntype == del_stmt)
1367 || (ntype == pass_stmt)
1368 || (ntype == flow_stmt)
1369 || (ntype == import_stmt)
1370 || (ntype == global_stmt)
1371 || (ntype == assert_stmt)
1372 || (ntype == exec_stmt))
1373 res = validate_node(CHILD(tree, 0));
1374 else {
1375 res = 0;
1376 err_string("illegal small_stmt child type");
1377 }
1378 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001379 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001380 res = 0;
1381 PyErr_Format(parser_error,
1382 "Unrecognized child node of small_stmt: %d.",
1383 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001384 }
1385 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001386}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001387
1388
1389/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001390 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001391 */
Guido van Rossum47478871996-08-21 14:32:37 +00001392static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001393validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001394{
1395 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001396 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001397 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001398
1399 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001400 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001401
1402 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001403 ntype = TYPE(tree);
1404 if ( (ntype == if_stmt)
1405 || (ntype == while_stmt)
1406 || (ntype == for_stmt)
1407 || (ntype == try_stmt)
1408 || (ntype == funcdef)
1409 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001410 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001411 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001412 res = 0;
1413 PyErr_Format(parser_error,
1414 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001415 }
1416 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001417}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001418
1419
Guido van Rossum47478871996-08-21 14:32:37 +00001420static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001421validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001422{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001423 int j;
1424 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001425 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001426 && is_odd(nch)
1427 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001428
Fred Drake28f739a2000-08-25 22:42:40 +00001429 if (res && nch == 3
1430 && TYPE(CHILD(tree, 1)) == augassign) {
1431 res = (validate_numnodes(CHILD(tree, 1), 1, "augassign")
1432 && validate_testlist(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001433
Fred Drake28f739a2000-08-25 22:42:40 +00001434 if (res) {
1435 char *s = STR(CHILD(CHILD(tree, 1), 0));
1436
1437 res = (strcmp(s, "+=") == 0
1438 || strcmp(s, "-=") == 0
1439 || strcmp(s, "*=") == 0
1440 || strcmp(s, "/=") == 0
1441 || strcmp(s, "%=") == 0
1442 || strcmp(s, "&=") == 0
1443 || strcmp(s, "|=") == 0
1444 || strcmp(s, "^=") == 0
1445 || strcmp(s, "<<=") == 0
1446 || strcmp(s, ">>=") == 0
1447 || strcmp(s, "**=") == 0);
1448 if (!res)
1449 err_string("illegal augmmented assignment operator");
1450 }
1451 }
1452 else {
1453 for (j = 1; res && (j < nch); j += 2)
1454 res = (validate_equal(CHILD(tree, j))
1455 && validate_testlist(CHILD(tree, j + 1)));
1456 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001457 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001458}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001459
1460
Guido van Rossum3d602e31996-07-21 02:33:56 +00001461/* print_stmt:
1462 *
Fred Drakecff283c2000-08-21 22:24:43 +00001463 * 'print' ( [ test (',' test)* [','] ]
1464 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001465 */
Guido van Rossum47478871996-08-21 14:32:37 +00001466static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001467validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001468{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001469 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001470 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001471 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001472 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001473
Fred Drakecff283c2000-08-21 22:24:43 +00001474 if (res && nch > 1) {
1475 int sym = TYPE(CHILD(tree, 1));
1476 int i = 1;
1477 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001478
Fred Drakecff283c2000-08-21 22:24:43 +00001479 if (sym == test)
1480 res = validate_test(CHILD(tree, i++));
1481 else {
1482 if (nch < 3)
1483 res = validate_numnodes(tree, 3, "print_stmt");
1484 else {
1485 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1486 && validate_test(CHILD(tree, i+1)));
1487 i += 2;
1488 allow_trailing_comma = 0;
1489 }
1490 }
1491 if (res) {
1492 /* ... (',' test)* [','] */
1493 while (res && i+2 <= nch) {
1494 res = (validate_comma(CHILD(tree, i))
1495 && validate_test(CHILD(tree, i+1)));
1496 allow_trailing_comma = 1;
1497 i += 2;
1498 }
1499 if (res && !allow_trailing_comma)
1500 res = validate_numnodes(tree, i, "print_stmt");
1501 else if (res && i < nch)
1502 res = validate_comma(CHILD(tree, i));
1503 }
1504 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001505 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001506}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001507
1508
Guido van Rossum47478871996-08-21 14:32:37 +00001509static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001510validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001511{
1512 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001513 && validate_name(CHILD(tree, 0), "del")
1514 && validate_exprlist(CHILD(tree, 1)));
1515}
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_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001520{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001521 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001522 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001523 && ((nch == 1) || (nch == 2))
1524 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001525
Guido van Rossum3d602e31996-07-21 02:33:56 +00001526 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001527 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001528
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001529 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001530}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001531
1532
Guido van Rossum47478871996-08-21 14:32:37 +00001533static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001534validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001535{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001536 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001537 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001538 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001539
Guido van Rossum3d602e31996-07-21 02:33:56 +00001540 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001541 res = validate_name(CHILD(tree, 0), "raise");
1542 if (res && (nch >= 2))
1543 res = validate_test(CHILD(tree, 1));
1544 if (res && nch > 2) {
1545 res = (validate_comma(CHILD(tree, 2))
1546 && validate_test(CHILD(tree, 3)));
1547 if (res && (nch > 4))
1548 res = (validate_comma(CHILD(tree, 4))
1549 && validate_test(CHILD(tree, 5)));
1550 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001551 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001552 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001553 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001554 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001555 res = (validate_comma(CHILD(tree, 2))
1556 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001557
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001558 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001559}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001560
1561
Fred Drake02126f22001-07-17 02:59:15 +00001562/* yield_stmt: 'yield' testlist
1563 */
1564static int
1565validate_yield_stmt(node *tree)
1566{
1567 return (validate_ntype(tree, yield_stmt)
1568 && validate_numnodes(tree, 2, "yield_stmt")
1569 && validate_name(CHILD(tree, 0), "yield")
1570 && validate_testlist(CHILD(tree, 1)));
1571}
1572
1573
Fred Drakecff283c2000-08-21 22:24:43 +00001574static int
1575validate_import_as_name(node *tree)
1576{
1577 int nch = NCH(tree);
1578 int ok = validate_ntype(tree, import_as_name);
1579
1580 if (ok) {
1581 if (nch == 1)
1582 ok = validate_name(CHILD(tree, 0), NULL);
1583 else if (nch == 3)
1584 ok = (validate_name(CHILD(tree, 0), NULL)
1585 && validate_name(CHILD(tree, 1), "as")
1586 && validate_name(CHILD(tree, 2), NULL));
1587 else
1588 ok = validate_numnodes(tree, 3, "import_as_name");
1589 }
1590 return ok;
1591}
1592
1593
Fred Drake71137082001-01-07 05:59:59 +00001594/* dotted_name: NAME ("." NAME)*
1595 */
1596static int
1597validate_dotted_name(node *tree)
1598{
1599 int nch = NCH(tree);
1600 int res = (validate_ntype(tree, dotted_name)
1601 && is_odd(nch)
1602 && validate_name(CHILD(tree, 0), NULL));
1603 int i;
1604
1605 for (i = 1; res && (i < nch); i += 2) {
1606 res = (validate_dot(CHILD(tree, i))
1607 && validate_name(CHILD(tree, i+1), NULL));
1608 }
1609 return res;
1610}
1611
1612
Fred Drakecff283c2000-08-21 22:24:43 +00001613/* dotted_as_name: dotted_name [NAME NAME]
1614 */
1615static int
1616validate_dotted_as_name(node *tree)
1617{
1618 int nch = NCH(tree);
1619 int res = validate_ntype(tree, dotted_as_name);
1620
1621 if (res) {
1622 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001623 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001624 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001625 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001626 && validate_name(CHILD(tree, 1), "as")
1627 && validate_name(CHILD(tree, 2), NULL));
1628 else {
1629 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001630 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001631 }
1632 }
1633 return res;
1634}
1635
1636
Guido van Rossum3d602e31996-07-21 02:33:56 +00001637/* import_stmt:
1638 *
Fred Drakecff283c2000-08-21 22:24:43 +00001639 * 'import' dotted_as_name (',' dotted_as_name)*
1640 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001641 */
Guido van Rossum47478871996-08-21 14:32:37 +00001642static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001643validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001644{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001645 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001646 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001647 && (nch >= 2) && is_even(nch)
Fred Drakecff283c2000-08-21 22:24:43 +00001648 && validate_ntype(CHILD(tree, 0), NAME));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001649
1650 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001651 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001652
Fred Drakecff283c2000-08-21 22:24:43 +00001653 res = validate_dotted_as_name(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00001654 for (j = 2; res && (j < nch); j += 2)
1655 res = (validate_comma(CHILD(tree, j))
Fred Drake71137082001-01-07 05:59:59 +00001656 && validate_dotted_as_name(CHILD(tree, j + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001657 }
Fred Drakecff283c2000-08-21 22:24:43 +00001658 else if (res && (res = validate_name(CHILD(tree, 0), "from"))) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001659 res = ((nch >= 4) && is_even(nch)
Fred Drake71137082001-01-07 05:59:59 +00001660 && validate_dotted_name(CHILD(tree, 1))
1661 && validate_name(CHILD(tree, 2), "import"));
Fred Drakeff9ea482000-04-19 13:54:15 +00001662 if (nch == 4) {
Fred Drakecff283c2000-08-21 22:24:43 +00001663 if (TYPE(CHILD(tree, 3)) == import_as_name)
1664 res = validate_import_as_name(CHILD(tree, 3));
1665 else
1666 res = validate_star(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001667 }
1668 else {
Fred Drakecff283c2000-08-21 22:24:43 +00001669 /* 'from' dotted_name 'import' import_as_name
1670 * (',' import_as_name)+
1671 */
Fred Drakeff9ea482000-04-19 13:54:15 +00001672 int j;
Fred Drakecff283c2000-08-21 22:24:43 +00001673 res = validate_import_as_name(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001674 for (j = 4; res && (j < nch); j += 2)
1675 res = (validate_comma(CHILD(tree, j))
Fred Drakecff283c2000-08-21 22:24:43 +00001676 && validate_import_as_name(CHILD(tree, j + 1)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001677 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001678 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001679 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001680 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001681
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001682 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001683}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001684
1685
Guido van Rossum47478871996-08-21 14:32:37 +00001686static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001687validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001688{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001689 int j;
1690 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001691 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001692 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001693
Guido van Rossum3d602e31996-07-21 02:33:56 +00001694 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001695 res = (validate_name(CHILD(tree, 0), "global")
1696 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001697 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001698 res = (validate_comma(CHILD(tree, j))
1699 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001700
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001701 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001702}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001703
1704
Guido van Rossum3d602e31996-07-21 02:33:56 +00001705/* exec_stmt:
1706 *
1707 * 'exec' expr ['in' test [',' test]]
1708 */
Guido van Rossum47478871996-08-21 14:32:37 +00001709static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001710validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001711{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001712 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001713 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001714 && ((nch == 2) || (nch == 4) || (nch == 6))
1715 && validate_name(CHILD(tree, 0), "exec")
1716 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001717
Guido van Rossum3d602e31996-07-21 02:33:56 +00001718 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001719 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001720 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001721 res = (validate_name(CHILD(tree, 2), "in")
1722 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001723 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001724 res = (validate_comma(CHILD(tree, 4))
1725 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001726
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001727 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001728}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001729
1730
Guido van Rossum925e5471997-04-02 05:32:13 +00001731/* assert_stmt:
1732 *
1733 * 'assert' test [',' test]
1734 */
1735static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001736validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001737{
1738 int nch = NCH(tree);
1739 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001740 && ((nch == 2) || (nch == 4))
1741 && (validate_name(CHILD(tree, 0), "__assert__") ||
1742 validate_name(CHILD(tree, 0), "assert"))
1743 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001744
1745 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001746 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001747 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001748 res = (validate_comma(CHILD(tree, 2))
1749 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001750
1751 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001752}
Guido van Rossum925e5471997-04-02 05:32:13 +00001753
1754
Guido van Rossum47478871996-08-21 14:32:37 +00001755static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001756validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001757{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001758 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001759 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001760 && ((nch == 4) || (nch == 7))
1761 && validate_name(CHILD(tree, 0), "while")
1762 && validate_test(CHILD(tree, 1))
1763 && validate_colon(CHILD(tree, 2))
1764 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001765
Guido van Rossum3d602e31996-07-21 02:33:56 +00001766 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001767 res = (validate_name(CHILD(tree, 4), "else")
1768 && validate_colon(CHILD(tree, 5))
1769 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001770
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001771 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001772}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001773
1774
Guido van Rossum47478871996-08-21 14:32:37 +00001775static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001776validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001777{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001778 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001779 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001780 && ((nch == 6) || (nch == 9))
1781 && validate_name(CHILD(tree, 0), "for")
1782 && validate_exprlist(CHILD(tree, 1))
1783 && validate_name(CHILD(tree, 2), "in")
1784 && validate_testlist(CHILD(tree, 3))
1785 && validate_colon(CHILD(tree, 4))
1786 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001787
Guido van Rossum3d602e31996-07-21 02:33:56 +00001788 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001789 res = (validate_name(CHILD(tree, 6), "else")
1790 && validate_colon(CHILD(tree, 7))
1791 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001792
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001793 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001794}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001795
1796
Guido van Rossum3d602e31996-07-21 02:33:56 +00001797/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001798 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001799 * | 'try' ':' suite 'finally' ':' suite
1800 *
1801 */
Guido van Rossum47478871996-08-21 14:32:37 +00001802static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001803validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001804{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001805 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001806 int pos = 3;
1807 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001808 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001809
1810 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001811 res = (validate_name(CHILD(tree, 0), "try")
1812 && validate_colon(CHILD(tree, 1))
1813 && validate_suite(CHILD(tree, 2))
1814 && validate_colon(CHILD(tree, nch - 2))
1815 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001816 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001817 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001818 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1819 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001820
1821 PyErr_Format(parser_error,
1822 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001823 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001824 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001825 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001826 res = (validate_except_clause(CHILD(tree, pos))
1827 && validate_colon(CHILD(tree, pos + 1))
1828 && validate_suite(CHILD(tree, pos + 2)));
1829 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001830 }
1831 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001832 res = validate_ntype(CHILD(tree, pos), NAME);
1833 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1834 res = (validate_numnodes(tree, 6, "try/finally")
1835 && validate_colon(CHILD(tree, 4))
1836 && validate_suite(CHILD(tree, 5)));
1837 else if (res) {
1838 if (nch == (pos + 3)) {
1839 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1840 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1841 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001842 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001843 }
1844 else if (nch == (pos + 6)) {
1845 res = (validate_name(CHILD(tree, pos), "except")
1846 && validate_colon(CHILD(tree, pos + 1))
1847 && validate_suite(CHILD(tree, pos + 2))
1848 && validate_name(CHILD(tree, pos + 3), "else"));
1849 }
1850 else
1851 res = validate_numnodes(tree, pos + 3, "try/except");
1852 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001853 }
1854 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001855}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001856
1857
Guido van Rossum47478871996-08-21 14:32:37 +00001858static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001859validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001860{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001861 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001862 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001863 && ((nch == 1) || (nch == 2) || (nch == 4))
1864 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001865
Guido van Rossum3d602e31996-07-21 02:33:56 +00001866 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001867 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001868 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001869 res = (validate_comma(CHILD(tree, 2))
1870 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001871
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001872 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001873}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001874
1875
Guido van Rossum47478871996-08-21 14:32:37 +00001876static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001877validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001878{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001879 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001880 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001881
Guido van Rossum3d602e31996-07-21 02:33:56 +00001882 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001883 res = ((nch == 1)
1884 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001885 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001886 int pos;
1887 res = validate_and_test(CHILD(tree, 0));
1888 for (pos = 1; res && (pos < nch); pos += 2)
1889 res = (validate_name(CHILD(tree, pos), "or")
1890 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001891 }
1892 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001893}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001894
1895
Guido van Rossum47478871996-08-21 14:32:37 +00001896static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001897validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001898{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001899 int pos;
1900 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001901 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001902 && is_odd(nch)
1903 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001904
Guido van Rossum3d602e31996-07-21 02:33:56 +00001905 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001906 res = (validate_name(CHILD(tree, pos), "and")
1907 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001908
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001909 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001910}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001911
1912
Guido van Rossum47478871996-08-21 14:32:37 +00001913static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001914validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001915{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001916 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001917 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001918
Guido van Rossum3d602e31996-07-21 02:33:56 +00001919 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001920 if (nch == 2)
1921 res = (validate_name(CHILD(tree, 0), "not")
1922 && validate_not_test(CHILD(tree, 1)));
1923 else if (nch == 1)
1924 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001925 }
1926 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001927}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001928
1929
Guido van Rossum47478871996-08-21 14:32:37 +00001930static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001931validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001932{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001933 int pos;
1934 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001935 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001936 && is_odd(nch)
1937 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001938
Guido van Rossum3d602e31996-07-21 02:33:56 +00001939 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001940 res = (validate_comp_op(CHILD(tree, pos))
1941 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001942
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001943 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001944}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001945
1946
Guido van Rossum47478871996-08-21 14:32:37 +00001947static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001948validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001949{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001950 int res = 0;
1951 int nch = NCH(tree);
1952
Guido van Rossum3d602e31996-07-21 02:33:56 +00001953 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001954 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001955 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001956 /*
1957 * Only child will be a terminal with a well-defined symbolic name
1958 * or a NAME with a string of either 'is' or 'in'
1959 */
1960 tree = CHILD(tree, 0);
1961 switch (TYPE(tree)) {
1962 case LESS:
1963 case GREATER:
1964 case EQEQUAL:
1965 case EQUAL:
1966 case LESSEQUAL:
1967 case GREATEREQUAL:
1968 case NOTEQUAL:
1969 res = 1;
1970 break;
1971 case NAME:
1972 res = ((strcmp(STR(tree), "in") == 0)
1973 || (strcmp(STR(tree), "is") == 0));
1974 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001975 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00001976 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00001977 }
1978 break;
1979 default:
Fred Drake661ea262000-10-24 19:57:45 +00001980 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001981 break;
1982 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001983 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00001984 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001985 res = (validate_ntype(CHILD(tree, 0), NAME)
1986 && validate_ntype(CHILD(tree, 1), NAME)
1987 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
1988 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
1989 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
1990 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
1991 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001992 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001993 }
1994 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001995}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001996
1997
Guido van Rossum47478871996-08-21 14:32:37 +00001998static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001999validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002000{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002001 int j;
2002 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002003 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002004 && is_odd(nch)
2005 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002006
Guido van Rossum3d602e31996-07-21 02:33:56 +00002007 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002008 res = (validate_xor_expr(CHILD(tree, j))
2009 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002010
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002011 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002012}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002013
2014
Guido van Rossum47478871996-08-21 14:32:37 +00002015static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002016validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002017{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002018 int j;
2019 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002020 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002021 && is_odd(nch)
2022 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002023
Guido van Rossum3d602e31996-07-21 02:33:56 +00002024 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002025 res = (validate_circumflex(CHILD(tree, j - 1))
2026 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002027
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002028 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002029}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002030
2031
Guido van Rossum47478871996-08-21 14:32:37 +00002032static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002033validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002034{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002035 int pos;
2036 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002037 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002038 && is_odd(nch)
2039 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002040
Guido van Rossum3d602e31996-07-21 02:33:56 +00002041 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002042 res = (validate_ampersand(CHILD(tree, pos))
2043 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002044
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002045 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002046}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002047
2048
2049static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002050validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002051 {
2052 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002053 int nch = NCH(tree);
2054 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002055 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002056
Guido van Rossum3d602e31996-07-21 02:33:56 +00002057 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002058 if (TYPE(CHILD(tree, pos)) != op1)
2059 res = validate_ntype(CHILD(tree, pos), op2);
2060 if (res)
2061 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002062 }
2063 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002064}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002065
2066
Guido van Rossum47478871996-08-21 14:32:37 +00002067static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002068validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002069{
2070 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002071 && validate_chain_two_ops(tree, validate_arith_expr,
2072 LEFTSHIFT, RIGHTSHIFT));
2073}
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_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002078{
2079 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002080 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2081}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002082
2083
Guido van Rossum47478871996-08-21 14:32:37 +00002084static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002085validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002086{
2087 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002088 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002089 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002090 && is_odd(nch)
2091 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002092
Guido van Rossum3d602e31996-07-21 02:33:56 +00002093 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002094 res = (((TYPE(CHILD(tree, pos)) == STAR)
2095 || (TYPE(CHILD(tree, pos)) == SLASH)
2096 || (TYPE(CHILD(tree, pos)) == PERCENT))
2097 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002098
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002099 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002100}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002101
2102
Guido van Rossum3d602e31996-07-21 02:33:56 +00002103/* factor:
2104 *
2105 * factor: ('+'|'-'|'~') factor | power
2106 */
Guido van Rossum47478871996-08-21 14:32:37 +00002107static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002108validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002109{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002110 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002111 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002112 && (((nch == 2)
2113 && ((TYPE(CHILD(tree, 0)) == PLUS)
2114 || (TYPE(CHILD(tree, 0)) == MINUS)
2115 || (TYPE(CHILD(tree, 0)) == TILDE))
2116 && validate_factor(CHILD(tree, 1)))
2117 || ((nch == 1)
2118 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002119 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002120}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002121
2122
Guido van Rossum3d602e31996-07-21 02:33:56 +00002123/* power:
2124 *
2125 * power: atom trailer* ('**' factor)*
2126 */
Guido van Rossum47478871996-08-21 14:32:37 +00002127static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002128validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002129{
2130 int pos = 1;
2131 int nch = NCH(tree);
2132 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002133 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002134
2135 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002136 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002137 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002138 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002139 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002140 return (0);
2141 }
2142 for ( ; res && (pos < (nch - 1)); pos += 2)
2143 res = (validate_doublestar(CHILD(tree, pos))
2144 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002145 }
2146 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002147}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002148
2149
Guido van Rossum47478871996-08-21 14:32:37 +00002150static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002151validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002152{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002153 int pos;
2154 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002155 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002156
Fred Drakecff283c2000-08-21 22:24:43 +00002157 if (res && nch < 1)
2158 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002159 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002160 switch (TYPE(CHILD(tree, 0))) {
2161 case LPAR:
2162 res = ((nch <= 3)
2163 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002164
Fred Drakeff9ea482000-04-19 13:54:15 +00002165 if (res && (nch == 3))
2166 res = validate_testlist(CHILD(tree, 1));
2167 break;
2168 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002169 if (nch == 2)
2170 res = validate_ntype(CHILD(tree, 1), RSQB);
2171 else if (nch == 3)
2172 res = (validate_listmaker(CHILD(tree, 1))
2173 && validate_ntype(CHILD(tree, 2), RSQB));
2174 else {
2175 res = 0;
2176 err_string("illegal list display atom");
2177 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002178 break;
2179 case LBRACE:
2180 res = ((nch <= 3)
2181 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002182
Fred Drakeff9ea482000-04-19 13:54:15 +00002183 if (res && (nch == 3))
2184 res = validate_dictmaker(CHILD(tree, 1));
2185 break;
2186 case BACKQUOTE:
2187 res = ((nch == 3)
2188 && validate_testlist(CHILD(tree, 1))
2189 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2190 break;
2191 case NAME:
2192 case NUMBER:
2193 res = (nch == 1);
2194 break;
2195 case STRING:
2196 for (pos = 1; res && (pos < nch); ++pos)
2197 res = validate_ntype(CHILD(tree, pos), STRING);
2198 break;
2199 default:
2200 res = 0;
2201 break;
2202 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002203 }
2204 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002205}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002206
2207
Fred Drake85bf3bb2000-08-23 15:35:26 +00002208/* listmaker:
2209 * test ( list_for | (',' test)* [','] )
2210 */
Fred Drakecff283c2000-08-21 22:24:43 +00002211static int
2212validate_listmaker(node *tree)
2213{
2214 int nch = NCH(tree);
2215 int ok = nch;
2216
2217 if (nch == 0)
2218 err_string("missing child nodes of listmaker");
2219 else
2220 ok = validate_test(CHILD(tree, 0));
2221
2222 /*
2223 * list_iter | (',' test)* [',']
2224 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002225 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2226 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002227 else {
2228 /* (',' test)* [','] */
2229 int i = 1;
2230 while (ok && nch - i >= 2) {
2231 ok = (validate_comma(CHILD(tree, i))
2232 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002233 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002234 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002235 if (ok && i == nch-1)
2236 ok = validate_comma(CHILD(tree, i));
2237 else if (i != nch) {
2238 ok = 0;
2239 err_string("illegal trailing nodes for listmaker");
2240 }
Fred Drakecff283c2000-08-21 22:24:43 +00002241 }
2242 return ok;
2243}
2244
2245
Guido van Rossum3d602e31996-07-21 02:33:56 +00002246/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002247 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002248 *
2249 */
Guido van Rossum47478871996-08-21 14:32:37 +00002250static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002251validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002252{
2253 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002254 && validate_numnodes(tree, 5, "funcdef")
2255 && validate_name(CHILD(tree, 0), "def")
2256 && validate_ntype(CHILD(tree, 1), NAME)
2257 && validate_colon(CHILD(tree, 3))
2258 && validate_parameters(CHILD(tree, 2))
2259 && validate_suite(CHILD(tree, 4)));
2260}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002261
2262
Guido van Rossum47478871996-08-21 14:32:37 +00002263static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002264validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002265{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002266 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002267 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002268 && ((nch == 3) || (nch == 4))
2269 && validate_name(CHILD(tree, 0), "lambda")
2270 && validate_colon(CHILD(tree, nch - 2))
2271 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002272
Guido van Rossum3d602e31996-07-21 02:33:56 +00002273 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002274 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002275 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002276 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002277
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002278 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002279}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002280
2281
Guido van Rossum3d602e31996-07-21 02:33:56 +00002282/* arglist:
2283 *
Fred Drakecff283c2000-08-21 22:24:43 +00002284 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002285 */
Guido van Rossum47478871996-08-21 14:32:37 +00002286static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002287validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002288{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002289 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002290 int i = 0;
2291 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002292
2293 if (nch <= 0)
2294 /* raise the right error from having an invalid number of children */
2295 return validate_numnodes(tree, nch + 1, "arglist");
2296
Fred Drakecff283c2000-08-21 22:24:43 +00002297 while (ok && nch-i >= 2) {
2298 /* skip leading (argument ',') */
2299 ok = (validate_argument(CHILD(tree, i))
2300 && validate_comma(CHILD(tree, i+1)));
2301 if (ok)
2302 i += 2;
2303 else
2304 PyErr_Clear();
2305 }
2306 ok = 1;
2307 if (nch-i > 0) {
2308 /*
2309 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002310 */
Fred Drakecff283c2000-08-21 22:24:43 +00002311 int sym = TYPE(CHILD(tree, i));
2312
2313 if (sym == argument) {
2314 ok = validate_argument(CHILD(tree, i));
2315 if (ok && i+1 != nch) {
2316 err_string("illegal arglist specification"
2317 " (extra stuff on end)");
2318 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002319 }
Fred Drakecff283c2000-08-21 22:24:43 +00002320 }
2321 else if (sym == STAR) {
2322 ok = validate_star(CHILD(tree, i));
2323 if (ok && (nch-i == 2))
2324 ok = validate_test(CHILD(tree, i+1));
2325 else if (ok && (nch-i == 5))
2326 ok = (validate_test(CHILD(tree, i+1))
2327 && validate_comma(CHILD(tree, i+2))
2328 && validate_doublestar(CHILD(tree, i+3))
2329 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002330 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002331 err_string("illegal use of '*' in arglist");
2332 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002333 }
2334 }
Fred Drakecff283c2000-08-21 22:24:43 +00002335 else if (sym == DOUBLESTAR) {
2336 if (nch-i == 2)
2337 ok = (validate_doublestar(CHILD(tree, i))
2338 && validate_test(CHILD(tree, i+1)));
2339 else {
2340 err_string("illegal use of '**' in arglist");
2341 ok = 0;
2342 }
2343 }
2344 else {
2345 err_string("illegal arglist specification");
2346 ok = 0;
2347 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002348 }
2349 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002350}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002351
2352
2353
2354/* argument:
2355 *
2356 * [test '='] test
2357 */
Guido van Rossum47478871996-08-21 14:32:37 +00002358static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002359validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002360{
2361 int nch = NCH(tree);
2362 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002363 && ((nch == 1) || (nch == 3))
2364 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002365
2366 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002367 res = (validate_equal(CHILD(tree, 1))
2368 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002369
2370 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002371}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002372
2373
2374
2375/* trailer:
2376 *
Guido van Rossum47478871996-08-21 14:32:37 +00002377 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002378 */
Guido van Rossum47478871996-08-21 14:32:37 +00002379static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002380validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002381{
2382 int nch = NCH(tree);
2383 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002384
2385 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002386 switch (TYPE(CHILD(tree, 0))) {
2387 case LPAR:
2388 res = validate_rparen(CHILD(tree, nch - 1));
2389 if (res && (nch == 3))
2390 res = validate_arglist(CHILD(tree, 1));
2391 break;
2392 case LSQB:
2393 res = (validate_numnodes(tree, 3, "trailer")
2394 && validate_subscriptlist(CHILD(tree, 1))
2395 && validate_ntype(CHILD(tree, 2), RSQB));
2396 break;
2397 case DOT:
2398 res = (validate_numnodes(tree, 2, "trailer")
2399 && validate_ntype(CHILD(tree, 1), NAME));
2400 break;
2401 default:
2402 res = 0;
2403 break;
2404 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002405 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002406 else {
2407 (void) validate_numnodes(tree, 2, "trailer");
2408 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002409 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002410}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002411
2412
Guido van Rossum47478871996-08-21 14:32:37 +00002413/* subscriptlist:
2414 *
2415 * subscript (',' subscript)* [',']
2416 */
2417static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002418validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002419{
2420 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002421 validate_subscript, "subscriptlist"));
2422}
Guido van Rossum47478871996-08-21 14:32:37 +00002423
2424
Guido van Rossum3d602e31996-07-21 02:33:56 +00002425/* subscript:
2426 *
Guido van Rossum47478871996-08-21 14:32:37 +00002427 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002428 */
Guido van Rossum47478871996-08-21 14:32:37 +00002429static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002430validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002431{
Guido van Rossum47478871996-08-21 14:32:37 +00002432 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002433 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002434 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002435
Guido van Rossum47478871996-08-21 14:32:37 +00002436 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002437 if (!PyErr_Occurred())
2438 err_string("invalid number of arguments for subscript node");
2439 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002440 }
Guido van Rossum47478871996-08-21 14:32:37 +00002441 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002442 /* take care of ('.' '.' '.') possibility */
2443 return (validate_numnodes(tree, 3, "subscript")
2444 && validate_dot(CHILD(tree, 0))
2445 && validate_dot(CHILD(tree, 1))
2446 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002447 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002448 if (TYPE(CHILD(tree, 0)) == test)
2449 res = validate_test(CHILD(tree, 0));
2450 else
2451 res = validate_colon(CHILD(tree, 0));
2452 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002453 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002454 /* Must be [test] ':' [test] [sliceop],
2455 * but at least one of the optional components will
2456 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002457 */
2458 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002459 res = validate_test(CHILD(tree, 0));
2460 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002461 }
2462 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002463 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002464 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002465 int rem = nch - ++offset;
2466 if (rem) {
2467 if (TYPE(CHILD(tree, offset)) == test) {
2468 res = validate_test(CHILD(tree, offset));
2469 ++offset;
2470 --rem;
2471 }
2472 if (res && rem)
2473 res = validate_sliceop(CHILD(tree, offset));
2474 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002475 }
2476 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002477}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002478
2479
Guido van Rossum47478871996-08-21 14:32:37 +00002480static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002481validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002482{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002483 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002484 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002485 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002486 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002487 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002488 }
Guido van Rossum47478871996-08-21 14:32:37 +00002489 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002490 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002491 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002492 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002493
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002494 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002495}
Guido van Rossum47478871996-08-21 14:32:37 +00002496
2497
2498static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002499validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002500{
2501 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002502 validate_expr, "exprlist"));
2503}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002504
2505
Guido van Rossum47478871996-08-21 14:32:37 +00002506static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002507validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002508{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002509 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002510 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002511 && (nch >= 3)
2512 && validate_test(CHILD(tree, 0))
2513 && validate_colon(CHILD(tree, 1))
2514 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002515
Guido van Rossum3d602e31996-07-21 02:33:56 +00002516 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002517 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002518 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002519 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002520
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002521 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002522 int pos = 3;
2523 /* ( ',' test ':' test )* */
2524 while (res && (pos < nch)) {
2525 res = (validate_comma(CHILD(tree, pos))
2526 && validate_test(CHILD(tree, pos + 1))
2527 && validate_colon(CHILD(tree, pos + 2))
2528 && validate_test(CHILD(tree, pos + 3)));
2529 pos += 4;
2530 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002531 }
2532 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002533}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002534
2535
Guido van Rossum47478871996-08-21 14:32:37 +00002536static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002537validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002538{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002539 int pos;
2540 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002541 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002542 && (nch >= 2)
2543 && validate_testlist(CHILD(tree, 0))
2544 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002545
Guido van Rossum3d602e31996-07-21 02:33:56 +00002546 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002547 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002548
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002549 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002550}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002551
2552
Guido van Rossum47478871996-08-21 14:32:37 +00002553static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002554validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002555{
Fred Drakeff9ea482000-04-19 13:54:15 +00002556 int nch = 0; /* num. children on current node */
2557 int res = 1; /* result value */
2558 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002559
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002560 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002561 nch = NCH(tree);
2562 next = 0;
2563 switch (TYPE(tree)) {
2564 /*
2565 * Definition nodes.
2566 */
2567 case funcdef:
2568 res = validate_funcdef(tree);
2569 break;
2570 case classdef:
2571 res = validate_class(tree);
2572 break;
2573 /*
2574 * "Trivial" parse tree nodes.
2575 * (Why did I call these trivial?)
2576 */
2577 case stmt:
2578 res = validate_stmt(tree);
2579 break;
2580 case small_stmt:
2581 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002582 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002583 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2584 */
2585 res = validate_small_stmt(tree);
2586 break;
2587 case flow_stmt:
2588 res = (validate_numnodes(tree, 1, "flow_stmt")
2589 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2590 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002591 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002592 || (TYPE(CHILD(tree, 0)) == return_stmt)
2593 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2594 if (res)
2595 next = CHILD(tree, 0);
2596 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002597 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002598 break;
Fred Drake02126f22001-07-17 02:59:15 +00002599 case yield_stmt:
2600 res = validate_yield_stmt(tree);
2601 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002602 /*
2603 * Compound statements.
2604 */
2605 case simple_stmt:
2606 res = validate_simple_stmt(tree);
2607 break;
2608 case compound_stmt:
2609 res = validate_compound_stmt(tree);
2610 break;
2611 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002612 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002613 */
2614 case expr_stmt:
2615 res = validate_expr_stmt(tree);
2616 break;
2617 case print_stmt:
2618 res = validate_print_stmt(tree);
2619 break;
2620 case del_stmt:
2621 res = validate_del_stmt(tree);
2622 break;
2623 case pass_stmt:
2624 res = (validate_numnodes(tree, 1, "pass")
2625 && validate_name(CHILD(tree, 0), "pass"));
2626 break;
2627 case break_stmt:
2628 res = (validate_numnodes(tree, 1, "break")
2629 && validate_name(CHILD(tree, 0), "break"));
2630 break;
2631 case continue_stmt:
2632 res = (validate_numnodes(tree, 1, "continue")
2633 && validate_name(CHILD(tree, 0), "continue"));
2634 break;
2635 case return_stmt:
2636 res = validate_return_stmt(tree);
2637 break;
2638 case raise_stmt:
2639 res = validate_raise_stmt(tree);
2640 break;
2641 case import_stmt:
2642 res = validate_import_stmt(tree);
2643 break;
2644 case global_stmt:
2645 res = validate_global_stmt(tree);
2646 break;
2647 case exec_stmt:
2648 res = validate_exec_stmt(tree);
2649 break;
2650 case assert_stmt:
2651 res = validate_assert_stmt(tree);
2652 break;
2653 case if_stmt:
2654 res = validate_if(tree);
2655 break;
2656 case while_stmt:
2657 res = validate_while(tree);
2658 break;
2659 case for_stmt:
2660 res = validate_for(tree);
2661 break;
2662 case try_stmt:
2663 res = validate_try(tree);
2664 break;
2665 case suite:
2666 res = validate_suite(tree);
2667 break;
2668 /*
2669 * Expression nodes.
2670 */
2671 case testlist:
2672 res = validate_testlist(tree);
2673 break;
2674 case test:
2675 res = validate_test(tree);
2676 break;
2677 case and_test:
2678 res = validate_and_test(tree);
2679 break;
2680 case not_test:
2681 res = validate_not_test(tree);
2682 break;
2683 case comparison:
2684 res = validate_comparison(tree);
2685 break;
2686 case exprlist:
2687 res = validate_exprlist(tree);
2688 break;
2689 case comp_op:
2690 res = validate_comp_op(tree);
2691 break;
2692 case expr:
2693 res = validate_expr(tree);
2694 break;
2695 case xor_expr:
2696 res = validate_xor_expr(tree);
2697 break;
2698 case and_expr:
2699 res = validate_and_expr(tree);
2700 break;
2701 case shift_expr:
2702 res = validate_shift_expr(tree);
2703 break;
2704 case arith_expr:
2705 res = validate_arith_expr(tree);
2706 break;
2707 case term:
2708 res = validate_term(tree);
2709 break;
2710 case factor:
2711 res = validate_factor(tree);
2712 break;
2713 case power:
2714 res = validate_power(tree);
2715 break;
2716 case atom:
2717 res = validate_atom(tree);
2718 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002719
Fred Drakeff9ea482000-04-19 13:54:15 +00002720 default:
2721 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002722 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002723 res = 0;
2724 break;
2725 }
2726 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002727 }
2728 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002729}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002730
2731
Guido van Rossum47478871996-08-21 14:32:37 +00002732static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002733validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002734{
2735 int res = validate_eval_input(tree);
2736
2737 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002738 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002739
2740 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002741}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002742
2743
Guido van Rossum3d602e31996-07-21 02:33:56 +00002744/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002745 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002746 */
Guido van Rossum47478871996-08-21 14:32:37 +00002747static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002748validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002749{
Fred Drakec2683dd2001-07-17 19:32:05 +00002750 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002751 int nch = NCH(tree) - 1;
2752 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002753 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002754
Fred Drakec2683dd2001-07-17 19:32:05 +00002755 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002756 if (TYPE(CHILD(tree, j)) == stmt)
2757 res = validate_stmt(CHILD(tree, j));
2758 else
2759 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002760 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002761 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002762 * user. Hopefully, this won't be needed. If a user reports getting
2763 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002764 */
2765 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002766 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002767
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002768 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002769}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002770
2771
Fred Drake43f8f9b1998-04-13 16:25:46 +00002772static PyObject*
2773pickle_constructor = NULL;
2774
2775
2776static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002777parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002778{
Fred Drake268397f1998-04-29 14:16:32 +00002779 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002780 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00002781 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002782 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002783
Fred Drakec2683dd2001-07-17 19:32:05 +00002784 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002785 PyObject *newargs;
2786 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002787
Fred Drake2a6875e1999-09-20 22:32:18 +00002788 if ((empty_dict = PyDict_New()) == NULL)
2789 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002790 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002791 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002792 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002793 if (tuple != NULL) {
2794 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2795 Py_DECREF(tuple);
2796 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002797 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002798 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002799 }
2800 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002801 Py_XDECREF(empty_dict);
2802
Fred Drake43f8f9b1998-04-13 16:25:46 +00002803 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002804}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002805
2806
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002807/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00002808 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002809 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002810 * We'd really have to write a wrapper around it all anyway to allow
2811 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002812 */
2813static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00002814 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
2815 "Creates a tuple-tree representation of an ST."},
2816 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
2817 "Creates a list-tree representation of an ST."},
2818 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
2819 "Compiles an ST object into a code object."},
2820 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
2821 "Compiles an ST object into a code object."},
2822 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
2823 "Creates an ST object from an expression."},
2824 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
2825 "Determines if an ST object was created from an expression."},
2826 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
2827 "Determines if an ST object was created from a suite."},
2828 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
2829 "Creates an ST object from a suite."},
2830 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2831 "Creates an ST object from a tree representation."},
2832 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2833 "Creates an ST object from a tree representation."},
2834 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
2835 "Creates a tuple-tree representation of an ST."},
2836 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
2837 "Creates a list-tree representation of an ST."},
2838 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2839 "Creates an ST object from a tree representation."},
2840 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2841 "Creates an ST object from a tree representation."},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002842
Fred Drake43f8f9b1998-04-13 16:25:46 +00002843 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00002844 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Fred Drakec2683dd2001-07-17 19:32:05 +00002845 "Returns the pickle magic to allow ST objects to be pickled."},
Fred Drake43f8f9b1998-04-13 16:25:46 +00002846
Fred Drake268397f1998-04-29 14:16:32 +00002847 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002848 };
2849
2850
Tim Peters6d7c4422000-08-26 07:38:06 +00002851DL_EXPORT(void) initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00002852
Guido van Rossum3886bb61998-12-04 18:50:17 +00002853DL_EXPORT(void)
Thomas Wouters5c669862000-07-24 15:49:08 +00002854initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00002855{
Fred Drake13130bc2001-08-15 16:44:56 +00002856 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00002857
2858 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002859 module = Py_InitModule("parser", parser_functions);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002860
Fred Drake7a15ba51999-09-09 14:21:52 +00002861 if (parser_error == 0)
2862 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002863
2864 if ((parser_error == 0)
Fred Drake13130bc2001-08-15 16:44:56 +00002865 || (PyModule_AddObject(module, "ParserError", parser_error) != 0)) {
Fred Drakec2683dd2001-07-17 19:32:05 +00002866 /* caller will check PyErr_Occurred() */
2867 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002868 }
Fred Drakec2683dd2001-07-17 19:32:05 +00002869 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002870 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00002871 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002872 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002873
Fred Drake13130bc2001-08-15 16:44:56 +00002874 PyModule_AddStringConstant(module, "__copyright__",
2875 parser_copyright_string);
2876 PyModule_AddStringConstant(module, "__doc__",
2877 parser_doc_string);
2878 PyModule_AddStringConstant(module, "__version__",
2879 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002880
Fred Drake78bdb9b2001-07-19 20:17:15 +00002881 /* Register to support pickling.
2882 * If this fails, the import of this module will fail because an
2883 * exception will be raised here; should we clear the exception?
2884 */
Fred Drake13130bc2001-08-15 16:44:56 +00002885 copyreg = PyImport_ImportModule("copy_reg");
2886 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002887 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002888
Fred Drake13130bc2001-08-15 16:44:56 +00002889 func = PyObject_GetAttrString(copyreg, "pickle");
2890 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
2891 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00002892 Py_XINCREF(pickle_constructor);
2893 if ((func != NULL) && (pickle_constructor != NULL)
2894 && (pickler != NULL)) {
2895 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002896
Fred Drakec2683dd2001-07-17 19:32:05 +00002897 res = PyObject_CallFunction(func, "OOO", &PyST_Type, pickler,
2898 pickle_constructor);
Fred Drakeff9ea482000-04-19 13:54:15 +00002899 Py_XDECREF(res);
2900 }
2901 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00002902 Py_XDECREF(pickle_constructor);
2903 Py_XDECREF(pickler);
2904 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002905 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002906}