blob: 0dcb50f3945fb2664b100813994c5bac91df5831 [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. */
77 int lineno) /* include line numbers? */
Guido van Rossum47478871996-08-21 14:32:37 +000078{
Guido van Rossum3d602e31996-07-21 02:33:56 +000079 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000080 Py_INCREF(Py_None);
81 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000082 }
83 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000084 int i;
85 PyObject *v;
86 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000087
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000088 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000089 if (v == NULL)
90 return (v);
91 w = PyInt_FromLong(TYPE(n));
92 if (w == NULL) {
93 Py_DECREF(v);
94 return ((PyObject*) NULL);
95 }
96 (void) addelem(v, 0, w);
97 for (i = 0; i < NCH(n); i++) {
98 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno);
99 if (w == NULL) {
100 Py_DECREF(v);
101 return ((PyObject*) NULL);
102 }
103 (void) addelem(v, i+1, w);
104 }
Tim Peters6a627252003-07-21 14:25:23 +0000105
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000106 if (TYPE(n) == encoding_decl)
107 (void) addelem(v, i+1, PyString_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000108 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000109 }
110 else if (ISTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000111 PyObject *result = mkseq(2 + lineno);
112 if (result != NULL) {
113 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
114 (void) addelem(result, 1, PyString_FromString(STR(n)));
115 if (lineno == 1)
116 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
117 }
118 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000119 }
120 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000121 PyErr_SetString(PyExc_SystemError,
122 "unrecognized parse tree node type");
123 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000124 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000125}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000126/*
127 * End of material copyrighted by Stichting Mathematisch Centrum.
128 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000129
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000130
131
132/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000133 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000134 * field of the object type to identify which any given object represents.
135 * These should probably go in an external header to allow other extensions
136 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000137 */
138
Fred Drakec2683dd2001-07-17 19:32:05 +0000139#define PyST_EXPR 1
140#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000141
142
143/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000144 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000145 * naming style, but the code uses the new naming convention.
146 */
147
148static PyObject*
149parser_error = 0;
150
151
Fred Drakec2683dd2001-07-17 19:32:05 +0000152typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000153 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000154 node* st_node; /* the node* returned by the parser */
155 int st_type; /* EXPR or SUITE ? */
156} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000157
158
Jeremy Hylton938ace62002-07-17 16:30:39 +0000159static void parser_free(PyST_Object *st);
160static int parser_compare(PyST_Object *left, PyST_Object *right);
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000161static PyObject *parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000162
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000163
Fred Drake268397f1998-04-29 14:16:32 +0000164static
Fred Drakec2683dd2001-07-17 19:32:05 +0000165PyTypeObject PyST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000166 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000167 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000168 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000169 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000170 0, /* tp_itemsize */
171 (destructor)parser_free, /* tp_dealloc */
172 0, /* tp_print */
173 parser_getattr, /* tp_getattr */
174 0, /* tp_setattr */
175 (cmpfunc)parser_compare, /* tp_compare */
176 0, /* tp_repr */
177 0, /* tp_as_number */
178 0, /* tp_as_sequence */
179 0, /* tp_as_mapping */
180 0, /* tp_hash */
181 0, /* tp_call */
182 0, /* tp_str */
183 0, /* tp_getattro */
184 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000185
186 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000187 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000188
Fred Drakeff9ea482000-04-19 13:54:15 +0000189 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000190
191 /* __doc__ */
192 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000193}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000194
195
196static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000197parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000198{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000199 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000200
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000201 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000202 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000203
204 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000205 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000206
207 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000208 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000209
210 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000211 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000212
213 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000214 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000215
216 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000217 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000218
Fred Drakeff9ea482000-04-19 13:54:15 +0000219 if (v != 0)
220 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000221 }
222 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000223}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000224
225
Fred Drakec2683dd2001-07-17 19:32:05 +0000226/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000227 *
228 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
229 * This really just wraps a call to parser_compare_nodes() with some easy
230 * checks and protection code.
231 *
232 */
233static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000234parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000235{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000236 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000237 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000238
239 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000240 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000241
Fred Drakec2683dd2001-07-17 19:32:05 +0000242 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000243}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000244
245
Fred Drakec2683dd2001-07-17 19:32:05 +0000246/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000248 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000249 * 'wrapper' object that holds a node* and allows it to be passed around in
250 * Python code.
251 *
252 */
253static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000254parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000255{
Fred Drakec2683dd2001-07-17 19:32:05 +0000256 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000257
258 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000259 o->st_node = st;
260 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000261 }
Fred Drake268397f1998-04-29 14:16:32 +0000262 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000263 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000264 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000266}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000267
268
Fred Drakec2683dd2001-07-17 19:32:05 +0000269/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000270 *
271 * This is called by a del statement that reduces the reference count to 0.
272 *
273 */
274static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000275parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000276{
Fred Drakec2683dd2001-07-17 19:32:05 +0000277 PyNode_Free(st->st_node);
278 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000279}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000280
281
Fred Drakec2683dd2001-07-17 19:32:05 +0000282/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000283 *
284 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000285 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000286 *
287 */
288static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000289parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000290{
Guido van Rossum47478871996-08-21 14:32:37 +0000291 PyObject *line_option = 0;
292 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000293 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000294
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000295 static char *keywords[] = {"ast", "line_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000296
Fred Drake268397f1998-04-29 14:16:32 +0000297 if (self == NULL) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000298 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2tuple", keywords,
299 &PyST_Type, &self, &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000300 }
Fred Drake503d8d61998-04-13 18:45:18 +0000301 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000302 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:totuple", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000303 &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000304 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000305 int lineno = 0;
306 if (line_option != NULL) {
307 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
308 }
309 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000310 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000311 * since it's known to work already.
312 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000313 res = node2tuple(((PyST_Object*)self)->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000314 PyTuple_New, PyTuple_SetItem, lineno);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000315 }
316 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000317}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000318
319
Fred Drakec2683dd2001-07-17 19:32:05 +0000320/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000321 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000322 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000323 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000324 *
325 */
326static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000327parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000328{
Guido van Rossum47478871996-08-21 14:32:37 +0000329 PyObject *line_option = 0;
330 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000331 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000332
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000333 static char *keywords[] = {"ast", "line_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000334
Fred Drake503d8d61998-04-13 18:45:18 +0000335 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000336 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2list", keywords,
337 &PyST_Type, &self, &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000338 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000339 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:tolist", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000340 &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000341 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000342 int lineno = 0;
343 if (line_option != 0) {
344 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
345 }
346 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000347 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000348 * since it's known to work already.
349 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000350 res = node2tuple(self->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000351 PyList_New, PyList_SetItem, lineno);
Guido van Rossum47478871996-08-21 14:32:37 +0000352 }
353 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000354}
Guido van Rossum47478871996-08-21 14:32:37 +0000355
356
Fred Drakec2683dd2001-07-17 19:32:05 +0000357/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000358 *
359 * This function creates code objects from the parse tree represented by
360 * the passed-in data object. An optional file name is passed in as well.
361 *
362 */
363static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000364parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000365{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000366 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000367 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000368 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000369
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000370 static char *keywords[] = {"ast", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000371
Fred Drake503d8d61998-04-13 18:45:18 +0000372 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000373 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
374 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000375 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000376 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000377 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000378
379 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000380 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000381
382 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000383}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000384
385
386/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
387 * PyObject* parser_issuite(PyObject* self, PyObject* args)
388 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000389 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000390 * a statement suite, respectively. The return is a Python truth value.
391 *
392 */
393static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000394parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000395{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000396 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000397 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000398
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000399 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000400
Fred Drake503d8d61998-04-13 18:45:18 +0000401 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000402 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000403 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000404 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000405 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000406
407 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000408 /* Check to see if the ST represents an expression or not. */
409 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000410 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000411 }
412 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000413}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000414
415
416static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000417parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000418{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000419 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000420 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000421
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000422 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000423
Fred Drake503d8d61998-04-13 18:45:18 +0000424 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000425 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000426 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000427 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000428 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000429
430 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000431 /* Check to see if the ST represents an expression or not. */
432 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000433 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000434 }
435 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000436}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000437
438
Fred Drake7a15ba51999-09-09 14:21:52 +0000439#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
440
Fred Drake503d8d61998-04-13 18:45:18 +0000441static PyMethodDef
442parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000443 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000444 PyDoc_STR("Compile this ST object into a code object.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000445 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000446 PyDoc_STR("Determines if this ST object was created from an expression.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000447 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000448 PyDoc_STR("Determines if this ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000449 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000450 PyDoc_STR("Creates a list-tree representation of this ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000451 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000452 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Fred Drake503d8d61998-04-13 18:45:18 +0000453
Fred Drake268397f1998-04-29 14:16:32 +0000454 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000455};
456
Fred Drake503d8d61998-04-13 18:45:18 +0000457
458static PyObject*
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000459parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000460{
Fred Drake503d8d61998-04-13 18:45:18 +0000461 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000462}
Fred Drake503d8d61998-04-13 18:45:18 +0000463
464
Guido van Rossum3d602e31996-07-21 02:33:56 +0000465/* err_string(char* message)
466 *
467 * Sets the error string for an exception of type ParserError.
468 *
469 */
470static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000471err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000472{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000473 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000474}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000475
476
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000477/* PyObject* parser_do_parse(PyObject* args, int type)
478 *
479 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000480 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000481 *
482 */
483static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000484parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000485{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000486 char* string = 0;
487 PyObject* res = 0;
488
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000489 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000490
491 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000492 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000493 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000494 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000495
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000496 if (n)
497 res = parser_newstobject(n, type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000498 }
499 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000500}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000501
502
503/* PyObject* parser_expr(PyObject* self, PyObject* args)
504 * PyObject* parser_suite(PyObject* self, PyObject* args)
505 *
506 * External interfaces to the parser itself. Which is called determines if
507 * the parser attempts to recognize an expression ('eval' form) or statement
508 * suite ('exec' form). The real work is done by parser_do_parse() above.
509 *
510 */
511static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000512parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000513{
Fred Drake268397f1998-04-29 14:16:32 +0000514 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000515 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000516}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000517
518
519static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000520parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000521{
Fred Drake268397f1998-04-29 14:16:32 +0000522 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000523 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000524}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000525
526
527
Fred Drakec2683dd2001-07-17 19:32:05 +0000528/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000529 * object requires that the input tuple be valid without having to rely on
530 * catching an exception from the compiler. This is done to allow the
531 * compiler itself to remain fast, since most of its input will come from
532 * the parser directly, and therefore be known to be syntactically correct.
533 * This validation is done to ensure that we don't core dump the compile
534 * phase, returning an exception instead.
535 *
536 * Two aspects can be broken out in this code: creating a node tree from
537 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000538 * advantageous to expand the number of ST types to include funcdefs and
539 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000540 * here. They are not necessary, and not quite as useful in a raw form.
541 * For now, let's get expressions and suites working reliably.
542 */
543
544
Jeremy Hylton938ace62002-07-17 16:30:39 +0000545static node* build_node_tree(PyObject *tuple);
546static int validate_expr_tree(node *tree);
547static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000548static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000549
Fred Drakec2683dd2001-07-17 19:32:05 +0000550/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000551 *
552 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000553 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000554 * tuple can be validated. It does this by checking the first code of the
555 * tuple, and, if acceptable, builds the internal representation. If this
556 * step succeeds, the internal representation is validated as fully as
557 * possible with the various validate_*() routines defined below.
558 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000559 * This function must be changed if support is to be added for PyST_FRAGMENT
560 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000561 *
562 */
563static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000564parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000565{
Fred Drake268397f1998-04-29 14:16:32 +0000566 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000567 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000568 PyObject *tuple;
569 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000570
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000571 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000572
Fred Drakec2683dd2001-07-17 19:32:05 +0000573 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000574 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000575 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000576 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000577 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000578 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000579 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000580 }
581 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000582 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000583 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000584 tree = build_node_tree(tuple);
585 if (tree != 0) {
586 int start_sym = TYPE(tree);
587 if (start_sym == eval_input) {
588 /* Might be an eval form. */
589 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000590 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000591 else
592 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000593 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000594 else if (start_sym == file_input) {
595 /* This looks like an exec form so far. */
596 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000597 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000598 else
599 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000600 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000601 else if (start_sym == encoding_decl) {
602 /* This looks like an encoding_decl so far. */
603 if (validate_encoding_decl(tree))
604 st = parser_newstobject(tree, PyST_SUITE);
605 else
606 PyNode_Free(tree);
607 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000608 else {
609 /* This is a fragment, at best. */
610 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000611 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000612 }
Guido van Rossum47478871996-08-21 14:32:37 +0000613 }
Guido van Rossum47478871996-08-21 14:32:37 +0000614 /* Make sure we throw an exception on all errors. We should never
615 * get this, but we'd do well to be sure something is done.
616 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000617 if (st == NULL && !PyErr_Occurred())
618 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000619
Fred Drakec2683dd2001-07-17 19:32:05 +0000620 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000621}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000622
623
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000624/* node* build_node_children()
625 *
626 * Iterate across the children of the current non-terminal node and build
627 * their structures. If successful, return the root of this portion of
628 * the tree, otherwise, 0. Any required exception will be specified already,
629 * and no memory will have been deallocated.
630 *
631 */
632static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000633build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000634{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000635 Py_ssize_t len = PyObject_Size(tuple);
636 Py_ssize_t i;
637 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000638
639 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000640 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000641 PyObject* elem = PySequence_GetItem(tuple, i);
642 int ok = elem != NULL;
643 long type = 0;
644 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000645
Fred Drakeff9ea482000-04-19 13:54:15 +0000646 if (ok)
647 ok = PySequence_Check(elem);
648 if (ok) {
649 PyObject *temp = PySequence_GetItem(elem, 0);
650 if (temp == NULL)
651 ok = 0;
652 else {
653 ok = PyInt_Check(temp);
654 if (ok)
655 type = PyInt_AS_LONG(temp);
656 Py_DECREF(temp);
657 }
658 }
659 if (!ok) {
660 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000661 Py_BuildValue("os", elem,
Fred Drakeff9ea482000-04-19 13:54:15 +0000662 "Illegal node construct."));
663 Py_XDECREF(elem);
664 return (0);
665 }
666 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000667 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000668 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000669
Fred Drake0ac9b072000-09-12 21:58:06 +0000670 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000671 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000672 return 0;
673 }
674 temp = PySequence_GetItem(elem, 1);
675 if (temp == NULL)
676 return 0;
677 if (!PyString_Check(temp)) {
678 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000679 "second item in terminal node must be a string,"
680 " found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000681 temp->ob_type->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000682 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000683 return 0;
684 }
685 if (len == 3) {
686 PyObject *o = PySequence_GetItem(elem, 2);
687 if (o != NULL) {
688 if (PyInt_Check(o))
689 *line_num = PyInt_AS_LONG(o);
690 else {
691 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000692 "third item in terminal node must be an"
693 " integer, found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000694 temp->ob_type->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000695 Py_DECREF(o);
696 Py_DECREF(temp);
697 return 0;
698 }
699 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000700 }
701 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000702 len = PyString_GET_SIZE(temp) + 1;
703 strn = (char *)PyMem_MALLOC(len);
704 if (strn != NULL)
705 (void) memcpy(strn, PyString_AS_STRING(temp), len);
706 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000707 }
708 else if (!ISNONTERMINAL(type)) {
709 /*
710 * It has to be one or the other; this is an error.
711 * Throw an exception.
712 */
713 PyErr_SetObject(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000714 Py_BuildValue("os", elem, "unknown node type."));
Fred Drakeff9ea482000-04-19 13:54:15 +0000715 Py_XDECREF(elem);
716 return (0);
717 }
Fred Drake8b55b2d2001-12-05 22:10:44 +0000718 err = PyNode_AddChild(root, type, strn, *line_num);
719 if (err == E_NOMEM) {
720 PyMem_DEL(strn);
721 return (node *) PyErr_NoMemory();
722 }
723 if (err == E_OVERFLOW) {
724 PyMem_DEL(strn);
725 PyErr_SetString(PyExc_ValueError,
726 "unsupported number of child nodes");
727 return NULL;
728 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000729
Fred Drakeff9ea482000-04-19 13:54:15 +0000730 if (ISNONTERMINAL(type)) {
731 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000732
Fred Drakeff9ea482000-04-19 13:54:15 +0000733 if (new_child != build_node_children(elem, new_child, line_num)) {
734 Py_XDECREF(elem);
735 return (0);
736 }
737 }
738 else if (type == NEWLINE) { /* It's true: we increment the */
739 ++(*line_num); /* line number *after* the newline! */
740 }
741 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000742 }
743 return (root);
Fred Drakeff9ea482000-04-19 13:54:15 +0000744}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000745
746
747static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000748build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000749{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000750 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000751 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000752 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000753
Guido van Rossum47478871996-08-21 14:32:37 +0000754 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000755 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000756 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000757 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000758 /*
759 * The tuple is simple, but it doesn't start with a start symbol.
760 * Throw an exception now and be done with it.
761 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000762 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000763 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000764 PyErr_SetObject(parser_error, tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000765 }
766 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000767 /*
768 * Not efficient, but that can be handled later.
769 */
770 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000771 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000772
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000773 if (num == encoding_decl) {
774 encoding = PySequence_GetItem(tuple, 2);
775 /* tuple isn't borrowed anymore here, need to DECREF */
776 tuple = PySequence_GetSlice(tuple, 0, 2);
777 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000778 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000779 if (res != NULL) {
780 if (res != build_node_children(tuple, res, &line_num)) {
781 PyNode_Free(res);
782 res = NULL;
783 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000784 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000785 Py_ssize_t len;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000786 len = PyString_GET_SIZE(encoding) + 1;
787 res->n_str = (char *)PyMem_MALLOC(len);
788 if (res->n_str != NULL)
789 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
790 Py_DECREF(encoding);
791 Py_DECREF(tuple);
792 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000793 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000794 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000795 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000796 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000797 * NONTERMINAL, we can't use it. Not sure the implementation
798 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000799 */
800 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000801 Py_BuildValue("os", tuple,
Fred Drakeff9ea482000-04-19 13:54:15 +0000802 "Illegal component tuple."));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000803
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000804 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000805}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000806
807
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000808/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000809 * Validation routines used within the validation section:
810 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000811static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000812
Fred Drakeff9ea482000-04-19 13:54:15 +0000813#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
814#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
815#define validate_colon(ch) validate_terminal(ch, COLON, ":")
816#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
817#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
818#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
819#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
820#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
821#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
822#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
823#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
824#define validate_star(ch) validate_terminal(ch, STAR, "*")
825#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
826#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
827#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000828#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000829#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000830
Fred Drake0ac9b072000-09-12 21:58:06 +0000831#define VALIDATER(n) static int validate_##n(node *tree)
832
Fred Drakeff9ea482000-04-19 13:54:15 +0000833VALIDATER(node); VALIDATER(small_stmt);
834VALIDATER(class); VALIDATER(node);
835VALIDATER(parameters); VALIDATER(suite);
836VALIDATER(testlist); VALIDATER(varargslist);
837VALIDATER(fpdef); VALIDATER(fplist);
838VALIDATER(stmt); VALIDATER(simple_stmt);
839VALIDATER(expr_stmt); VALIDATER(power);
840VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000841VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000842VALIDATER(raise_stmt); VALIDATER(import_stmt);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000843VALIDATER(import_name); VALIDATER(import_from);
Fred Drakecff283c2000-08-21 22:24:43 +0000844VALIDATER(global_stmt); VALIDATER(list_if);
845VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000846VALIDATER(exec_stmt); VALIDATER(compound_stmt);
847VALIDATER(while); VALIDATER(for);
848VALIDATER(try); VALIDATER(except_clause);
849VALIDATER(test); VALIDATER(and_test);
850VALIDATER(not_test); VALIDATER(comparison);
851VALIDATER(comp_op); VALIDATER(expr);
852VALIDATER(xor_expr); VALIDATER(and_expr);
853VALIDATER(shift_expr); VALIDATER(arith_expr);
854VALIDATER(term); VALIDATER(factor);
855VALIDATER(atom); VALIDATER(lambdef);
856VALIDATER(trailer); VALIDATER(subscript);
857VALIDATER(subscriptlist); VALIDATER(sliceop);
858VALIDATER(exprlist); VALIDATER(dictmaker);
859VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000860VALIDATER(listmaker); VALIDATER(yield_stmt);
Raymond Hettinger354433a2004-05-19 08:20:33 +0000861VALIDATER(testlist1); VALIDATER(gen_for);
862VALIDATER(gen_iter); VALIDATER(gen_if);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000863VALIDATER(testlist_gexp); VALIDATER(yield_expr);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +0000864VALIDATER(yield_or_testlist); VALIDATER(or_test);
865VALIDATER(old_test); VALIDATER(old_lambdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000866
Fred Drake0ac9b072000-09-12 21:58:06 +0000867#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000868
Fred Drakeff9ea482000-04-19 13:54:15 +0000869#define is_even(n) (((n) & 1) == 0)
870#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000871
872
873static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000874validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000875{
Fred Drake0ac9b072000-09-12 21:58:06 +0000876 if (TYPE(n) != t) {
877 PyErr_Format(parser_error, "Expected node type %d, got %d.",
878 t, TYPE(n));
879 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000880 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000881 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000882}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000883
884
Fred Drakee7ab64e2000-04-25 04:14:46 +0000885/* Verifies that the number of child nodes is exactly 'num', raising
886 * an exception if it isn't. The exception message does not indicate
887 * the exact number of nodes, allowing this to be used to raise the
888 * "right" exception when the wrong number of nodes is present in a
889 * specific variant of a statement's syntax. This is commonly used
890 * in that fashion.
891 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000892static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000893validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000894{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000895 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000896 PyErr_Format(parser_error,
897 "Illegal number of children for %s node.", name);
898 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000899 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000900 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000901}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000902
903
904static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000905validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000906{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000907 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000908 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000909
910 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000911 PyErr_Format(parser_error,
912 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000913 }
914 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000915}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000916
917
Guido van Rossum47478871996-08-21 14:32:37 +0000918/* X (',' X) [',']
919 */
920static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000921validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000922 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000923{
924 int nch = NCH(tree);
925 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000926 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000927
928 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000929 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000930 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000931 if (is_even(nch))
932 res = validate_comma(CHILD(tree, --nch));
933 if (res && nch > 1) {
934 int pos = 1;
935 for ( ; res && pos < nch; pos += 2)
936 res = (validate_comma(CHILD(tree, pos))
937 && vfunc(CHILD(tree, pos + 1)));
938 }
Guido van Rossum47478871996-08-21 14:32:37 +0000939 }
940 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000941}
Guido van Rossum47478871996-08-21 14:32:37 +0000942
943
Fred Drakecff283c2000-08-21 22:24:43 +0000944/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000945 *
946 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000947 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000948 */
Guido van Rossum47478871996-08-21 14:32:37 +0000949static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000950validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000951{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000952 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +0000953 int res = (validate_ntype(tree, classdef) &&
954 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000955
Guido van Rossum3d602e31996-07-21 02:33:56 +0000956 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000957 res = (validate_name(CHILD(tree, 0), "class")
958 && validate_ntype(CHILD(tree, 1), NAME)
959 && validate_colon(CHILD(tree, nch - 2))
960 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000961 }
Brett Cannonf4189912005-04-09 02:30:16 +0000962 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000963 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +0000964 }
965
966 if (res) {
967 if (nch == 7) {
968 res = ((validate_lparen(CHILD(tree, 2)) &&
969 validate_testlist(CHILD(tree, 3)) &&
970 validate_rparen(CHILD(tree, 4))));
971 }
972 else if (nch == 6) {
973 res = (validate_lparen(CHILD(tree,2)) &&
974 validate_rparen(CHILD(tree,3)));
975 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000976 }
977 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000978}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000979
980
Guido van Rossum3d602e31996-07-21 02:33:56 +0000981/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +0000982 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +0000983 */
Guido van Rossum47478871996-08-21 14:32:37 +0000984static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000985validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000986{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000987 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000988 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +0000989 && (nch >= 4)
990 && validate_name(CHILD(tree, 0), "if")
991 && validate_test(CHILD(tree, 1))
992 && validate_colon(CHILD(tree, 2))
993 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000994
995 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000996 /* ... 'else' ':' suite */
997 res = (validate_name(CHILD(tree, nch - 3), "else")
998 && validate_colon(CHILD(tree, nch - 2))
999 && validate_suite(CHILD(tree, nch - 1)));
1000 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001001 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001002 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001003 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001004 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001005 /* Will catch the case for nch < 4 */
1006 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001007 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001008 /* ... ('elif' test ':' suite)+ ... */
1009 int j = 4;
1010 while ((j < nch) && res) {
1011 res = (validate_name(CHILD(tree, j), "elif")
1012 && validate_colon(CHILD(tree, j + 2))
1013 && validate_test(CHILD(tree, j + 1))
1014 && validate_suite(CHILD(tree, j + 3)));
1015 j += 4;
1016 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001017 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001018 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001019}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001020
1021
Guido van Rossum3d602e31996-07-21 02:33:56 +00001022/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001023 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001024 *
1025 */
Guido van Rossum47478871996-08-21 14:32:37 +00001026static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001027validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001028{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001029 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001030 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001031
Guido van Rossum3d602e31996-07-21 02:33:56 +00001032 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001033 res = (validate_lparen(CHILD(tree, 0))
1034 && validate_rparen(CHILD(tree, nch - 1)));
1035 if (res && (nch == 3))
1036 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001037 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001038 else {
1039 (void) validate_numnodes(tree, 2, "parameters");
1040 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001041 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001042}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001043
1044
Fred Drakecff283c2000-08-21 22:24:43 +00001045/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001046 *
1047 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001048 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001049 * | NEWLINE INDENT stmt+ DEDENT
1050 */
Guido van Rossum47478871996-08-21 14:32:37 +00001051static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001052validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001053{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001054 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001055 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001056
Guido van Rossum3d602e31996-07-21 02:33:56 +00001057 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001058 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001059 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001060 /* NEWLINE INDENT stmt+ DEDENT */
1061 res = (validate_newline(CHILD(tree, 0))
1062 && validate_indent(CHILD(tree, 1))
1063 && validate_stmt(CHILD(tree, 2))
1064 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001065
Fred Drakeff9ea482000-04-19 13:54:15 +00001066 if (res && (nch > 4)) {
1067 int i = 3;
1068 --nch; /* forget the DEDENT */
1069 for ( ; res && (i < nch); ++i)
1070 res = validate_stmt(CHILD(tree, i));
1071 }
1072 else if (nch < 4)
1073 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001074 }
1075 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001076}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001077
1078
Guido van Rossum47478871996-08-21 14:32:37 +00001079static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001080validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001081{
Guido van Rossum47478871996-08-21 14:32:37 +00001082 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001083 validate_test, "testlist"));
1084}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001085
1086
Guido van Rossum1c917072001-10-15 15:44:05 +00001087static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001088validate_testlist1(node *tree)
1089{
1090 return (validate_repeating_list(tree, testlist1,
1091 validate_test, "testlist1"));
1092}
1093
1094
1095static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001096validate_testlist_safe(node *tree)
1097{
1098 return (validate_repeating_list(tree, testlist_safe,
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001099 validate_old_test, "testlist_safe"));
Guido van Rossum1c917072001-10-15 15:44:05 +00001100}
1101
1102
Fred Drakecff283c2000-08-21 22:24:43 +00001103/* '*' NAME [',' '**' NAME] | '**' NAME
1104 */
1105static int
1106validate_varargslist_trailer(node *tree, int start)
1107{
1108 int nch = NCH(tree);
1109 int res = 0;
1110 int sym;
1111
1112 if (nch <= start) {
1113 err_string("expected variable argument trailer for varargslist");
1114 return 0;
1115 }
1116 sym = TYPE(CHILD(tree, start));
1117 if (sym == STAR) {
1118 /*
1119 * ('*' NAME [',' '**' NAME]
1120 */
1121 if (nch-start == 2)
1122 res = validate_name(CHILD(tree, start+1), NULL);
1123 else if (nch-start == 5)
1124 res = (validate_name(CHILD(tree, start+1), NULL)
1125 && validate_comma(CHILD(tree, start+2))
1126 && validate_doublestar(CHILD(tree, start+3))
1127 && validate_name(CHILD(tree, start+4), NULL));
1128 }
1129 else if (sym == DOUBLESTAR) {
1130 /*
1131 * '**' NAME
1132 */
1133 if (nch-start == 2)
1134 res = validate_name(CHILD(tree, start+1), NULL);
1135 }
1136 if (!res)
1137 err_string("illegal variable argument trailer for varargslist");
1138 return res;
1139}
1140
1141
1142/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001143 *
1144 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001145 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001146 * ('*' NAME [',' '**' NAME]
1147 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001148 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1149 *
1150 */
Guido van Rossum47478871996-08-21 14:32:37 +00001151static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001152validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001153{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001154 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001155 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001156 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001157
Fred Drakeb6429a22000-12-11 22:08:27 +00001158 if (!res)
1159 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001160 if (nch < 1) {
1161 err_string("varargslist missing child nodes");
1162 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001163 }
Fred Drakecff283c2000-08-21 22:24:43 +00001164 sym = TYPE(CHILD(tree, 0));
1165 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001166 /* whole thing matches:
1167 * '*' NAME [',' '**' NAME] | '**' NAME
1168 */
Fred Drakecff283c2000-08-21 22:24:43 +00001169 res = validate_varargslist_trailer(tree, 0);
1170 else if (sym == fpdef) {
1171 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001172
Fred Drakecff283c2000-08-21 22:24:43 +00001173 sym = TYPE(CHILD(tree, nch-1));
1174 if (sym == NAME) {
1175 /*
1176 * (fpdef ['=' test] ',')+
1177 * ('*' NAME [',' '**' NAME]
1178 * | '**' NAME)
1179 */
1180 /* skip over (fpdef ['=' test] ',')+ */
1181 while (res && (i+2 <= nch)) {
1182 res = validate_fpdef(CHILD(tree, i));
1183 ++i;
1184 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1185 res = (validate_equal(CHILD(tree, i))
1186 && validate_test(CHILD(tree, i+1)));
1187 if (res)
1188 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001189 }
Fred Drakecff283c2000-08-21 22:24:43 +00001190 if (res && i < nch) {
1191 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001192 ++i;
1193 if (res && i < nch
1194 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1195 || TYPE(CHILD(tree, i)) == STAR))
1196 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001197 }
1198 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001199 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1200 * i --^^^
1201 */
Fred Drakecff283c2000-08-21 22:24:43 +00001202 if (res)
1203 res = validate_varargslist_trailer(tree, i);
1204 }
1205 else {
1206 /*
1207 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1208 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001209 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001210 if (sym == COMMA) {
1211 res = validate_comma(CHILD(tree, nch-1));
1212 if (!res)
1213 return 0;
1214 --nch;
1215 }
1216 /*
1217 * fpdef ['=' test] (',' fpdef ['=' test])*
1218 */
1219 res = validate_fpdef(CHILD(tree, 0));
1220 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001221 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1222 res = (validate_equal(CHILD(tree, i))
1223 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001224 i += 2;
1225 }
1226 /*
1227 * ... (',' fpdef ['=' test])*
1228 * i ---^^^
1229 */
1230 while (res && (nch - i) >= 2) {
1231 res = (validate_comma(CHILD(tree, i))
1232 && validate_fpdef(CHILD(tree, i+1)));
1233 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001234 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1235 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001236 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001237 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001238 }
1239 }
1240 if (res && nch - i != 0) {
1241 res = 0;
1242 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001243 }
1244 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001245 }
Fred Drakecff283c2000-08-21 22:24:43 +00001246 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001247}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001248
1249
Fred Drakecff283c2000-08-21 22:24:43 +00001250/* list_iter: list_for | list_if
1251 */
1252static int
1253validate_list_iter(node *tree)
1254{
1255 int res = (validate_ntype(tree, list_iter)
1256 && validate_numnodes(tree, 1, "list_iter"));
1257 if (res && TYPE(CHILD(tree, 0)) == list_for)
1258 res = validate_list_for(CHILD(tree, 0));
1259 else
1260 res = validate_list_if(CHILD(tree, 0));
1261
1262 return res;
1263}
1264
Raymond Hettinger354433a2004-05-19 08:20:33 +00001265/* gen_iter: gen_for | gen_if
1266 */
1267static int
1268validate_gen_iter(node *tree)
1269{
1270 int res = (validate_ntype(tree, gen_iter)
1271 && validate_numnodes(tree, 1, "gen_iter"));
1272 if (res && TYPE(CHILD(tree, 0)) == gen_for)
1273 res = validate_gen_for(CHILD(tree, 0));
1274 else
1275 res = validate_gen_if(CHILD(tree, 0));
1276
1277 return res;
1278}
1279
Fred Drakecff283c2000-08-21 22:24:43 +00001280/* list_for: 'for' exprlist 'in' testlist [list_iter]
1281 */
1282static int
1283validate_list_for(node *tree)
1284{
1285 int nch = NCH(tree);
1286 int res;
1287
1288 if (nch == 5)
1289 res = validate_list_iter(CHILD(tree, 4));
1290 else
1291 res = validate_numnodes(tree, 4, "list_for");
1292
1293 if (res)
1294 res = (validate_name(CHILD(tree, 0), "for")
1295 && validate_exprlist(CHILD(tree, 1))
1296 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001297 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001298
1299 return res;
1300}
1301
Raymond Hettinger354433a2004-05-19 08:20:33 +00001302/* gen_for: 'for' exprlist 'in' test [gen_iter]
1303 */
1304static int
1305validate_gen_for(node *tree)
1306{
1307 int nch = NCH(tree);
1308 int res;
1309
1310 if (nch == 5)
1311 res = validate_gen_iter(CHILD(tree, 4));
1312 else
1313 res = validate_numnodes(tree, 4, "gen_for");
1314
1315 if (res)
1316 res = (validate_name(CHILD(tree, 0), "for")
1317 && validate_exprlist(CHILD(tree, 1))
1318 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001319 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001320
1321 return res;
1322}
1323
Fred Drakecff283c2000-08-21 22:24:43 +00001324/* list_if: 'if' test [list_iter]
1325 */
1326static int
1327validate_list_if(node *tree)
1328{
1329 int nch = NCH(tree);
1330 int res;
1331
1332 if (nch == 3)
1333 res = validate_list_iter(CHILD(tree, 2));
1334 else
1335 res = validate_numnodes(tree, 2, "list_if");
1336
1337 if (res)
1338 res = (validate_name(CHILD(tree, 0), "if")
1339 && validate_test(CHILD(tree, 1)));
1340
1341 return res;
1342}
1343
Raymond Hettinger354433a2004-05-19 08:20:33 +00001344/* gen_if: 'if' test [gen_iter]
1345 */
1346static int
1347validate_gen_if(node *tree)
1348{
1349 int nch = NCH(tree);
1350 int res;
1351
1352 if (nch == 3)
1353 res = validate_gen_iter(CHILD(tree, 2));
1354 else
1355 res = validate_numnodes(tree, 2, "gen_if");
1356
1357 if (res)
1358 res = (validate_name(CHILD(tree, 0), "if")
1359 && validate_test(CHILD(tree, 1)));
1360
1361 return res;
1362}
Fred Drakecff283c2000-08-21 22:24:43 +00001363
1364/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001365 *
1366 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001367 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001368 * | '(' fplist ')'
1369 */
Guido van Rossum47478871996-08-21 14:32:37 +00001370static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001371validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001372{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001373 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001374 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001375
Guido van Rossum3d602e31996-07-21 02:33:56 +00001376 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001377 if (nch == 1)
1378 res = validate_ntype(CHILD(tree, 0), NAME);
1379 else if (nch == 3)
1380 res = (validate_lparen(CHILD(tree, 0))
1381 && validate_fplist(CHILD(tree, 1))
1382 && validate_rparen(CHILD(tree, 2)));
1383 else
1384 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001385 }
1386 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001387}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001388
1389
Guido van Rossum47478871996-08-21 14:32:37 +00001390static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001391validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001392{
Guido van Rossum47478871996-08-21 14:32:37 +00001393 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001394 validate_fpdef, "fplist"));
1395}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001396
1397
Guido van Rossum3d602e31996-07-21 02:33:56 +00001398/* simple_stmt | compound_stmt
1399 *
1400 */
Guido van Rossum47478871996-08-21 14:32:37 +00001401static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001402validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001403{
1404 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001405 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001406
Guido van Rossum3d602e31996-07-21 02:33:56 +00001407 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001408 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001409
Fred Drakeff9ea482000-04-19 13:54:15 +00001410 if (TYPE(tree) == simple_stmt)
1411 res = validate_simple_stmt(tree);
1412 else
1413 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001414 }
1415 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001416}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001417
1418
Guido van Rossum3d602e31996-07-21 02:33:56 +00001419/* small_stmt (';' small_stmt)* [';'] NEWLINE
1420 *
1421 */
Guido van Rossum47478871996-08-21 14:32:37 +00001422static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001423validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001424{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001425 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001426 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001427 && (nch >= 2)
1428 && validate_small_stmt(CHILD(tree, 0))
1429 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001430
Guido van Rossum3d602e31996-07-21 02:33:56 +00001431 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001432 res = validate_numnodes(tree, 2, "simple_stmt");
1433 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001434 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001435 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001436 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001437 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001438
Fred Drakeff9ea482000-04-19 13:54:15 +00001439 for (i = 1; res && (i < nch); i += 2)
1440 res = (validate_semi(CHILD(tree, i))
1441 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001442 }
1443 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001444}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001445
1446
Guido van Rossum47478871996-08-21 14:32:37 +00001447static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001448validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001449{
1450 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001451 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001452
Fred Drake0ac9b072000-09-12 21:58:06 +00001453 if (res) {
1454 int ntype = TYPE(CHILD(tree, 0));
1455
1456 if ( (ntype == expr_stmt)
1457 || (ntype == print_stmt)
1458 || (ntype == del_stmt)
1459 || (ntype == pass_stmt)
1460 || (ntype == flow_stmt)
1461 || (ntype == import_stmt)
1462 || (ntype == global_stmt)
1463 || (ntype == assert_stmt)
1464 || (ntype == exec_stmt))
1465 res = validate_node(CHILD(tree, 0));
1466 else {
1467 res = 0;
1468 err_string("illegal small_stmt child type");
1469 }
1470 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001471 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001472 res = 0;
1473 PyErr_Format(parser_error,
1474 "Unrecognized child node of small_stmt: %d.",
1475 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001476 }
1477 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001478}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001479
1480
1481/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001482 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001483 */
Guido van Rossum47478871996-08-21 14:32:37 +00001484static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001485validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001486{
1487 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001488 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001489 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001490
1491 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001492 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001493
1494 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001495 ntype = TYPE(tree);
1496 if ( (ntype == if_stmt)
1497 || (ntype == while_stmt)
1498 || (ntype == for_stmt)
1499 || (ntype == try_stmt)
1500 || (ntype == funcdef)
1501 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001502 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001503 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001504 res = 0;
1505 PyErr_Format(parser_error,
1506 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001507 }
1508 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001509}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001510
1511
Guido van Rossum47478871996-08-21 14:32:37 +00001512static int
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001513validate_yield_or_testlist(node *tree)
1514{
1515 if (TYPE(tree) == yield_expr)
1516 return validate_yield_expr(tree);
1517 else
1518 return validate_testlist(tree);
1519}
1520
1521static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001522validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001523{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001524 int j;
1525 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001526 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001527 && is_odd(nch)
1528 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001529
Fred Drake28f739a2000-08-25 22:42:40 +00001530 if (res && nch == 3
1531 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001532 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1533 && validate_yield_or_testlist(CHILD(tree, 2));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001534
Fred Drake28f739a2000-08-25 22:42:40 +00001535 if (res) {
1536 char *s = STR(CHILD(CHILD(tree, 1), 0));
1537
1538 res = (strcmp(s, "+=") == 0
1539 || strcmp(s, "-=") == 0
1540 || strcmp(s, "*=") == 0
1541 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001542 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001543 || strcmp(s, "%=") == 0
1544 || strcmp(s, "&=") == 0
1545 || strcmp(s, "|=") == 0
1546 || strcmp(s, "^=") == 0
1547 || strcmp(s, "<<=") == 0
1548 || strcmp(s, ">>=") == 0
1549 || strcmp(s, "**=") == 0);
1550 if (!res)
1551 err_string("illegal augmmented assignment operator");
1552 }
1553 }
1554 else {
1555 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001556 res = validate_equal(CHILD(tree, j))
1557 && validate_yield_or_testlist(CHILD(tree, j + 1));
Fred Drake28f739a2000-08-25 22:42:40 +00001558 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001559 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001560}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001561
1562
Guido van Rossum3d602e31996-07-21 02:33:56 +00001563/* print_stmt:
1564 *
Fred Drakecff283c2000-08-21 22:24:43 +00001565 * 'print' ( [ test (',' test)* [','] ]
1566 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001567 */
Guido van Rossum47478871996-08-21 14:32:37 +00001568static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001569validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001570{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001571 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001572 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001573 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001574 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001575
Fred Drakecff283c2000-08-21 22:24:43 +00001576 if (res && nch > 1) {
1577 int sym = TYPE(CHILD(tree, 1));
1578 int i = 1;
1579 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001580
Fred Drakecff283c2000-08-21 22:24:43 +00001581 if (sym == test)
1582 res = validate_test(CHILD(tree, i++));
1583 else {
1584 if (nch < 3)
1585 res = validate_numnodes(tree, 3, "print_stmt");
1586 else {
1587 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1588 && validate_test(CHILD(tree, i+1)));
1589 i += 2;
1590 allow_trailing_comma = 0;
1591 }
1592 }
1593 if (res) {
1594 /* ... (',' test)* [','] */
1595 while (res && i+2 <= nch) {
1596 res = (validate_comma(CHILD(tree, i))
1597 && validate_test(CHILD(tree, i+1)));
1598 allow_trailing_comma = 1;
1599 i += 2;
1600 }
1601 if (res && !allow_trailing_comma)
1602 res = validate_numnodes(tree, i, "print_stmt");
1603 else if (res && i < nch)
1604 res = validate_comma(CHILD(tree, i));
1605 }
1606 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001607 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001608}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001609
1610
Guido van Rossum47478871996-08-21 14:32:37 +00001611static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001612validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001613{
1614 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001615 && validate_name(CHILD(tree, 0), "del")
1616 && validate_exprlist(CHILD(tree, 1)));
1617}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001618
1619
Guido van Rossum47478871996-08-21 14:32:37 +00001620static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001621validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001622{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001623 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001624 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001625 && ((nch == 1) || (nch == 2))
1626 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001627
Guido van Rossum3d602e31996-07-21 02:33:56 +00001628 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001629 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001630
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001631 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001632}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001633
1634
Guido van Rossum47478871996-08-21 14:32:37 +00001635static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001636validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001637{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001638 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001639 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001640 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001641
Guido van Rossum3d602e31996-07-21 02:33:56 +00001642 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001643 res = validate_name(CHILD(tree, 0), "raise");
1644 if (res && (nch >= 2))
1645 res = validate_test(CHILD(tree, 1));
1646 if (res && nch > 2) {
1647 res = (validate_comma(CHILD(tree, 2))
1648 && validate_test(CHILD(tree, 3)));
1649 if (res && (nch > 4))
1650 res = (validate_comma(CHILD(tree, 4))
1651 && validate_test(CHILD(tree, 5)));
1652 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001653 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001654 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001655 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001656 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001657 res = (validate_comma(CHILD(tree, 2))
1658 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001659
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001660 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001661}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001662
1663
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001664/* yield_expr: 'yield' [testlist]
1665 */
1666static int
1667validate_yield_expr(node *tree)
1668{
1669 int nch = NCH(tree);
1670 int res = (validate_ntype(tree, yield_expr)
1671 && ((nch == 1) || (nch == 2))
1672 && validate_name(CHILD(tree, 0), "yield"));
1673
1674 if (res && (nch == 2))
1675 res = validate_testlist(CHILD(tree, 1));
1676
1677 return (res);
1678}
1679
1680
1681/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001682 */
1683static int
1684validate_yield_stmt(node *tree)
1685{
1686 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001687 && validate_numnodes(tree, 1, "yield_stmt")
1688 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001689}
1690
1691
Fred Drakecff283c2000-08-21 22:24:43 +00001692static int
1693validate_import_as_name(node *tree)
1694{
1695 int nch = NCH(tree);
1696 int ok = validate_ntype(tree, import_as_name);
1697
1698 if (ok) {
1699 if (nch == 1)
1700 ok = validate_name(CHILD(tree, 0), NULL);
1701 else if (nch == 3)
1702 ok = (validate_name(CHILD(tree, 0), NULL)
1703 && validate_name(CHILD(tree, 1), "as")
1704 && validate_name(CHILD(tree, 2), NULL));
1705 else
1706 ok = validate_numnodes(tree, 3, "import_as_name");
1707 }
1708 return ok;
1709}
1710
1711
Fred Drake71137082001-01-07 05:59:59 +00001712/* dotted_name: NAME ("." NAME)*
1713 */
1714static int
1715validate_dotted_name(node *tree)
1716{
1717 int nch = NCH(tree);
1718 int res = (validate_ntype(tree, dotted_name)
1719 && is_odd(nch)
1720 && validate_name(CHILD(tree, 0), NULL));
1721 int i;
1722
1723 for (i = 1; res && (i < nch); i += 2) {
1724 res = (validate_dot(CHILD(tree, i))
1725 && validate_name(CHILD(tree, i+1), NULL));
1726 }
1727 return res;
1728}
1729
1730
Fred Drakecff283c2000-08-21 22:24:43 +00001731/* dotted_as_name: dotted_name [NAME NAME]
1732 */
1733static int
1734validate_dotted_as_name(node *tree)
1735{
1736 int nch = NCH(tree);
1737 int res = validate_ntype(tree, dotted_as_name);
1738
1739 if (res) {
1740 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001741 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001742 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001743 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001744 && validate_name(CHILD(tree, 1), "as")
1745 && validate_name(CHILD(tree, 2), NULL));
1746 else {
1747 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001748 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001749 }
1750 }
1751 return res;
1752}
1753
1754
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001755/* dotted_as_name (',' dotted_as_name)* */
1756static int
1757validate_dotted_as_names(node *tree)
1758{
1759 int nch = NCH(tree);
1760 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1761 int i;
1762
1763 for (i = 1; res && (i < nch); i += 2)
1764 res = (validate_comma(CHILD(tree, i))
1765 && validate_dotted_as_name(CHILD(tree, i + 1)));
1766 return (res);
1767}
1768
1769
1770/* import_as_name (',' import_as_name)* [','] */
1771static int
1772validate_import_as_names(node *tree)
1773{
1774 int nch = NCH(tree);
1775 int res = validate_import_as_name(CHILD(tree, 0));
1776 int i;
1777
1778 for (i = 1; res && (i + 1 < nch); i += 2)
1779 res = (validate_comma(CHILD(tree, i))
1780 && validate_import_as_name(CHILD(tree, i + 1)));
1781 return (res);
1782}
1783
1784
1785/* 'import' dotted_as_names */
1786static int
1787validate_import_name(node *tree)
1788{
1789 return (validate_ntype(tree, import_name)
1790 && validate_numnodes(tree, 2, "import_name")
1791 && validate_name(CHILD(tree, 0), "import")
1792 && validate_dotted_as_names(CHILD(tree, 1)));
1793}
1794
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001795/* Helper function to count the number of leading dots in
1796 * 'from ...module import name'
1797 */
1798static int
1799count_from_dots(node *tree)
1800{
1801 int i;
1802 for (i = 0; i < NCH(tree); i++)
1803 if (TYPE(CHILD(tree, i)) != DOT)
1804 break;
1805 return i;
1806}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001807
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001808/* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001809 * import_as_names
Guido van Rossum3d602e31996-07-21 02:33:56 +00001810 */
Guido van Rossum47478871996-08-21 14:32:37 +00001811static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001812validate_import_from(node *tree)
1813{
1814 int nch = NCH(tree);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001815 int ndots = count_from_dots(tree);
1816 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1817 int offset = ndots + havename;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001818 int res = validate_ntype(tree, import_from)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001819 && (nch >= 4 + ndots)
1820 && validate_name(CHILD(tree, 0), "from")
1821 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1822 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001823
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001824 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1825 res = ((nch == offset + 5)
1826 && validate_lparen(CHILD(tree, offset + 2))
1827 && validate_import_as_names(CHILD(tree, offset + 3))
1828 && validate_rparen(CHILD(tree, offset + 4)));
1829 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1830 res = validate_import_as_names(CHILD(tree, offset + 2));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001831 return (res);
1832}
1833
1834
1835/* import_stmt: import_name | import_from */
1836static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001837validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001838{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001839 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001840 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001841
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001842 if (res) {
1843 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001844
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001845 if (ntype == import_name || ntype == import_from)
1846 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001847 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001848 res = 0;
1849 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001850 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001851 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001852 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001853 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001854 PyErr_Format(parser_error,
1855 "Unrecognized child node of import_stmt: %d.",
1856 TYPE(CHILD(tree, 0)));
1857 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001858 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001859}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001860
1861
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001862
1863
Guido van Rossum47478871996-08-21 14:32:37 +00001864static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001865validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001866{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001867 int j;
1868 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001869 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001870 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001871
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001872 if (!res && !PyErr_Occurred())
1873 err_string("illegal global statement");
1874
Guido van Rossum3d602e31996-07-21 02:33:56 +00001875 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001876 res = (validate_name(CHILD(tree, 0), "global")
1877 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001878 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001879 res = (validate_comma(CHILD(tree, j))
1880 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001881
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001882 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001883}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001884
1885
Guido van Rossum3d602e31996-07-21 02:33:56 +00001886/* exec_stmt:
1887 *
1888 * 'exec' expr ['in' test [',' test]]
1889 */
Guido van Rossum47478871996-08-21 14:32:37 +00001890static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001891validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001892{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001893 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001894 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001895 && ((nch == 2) || (nch == 4) || (nch == 6))
1896 && validate_name(CHILD(tree, 0), "exec")
1897 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001898
Guido van Rossum3d602e31996-07-21 02:33:56 +00001899 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001900 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001901 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001902 res = (validate_name(CHILD(tree, 2), "in")
1903 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001904 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001905 res = (validate_comma(CHILD(tree, 4))
1906 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001907
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001908 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001909}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001910
1911
Guido van Rossum925e5471997-04-02 05:32:13 +00001912/* assert_stmt:
1913 *
1914 * 'assert' test [',' test]
1915 */
1916static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001917validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001918{
1919 int nch = NCH(tree);
1920 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001921 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001922 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001923 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001924
1925 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001926 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001927 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001928 res = (validate_comma(CHILD(tree, 2))
1929 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001930
1931 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001932}
Guido van Rossum925e5471997-04-02 05:32:13 +00001933
1934
Guido van Rossum47478871996-08-21 14:32:37 +00001935static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001936validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001937{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001938 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001939 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001940 && ((nch == 4) || (nch == 7))
1941 && validate_name(CHILD(tree, 0), "while")
1942 && validate_test(CHILD(tree, 1))
1943 && validate_colon(CHILD(tree, 2))
1944 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001945
Guido van Rossum3d602e31996-07-21 02:33:56 +00001946 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001947 res = (validate_name(CHILD(tree, 4), "else")
1948 && validate_colon(CHILD(tree, 5))
1949 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001950
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001951 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001952}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001953
1954
Guido van Rossum47478871996-08-21 14:32:37 +00001955static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001956validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001957{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001958 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001959 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001960 && ((nch == 6) || (nch == 9))
1961 && validate_name(CHILD(tree, 0), "for")
1962 && validate_exprlist(CHILD(tree, 1))
1963 && validate_name(CHILD(tree, 2), "in")
1964 && validate_testlist(CHILD(tree, 3))
1965 && validate_colon(CHILD(tree, 4))
1966 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001967
Guido van Rossum3d602e31996-07-21 02:33:56 +00001968 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001969 res = (validate_name(CHILD(tree, 6), "else")
1970 && validate_colon(CHILD(tree, 7))
1971 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001972
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001973 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001974}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001975
1976
Guido van Rossum3d602e31996-07-21 02:33:56 +00001977/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001978 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001979 * | 'try' ':' suite 'finally' ':' suite
1980 *
1981 */
Guido van Rossum47478871996-08-21 14:32:37 +00001982static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001983validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001984{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001985 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001986 int pos = 3;
1987 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001988 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001989
1990 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001991 res = (validate_name(CHILD(tree, 0), "try")
1992 && validate_colon(CHILD(tree, 1))
1993 && validate_suite(CHILD(tree, 2))
1994 && validate_colon(CHILD(tree, nch - 2))
1995 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001996 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001997 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001998 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1999 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00002000
2001 PyErr_Format(parser_error,
2002 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002003 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002004 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002005 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002006 res = (validate_except_clause(CHILD(tree, pos))
2007 && validate_colon(CHILD(tree, pos + 1))
2008 && validate_suite(CHILD(tree, pos + 2)));
2009 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002010 }
2011 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002012 res = validate_ntype(CHILD(tree, pos), NAME);
2013 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
2014 res = (validate_numnodes(tree, 6, "try/finally")
2015 && validate_colon(CHILD(tree, 4))
2016 && validate_suite(CHILD(tree, 5)));
2017 else if (res) {
2018 if (nch == (pos + 3)) {
2019 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
2020 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
2021 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00002022 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00002023 }
2024 else if (nch == (pos + 6)) {
2025 res = (validate_name(CHILD(tree, pos), "except")
2026 && validate_colon(CHILD(tree, pos + 1))
2027 && validate_suite(CHILD(tree, pos + 2))
2028 && validate_name(CHILD(tree, pos + 3), "else"));
2029 }
2030 else
2031 res = validate_numnodes(tree, pos + 3, "try/except");
2032 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002033 }
2034 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002035}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002036
2037
Guido van Rossum47478871996-08-21 14:32:37 +00002038static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002039validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002040{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002041 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002042 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00002043 && ((nch == 1) || (nch == 2) || (nch == 4))
2044 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002045
Guido van Rossum3d602e31996-07-21 02:33:56 +00002046 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002047 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002048 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002049 res = (validate_comma(CHILD(tree, 2))
2050 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002051
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002052 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002053}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002054
2055
Guido van Rossum47478871996-08-21 14:32:37 +00002056static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002057validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002058{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002059 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002060 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002061
Guido van Rossum3d602e31996-07-21 02:33:56 +00002062 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002063 res = ((nch == 1)
2064 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002065 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002066 res = validate_or_test(CHILD(tree, 0));
2067 res = (res && (nch == 1 || (nch == 5 &&
2068 validate_name(CHILD(tree, 1), "if") &&
2069 validate_or_test(CHILD(tree, 2)) &&
2070 validate_name(CHILD(tree, 3), "else") &&
2071 validate_test(CHILD(tree, 4)))));
2072 }
2073 return (res);
2074}
2075
2076static int
2077validate_old_test(node *tree)
2078{
2079 int nch = NCH(tree);
2080 int res = validate_ntype(tree, old_test) && (nch == 1);
2081
2082 if (res && (TYPE(CHILD(tree, 0)) == old_lambdef))
2083 res = (validate_old_lambdef(CHILD(tree, 0)));
2084 else if (res) {
2085 res = (validate_or_test(CHILD(tree, 0)));
2086 }
2087 return (res);
2088}
2089
2090static int
2091validate_or_test(node *tree)
2092{
2093 int nch = NCH(tree);
2094 int res = validate_ntype(tree, or_test) && is_odd(nch);
2095
2096 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002097 int pos;
2098 res = validate_and_test(CHILD(tree, 0));
2099 for (pos = 1; res && (pos < nch); pos += 2)
2100 res = (validate_name(CHILD(tree, pos), "or")
2101 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002102 }
2103 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002104}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002105
2106
Guido van Rossum47478871996-08-21 14:32:37 +00002107static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002108validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002109{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002110 int pos;
2111 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002112 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002113 && is_odd(nch)
2114 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002115
Guido van Rossum3d602e31996-07-21 02:33:56 +00002116 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002117 res = (validate_name(CHILD(tree, pos), "and")
2118 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002119
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002120 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002121}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002122
2123
Guido van Rossum47478871996-08-21 14:32:37 +00002124static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002125validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002126{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002127 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002128 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002129
Guido van Rossum3d602e31996-07-21 02:33:56 +00002130 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002131 if (nch == 2)
2132 res = (validate_name(CHILD(tree, 0), "not")
2133 && validate_not_test(CHILD(tree, 1)));
2134 else if (nch == 1)
2135 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002136 }
2137 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002138}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002139
2140
Guido van Rossum47478871996-08-21 14:32:37 +00002141static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002142validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002143{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002144 int pos;
2145 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002146 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002147 && is_odd(nch)
2148 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002149
Guido van Rossum3d602e31996-07-21 02:33:56 +00002150 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002151 res = (validate_comp_op(CHILD(tree, pos))
2152 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002153
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002154 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002155}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002156
2157
Guido van Rossum47478871996-08-21 14:32:37 +00002158static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002159validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002160{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002161 int res = 0;
2162 int nch = NCH(tree);
2163
Guido van Rossum3d602e31996-07-21 02:33:56 +00002164 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002165 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002166 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002167 /*
2168 * Only child will be a terminal with a well-defined symbolic name
2169 * or a NAME with a string of either 'is' or 'in'
2170 */
2171 tree = CHILD(tree, 0);
2172 switch (TYPE(tree)) {
2173 case LESS:
2174 case GREATER:
2175 case EQEQUAL:
2176 case EQUAL:
2177 case LESSEQUAL:
2178 case GREATEREQUAL:
2179 case NOTEQUAL:
2180 res = 1;
2181 break;
2182 case NAME:
2183 res = ((strcmp(STR(tree), "in") == 0)
2184 || (strcmp(STR(tree), "is") == 0));
2185 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002186 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002187 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002188 }
2189 break;
2190 default:
Fred Drake661ea262000-10-24 19:57:45 +00002191 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002192 break;
2193 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002194 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002195 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002196 res = (validate_ntype(CHILD(tree, 0), NAME)
2197 && validate_ntype(CHILD(tree, 1), NAME)
2198 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2199 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2200 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2201 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2202 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002203 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002204 }
2205 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002206}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002207
2208
Guido van Rossum47478871996-08-21 14:32:37 +00002209static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002210validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002211{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002212 int j;
2213 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002214 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002215 && is_odd(nch)
2216 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002217
Guido van Rossum3d602e31996-07-21 02:33:56 +00002218 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002219 res = (validate_xor_expr(CHILD(tree, j))
2220 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002221
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002222 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002223}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002224
2225
Guido van Rossum47478871996-08-21 14:32:37 +00002226static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002227validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002228{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002229 int j;
2230 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002231 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002232 && is_odd(nch)
2233 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002234
Guido van Rossum3d602e31996-07-21 02:33:56 +00002235 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002236 res = (validate_circumflex(CHILD(tree, j - 1))
2237 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002238
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002239 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002240}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002241
2242
Guido van Rossum47478871996-08-21 14:32:37 +00002243static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002244validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002245{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002246 int pos;
2247 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002248 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002249 && is_odd(nch)
2250 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002251
Guido van Rossum3d602e31996-07-21 02:33:56 +00002252 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002253 res = (validate_ampersand(CHILD(tree, pos))
2254 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002255
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002256 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002257}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002258
2259
2260static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002261validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002262 {
2263 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002264 int nch = NCH(tree);
2265 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002266 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002267
Guido van Rossum3d602e31996-07-21 02:33:56 +00002268 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002269 if (TYPE(CHILD(tree, pos)) != op1)
2270 res = validate_ntype(CHILD(tree, pos), op2);
2271 if (res)
2272 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002273 }
2274 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002275}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002276
2277
Guido van Rossum47478871996-08-21 14:32:37 +00002278static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002279validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002280{
2281 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002282 && validate_chain_two_ops(tree, validate_arith_expr,
2283 LEFTSHIFT, RIGHTSHIFT));
2284}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002285
2286
Guido van Rossum47478871996-08-21 14:32:37 +00002287static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002288validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002289{
2290 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002291 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2292}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002293
2294
Guido van Rossum47478871996-08-21 14:32:37 +00002295static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002296validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002297{
2298 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002299 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002300 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002301 && is_odd(nch)
2302 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002303
Guido van Rossum3d602e31996-07-21 02:33:56 +00002304 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002305 res = (((TYPE(CHILD(tree, pos)) == STAR)
2306 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002307 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002308 || (TYPE(CHILD(tree, pos)) == PERCENT))
2309 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002310
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002311 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002312}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002313
2314
Guido van Rossum3d602e31996-07-21 02:33:56 +00002315/* factor:
2316 *
2317 * factor: ('+'|'-'|'~') factor | power
2318 */
Guido van Rossum47478871996-08-21 14:32:37 +00002319static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002320validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002321{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002322 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002323 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002324 && (((nch == 2)
2325 && ((TYPE(CHILD(tree, 0)) == PLUS)
2326 || (TYPE(CHILD(tree, 0)) == MINUS)
2327 || (TYPE(CHILD(tree, 0)) == TILDE))
2328 && validate_factor(CHILD(tree, 1)))
2329 || ((nch == 1)
2330 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002331 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002332}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002333
2334
Guido van Rossum3d602e31996-07-21 02:33:56 +00002335/* power:
2336 *
2337 * power: atom trailer* ('**' factor)*
2338 */
Guido van Rossum47478871996-08-21 14:32:37 +00002339static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002340validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002341{
2342 int pos = 1;
2343 int nch = NCH(tree);
2344 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002345 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002346
2347 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002348 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002349 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002350 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002351 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002352 return (0);
2353 }
2354 for ( ; res && (pos < (nch - 1)); pos += 2)
2355 res = (validate_doublestar(CHILD(tree, pos))
2356 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002357 }
2358 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002359}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002360
2361
Guido van Rossum47478871996-08-21 14:32:37 +00002362static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002363validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002364{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002365 int pos;
2366 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002367 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002368
Fred Drakecff283c2000-08-21 22:24:43 +00002369 if (res && nch < 1)
2370 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002371 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002372 switch (TYPE(CHILD(tree, 0))) {
2373 case LPAR:
2374 res = ((nch <= 3)
2375 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002376
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002377 if (res && (nch == 3)) {
2378 if (TYPE(CHILD(tree, 1))==yield_expr)
2379 res = validate_yield_expr(CHILD(tree, 1));
2380 else
2381 res = validate_testlist_gexp(CHILD(tree, 1));
2382 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002383 break;
2384 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002385 if (nch == 2)
2386 res = validate_ntype(CHILD(tree, 1), RSQB);
2387 else if (nch == 3)
2388 res = (validate_listmaker(CHILD(tree, 1))
2389 && validate_ntype(CHILD(tree, 2), RSQB));
2390 else {
2391 res = 0;
2392 err_string("illegal list display atom");
2393 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002394 break;
2395 case LBRACE:
2396 res = ((nch <= 3)
2397 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002398
Fred Drakeff9ea482000-04-19 13:54:15 +00002399 if (res && (nch == 3))
2400 res = validate_dictmaker(CHILD(tree, 1));
2401 break;
2402 case BACKQUOTE:
2403 res = ((nch == 3)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002404 && validate_testlist1(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002405 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2406 break;
2407 case NAME:
2408 case NUMBER:
2409 res = (nch == 1);
2410 break;
2411 case STRING:
2412 for (pos = 1; res && (pos < nch); ++pos)
2413 res = validate_ntype(CHILD(tree, pos), STRING);
2414 break;
2415 default:
2416 res = 0;
2417 break;
2418 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002419 }
2420 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002421}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002422
2423
Fred Drake85bf3bb2000-08-23 15:35:26 +00002424/* listmaker:
2425 * test ( list_for | (',' test)* [','] )
2426 */
Fred Drakecff283c2000-08-21 22:24:43 +00002427static int
2428validate_listmaker(node *tree)
2429{
2430 int nch = NCH(tree);
2431 int ok = nch;
2432
2433 if (nch == 0)
2434 err_string("missing child nodes of listmaker");
2435 else
2436 ok = validate_test(CHILD(tree, 0));
2437
2438 /*
Raymond Hettinger354433a2004-05-19 08:20:33 +00002439 * list_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002440 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002441 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2442 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002443 else {
2444 /* (',' test)* [','] */
2445 int i = 1;
2446 while (ok && nch - i >= 2) {
2447 ok = (validate_comma(CHILD(tree, i))
2448 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002449 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002450 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002451 if (ok && i == nch-1)
2452 ok = validate_comma(CHILD(tree, i));
2453 else if (i != nch) {
2454 ok = 0;
2455 err_string("illegal trailing nodes for listmaker");
2456 }
Fred Drakecff283c2000-08-21 22:24:43 +00002457 }
2458 return ok;
2459}
2460
Raymond Hettinger354433a2004-05-19 08:20:33 +00002461/* testlist_gexp:
2462 * test ( gen_for | (',' test)* [','] )
2463 */
2464static int
2465validate_testlist_gexp(node *tree)
2466{
2467 int nch = NCH(tree);
2468 int ok = nch;
2469
2470 if (nch == 0)
2471 err_string("missing child nodes of testlist_gexp");
2472 else {
2473 ok = validate_test(CHILD(tree, 0));
2474 }
2475
2476 /*
2477 * gen_for | (',' test)* [',']
2478 */
2479 if (nch == 2 && TYPE(CHILD(tree, 1)) == gen_for)
2480 ok = validate_gen_for(CHILD(tree, 1));
2481 else {
2482 /* (',' test)* [','] */
2483 int i = 1;
2484 while (ok && nch - i >= 2) {
2485 ok = (validate_comma(CHILD(tree, i))
2486 && validate_test(CHILD(tree, i+1)));
2487 i += 2;
2488 }
2489 if (ok && i == nch-1)
2490 ok = validate_comma(CHILD(tree, i));
2491 else if (i != nch) {
2492 ok = 0;
2493 err_string("illegal trailing nodes for testlist_gexp");
2494 }
2495 }
2496 return ok;
2497}
Fred Drakecff283c2000-08-21 22:24:43 +00002498
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002499/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002500 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002501 */
2502static int
2503validate_decorator(node *tree)
2504{
2505 int ok;
2506 int nch = NCH(tree);
2507 ok = (validate_ntype(tree, decorator) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002508 (nch == 3 || nch == 5 || nch == 6) &&
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002509 validate_at(CHILD(tree, 0)) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002510 validate_dotted_name(CHILD(tree, 1)) &&
2511 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002512
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002513 if (ok && nch != 3) {
2514 ok = (validate_lparen(CHILD(tree, 2)) &&
2515 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002516
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002517 if (ok && nch == 6)
2518 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002519 }
2520
2521 return ok;
2522}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002523
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002524/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002525 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002526 */
2527static int
2528validate_decorators(node *tree)
2529{
2530 int i, nch, ok;
2531 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002532 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002533
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002534 for (i = 0; ok && i < nch; ++i)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002535 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002536
2537 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002538}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002539
Guido van Rossum3d602e31996-07-21 02:33:56 +00002540/* funcdef:
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002541 *
2542 * -6 -5 -4 -3 -2 -1
2543 * [decorators] 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002544 */
Guido van Rossum47478871996-08-21 14:32:37 +00002545static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002546validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002547{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002548 int nch = NCH(tree);
2549 int ok = (validate_ntype(tree, funcdef)
2550 && ((nch == 5) || (nch == 6))
2551 && validate_name(RCHILD(tree, -5), "def")
2552 && validate_ntype(RCHILD(tree, -4), NAME)
2553 && validate_colon(RCHILD(tree, -2))
2554 && validate_parameters(RCHILD(tree, -3))
2555 && validate_suite(RCHILD(tree, -1)));
2556
2557 if (ok && (nch == 6))
2558 ok = validate_decorators(CHILD(tree, 0));
2559
2560 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002561}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002562
2563
Guido van Rossum47478871996-08-21 14:32:37 +00002564static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002565validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002566{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002567 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002568 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002569 && ((nch == 3) || (nch == 4))
2570 && validate_name(CHILD(tree, 0), "lambda")
2571 && validate_colon(CHILD(tree, nch - 2))
2572 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002573
Guido van Rossum3d602e31996-07-21 02:33:56 +00002574 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002575 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002576 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002577 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002578
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002579 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002580}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002581
2582
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002583static int
2584validate_old_lambdef(node *tree)
2585{
2586 int nch = NCH(tree);
2587 int res = (validate_ntype(tree, old_lambdef)
2588 && ((nch == 3) || (nch == 4))
2589 && validate_name(CHILD(tree, 0), "lambda")
2590 && validate_colon(CHILD(tree, nch - 2))
2591 && validate_test(CHILD(tree, nch - 1)));
2592
2593 if (res && (nch == 4))
2594 res = validate_varargslist(CHILD(tree, 1));
2595 else if (!res && !PyErr_Occurred())
2596 (void) validate_numnodes(tree, 3, "old_lambdef");
2597
2598 return (res);
2599}
2600
2601
Guido van Rossum3d602e31996-07-21 02:33:56 +00002602/* arglist:
2603 *
Fred Drakecff283c2000-08-21 22:24:43 +00002604 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002605 */
Guido van Rossum47478871996-08-21 14:32:37 +00002606static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002607validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002608{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002609 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002610 int i = 0;
2611 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002612
2613 if (nch <= 0)
2614 /* raise the right error from having an invalid number of children */
2615 return validate_numnodes(tree, nch + 1, "arglist");
2616
Raymond Hettinger354433a2004-05-19 08:20:33 +00002617 if (nch > 1) {
2618 for (i=0; i<nch; i++) {
2619 if (TYPE(CHILD(tree, i)) == argument) {
2620 node *ch = CHILD(tree, i);
2621 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == gen_for) {
2622 err_string("need '(', ')' for generator expression");
2623 return 0;
2624 }
2625 }
2626 }
2627 }
2628
Fred Drakecff283c2000-08-21 22:24:43 +00002629 while (ok && nch-i >= 2) {
2630 /* skip leading (argument ',') */
2631 ok = (validate_argument(CHILD(tree, i))
2632 && validate_comma(CHILD(tree, i+1)));
2633 if (ok)
2634 i += 2;
2635 else
2636 PyErr_Clear();
2637 }
2638 ok = 1;
2639 if (nch-i > 0) {
2640 /*
2641 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002642 */
Fred Drakecff283c2000-08-21 22:24:43 +00002643 int sym = TYPE(CHILD(tree, i));
2644
2645 if (sym == argument) {
2646 ok = validate_argument(CHILD(tree, i));
2647 if (ok && i+1 != nch) {
2648 err_string("illegal arglist specification"
2649 " (extra stuff on end)");
2650 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002651 }
Fred Drakecff283c2000-08-21 22:24:43 +00002652 }
2653 else if (sym == STAR) {
2654 ok = validate_star(CHILD(tree, i));
2655 if (ok && (nch-i == 2))
2656 ok = validate_test(CHILD(tree, i+1));
2657 else if (ok && (nch-i == 5))
2658 ok = (validate_test(CHILD(tree, i+1))
2659 && validate_comma(CHILD(tree, i+2))
2660 && validate_doublestar(CHILD(tree, i+3))
2661 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002662 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002663 err_string("illegal use of '*' in arglist");
2664 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002665 }
2666 }
Fred Drakecff283c2000-08-21 22:24:43 +00002667 else if (sym == DOUBLESTAR) {
2668 if (nch-i == 2)
2669 ok = (validate_doublestar(CHILD(tree, i))
2670 && validate_test(CHILD(tree, i+1)));
2671 else {
2672 err_string("illegal use of '**' in arglist");
2673 ok = 0;
2674 }
2675 }
2676 else {
2677 err_string("illegal arglist specification");
2678 ok = 0;
2679 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002680 }
2681 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002682}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002683
2684
2685
2686/* argument:
2687 *
Raymond Hettinger354433a2004-05-19 08:20:33 +00002688 * [test '='] test [gen_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002689 */
Guido van Rossum47478871996-08-21 14:32:37 +00002690static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002691validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002692{
2693 int nch = NCH(tree);
2694 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002695 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002696 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002697
Raymond Hettinger354433a2004-05-19 08:20:33 +00002698 if (res && (nch == 2))
2699 res = validate_gen_for(CHILD(tree, 1));
2700 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002701 res = (validate_equal(CHILD(tree, 1))
2702 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002703
2704 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002705}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002706
2707
2708
2709/* trailer:
2710 *
Guido van Rossum47478871996-08-21 14:32:37 +00002711 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002712 */
Guido van Rossum47478871996-08-21 14:32:37 +00002713static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002714validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002715{
2716 int nch = NCH(tree);
2717 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002718
2719 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002720 switch (TYPE(CHILD(tree, 0))) {
2721 case LPAR:
2722 res = validate_rparen(CHILD(tree, nch - 1));
2723 if (res && (nch == 3))
2724 res = validate_arglist(CHILD(tree, 1));
2725 break;
2726 case LSQB:
2727 res = (validate_numnodes(tree, 3, "trailer")
2728 && validate_subscriptlist(CHILD(tree, 1))
2729 && validate_ntype(CHILD(tree, 2), RSQB));
2730 break;
2731 case DOT:
2732 res = (validate_numnodes(tree, 2, "trailer")
2733 && validate_ntype(CHILD(tree, 1), NAME));
2734 break;
2735 default:
2736 res = 0;
2737 break;
2738 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002739 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002740 else {
2741 (void) validate_numnodes(tree, 2, "trailer");
2742 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002743 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002744}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002745
2746
Guido van Rossum47478871996-08-21 14:32:37 +00002747/* subscriptlist:
2748 *
2749 * subscript (',' subscript)* [',']
2750 */
2751static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002752validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002753{
2754 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002755 validate_subscript, "subscriptlist"));
2756}
Guido van Rossum47478871996-08-21 14:32:37 +00002757
2758
Guido van Rossum3d602e31996-07-21 02:33:56 +00002759/* subscript:
2760 *
Guido van Rossum47478871996-08-21 14:32:37 +00002761 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002762 */
Guido van Rossum47478871996-08-21 14:32:37 +00002763static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002764validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002765{
Guido van Rossum47478871996-08-21 14:32:37 +00002766 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002767 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002768 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002769
Guido van Rossum47478871996-08-21 14:32:37 +00002770 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002771 if (!PyErr_Occurred())
2772 err_string("invalid number of arguments for subscript node");
2773 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002774 }
Guido van Rossum47478871996-08-21 14:32:37 +00002775 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002776 /* take care of ('.' '.' '.') possibility */
2777 return (validate_numnodes(tree, 3, "subscript")
2778 && validate_dot(CHILD(tree, 0))
2779 && validate_dot(CHILD(tree, 1))
2780 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002781 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002782 if (TYPE(CHILD(tree, 0)) == test)
2783 res = validate_test(CHILD(tree, 0));
2784 else
2785 res = validate_colon(CHILD(tree, 0));
2786 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002787 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002788 /* Must be [test] ':' [test] [sliceop],
2789 * but at least one of the optional components will
2790 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002791 */
2792 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002793 res = validate_test(CHILD(tree, 0));
2794 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002795 }
2796 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002797 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002798 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002799 int rem = nch - ++offset;
2800 if (rem) {
2801 if (TYPE(CHILD(tree, offset)) == test) {
2802 res = validate_test(CHILD(tree, offset));
2803 ++offset;
2804 --rem;
2805 }
2806 if (res && rem)
2807 res = validate_sliceop(CHILD(tree, offset));
2808 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002809 }
2810 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002811}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002812
2813
Guido van Rossum47478871996-08-21 14:32:37 +00002814static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002815validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002816{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002817 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002818 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002819 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002820 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002821 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002822 }
Guido van Rossum47478871996-08-21 14:32:37 +00002823 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002824 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002825 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002826 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002827
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002828 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002829}
Guido van Rossum47478871996-08-21 14:32:37 +00002830
2831
2832static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002833validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002834{
2835 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002836 validate_expr, "exprlist"));
2837}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002838
2839
Guido van Rossum47478871996-08-21 14:32:37 +00002840static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002841validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002842{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002843 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002844 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002845 && (nch >= 3)
2846 && validate_test(CHILD(tree, 0))
2847 && validate_colon(CHILD(tree, 1))
2848 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002849
Guido van Rossum3d602e31996-07-21 02:33:56 +00002850 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002851 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002852 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002853 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002854
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002855 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002856 int pos = 3;
2857 /* ( ',' test ':' test )* */
2858 while (res && (pos < nch)) {
2859 res = (validate_comma(CHILD(tree, pos))
2860 && validate_test(CHILD(tree, pos + 1))
2861 && validate_colon(CHILD(tree, pos + 2))
2862 && validate_test(CHILD(tree, pos + 3)));
2863 pos += 4;
2864 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002865 }
2866 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002867}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002868
2869
Guido van Rossum47478871996-08-21 14:32:37 +00002870static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002871validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002872{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002873 int pos;
2874 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002875 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002876 && (nch >= 2)
2877 && validate_testlist(CHILD(tree, 0))
2878 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002879
Guido van Rossum3d602e31996-07-21 02:33:56 +00002880 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002881 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002882
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002883 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002884}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002885
2886
Guido van Rossum47478871996-08-21 14:32:37 +00002887static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002888validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002889{
Fred Drakeff9ea482000-04-19 13:54:15 +00002890 int nch = 0; /* num. children on current node */
2891 int res = 1; /* result value */
2892 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002893
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002894 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002895 nch = NCH(tree);
2896 next = 0;
2897 switch (TYPE(tree)) {
2898 /*
2899 * Definition nodes.
2900 */
2901 case funcdef:
2902 res = validate_funcdef(tree);
2903 break;
2904 case classdef:
2905 res = validate_class(tree);
2906 break;
2907 /*
2908 * "Trivial" parse tree nodes.
2909 * (Why did I call these trivial?)
2910 */
2911 case stmt:
2912 res = validate_stmt(tree);
2913 break;
2914 case small_stmt:
2915 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002916 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002917 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2918 */
2919 res = validate_small_stmt(tree);
2920 break;
2921 case flow_stmt:
2922 res = (validate_numnodes(tree, 1, "flow_stmt")
2923 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2924 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002925 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002926 || (TYPE(CHILD(tree, 0)) == return_stmt)
2927 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2928 if (res)
2929 next = CHILD(tree, 0);
2930 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002931 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002932 break;
Fred Drake02126f22001-07-17 02:59:15 +00002933 case yield_stmt:
2934 res = validate_yield_stmt(tree);
2935 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002936 /*
2937 * Compound statements.
2938 */
2939 case simple_stmt:
2940 res = validate_simple_stmt(tree);
2941 break;
2942 case compound_stmt:
2943 res = validate_compound_stmt(tree);
2944 break;
2945 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002946 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002947 */
2948 case expr_stmt:
2949 res = validate_expr_stmt(tree);
2950 break;
2951 case print_stmt:
2952 res = validate_print_stmt(tree);
2953 break;
2954 case del_stmt:
2955 res = validate_del_stmt(tree);
2956 break;
2957 case pass_stmt:
2958 res = (validate_numnodes(tree, 1, "pass")
2959 && validate_name(CHILD(tree, 0), "pass"));
2960 break;
2961 case break_stmt:
2962 res = (validate_numnodes(tree, 1, "break")
2963 && validate_name(CHILD(tree, 0), "break"));
2964 break;
2965 case continue_stmt:
2966 res = (validate_numnodes(tree, 1, "continue")
2967 && validate_name(CHILD(tree, 0), "continue"));
2968 break;
2969 case return_stmt:
2970 res = validate_return_stmt(tree);
2971 break;
2972 case raise_stmt:
2973 res = validate_raise_stmt(tree);
2974 break;
2975 case import_stmt:
2976 res = validate_import_stmt(tree);
2977 break;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00002978 case import_name:
2979 res = validate_import_name(tree);
2980 break;
2981 case import_from:
2982 res = validate_import_from(tree);
2983 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002984 case global_stmt:
2985 res = validate_global_stmt(tree);
2986 break;
2987 case exec_stmt:
2988 res = validate_exec_stmt(tree);
2989 break;
2990 case assert_stmt:
2991 res = validate_assert_stmt(tree);
2992 break;
2993 case if_stmt:
2994 res = validate_if(tree);
2995 break;
2996 case while_stmt:
2997 res = validate_while(tree);
2998 break;
2999 case for_stmt:
3000 res = validate_for(tree);
3001 break;
3002 case try_stmt:
3003 res = validate_try(tree);
3004 break;
3005 case suite:
3006 res = validate_suite(tree);
3007 break;
3008 /*
3009 * Expression nodes.
3010 */
3011 case testlist:
3012 res = validate_testlist(tree);
3013 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003014 case yield_expr:
3015 res = validate_yield_expr(tree);
3016 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00003017 case testlist1:
3018 res = validate_testlist1(tree);
3019 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003020 case test:
3021 res = validate_test(tree);
3022 break;
3023 case and_test:
3024 res = validate_and_test(tree);
3025 break;
3026 case not_test:
3027 res = validate_not_test(tree);
3028 break;
3029 case comparison:
3030 res = validate_comparison(tree);
3031 break;
3032 case exprlist:
3033 res = validate_exprlist(tree);
3034 break;
3035 case comp_op:
3036 res = validate_comp_op(tree);
3037 break;
3038 case expr:
3039 res = validate_expr(tree);
3040 break;
3041 case xor_expr:
3042 res = validate_xor_expr(tree);
3043 break;
3044 case and_expr:
3045 res = validate_and_expr(tree);
3046 break;
3047 case shift_expr:
3048 res = validate_shift_expr(tree);
3049 break;
3050 case arith_expr:
3051 res = validate_arith_expr(tree);
3052 break;
3053 case term:
3054 res = validate_term(tree);
3055 break;
3056 case factor:
3057 res = validate_factor(tree);
3058 break;
3059 case power:
3060 res = validate_power(tree);
3061 break;
3062 case atom:
3063 res = validate_atom(tree);
3064 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003065
Fred Drakeff9ea482000-04-19 13:54:15 +00003066 default:
3067 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00003068 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003069 res = 0;
3070 break;
3071 }
3072 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003073 }
3074 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003075}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003076
3077
Guido van Rossum47478871996-08-21 14:32:37 +00003078static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003079validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003080{
3081 int res = validate_eval_input(tree);
3082
3083 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003084 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003085
3086 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003087}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003088
3089
Guido van Rossum3d602e31996-07-21 02:33:56 +00003090/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003091 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003092 */
Guido van Rossum47478871996-08-21 14:32:37 +00003093static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003094validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003095{
Fred Drakec2683dd2001-07-17 19:32:05 +00003096 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003097 int nch = NCH(tree) - 1;
3098 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003099 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003100
Fred Drakec2683dd2001-07-17 19:32:05 +00003101 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003102 if (TYPE(CHILD(tree, j)) == stmt)
3103 res = validate_stmt(CHILD(tree, j));
3104 else
3105 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003106 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003107 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003108 * user. Hopefully, this won't be needed. If a user reports getting
3109 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003110 */
3111 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003112 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003113
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003114 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003115}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003116
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003117static int
3118validate_encoding_decl(node *tree)
3119{
3120 int nch = NCH(tree);
3121 int res = ((nch == 1)
3122 && validate_file_input(CHILD(tree, 0)));
3123
3124 if (!res && !PyErr_Occurred())
3125 err_string("Error Parsing encoding_decl");
3126
3127 return res;
3128}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003129
Fred Drake43f8f9b1998-04-13 16:25:46 +00003130static PyObject*
3131pickle_constructor = NULL;
3132
3133
3134static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003135parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003136{
Fred Drake268397f1998-04-29 14:16:32 +00003137 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003138 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003139 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003140 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003141
Fred Drakec2683dd2001-07-17 19:32:05 +00003142 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003143 PyObject *newargs;
3144 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003145
Fred Drake2a6875e1999-09-20 22:32:18 +00003146 if ((empty_dict = PyDict_New()) == NULL)
3147 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003148 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003149 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003150 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003151 if (tuple != NULL) {
3152 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3153 Py_DECREF(tuple);
3154 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003155 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003156 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003157 }
3158 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003159 Py_XDECREF(empty_dict);
3160
Fred Drake43f8f9b1998-04-13 16:25:46 +00003161 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003162}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003163
3164
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003165/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003166 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003167 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003168 * We'd really have to write a wrapper around it all anyway to allow
3169 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003170 */
3171static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003172 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003173 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003174 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003175 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003176 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003177 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003178 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003179 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003180 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003181 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003182 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003183 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003184 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003185 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003186 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003187 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003188 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003189 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003190 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003191 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003192 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003193 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003194 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003195 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003196 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003197 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003198 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003199 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003200
Fred Drake43f8f9b1998-04-13 16:25:46 +00003201 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003202 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003203 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003204
Fred Drake268397f1998-04-29 14:16:32 +00003205 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003206 };
3207
3208
Mark Hammond62b1ab12002-07-23 06:31:15 +00003209PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003210
Mark Hammond62b1ab12002-07-23 06:31:15 +00003211PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00003212initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003213{
Fred Drake13130bc2001-08-15 16:44:56 +00003214 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003215
3216 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00003217 module = Py_InitModule("parser", parser_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003218 if (module == NULL)
3219 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003220
Fred Drake7a15ba51999-09-09 14:21:52 +00003221 if (parser_error == 0)
3222 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003223
Tim Peters6a627252003-07-21 14:25:23 +00003224 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00003225 /* caller will check PyErr_Occurred() */
3226 return;
Tim Peters6a627252003-07-21 14:25:23 +00003227 /* CAUTION: The code next used to skip bumping the refcount on
3228 * parser_error. That's a disaster if initparser() gets called more
3229 * than once. By incref'ing, we ensure that each module dict that
3230 * gets created owns its reference to the shared parser_error object,
3231 * and the file static parser_error vrbl owns a reference too.
3232 */
3233 Py_INCREF(parser_error);
3234 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3235 return;
3236
Fred Drakec2683dd2001-07-17 19:32:05 +00003237 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003238 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00003239 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003240 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003241
Fred Drake13130bc2001-08-15 16:44:56 +00003242 PyModule_AddStringConstant(module, "__copyright__",
3243 parser_copyright_string);
3244 PyModule_AddStringConstant(module, "__doc__",
3245 parser_doc_string);
3246 PyModule_AddStringConstant(module, "__version__",
3247 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003248
Fred Drake78bdb9b2001-07-19 20:17:15 +00003249 /* Register to support pickling.
3250 * If this fails, the import of this module will fail because an
3251 * exception will be raised here; should we clear the exception?
3252 */
Fred Drake13130bc2001-08-15 16:44:56 +00003253 copyreg = PyImport_ImportModule("copy_reg");
3254 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003255 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003256
Fred Drake13130bc2001-08-15 16:44:56 +00003257 func = PyObject_GetAttrString(copyreg, "pickle");
3258 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3259 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003260 Py_XINCREF(pickle_constructor);
3261 if ((func != NULL) && (pickle_constructor != NULL)
3262 && (pickler != NULL)) {
3263 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003264
Fred Drakec2683dd2001-07-17 19:32:05 +00003265 res = PyObject_CallFunction(func, "OOO", &PyST_Type, pickler,
3266 pickle_constructor);
Fred Drakeff9ea482000-04-19 13:54:15 +00003267 Py_XDECREF(res);
3268 }
3269 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003270 Py_XDECREF(pickle_constructor);
3271 Py_XDECREF(pickler);
3272 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003273 }
Fred Drakeff9ea482000-04-19 13:54:15 +00003274}