blob: ec3aa5ada4004def968193b9091110be46314c31 [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 Rossumd9e9f9c1995-10-11 17:35:38 +000042/* String constants used to initialize module attributes.
43 *
44 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045static char parser_copyright_string[] =
46"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000047University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
48Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
49Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000050
51
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000052PyDoc_STRVAR(parser_doc_string,
53"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000054
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000055static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000056
57
Martin v. Löwis18e16552006-02-15 17:27:45 +000058typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000059typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000060 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000061 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000062
Thomas Wouters7e474022000-07-16 12:04:32 +000063/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000064 * original copyright statement is included below, and continues to apply
65 * in full to the function immediately following. All other material is
66 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
67 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000068 * new naming conventions. Added arguments to provide support for creating
69 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000070 */
71
Guido van Rossum52f2c051993-11-10 12:53:24 +000072
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000073static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000074node2tuple(node *n, /* node to convert */
75 SeqMaker mkseq, /* create sequence */
76 SeqInserter addelem, /* func. to add elem. in seq. */
Thomas Wouters89f507f2006-12-13 04:49:30 +000077 int lineno, /* include line numbers? */
78 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000079{
Guido van Rossum3d602e31996-07-21 02:33:56 +000080 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000081 Py_INCREF(Py_None);
82 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000083 }
84 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000085 int i;
86 PyObject *v;
87 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000088
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000089 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000090 if (v == NULL)
91 return (v);
92 w = PyInt_FromLong(TYPE(n));
93 if (w == NULL) {
94 Py_DECREF(v);
95 return ((PyObject*) NULL);
96 }
97 (void) addelem(v, 0, w);
98 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +000099 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000100 if (w == NULL) {
101 Py_DECREF(v);
102 return ((PyObject*) NULL);
103 }
104 (void) addelem(v, i+1, w);
105 }
Tim Peters6a627252003-07-21 14:25:23 +0000106
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000107 if (TYPE(n) == encoding_decl)
108 (void) addelem(v, i+1, PyString_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000109 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000110 }
111 else if (ISTERMINAL(TYPE(n))) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000112 PyObject *result = mkseq(2 + lineno + col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000113 if (result != NULL) {
114 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
115 (void) addelem(result, 1, PyString_FromString(STR(n)));
116 if (lineno == 1)
117 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000118 if (col_offset == 1)
119 (void) addelem(result, 3, PyInt_FromLong(n->n_col_offset));
Fred Drakeff9ea482000-04-19 13:54:15 +0000120 }
121 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000122 }
123 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000124 PyErr_SetString(PyExc_SystemError,
125 "unrecognized parse tree node type");
126 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000127 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000128}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000129/*
130 * End of material copyrighted by Stichting Mathematisch Centrum.
131 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000132
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000133
134
135/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000136 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000137 * field of the object type to identify which any given object represents.
138 * These should probably go in an external header to allow other extensions
139 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000140 */
141
Fred Drakec2683dd2001-07-17 19:32:05 +0000142#define PyST_EXPR 1
143#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000144
145
146/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000147 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000148 * naming style, but the code uses the new naming convention.
149 */
150
151static PyObject*
152parser_error = 0;
153
154
Fred Drakec2683dd2001-07-17 19:32:05 +0000155typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000156 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000157 node* st_node; /* the node* returned by the parser */
158 int st_type; /* EXPR or SUITE ? */
159} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000160
161
Jeremy Hylton938ace62002-07-17 16:30:39 +0000162static void parser_free(PyST_Object *st);
163static int parser_compare(PyST_Object *left, PyST_Object *right);
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000164static PyObject *parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000165
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000166
Fred Drake268397f1998-04-29 14:16:32 +0000167static
Fred Drakec2683dd2001-07-17 19:32:05 +0000168PyTypeObject PyST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000169 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000170 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000171 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000172 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000173 0, /* tp_itemsize */
174 (destructor)parser_free, /* tp_dealloc */
175 0, /* tp_print */
176 parser_getattr, /* tp_getattr */
177 0, /* tp_setattr */
178 (cmpfunc)parser_compare, /* tp_compare */
179 0, /* tp_repr */
180 0, /* tp_as_number */
181 0, /* tp_as_sequence */
182 0, /* tp_as_mapping */
183 0, /* tp_hash */
184 0, /* tp_call */
185 0, /* tp_str */
186 0, /* tp_getattro */
187 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000188
189 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000190 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000191
Fred Drakeff9ea482000-04-19 13:54:15 +0000192 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000193
194 /* __doc__ */
195 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000196}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000197
198
199static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000200parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000201{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000202 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000203
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000204 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000205 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000206
207 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000208 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000209
210 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000211 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000212
213 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000214 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000215
216 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000217 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000218
219 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000220 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000221
Fred Drakeff9ea482000-04-19 13:54:15 +0000222 if (v != 0)
223 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000224 }
225 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000226}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000227
228
Fred Drakec2683dd2001-07-17 19:32:05 +0000229/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000230 *
231 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
232 * This really just wraps a call to parser_compare_nodes() with some easy
233 * checks and protection code.
234 *
235 */
236static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000237parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000238{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000239 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000240 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000241
242 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000243 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000244
Fred Drakec2683dd2001-07-17 19:32:05 +0000245 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000246}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247
248
Fred Drakec2683dd2001-07-17 19:32:05 +0000249/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000250 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000251 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000252 * 'wrapper' object that holds a node* and allows it to be passed around in
253 * Python code.
254 *
255 */
256static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000257parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000258{
Fred Drakec2683dd2001-07-17 19:32:05 +0000259 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000260
261 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000262 o->st_node = st;
263 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000264 }
Fred Drake268397f1998-04-29 14:16:32 +0000265 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000266 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000267 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000268 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000269}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000270
271
Fred Drakec2683dd2001-07-17 19:32:05 +0000272/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000273 *
274 * This is called by a del statement that reduces the reference count to 0.
275 *
276 */
277static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000278parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000279{
Fred Drakec2683dd2001-07-17 19:32:05 +0000280 PyNode_Free(st->st_node);
281 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000282}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000283
284
Fred Drakec2683dd2001-07-17 19:32:05 +0000285/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000286 *
287 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000288 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000289 *
290 */
291static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000292parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000293{
Guido van Rossum47478871996-08-21 14:32:37 +0000294 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000295 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000296 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000297 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000298
Thomas Wouters89f507f2006-12-13 04:49:30 +0000299 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000300
Fred Drake268397f1998-04-29 14:16:32 +0000301 if (self == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000302 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
303 &PyST_Type, &self, &line_option,
304 &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000305 }
Fred Drake503d8d61998-04-13 18:45:18 +0000306 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000307 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
308 &line_option, &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000309 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000310 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000311 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000312 if (line_option != NULL) {
313 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
314 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000315 if (col_option != NULL) {
316 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
317 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000318 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000319 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000320 * since it's known to work already.
321 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000322 res = node2tuple(((PyST_Object*)self)->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000323 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000324 }
325 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000326}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000327
328
Fred Drakec2683dd2001-07-17 19:32:05 +0000329/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000330 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000331 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000332 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000333 *
334 */
335static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000336parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000337{
Guido van Rossum47478871996-08-21 14:32:37 +0000338 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000339 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000340 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000341 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000342
Thomas Wouters89f507f2006-12-13 04:49:30 +0000343 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000344
Fred Drake503d8d61998-04-13 18:45:18 +0000345 if (self == NULL)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000346 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
347 &PyST_Type, &self, &line_option,
348 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000349 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000350 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
351 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000352 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000353 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000354 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000355 if (line_option != 0) {
356 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
357 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000358 if (col_option != NULL) {
359 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
360 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000361 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000362 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000363 * since it's known to work already.
364 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000365 res = node2tuple(self->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000366 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000367 }
368 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000369}
Guido van Rossum47478871996-08-21 14:32:37 +0000370
371
Fred Drakec2683dd2001-07-17 19:32:05 +0000372/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000373 *
374 * This function creates code objects from the parse tree represented by
375 * the passed-in data object. An optional file name is passed in as well.
376 *
377 */
378static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000379parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000380{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000381 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000382 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000383 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000384
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000385 static char *keywords[] = {"ast", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000386
Fred Drake503d8d61998-04-13 18:45:18 +0000387 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000388 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
389 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000390 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000391 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000392 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000393
394 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000395 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000396
397 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000398}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000399
400
401/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
402 * PyObject* parser_issuite(PyObject* self, PyObject* args)
403 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000404 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000405 * a statement suite, respectively. The return is a Python truth value.
406 *
407 */
408static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000409parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000410{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000411 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000412 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000413
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000414 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000415
Fred Drake503d8d61998-04-13 18:45:18 +0000416 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000417 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000418 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000419 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000420 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000421
422 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000423 /* Check to see if the ST represents an expression or not. */
424 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000425 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000426 }
427 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000428}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000429
430
431static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000432parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000433{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000434 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000435 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000436
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000437 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000438
Fred Drake503d8d61998-04-13 18:45:18 +0000439 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000440 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000441 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000442 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000443 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000444
445 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000446 /* Check to see if the ST represents an expression or not. */
447 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000448 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000449 }
450 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000451}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000452
453
Fred Drake7a15ba51999-09-09 14:21:52 +0000454#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
455
Fred Drake503d8d61998-04-13 18:45:18 +0000456static PyMethodDef
457parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000458 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000459 PyDoc_STR("Compile this ST object into a code object.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000460 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000461 PyDoc_STR("Determines if this ST object was created from an expression.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000462 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000463 PyDoc_STR("Determines if this ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000464 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000465 PyDoc_STR("Creates a list-tree representation of this ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000466 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000467 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Fred Drake503d8d61998-04-13 18:45:18 +0000468
Fred Drake268397f1998-04-29 14:16:32 +0000469 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000470};
471
Fred Drake503d8d61998-04-13 18:45:18 +0000472
473static PyObject*
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000474parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000475{
Fred Drake503d8d61998-04-13 18:45:18 +0000476 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000477}
Fred Drake503d8d61998-04-13 18:45:18 +0000478
479
Guido van Rossum3d602e31996-07-21 02:33:56 +0000480/* err_string(char* message)
481 *
482 * Sets the error string for an exception of type ParserError.
483 *
484 */
485static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000486err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000487{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000488 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000489}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000490
491
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000492/* PyObject* parser_do_parse(PyObject* args, int type)
493 *
494 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000495 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000496 *
497 */
498static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000499parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000500{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000501 char* string = 0;
502 PyObject* res = 0;
503
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000504 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000505
506 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000507 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000508 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000509 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000510
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000511 if (n)
512 res = parser_newstobject(n, type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000513 }
514 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000515}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000516
517
518/* PyObject* parser_expr(PyObject* self, PyObject* args)
519 * PyObject* parser_suite(PyObject* self, PyObject* args)
520 *
521 * External interfaces to the parser itself. Which is called determines if
522 * the parser attempts to recognize an expression ('eval' form) or statement
523 * suite ('exec' form). The real work is done by parser_do_parse() above.
524 *
525 */
526static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000527parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000528{
Fred Drake268397f1998-04-29 14:16:32 +0000529 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000530 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000531}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000532
533
534static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000535parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000536{
Fred Drake268397f1998-04-29 14:16:32 +0000537 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000538 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000539}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000540
541
542
Fred Drakec2683dd2001-07-17 19:32:05 +0000543/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000544 * object requires that the input tuple be valid without having to rely on
545 * catching an exception from the compiler. This is done to allow the
546 * compiler itself to remain fast, since most of its input will come from
547 * the parser directly, and therefore be known to be syntactically correct.
548 * This validation is done to ensure that we don't core dump the compile
549 * phase, returning an exception instead.
550 *
551 * Two aspects can be broken out in this code: creating a node tree from
552 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000553 * advantageous to expand the number of ST types to include funcdefs and
554 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000555 * here. They are not necessary, and not quite as useful in a raw form.
556 * For now, let's get expressions and suites working reliably.
557 */
558
559
Jeremy Hylton938ace62002-07-17 16:30:39 +0000560static node* build_node_tree(PyObject *tuple);
561static int validate_expr_tree(node *tree);
562static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000563static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000564
Fred Drakec2683dd2001-07-17 19:32:05 +0000565/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000566 *
567 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000568 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000569 * tuple can be validated. It does this by checking the first code of the
570 * tuple, and, if acceptable, builds the internal representation. If this
571 * step succeeds, the internal representation is validated as fully as
572 * possible with the various validate_*() routines defined below.
573 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000574 * This function must be changed if support is to be added for PyST_FRAGMENT
575 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000576 *
577 */
578static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000579parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000580{
Fred Drake268397f1998-04-29 14:16:32 +0000581 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000582 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000583 PyObject *tuple;
584 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000585
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000586 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000587
Fred Drakec2683dd2001-07-17 19:32:05 +0000588 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000589 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000590 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000591 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000592 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000593 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000594 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000595 }
596 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000597 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000598 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000599 tree = build_node_tree(tuple);
600 if (tree != 0) {
601 int start_sym = TYPE(tree);
602 if (start_sym == eval_input) {
603 /* Might be an eval form. */
604 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000605 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000606 else
607 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000608 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000609 else if (start_sym == file_input) {
610 /* This looks like an exec form so far. */
611 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000612 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000613 else
614 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000615 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000616 else if (start_sym == encoding_decl) {
617 /* This looks like an encoding_decl so far. */
618 if (validate_encoding_decl(tree))
619 st = parser_newstobject(tree, PyST_SUITE);
620 else
621 PyNode_Free(tree);
622 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000623 else {
624 /* This is a fragment, at best. */
625 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000626 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000627 }
Guido van Rossum47478871996-08-21 14:32:37 +0000628 }
Guido van Rossum47478871996-08-21 14:32:37 +0000629 /* Make sure we throw an exception on all errors. We should never
630 * get this, but we'd do well to be sure something is done.
631 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000632 if (st == NULL && !PyErr_Occurred())
633 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000634
Fred Drakec2683dd2001-07-17 19:32:05 +0000635 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000636}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000637
638
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000639/* node* build_node_children()
640 *
641 * Iterate across the children of the current non-terminal node and build
642 * their structures. If successful, return the root of this portion of
643 * the tree, otherwise, 0. Any required exception will be specified already,
644 * and no memory will have been deallocated.
645 *
646 */
647static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000648build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000649{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000650 Py_ssize_t len = PyObject_Size(tuple);
651 Py_ssize_t i;
652 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000653
654 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000655 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000656 PyObject* elem = PySequence_GetItem(tuple, i);
657 int ok = elem != NULL;
658 long type = 0;
659 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000660
Fred Drakeff9ea482000-04-19 13:54:15 +0000661 if (ok)
662 ok = PySequence_Check(elem);
663 if (ok) {
664 PyObject *temp = PySequence_GetItem(elem, 0);
665 if (temp == NULL)
666 ok = 0;
667 else {
668 ok = PyInt_Check(temp);
669 if (ok)
670 type = PyInt_AS_LONG(temp);
671 Py_DECREF(temp);
672 }
673 }
674 if (!ok) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675 PyObject *err = Py_BuildValue("os", elem,
676 "Illegal node construct.");
677 PyErr_SetObject(parser_error, err);
678 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000679 Py_XDECREF(elem);
680 return (0);
681 }
682 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000683 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000684 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000685
Fred Drake0ac9b072000-09-12 21:58:06 +0000686 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000687 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000688 return 0;
689 }
690 temp = PySequence_GetItem(elem, 1);
691 if (temp == NULL)
692 return 0;
693 if (!PyString_Check(temp)) {
694 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000695 "second item in terminal node must be a string,"
696 " found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000697 temp->ob_type->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000698 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000699 return 0;
700 }
701 if (len == 3) {
702 PyObject *o = PySequence_GetItem(elem, 2);
703 if (o != NULL) {
704 if (PyInt_Check(o))
705 *line_num = PyInt_AS_LONG(o);
706 else {
707 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000708 "third item in terminal node must be an"
709 " integer, found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000710 temp->ob_type->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000711 Py_DECREF(o);
712 Py_DECREF(temp);
713 return 0;
714 }
715 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000716 }
717 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000718 len = PyString_GET_SIZE(temp) + 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000719 strn = (char *)PyObject_MALLOC(len);
Fred Drake0ac9b072000-09-12 21:58:06 +0000720 if (strn != NULL)
721 (void) memcpy(strn, PyString_AS_STRING(temp), len);
722 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000723 }
724 else if (!ISNONTERMINAL(type)) {
725 /*
726 * It has to be one or the other; this is an error.
727 * Throw an exception.
728 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000729 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
730 PyErr_SetObject(parser_error, err);
731 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000732 Py_XDECREF(elem);
733 return (0);
734 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000735 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000736 if (err == E_NOMEM) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000737 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000738 return (node *) PyErr_NoMemory();
739 }
740 if (err == E_OVERFLOW) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000741 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000742 PyErr_SetString(PyExc_ValueError,
743 "unsupported number of child nodes");
744 return NULL;
745 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000746
Fred Drakeff9ea482000-04-19 13:54:15 +0000747 if (ISNONTERMINAL(type)) {
748 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000749
Fred Drakeff9ea482000-04-19 13:54:15 +0000750 if (new_child != build_node_children(elem, new_child, line_num)) {
751 Py_XDECREF(elem);
752 return (0);
753 }
754 }
755 else if (type == NEWLINE) { /* It's true: we increment the */
756 ++(*line_num); /* line number *after* the newline! */
757 }
758 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000759 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000760 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000761}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000762
763
764static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000765build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000766{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000767 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000768 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000769 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000770
Guido van Rossum47478871996-08-21 14:32:37 +0000771 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000772 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000773 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000774 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000775 /*
776 * The tuple is simple, but it doesn't start with a start symbol.
777 * Throw an exception now and be done with it.
778 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000779 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000780 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000781 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000782 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000783 }
784 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000785 /*
786 * Not efficient, but that can be handled later.
787 */
788 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000789 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000790
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000791 if (num == encoding_decl) {
792 encoding = PySequence_GetItem(tuple, 2);
793 /* tuple isn't borrowed anymore here, need to DECREF */
794 tuple = PySequence_GetSlice(tuple, 0, 2);
795 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000796 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000797 if (res != NULL) {
798 if (res != build_node_children(tuple, res, &line_num)) {
799 PyNode_Free(res);
800 res = NULL;
801 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000802 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000803 Py_ssize_t len;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000804 len = PyString_GET_SIZE(encoding) + 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000805 res->n_str = (char *)PyObject_MALLOC(len);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000806 if (res->n_str != NULL)
807 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
808 Py_DECREF(encoding);
809 Py_DECREF(tuple);
810 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000811 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000812 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000813 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000814 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000815 * NONTERMINAL, we can't use it. Not sure the implementation
816 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000817 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000818 PyObject *err = Py_BuildValue("os", tuple,
819 "Illegal component tuple.");
820 PyErr_SetObject(parser_error, err);
821 Py_XDECREF(err);
822 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000823
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000824 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000825}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000826
827
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000828/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000829 * Validation routines used within the validation section:
830 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000831static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000832
Fred Drakeff9ea482000-04-19 13:54:15 +0000833#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
834#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
835#define validate_colon(ch) validate_terminal(ch, COLON, ":")
836#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
837#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
838#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
839#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
840#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
841#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
842#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
843#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
844#define validate_star(ch) validate_terminal(ch, STAR, "*")
845#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
846#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
847#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000848#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000849#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000850
Fred Drake0ac9b072000-09-12 21:58:06 +0000851#define VALIDATER(n) static int validate_##n(node *tree)
852
Fred Drakeff9ea482000-04-19 13:54:15 +0000853VALIDATER(node); VALIDATER(small_stmt);
854VALIDATER(class); VALIDATER(node);
855VALIDATER(parameters); VALIDATER(suite);
856VALIDATER(testlist); VALIDATER(varargslist);
Neal Norwitzc1505362006-12-28 06:47:50 +0000857VALIDATER(vfpdef); VALIDATER(vfplist);
Fred Drakeff9ea482000-04-19 13:54:15 +0000858VALIDATER(stmt); VALIDATER(simple_stmt);
859VALIDATER(expr_stmt); VALIDATER(power);
Guido van Rossum452bf512007-02-09 05:32:43 +0000860VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000861VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000862VALIDATER(raise_stmt); VALIDATER(import_stmt);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000863VALIDATER(import_name); VALIDATER(import_from);
Fred Drakecff283c2000-08-21 22:24:43 +0000864VALIDATER(global_stmt); VALIDATER(list_if);
865VALIDATER(assert_stmt); VALIDATER(list_for);
Neal Norwitzc1505362006-12-28 06:47:50 +0000866VALIDATER(compound_stmt); VALIDATER(vname);
Fred Drakeff9ea482000-04-19 13:54:15 +0000867VALIDATER(while); VALIDATER(for);
868VALIDATER(try); VALIDATER(except_clause);
869VALIDATER(test); VALIDATER(and_test);
870VALIDATER(not_test); VALIDATER(comparison);
871VALIDATER(comp_op); VALIDATER(expr);
872VALIDATER(xor_expr); VALIDATER(and_expr);
873VALIDATER(shift_expr); VALIDATER(arith_expr);
874VALIDATER(term); VALIDATER(factor);
875VALIDATER(atom); VALIDATER(lambdef);
876VALIDATER(trailer); VALIDATER(subscript);
877VALIDATER(subscriptlist); VALIDATER(sliceop);
Neal Norwitz6309f2d2006-08-29 05:53:33 +0000878VALIDATER(exprlist); VALIDATER(dictsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000879VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000880VALIDATER(listmaker); VALIDATER(yield_stmt);
Raymond Hettinger354433a2004-05-19 08:20:33 +0000881VALIDATER(testlist1); VALIDATER(gen_for);
882VALIDATER(gen_iter); VALIDATER(gen_if);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000883VALIDATER(testlist_gexp); VALIDATER(yield_expr);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +0000884VALIDATER(yield_or_testlist); VALIDATER(or_test);
885VALIDATER(old_test); VALIDATER(old_lambdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000886
Fred Drake0ac9b072000-09-12 21:58:06 +0000887#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000888
Fred Drakeff9ea482000-04-19 13:54:15 +0000889#define is_even(n) (((n) & 1) == 0)
890#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000891
892
893static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000894validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000895{
Fred Drake0ac9b072000-09-12 21:58:06 +0000896 if (TYPE(n) != t) {
897 PyErr_Format(parser_error, "Expected node type %d, got %d.",
898 t, TYPE(n));
899 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000900 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000901 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000902}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000903
904
Fred Drakee7ab64e2000-04-25 04:14:46 +0000905/* Verifies that the number of child nodes is exactly 'num', raising
906 * an exception if it isn't. The exception message does not indicate
907 * the exact number of nodes, allowing this to be used to raise the
908 * "right" exception when the wrong number of nodes is present in a
909 * specific variant of a statement's syntax. This is commonly used
910 * in that fashion.
911 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000912static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000913validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000914{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000915 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000916 PyErr_Format(parser_error,
917 "Illegal number of children for %s node.", name);
918 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000919 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000920 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000921}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000922
923
924static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000925validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000926{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000927 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000928 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000929
930 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000931 PyErr_Format(parser_error,
932 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000933 }
934 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000935}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000936
937
Guido van Rossum47478871996-08-21 14:32:37 +0000938/* X (',' X) [',']
939 */
940static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000941validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000942 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000943{
944 int nch = NCH(tree);
945 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000946 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000947
948 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000949 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000950 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000951 if (is_even(nch))
952 res = validate_comma(CHILD(tree, --nch));
953 if (res && nch > 1) {
954 int pos = 1;
955 for ( ; res && pos < nch; pos += 2)
956 res = (validate_comma(CHILD(tree, pos))
957 && vfunc(CHILD(tree, pos + 1)));
958 }
Guido van Rossum47478871996-08-21 14:32:37 +0000959 }
960 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000961}
Guido van Rossum47478871996-08-21 14:32:37 +0000962
963
Fred Drakecff283c2000-08-21 22:24:43 +0000964/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000965 *
966 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000967 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000968 */
Guido van Rossum47478871996-08-21 14:32:37 +0000969static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000970validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000971{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000972 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +0000973 int res = (validate_ntype(tree, classdef) &&
974 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000975
Guido van Rossum3d602e31996-07-21 02:33:56 +0000976 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000977 res = (validate_name(CHILD(tree, 0), "class")
978 && validate_ntype(CHILD(tree, 1), NAME)
979 && validate_colon(CHILD(tree, nch - 2))
980 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000981 }
Brett Cannonf4189912005-04-09 02:30:16 +0000982 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000983 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +0000984 }
985
986 if (res) {
987 if (nch == 7) {
988 res = ((validate_lparen(CHILD(tree, 2)) &&
989 validate_testlist(CHILD(tree, 3)) &&
990 validate_rparen(CHILD(tree, 4))));
991 }
992 else if (nch == 6) {
993 res = (validate_lparen(CHILD(tree,2)) &&
994 validate_rparen(CHILD(tree,3)));
995 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000996 }
997 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000998}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000999
1000
Guido van Rossum3d602e31996-07-21 02:33:56 +00001001/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001002 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001003 */
Guido van Rossum47478871996-08-21 14:32:37 +00001004static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001005validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001006{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001007 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001008 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001009 && (nch >= 4)
1010 && validate_name(CHILD(tree, 0), "if")
1011 && validate_test(CHILD(tree, 1))
1012 && validate_colon(CHILD(tree, 2))
1013 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001014
1015 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001016 /* ... 'else' ':' suite */
1017 res = (validate_name(CHILD(tree, nch - 3), "else")
1018 && validate_colon(CHILD(tree, nch - 2))
1019 && validate_suite(CHILD(tree, nch - 1)));
1020 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001021 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001022 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001023 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001024 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001025 /* Will catch the case for nch < 4 */
1026 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001027 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001028 /* ... ('elif' test ':' suite)+ ... */
1029 int j = 4;
1030 while ((j < nch) && res) {
1031 res = (validate_name(CHILD(tree, j), "elif")
1032 && validate_colon(CHILD(tree, j + 2))
1033 && validate_test(CHILD(tree, j + 1))
1034 && validate_suite(CHILD(tree, j + 3)));
1035 j += 4;
1036 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001037 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001038 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001039}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001040
1041
Guido van Rossum3d602e31996-07-21 02:33:56 +00001042/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001043 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001044 *
1045 */
Guido van Rossum47478871996-08-21 14:32:37 +00001046static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001047validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001048{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001049 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001050 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001051
Guido van Rossum3d602e31996-07-21 02:33:56 +00001052 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001053 res = (validate_lparen(CHILD(tree, 0))
1054 && validate_rparen(CHILD(tree, nch - 1)));
1055 if (res && (nch == 3))
1056 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001057 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001058 else {
1059 (void) validate_numnodes(tree, 2, "parameters");
1060 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001061 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001062}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001063
1064
Fred Drakecff283c2000-08-21 22:24:43 +00001065/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001066 *
1067 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001068 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001069 * | NEWLINE INDENT stmt+ DEDENT
1070 */
Guido van Rossum47478871996-08-21 14:32:37 +00001071static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001072validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001073{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001074 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001075 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001076
Guido van Rossum3d602e31996-07-21 02:33:56 +00001077 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001078 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001079 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001080 /* NEWLINE INDENT stmt+ DEDENT */
1081 res = (validate_newline(CHILD(tree, 0))
1082 && validate_indent(CHILD(tree, 1))
1083 && validate_stmt(CHILD(tree, 2))
1084 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001085
Fred Drakeff9ea482000-04-19 13:54:15 +00001086 if (res && (nch > 4)) {
1087 int i = 3;
1088 --nch; /* forget the DEDENT */
1089 for ( ; res && (i < nch); ++i)
1090 res = validate_stmt(CHILD(tree, i));
1091 }
1092 else if (nch < 4)
1093 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001094 }
1095 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001096}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001097
1098
Guido van Rossum47478871996-08-21 14:32:37 +00001099static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001100validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001101{
Guido van Rossum47478871996-08-21 14:32:37 +00001102 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001103 validate_test, "testlist"));
1104}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001105
1106
Guido van Rossum1c917072001-10-15 15:44:05 +00001107static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001108validate_testlist1(node *tree)
1109{
1110 return (validate_repeating_list(tree, testlist1,
1111 validate_test, "testlist1"));
1112}
1113
1114
1115static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001116validate_testlist_safe(node *tree)
1117{
1118 return (validate_repeating_list(tree, testlist_safe,
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001119 validate_old_test, "testlist_safe"));
Guido van Rossum1c917072001-10-15 15:44:05 +00001120}
1121
1122
Neal Norwitzc1505362006-12-28 06:47:50 +00001123/* validate either vname or tname.
1124 * vname: NAME
1125 * tname: NAME [':' test]
1126 */
1127static int
1128validate_vname(node *tree)
1129{
1130 int nch = NCH(tree);
1131 if (TYPE(tree) == vname) {
1132 return nch == 1 && validate_name(CHILD(tree, 0), NULL);
1133 }
1134 else if (TYPE(tree) == tname) {
1135 if (nch == 1) {
1136 return validate_name(CHILD(tree, 0), NULL);
1137 }
1138 else if (nch == 3) {
1139 return validate_name(CHILD(tree, 0), NULL) &&
1140 validate_colon(CHILD(tree, 1)) &&
1141 validate_test(CHILD(tree, 2));
1142 }
1143 }
1144 return 0;
1145}
1146
1147/* '*' vname (',' vname ['=' test])* [',' '**' vname] | '**' vname
1148 * ..or tname in place of vname. vname: NAME; tname: NAME [':' test]
Fred Drakecff283c2000-08-21 22:24:43 +00001149 */
1150static int
1151validate_varargslist_trailer(node *tree, int start)
1152{
1153 int nch = NCH(tree);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001154 int res = 0, i;
Fred Drakecff283c2000-08-21 22:24:43 +00001155 int sym;
1156
1157 if (nch <= start) {
1158 err_string("expected variable argument trailer for varargslist");
1159 return 0;
1160 }
1161 sym = TYPE(CHILD(tree, start));
1162 if (sym == STAR) {
1163 /*
Neal Norwitzc1505362006-12-28 06:47:50 +00001164 * '*' vname (',' vname ['=' test])* [',' '**' vname] | '**' vname
Fred Drakecff283c2000-08-21 22:24:43 +00001165 */
1166 if (nch-start == 2)
Neal Norwitzc1505362006-12-28 06:47:50 +00001167 res = validate_vname(CHILD(tree, start+1));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001168 else if (nch-start == 5 && TYPE(CHILD(tree, start+2)) == COMMA)
Neal Norwitzc1505362006-12-28 06:47:50 +00001169 res = (validate_vname(CHILD(tree, start+1))
Fred Drakecff283c2000-08-21 22:24:43 +00001170 && validate_comma(CHILD(tree, start+2))
1171 && validate_doublestar(CHILD(tree, start+3))
Neal Norwitzc1505362006-12-28 06:47:50 +00001172 && validate_vname(CHILD(tree, start+4)));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001173 else {
Neal Norwitzc1505362006-12-28 06:47:50 +00001174 /* skip over vname (',' vname ['=' test])* */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001175 i = start + 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001176 if (TYPE(CHILD(tree, i)) == vname ||
1177 TYPE(CHILD(tree, i)) == tname) { /* skip over vname or tname */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001178 i += 1;
1179 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001180 while (res && i+1 < nch) { /* validate (',' vname ['=' test])* */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001181 res = validate_comma(CHILD(tree, i));
1182 if (TYPE(CHILD(tree, i+1)) == DOUBLESTAR)
1183 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001184 res = res && validate_vname(CHILD(tree, i+1));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001185 if (res && i+2 < nch && TYPE(CHILD(tree, i+2)) == EQUAL) {
1186 res = res && (i+3 < nch)
1187 && validate_test(CHILD(tree, i+3));
1188 i += 4;
1189 }
1190 else {
1191 i += 2;
1192 }
1193 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001194 /* [',' '**' vname] */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001195 if (res && i+1 < nch && TYPE(CHILD(tree, i+1)) == DOUBLESTAR) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001196 res = validate_vname(CHILD(tree, i+2));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001197 }
1198 }
Fred Drakecff283c2000-08-21 22:24:43 +00001199 }
1200 else if (sym == DOUBLESTAR) {
1201 /*
1202 * '**' NAME
1203 */
1204 if (nch-start == 2)
Neal Norwitzc1505362006-12-28 06:47:50 +00001205 res = validate_vname(CHILD(tree, start+1));
Fred Drakecff283c2000-08-21 22:24:43 +00001206 }
1207 if (!res)
1208 err_string("illegal variable argument trailer for varargslist");
1209 return res;
1210}
1211
1212
Neal Norwitzc1505362006-12-28 06:47:50 +00001213/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001214 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001215 * Validate typedargslist or varargslist.
1216 *
1217 * typedargslist: ((tfpdef ['=' test] ',')*
1218 * ('*' [tname] (',' tname ['=' test])* [',' '**' tname] |
1219 * '**' tname)
1220 * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
1221 * tname: NAME [':' test]
1222 * tfpdef: tname | '(' tfplist ')'
1223 * tfplist: tfpdef (',' tfpdef)* [',']
1224 * varargslist: ((vfpdef ['=' test] ',')*
1225 * ('*' [vname] (',' vname ['=' test])* [',' '**' vname] |
1226 * '**' vname)
1227 * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
1228 * vname: NAME
1229 * vfpdef: vname | '(' vfplist ')'
1230 * vfplist: vfpdef (',' vfpdef)* [',']
Guido van Rossum3d602e31996-07-21 02:33:56 +00001231 *
1232 */
Guido van Rossum47478871996-08-21 14:32:37 +00001233static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001234validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001235{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001236 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001237 int res = (TYPE(tree) == varargslist ||
1238 TYPE(tree) == typedargslist) &&
1239 (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001240 int sym;
Fred Drakeb6429a22000-12-11 22:08:27 +00001241 if (!res)
1242 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001243 if (nch < 1) {
1244 err_string("varargslist missing child nodes");
1245 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001246 }
Fred Drakecff283c2000-08-21 22:24:43 +00001247 sym = TYPE(CHILD(tree, 0));
1248 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001249 /* whole thing matches:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001250 * '*' [NAME] (',' NAME ['=' test])* [',' '**' NAME] | '**' NAME
Fred Drakeb6429a22000-12-11 22:08:27 +00001251 */
Fred Drakecff283c2000-08-21 22:24:43 +00001252 res = validate_varargslist_trailer(tree, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001253 else if (sym == vfpdef || sym == tfpdef) {
Fred Drakecff283c2000-08-21 22:24:43 +00001254 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001255
Fred Drakecff283c2000-08-21 22:24:43 +00001256 sym = TYPE(CHILD(tree, nch-1));
Neal Norwitzc1505362006-12-28 06:47:50 +00001257 if (sym == vname || sym == tname) {
Fred Drakecff283c2000-08-21 22:24:43 +00001258 /*
Neal Norwitzc1505362006-12-28 06:47:50 +00001259 * (vfpdef ['=' test] ',')+
1260 * ('*' vname [',' '**' vname]
1261 * | '**' vname)
Fred Drakecff283c2000-08-21 22:24:43 +00001262 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001263 /* skip over (vfpdef ['=' test] ',')+ */
Fred Drakecff283c2000-08-21 22:24:43 +00001264 while (res && (i+2 <= nch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001265 res = validate_vfpdef(CHILD(tree, i));
Fred Drakecff283c2000-08-21 22:24:43 +00001266 ++i;
1267 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1268 res = (validate_equal(CHILD(tree, i))
1269 && validate_test(CHILD(tree, i+1)));
1270 if (res)
1271 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001272 }
Fred Drakecff283c2000-08-21 22:24:43 +00001273 if (res && i < nch) {
1274 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001275 ++i;
1276 if (res && i < nch
1277 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1278 || TYPE(CHILD(tree, i)) == STAR))
1279 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001280 }
1281 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001282 /* .. ('*' [NAME] (',' NAME ['=' test])* [',' '**' NAME] | '**' NAME)
Fred Drakeb6429a22000-12-11 22:08:27 +00001283 * i --^^^
1284 */
Fred Drakecff283c2000-08-21 22:24:43 +00001285 if (res)
1286 res = validate_varargslist_trailer(tree, i);
1287 }
1288 else {
1289 /*
Neal Norwitzc1505362006-12-28 06:47:50 +00001290 * vfpdef ['=' test] (',' vfpdef ['=' test])* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00001291 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001292 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001293 if (sym == COMMA) {
1294 res = validate_comma(CHILD(tree, nch-1));
1295 if (!res)
1296 return 0;
1297 --nch;
1298 }
1299 /*
Neal Norwitzc1505362006-12-28 06:47:50 +00001300 * vfpdef ['=' test] (',' vfpdef ['=' test])*
Fred Drakecff283c2000-08-21 22:24:43 +00001301 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001302 res = validate_vfpdef(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001303 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001304 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1305 res = (validate_equal(CHILD(tree, i))
1306 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001307 i += 2;
1308 }
1309 /*
Neal Norwitzc1505362006-12-28 06:47:50 +00001310 * ... (',' vfpdef ['=' test])*
Fred Drakecff283c2000-08-21 22:24:43 +00001311 * i ---^^^
1312 */
1313 while (res && (nch - i) >= 2) {
1314 res = (validate_comma(CHILD(tree, i))
Neal Norwitzc1505362006-12-28 06:47:50 +00001315 && validate_vfpdef(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001316 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001317 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1318 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001319 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001320 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001321 }
1322 }
1323 if (res && nch - i != 0) {
1324 res = 0;
1325 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001326 }
1327 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001328 }
Fred Drakecff283c2000-08-21 22:24:43 +00001329 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001330}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001331
1332
Fred Drakecff283c2000-08-21 22:24:43 +00001333/* list_iter: list_for | list_if
1334 */
1335static int
1336validate_list_iter(node *tree)
1337{
1338 int res = (validate_ntype(tree, list_iter)
1339 && validate_numnodes(tree, 1, "list_iter"));
1340 if (res && TYPE(CHILD(tree, 0)) == list_for)
1341 res = validate_list_for(CHILD(tree, 0));
1342 else
1343 res = validate_list_if(CHILD(tree, 0));
1344
1345 return res;
1346}
1347
Raymond Hettinger354433a2004-05-19 08:20:33 +00001348/* gen_iter: gen_for | gen_if
1349 */
1350static int
1351validate_gen_iter(node *tree)
1352{
1353 int res = (validate_ntype(tree, gen_iter)
1354 && validate_numnodes(tree, 1, "gen_iter"));
1355 if (res && TYPE(CHILD(tree, 0)) == gen_for)
1356 res = validate_gen_for(CHILD(tree, 0));
1357 else
1358 res = validate_gen_if(CHILD(tree, 0));
1359
1360 return res;
1361}
1362
Fred Drakecff283c2000-08-21 22:24:43 +00001363/* list_for: 'for' exprlist 'in' testlist [list_iter]
1364 */
1365static int
1366validate_list_for(node *tree)
1367{
1368 int nch = NCH(tree);
1369 int res;
1370
1371 if (nch == 5)
1372 res = validate_list_iter(CHILD(tree, 4));
1373 else
1374 res = validate_numnodes(tree, 4, "list_for");
1375
1376 if (res)
1377 res = (validate_name(CHILD(tree, 0), "for")
1378 && validate_exprlist(CHILD(tree, 1))
1379 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001380 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001381
1382 return res;
1383}
1384
Raymond Hettinger354433a2004-05-19 08:20:33 +00001385/* gen_for: 'for' exprlist 'in' test [gen_iter]
1386 */
1387static int
1388validate_gen_for(node *tree)
1389{
1390 int nch = NCH(tree);
1391 int res;
1392
1393 if (nch == 5)
1394 res = validate_gen_iter(CHILD(tree, 4));
1395 else
1396 res = validate_numnodes(tree, 4, "gen_for");
1397
1398 if (res)
1399 res = (validate_name(CHILD(tree, 0), "for")
1400 && validate_exprlist(CHILD(tree, 1))
1401 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001402 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001403
1404 return res;
1405}
1406
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001407/* list_if: 'if' old_test [list_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001408 */
1409static int
1410validate_list_if(node *tree)
1411{
1412 int nch = NCH(tree);
1413 int res;
1414
1415 if (nch == 3)
1416 res = validate_list_iter(CHILD(tree, 2));
1417 else
1418 res = validate_numnodes(tree, 2, "list_if");
1419
1420 if (res)
1421 res = (validate_name(CHILD(tree, 0), "if")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001422 && validate_old_test(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001423
1424 return res;
1425}
1426
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001427/* gen_if: 'if' old_test [gen_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001428 */
1429static int
1430validate_gen_if(node *tree)
1431{
1432 int nch = NCH(tree);
1433 int res;
1434
1435 if (nch == 3)
1436 res = validate_gen_iter(CHILD(tree, 2));
1437 else
1438 res = validate_numnodes(tree, 2, "gen_if");
1439
1440 if (res)
1441 res = (validate_name(CHILD(tree, 0), "if")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001442 && validate_old_test(CHILD(tree, 1)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001443
1444 return res;
1445}
Fred Drakecff283c2000-08-21 22:24:43 +00001446
Neal Norwitzc1505362006-12-28 06:47:50 +00001447/* validate_vfpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001448 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001449 * Validate vfpdef or tfpdef.
1450 *
1451 * vname: NAME
1452 * vfpdef: vname | '(' vfplist ')'
1453 * vfplist: vfpdef (',' vfpdef)* [',']
1454 *
1455 * tname: NAME [':' test]
1456 * tfpdef: tname | '(' tfplist ')'
1457 * tfplist: tfpdef (',' tfpdef)* [',']
1458 *
Guido van Rossum3d602e31996-07-21 02:33:56 +00001459 */
Guido van Rossum47478871996-08-21 14:32:37 +00001460static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001461validate_vfpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001462{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001463 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001464 int typ = TYPE(tree);
1465 int res = typ == vfpdef || typ == tfpdef;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001466
Guido van Rossum3d602e31996-07-21 02:33:56 +00001467 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001468 if (nch == 1)
Neal Norwitzc1505362006-12-28 06:47:50 +00001469 res = validate_vname(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001470 else if (nch == 3)
1471 res = (validate_lparen(CHILD(tree, 0))
Neal Norwitzc1505362006-12-28 06:47:50 +00001472 && validate_vfplist(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001473 && validate_rparen(CHILD(tree, 2)));
1474 else
1475 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001476 }
1477 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001478}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001479
1480
Guido van Rossum47478871996-08-21 14:32:37 +00001481static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001482validate_vfplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001483{
Neal Norwitzc1505362006-12-28 06:47:50 +00001484 return (validate_repeating_list(tree, vfplist,
1485 validate_vfpdef, "vfplist"));
Fred Drakeff9ea482000-04-19 13:54:15 +00001486}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001487
1488
Guido van Rossum3d602e31996-07-21 02:33:56 +00001489/* simple_stmt | compound_stmt
1490 *
1491 */
Guido van Rossum47478871996-08-21 14:32:37 +00001492static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001493validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001494{
1495 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001496 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001497
Guido van Rossum3d602e31996-07-21 02:33:56 +00001498 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001499 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001500
Fred Drakeff9ea482000-04-19 13:54:15 +00001501 if (TYPE(tree) == simple_stmt)
1502 res = validate_simple_stmt(tree);
1503 else
1504 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001505 }
1506 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001507}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001508
1509
Guido van Rossum3d602e31996-07-21 02:33:56 +00001510/* small_stmt (';' small_stmt)* [';'] NEWLINE
1511 *
1512 */
Guido van Rossum47478871996-08-21 14:32:37 +00001513static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001514validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001515{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001516 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001517 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001518 && (nch >= 2)
1519 && validate_small_stmt(CHILD(tree, 0))
1520 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001521
Guido van Rossum3d602e31996-07-21 02:33:56 +00001522 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001523 res = validate_numnodes(tree, 2, "simple_stmt");
1524 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001525 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001526 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001527 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001528 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001529
Fred Drakeff9ea482000-04-19 13:54:15 +00001530 for (i = 1; res && (i < nch); i += 2)
1531 res = (validate_semi(CHILD(tree, i))
1532 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001533 }
1534 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001535}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001536
1537
Guido van Rossum47478871996-08-21 14:32:37 +00001538static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001539validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001540{
1541 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001542 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001543
Fred Drake0ac9b072000-09-12 21:58:06 +00001544 if (res) {
1545 int ntype = TYPE(CHILD(tree, 0));
1546
1547 if ( (ntype == expr_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001548 || (ntype == del_stmt)
1549 || (ntype == pass_stmt)
1550 || (ntype == flow_stmt)
1551 || (ntype == import_stmt)
1552 || (ntype == global_stmt)
Georg Brandl7cae87c2006-09-06 06:51:57 +00001553 || (ntype == assert_stmt))
Fred Drake0ac9b072000-09-12 21:58:06 +00001554 res = validate_node(CHILD(tree, 0));
1555 else {
1556 res = 0;
1557 err_string("illegal small_stmt child type");
1558 }
1559 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001560 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001561 res = 0;
1562 PyErr_Format(parser_error,
1563 "Unrecognized child node of small_stmt: %d.",
1564 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001565 }
1566 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001567}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001568
1569
1570/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001571 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001572 */
Guido van Rossum47478871996-08-21 14:32:37 +00001573static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001574validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001575{
1576 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001577 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001578 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001579
1580 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001581 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001582
1583 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001584 ntype = TYPE(tree);
1585 if ( (ntype == if_stmt)
1586 || (ntype == while_stmt)
1587 || (ntype == for_stmt)
1588 || (ntype == try_stmt)
1589 || (ntype == funcdef)
1590 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001591 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001592 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001593 res = 0;
1594 PyErr_Format(parser_error,
1595 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001596 }
1597 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001598}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001599
1600
Guido van Rossum47478871996-08-21 14:32:37 +00001601static int
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001602validate_yield_or_testlist(node *tree)
1603{
1604 if (TYPE(tree) == yield_expr)
1605 return validate_yield_expr(tree);
1606 else
1607 return validate_testlist(tree);
1608}
1609
1610static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001611validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001612{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001613 int j;
1614 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001615 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001616 && is_odd(nch)
1617 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001618
Fred Drake28f739a2000-08-25 22:42:40 +00001619 if (res && nch == 3
1620 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001621 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1622 && validate_yield_or_testlist(CHILD(tree, 2));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001623
Fred Drake28f739a2000-08-25 22:42:40 +00001624 if (res) {
1625 char *s = STR(CHILD(CHILD(tree, 1), 0));
1626
1627 res = (strcmp(s, "+=") == 0
1628 || strcmp(s, "-=") == 0
1629 || strcmp(s, "*=") == 0
1630 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001631 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001632 || strcmp(s, "%=") == 0
1633 || strcmp(s, "&=") == 0
1634 || strcmp(s, "|=") == 0
1635 || strcmp(s, "^=") == 0
1636 || strcmp(s, "<<=") == 0
1637 || strcmp(s, ">>=") == 0
1638 || strcmp(s, "**=") == 0);
1639 if (!res)
1640 err_string("illegal augmmented assignment operator");
1641 }
1642 }
1643 else {
1644 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001645 res = validate_equal(CHILD(tree, j))
1646 && validate_yield_or_testlist(CHILD(tree, j + 1));
Fred Drake28f739a2000-08-25 22:42:40 +00001647 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001648 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001649}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001650
1651
Guido van Rossum47478871996-08-21 14:32:37 +00001652static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001653validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001654{
1655 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001656 && validate_name(CHILD(tree, 0), "del")
1657 && validate_exprlist(CHILD(tree, 1)));
1658}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001659
1660
Guido van Rossum47478871996-08-21 14:32:37 +00001661static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001662validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001663{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001664 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001665 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001666 && ((nch == 1) || (nch == 2))
1667 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001668
Guido van Rossum3d602e31996-07-21 02:33:56 +00001669 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001670 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001671
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001672 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001673}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001674
1675
Guido van Rossum47478871996-08-21 14:32:37 +00001676static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001677validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001678{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001679 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001680 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001681 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001682
Guido van Rossum3d602e31996-07-21 02:33:56 +00001683 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001684 res = validate_name(CHILD(tree, 0), "raise");
1685 if (res && (nch >= 2))
1686 res = validate_test(CHILD(tree, 1));
1687 if (res && nch > 2) {
1688 res = (validate_comma(CHILD(tree, 2))
1689 && validate_test(CHILD(tree, 3)));
1690 if (res && (nch > 4))
1691 res = (validate_comma(CHILD(tree, 4))
1692 && validate_test(CHILD(tree, 5)));
1693 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001694 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001695 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001696 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001697 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001698 res = (validate_comma(CHILD(tree, 2))
1699 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001700
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001701 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001702}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001703
1704
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001705/* yield_expr: 'yield' [testlist]
1706 */
1707static int
1708validate_yield_expr(node *tree)
1709{
1710 int nch = NCH(tree);
1711 int res = (validate_ntype(tree, yield_expr)
1712 && ((nch == 1) || (nch == 2))
1713 && validate_name(CHILD(tree, 0), "yield"));
1714
1715 if (res && (nch == 2))
1716 res = validate_testlist(CHILD(tree, 1));
1717
1718 return (res);
1719}
1720
1721
1722/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001723 */
1724static int
1725validate_yield_stmt(node *tree)
1726{
1727 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001728 && validate_numnodes(tree, 1, "yield_stmt")
1729 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001730}
1731
1732
Fred Drakecff283c2000-08-21 22:24:43 +00001733static int
1734validate_import_as_name(node *tree)
1735{
1736 int nch = NCH(tree);
1737 int ok = validate_ntype(tree, import_as_name);
1738
1739 if (ok) {
1740 if (nch == 1)
1741 ok = validate_name(CHILD(tree, 0), NULL);
1742 else if (nch == 3)
1743 ok = (validate_name(CHILD(tree, 0), NULL)
1744 && validate_name(CHILD(tree, 1), "as")
1745 && validate_name(CHILD(tree, 2), NULL));
1746 else
1747 ok = validate_numnodes(tree, 3, "import_as_name");
1748 }
1749 return ok;
1750}
1751
1752
Fred Drake71137082001-01-07 05:59:59 +00001753/* dotted_name: NAME ("." NAME)*
1754 */
1755static int
1756validate_dotted_name(node *tree)
1757{
1758 int nch = NCH(tree);
1759 int res = (validate_ntype(tree, dotted_name)
1760 && is_odd(nch)
1761 && validate_name(CHILD(tree, 0), NULL));
1762 int i;
1763
1764 for (i = 1; res && (i < nch); i += 2) {
1765 res = (validate_dot(CHILD(tree, i))
1766 && validate_name(CHILD(tree, i+1), NULL));
1767 }
1768 return res;
1769}
1770
1771
Fred Drakecff283c2000-08-21 22:24:43 +00001772/* dotted_as_name: dotted_name [NAME NAME]
1773 */
1774static int
1775validate_dotted_as_name(node *tree)
1776{
1777 int nch = NCH(tree);
1778 int res = validate_ntype(tree, dotted_as_name);
1779
1780 if (res) {
1781 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001782 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001783 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001784 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001785 && validate_name(CHILD(tree, 1), "as")
1786 && validate_name(CHILD(tree, 2), NULL));
1787 else {
1788 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001789 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001790 }
1791 }
1792 return res;
1793}
1794
1795
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001796/* dotted_as_name (',' dotted_as_name)* */
1797static int
1798validate_dotted_as_names(node *tree)
1799{
1800 int nch = NCH(tree);
1801 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1802 int i;
1803
1804 for (i = 1; res && (i < nch); i += 2)
1805 res = (validate_comma(CHILD(tree, i))
1806 && validate_dotted_as_name(CHILD(tree, i + 1)));
1807 return (res);
1808}
1809
1810
1811/* import_as_name (',' import_as_name)* [','] */
1812static int
1813validate_import_as_names(node *tree)
1814{
1815 int nch = NCH(tree);
1816 int res = validate_import_as_name(CHILD(tree, 0));
1817 int i;
1818
1819 for (i = 1; res && (i + 1 < nch); i += 2)
1820 res = (validate_comma(CHILD(tree, i))
1821 && validate_import_as_name(CHILD(tree, i + 1)));
1822 return (res);
1823}
1824
1825
1826/* 'import' dotted_as_names */
1827static int
1828validate_import_name(node *tree)
1829{
1830 return (validate_ntype(tree, import_name)
1831 && validate_numnodes(tree, 2, "import_name")
1832 && validate_name(CHILD(tree, 0), "import")
1833 && validate_dotted_as_names(CHILD(tree, 1)));
1834}
1835
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001836/* Helper function to count the number of leading dots in
1837 * 'from ...module import name'
1838 */
1839static int
1840count_from_dots(node *tree)
1841{
1842 int i;
1843 for (i = 0; i < NCH(tree); i++)
1844 if (TYPE(CHILD(tree, i)) != DOT)
1845 break;
1846 return i;
1847}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001848
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001849/* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001850 * import_as_names
Guido van Rossum3d602e31996-07-21 02:33:56 +00001851 */
Guido van Rossum47478871996-08-21 14:32:37 +00001852static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001853validate_import_from(node *tree)
1854{
1855 int nch = NCH(tree);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001856 int ndots = count_from_dots(tree);
1857 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1858 int offset = ndots + havename;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001859 int res = validate_ntype(tree, import_from)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001860 && (nch >= 4 + ndots)
1861 && validate_name(CHILD(tree, 0), "from")
1862 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1863 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001864
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001865 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1866 res = ((nch == offset + 5)
1867 && validate_lparen(CHILD(tree, offset + 2))
1868 && validate_import_as_names(CHILD(tree, offset + 3))
1869 && validate_rparen(CHILD(tree, offset + 4)));
1870 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1871 res = validate_import_as_names(CHILD(tree, offset + 2));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001872 return (res);
1873}
1874
1875
1876/* import_stmt: import_name | import_from */
1877static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001878validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001879{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001880 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001881 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001882
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001883 if (res) {
1884 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001885
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001886 if (ntype == import_name || ntype == import_from)
1887 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001888 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001889 res = 0;
1890 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001891 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001892 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001893 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001894 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001895 PyErr_Format(parser_error,
1896 "Unrecognized child node of import_stmt: %d.",
1897 TYPE(CHILD(tree, 0)));
1898 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001899 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001900}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001901
1902
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001903
1904
Guido van Rossum47478871996-08-21 14:32:37 +00001905static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001906validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001907{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001908 int j;
1909 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001910 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001911 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001912
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001913 if (!res && !PyErr_Occurred())
1914 err_string("illegal global statement");
1915
Guido van Rossum3d602e31996-07-21 02:33:56 +00001916 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001917 res = (validate_name(CHILD(tree, 0), "global")
1918 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001919 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001920 res = (validate_comma(CHILD(tree, j))
1921 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001922
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001923 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001924}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001925
1926
Guido van Rossum925e5471997-04-02 05:32:13 +00001927/* assert_stmt:
1928 *
1929 * 'assert' test [',' test]
1930 */
1931static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001932validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001933{
1934 int nch = NCH(tree);
1935 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001936 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001937 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001938 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001939
1940 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001941 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001942 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001943 res = (validate_comma(CHILD(tree, 2))
1944 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001945
1946 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001947}
Guido van Rossum925e5471997-04-02 05:32:13 +00001948
1949
Guido van Rossum47478871996-08-21 14:32:37 +00001950static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001951validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001952{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001953 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001954 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001955 && ((nch == 4) || (nch == 7))
1956 && validate_name(CHILD(tree, 0), "while")
1957 && validate_test(CHILD(tree, 1))
1958 && validate_colon(CHILD(tree, 2))
1959 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001960
Guido van Rossum3d602e31996-07-21 02:33:56 +00001961 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001962 res = (validate_name(CHILD(tree, 4), "else")
1963 && validate_colon(CHILD(tree, 5))
1964 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001965
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001966 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001967}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001968
1969
Guido van Rossum47478871996-08-21 14:32:37 +00001970static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001971validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001972{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001973 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001974 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001975 && ((nch == 6) || (nch == 9))
1976 && validate_name(CHILD(tree, 0), "for")
1977 && validate_exprlist(CHILD(tree, 1))
1978 && validate_name(CHILD(tree, 2), "in")
1979 && validate_testlist(CHILD(tree, 3))
1980 && validate_colon(CHILD(tree, 4))
1981 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001982
Guido van Rossum3d602e31996-07-21 02:33:56 +00001983 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001984 res = (validate_name(CHILD(tree, 6), "else")
1985 && validate_colon(CHILD(tree, 7))
1986 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001987
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001988 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001989}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001990
1991
Guido van Rossum3d602e31996-07-21 02:33:56 +00001992/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001993 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001994 * | 'try' ':' suite 'finally' ':' suite
1995 *
1996 */
Guido van Rossum47478871996-08-21 14:32:37 +00001997static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001998validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001999{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002000 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002001 int pos = 3;
2002 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002003 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002004
2005 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002006 res = (validate_name(CHILD(tree, 0), "try")
2007 && validate_colon(CHILD(tree, 1))
2008 && validate_suite(CHILD(tree, 2))
2009 && validate_colon(CHILD(tree, nch - 2))
2010 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00002011 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00002012 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00002013 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
2014 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00002015
2016 PyErr_Format(parser_error,
2017 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002018 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002019 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002020 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002021 res = (validate_except_clause(CHILD(tree, pos))
2022 && validate_colon(CHILD(tree, pos + 1))
2023 && validate_suite(CHILD(tree, pos + 2)));
2024 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002025 }
2026 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002027 res = validate_ntype(CHILD(tree, pos), NAME);
2028 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
2029 res = (validate_numnodes(tree, 6, "try/finally")
2030 && validate_colon(CHILD(tree, 4))
2031 && validate_suite(CHILD(tree, 5)));
2032 else if (res) {
2033 if (nch == (pos + 3)) {
2034 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
2035 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
2036 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00002037 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00002038 }
2039 else if (nch == (pos + 6)) {
2040 res = (validate_name(CHILD(tree, pos), "except")
2041 && validate_colon(CHILD(tree, pos + 1))
2042 && validate_suite(CHILD(tree, pos + 2))
2043 && validate_name(CHILD(tree, pos + 3), "else"));
2044 }
2045 else
2046 res = validate_numnodes(tree, pos + 3, "try/except");
2047 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002048 }
2049 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002050}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002051
2052
Guido van Rossum47478871996-08-21 14:32:37 +00002053static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002054validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002055{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002056 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002057 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00002058 && ((nch == 1) || (nch == 2) || (nch == 4))
2059 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002060
Guido van Rossum3d602e31996-07-21 02:33:56 +00002061 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002062 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002063 if (res && (nch == 4))
Guido van Rossumb940e112007-01-10 16:19:56 +00002064 res = (validate_name(CHILD(tree, 2), "as")
2065 && validate_ntype(CHILD(tree, 3), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002066
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002067 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002068}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002069
2070
Guido van Rossum47478871996-08-21 14:32:37 +00002071static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002072validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002073{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002074 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002075 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002076
Guido van Rossum3d602e31996-07-21 02:33:56 +00002077 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002078 res = ((nch == 1)
2079 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002080 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002081 res = validate_or_test(CHILD(tree, 0));
2082 res = (res && (nch == 1 || (nch == 5 &&
2083 validate_name(CHILD(tree, 1), "if") &&
2084 validate_or_test(CHILD(tree, 2)) &&
2085 validate_name(CHILD(tree, 3), "else") &&
2086 validate_test(CHILD(tree, 4)))));
2087 }
2088 return (res);
2089}
2090
2091static int
2092validate_old_test(node *tree)
2093{
2094 int nch = NCH(tree);
2095 int res = validate_ntype(tree, old_test) && (nch == 1);
2096
2097 if (res && (TYPE(CHILD(tree, 0)) == old_lambdef))
2098 res = (validate_old_lambdef(CHILD(tree, 0)));
2099 else if (res) {
2100 res = (validate_or_test(CHILD(tree, 0)));
2101 }
2102 return (res);
2103}
2104
2105static int
2106validate_or_test(node *tree)
2107{
2108 int nch = NCH(tree);
2109 int res = validate_ntype(tree, or_test) && is_odd(nch);
2110
2111 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002112 int pos;
2113 res = validate_and_test(CHILD(tree, 0));
2114 for (pos = 1; res && (pos < nch); pos += 2)
2115 res = (validate_name(CHILD(tree, pos), "or")
2116 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002117 }
2118 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002119}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002120
2121
Guido van Rossum47478871996-08-21 14:32:37 +00002122static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002123validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002124{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002125 int pos;
2126 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002127 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002128 && is_odd(nch)
2129 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002130
Guido van Rossum3d602e31996-07-21 02:33:56 +00002131 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002132 res = (validate_name(CHILD(tree, pos), "and")
2133 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002134
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002135 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002136}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002137
2138
Guido van Rossum47478871996-08-21 14:32:37 +00002139static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002140validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002141{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002142 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002143 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002144
Guido van Rossum3d602e31996-07-21 02:33:56 +00002145 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002146 if (nch == 2)
2147 res = (validate_name(CHILD(tree, 0), "not")
2148 && validate_not_test(CHILD(tree, 1)));
2149 else if (nch == 1)
2150 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002151 }
2152 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002153}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002154
2155
Guido van Rossum47478871996-08-21 14:32:37 +00002156static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002157validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002158{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002159 int pos;
2160 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002161 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002162 && is_odd(nch)
2163 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002164
Guido van Rossum3d602e31996-07-21 02:33:56 +00002165 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002166 res = (validate_comp_op(CHILD(tree, pos))
2167 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002168
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002169 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002170}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002171
2172
Guido van Rossum47478871996-08-21 14:32:37 +00002173static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002174validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002175{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002176 int res = 0;
2177 int nch = NCH(tree);
2178
Guido van Rossum3d602e31996-07-21 02:33:56 +00002179 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002180 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002181 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002182 /*
2183 * Only child will be a terminal with a well-defined symbolic name
2184 * or a NAME with a string of either 'is' or 'in'
2185 */
2186 tree = CHILD(tree, 0);
2187 switch (TYPE(tree)) {
2188 case LESS:
2189 case GREATER:
2190 case EQEQUAL:
2191 case EQUAL:
2192 case LESSEQUAL:
2193 case GREATEREQUAL:
2194 case NOTEQUAL:
2195 res = 1;
2196 break;
2197 case NAME:
2198 res = ((strcmp(STR(tree), "in") == 0)
2199 || (strcmp(STR(tree), "is") == 0));
2200 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002201 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002202 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002203 }
2204 break;
2205 default:
Fred Drake661ea262000-10-24 19:57:45 +00002206 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002207 break;
2208 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002209 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002210 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002211 res = (validate_ntype(CHILD(tree, 0), NAME)
2212 && validate_ntype(CHILD(tree, 1), NAME)
2213 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2214 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2215 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2216 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2217 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002218 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002219 }
2220 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002221}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002222
2223
Guido van Rossum47478871996-08-21 14:32:37 +00002224static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002225validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002226{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002227 int j;
2228 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002229 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002230 && is_odd(nch)
2231 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002232
Guido van Rossum3d602e31996-07-21 02:33:56 +00002233 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002234 res = (validate_xor_expr(CHILD(tree, j))
2235 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002236
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002237 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002238}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002239
2240
Guido van Rossum47478871996-08-21 14:32:37 +00002241static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002242validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002243{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002244 int j;
2245 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002246 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002247 && is_odd(nch)
2248 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002249
Guido van Rossum3d602e31996-07-21 02:33:56 +00002250 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002251 res = (validate_circumflex(CHILD(tree, j - 1))
2252 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002253
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002254 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002255}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002256
2257
Guido van Rossum47478871996-08-21 14:32:37 +00002258static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002259validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002260{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002261 int pos;
2262 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002263 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002264 && is_odd(nch)
2265 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002266
Guido van Rossum3d602e31996-07-21 02:33:56 +00002267 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002268 res = (validate_ampersand(CHILD(tree, pos))
2269 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002270
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002271 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002272}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002273
2274
2275static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002276validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002277 {
2278 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002279 int nch = NCH(tree);
2280 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002281 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002282
Guido van Rossum3d602e31996-07-21 02:33:56 +00002283 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002284 if (TYPE(CHILD(tree, pos)) != op1)
2285 res = validate_ntype(CHILD(tree, pos), op2);
2286 if (res)
2287 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002288 }
2289 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002290}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002291
2292
Guido van Rossum47478871996-08-21 14:32:37 +00002293static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002294validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002295{
2296 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002297 && validate_chain_two_ops(tree, validate_arith_expr,
2298 LEFTSHIFT, RIGHTSHIFT));
2299}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002300
2301
Guido van Rossum47478871996-08-21 14:32:37 +00002302static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002303validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002304{
2305 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002306 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2307}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002308
2309
Guido van Rossum47478871996-08-21 14:32:37 +00002310static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002311validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002312{
2313 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002314 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002315 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002316 && is_odd(nch)
2317 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002318
Guido van Rossum3d602e31996-07-21 02:33:56 +00002319 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002320 res = (((TYPE(CHILD(tree, pos)) == STAR)
2321 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002322 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002323 || (TYPE(CHILD(tree, pos)) == PERCENT))
2324 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002325
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002326 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002327}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002328
2329
Guido van Rossum3d602e31996-07-21 02:33:56 +00002330/* factor:
2331 *
2332 * factor: ('+'|'-'|'~') factor | power
2333 */
Guido van Rossum47478871996-08-21 14:32:37 +00002334static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002335validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002336{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002337 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002338 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002339 && (((nch == 2)
2340 && ((TYPE(CHILD(tree, 0)) == PLUS)
2341 || (TYPE(CHILD(tree, 0)) == MINUS)
2342 || (TYPE(CHILD(tree, 0)) == TILDE))
2343 && validate_factor(CHILD(tree, 1)))
2344 || ((nch == 1)
2345 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002346 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002347}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002348
2349
Guido van Rossum3d602e31996-07-21 02:33:56 +00002350/* power:
2351 *
2352 * power: atom trailer* ('**' factor)*
2353 */
Guido van Rossum47478871996-08-21 14:32:37 +00002354static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002355validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002356{
2357 int pos = 1;
2358 int nch = NCH(tree);
2359 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002360 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002361
2362 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002363 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002364 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002365 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002366 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002367 return (0);
2368 }
2369 for ( ; res && (pos < (nch - 1)); pos += 2)
2370 res = (validate_doublestar(CHILD(tree, pos))
2371 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002372 }
2373 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002374}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002375
2376
Guido van Rossum47478871996-08-21 14:32:37 +00002377static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002378validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002379{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002380 int pos;
2381 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002382 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002383
Fred Drakecff283c2000-08-21 22:24:43 +00002384 if (res && nch < 1)
2385 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002386 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002387 switch (TYPE(CHILD(tree, 0))) {
2388 case LPAR:
2389 res = ((nch <= 3)
2390 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002391
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002392 if (res && (nch == 3)) {
2393 if (TYPE(CHILD(tree, 1))==yield_expr)
2394 res = validate_yield_expr(CHILD(tree, 1));
2395 else
2396 res = validate_testlist_gexp(CHILD(tree, 1));
2397 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002398 break;
2399 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002400 if (nch == 2)
2401 res = validate_ntype(CHILD(tree, 1), RSQB);
2402 else if (nch == 3)
2403 res = (validate_listmaker(CHILD(tree, 1))
2404 && validate_ntype(CHILD(tree, 2), RSQB));
2405 else {
2406 res = 0;
2407 err_string("illegal list display atom");
2408 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002409 break;
2410 case LBRACE:
2411 res = ((nch <= 3)
2412 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002413
Fred Drakeff9ea482000-04-19 13:54:15 +00002414 if (res && (nch == 3))
Neal Norwitz6309f2d2006-08-29 05:53:33 +00002415 res = validate_dictsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002416 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002417 case NAME:
2418 case NUMBER:
2419 res = (nch == 1);
2420 break;
2421 case STRING:
2422 for (pos = 1; res && (pos < nch); ++pos)
2423 res = validate_ntype(CHILD(tree, pos), STRING);
2424 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002425 case DOT:
2426 res = (nch == 3 &&
2427 validate_ntype(CHILD(tree, 1), DOT) &&
2428 validate_ntype(CHILD(tree, 2), DOT));
2429 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002430 default:
2431 res = 0;
2432 break;
2433 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002434 }
2435 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002436}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002437
2438
Fred Drake85bf3bb2000-08-23 15:35:26 +00002439/* listmaker:
2440 * test ( list_for | (',' test)* [','] )
2441 */
Fred Drakecff283c2000-08-21 22:24:43 +00002442static int
2443validate_listmaker(node *tree)
2444{
2445 int nch = NCH(tree);
2446 int ok = nch;
2447
2448 if (nch == 0)
2449 err_string("missing child nodes of listmaker");
2450 else
2451 ok = validate_test(CHILD(tree, 0));
2452
2453 /*
Raymond Hettinger354433a2004-05-19 08:20:33 +00002454 * list_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002455 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002456 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2457 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002458 else {
2459 /* (',' test)* [','] */
2460 int i = 1;
2461 while (ok && nch - i >= 2) {
2462 ok = (validate_comma(CHILD(tree, i))
2463 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002464 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002465 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002466 if (ok && i == nch-1)
2467 ok = validate_comma(CHILD(tree, i));
2468 else if (i != nch) {
2469 ok = 0;
2470 err_string("illegal trailing nodes for listmaker");
2471 }
Fred Drakecff283c2000-08-21 22:24:43 +00002472 }
2473 return ok;
2474}
2475
Raymond Hettinger354433a2004-05-19 08:20:33 +00002476/* testlist_gexp:
2477 * test ( gen_for | (',' test)* [','] )
2478 */
2479static int
2480validate_testlist_gexp(node *tree)
2481{
2482 int nch = NCH(tree);
2483 int ok = nch;
2484
2485 if (nch == 0)
2486 err_string("missing child nodes of testlist_gexp");
2487 else {
2488 ok = validate_test(CHILD(tree, 0));
2489 }
2490
2491 /*
2492 * gen_for | (',' test)* [',']
2493 */
2494 if (nch == 2 && TYPE(CHILD(tree, 1)) == gen_for)
2495 ok = validate_gen_for(CHILD(tree, 1));
2496 else {
2497 /* (',' test)* [','] */
2498 int i = 1;
2499 while (ok && nch - i >= 2) {
2500 ok = (validate_comma(CHILD(tree, i))
2501 && validate_test(CHILD(tree, i+1)));
2502 i += 2;
2503 }
2504 if (ok && i == nch-1)
2505 ok = validate_comma(CHILD(tree, i));
2506 else if (i != nch) {
2507 ok = 0;
2508 err_string("illegal trailing nodes for testlist_gexp");
2509 }
2510 }
2511 return ok;
2512}
Fred Drakecff283c2000-08-21 22:24:43 +00002513
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002514/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002515 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002516 */
2517static int
2518validate_decorator(node *tree)
2519{
2520 int ok;
2521 int nch = NCH(tree);
2522 ok = (validate_ntype(tree, decorator) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002523 (nch == 3 || nch == 5 || nch == 6) &&
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002524 validate_at(CHILD(tree, 0)) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002525 validate_dotted_name(CHILD(tree, 1)) &&
2526 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002527
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002528 if (ok && nch != 3) {
2529 ok = (validate_lparen(CHILD(tree, 2)) &&
2530 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002531
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002532 if (ok && nch == 6)
2533 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002534 }
2535
2536 return ok;
2537}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002538
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002539/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002540 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002541 */
2542static int
2543validate_decorators(node *tree)
2544{
2545 int i, nch, ok;
2546 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002547 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002548
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002549 for (i = 0; ok && i < nch; ++i)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002550 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002551
2552 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002553}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002554
Guido van Rossum3d602e31996-07-21 02:33:56 +00002555/* funcdef:
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002556 *
2557 * -6 -5 -4 -3 -2 -1
2558 * [decorators] 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002559 */
Guido van Rossum47478871996-08-21 14:32:37 +00002560static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002561validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002562{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002563 int nch = NCH(tree);
2564 int ok = (validate_ntype(tree, funcdef)
2565 && ((nch == 5) || (nch == 6))
2566 && validate_name(RCHILD(tree, -5), "def")
2567 && validate_ntype(RCHILD(tree, -4), NAME)
2568 && validate_colon(RCHILD(tree, -2))
2569 && validate_parameters(RCHILD(tree, -3))
2570 && validate_suite(RCHILD(tree, -1)));
2571
2572 if (ok && (nch == 6))
2573 ok = validate_decorators(CHILD(tree, 0));
2574
2575 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002576}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002577
2578
Guido van Rossum47478871996-08-21 14:32:37 +00002579static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002580validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002581{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002582 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002583 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002584 && ((nch == 3) || (nch == 4))
2585 && validate_name(CHILD(tree, 0), "lambda")
2586 && validate_colon(CHILD(tree, nch - 2))
2587 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002588
Guido van Rossum3d602e31996-07-21 02:33:56 +00002589 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002590 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002591 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002592 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002593
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002594 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002595}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002596
2597
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002598static int
2599validate_old_lambdef(node *tree)
2600{
2601 int nch = NCH(tree);
2602 int res = (validate_ntype(tree, old_lambdef)
2603 && ((nch == 3) || (nch == 4))
2604 && validate_name(CHILD(tree, 0), "lambda")
2605 && validate_colon(CHILD(tree, nch - 2))
2606 && validate_test(CHILD(tree, nch - 1)));
2607
2608 if (res && (nch == 4))
2609 res = validate_varargslist(CHILD(tree, 1));
2610 else if (!res && !PyErr_Occurred())
2611 (void) validate_numnodes(tree, 3, "old_lambdef");
2612
2613 return (res);
2614}
2615
2616
Guido van Rossum3d602e31996-07-21 02:33:56 +00002617/* arglist:
2618 *
Fred Drakecff283c2000-08-21 22:24:43 +00002619 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002620 */
Guido van Rossum47478871996-08-21 14:32:37 +00002621static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002622validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002623{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002624 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002625 int i = 0;
2626 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002627
2628 if (nch <= 0)
2629 /* raise the right error from having an invalid number of children */
2630 return validate_numnodes(tree, nch + 1, "arglist");
2631
Raymond Hettinger354433a2004-05-19 08:20:33 +00002632 if (nch > 1) {
2633 for (i=0; i<nch; i++) {
2634 if (TYPE(CHILD(tree, i)) == argument) {
2635 node *ch = CHILD(tree, i);
2636 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == gen_for) {
2637 err_string("need '(', ')' for generator expression");
2638 return 0;
2639 }
2640 }
2641 }
2642 }
2643
Fred Drakecff283c2000-08-21 22:24:43 +00002644 while (ok && nch-i >= 2) {
2645 /* skip leading (argument ',') */
2646 ok = (validate_argument(CHILD(tree, i))
2647 && validate_comma(CHILD(tree, i+1)));
2648 if (ok)
2649 i += 2;
2650 else
2651 PyErr_Clear();
2652 }
2653 ok = 1;
2654 if (nch-i > 0) {
2655 /*
2656 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002657 */
Fred Drakecff283c2000-08-21 22:24:43 +00002658 int sym = TYPE(CHILD(tree, i));
2659
2660 if (sym == argument) {
2661 ok = validate_argument(CHILD(tree, i));
2662 if (ok && i+1 != nch) {
2663 err_string("illegal arglist specification"
2664 " (extra stuff on end)");
2665 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002666 }
Fred Drakecff283c2000-08-21 22:24:43 +00002667 }
2668 else if (sym == STAR) {
2669 ok = validate_star(CHILD(tree, i));
2670 if (ok && (nch-i == 2))
2671 ok = validate_test(CHILD(tree, i+1));
2672 else if (ok && (nch-i == 5))
2673 ok = (validate_test(CHILD(tree, i+1))
2674 && validate_comma(CHILD(tree, i+2))
2675 && validate_doublestar(CHILD(tree, i+3))
2676 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002677 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002678 err_string("illegal use of '*' in arglist");
2679 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002680 }
2681 }
Fred Drakecff283c2000-08-21 22:24:43 +00002682 else if (sym == DOUBLESTAR) {
2683 if (nch-i == 2)
2684 ok = (validate_doublestar(CHILD(tree, i))
2685 && validate_test(CHILD(tree, i+1)));
2686 else {
2687 err_string("illegal use of '**' in arglist");
2688 ok = 0;
2689 }
2690 }
2691 else {
2692 err_string("illegal arglist specification");
2693 ok = 0;
2694 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002695 }
2696 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002697}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002698
2699
2700
2701/* argument:
2702 *
Raymond Hettinger354433a2004-05-19 08:20:33 +00002703 * [test '='] test [gen_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002704 */
Guido van Rossum47478871996-08-21 14:32:37 +00002705static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002706validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002707{
2708 int nch = NCH(tree);
2709 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002710 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002711 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002712
Raymond Hettinger354433a2004-05-19 08:20:33 +00002713 if (res && (nch == 2))
2714 res = validate_gen_for(CHILD(tree, 1));
2715 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002716 res = (validate_equal(CHILD(tree, 1))
2717 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002718
2719 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002720}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002721
2722
2723
2724/* trailer:
2725 *
Guido van Rossum47478871996-08-21 14:32:37 +00002726 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002727 */
Guido van Rossum47478871996-08-21 14:32:37 +00002728static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002729validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002730{
2731 int nch = NCH(tree);
2732 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002733
2734 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002735 switch (TYPE(CHILD(tree, 0))) {
2736 case LPAR:
2737 res = validate_rparen(CHILD(tree, nch - 1));
2738 if (res && (nch == 3))
2739 res = validate_arglist(CHILD(tree, 1));
2740 break;
2741 case LSQB:
2742 res = (validate_numnodes(tree, 3, "trailer")
2743 && validate_subscriptlist(CHILD(tree, 1))
2744 && validate_ntype(CHILD(tree, 2), RSQB));
2745 break;
2746 case DOT:
2747 res = (validate_numnodes(tree, 2, "trailer")
2748 && validate_ntype(CHILD(tree, 1), NAME));
2749 break;
2750 default:
2751 res = 0;
2752 break;
2753 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002754 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002755 else {
2756 (void) validate_numnodes(tree, 2, "trailer");
2757 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002758 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002759}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002760
2761
Guido van Rossum47478871996-08-21 14:32:37 +00002762/* subscriptlist:
2763 *
2764 * subscript (',' subscript)* [',']
2765 */
2766static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002767validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002768{
2769 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002770 validate_subscript, "subscriptlist"));
2771}
Guido van Rossum47478871996-08-21 14:32:37 +00002772
2773
Guido van Rossum3d602e31996-07-21 02:33:56 +00002774/* subscript:
2775 *
Guido van Rossum47478871996-08-21 14:32:37 +00002776 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002777 */
Guido van Rossum47478871996-08-21 14:32:37 +00002778static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002779validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002780{
Guido van Rossum47478871996-08-21 14:32:37 +00002781 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002782 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002783 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002784
Guido van Rossum47478871996-08-21 14:32:37 +00002785 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002786 if (!PyErr_Occurred())
2787 err_string("invalid number of arguments for subscript node");
2788 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002789 }
Guido van Rossum47478871996-08-21 14:32:37 +00002790 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002791 /* take care of ('.' '.' '.') possibility */
2792 return (validate_numnodes(tree, 3, "subscript")
2793 && validate_dot(CHILD(tree, 0))
2794 && validate_dot(CHILD(tree, 1))
2795 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002796 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002797 if (TYPE(CHILD(tree, 0)) == test)
2798 res = validate_test(CHILD(tree, 0));
2799 else
2800 res = validate_colon(CHILD(tree, 0));
2801 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002802 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002803 /* Must be [test] ':' [test] [sliceop],
2804 * but at least one of the optional components will
2805 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002806 */
2807 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002808 res = validate_test(CHILD(tree, 0));
2809 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002810 }
2811 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002812 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002813 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002814 int rem = nch - ++offset;
2815 if (rem) {
2816 if (TYPE(CHILD(tree, offset)) == test) {
2817 res = validate_test(CHILD(tree, offset));
2818 ++offset;
2819 --rem;
2820 }
2821 if (res && rem)
2822 res = validate_sliceop(CHILD(tree, offset));
2823 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002824 }
2825 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002826}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002827
2828
Guido van Rossum47478871996-08-21 14:32:37 +00002829static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002830validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002831{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002832 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002833 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002834 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002835 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002836 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002837 }
Guido van Rossum47478871996-08-21 14:32:37 +00002838 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002839 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002840 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002841 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002842
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002843 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002844}
Guido van Rossum47478871996-08-21 14:32:37 +00002845
2846
2847static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002848validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002849{
2850 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002851 validate_expr, "exprlist"));
2852}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002853
2854
Guido van Rossum47478871996-08-21 14:32:37 +00002855static int
Neal Norwitz6309f2d2006-08-29 05:53:33 +00002856validate_dictsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002857{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002858 int nch = NCH(tree);
Neal Norwitz6309f2d2006-08-29 05:53:33 +00002859 int res = (validate_ntype(tree, dictsetmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002860 && (nch >= 3)
2861 && validate_test(CHILD(tree, 0))
2862 && validate_colon(CHILD(tree, 1))
2863 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002864
Guido van Rossum3d602e31996-07-21 02:33:56 +00002865 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002866 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002867 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002868 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002869
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002870 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002871 int pos = 3;
2872 /* ( ',' test ':' test )* */
2873 while (res && (pos < nch)) {
2874 res = (validate_comma(CHILD(tree, pos))
2875 && validate_test(CHILD(tree, pos + 1))
2876 && validate_colon(CHILD(tree, pos + 2))
2877 && validate_test(CHILD(tree, pos + 3)));
2878 pos += 4;
2879 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002880 }
2881 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002882}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002883
2884
Guido van Rossum47478871996-08-21 14:32:37 +00002885static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002886validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002887{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002888 int pos;
2889 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002890 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002891 && (nch >= 2)
2892 && validate_testlist(CHILD(tree, 0))
2893 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002894
Guido van Rossum3d602e31996-07-21 02:33:56 +00002895 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002896 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002897
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002898 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002899}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002900
2901
Guido van Rossum47478871996-08-21 14:32:37 +00002902static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002903validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002904{
Fred Drakeff9ea482000-04-19 13:54:15 +00002905 int nch = 0; /* num. children on current node */
2906 int res = 1; /* result value */
2907 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002908
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002909 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002910 nch = NCH(tree);
2911 next = 0;
2912 switch (TYPE(tree)) {
2913 /*
2914 * Definition nodes.
2915 */
2916 case funcdef:
2917 res = validate_funcdef(tree);
2918 break;
2919 case classdef:
2920 res = validate_class(tree);
2921 break;
2922 /*
2923 * "Trivial" parse tree nodes.
2924 * (Why did I call these trivial?)
2925 */
2926 case stmt:
2927 res = validate_stmt(tree);
2928 break;
2929 case small_stmt:
2930 /*
Guido van Rossum452bf512007-02-09 05:32:43 +00002931 * expr_stmt | del_stmt | pass_stmt | flow_stmt
Georg Brandl7cae87c2006-09-06 06:51:57 +00002932 * | import_stmt | global_stmt | assert_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002933 */
2934 res = validate_small_stmt(tree);
2935 break;
2936 case flow_stmt:
2937 res = (validate_numnodes(tree, 1, "flow_stmt")
2938 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2939 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002940 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002941 || (TYPE(CHILD(tree, 0)) == return_stmt)
2942 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2943 if (res)
2944 next = CHILD(tree, 0);
2945 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002946 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002947 break;
Fred Drake02126f22001-07-17 02:59:15 +00002948 case yield_stmt:
2949 res = validate_yield_stmt(tree);
2950 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002951 /*
2952 * Compound statements.
2953 */
2954 case simple_stmt:
2955 res = validate_simple_stmt(tree);
2956 break;
2957 case compound_stmt:
2958 res = validate_compound_stmt(tree);
2959 break;
2960 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002961 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002962 */
2963 case expr_stmt:
2964 res = validate_expr_stmt(tree);
2965 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002966 case del_stmt:
2967 res = validate_del_stmt(tree);
2968 break;
2969 case pass_stmt:
2970 res = (validate_numnodes(tree, 1, "pass")
2971 && validate_name(CHILD(tree, 0), "pass"));
2972 break;
2973 case break_stmt:
2974 res = (validate_numnodes(tree, 1, "break")
2975 && validate_name(CHILD(tree, 0), "break"));
2976 break;
2977 case continue_stmt:
2978 res = (validate_numnodes(tree, 1, "continue")
2979 && validate_name(CHILD(tree, 0), "continue"));
2980 break;
2981 case return_stmt:
2982 res = validate_return_stmt(tree);
2983 break;
2984 case raise_stmt:
2985 res = validate_raise_stmt(tree);
2986 break;
2987 case import_stmt:
2988 res = validate_import_stmt(tree);
2989 break;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00002990 case import_name:
2991 res = validate_import_name(tree);
2992 break;
2993 case import_from:
2994 res = validate_import_from(tree);
2995 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002996 case global_stmt:
2997 res = validate_global_stmt(tree);
2998 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002999 case assert_stmt:
3000 res = validate_assert_stmt(tree);
3001 break;
3002 case if_stmt:
3003 res = validate_if(tree);
3004 break;
3005 case while_stmt:
3006 res = validate_while(tree);
3007 break;
3008 case for_stmt:
3009 res = validate_for(tree);
3010 break;
3011 case try_stmt:
3012 res = validate_try(tree);
3013 break;
3014 case suite:
3015 res = validate_suite(tree);
3016 break;
3017 /*
3018 * Expression nodes.
3019 */
3020 case testlist:
3021 res = validate_testlist(tree);
3022 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003023 case yield_expr:
3024 res = validate_yield_expr(tree);
3025 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00003026 case testlist1:
3027 res = validate_testlist1(tree);
3028 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003029 case test:
3030 res = validate_test(tree);
3031 break;
3032 case and_test:
3033 res = validate_and_test(tree);
3034 break;
3035 case not_test:
3036 res = validate_not_test(tree);
3037 break;
3038 case comparison:
3039 res = validate_comparison(tree);
3040 break;
3041 case exprlist:
3042 res = validate_exprlist(tree);
3043 break;
3044 case comp_op:
3045 res = validate_comp_op(tree);
3046 break;
3047 case expr:
3048 res = validate_expr(tree);
3049 break;
3050 case xor_expr:
3051 res = validate_xor_expr(tree);
3052 break;
3053 case and_expr:
3054 res = validate_and_expr(tree);
3055 break;
3056 case shift_expr:
3057 res = validate_shift_expr(tree);
3058 break;
3059 case arith_expr:
3060 res = validate_arith_expr(tree);
3061 break;
3062 case term:
3063 res = validate_term(tree);
3064 break;
3065 case factor:
3066 res = validate_factor(tree);
3067 break;
3068 case power:
3069 res = validate_power(tree);
3070 break;
3071 case atom:
3072 res = validate_atom(tree);
3073 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003074
Fred Drakeff9ea482000-04-19 13:54:15 +00003075 default:
3076 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00003077 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003078 res = 0;
3079 break;
3080 }
3081 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003082 }
3083 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003084}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003085
3086
Guido van Rossum47478871996-08-21 14:32:37 +00003087static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003088validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003089{
3090 int res = validate_eval_input(tree);
3091
3092 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003093 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003094
3095 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003096}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003097
3098
Guido van Rossum3d602e31996-07-21 02:33:56 +00003099/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003100 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003101 */
Guido van Rossum47478871996-08-21 14:32:37 +00003102static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003103validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003104{
Fred Drakec2683dd2001-07-17 19:32:05 +00003105 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003106 int nch = NCH(tree) - 1;
3107 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003108 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003109
Fred Drakec2683dd2001-07-17 19:32:05 +00003110 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003111 if (TYPE(CHILD(tree, j)) == stmt)
3112 res = validate_stmt(CHILD(tree, j));
3113 else
3114 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003115 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003116 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003117 * user. Hopefully, this won't be needed. If a user reports getting
3118 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003119 */
3120 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003121 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003122
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003123 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003124}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003125
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003126static int
3127validate_encoding_decl(node *tree)
3128{
3129 int nch = NCH(tree);
3130 int res = ((nch == 1)
3131 && validate_file_input(CHILD(tree, 0)));
3132
3133 if (!res && !PyErr_Occurred())
3134 err_string("Error Parsing encoding_decl");
3135
3136 return res;
3137}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003138
Fred Drake43f8f9b1998-04-13 16:25:46 +00003139static PyObject*
3140pickle_constructor = NULL;
3141
3142
3143static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003144parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003145{
Fred Drake268397f1998-04-29 14:16:32 +00003146 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003147 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003148 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003149 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003150
Fred Drakec2683dd2001-07-17 19:32:05 +00003151 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003152 PyObject *newargs;
3153 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003154
Fred Drake2a6875e1999-09-20 22:32:18 +00003155 if ((empty_dict = PyDict_New()) == NULL)
3156 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003157 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003158 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003159 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003160 if (tuple != NULL) {
3161 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3162 Py_DECREF(tuple);
3163 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003164 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003165 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003166 }
3167 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003168 Py_XDECREF(empty_dict);
3169
Fred Drake43f8f9b1998-04-13 16:25:46 +00003170 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003171}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003172
3173
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003174/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003175 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003176 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003177 * We'd really have to write a wrapper around it all anyway to allow
3178 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003179 */
3180static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003181 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003182 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003183 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003184 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003185 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003186 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003187 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003188 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003189 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003190 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003191 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003192 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003193 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003194 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003195 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003196 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003197 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003198 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003199 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003200 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003201 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003202 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003203 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003204 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003205 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003206 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003207 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003208 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003209
Fred Drake43f8f9b1998-04-13 16:25:46 +00003210 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003211 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003212 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003213
Fred Drake268397f1998-04-29 14:16:32 +00003214 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003215 };
3216
3217
Mark Hammond62b1ab12002-07-23 06:31:15 +00003218PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003219
Mark Hammond62b1ab12002-07-23 06:31:15 +00003220PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00003221initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003222{
Fred Drake13130bc2001-08-15 16:44:56 +00003223 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003224
3225 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00003226 module = Py_InitModule("parser", parser_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003227 if (module == NULL)
3228 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003229
Fred Drake7a15ba51999-09-09 14:21:52 +00003230 if (parser_error == 0)
3231 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003232
Tim Peters6a627252003-07-21 14:25:23 +00003233 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00003234 /* caller will check PyErr_Occurred() */
3235 return;
Tim Peters6a627252003-07-21 14:25:23 +00003236 /* CAUTION: The code next used to skip bumping the refcount on
3237 * parser_error. That's a disaster if initparser() gets called more
3238 * than once. By incref'ing, we ensure that each module dict that
3239 * gets created owns its reference to the shared parser_error object,
3240 * and the file static parser_error vrbl owns a reference too.
3241 */
3242 Py_INCREF(parser_error);
3243 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3244 return;
3245
Fred Drakec2683dd2001-07-17 19:32:05 +00003246 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003247 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00003248 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003249 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003250
Fred Drake13130bc2001-08-15 16:44:56 +00003251 PyModule_AddStringConstant(module, "__copyright__",
3252 parser_copyright_string);
3253 PyModule_AddStringConstant(module, "__doc__",
3254 parser_doc_string);
3255 PyModule_AddStringConstant(module, "__version__",
3256 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003257
Fred Drake78bdb9b2001-07-19 20:17:15 +00003258 /* Register to support pickling.
3259 * If this fails, the import of this module will fail because an
3260 * exception will be raised here; should we clear the exception?
3261 */
Fred Drake13130bc2001-08-15 16:44:56 +00003262 copyreg = PyImport_ImportModule("copy_reg");
3263 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003264 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003265
Fred Drake13130bc2001-08-15 16:44:56 +00003266 func = PyObject_GetAttrString(copyreg, "pickle");
3267 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3268 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003269 Py_XINCREF(pickle_constructor);
3270 if ((func != NULL) && (pickle_constructor != NULL)
3271 && (pickler != NULL)) {
3272 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003273
Thomas Wouters477c8d52006-05-27 19:21:47 +00003274 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3275 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003276 Py_XDECREF(res);
3277 }
3278 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003279 Py_XDECREF(pickle_constructor);
3280 Py_XDECREF(pickler);
3281 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003282 }
Fred Drakeff9ea482000-04-19 13:54:15 +00003283}