blob: e0c74313c8208d7be3a44fd68f8ec0b9ec908bc8 [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000026 */
27
Fred Drakeff9ea482000-04-19 13:54:15 +000028#include "Python.h" /* general Python API */
29#include "graminit.h" /* symbols defined in the grammar */
30#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000031#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000032#include "token.h" /* token definitions */
33 /* ISTERMINAL() / ISNONTERMINAL() */
34#include "compile.h" /* PyNode_Compile() */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000035
Fred Drake268397f1998-04-29 14:16:32 +000036#ifdef lint
37#include <note.h>
38#else
39#define NOTE(x)
40#endif
41
Guido van Rossum19efc5f1998-04-28 16:10:19 +000042#ifdef macintosh
Fred Drakeff9ea482000-04-19 13:54:15 +000043char *strdup(char *);
Guido van Rossum19efc5f1998-04-28 16:10:19 +000044#endif
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000045
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000046/* String constants used to initialize module attributes.
47 *
48 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000049static char parser_copyright_string[] =
50"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000051University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
52Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
53Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000054
55
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000056PyDoc_STRVAR(parser_doc_string,
57"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000058
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000059static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000060
61
Fred Drakeff9ea482000-04-19 13:54:15 +000062typedef PyObject* (*SeqMaker) (int length);
63typedef int (*SeqInserter) (PyObject* sequence,
64 int index,
65 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000066
Thomas Wouters7e474022000-07-16 12:04:32 +000067/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000068 * original copyright statement is included below, and continues to apply
69 * in full to the function immediately following. All other material is
70 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
71 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000072 * new naming conventions. Added arguments to provide support for creating
73 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000074 */
75
Guido van Rossum52f2c051993-11-10 12:53:24 +000076
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000077static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000078node2tuple(node *n, /* node to convert */
79 SeqMaker mkseq, /* create sequence */
80 SeqInserter addelem, /* func. to add elem. in seq. */
81 int lineno) /* include line numbers? */
Guido van Rossum47478871996-08-21 14:32:37 +000082{
Guido van Rossum3d602e31996-07-21 02:33:56 +000083 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000084 Py_INCREF(Py_None);
85 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000086 }
87 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000088 int i;
89 PyObject *v;
90 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000091
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000092 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000093 if (v == NULL)
94 return (v);
95 w = PyInt_FromLong(TYPE(n));
96 if (w == NULL) {
97 Py_DECREF(v);
98 return ((PyObject*) NULL);
99 }
100 (void) addelem(v, 0, w);
101 for (i = 0; i < NCH(n); i++) {
102 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno);
103 if (w == NULL) {
104 Py_DECREF(v);
105 return ((PyObject*) NULL);
106 }
107 (void) addelem(v, i+1, w);
108 }
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000109
110 if (TYPE(n) == encoding_decl)
111 (void) addelem(v, i+1, PyString_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000112 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000113 }
114 else if (ISTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000115 PyObject *result = mkseq(2 + lineno);
116 if (result != NULL) {
117 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
118 (void) addelem(result, 1, PyString_FromString(STR(n)));
119 if (lineno == 1)
120 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
121 }
122 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000123 }
124 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000125 PyErr_SetString(PyExc_SystemError,
126 "unrecognized parse tree node type");
127 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000128 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000129}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000130/*
131 * End of material copyrighted by Stichting Mathematisch Centrum.
132 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000133
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000134
135
136/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000137 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000138 * field of the object type to identify which any given object represents.
139 * These should probably go in an external header to allow other extensions
140 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000141 */
142
Fred Drakec2683dd2001-07-17 19:32:05 +0000143#define PyST_EXPR 1
144#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000145
146
147/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000148 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000149 * naming style, but the code uses the new naming convention.
150 */
151
152static PyObject*
153parser_error = 0;
154
155
Fred Drakec2683dd2001-07-17 19:32:05 +0000156typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000157 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000158 node* st_node; /* the node* returned by the parser */
159 int st_type; /* EXPR or SUITE ? */
160} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000161
162
Jeremy Hylton938ace62002-07-17 16:30:39 +0000163static void parser_free(PyST_Object *st);
164static int parser_compare(PyST_Object *left, PyST_Object *right);
165static PyObject *parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000166
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000167
Fred Drake268397f1998-04-29 14:16:32 +0000168static
Fred Drakec2683dd2001-07-17 19:32:05 +0000169PyTypeObject PyST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000170 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000171 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000172 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000173 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000174 0, /* tp_itemsize */
175 (destructor)parser_free, /* tp_dealloc */
176 0, /* tp_print */
177 parser_getattr, /* tp_getattr */
178 0, /* tp_setattr */
179 (cmpfunc)parser_compare, /* tp_compare */
180 0, /* tp_repr */
181 0, /* tp_as_number */
182 0, /* tp_as_sequence */
183 0, /* tp_as_mapping */
184 0, /* tp_hash */
185 0, /* tp_call */
186 0, /* tp_str */
187 0, /* tp_getattro */
188 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000189
190 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000191 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000192
Fred Drakeff9ea482000-04-19 13:54:15 +0000193 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000194
195 /* __doc__ */
196 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000197}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000198
199
200static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000201parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000202{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000203 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000204
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000205 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000206 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000207
208 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000209 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000210
211 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000212 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000213
214 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000215 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000216
217 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000218 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000219
220 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000221 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000222
Fred Drakeff9ea482000-04-19 13:54:15 +0000223 if (v != 0)
224 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000225 }
226 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000227}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000228
229
Fred Drakec2683dd2001-07-17 19:32:05 +0000230/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000231 *
232 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
233 * This really just wraps a call to parser_compare_nodes() with some easy
234 * checks and protection code.
235 *
236 */
237static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000238parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000239{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000240 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000241 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000242
243 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000244 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000245
Fred Drakec2683dd2001-07-17 19:32:05 +0000246 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000247}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000248
249
Fred Drakec2683dd2001-07-17 19:32:05 +0000250/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000251 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000252 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000253 * 'wrapper' object that holds a node* and allows it to be passed around in
254 * Python code.
255 *
256 */
257static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000258parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000259{
Fred Drakec2683dd2001-07-17 19:32:05 +0000260 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000261
262 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000263 o->st_node = st;
264 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265 }
Fred Drake268397f1998-04-29 14:16:32 +0000266 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000267 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000268 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000269 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000270}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000271
272
Fred Drakec2683dd2001-07-17 19:32:05 +0000273/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000274 *
275 * This is called by a del statement that reduces the reference count to 0.
276 *
277 */
278static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000279parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000280{
Fred Drakec2683dd2001-07-17 19:32:05 +0000281 PyNode_Free(st->st_node);
282 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000283}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000284
285
Fred Drakec2683dd2001-07-17 19:32:05 +0000286/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000287 *
288 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000289 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000290 *
291 */
292static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000293parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000294{
Guido van Rossum47478871996-08-21 14:32:37 +0000295 PyObject *line_option = 0;
296 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000297 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000298
Fred Drake7a15ba51999-09-09 14:21:52 +0000299 static char *keywords[] = {"ast", "line_info", NULL};
300
Fred Drake268397f1998-04-29 14:16:32 +0000301 if (self == NULL) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000302 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2tuple", keywords,
303 &PyST_Type, &self, &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000304 }
Fred Drake503d8d61998-04-13 18:45:18 +0000305 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000306 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:totuple", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000307 &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000308 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000309 int lineno = 0;
310 if (line_option != NULL) {
311 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
312 }
313 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000314 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000315 * since it's known to work already.
316 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000317 res = node2tuple(((PyST_Object*)self)->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000318 PyTuple_New, PyTuple_SetItem, lineno);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000319 }
320 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000321}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000322
323
Fred Drakec2683dd2001-07-17 19:32:05 +0000324/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000325 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000326 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000327 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000328 *
329 */
330static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000331parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000332{
Guido van Rossum47478871996-08-21 14:32:37 +0000333 PyObject *line_option = 0;
334 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000335 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000336
Fred Drake7a15ba51999-09-09 14:21:52 +0000337 static char *keywords[] = {"ast", "line_info", NULL};
338
Fred Drake503d8d61998-04-13 18:45:18 +0000339 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000340 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2list", keywords,
341 &PyST_Type, &self, &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000342 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000343 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:tolist", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000344 &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000345 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000346 int lineno = 0;
347 if (line_option != 0) {
348 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
349 }
350 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000351 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000352 * since it's known to work already.
353 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000354 res = node2tuple(self->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000355 PyList_New, PyList_SetItem, lineno);
Guido van Rossum47478871996-08-21 14:32:37 +0000356 }
357 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000358}
Guido van Rossum47478871996-08-21 14:32:37 +0000359
360
Fred Drakec2683dd2001-07-17 19:32:05 +0000361/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000362 *
363 * This function creates code objects from the parse tree represented by
364 * the passed-in data object. An optional file name is passed in as well.
365 *
366 */
367static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000368parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000369{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000370 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000371 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000372 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000373
Fred Drake7a15ba51999-09-09 14:21:52 +0000374 static char *keywords[] = {"ast", "filename", NULL};
375
Fred Drake503d8d61998-04-13 18:45:18 +0000376 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000377 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
378 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000379 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000380 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000381 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000382
383 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000384 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000385
386 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000387}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000388
389
390/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
391 * PyObject* parser_issuite(PyObject* self, PyObject* args)
392 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000393 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000394 * a statement suite, respectively. The return is a Python truth value.
395 *
396 */
397static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000398parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000399{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000400 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000401 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000402
Fred Drake7a15ba51999-09-09 14:21:52 +0000403 static char *keywords[] = {"ast", NULL};
404
Fred Drake503d8d61998-04-13 18:45:18 +0000405 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000406 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000407 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000408 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000409 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000410
411 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000412 /* Check to see if the ST represents an expression or not. */
413 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000414 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000415 }
416 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000417}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000418
419
420static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000421parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000422{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000423 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000424 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000425
Fred Drake7a15ba51999-09-09 14:21:52 +0000426 static char *keywords[] = {"ast", NULL};
427
Fred Drake503d8d61998-04-13 18:45:18 +0000428 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000429 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000430 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000431 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000432 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000433
434 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000435 /* Check to see if the ST represents an expression or not. */
436 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000437 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000438 }
439 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000440}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000441
442
Fred Drake7a15ba51999-09-09 14:21:52 +0000443#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
444
Fred Drake503d8d61998-04-13 18:45:18 +0000445static PyMethodDef
446parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000447 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000448 PyDoc_STR("Compile this ST object into a code object.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000449 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000450 PyDoc_STR("Determines if this ST object was created from an expression.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000451 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000452 PyDoc_STR("Determines if this ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000453 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000454 PyDoc_STR("Creates a list-tree representation of this ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000455 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000456 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Fred Drake503d8d61998-04-13 18:45:18 +0000457
Fred Drake268397f1998-04-29 14:16:32 +0000458 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000459};
460
Fred Drake503d8d61998-04-13 18:45:18 +0000461
462static PyObject*
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000463parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000464{
Fred Drake503d8d61998-04-13 18:45:18 +0000465 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000466}
Fred Drake503d8d61998-04-13 18:45:18 +0000467
468
Guido van Rossum3d602e31996-07-21 02:33:56 +0000469/* err_string(char* message)
470 *
471 * Sets the error string for an exception of type ParserError.
472 *
473 */
474static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000475err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000476{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000477 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000478}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000479
480
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000481/* PyObject* parser_do_parse(PyObject* args, int type)
482 *
483 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000484 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000485 *
486 */
487static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000488parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000489{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000490 char* string = 0;
491 PyObject* res = 0;
492
Fred Drake7a15ba51999-09-09 14:21:52 +0000493 static char *keywords[] = {"source", NULL};
494
495 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000496 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000497 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000498 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000499
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000500 if (n)
501 res = parser_newstobject(n, type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000502 }
503 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000504}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000505
506
507/* PyObject* parser_expr(PyObject* self, PyObject* args)
508 * PyObject* parser_suite(PyObject* self, PyObject* args)
509 *
510 * External interfaces to the parser itself. Which is called determines if
511 * the parser attempts to recognize an expression ('eval' form) or statement
512 * suite ('exec' form). The real work is done by parser_do_parse() above.
513 *
514 */
515static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000516parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000517{
Fred Drake268397f1998-04-29 14:16:32 +0000518 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000519 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000520}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000521
522
523static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000524parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000525{
Fred Drake268397f1998-04-29 14:16:32 +0000526 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000527 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000528}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000529
530
531
Fred Drakec2683dd2001-07-17 19:32:05 +0000532/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000533 * object requires that the input tuple be valid without having to rely on
534 * catching an exception from the compiler. This is done to allow the
535 * compiler itself to remain fast, since most of its input will come from
536 * the parser directly, and therefore be known to be syntactically correct.
537 * This validation is done to ensure that we don't core dump the compile
538 * phase, returning an exception instead.
539 *
540 * Two aspects can be broken out in this code: creating a node tree from
541 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000542 * advantageous to expand the number of ST types to include funcdefs and
543 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000544 * here. They are not necessary, and not quite as useful in a raw form.
545 * For now, let's get expressions and suites working reliably.
546 */
547
548
Jeremy Hylton938ace62002-07-17 16:30:39 +0000549static node* build_node_tree(PyObject *tuple);
550static int validate_expr_tree(node *tree);
551static int validate_file_input(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000552
553
Fred Drakec2683dd2001-07-17 19:32:05 +0000554/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000555 *
556 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000557 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000558 * tuple can be validated. It does this by checking the first code of the
559 * tuple, and, if acceptable, builds the internal representation. If this
560 * step succeeds, the internal representation is validated as fully as
561 * possible with the various validate_*() routines defined below.
562 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000563 * This function must be changed if support is to be added for PyST_FRAGMENT
564 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000565 *
566 */
567static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000568parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000569{
Fred Drake268397f1998-04-29 14:16:32 +0000570 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000571 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000572 PyObject *tuple;
573 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000574
Fred Drake7a15ba51999-09-09 14:21:52 +0000575 static char *keywords[] = {"sequence", NULL};
576
Fred Drakec2683dd2001-07-17 19:32:05 +0000577 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000578 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000579 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000580 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000581 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000582 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000583 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000584 }
585 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000586 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000587 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000588 tree = build_node_tree(tuple);
589 if (tree != 0) {
590 int start_sym = TYPE(tree);
591 if (start_sym == eval_input) {
592 /* Might be an eval form. */
593 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000594 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000595 else
596 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000597 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000598 else if (start_sym == file_input) {
599 /* This looks like an exec form so far. */
600 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000601 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000602 else
603 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000604 }
605 else {
606 /* This is a fragment, at best. */
607 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000608 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000609 }
Guido van Rossum47478871996-08-21 14:32:37 +0000610 }
Guido van Rossum47478871996-08-21 14:32:37 +0000611 /* Make sure we throw an exception on all errors. We should never
612 * get this, but we'd do well to be sure something is done.
613 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000614 if (st == NULL && !PyErr_Occurred())
615 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000616
Fred Drakec2683dd2001-07-17 19:32:05 +0000617 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000618}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000619
620
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000621/* node* build_node_children()
622 *
623 * Iterate across the children of the current non-terminal node and build
624 * their structures. If successful, return the root of this portion of
625 * the tree, otherwise, 0. Any required exception will be specified already,
626 * and no memory will have been deallocated.
627 *
628 */
629static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000630build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000631{
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000632 int len = PyObject_Size(tuple);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000633 int i, err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000634
635 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000636 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000637 PyObject* elem = PySequence_GetItem(tuple, i);
638 int ok = elem != NULL;
639 long type = 0;
640 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000641
Fred Drakeff9ea482000-04-19 13:54:15 +0000642 if (ok)
643 ok = PySequence_Check(elem);
644 if (ok) {
645 PyObject *temp = PySequence_GetItem(elem, 0);
646 if (temp == NULL)
647 ok = 0;
648 else {
649 ok = PyInt_Check(temp);
650 if (ok)
651 type = PyInt_AS_LONG(temp);
652 Py_DECREF(temp);
653 }
654 }
655 if (!ok) {
656 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000657 Py_BuildValue("os", elem,
Fred Drakeff9ea482000-04-19 13:54:15 +0000658 "Illegal node construct."));
659 Py_XDECREF(elem);
660 return (0);
661 }
662 if (ISTERMINAL(type)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000663 int len = PyObject_Size(elem);
664 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000665
Fred Drake0ac9b072000-09-12 21:58:06 +0000666 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000667 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000668 return 0;
669 }
670 temp = PySequence_GetItem(elem, 1);
671 if (temp == NULL)
672 return 0;
673 if (!PyString_Check(temp)) {
674 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000675 "second item in terminal node must be a string,"
676 " found %s",
Fred Drake0ac9b072000-09-12 21:58:06 +0000677 ((PyTypeObject*)PyObject_Type(temp))->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000678 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000679 return 0;
680 }
681 if (len == 3) {
682 PyObject *o = PySequence_GetItem(elem, 2);
683 if (o != NULL) {
684 if (PyInt_Check(o))
685 *line_num = PyInt_AS_LONG(o);
686 else {
687 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000688 "third item in terminal node must be an"
689 " integer, found %s",
Fred Drake0ac9b072000-09-12 21:58:06 +0000690 ((PyTypeObject*)PyObject_Type(temp))->tp_name);
691 Py_DECREF(o);
692 Py_DECREF(temp);
693 return 0;
694 }
695 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000696 }
697 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000698 len = PyString_GET_SIZE(temp) + 1;
699 strn = (char *)PyMem_MALLOC(len);
700 if (strn != NULL)
701 (void) memcpy(strn, PyString_AS_STRING(temp), len);
702 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000703 }
704 else if (!ISNONTERMINAL(type)) {
705 /*
706 * It has to be one or the other; this is an error.
707 * Throw an exception.
708 */
709 PyErr_SetObject(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000710 Py_BuildValue("os", elem, "unknown node type."));
Fred Drakeff9ea482000-04-19 13:54:15 +0000711 Py_XDECREF(elem);
712 return (0);
713 }
Fred Drake8b55b2d2001-12-05 22:10:44 +0000714 err = PyNode_AddChild(root, type, strn, *line_num);
715 if (err == E_NOMEM) {
716 PyMem_DEL(strn);
717 return (node *) PyErr_NoMemory();
718 }
719 if (err == E_OVERFLOW) {
720 PyMem_DEL(strn);
721 PyErr_SetString(PyExc_ValueError,
722 "unsupported number of child nodes");
723 return NULL;
724 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000725
Fred Drakeff9ea482000-04-19 13:54:15 +0000726 if (ISNONTERMINAL(type)) {
727 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000728
Fred Drakeff9ea482000-04-19 13:54:15 +0000729 if (new_child != build_node_children(elem, new_child, line_num)) {
730 Py_XDECREF(elem);
731 return (0);
732 }
733 }
734 else if (type == NEWLINE) { /* It's true: we increment the */
735 ++(*line_num); /* line number *after* the newline! */
736 }
737 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000738 }
739 return (root);
Fred Drakeff9ea482000-04-19 13:54:15 +0000740}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000741
742
743static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000744build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000745{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000746 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000747 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000748 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000749
Guido van Rossum47478871996-08-21 14:32:37 +0000750 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000751 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000752 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000753 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000754 /*
755 * The tuple is simple, but it doesn't start with a start symbol.
756 * Throw an exception now and be done with it.
757 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000758 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000759 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000760 PyErr_SetObject(parser_error, tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000761 }
762 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000763 /*
764 * Not efficient, but that can be handled later.
765 */
766 int line_num = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000767
Fred Drakeff9ea482000-04-19 13:54:15 +0000768 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000769 if (res != NULL) {
770 if (res != build_node_children(tuple, res, &line_num)) {
771 PyNode_Free(res);
772 res = NULL;
773 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000774 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000775 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000776 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000777 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000778 * NONTERMINAL, we can't use it. Not sure the implementation
779 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000780 */
781 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000782 Py_BuildValue("os", tuple,
Fred Drakeff9ea482000-04-19 13:54:15 +0000783 "Illegal component tuple."));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000784
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000785 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000786}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000787
788
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000789/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000790 * Validation routines used within the validation section:
791 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000792static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000793
Fred Drakeff9ea482000-04-19 13:54:15 +0000794#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
795#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
796#define validate_colon(ch) validate_terminal(ch, COLON, ":")
797#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
798#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
799#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
800#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
801#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
802#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
803#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
804#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
805#define validate_star(ch) validate_terminal(ch, STAR, "*")
806#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
807#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
808#define validate_dot(ch) validate_terminal(ch, DOT, ".")
809#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000810
Fred Drake0ac9b072000-09-12 21:58:06 +0000811#define VALIDATER(n) static int validate_##n(node *tree)
812
Fred Drakeff9ea482000-04-19 13:54:15 +0000813VALIDATER(node); VALIDATER(small_stmt);
814VALIDATER(class); VALIDATER(node);
815VALIDATER(parameters); VALIDATER(suite);
816VALIDATER(testlist); VALIDATER(varargslist);
817VALIDATER(fpdef); VALIDATER(fplist);
818VALIDATER(stmt); VALIDATER(simple_stmt);
819VALIDATER(expr_stmt); VALIDATER(power);
820VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000821VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000822VALIDATER(raise_stmt); VALIDATER(import_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000823VALIDATER(global_stmt); VALIDATER(list_if);
824VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000825VALIDATER(exec_stmt); VALIDATER(compound_stmt);
826VALIDATER(while); VALIDATER(for);
827VALIDATER(try); VALIDATER(except_clause);
828VALIDATER(test); VALIDATER(and_test);
829VALIDATER(not_test); VALIDATER(comparison);
830VALIDATER(comp_op); VALIDATER(expr);
831VALIDATER(xor_expr); VALIDATER(and_expr);
832VALIDATER(shift_expr); VALIDATER(arith_expr);
833VALIDATER(term); VALIDATER(factor);
834VALIDATER(atom); VALIDATER(lambdef);
835VALIDATER(trailer); VALIDATER(subscript);
836VALIDATER(subscriptlist); VALIDATER(sliceop);
837VALIDATER(exprlist); VALIDATER(dictmaker);
838VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000839VALIDATER(listmaker); VALIDATER(yield_stmt);
Guido van Rossum2d3b9862002-05-24 15:47:06 +0000840VALIDATER(testlist1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000841
Fred Drake0ac9b072000-09-12 21:58:06 +0000842#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000843
Fred Drakeff9ea482000-04-19 13:54:15 +0000844#define is_even(n) (((n) & 1) == 0)
845#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000846
847
848static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000849validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000850{
Fred Drake0ac9b072000-09-12 21:58:06 +0000851 if (TYPE(n) != t) {
852 PyErr_Format(parser_error, "Expected node type %d, got %d.",
853 t, TYPE(n));
854 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000855 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000856 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000857}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000858
859
Fred Drakee7ab64e2000-04-25 04:14:46 +0000860/* Verifies that the number of child nodes is exactly 'num', raising
861 * an exception if it isn't. The exception message does not indicate
862 * the exact number of nodes, allowing this to be used to raise the
863 * "right" exception when the wrong number of nodes is present in a
864 * specific variant of a statement's syntax. This is commonly used
865 * in that fashion.
866 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000867static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000868validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000869{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000870 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000871 PyErr_Format(parser_error,
872 "Illegal number of children for %s node.", name);
873 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000874 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000875 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000876}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000877
878
879static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000880validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000881{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000882 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000883 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000884
885 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000886 PyErr_Format(parser_error,
887 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000888 }
889 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000890}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000891
892
Guido van Rossum47478871996-08-21 14:32:37 +0000893/* X (',' X) [',']
894 */
895static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000896validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000897 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000898{
899 int nch = NCH(tree);
900 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000901 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000902
903 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000904 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000905 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000906 if (is_even(nch))
907 res = validate_comma(CHILD(tree, --nch));
908 if (res && nch > 1) {
909 int pos = 1;
910 for ( ; res && pos < nch; pos += 2)
911 res = (validate_comma(CHILD(tree, pos))
912 && vfunc(CHILD(tree, pos + 1)));
913 }
Guido van Rossum47478871996-08-21 14:32:37 +0000914 }
915 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000916}
Guido van Rossum47478871996-08-21 14:32:37 +0000917
918
Fred Drakecff283c2000-08-21 22:24:43 +0000919/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000920 *
921 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000922 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000923 */
Guido van Rossum47478871996-08-21 14:32:37 +0000924static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000925validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000926{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000927 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000928 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000929
Guido van Rossum3d602e31996-07-21 02:33:56 +0000930 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000931 res = (validate_name(CHILD(tree, 0), "class")
932 && validate_ntype(CHILD(tree, 1), NAME)
933 && validate_colon(CHILD(tree, nch - 2))
934 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000935 }
936 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000937 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000938 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000939 res = (validate_lparen(CHILD(tree, 2))
940 && validate_testlist(CHILD(tree, 3))
941 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000942 }
943 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000944}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000945
946
Guido van Rossum3d602e31996-07-21 02:33:56 +0000947/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +0000948 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +0000949 */
Guido van Rossum47478871996-08-21 14:32:37 +0000950static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000951validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000952{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000953 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000954 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +0000955 && (nch >= 4)
956 && validate_name(CHILD(tree, 0), "if")
957 && validate_test(CHILD(tree, 1))
958 && validate_colon(CHILD(tree, 2))
959 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000960
961 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000962 /* ... 'else' ':' suite */
963 res = (validate_name(CHILD(tree, nch - 3), "else")
964 && validate_colon(CHILD(tree, nch - 2))
965 && validate_suite(CHILD(tree, nch - 1)));
966 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000967 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000968 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000969 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000970 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +0000971 /* Will catch the case for nch < 4 */
972 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000973 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000974 /* ... ('elif' test ':' suite)+ ... */
975 int j = 4;
976 while ((j < nch) && res) {
977 res = (validate_name(CHILD(tree, j), "elif")
978 && validate_colon(CHILD(tree, j + 2))
979 && validate_test(CHILD(tree, j + 1))
980 && validate_suite(CHILD(tree, j + 3)));
981 j += 4;
982 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000983 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000984 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000985}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000986
987
Guido van Rossum3d602e31996-07-21 02:33:56 +0000988/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +0000989 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +0000990 *
991 */
Guido van Rossum47478871996-08-21 14:32:37 +0000992static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000993validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000994{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000995 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000996 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000997
Guido van Rossum3d602e31996-07-21 02:33:56 +0000998 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000999 res = (validate_lparen(CHILD(tree, 0))
1000 && validate_rparen(CHILD(tree, nch - 1)));
1001 if (res && (nch == 3))
1002 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001003 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001004 else {
1005 (void) validate_numnodes(tree, 2, "parameters");
1006 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001007 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001008}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001009
1010
Fred Drakecff283c2000-08-21 22:24:43 +00001011/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001012 *
1013 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001014 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001015 * | NEWLINE INDENT stmt+ DEDENT
1016 */
Guido van Rossum47478871996-08-21 14:32:37 +00001017static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001018validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001019{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001020 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001021 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001022
Guido van Rossum3d602e31996-07-21 02:33:56 +00001023 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001024 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001025 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001026 /* NEWLINE INDENT stmt+ DEDENT */
1027 res = (validate_newline(CHILD(tree, 0))
1028 && validate_indent(CHILD(tree, 1))
1029 && validate_stmt(CHILD(tree, 2))
1030 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001031
Fred Drakeff9ea482000-04-19 13:54:15 +00001032 if (res && (nch > 4)) {
1033 int i = 3;
1034 --nch; /* forget the DEDENT */
1035 for ( ; res && (i < nch); ++i)
1036 res = validate_stmt(CHILD(tree, i));
1037 }
1038 else if (nch < 4)
1039 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001040 }
1041 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001042}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001043
1044
Guido van Rossum47478871996-08-21 14:32:37 +00001045static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001046validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001047{
Guido van Rossum47478871996-08-21 14:32:37 +00001048 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001049 validate_test, "testlist"));
1050}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001051
1052
Guido van Rossum1c917072001-10-15 15:44:05 +00001053static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001054validate_testlist1(node *tree)
1055{
1056 return (validate_repeating_list(tree, testlist1,
1057 validate_test, "testlist1"));
1058}
1059
1060
1061static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001062validate_testlist_safe(node *tree)
1063{
1064 return (validate_repeating_list(tree, testlist_safe,
1065 validate_test, "testlist_safe"));
1066}
1067
1068
Fred Drakecff283c2000-08-21 22:24:43 +00001069/* '*' NAME [',' '**' NAME] | '**' NAME
1070 */
1071static int
1072validate_varargslist_trailer(node *tree, int start)
1073{
1074 int nch = NCH(tree);
1075 int res = 0;
1076 int sym;
1077
1078 if (nch <= start) {
1079 err_string("expected variable argument trailer for varargslist");
1080 return 0;
1081 }
1082 sym = TYPE(CHILD(tree, start));
1083 if (sym == STAR) {
1084 /*
1085 * ('*' NAME [',' '**' NAME]
1086 */
1087 if (nch-start == 2)
1088 res = validate_name(CHILD(tree, start+1), NULL);
1089 else if (nch-start == 5)
1090 res = (validate_name(CHILD(tree, start+1), NULL)
1091 && validate_comma(CHILD(tree, start+2))
1092 && validate_doublestar(CHILD(tree, start+3))
1093 && validate_name(CHILD(tree, start+4), NULL));
1094 }
1095 else if (sym == DOUBLESTAR) {
1096 /*
1097 * '**' NAME
1098 */
1099 if (nch-start == 2)
1100 res = validate_name(CHILD(tree, start+1), NULL);
1101 }
1102 if (!res)
1103 err_string("illegal variable argument trailer for varargslist");
1104 return res;
1105}
1106
1107
1108/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001109 *
1110 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001111 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001112 * ('*' NAME [',' '**' NAME]
1113 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001114 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1115 *
1116 */
Guido van Rossum47478871996-08-21 14:32:37 +00001117static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001118validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001119{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001120 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001121 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001122 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001123
Fred Drakeb6429a22000-12-11 22:08:27 +00001124 if (!res)
1125 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001126 if (nch < 1) {
1127 err_string("varargslist missing child nodes");
1128 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001129 }
Fred Drakecff283c2000-08-21 22:24:43 +00001130 sym = TYPE(CHILD(tree, 0));
1131 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001132 /* whole thing matches:
1133 * '*' NAME [',' '**' NAME] | '**' NAME
1134 */
Fred Drakecff283c2000-08-21 22:24:43 +00001135 res = validate_varargslist_trailer(tree, 0);
1136 else if (sym == fpdef) {
1137 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001138
Fred Drakecff283c2000-08-21 22:24:43 +00001139 sym = TYPE(CHILD(tree, nch-1));
1140 if (sym == NAME) {
1141 /*
1142 * (fpdef ['=' test] ',')+
1143 * ('*' NAME [',' '**' NAME]
1144 * | '**' NAME)
1145 */
1146 /* skip over (fpdef ['=' test] ',')+ */
1147 while (res && (i+2 <= nch)) {
1148 res = validate_fpdef(CHILD(tree, i));
1149 ++i;
1150 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1151 res = (validate_equal(CHILD(tree, i))
1152 && validate_test(CHILD(tree, i+1)));
1153 if (res)
1154 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001155 }
Fred Drakecff283c2000-08-21 22:24:43 +00001156 if (res && i < nch) {
1157 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001158 ++i;
1159 if (res && i < nch
1160 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1161 || TYPE(CHILD(tree, i)) == STAR))
1162 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001163 }
1164 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001165 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1166 * i --^^^
1167 */
Fred Drakecff283c2000-08-21 22:24:43 +00001168 if (res)
1169 res = validate_varargslist_trailer(tree, i);
1170 }
1171 else {
1172 /*
1173 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1174 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001175 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001176 if (sym == COMMA) {
1177 res = validate_comma(CHILD(tree, nch-1));
1178 if (!res)
1179 return 0;
1180 --nch;
1181 }
1182 /*
1183 * fpdef ['=' test] (',' fpdef ['=' test])*
1184 */
1185 res = validate_fpdef(CHILD(tree, 0));
1186 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001187 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1188 res = (validate_equal(CHILD(tree, i))
1189 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001190 i += 2;
1191 }
1192 /*
1193 * ... (',' fpdef ['=' test])*
1194 * i ---^^^
1195 */
1196 while (res && (nch - i) >= 2) {
1197 res = (validate_comma(CHILD(tree, i))
1198 && validate_fpdef(CHILD(tree, i+1)));
1199 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001200 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1201 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001202 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001203 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001204 }
1205 }
1206 if (res && nch - i != 0) {
1207 res = 0;
1208 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001209 }
1210 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001211 }
Fred Drakecff283c2000-08-21 22:24:43 +00001212 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001213}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001214
1215
Fred Drakecff283c2000-08-21 22:24:43 +00001216/* list_iter: list_for | list_if
1217 */
1218static int
1219validate_list_iter(node *tree)
1220{
1221 int res = (validate_ntype(tree, list_iter)
1222 && validate_numnodes(tree, 1, "list_iter"));
1223 if (res && TYPE(CHILD(tree, 0)) == list_for)
1224 res = validate_list_for(CHILD(tree, 0));
1225 else
1226 res = validate_list_if(CHILD(tree, 0));
1227
1228 return res;
1229}
1230
1231/* list_for: 'for' exprlist 'in' testlist [list_iter]
1232 */
1233static int
1234validate_list_for(node *tree)
1235{
1236 int nch = NCH(tree);
1237 int res;
1238
1239 if (nch == 5)
1240 res = validate_list_iter(CHILD(tree, 4));
1241 else
1242 res = validate_numnodes(tree, 4, "list_for");
1243
1244 if (res)
1245 res = (validate_name(CHILD(tree, 0), "for")
1246 && validate_exprlist(CHILD(tree, 1))
1247 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001248 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001249
1250 return res;
1251}
1252
1253/* list_if: 'if' test [list_iter]
1254 */
1255static int
1256validate_list_if(node *tree)
1257{
1258 int nch = NCH(tree);
1259 int res;
1260
1261 if (nch == 3)
1262 res = validate_list_iter(CHILD(tree, 2));
1263 else
1264 res = validate_numnodes(tree, 2, "list_if");
1265
1266 if (res)
1267 res = (validate_name(CHILD(tree, 0), "if")
1268 && validate_test(CHILD(tree, 1)));
1269
1270 return res;
1271}
1272
1273
1274/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001275 *
1276 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001277 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001278 * | '(' fplist ')'
1279 */
Guido van Rossum47478871996-08-21 14:32:37 +00001280static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001281validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001282{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001283 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001284 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001285
Guido van Rossum3d602e31996-07-21 02:33:56 +00001286 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001287 if (nch == 1)
1288 res = validate_ntype(CHILD(tree, 0), NAME);
1289 else if (nch == 3)
1290 res = (validate_lparen(CHILD(tree, 0))
1291 && validate_fplist(CHILD(tree, 1))
1292 && validate_rparen(CHILD(tree, 2)));
1293 else
1294 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001295 }
1296 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001297}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001298
1299
Guido van Rossum47478871996-08-21 14:32:37 +00001300static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001301validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001302{
Guido van Rossum47478871996-08-21 14:32:37 +00001303 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001304 validate_fpdef, "fplist"));
1305}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001306
1307
Guido van Rossum3d602e31996-07-21 02:33:56 +00001308/* simple_stmt | compound_stmt
1309 *
1310 */
Guido van Rossum47478871996-08-21 14:32:37 +00001311static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001312validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001313{
1314 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001315 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001316
Guido van Rossum3d602e31996-07-21 02:33:56 +00001317 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001318 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001319
Fred Drakeff9ea482000-04-19 13:54:15 +00001320 if (TYPE(tree) == simple_stmt)
1321 res = validate_simple_stmt(tree);
1322 else
1323 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001324 }
1325 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001326}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001327
1328
Guido van Rossum3d602e31996-07-21 02:33:56 +00001329/* small_stmt (';' small_stmt)* [';'] NEWLINE
1330 *
1331 */
Guido van Rossum47478871996-08-21 14:32:37 +00001332static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001333validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001334{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001335 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001336 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001337 && (nch >= 2)
1338 && validate_small_stmt(CHILD(tree, 0))
1339 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001340
Guido van Rossum3d602e31996-07-21 02:33:56 +00001341 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001342 res = validate_numnodes(tree, 2, "simple_stmt");
1343 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001344 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001345 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001346 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001347 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001348
Fred Drakeff9ea482000-04-19 13:54:15 +00001349 for (i = 1; res && (i < nch); i += 2)
1350 res = (validate_semi(CHILD(tree, i))
1351 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001352 }
1353 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001354}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001355
1356
Guido van Rossum47478871996-08-21 14:32:37 +00001357static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001358validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001359{
1360 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001361 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001362
Fred Drake0ac9b072000-09-12 21:58:06 +00001363 if (res) {
1364 int ntype = TYPE(CHILD(tree, 0));
1365
1366 if ( (ntype == expr_stmt)
1367 || (ntype == print_stmt)
1368 || (ntype == del_stmt)
1369 || (ntype == pass_stmt)
1370 || (ntype == flow_stmt)
1371 || (ntype == import_stmt)
1372 || (ntype == global_stmt)
1373 || (ntype == assert_stmt)
1374 || (ntype == exec_stmt))
1375 res = validate_node(CHILD(tree, 0));
1376 else {
1377 res = 0;
1378 err_string("illegal small_stmt child type");
1379 }
1380 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001381 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001382 res = 0;
1383 PyErr_Format(parser_error,
1384 "Unrecognized child node of small_stmt: %d.",
1385 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001386 }
1387 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001388}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001389
1390
1391/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001392 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001393 */
Guido van Rossum47478871996-08-21 14:32:37 +00001394static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001395validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001396{
1397 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001398 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001399 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001400
1401 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001402 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001403
1404 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001405 ntype = TYPE(tree);
1406 if ( (ntype == if_stmt)
1407 || (ntype == while_stmt)
1408 || (ntype == for_stmt)
1409 || (ntype == try_stmt)
1410 || (ntype == funcdef)
1411 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001412 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001413 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001414 res = 0;
1415 PyErr_Format(parser_error,
1416 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001417 }
1418 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001419}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001420
1421
Guido van Rossum47478871996-08-21 14:32:37 +00001422static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001423validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001424{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001425 int j;
1426 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001427 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001428 && is_odd(nch)
1429 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001430
Fred Drake28f739a2000-08-25 22:42:40 +00001431 if (res && nch == 3
1432 && TYPE(CHILD(tree, 1)) == augassign) {
1433 res = (validate_numnodes(CHILD(tree, 1), 1, "augassign")
1434 && validate_testlist(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001435
Fred Drake28f739a2000-08-25 22:42:40 +00001436 if (res) {
1437 char *s = STR(CHILD(CHILD(tree, 1), 0));
1438
1439 res = (strcmp(s, "+=") == 0
1440 || strcmp(s, "-=") == 0
1441 || strcmp(s, "*=") == 0
1442 || strcmp(s, "/=") == 0
1443 || strcmp(s, "%=") == 0
1444 || strcmp(s, "&=") == 0
1445 || strcmp(s, "|=") == 0
1446 || strcmp(s, "^=") == 0
1447 || strcmp(s, "<<=") == 0
1448 || strcmp(s, ">>=") == 0
1449 || strcmp(s, "**=") == 0);
1450 if (!res)
1451 err_string("illegal augmmented assignment operator");
1452 }
1453 }
1454 else {
1455 for (j = 1; res && (j < nch); j += 2)
1456 res = (validate_equal(CHILD(tree, j))
1457 && validate_testlist(CHILD(tree, j + 1)));
1458 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001459 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001460}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001461
1462
Guido van Rossum3d602e31996-07-21 02:33:56 +00001463/* print_stmt:
1464 *
Fred Drakecff283c2000-08-21 22:24:43 +00001465 * 'print' ( [ test (',' test)* [','] ]
1466 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001467 */
Guido van Rossum47478871996-08-21 14:32:37 +00001468static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001469validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001470{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001471 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001472 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001473 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001474 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001475
Fred Drakecff283c2000-08-21 22:24:43 +00001476 if (res && nch > 1) {
1477 int sym = TYPE(CHILD(tree, 1));
1478 int i = 1;
1479 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001480
Fred Drakecff283c2000-08-21 22:24:43 +00001481 if (sym == test)
1482 res = validate_test(CHILD(tree, i++));
1483 else {
1484 if (nch < 3)
1485 res = validate_numnodes(tree, 3, "print_stmt");
1486 else {
1487 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1488 && validate_test(CHILD(tree, i+1)));
1489 i += 2;
1490 allow_trailing_comma = 0;
1491 }
1492 }
1493 if (res) {
1494 /* ... (',' test)* [','] */
1495 while (res && i+2 <= nch) {
1496 res = (validate_comma(CHILD(tree, i))
1497 && validate_test(CHILD(tree, i+1)));
1498 allow_trailing_comma = 1;
1499 i += 2;
1500 }
1501 if (res && !allow_trailing_comma)
1502 res = validate_numnodes(tree, i, "print_stmt");
1503 else if (res && i < nch)
1504 res = validate_comma(CHILD(tree, i));
1505 }
1506 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001507 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001508}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001509
1510
Guido van Rossum47478871996-08-21 14:32:37 +00001511static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001512validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001513{
1514 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001515 && validate_name(CHILD(tree, 0), "del")
1516 && validate_exprlist(CHILD(tree, 1)));
1517}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001518
1519
Guido van Rossum47478871996-08-21 14:32:37 +00001520static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001521validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001522{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001523 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001524 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001525 && ((nch == 1) || (nch == 2))
1526 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001527
Guido van Rossum3d602e31996-07-21 02:33:56 +00001528 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001529 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001530
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001531 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001532}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001533
1534
Guido van Rossum47478871996-08-21 14:32:37 +00001535static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001536validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001537{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001538 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001539 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001540 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001541
Guido van Rossum3d602e31996-07-21 02:33:56 +00001542 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001543 res = validate_name(CHILD(tree, 0), "raise");
1544 if (res && (nch >= 2))
1545 res = validate_test(CHILD(tree, 1));
1546 if (res && nch > 2) {
1547 res = (validate_comma(CHILD(tree, 2))
1548 && validate_test(CHILD(tree, 3)));
1549 if (res && (nch > 4))
1550 res = (validate_comma(CHILD(tree, 4))
1551 && validate_test(CHILD(tree, 5)));
1552 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001553 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001554 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001555 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001556 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001557 res = (validate_comma(CHILD(tree, 2))
1558 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001559
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001560 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001561}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001562
1563
Fred Drake02126f22001-07-17 02:59:15 +00001564/* yield_stmt: 'yield' testlist
1565 */
1566static int
1567validate_yield_stmt(node *tree)
1568{
1569 return (validate_ntype(tree, yield_stmt)
1570 && validate_numnodes(tree, 2, "yield_stmt")
1571 && validate_name(CHILD(tree, 0), "yield")
1572 && validate_testlist(CHILD(tree, 1)));
1573}
1574
1575
Fred Drakecff283c2000-08-21 22:24:43 +00001576static int
1577validate_import_as_name(node *tree)
1578{
1579 int nch = NCH(tree);
1580 int ok = validate_ntype(tree, import_as_name);
1581
1582 if (ok) {
1583 if (nch == 1)
1584 ok = validate_name(CHILD(tree, 0), NULL);
1585 else if (nch == 3)
1586 ok = (validate_name(CHILD(tree, 0), NULL)
1587 && validate_name(CHILD(tree, 1), "as")
1588 && validate_name(CHILD(tree, 2), NULL));
1589 else
1590 ok = validate_numnodes(tree, 3, "import_as_name");
1591 }
1592 return ok;
1593}
1594
1595
Fred Drake71137082001-01-07 05:59:59 +00001596/* dotted_name: NAME ("." NAME)*
1597 */
1598static int
1599validate_dotted_name(node *tree)
1600{
1601 int nch = NCH(tree);
1602 int res = (validate_ntype(tree, dotted_name)
1603 && is_odd(nch)
1604 && validate_name(CHILD(tree, 0), NULL));
1605 int i;
1606
1607 for (i = 1; res && (i < nch); i += 2) {
1608 res = (validate_dot(CHILD(tree, i))
1609 && validate_name(CHILD(tree, i+1), NULL));
1610 }
1611 return res;
1612}
1613
1614
Fred Drakecff283c2000-08-21 22:24:43 +00001615/* dotted_as_name: dotted_name [NAME NAME]
1616 */
1617static int
1618validate_dotted_as_name(node *tree)
1619{
1620 int nch = NCH(tree);
1621 int res = validate_ntype(tree, dotted_as_name);
1622
1623 if (res) {
1624 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001625 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001626 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001627 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001628 && validate_name(CHILD(tree, 1), "as")
1629 && validate_name(CHILD(tree, 2), NULL));
1630 else {
1631 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001632 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001633 }
1634 }
1635 return res;
1636}
1637
1638
Guido van Rossum3d602e31996-07-21 02:33:56 +00001639/* import_stmt:
1640 *
Fred Drakecff283c2000-08-21 22:24:43 +00001641 * 'import' dotted_as_name (',' dotted_as_name)*
1642 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001643 */
Guido van Rossum47478871996-08-21 14:32:37 +00001644static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001645validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001646{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001647 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001648 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001649 && (nch >= 2) && is_even(nch)
Fred Drakecff283c2000-08-21 22:24:43 +00001650 && validate_ntype(CHILD(tree, 0), NAME));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001651
1652 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001653 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001654
Fred Drakecff283c2000-08-21 22:24:43 +00001655 res = validate_dotted_as_name(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00001656 for (j = 2; res && (j < nch); j += 2)
1657 res = (validate_comma(CHILD(tree, j))
Fred Drake71137082001-01-07 05:59:59 +00001658 && validate_dotted_as_name(CHILD(tree, j + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001659 }
Fred Drakecff283c2000-08-21 22:24:43 +00001660 else if (res && (res = validate_name(CHILD(tree, 0), "from"))) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001661 res = ((nch >= 4) && is_even(nch)
Fred Drake71137082001-01-07 05:59:59 +00001662 && validate_dotted_name(CHILD(tree, 1))
1663 && validate_name(CHILD(tree, 2), "import"));
Fred Drakeff9ea482000-04-19 13:54:15 +00001664 if (nch == 4) {
Fred Drakecff283c2000-08-21 22:24:43 +00001665 if (TYPE(CHILD(tree, 3)) == import_as_name)
1666 res = validate_import_as_name(CHILD(tree, 3));
1667 else
1668 res = validate_star(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001669 }
1670 else {
Fred Drakecff283c2000-08-21 22:24:43 +00001671 /* 'from' dotted_name 'import' import_as_name
1672 * (',' import_as_name)+
1673 */
Fred Drakeff9ea482000-04-19 13:54:15 +00001674 int j;
Fred Drakecff283c2000-08-21 22:24:43 +00001675 res = validate_import_as_name(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001676 for (j = 4; res && (j < nch); j += 2)
1677 res = (validate_comma(CHILD(tree, j))
Fred Drakecff283c2000-08-21 22:24:43 +00001678 && validate_import_as_name(CHILD(tree, j + 1)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001679 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001680 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001681 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001682 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001683
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001684 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001685}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001686
1687
Guido van Rossum47478871996-08-21 14:32:37 +00001688static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001689validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001690{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001691 int j;
1692 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001693 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001694 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001695
Guido van Rossum3d602e31996-07-21 02:33:56 +00001696 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001697 res = (validate_name(CHILD(tree, 0), "global")
1698 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001699 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001700 res = (validate_comma(CHILD(tree, j))
1701 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001702
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001703 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001704}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001705
1706
Guido van Rossum3d602e31996-07-21 02:33:56 +00001707/* exec_stmt:
1708 *
1709 * 'exec' expr ['in' test [',' test]]
1710 */
Guido van Rossum47478871996-08-21 14:32:37 +00001711static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001712validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001713{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001714 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001715 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001716 && ((nch == 2) || (nch == 4) || (nch == 6))
1717 && validate_name(CHILD(tree, 0), "exec")
1718 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001719
Guido van Rossum3d602e31996-07-21 02:33:56 +00001720 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001721 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001722 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001723 res = (validate_name(CHILD(tree, 2), "in")
1724 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001725 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001726 res = (validate_comma(CHILD(tree, 4))
1727 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001728
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001729 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001730}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001731
1732
Guido van Rossum925e5471997-04-02 05:32:13 +00001733/* assert_stmt:
1734 *
1735 * 'assert' test [',' test]
1736 */
1737static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001738validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001739{
1740 int nch = NCH(tree);
1741 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001742 && ((nch == 2) || (nch == 4))
1743 && (validate_name(CHILD(tree, 0), "__assert__") ||
1744 validate_name(CHILD(tree, 0), "assert"))
1745 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001746
1747 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001748 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001749 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001750 res = (validate_comma(CHILD(tree, 2))
1751 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001752
1753 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001754}
Guido van Rossum925e5471997-04-02 05:32:13 +00001755
1756
Guido van Rossum47478871996-08-21 14:32:37 +00001757static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001758validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001759{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001760 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001761 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001762 && ((nch == 4) || (nch == 7))
1763 && validate_name(CHILD(tree, 0), "while")
1764 && validate_test(CHILD(tree, 1))
1765 && validate_colon(CHILD(tree, 2))
1766 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001767
Guido van Rossum3d602e31996-07-21 02:33:56 +00001768 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001769 res = (validate_name(CHILD(tree, 4), "else")
1770 && validate_colon(CHILD(tree, 5))
1771 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001772
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001773 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001774}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001775
1776
Guido van Rossum47478871996-08-21 14:32:37 +00001777static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001778validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001779{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001780 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001781 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001782 && ((nch == 6) || (nch == 9))
1783 && validate_name(CHILD(tree, 0), "for")
1784 && validate_exprlist(CHILD(tree, 1))
1785 && validate_name(CHILD(tree, 2), "in")
1786 && validate_testlist(CHILD(tree, 3))
1787 && validate_colon(CHILD(tree, 4))
1788 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001789
Guido van Rossum3d602e31996-07-21 02:33:56 +00001790 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001791 res = (validate_name(CHILD(tree, 6), "else")
1792 && validate_colon(CHILD(tree, 7))
1793 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001794
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001795 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001796}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001797
1798
Guido van Rossum3d602e31996-07-21 02:33:56 +00001799/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001800 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001801 * | 'try' ':' suite 'finally' ':' suite
1802 *
1803 */
Guido van Rossum47478871996-08-21 14:32:37 +00001804static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001805validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001806{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001807 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001808 int pos = 3;
1809 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001810 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001811
1812 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001813 res = (validate_name(CHILD(tree, 0), "try")
1814 && validate_colon(CHILD(tree, 1))
1815 && validate_suite(CHILD(tree, 2))
1816 && validate_colon(CHILD(tree, nch - 2))
1817 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001818 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001819 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001820 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1821 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001822
1823 PyErr_Format(parser_error,
1824 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001825 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001826 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001827 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001828 res = (validate_except_clause(CHILD(tree, pos))
1829 && validate_colon(CHILD(tree, pos + 1))
1830 && validate_suite(CHILD(tree, pos + 2)));
1831 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001832 }
1833 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001834 res = validate_ntype(CHILD(tree, pos), NAME);
1835 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1836 res = (validate_numnodes(tree, 6, "try/finally")
1837 && validate_colon(CHILD(tree, 4))
1838 && validate_suite(CHILD(tree, 5)));
1839 else if (res) {
1840 if (nch == (pos + 3)) {
1841 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1842 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1843 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001844 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001845 }
1846 else if (nch == (pos + 6)) {
1847 res = (validate_name(CHILD(tree, pos), "except")
1848 && validate_colon(CHILD(tree, pos + 1))
1849 && validate_suite(CHILD(tree, pos + 2))
1850 && validate_name(CHILD(tree, pos + 3), "else"));
1851 }
1852 else
1853 res = validate_numnodes(tree, pos + 3, "try/except");
1854 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001855 }
1856 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001857}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001858
1859
Guido van Rossum47478871996-08-21 14:32:37 +00001860static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001861validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001862{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001863 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001864 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001865 && ((nch == 1) || (nch == 2) || (nch == 4))
1866 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001867
Guido van Rossum3d602e31996-07-21 02:33:56 +00001868 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001869 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001870 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001871 res = (validate_comma(CHILD(tree, 2))
1872 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001873
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001874 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001875}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001876
1877
Guido van Rossum47478871996-08-21 14:32:37 +00001878static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001879validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001880{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001881 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001882 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001883
Guido van Rossum3d602e31996-07-21 02:33:56 +00001884 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001885 res = ((nch == 1)
1886 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001887 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001888 int pos;
1889 res = validate_and_test(CHILD(tree, 0));
1890 for (pos = 1; res && (pos < nch); pos += 2)
1891 res = (validate_name(CHILD(tree, pos), "or")
1892 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001893 }
1894 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001895}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001896
1897
Guido van Rossum47478871996-08-21 14:32:37 +00001898static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001899validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001900{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001901 int pos;
1902 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001903 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001904 && is_odd(nch)
1905 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001906
Guido van Rossum3d602e31996-07-21 02:33:56 +00001907 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001908 res = (validate_name(CHILD(tree, pos), "and")
1909 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001910
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001911 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001912}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001913
1914
Guido van Rossum47478871996-08-21 14:32:37 +00001915static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001916validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001917{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001918 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001919 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001920
Guido van Rossum3d602e31996-07-21 02:33:56 +00001921 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001922 if (nch == 2)
1923 res = (validate_name(CHILD(tree, 0), "not")
1924 && validate_not_test(CHILD(tree, 1)));
1925 else if (nch == 1)
1926 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001927 }
1928 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001929}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001930
1931
Guido van Rossum47478871996-08-21 14:32:37 +00001932static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001933validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001934{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001935 int pos;
1936 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001937 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001938 && is_odd(nch)
1939 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001940
Guido van Rossum3d602e31996-07-21 02:33:56 +00001941 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001942 res = (validate_comp_op(CHILD(tree, pos))
1943 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001944
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001945 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001946}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001947
1948
Guido van Rossum47478871996-08-21 14:32:37 +00001949static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001950validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001951{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001952 int res = 0;
1953 int nch = NCH(tree);
1954
Guido van Rossum3d602e31996-07-21 02:33:56 +00001955 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001956 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001957 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001958 /*
1959 * Only child will be a terminal with a well-defined symbolic name
1960 * or a NAME with a string of either 'is' or 'in'
1961 */
1962 tree = CHILD(tree, 0);
1963 switch (TYPE(tree)) {
1964 case LESS:
1965 case GREATER:
1966 case EQEQUAL:
1967 case EQUAL:
1968 case LESSEQUAL:
1969 case GREATEREQUAL:
1970 case NOTEQUAL:
1971 res = 1;
1972 break;
1973 case NAME:
1974 res = ((strcmp(STR(tree), "in") == 0)
1975 || (strcmp(STR(tree), "is") == 0));
1976 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001977 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00001978 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00001979 }
1980 break;
1981 default:
Fred Drake661ea262000-10-24 19:57:45 +00001982 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001983 break;
1984 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001985 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00001986 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001987 res = (validate_ntype(CHILD(tree, 0), NAME)
1988 && validate_ntype(CHILD(tree, 1), NAME)
1989 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
1990 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
1991 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
1992 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
1993 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001994 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001995 }
1996 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001997}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001998
1999
Guido van Rossum47478871996-08-21 14:32:37 +00002000static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002001validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002002{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002003 int j;
2004 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002005 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002006 && is_odd(nch)
2007 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002008
Guido van Rossum3d602e31996-07-21 02:33:56 +00002009 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002010 res = (validate_xor_expr(CHILD(tree, j))
2011 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002012
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002013 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002014}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002015
2016
Guido van Rossum47478871996-08-21 14:32:37 +00002017static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002018validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002019{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002020 int j;
2021 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002022 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002023 && is_odd(nch)
2024 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002025
Guido van Rossum3d602e31996-07-21 02:33:56 +00002026 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002027 res = (validate_circumflex(CHILD(tree, j - 1))
2028 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002029
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002030 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002031}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002032
2033
Guido van Rossum47478871996-08-21 14:32:37 +00002034static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002035validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002036{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002037 int pos;
2038 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002039 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002040 && is_odd(nch)
2041 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002042
Guido van Rossum3d602e31996-07-21 02:33:56 +00002043 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002044 res = (validate_ampersand(CHILD(tree, pos))
2045 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002046
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002047 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002048}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002049
2050
2051static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002052validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002053 {
2054 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002055 int nch = NCH(tree);
2056 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002057 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002058
Guido van Rossum3d602e31996-07-21 02:33:56 +00002059 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002060 if (TYPE(CHILD(tree, pos)) != op1)
2061 res = validate_ntype(CHILD(tree, pos), op2);
2062 if (res)
2063 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002064 }
2065 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002066}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002067
2068
Guido van Rossum47478871996-08-21 14:32:37 +00002069static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002070validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002071{
2072 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002073 && validate_chain_two_ops(tree, validate_arith_expr,
2074 LEFTSHIFT, RIGHTSHIFT));
2075}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002076
2077
Guido van Rossum47478871996-08-21 14:32:37 +00002078static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002079validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002080{
2081 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002082 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2083}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002084
2085
Guido van Rossum47478871996-08-21 14:32:37 +00002086static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002087validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002088{
2089 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002090 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002091 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002092 && is_odd(nch)
2093 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002094
Guido van Rossum3d602e31996-07-21 02:33:56 +00002095 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002096 res = (((TYPE(CHILD(tree, pos)) == STAR)
2097 || (TYPE(CHILD(tree, pos)) == SLASH)
2098 || (TYPE(CHILD(tree, pos)) == PERCENT))
2099 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002100
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002101 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002102}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002103
2104
Guido van Rossum3d602e31996-07-21 02:33:56 +00002105/* factor:
2106 *
2107 * factor: ('+'|'-'|'~') factor | power
2108 */
Guido van Rossum47478871996-08-21 14:32:37 +00002109static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002110validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002111{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002112 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002113 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002114 && (((nch == 2)
2115 && ((TYPE(CHILD(tree, 0)) == PLUS)
2116 || (TYPE(CHILD(tree, 0)) == MINUS)
2117 || (TYPE(CHILD(tree, 0)) == TILDE))
2118 && validate_factor(CHILD(tree, 1)))
2119 || ((nch == 1)
2120 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002121 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002122}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002123
2124
Guido van Rossum3d602e31996-07-21 02:33:56 +00002125/* power:
2126 *
2127 * power: atom trailer* ('**' factor)*
2128 */
Guido van Rossum47478871996-08-21 14:32:37 +00002129static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002130validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002131{
2132 int pos = 1;
2133 int nch = NCH(tree);
2134 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002135 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002136
2137 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002138 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002139 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002140 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002141 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002142 return (0);
2143 }
2144 for ( ; res && (pos < (nch - 1)); pos += 2)
2145 res = (validate_doublestar(CHILD(tree, pos))
2146 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002147 }
2148 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002149}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002150
2151
Guido van Rossum47478871996-08-21 14:32:37 +00002152static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002153validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002154{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002155 int pos;
2156 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002157 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002158
Fred Drakecff283c2000-08-21 22:24:43 +00002159 if (res && nch < 1)
2160 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002161 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002162 switch (TYPE(CHILD(tree, 0))) {
2163 case LPAR:
2164 res = ((nch <= 3)
2165 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002166
Fred Drakeff9ea482000-04-19 13:54:15 +00002167 if (res && (nch == 3))
2168 res = validate_testlist(CHILD(tree, 1));
2169 break;
2170 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002171 if (nch == 2)
2172 res = validate_ntype(CHILD(tree, 1), RSQB);
2173 else if (nch == 3)
2174 res = (validate_listmaker(CHILD(tree, 1))
2175 && validate_ntype(CHILD(tree, 2), RSQB));
2176 else {
2177 res = 0;
2178 err_string("illegal list display atom");
2179 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002180 break;
2181 case LBRACE:
2182 res = ((nch <= 3)
2183 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002184
Fred Drakeff9ea482000-04-19 13:54:15 +00002185 if (res && (nch == 3))
2186 res = validate_dictmaker(CHILD(tree, 1));
2187 break;
2188 case BACKQUOTE:
2189 res = ((nch == 3)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002190 && validate_testlist1(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002191 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2192 break;
2193 case NAME:
2194 case NUMBER:
2195 res = (nch == 1);
2196 break;
2197 case STRING:
2198 for (pos = 1; res && (pos < nch); ++pos)
2199 res = validate_ntype(CHILD(tree, pos), STRING);
2200 break;
2201 default:
2202 res = 0;
2203 break;
2204 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002205 }
2206 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002207}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002208
2209
Fred Drake85bf3bb2000-08-23 15:35:26 +00002210/* listmaker:
2211 * test ( list_for | (',' test)* [','] )
2212 */
Fred Drakecff283c2000-08-21 22:24:43 +00002213static int
2214validate_listmaker(node *tree)
2215{
2216 int nch = NCH(tree);
2217 int ok = nch;
2218
2219 if (nch == 0)
2220 err_string("missing child nodes of listmaker");
2221 else
2222 ok = validate_test(CHILD(tree, 0));
2223
2224 /*
2225 * list_iter | (',' test)* [',']
2226 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002227 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2228 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002229 else {
2230 /* (',' test)* [','] */
2231 int i = 1;
2232 while (ok && nch - i >= 2) {
2233 ok = (validate_comma(CHILD(tree, i))
2234 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002235 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002236 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002237 if (ok && i == nch-1)
2238 ok = validate_comma(CHILD(tree, i));
2239 else if (i != nch) {
2240 ok = 0;
2241 err_string("illegal trailing nodes for listmaker");
2242 }
Fred Drakecff283c2000-08-21 22:24:43 +00002243 }
2244 return ok;
2245}
2246
2247
Guido van Rossum3d602e31996-07-21 02:33:56 +00002248/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002249 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002250 *
2251 */
Guido van Rossum47478871996-08-21 14:32:37 +00002252static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002253validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002254{
2255 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002256 && validate_numnodes(tree, 5, "funcdef")
2257 && validate_name(CHILD(tree, 0), "def")
2258 && validate_ntype(CHILD(tree, 1), NAME)
2259 && validate_colon(CHILD(tree, 3))
2260 && validate_parameters(CHILD(tree, 2))
2261 && validate_suite(CHILD(tree, 4)));
2262}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002263
2264
Guido van Rossum47478871996-08-21 14:32:37 +00002265static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002266validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002267{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002268 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002269 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002270 && ((nch == 3) || (nch == 4))
2271 && validate_name(CHILD(tree, 0), "lambda")
2272 && validate_colon(CHILD(tree, nch - 2))
2273 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002274
Guido van Rossum3d602e31996-07-21 02:33:56 +00002275 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002276 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002277 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002278 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002279
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002280 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002281}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002282
2283
Guido van Rossum3d602e31996-07-21 02:33:56 +00002284/* arglist:
2285 *
Fred Drakecff283c2000-08-21 22:24:43 +00002286 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002287 */
Guido van Rossum47478871996-08-21 14:32:37 +00002288static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002289validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002290{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002291 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002292 int i = 0;
2293 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002294
2295 if (nch <= 0)
2296 /* raise the right error from having an invalid number of children */
2297 return validate_numnodes(tree, nch + 1, "arglist");
2298
Fred Drakecff283c2000-08-21 22:24:43 +00002299 while (ok && nch-i >= 2) {
2300 /* skip leading (argument ',') */
2301 ok = (validate_argument(CHILD(tree, i))
2302 && validate_comma(CHILD(tree, i+1)));
2303 if (ok)
2304 i += 2;
2305 else
2306 PyErr_Clear();
2307 }
2308 ok = 1;
2309 if (nch-i > 0) {
2310 /*
2311 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002312 */
Fred Drakecff283c2000-08-21 22:24:43 +00002313 int sym = TYPE(CHILD(tree, i));
2314
2315 if (sym == argument) {
2316 ok = validate_argument(CHILD(tree, i));
2317 if (ok && i+1 != nch) {
2318 err_string("illegal arglist specification"
2319 " (extra stuff on end)");
2320 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002321 }
Fred Drakecff283c2000-08-21 22:24:43 +00002322 }
2323 else if (sym == STAR) {
2324 ok = validate_star(CHILD(tree, i));
2325 if (ok && (nch-i == 2))
2326 ok = validate_test(CHILD(tree, i+1));
2327 else if (ok && (nch-i == 5))
2328 ok = (validate_test(CHILD(tree, i+1))
2329 && validate_comma(CHILD(tree, i+2))
2330 && validate_doublestar(CHILD(tree, i+3))
2331 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002332 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002333 err_string("illegal use of '*' in arglist");
2334 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002335 }
2336 }
Fred Drakecff283c2000-08-21 22:24:43 +00002337 else if (sym == DOUBLESTAR) {
2338 if (nch-i == 2)
2339 ok = (validate_doublestar(CHILD(tree, i))
2340 && validate_test(CHILD(tree, i+1)));
2341 else {
2342 err_string("illegal use of '**' in arglist");
2343 ok = 0;
2344 }
2345 }
2346 else {
2347 err_string("illegal arglist specification");
2348 ok = 0;
2349 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002350 }
2351 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002352}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002353
2354
2355
2356/* argument:
2357 *
2358 * [test '='] test
2359 */
Guido van Rossum47478871996-08-21 14:32:37 +00002360static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002361validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002362{
2363 int nch = NCH(tree);
2364 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002365 && ((nch == 1) || (nch == 3))
2366 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002367
2368 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002369 res = (validate_equal(CHILD(tree, 1))
2370 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002371
2372 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002373}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002374
2375
2376
2377/* trailer:
2378 *
Guido van Rossum47478871996-08-21 14:32:37 +00002379 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002380 */
Guido van Rossum47478871996-08-21 14:32:37 +00002381static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002382validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002383{
2384 int nch = NCH(tree);
2385 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002386
2387 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002388 switch (TYPE(CHILD(tree, 0))) {
2389 case LPAR:
2390 res = validate_rparen(CHILD(tree, nch - 1));
2391 if (res && (nch == 3))
2392 res = validate_arglist(CHILD(tree, 1));
2393 break;
2394 case LSQB:
2395 res = (validate_numnodes(tree, 3, "trailer")
2396 && validate_subscriptlist(CHILD(tree, 1))
2397 && validate_ntype(CHILD(tree, 2), RSQB));
2398 break;
2399 case DOT:
2400 res = (validate_numnodes(tree, 2, "trailer")
2401 && validate_ntype(CHILD(tree, 1), NAME));
2402 break;
2403 default:
2404 res = 0;
2405 break;
2406 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002407 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002408 else {
2409 (void) validate_numnodes(tree, 2, "trailer");
2410 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002411 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002412}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002413
2414
Guido van Rossum47478871996-08-21 14:32:37 +00002415/* subscriptlist:
2416 *
2417 * subscript (',' subscript)* [',']
2418 */
2419static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002420validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002421{
2422 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002423 validate_subscript, "subscriptlist"));
2424}
Guido van Rossum47478871996-08-21 14:32:37 +00002425
2426
Guido van Rossum3d602e31996-07-21 02:33:56 +00002427/* subscript:
2428 *
Guido van Rossum47478871996-08-21 14:32:37 +00002429 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002430 */
Guido van Rossum47478871996-08-21 14:32:37 +00002431static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002432validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002433{
Guido van Rossum47478871996-08-21 14:32:37 +00002434 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002435 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002436 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002437
Guido van Rossum47478871996-08-21 14:32:37 +00002438 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002439 if (!PyErr_Occurred())
2440 err_string("invalid number of arguments for subscript node");
2441 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002442 }
Guido van Rossum47478871996-08-21 14:32:37 +00002443 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002444 /* take care of ('.' '.' '.') possibility */
2445 return (validate_numnodes(tree, 3, "subscript")
2446 && validate_dot(CHILD(tree, 0))
2447 && validate_dot(CHILD(tree, 1))
2448 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002449 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002450 if (TYPE(CHILD(tree, 0)) == test)
2451 res = validate_test(CHILD(tree, 0));
2452 else
2453 res = validate_colon(CHILD(tree, 0));
2454 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002455 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002456 /* Must be [test] ':' [test] [sliceop],
2457 * but at least one of the optional components will
2458 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002459 */
2460 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002461 res = validate_test(CHILD(tree, 0));
2462 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002463 }
2464 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002465 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002466 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002467 int rem = nch - ++offset;
2468 if (rem) {
2469 if (TYPE(CHILD(tree, offset)) == test) {
2470 res = validate_test(CHILD(tree, offset));
2471 ++offset;
2472 --rem;
2473 }
2474 if (res && rem)
2475 res = validate_sliceop(CHILD(tree, offset));
2476 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002477 }
2478 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002479}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002480
2481
Guido van Rossum47478871996-08-21 14:32:37 +00002482static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002483validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002484{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002485 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002486 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002487 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002488 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002489 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002490 }
Guido van Rossum47478871996-08-21 14:32:37 +00002491 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002492 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002493 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002494 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002495
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002496 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002497}
Guido van Rossum47478871996-08-21 14:32:37 +00002498
2499
2500static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002501validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002502{
2503 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002504 validate_expr, "exprlist"));
2505}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002506
2507
Guido van Rossum47478871996-08-21 14:32:37 +00002508static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002509validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002510{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002511 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002512 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002513 && (nch >= 3)
2514 && validate_test(CHILD(tree, 0))
2515 && validate_colon(CHILD(tree, 1))
2516 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002517
Guido van Rossum3d602e31996-07-21 02:33:56 +00002518 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002519 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002520 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002521 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002522
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002523 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002524 int pos = 3;
2525 /* ( ',' test ':' test )* */
2526 while (res && (pos < nch)) {
2527 res = (validate_comma(CHILD(tree, pos))
2528 && validate_test(CHILD(tree, pos + 1))
2529 && validate_colon(CHILD(tree, pos + 2))
2530 && validate_test(CHILD(tree, pos + 3)));
2531 pos += 4;
2532 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002533 }
2534 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002535}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002536
2537
Guido van Rossum47478871996-08-21 14:32:37 +00002538static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002539validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002540{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002541 int pos;
2542 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002543 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002544 && (nch >= 2)
2545 && validate_testlist(CHILD(tree, 0))
2546 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002547
Guido van Rossum3d602e31996-07-21 02:33:56 +00002548 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002549 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002550
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002551 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002552}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002553
2554
Guido van Rossum47478871996-08-21 14:32:37 +00002555static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002556validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002557{
Fred Drakeff9ea482000-04-19 13:54:15 +00002558 int nch = 0; /* num. children on current node */
2559 int res = 1; /* result value */
2560 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002561
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002562 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002563 nch = NCH(tree);
2564 next = 0;
2565 switch (TYPE(tree)) {
2566 /*
2567 * Definition nodes.
2568 */
2569 case funcdef:
2570 res = validate_funcdef(tree);
2571 break;
2572 case classdef:
2573 res = validate_class(tree);
2574 break;
2575 /*
2576 * "Trivial" parse tree nodes.
2577 * (Why did I call these trivial?)
2578 */
2579 case stmt:
2580 res = validate_stmt(tree);
2581 break;
2582 case small_stmt:
2583 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002584 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002585 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2586 */
2587 res = validate_small_stmt(tree);
2588 break;
2589 case flow_stmt:
2590 res = (validate_numnodes(tree, 1, "flow_stmt")
2591 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2592 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002593 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002594 || (TYPE(CHILD(tree, 0)) == return_stmt)
2595 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2596 if (res)
2597 next = CHILD(tree, 0);
2598 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002599 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002600 break;
Fred Drake02126f22001-07-17 02:59:15 +00002601 case yield_stmt:
2602 res = validate_yield_stmt(tree);
2603 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002604 /*
2605 * Compound statements.
2606 */
2607 case simple_stmt:
2608 res = validate_simple_stmt(tree);
2609 break;
2610 case compound_stmt:
2611 res = validate_compound_stmt(tree);
2612 break;
2613 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002614 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002615 */
2616 case expr_stmt:
2617 res = validate_expr_stmt(tree);
2618 break;
2619 case print_stmt:
2620 res = validate_print_stmt(tree);
2621 break;
2622 case del_stmt:
2623 res = validate_del_stmt(tree);
2624 break;
2625 case pass_stmt:
2626 res = (validate_numnodes(tree, 1, "pass")
2627 && validate_name(CHILD(tree, 0), "pass"));
2628 break;
2629 case break_stmt:
2630 res = (validate_numnodes(tree, 1, "break")
2631 && validate_name(CHILD(tree, 0), "break"));
2632 break;
2633 case continue_stmt:
2634 res = (validate_numnodes(tree, 1, "continue")
2635 && validate_name(CHILD(tree, 0), "continue"));
2636 break;
2637 case return_stmt:
2638 res = validate_return_stmt(tree);
2639 break;
2640 case raise_stmt:
2641 res = validate_raise_stmt(tree);
2642 break;
2643 case import_stmt:
2644 res = validate_import_stmt(tree);
2645 break;
2646 case global_stmt:
2647 res = validate_global_stmt(tree);
2648 break;
2649 case exec_stmt:
2650 res = validate_exec_stmt(tree);
2651 break;
2652 case assert_stmt:
2653 res = validate_assert_stmt(tree);
2654 break;
2655 case if_stmt:
2656 res = validate_if(tree);
2657 break;
2658 case while_stmt:
2659 res = validate_while(tree);
2660 break;
2661 case for_stmt:
2662 res = validate_for(tree);
2663 break;
2664 case try_stmt:
2665 res = validate_try(tree);
2666 break;
2667 case suite:
2668 res = validate_suite(tree);
2669 break;
2670 /*
2671 * Expression nodes.
2672 */
2673 case testlist:
2674 res = validate_testlist(tree);
2675 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002676 case testlist1:
2677 res = validate_testlist1(tree);
2678 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002679 case test:
2680 res = validate_test(tree);
2681 break;
2682 case and_test:
2683 res = validate_and_test(tree);
2684 break;
2685 case not_test:
2686 res = validate_not_test(tree);
2687 break;
2688 case comparison:
2689 res = validate_comparison(tree);
2690 break;
2691 case exprlist:
2692 res = validate_exprlist(tree);
2693 break;
2694 case comp_op:
2695 res = validate_comp_op(tree);
2696 break;
2697 case expr:
2698 res = validate_expr(tree);
2699 break;
2700 case xor_expr:
2701 res = validate_xor_expr(tree);
2702 break;
2703 case and_expr:
2704 res = validate_and_expr(tree);
2705 break;
2706 case shift_expr:
2707 res = validate_shift_expr(tree);
2708 break;
2709 case arith_expr:
2710 res = validate_arith_expr(tree);
2711 break;
2712 case term:
2713 res = validate_term(tree);
2714 break;
2715 case factor:
2716 res = validate_factor(tree);
2717 break;
2718 case power:
2719 res = validate_power(tree);
2720 break;
2721 case atom:
2722 res = validate_atom(tree);
2723 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002724
Fred Drakeff9ea482000-04-19 13:54:15 +00002725 default:
2726 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002727 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002728 res = 0;
2729 break;
2730 }
2731 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002732 }
2733 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002734}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002735
2736
Guido van Rossum47478871996-08-21 14:32:37 +00002737static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002738validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002739{
2740 int res = validate_eval_input(tree);
2741
2742 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002743 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002744
2745 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002746}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002747
2748
Guido van Rossum3d602e31996-07-21 02:33:56 +00002749/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002750 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002751 */
Guido van Rossum47478871996-08-21 14:32:37 +00002752static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002753validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002754{
Fred Drakec2683dd2001-07-17 19:32:05 +00002755 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002756 int nch = NCH(tree) - 1;
2757 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002758 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002759
Fred Drakec2683dd2001-07-17 19:32:05 +00002760 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002761 if (TYPE(CHILD(tree, j)) == stmt)
2762 res = validate_stmt(CHILD(tree, j));
2763 else
2764 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002765 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002766 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002767 * user. Hopefully, this won't be needed. If a user reports getting
2768 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002769 */
2770 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002771 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002772
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002773 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002774}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002775
2776
Fred Drake43f8f9b1998-04-13 16:25:46 +00002777static PyObject*
2778pickle_constructor = NULL;
2779
2780
2781static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002782parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002783{
Fred Drake268397f1998-04-29 14:16:32 +00002784 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002785 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00002786 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002787 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002788
Fred Drakec2683dd2001-07-17 19:32:05 +00002789 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002790 PyObject *newargs;
2791 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002792
Fred Drake2a6875e1999-09-20 22:32:18 +00002793 if ((empty_dict = PyDict_New()) == NULL)
2794 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002795 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002796 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002797 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002798 if (tuple != NULL) {
2799 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2800 Py_DECREF(tuple);
2801 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002802 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002803 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002804 }
2805 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002806 Py_XDECREF(empty_dict);
2807
Fred Drake43f8f9b1998-04-13 16:25:46 +00002808 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002809}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002810
2811
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002812/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00002813 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002814 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002815 * We'd really have to write a wrapper around it all anyway to allow
2816 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002817 */
2818static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00002819 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002820 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002821 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002822 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002823 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002824 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002825 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002826 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002827 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002828 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002829 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002830 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002831 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002832 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002833 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002834 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002835 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002836 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002837 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002838 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002839 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002840 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002841 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002842 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002843 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002844 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002845 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002846 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002847
Fred Drake43f8f9b1998-04-13 16:25:46 +00002848 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00002849 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002850 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00002851
Fred Drake268397f1998-04-29 14:16:32 +00002852 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002853 };
2854
2855
Mark Hammond62b1ab12002-07-23 06:31:15 +00002856PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00002857
Mark Hammond62b1ab12002-07-23 06:31:15 +00002858PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00002859initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00002860{
Fred Drake13130bc2001-08-15 16:44:56 +00002861 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00002862
2863 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002864 module = Py_InitModule("parser", parser_functions);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002865
Fred Drake7a15ba51999-09-09 14:21:52 +00002866 if (parser_error == 0)
2867 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002868
2869 if ((parser_error == 0)
Fred Drake13130bc2001-08-15 16:44:56 +00002870 || (PyModule_AddObject(module, "ParserError", parser_error) != 0)) {
Fred Drakec2683dd2001-07-17 19:32:05 +00002871 /* caller will check PyErr_Occurred() */
2872 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002873 }
Fred Drakec2683dd2001-07-17 19:32:05 +00002874 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002875 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00002876 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002877 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002878
Fred Drake13130bc2001-08-15 16:44:56 +00002879 PyModule_AddStringConstant(module, "__copyright__",
2880 parser_copyright_string);
2881 PyModule_AddStringConstant(module, "__doc__",
2882 parser_doc_string);
2883 PyModule_AddStringConstant(module, "__version__",
2884 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002885
Fred Drake78bdb9b2001-07-19 20:17:15 +00002886 /* Register to support pickling.
2887 * If this fails, the import of this module will fail because an
2888 * exception will be raised here; should we clear the exception?
2889 */
Fred Drake13130bc2001-08-15 16:44:56 +00002890 copyreg = PyImport_ImportModule("copy_reg");
2891 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002892 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002893
Fred Drake13130bc2001-08-15 16:44:56 +00002894 func = PyObject_GetAttrString(copyreg, "pickle");
2895 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
2896 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00002897 Py_XINCREF(pickle_constructor);
2898 if ((func != NULL) && (pickle_constructor != NULL)
2899 && (pickler != NULL)) {
2900 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002901
Fred Drakec2683dd2001-07-17 19:32:05 +00002902 res = PyObject_CallFunction(func, "OOO", &PyST_Type, pickler,
2903 pickle_constructor);
Fred Drakeff9ea482000-04-19 13:54:15 +00002904 Py_XDECREF(res);
2905 }
2906 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00002907 Py_XDECREF(pickle_constructor);
2908 Py_XDECREF(pickler);
2909 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002910 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002911}