blob: 23364fe240ff263cb4b54c9d1e8c65698b961d3d [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. */
Jeremy Hylton60e96f62006-08-22 20:46:00 +000077 int lineno, /* include line numbers? */
78 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000079{
Guido van Rossum3d602e31996-07-21 02:33:56 +000080 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000081 Py_INCREF(Py_None);
82 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000083 }
84 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000085 int i;
86 PyObject *v;
87 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000088
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000089 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000090 if (v == NULL)
91 return (v);
92 w = PyInt_FromLong(TYPE(n));
93 if (w == NULL) {
94 Py_DECREF(v);
95 return ((PyObject*) NULL);
96 }
97 (void) addelem(v, 0, w);
98 for (i = 0; i < NCH(n); i++) {
Jeremy Hylton60e96f62006-08-22 20:46:00 +000099 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000100 if (w == NULL) {
101 Py_DECREF(v);
102 return ((PyObject*) NULL);
103 }
104 (void) addelem(v, i+1, w);
105 }
Tim Peters6a627252003-07-21 14:25:23 +0000106
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000107 if (TYPE(n) == encoding_decl)
108 (void) addelem(v, i+1, PyString_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000109 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000110 }
111 else if (ISTERMINAL(TYPE(n))) {
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000112 PyObject *result = mkseq(2 + lineno + col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000113 if (result != NULL) {
114 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
115 (void) addelem(result, 1, PyString_FromString(STR(n)));
116 if (lineno == 1)
117 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000118 if (col_offset == 1)
119 (void) addelem(result, 3, PyInt_FromLong(n->n_col_offset));
Fred Drakeff9ea482000-04-19 13:54:15 +0000120 }
121 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000122 }
123 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000124 PyErr_SetString(PyExc_SystemError,
125 "unrecognized parse tree node type");
126 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000127 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000128}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000129/*
130 * End of material copyrighted by Stichting Mathematisch Centrum.
131 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000132
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000133
134
135/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000136 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000137 * field of the object type to identify which any given object represents.
138 * These should probably go in an external header to allow other extensions
139 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000140 */
141
Fred Drakec2683dd2001-07-17 19:32:05 +0000142#define PyST_EXPR 1
143#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000144
145
146/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000147 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000148 * naming style, but the code uses the new naming convention.
149 */
150
151static PyObject*
152parser_error = 0;
153
154
Fred Drakec2683dd2001-07-17 19:32:05 +0000155typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000156 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000157 node* st_node; /* the node* returned by the parser */
158 int st_type; /* EXPR or SUITE ? */
159} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000160
161
Jeremy Hylton938ace62002-07-17 16:30:39 +0000162static void parser_free(PyST_Object *st);
163static int parser_compare(PyST_Object *left, PyST_Object *right);
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000164static PyObject *parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000165
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000166
Fred Drake268397f1998-04-29 14:16:32 +0000167static
Fred Drakec2683dd2001-07-17 19:32:05 +0000168PyTypeObject PyST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000169 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000170 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000171 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000172 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000173 0, /* tp_itemsize */
174 (destructor)parser_free, /* tp_dealloc */
175 0, /* tp_print */
176 parser_getattr, /* tp_getattr */
177 0, /* tp_setattr */
178 (cmpfunc)parser_compare, /* tp_compare */
179 0, /* tp_repr */
180 0, /* tp_as_number */
181 0, /* tp_as_sequence */
182 0, /* tp_as_mapping */
183 0, /* tp_hash */
184 0, /* tp_call */
185 0, /* tp_str */
186 0, /* tp_getattro */
187 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000188
189 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000190 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000191
Fred Drakeff9ea482000-04-19 13:54:15 +0000192 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000193
194 /* __doc__ */
195 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000196}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000197
198
199static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000200parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000201{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000202 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000203
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000204 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000205 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000206
207 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000208 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000209
210 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000211 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000212
213 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000214 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000215
216 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000217 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000218
219 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000220 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000221
Fred Drakeff9ea482000-04-19 13:54:15 +0000222 if (v != 0)
223 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000224 }
225 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000226}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000227
228
Fred Drakec2683dd2001-07-17 19:32:05 +0000229/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000230 *
231 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
232 * This really just wraps a call to parser_compare_nodes() with some easy
233 * checks and protection code.
234 *
235 */
236static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000237parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000238{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000239 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000240 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000241
242 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000243 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000244
Fred Drakec2683dd2001-07-17 19:32:05 +0000245 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000246}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247
248
Fred Drakec2683dd2001-07-17 19:32:05 +0000249/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000250 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000251 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000252 * 'wrapper' object that holds a node* and allows it to be passed around in
253 * Python code.
254 *
255 */
256static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000257parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000258{
Fred Drakec2683dd2001-07-17 19:32:05 +0000259 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000260
261 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000262 o->st_node = st;
263 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000264 }
Fred Drake268397f1998-04-29 14:16:32 +0000265 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000266 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000267 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000268 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000269}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000270
271
Fred Drakec2683dd2001-07-17 19:32:05 +0000272/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000273 *
274 * This is called by a del statement that reduces the reference count to 0.
275 *
276 */
277static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000278parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000279{
Fred Drakec2683dd2001-07-17 19:32:05 +0000280 PyNode_Free(st->st_node);
281 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000282}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000283
284
Fred Drakec2683dd2001-07-17 19:32:05 +0000285/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000286 *
287 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000288 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000289 *
290 */
291static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000292parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000293{
Guido van Rossum47478871996-08-21 14:32:37 +0000294 PyObject *line_option = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000295 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000296 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000297 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000298
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000299 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000300
Fred Drake268397f1998-04-29 14:16:32 +0000301 if (self == NULL) {
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000302 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
303 &PyST_Type, &self, &line_option,
304 &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000305 }
Fred Drake503d8d61998-04-13 18:45:18 +0000306 else
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000307 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
308 &line_option, &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000309 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000310 int lineno = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000311 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000312 if (line_option != NULL) {
313 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
314 }
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000315 if (col_option != NULL) {
316 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
317 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000318 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000319 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000320 * since it's known to work already.
321 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000322 res = node2tuple(((PyST_Object*)self)->st_node,
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000323 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000324 }
325 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000326}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000327
328
Fred Drakec2683dd2001-07-17 19:32:05 +0000329/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000330 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000331 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000332 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000333 *
334 */
335static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000336parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000337{
Guido van Rossum47478871996-08-21 14:32:37 +0000338 PyObject *line_option = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000339 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000340 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000341 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000342
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000343 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000344
Fred Drake503d8d61998-04-13 18:45:18 +0000345 if (self == NULL)
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000346 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
347 &PyST_Type, &self, &line_option,
348 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000349 else
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000350 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
351 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000352 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000353 int lineno = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000354 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000355 if (line_option != 0) {
356 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
357 }
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000358 if (col_option != NULL) {
359 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
360 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000361 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000362 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000363 * since it's known to work already.
364 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000365 res = node2tuple(self->st_node,
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000366 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000367 }
368 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000369}
Guido van Rossum47478871996-08-21 14:32:37 +0000370
371
Fred Drakec2683dd2001-07-17 19:32:05 +0000372/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000373 *
374 * This function creates code objects from the parse tree represented by
375 * the passed-in data object. An optional file name is passed in as well.
376 *
377 */
378static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000379parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000380{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000381 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000382 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000383 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000384
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000385 static char *keywords[] = {"ast", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000386
Fred Drake503d8d61998-04-13 18:45:18 +0000387 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000388 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
389 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000390 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000391 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000392 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000393
394 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000395 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000396
397 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000398}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000399
400
401/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
402 * PyObject* parser_issuite(PyObject* self, PyObject* args)
403 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000404 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000405 * a statement suite, respectively. The return is a Python truth value.
406 *
407 */
408static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000409parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000410{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000411 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000412 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000413
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000414 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000415
Fred Drake503d8d61998-04-13 18:45:18 +0000416 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000417 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000418 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000419 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000420 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000421
422 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000423 /* Check to see if the ST represents an expression or not. */
424 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000425 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000426 }
427 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000428}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000429
430
431static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000432parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000433{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000434 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000435 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000436
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000437 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000438
Fred Drake503d8d61998-04-13 18:45:18 +0000439 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000440 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000441 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000442 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000443 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000444
445 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000446 /* Check to see if the ST represents an expression or not. */
447 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000448 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000449 }
450 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000451}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000452
453
Fred Drake7a15ba51999-09-09 14:21:52 +0000454#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
455
Fred Drake503d8d61998-04-13 18:45:18 +0000456static PyMethodDef
457parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000458 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000459 PyDoc_STR("Compile this ST object into a code object.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000460 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000461 PyDoc_STR("Determines if this ST object was created from an expression.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000462 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000463 PyDoc_STR("Determines if this ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000464 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000465 PyDoc_STR("Creates a list-tree representation of this ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000466 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000467 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Fred Drake503d8d61998-04-13 18:45:18 +0000468
Fred Drake268397f1998-04-29 14:16:32 +0000469 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000470};
471
Fred Drake503d8d61998-04-13 18:45:18 +0000472
473static PyObject*
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000474parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000475{
Fred Drake503d8d61998-04-13 18:45:18 +0000476 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000477}
Fred Drake503d8d61998-04-13 18:45:18 +0000478
479
Guido van Rossum3d602e31996-07-21 02:33:56 +0000480/* err_string(char* message)
481 *
482 * Sets the error string for an exception of type ParserError.
483 *
484 */
485static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000486err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000487{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000488 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000489}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000490
491
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000492/* PyObject* parser_do_parse(PyObject* args, int type)
493 *
494 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000495 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000496 *
497 */
498static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000499parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000500{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000501 char* string = 0;
502 PyObject* res = 0;
503
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000504 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000505
506 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000507 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000508 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000509 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000510
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000511 if (n)
512 res = parser_newstobject(n, type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000513 }
514 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000515}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000516
517
518/* PyObject* parser_expr(PyObject* self, PyObject* args)
519 * PyObject* parser_suite(PyObject* self, PyObject* args)
520 *
521 * External interfaces to the parser itself. Which is called determines if
522 * the parser attempts to recognize an expression ('eval' form) or statement
523 * suite ('exec' form). The real work is done by parser_do_parse() above.
524 *
525 */
526static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000527parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000528{
Fred Drake268397f1998-04-29 14:16:32 +0000529 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000530 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000531}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000532
533
534static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000535parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000536{
Fred Drake268397f1998-04-29 14:16:32 +0000537 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000538 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000539}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000540
541
542
Fred Drakec2683dd2001-07-17 19:32:05 +0000543/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000544 * object requires that the input tuple be valid without having to rely on
545 * catching an exception from the compiler. This is done to allow the
546 * compiler itself to remain fast, since most of its input will come from
547 * the parser directly, and therefore be known to be syntactically correct.
548 * This validation is done to ensure that we don't core dump the compile
549 * phase, returning an exception instead.
550 *
551 * Two aspects can be broken out in this code: creating a node tree from
552 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000553 * advantageous to expand the number of ST types to include funcdefs and
554 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000555 * here. They are not necessary, and not quite as useful in a raw form.
556 * For now, let's get expressions and suites working reliably.
557 */
558
559
Jeremy Hylton938ace62002-07-17 16:30:39 +0000560static node* build_node_tree(PyObject *tuple);
561static int validate_expr_tree(node *tree);
562static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000563static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000564
Fred Drakec2683dd2001-07-17 19:32:05 +0000565/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000566 *
567 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000568 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000569 * tuple can be validated. It does this by checking the first code of the
570 * tuple, and, if acceptable, builds the internal representation. If this
571 * step succeeds, the internal representation is validated as fully as
572 * possible with the various validate_*() routines defined below.
573 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000574 * This function must be changed if support is to be added for PyST_FRAGMENT
575 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000576 *
577 */
578static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000579parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000580{
Fred Drake268397f1998-04-29 14:16:32 +0000581 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000582 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000583 PyObject *tuple;
584 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000585
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000586 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000587
Fred Drakec2683dd2001-07-17 19:32:05 +0000588 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000589 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000590 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000591 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000592 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000593 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000594 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000595 }
596 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000597 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000598 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000599 tree = build_node_tree(tuple);
600 if (tree != 0) {
601 int start_sym = TYPE(tree);
602 if (start_sym == eval_input) {
603 /* Might be an eval form. */
604 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000605 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000606 else
607 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000608 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000609 else if (start_sym == file_input) {
610 /* This looks like an exec form so far. */
611 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000612 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000613 else
614 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000615 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000616 else if (start_sym == encoding_decl) {
617 /* This looks like an encoding_decl so far. */
618 if (validate_encoding_decl(tree))
619 st = parser_newstobject(tree, PyST_SUITE);
620 else
621 PyNode_Free(tree);
622 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000623 else {
624 /* This is a fragment, at best. */
625 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000626 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000627 }
Guido van Rossum47478871996-08-21 14:32:37 +0000628 }
Guido van Rossum47478871996-08-21 14:32:37 +0000629 /* Make sure we throw an exception on all errors. We should never
630 * get this, but we'd do well to be sure something is done.
631 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000632 if (st == NULL && !PyErr_Occurred())
633 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000634
Fred Drakec2683dd2001-07-17 19:32:05 +0000635 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000636}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000637
638
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000639/* node* build_node_children()
640 *
641 * Iterate across the children of the current non-terminal node and build
642 * their structures. If successful, return the root of this portion of
643 * the tree, otherwise, 0. Any required exception will be specified already,
644 * and no memory will have been deallocated.
645 *
646 */
647static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000648build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000649{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000650 Py_ssize_t len = PyObject_Size(tuple);
651 Py_ssize_t i;
652 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000653
654 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000655 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000656 PyObject* elem = PySequence_GetItem(tuple, i);
657 int ok = elem != NULL;
658 long type = 0;
659 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000660
Fred Drakeff9ea482000-04-19 13:54:15 +0000661 if (ok)
662 ok = PySequence_Check(elem);
663 if (ok) {
664 PyObject *temp = PySequence_GetItem(elem, 0);
665 if (temp == NULL)
666 ok = 0;
667 else {
668 ok = PyInt_Check(temp);
669 if (ok)
670 type = PyInt_AS_LONG(temp);
671 Py_DECREF(temp);
672 }
673 }
674 if (!ok) {
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000675 PyObject *err = Py_BuildValue("os", elem,
676 "Illegal node construct.");
677 PyErr_SetObject(parser_error, err);
678 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000679 Py_XDECREF(elem);
680 return (0);
681 }
682 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000683 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000684 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000685
Fred Drake0ac9b072000-09-12 21:58:06 +0000686 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000687 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000688 return 0;
689 }
690 temp = PySequence_GetItem(elem, 1);
691 if (temp == NULL)
692 return 0;
693 if (!PyString_Check(temp)) {
694 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000695 "second item in terminal node must be a string,"
696 " found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000697 temp->ob_type->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000698 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000699 return 0;
700 }
701 if (len == 3) {
702 PyObject *o = PySequence_GetItem(elem, 2);
703 if (o != NULL) {
704 if (PyInt_Check(o))
705 *line_num = PyInt_AS_LONG(o);
706 else {
707 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000708 "third item in terminal node must be an"
709 " integer, found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000710 temp->ob_type->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000711 Py_DECREF(o);
712 Py_DECREF(temp);
713 return 0;
714 }
715 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000716 }
717 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000718 len = PyString_GET_SIZE(temp) + 1;
Tim Petersc9d78aa2006-03-26 23:27:58 +0000719 strn = (char *)PyObject_MALLOC(len);
Fred Drake0ac9b072000-09-12 21:58:06 +0000720 if (strn != NULL)
721 (void) memcpy(strn, PyString_AS_STRING(temp), len);
722 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000723 }
724 else if (!ISNONTERMINAL(type)) {
725 /*
726 * It has to be one or the other; this is an error.
727 * Throw an exception.
728 */
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000729 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
730 PyErr_SetObject(parser_error, err);
731 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000732 Py_XDECREF(elem);
733 return (0);
734 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000735 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000736 if (err == E_NOMEM) {
Tim Petersc9d78aa2006-03-26 23:27:58 +0000737 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000738 return (node *) PyErr_NoMemory();
739 }
740 if (err == E_OVERFLOW) {
Tim Petersc9d78aa2006-03-26 23:27:58 +0000741 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000742 PyErr_SetString(PyExc_ValueError,
743 "unsupported number of child nodes");
744 return NULL;
745 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000746
Fred Drakeff9ea482000-04-19 13:54:15 +0000747 if (ISNONTERMINAL(type)) {
748 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000749
Fred Drakeff9ea482000-04-19 13:54:15 +0000750 if (new_child != build_node_children(elem, new_child, line_num)) {
751 Py_XDECREF(elem);
752 return (0);
753 }
754 }
755 else if (type == NEWLINE) { /* It's true: we increment the */
756 ++(*line_num); /* line number *after* the newline! */
757 }
758 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000759 }
Tim Petersc9d78aa2006-03-26 23:27:58 +0000760 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000761}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000762
763
764static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000765build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000766{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000767 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000768 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000769 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000770
Guido van Rossum47478871996-08-21 14:32:37 +0000771 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000772 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000773 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000774 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000775 /*
776 * The tuple is simple, but it doesn't start with a start symbol.
777 * Throw an exception now and be done with it.
778 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000779 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000780 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000781 PyErr_SetObject(parser_error, tuple);
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000782 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000783 }
784 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000785 /*
786 * Not efficient, but that can be handled later.
787 */
788 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000789 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000790
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000791 if (num == encoding_decl) {
792 encoding = PySequence_GetItem(tuple, 2);
793 /* tuple isn't borrowed anymore here, need to DECREF */
794 tuple = PySequence_GetSlice(tuple, 0, 2);
795 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000796 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000797 if (res != NULL) {
798 if (res != build_node_children(tuple, res, &line_num)) {
799 PyNode_Free(res);
800 res = NULL;
801 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000802 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000803 Py_ssize_t len;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000804 len = PyString_GET_SIZE(encoding) + 1;
Tim Petersc9d78aa2006-03-26 23:27:58 +0000805 res->n_str = (char *)PyObject_MALLOC(len);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000806 if (res->n_str != NULL)
807 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
808 Py_DECREF(encoding);
809 Py_DECREF(tuple);
810 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000811 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000812 }
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000813 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000814 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000815 * NONTERMINAL, we can't use it. Not sure the implementation
816 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000817 */
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000818 PyObject *err = Py_BuildValue("os", tuple,
819 "Illegal component tuple.");
820 PyErr_SetObject(parser_error, err);
821 Py_XDECREF(err);
822 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000823
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000824 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000825}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000826
827
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000828/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000829 * Validation routines used within the validation section:
830 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000831static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000832
Fred Drakeff9ea482000-04-19 13:54:15 +0000833#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
834#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
835#define validate_colon(ch) validate_terminal(ch, COLON, ":")
836#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
837#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
838#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
839#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
840#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
841#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
842#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
843#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
844#define validate_star(ch) validate_terminal(ch, STAR, "*")
845#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
846#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
847#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000848#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000849#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000850
Fred Drake0ac9b072000-09-12 21:58:06 +0000851#define VALIDATER(n) static int validate_##n(node *tree)
852
Fred Drakeff9ea482000-04-19 13:54:15 +0000853VALIDATER(node); VALIDATER(small_stmt);
854VALIDATER(class); VALIDATER(node);
855VALIDATER(parameters); VALIDATER(suite);
856VALIDATER(testlist); VALIDATER(varargslist);
857VALIDATER(fpdef); VALIDATER(fplist);
858VALIDATER(stmt); VALIDATER(simple_stmt);
859VALIDATER(expr_stmt); VALIDATER(power);
860VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000861VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000862VALIDATER(raise_stmt); VALIDATER(import_stmt);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000863VALIDATER(import_name); VALIDATER(import_from);
Fred Drakecff283c2000-08-21 22:24:43 +0000864VALIDATER(global_stmt); VALIDATER(list_if);
865VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000866VALIDATER(exec_stmt); VALIDATER(compound_stmt);
867VALIDATER(while); VALIDATER(for);
868VALIDATER(try); VALIDATER(except_clause);
869VALIDATER(test); VALIDATER(and_test);
870VALIDATER(not_test); VALIDATER(comparison);
871VALIDATER(comp_op); VALIDATER(expr);
872VALIDATER(xor_expr); VALIDATER(and_expr);
873VALIDATER(shift_expr); VALIDATER(arith_expr);
874VALIDATER(term); VALIDATER(factor);
875VALIDATER(atom); VALIDATER(lambdef);
876VALIDATER(trailer); VALIDATER(subscript);
877VALIDATER(subscriptlist); VALIDATER(sliceop);
878VALIDATER(exprlist); VALIDATER(dictmaker);
879VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000880VALIDATER(listmaker); VALIDATER(yield_stmt);
Raymond Hettinger354433a2004-05-19 08:20:33 +0000881VALIDATER(testlist1); VALIDATER(gen_for);
882VALIDATER(gen_iter); VALIDATER(gen_if);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000883VALIDATER(testlist_gexp); VALIDATER(yield_expr);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +0000884VALIDATER(yield_or_testlist); VALIDATER(or_test);
885VALIDATER(old_test); VALIDATER(old_lambdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000886
Fred Drake0ac9b072000-09-12 21:58:06 +0000887#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000888
Fred Drakeff9ea482000-04-19 13:54:15 +0000889#define is_even(n) (((n) & 1) == 0)
890#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000891
892
893static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000894validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000895{
Fred Drake0ac9b072000-09-12 21:58:06 +0000896 if (TYPE(n) != t) {
897 PyErr_Format(parser_error, "Expected node type %d, got %d.",
898 t, TYPE(n));
899 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000900 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000901 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000902}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000903
904
Fred Drakee7ab64e2000-04-25 04:14:46 +0000905/* Verifies that the number of child nodes is exactly 'num', raising
906 * an exception if it isn't. The exception message does not indicate
907 * the exact number of nodes, allowing this to be used to raise the
908 * "right" exception when the wrong number of nodes is present in a
909 * specific variant of a statement's syntax. This is commonly used
910 * in that fashion.
911 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000912static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000913validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000914{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000915 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000916 PyErr_Format(parser_error,
917 "Illegal number of children for %s node.", name);
918 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000919 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000920 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000921}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000922
923
924static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000925validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000926{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000927 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000928 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000929
930 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000931 PyErr_Format(parser_error,
932 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000933 }
934 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000935}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000936
937
Guido van Rossum47478871996-08-21 14:32:37 +0000938/* X (',' X) [',']
939 */
940static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000941validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000942 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000943{
944 int nch = NCH(tree);
945 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000946 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000947
948 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000949 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000950 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000951 if (is_even(nch))
952 res = validate_comma(CHILD(tree, --nch));
953 if (res && nch > 1) {
954 int pos = 1;
955 for ( ; res && pos < nch; pos += 2)
956 res = (validate_comma(CHILD(tree, pos))
957 && vfunc(CHILD(tree, pos + 1)));
958 }
Guido van Rossum47478871996-08-21 14:32:37 +0000959 }
960 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000961}
Guido van Rossum47478871996-08-21 14:32:37 +0000962
963
Fred Drakecff283c2000-08-21 22:24:43 +0000964/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000965 *
966 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000967 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000968 */
Guido van Rossum47478871996-08-21 14:32:37 +0000969static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000970validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000971{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000972 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +0000973 int res = (validate_ntype(tree, classdef) &&
974 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000975
Guido van Rossum3d602e31996-07-21 02:33:56 +0000976 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000977 res = (validate_name(CHILD(tree, 0), "class")
978 && validate_ntype(CHILD(tree, 1), NAME)
979 && validate_colon(CHILD(tree, nch - 2))
980 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000981 }
Brett Cannonf4189912005-04-09 02:30:16 +0000982 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000983 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +0000984 }
985
986 if (res) {
987 if (nch == 7) {
988 res = ((validate_lparen(CHILD(tree, 2)) &&
989 validate_testlist(CHILD(tree, 3)) &&
990 validate_rparen(CHILD(tree, 4))));
991 }
992 else if (nch == 6) {
993 res = (validate_lparen(CHILD(tree,2)) &&
994 validate_rparen(CHILD(tree,3)));
995 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000996 }
997 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000998}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000999
1000
Guido van Rossum3d602e31996-07-21 02:33:56 +00001001/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001002 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001003 */
Guido van Rossum47478871996-08-21 14:32:37 +00001004static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001005validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001006{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001007 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001008 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001009 && (nch >= 4)
1010 && validate_name(CHILD(tree, 0), "if")
1011 && validate_test(CHILD(tree, 1))
1012 && validate_colon(CHILD(tree, 2))
1013 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001014
1015 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001016 /* ... 'else' ':' suite */
1017 res = (validate_name(CHILD(tree, nch - 3), "else")
1018 && validate_colon(CHILD(tree, nch - 2))
1019 && validate_suite(CHILD(tree, nch - 1)));
1020 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001021 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001022 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001023 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001024 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001025 /* Will catch the case for nch < 4 */
1026 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001027 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001028 /* ... ('elif' test ':' suite)+ ... */
1029 int j = 4;
1030 while ((j < nch) && res) {
1031 res = (validate_name(CHILD(tree, j), "elif")
1032 && validate_colon(CHILD(tree, j + 2))
1033 && validate_test(CHILD(tree, j + 1))
1034 && validate_suite(CHILD(tree, j + 3)));
1035 j += 4;
1036 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001037 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001038 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001039}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001040
1041
Guido van Rossum3d602e31996-07-21 02:33:56 +00001042/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001043 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001044 *
1045 */
Guido van Rossum47478871996-08-21 14:32:37 +00001046static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001047validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001048{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001049 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001050 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001051
Guido van Rossum3d602e31996-07-21 02:33:56 +00001052 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001053 res = (validate_lparen(CHILD(tree, 0))
1054 && validate_rparen(CHILD(tree, nch - 1)));
1055 if (res && (nch == 3))
1056 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001057 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001058 else {
1059 (void) validate_numnodes(tree, 2, "parameters");
1060 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001061 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001062}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001063
1064
Fred Drakecff283c2000-08-21 22:24:43 +00001065/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001066 *
1067 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001068 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001069 * | NEWLINE INDENT stmt+ DEDENT
1070 */
Guido van Rossum47478871996-08-21 14:32:37 +00001071static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001072validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001073{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001074 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001075 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001076
Guido van Rossum3d602e31996-07-21 02:33:56 +00001077 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001078 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001079 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001080 /* NEWLINE INDENT stmt+ DEDENT */
1081 res = (validate_newline(CHILD(tree, 0))
1082 && validate_indent(CHILD(tree, 1))
1083 && validate_stmt(CHILD(tree, 2))
1084 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001085
Fred Drakeff9ea482000-04-19 13:54:15 +00001086 if (res && (nch > 4)) {
1087 int i = 3;
1088 --nch; /* forget the DEDENT */
1089 for ( ; res && (i < nch); ++i)
1090 res = validate_stmt(CHILD(tree, i));
1091 }
1092 else if (nch < 4)
1093 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001094 }
1095 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001096}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001097
1098
Guido van Rossum47478871996-08-21 14:32:37 +00001099static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001100validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001101{
Guido van Rossum47478871996-08-21 14:32:37 +00001102 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001103 validate_test, "testlist"));
1104}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001105
1106
Guido van Rossum1c917072001-10-15 15:44:05 +00001107static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001108validate_testlist1(node *tree)
1109{
1110 return (validate_repeating_list(tree, testlist1,
1111 validate_test, "testlist1"));
1112}
1113
1114
1115static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001116validate_testlist_safe(node *tree)
1117{
1118 return (validate_repeating_list(tree, testlist_safe,
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001119 validate_old_test, "testlist_safe"));
Guido van Rossum1c917072001-10-15 15:44:05 +00001120}
1121
1122
Fred Drakecff283c2000-08-21 22:24:43 +00001123/* '*' NAME [',' '**' NAME] | '**' NAME
1124 */
1125static int
1126validate_varargslist_trailer(node *tree, int start)
1127{
1128 int nch = NCH(tree);
1129 int res = 0;
1130 int sym;
1131
1132 if (nch <= start) {
1133 err_string("expected variable argument trailer for varargslist");
1134 return 0;
1135 }
1136 sym = TYPE(CHILD(tree, start));
1137 if (sym == STAR) {
1138 /*
1139 * ('*' NAME [',' '**' NAME]
1140 */
1141 if (nch-start == 2)
1142 res = validate_name(CHILD(tree, start+1), NULL);
1143 else if (nch-start == 5)
1144 res = (validate_name(CHILD(tree, start+1), NULL)
1145 && validate_comma(CHILD(tree, start+2))
1146 && validate_doublestar(CHILD(tree, start+3))
1147 && validate_name(CHILD(tree, start+4), NULL));
1148 }
1149 else if (sym == DOUBLESTAR) {
1150 /*
1151 * '**' NAME
1152 */
1153 if (nch-start == 2)
1154 res = validate_name(CHILD(tree, start+1), NULL);
1155 }
1156 if (!res)
1157 err_string("illegal variable argument trailer for varargslist");
1158 return res;
1159}
1160
1161
1162/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001163 *
1164 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001165 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001166 * ('*' NAME [',' '**' NAME]
1167 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001168 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1169 *
1170 */
Guido van Rossum47478871996-08-21 14:32:37 +00001171static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001172validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001173{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001174 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001175 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001176 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001177
Fred Drakeb6429a22000-12-11 22:08:27 +00001178 if (!res)
1179 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001180 if (nch < 1) {
1181 err_string("varargslist missing child nodes");
1182 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001183 }
Fred Drakecff283c2000-08-21 22:24:43 +00001184 sym = TYPE(CHILD(tree, 0));
1185 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001186 /* whole thing matches:
1187 * '*' NAME [',' '**' NAME] | '**' NAME
1188 */
Fred Drakecff283c2000-08-21 22:24:43 +00001189 res = validate_varargslist_trailer(tree, 0);
1190 else if (sym == fpdef) {
1191 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001192
Fred Drakecff283c2000-08-21 22:24:43 +00001193 sym = TYPE(CHILD(tree, nch-1));
1194 if (sym == NAME) {
1195 /*
1196 * (fpdef ['=' test] ',')+
1197 * ('*' NAME [',' '**' NAME]
1198 * | '**' NAME)
1199 */
1200 /* skip over (fpdef ['=' test] ',')+ */
1201 while (res && (i+2 <= nch)) {
1202 res = validate_fpdef(CHILD(tree, i));
1203 ++i;
1204 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1205 res = (validate_equal(CHILD(tree, i))
1206 && validate_test(CHILD(tree, i+1)));
1207 if (res)
1208 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001209 }
Fred Drakecff283c2000-08-21 22:24:43 +00001210 if (res && i < nch) {
1211 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001212 ++i;
1213 if (res && i < nch
1214 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1215 || TYPE(CHILD(tree, i)) == STAR))
1216 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001217 }
1218 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001219 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1220 * i --^^^
1221 */
Fred Drakecff283c2000-08-21 22:24:43 +00001222 if (res)
1223 res = validate_varargslist_trailer(tree, i);
1224 }
1225 else {
1226 /*
1227 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1228 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001229 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001230 if (sym == COMMA) {
1231 res = validate_comma(CHILD(tree, nch-1));
1232 if (!res)
1233 return 0;
1234 --nch;
1235 }
1236 /*
1237 * fpdef ['=' test] (',' fpdef ['=' test])*
1238 */
1239 res = validate_fpdef(CHILD(tree, 0));
1240 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001241 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1242 res = (validate_equal(CHILD(tree, i))
1243 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001244 i += 2;
1245 }
1246 /*
1247 * ... (',' fpdef ['=' test])*
1248 * i ---^^^
1249 */
1250 while (res && (nch - i) >= 2) {
1251 res = (validate_comma(CHILD(tree, i))
1252 && validate_fpdef(CHILD(tree, i+1)));
1253 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001254 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1255 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001256 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001257 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001258 }
1259 }
1260 if (res && nch - i != 0) {
1261 res = 0;
1262 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001263 }
1264 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001265 }
Fred Drakecff283c2000-08-21 22:24:43 +00001266 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001267}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001268
1269
Fred Drakecff283c2000-08-21 22:24:43 +00001270/* list_iter: list_for | list_if
1271 */
1272static int
1273validate_list_iter(node *tree)
1274{
1275 int res = (validate_ntype(tree, list_iter)
1276 && validate_numnodes(tree, 1, "list_iter"));
1277 if (res && TYPE(CHILD(tree, 0)) == list_for)
1278 res = validate_list_for(CHILD(tree, 0));
1279 else
1280 res = validate_list_if(CHILD(tree, 0));
1281
1282 return res;
1283}
1284
Raymond Hettinger354433a2004-05-19 08:20:33 +00001285/* gen_iter: gen_for | gen_if
1286 */
1287static int
1288validate_gen_iter(node *tree)
1289{
1290 int res = (validate_ntype(tree, gen_iter)
1291 && validate_numnodes(tree, 1, "gen_iter"));
1292 if (res && TYPE(CHILD(tree, 0)) == gen_for)
1293 res = validate_gen_for(CHILD(tree, 0));
1294 else
1295 res = validate_gen_if(CHILD(tree, 0));
1296
1297 return res;
1298}
1299
Fred Drakecff283c2000-08-21 22:24:43 +00001300/* list_for: 'for' exprlist 'in' testlist [list_iter]
1301 */
1302static int
1303validate_list_for(node *tree)
1304{
1305 int nch = NCH(tree);
1306 int res;
1307
1308 if (nch == 5)
1309 res = validate_list_iter(CHILD(tree, 4));
1310 else
1311 res = validate_numnodes(tree, 4, "list_for");
1312
1313 if (res)
1314 res = (validate_name(CHILD(tree, 0), "for")
1315 && validate_exprlist(CHILD(tree, 1))
1316 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001317 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001318
1319 return res;
1320}
1321
Raymond Hettinger354433a2004-05-19 08:20:33 +00001322/* gen_for: 'for' exprlist 'in' test [gen_iter]
1323 */
1324static int
1325validate_gen_for(node *tree)
1326{
1327 int nch = NCH(tree);
1328 int res;
1329
1330 if (nch == 5)
1331 res = validate_gen_iter(CHILD(tree, 4));
1332 else
1333 res = validate_numnodes(tree, 4, "gen_for");
1334
1335 if (res)
1336 res = (validate_name(CHILD(tree, 0), "for")
1337 && validate_exprlist(CHILD(tree, 1))
1338 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001339 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001340
1341 return res;
1342}
1343
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001344/* list_if: 'if' old_test [list_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001345 */
1346static int
1347validate_list_if(node *tree)
1348{
1349 int nch = NCH(tree);
1350 int res;
1351
1352 if (nch == 3)
1353 res = validate_list_iter(CHILD(tree, 2));
1354 else
1355 res = validate_numnodes(tree, 2, "list_if");
1356
1357 if (res)
1358 res = (validate_name(CHILD(tree, 0), "if")
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001359 && validate_old_test(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001360
1361 return res;
1362}
1363
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001364/* gen_if: 'if' old_test [gen_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001365 */
1366static int
1367validate_gen_if(node *tree)
1368{
1369 int nch = NCH(tree);
1370 int res;
1371
1372 if (nch == 3)
1373 res = validate_gen_iter(CHILD(tree, 2));
1374 else
1375 res = validate_numnodes(tree, 2, "gen_if");
1376
1377 if (res)
1378 res = (validate_name(CHILD(tree, 0), "if")
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001379 && validate_old_test(CHILD(tree, 1)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001380
1381 return res;
1382}
Fred Drakecff283c2000-08-21 22:24:43 +00001383
1384/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001385 *
1386 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001387 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001388 * | '(' fplist ')'
1389 */
Guido van Rossum47478871996-08-21 14:32:37 +00001390static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001391validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001392{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001393 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001394 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001395
Guido van Rossum3d602e31996-07-21 02:33:56 +00001396 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001397 if (nch == 1)
1398 res = validate_ntype(CHILD(tree, 0), NAME);
1399 else if (nch == 3)
1400 res = (validate_lparen(CHILD(tree, 0))
1401 && validate_fplist(CHILD(tree, 1))
1402 && validate_rparen(CHILD(tree, 2)));
1403 else
1404 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001405 }
1406 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001407}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001408
1409
Guido van Rossum47478871996-08-21 14:32:37 +00001410static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001411validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001412{
Guido van Rossum47478871996-08-21 14:32:37 +00001413 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001414 validate_fpdef, "fplist"));
1415}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001416
1417
Guido van Rossum3d602e31996-07-21 02:33:56 +00001418/* simple_stmt | compound_stmt
1419 *
1420 */
Guido van Rossum47478871996-08-21 14:32:37 +00001421static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001422validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001423{
1424 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001425 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001426
Guido van Rossum3d602e31996-07-21 02:33:56 +00001427 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001428 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001429
Fred Drakeff9ea482000-04-19 13:54:15 +00001430 if (TYPE(tree) == simple_stmt)
1431 res = validate_simple_stmt(tree);
1432 else
1433 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001434 }
1435 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001436}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001437
1438
Guido van Rossum3d602e31996-07-21 02:33:56 +00001439/* small_stmt (';' small_stmt)* [';'] NEWLINE
1440 *
1441 */
Guido van Rossum47478871996-08-21 14:32:37 +00001442static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001443validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001444{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001445 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001446 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001447 && (nch >= 2)
1448 && validate_small_stmt(CHILD(tree, 0))
1449 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001450
Guido van Rossum3d602e31996-07-21 02:33:56 +00001451 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001452 res = validate_numnodes(tree, 2, "simple_stmt");
1453 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001454 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001455 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001456 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001457 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001458
Fred Drakeff9ea482000-04-19 13:54:15 +00001459 for (i = 1; res && (i < nch); i += 2)
1460 res = (validate_semi(CHILD(tree, i))
1461 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001462 }
1463 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001464}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001465
1466
Guido van Rossum47478871996-08-21 14:32:37 +00001467static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001468validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001469{
1470 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001471 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001472
Fred Drake0ac9b072000-09-12 21:58:06 +00001473 if (res) {
1474 int ntype = TYPE(CHILD(tree, 0));
1475
1476 if ( (ntype == expr_stmt)
1477 || (ntype == print_stmt)
1478 || (ntype == del_stmt)
1479 || (ntype == pass_stmt)
1480 || (ntype == flow_stmt)
1481 || (ntype == import_stmt)
1482 || (ntype == global_stmt)
1483 || (ntype == assert_stmt)
1484 || (ntype == exec_stmt))
1485 res = validate_node(CHILD(tree, 0));
1486 else {
1487 res = 0;
1488 err_string("illegal small_stmt child type");
1489 }
1490 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001491 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001492 res = 0;
1493 PyErr_Format(parser_error,
1494 "Unrecognized child node of small_stmt: %d.",
1495 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001496 }
1497 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001498}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001499
1500
1501/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001502 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001503 */
Guido van Rossum47478871996-08-21 14:32:37 +00001504static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001505validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001506{
1507 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001508 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001509 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001510
1511 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001512 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001513
1514 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001515 ntype = TYPE(tree);
1516 if ( (ntype == if_stmt)
1517 || (ntype == while_stmt)
1518 || (ntype == for_stmt)
1519 || (ntype == try_stmt)
1520 || (ntype == funcdef)
1521 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001522 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001523 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001524 res = 0;
1525 PyErr_Format(parser_error,
1526 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001527 }
1528 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001529}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001530
1531
Guido van Rossum47478871996-08-21 14:32:37 +00001532static int
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001533validate_yield_or_testlist(node *tree)
1534{
1535 if (TYPE(tree) == yield_expr)
1536 return validate_yield_expr(tree);
1537 else
1538 return validate_testlist(tree);
1539}
1540
1541static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001542validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001543{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001544 int j;
1545 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001546 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001547 && is_odd(nch)
1548 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001549
Fred Drake28f739a2000-08-25 22:42:40 +00001550 if (res && nch == 3
1551 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001552 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1553 && validate_yield_or_testlist(CHILD(tree, 2));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001554
Fred Drake28f739a2000-08-25 22:42:40 +00001555 if (res) {
1556 char *s = STR(CHILD(CHILD(tree, 1), 0));
1557
1558 res = (strcmp(s, "+=") == 0
1559 || strcmp(s, "-=") == 0
1560 || strcmp(s, "*=") == 0
1561 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001562 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001563 || strcmp(s, "%=") == 0
1564 || strcmp(s, "&=") == 0
1565 || strcmp(s, "|=") == 0
1566 || strcmp(s, "^=") == 0
1567 || strcmp(s, "<<=") == 0
1568 || strcmp(s, ">>=") == 0
1569 || strcmp(s, "**=") == 0);
1570 if (!res)
1571 err_string("illegal augmmented assignment operator");
1572 }
1573 }
1574 else {
1575 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001576 res = validate_equal(CHILD(tree, j))
1577 && validate_yield_or_testlist(CHILD(tree, j + 1));
Fred Drake28f739a2000-08-25 22:42:40 +00001578 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001579 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001580}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001581
1582
Guido van Rossum3d602e31996-07-21 02:33:56 +00001583/* print_stmt:
1584 *
Fred Drakecff283c2000-08-21 22:24:43 +00001585 * 'print' ( [ test (',' test)* [','] ]
1586 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001587 */
Guido van Rossum47478871996-08-21 14:32:37 +00001588static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001589validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001590{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001591 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001592 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001593 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001594 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001595
Fred Drakecff283c2000-08-21 22:24:43 +00001596 if (res && nch > 1) {
1597 int sym = TYPE(CHILD(tree, 1));
1598 int i = 1;
1599 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001600
Fred Drakecff283c2000-08-21 22:24:43 +00001601 if (sym == test)
1602 res = validate_test(CHILD(tree, i++));
1603 else {
1604 if (nch < 3)
1605 res = validate_numnodes(tree, 3, "print_stmt");
1606 else {
1607 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1608 && validate_test(CHILD(tree, i+1)));
1609 i += 2;
1610 allow_trailing_comma = 0;
1611 }
1612 }
1613 if (res) {
1614 /* ... (',' test)* [','] */
1615 while (res && i+2 <= nch) {
1616 res = (validate_comma(CHILD(tree, i))
1617 && validate_test(CHILD(tree, i+1)));
1618 allow_trailing_comma = 1;
1619 i += 2;
1620 }
1621 if (res && !allow_trailing_comma)
1622 res = validate_numnodes(tree, i, "print_stmt");
1623 else if (res && i < nch)
1624 res = validate_comma(CHILD(tree, i));
1625 }
1626 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001627 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001628}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001629
1630
Guido van Rossum47478871996-08-21 14:32:37 +00001631static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001632validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001633{
1634 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001635 && validate_name(CHILD(tree, 0), "del")
1636 && validate_exprlist(CHILD(tree, 1)));
1637}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001638
1639
Guido van Rossum47478871996-08-21 14:32:37 +00001640static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001641validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001642{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001643 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001644 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001645 && ((nch == 1) || (nch == 2))
1646 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001647
Guido van Rossum3d602e31996-07-21 02:33:56 +00001648 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001649 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001650
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001651 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001652}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001653
1654
Guido van Rossum47478871996-08-21 14:32:37 +00001655static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001656validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001657{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001658 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001659 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001660 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001661
Guido van Rossum3d602e31996-07-21 02:33:56 +00001662 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001663 res = validate_name(CHILD(tree, 0), "raise");
1664 if (res && (nch >= 2))
1665 res = validate_test(CHILD(tree, 1));
1666 if (res && nch > 2) {
1667 res = (validate_comma(CHILD(tree, 2))
1668 && validate_test(CHILD(tree, 3)));
1669 if (res && (nch > 4))
1670 res = (validate_comma(CHILD(tree, 4))
1671 && validate_test(CHILD(tree, 5)));
1672 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001673 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001674 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001675 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001676 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001677 res = (validate_comma(CHILD(tree, 2))
1678 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001679
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001680 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001681}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001682
1683
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001684/* yield_expr: 'yield' [testlist]
1685 */
1686static int
1687validate_yield_expr(node *tree)
1688{
1689 int nch = NCH(tree);
1690 int res = (validate_ntype(tree, yield_expr)
1691 && ((nch == 1) || (nch == 2))
1692 && validate_name(CHILD(tree, 0), "yield"));
1693
1694 if (res && (nch == 2))
1695 res = validate_testlist(CHILD(tree, 1));
1696
1697 return (res);
1698}
1699
1700
1701/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001702 */
1703static int
1704validate_yield_stmt(node *tree)
1705{
1706 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001707 && validate_numnodes(tree, 1, "yield_stmt")
1708 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001709}
1710
1711
Fred Drakecff283c2000-08-21 22:24:43 +00001712static int
1713validate_import_as_name(node *tree)
1714{
1715 int nch = NCH(tree);
1716 int ok = validate_ntype(tree, import_as_name);
1717
1718 if (ok) {
1719 if (nch == 1)
1720 ok = validate_name(CHILD(tree, 0), NULL);
1721 else if (nch == 3)
1722 ok = (validate_name(CHILD(tree, 0), NULL)
1723 && validate_name(CHILD(tree, 1), "as")
1724 && validate_name(CHILD(tree, 2), NULL));
1725 else
1726 ok = validate_numnodes(tree, 3, "import_as_name");
1727 }
1728 return ok;
1729}
1730
1731
Fred Drake71137082001-01-07 05:59:59 +00001732/* dotted_name: NAME ("." NAME)*
1733 */
1734static int
1735validate_dotted_name(node *tree)
1736{
1737 int nch = NCH(tree);
1738 int res = (validate_ntype(tree, dotted_name)
1739 && is_odd(nch)
1740 && validate_name(CHILD(tree, 0), NULL));
1741 int i;
1742
1743 for (i = 1; res && (i < nch); i += 2) {
1744 res = (validate_dot(CHILD(tree, i))
1745 && validate_name(CHILD(tree, i+1), NULL));
1746 }
1747 return res;
1748}
1749
1750
Fred Drakecff283c2000-08-21 22:24:43 +00001751/* dotted_as_name: dotted_name [NAME NAME]
1752 */
1753static int
1754validate_dotted_as_name(node *tree)
1755{
1756 int nch = NCH(tree);
1757 int res = validate_ntype(tree, dotted_as_name);
1758
1759 if (res) {
1760 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001761 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001762 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001763 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001764 && validate_name(CHILD(tree, 1), "as")
1765 && validate_name(CHILD(tree, 2), NULL));
1766 else {
1767 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001768 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001769 }
1770 }
1771 return res;
1772}
1773
1774
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001775/* dotted_as_name (',' dotted_as_name)* */
1776static int
1777validate_dotted_as_names(node *tree)
1778{
1779 int nch = NCH(tree);
1780 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1781 int i;
1782
1783 for (i = 1; res && (i < nch); i += 2)
1784 res = (validate_comma(CHILD(tree, i))
1785 && validate_dotted_as_name(CHILD(tree, i + 1)));
1786 return (res);
1787}
1788
1789
1790/* import_as_name (',' import_as_name)* [','] */
1791static int
1792validate_import_as_names(node *tree)
1793{
1794 int nch = NCH(tree);
1795 int res = validate_import_as_name(CHILD(tree, 0));
1796 int i;
1797
1798 for (i = 1; res && (i + 1 < nch); i += 2)
1799 res = (validate_comma(CHILD(tree, i))
1800 && validate_import_as_name(CHILD(tree, i + 1)));
1801 return (res);
1802}
1803
1804
1805/* 'import' dotted_as_names */
1806static int
1807validate_import_name(node *tree)
1808{
1809 return (validate_ntype(tree, import_name)
1810 && validate_numnodes(tree, 2, "import_name")
1811 && validate_name(CHILD(tree, 0), "import")
1812 && validate_dotted_as_names(CHILD(tree, 1)));
1813}
1814
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001815/* Helper function to count the number of leading dots in
1816 * 'from ...module import name'
1817 */
1818static int
1819count_from_dots(node *tree)
1820{
1821 int i;
1822 for (i = 0; i < NCH(tree); i++)
1823 if (TYPE(CHILD(tree, i)) != DOT)
1824 break;
1825 return i;
1826}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001827
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001828/* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001829 * import_as_names
Guido van Rossum3d602e31996-07-21 02:33:56 +00001830 */
Guido van Rossum47478871996-08-21 14:32:37 +00001831static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001832validate_import_from(node *tree)
1833{
1834 int nch = NCH(tree);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001835 int ndots = count_from_dots(tree);
1836 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1837 int offset = ndots + havename;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001838 int res = validate_ntype(tree, import_from)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001839 && (nch >= 4 + ndots)
1840 && validate_name(CHILD(tree, 0), "from")
1841 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1842 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001843
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001844 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1845 res = ((nch == offset + 5)
1846 && validate_lparen(CHILD(tree, offset + 2))
1847 && validate_import_as_names(CHILD(tree, offset + 3))
1848 && validate_rparen(CHILD(tree, offset + 4)));
1849 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1850 res = validate_import_as_names(CHILD(tree, offset + 2));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001851 return (res);
1852}
1853
1854
1855/* import_stmt: import_name | import_from */
1856static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001857validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001858{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001859 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001860 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001861
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001862 if (res) {
1863 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001864
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001865 if (ntype == import_name || ntype == import_from)
1866 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001867 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001868 res = 0;
1869 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001870 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001871 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001872 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001873 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001874 PyErr_Format(parser_error,
1875 "Unrecognized child node of import_stmt: %d.",
1876 TYPE(CHILD(tree, 0)));
1877 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001878 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001879}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001880
1881
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001882
1883
Guido van Rossum47478871996-08-21 14:32:37 +00001884static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001885validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001886{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001887 int j;
1888 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001889 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001890 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001891
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001892 if (!res && !PyErr_Occurred())
1893 err_string("illegal global statement");
1894
Guido van Rossum3d602e31996-07-21 02:33:56 +00001895 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001896 res = (validate_name(CHILD(tree, 0), "global")
1897 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001898 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001899 res = (validate_comma(CHILD(tree, j))
1900 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001901
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001902 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001903}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001904
1905
Guido van Rossum3d602e31996-07-21 02:33:56 +00001906/* exec_stmt:
1907 *
1908 * 'exec' expr ['in' test [',' test]]
1909 */
Guido van Rossum47478871996-08-21 14:32:37 +00001910static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001911validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001912{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001913 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001914 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001915 && ((nch == 2) || (nch == 4) || (nch == 6))
1916 && validate_name(CHILD(tree, 0), "exec")
1917 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001918
Guido van Rossum3d602e31996-07-21 02:33:56 +00001919 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001920 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001921 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001922 res = (validate_name(CHILD(tree, 2), "in")
1923 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001924 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001925 res = (validate_comma(CHILD(tree, 4))
1926 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001927
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001928 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001929}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001930
1931
Guido van Rossum925e5471997-04-02 05:32:13 +00001932/* assert_stmt:
1933 *
1934 * 'assert' test [',' test]
1935 */
1936static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001937validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001938{
1939 int nch = NCH(tree);
1940 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001941 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001942 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001943 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001944
1945 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001946 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001947 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001948 res = (validate_comma(CHILD(tree, 2))
1949 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001950
1951 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001952}
Guido van Rossum925e5471997-04-02 05:32:13 +00001953
1954
Guido van Rossum47478871996-08-21 14:32:37 +00001955static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001956validate_while(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, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001960 && ((nch == 4) || (nch == 7))
1961 && validate_name(CHILD(tree, 0), "while")
1962 && validate_test(CHILD(tree, 1))
1963 && validate_colon(CHILD(tree, 2))
1964 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001965
Guido van Rossum3d602e31996-07-21 02:33:56 +00001966 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001967 res = (validate_name(CHILD(tree, 4), "else")
1968 && validate_colon(CHILD(tree, 5))
1969 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001970
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001971 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001972}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001973
1974
Guido van Rossum47478871996-08-21 14:32:37 +00001975static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001976validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001977{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001978 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001979 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001980 && ((nch == 6) || (nch == 9))
1981 && validate_name(CHILD(tree, 0), "for")
1982 && validate_exprlist(CHILD(tree, 1))
1983 && validate_name(CHILD(tree, 2), "in")
1984 && validate_testlist(CHILD(tree, 3))
1985 && validate_colon(CHILD(tree, 4))
1986 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001987
Guido van Rossum3d602e31996-07-21 02:33:56 +00001988 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001989 res = (validate_name(CHILD(tree, 6), "else")
1990 && validate_colon(CHILD(tree, 7))
1991 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001992
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001993 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001994}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001995
1996
Guido van Rossum3d602e31996-07-21 02:33:56 +00001997/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001998 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001999 * | 'try' ':' suite 'finally' ':' suite
2000 *
2001 */
Guido van Rossum47478871996-08-21 14:32:37 +00002002static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002003validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002004{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002005 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002006 int pos = 3;
2007 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002008 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002009
2010 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002011 res = (validate_name(CHILD(tree, 0), "try")
2012 && validate_colon(CHILD(tree, 1))
2013 && validate_suite(CHILD(tree, 2))
2014 && validate_colon(CHILD(tree, nch - 2))
2015 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00002016 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00002017 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00002018 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
2019 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00002020
2021 PyErr_Format(parser_error,
2022 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002023 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002024 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002025 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002026 res = (validate_except_clause(CHILD(tree, pos))
2027 && validate_colon(CHILD(tree, pos + 1))
2028 && validate_suite(CHILD(tree, pos + 2)));
2029 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002030 }
2031 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002032 res = validate_ntype(CHILD(tree, pos), NAME);
2033 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
2034 res = (validate_numnodes(tree, 6, "try/finally")
2035 && validate_colon(CHILD(tree, 4))
2036 && validate_suite(CHILD(tree, 5)));
2037 else if (res) {
2038 if (nch == (pos + 3)) {
2039 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
2040 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
2041 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00002042 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00002043 }
2044 else if (nch == (pos + 6)) {
2045 res = (validate_name(CHILD(tree, pos), "except")
2046 && validate_colon(CHILD(tree, pos + 1))
2047 && validate_suite(CHILD(tree, pos + 2))
2048 && validate_name(CHILD(tree, pos + 3), "else"));
2049 }
2050 else
2051 res = validate_numnodes(tree, pos + 3, "try/except");
2052 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002053 }
2054 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002055}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002056
2057
Guido van Rossum47478871996-08-21 14:32:37 +00002058static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002059validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002060{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002061 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002062 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00002063 && ((nch == 1) || (nch == 2) || (nch == 4))
2064 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002065
Guido van Rossum3d602e31996-07-21 02:33:56 +00002066 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002067 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002068 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002069 res = (validate_comma(CHILD(tree, 2))
2070 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002071
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002072 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002073}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002074
2075
Guido van Rossum47478871996-08-21 14:32:37 +00002076static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002077validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002078{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002079 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002080 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002081
Guido van Rossum3d602e31996-07-21 02:33:56 +00002082 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002083 res = ((nch == 1)
2084 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002085 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002086 res = validate_or_test(CHILD(tree, 0));
2087 res = (res && (nch == 1 || (nch == 5 &&
2088 validate_name(CHILD(tree, 1), "if") &&
2089 validate_or_test(CHILD(tree, 2)) &&
2090 validate_name(CHILD(tree, 3), "else") &&
2091 validate_test(CHILD(tree, 4)))));
2092 }
2093 return (res);
2094}
2095
2096static int
2097validate_old_test(node *tree)
2098{
2099 int nch = NCH(tree);
2100 int res = validate_ntype(tree, old_test) && (nch == 1);
2101
2102 if (res && (TYPE(CHILD(tree, 0)) == old_lambdef))
2103 res = (validate_old_lambdef(CHILD(tree, 0)));
2104 else if (res) {
2105 res = (validate_or_test(CHILD(tree, 0)));
2106 }
2107 return (res);
2108}
2109
2110static int
2111validate_or_test(node *tree)
2112{
2113 int nch = NCH(tree);
2114 int res = validate_ntype(tree, or_test) && is_odd(nch);
2115
2116 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002117 int pos;
2118 res = validate_and_test(CHILD(tree, 0));
2119 for (pos = 1; res && (pos < nch); pos += 2)
2120 res = (validate_name(CHILD(tree, pos), "or")
2121 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002122 }
2123 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002124}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002125
2126
Guido van Rossum47478871996-08-21 14:32:37 +00002127static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002128validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002129{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002130 int pos;
2131 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002132 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002133 && is_odd(nch)
2134 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002135
Guido van Rossum3d602e31996-07-21 02:33:56 +00002136 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002137 res = (validate_name(CHILD(tree, pos), "and")
2138 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002139
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002140 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002141}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002142
2143
Guido van Rossum47478871996-08-21 14:32:37 +00002144static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002145validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002146{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002147 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002148 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002149
Guido van Rossum3d602e31996-07-21 02:33:56 +00002150 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002151 if (nch == 2)
2152 res = (validate_name(CHILD(tree, 0), "not")
2153 && validate_not_test(CHILD(tree, 1)));
2154 else if (nch == 1)
2155 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002156 }
2157 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002158}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002159
2160
Guido van Rossum47478871996-08-21 14:32:37 +00002161static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002162validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002163{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002164 int pos;
2165 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002166 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002167 && is_odd(nch)
2168 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002169
Guido van Rossum3d602e31996-07-21 02:33:56 +00002170 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002171 res = (validate_comp_op(CHILD(tree, pos))
2172 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002173
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002174 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002175}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002176
2177
Guido van Rossum47478871996-08-21 14:32:37 +00002178static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002179validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002180{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002181 int res = 0;
2182 int nch = NCH(tree);
2183
Guido van Rossum3d602e31996-07-21 02:33:56 +00002184 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002185 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002186 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002187 /*
2188 * Only child will be a terminal with a well-defined symbolic name
2189 * or a NAME with a string of either 'is' or 'in'
2190 */
2191 tree = CHILD(tree, 0);
2192 switch (TYPE(tree)) {
2193 case LESS:
2194 case GREATER:
2195 case EQEQUAL:
2196 case EQUAL:
2197 case LESSEQUAL:
2198 case GREATEREQUAL:
2199 case NOTEQUAL:
2200 res = 1;
2201 break;
2202 case NAME:
2203 res = ((strcmp(STR(tree), "in") == 0)
2204 || (strcmp(STR(tree), "is") == 0));
2205 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002206 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002207 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002208 }
2209 break;
2210 default:
Fred Drake661ea262000-10-24 19:57:45 +00002211 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002212 break;
2213 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002214 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002215 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002216 res = (validate_ntype(CHILD(tree, 0), NAME)
2217 && validate_ntype(CHILD(tree, 1), NAME)
2218 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2219 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2220 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2221 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2222 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002223 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002224 }
2225 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002226}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002227
2228
Guido van Rossum47478871996-08-21 14:32:37 +00002229static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002230validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002231{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002232 int j;
2233 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002234 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002235 && is_odd(nch)
2236 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002237
Guido van Rossum3d602e31996-07-21 02:33:56 +00002238 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002239 res = (validate_xor_expr(CHILD(tree, j))
2240 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002241
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002242 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002243}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002244
2245
Guido van Rossum47478871996-08-21 14:32:37 +00002246static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002247validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002248{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002249 int j;
2250 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002251 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002252 && is_odd(nch)
2253 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002254
Guido van Rossum3d602e31996-07-21 02:33:56 +00002255 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002256 res = (validate_circumflex(CHILD(tree, j - 1))
2257 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002258
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002259 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002260}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002261
2262
Guido van Rossum47478871996-08-21 14:32:37 +00002263static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002264validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002265{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002266 int pos;
2267 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002268 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002269 && is_odd(nch)
2270 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002271
Guido van Rossum3d602e31996-07-21 02:33:56 +00002272 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002273 res = (validate_ampersand(CHILD(tree, pos))
2274 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002275
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002276 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002277}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002278
2279
2280static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002281validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002282 {
2283 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002284 int nch = NCH(tree);
2285 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002286 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002287
Guido van Rossum3d602e31996-07-21 02:33:56 +00002288 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002289 if (TYPE(CHILD(tree, pos)) != op1)
2290 res = validate_ntype(CHILD(tree, pos), op2);
2291 if (res)
2292 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002293 }
2294 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002295}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002296
2297
Guido van Rossum47478871996-08-21 14:32:37 +00002298static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002299validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002300{
2301 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002302 && validate_chain_two_ops(tree, validate_arith_expr,
2303 LEFTSHIFT, RIGHTSHIFT));
2304}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002305
2306
Guido van Rossum47478871996-08-21 14:32:37 +00002307static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002308validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002309{
2310 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002311 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2312}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002313
2314
Guido van Rossum47478871996-08-21 14:32:37 +00002315static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002316validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002317{
2318 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002319 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002320 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002321 && is_odd(nch)
2322 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002323
Guido van Rossum3d602e31996-07-21 02:33:56 +00002324 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002325 res = (((TYPE(CHILD(tree, pos)) == STAR)
2326 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002327 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002328 || (TYPE(CHILD(tree, pos)) == PERCENT))
2329 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002330
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/* factor:
2336 *
2337 * factor: ('+'|'-'|'~') factor | power
2338 */
Guido van Rossum47478871996-08-21 14:32:37 +00002339static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002340validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002341{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002342 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002343 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002344 && (((nch == 2)
2345 && ((TYPE(CHILD(tree, 0)) == PLUS)
2346 || (TYPE(CHILD(tree, 0)) == MINUS)
2347 || (TYPE(CHILD(tree, 0)) == TILDE))
2348 && validate_factor(CHILD(tree, 1)))
2349 || ((nch == 1)
2350 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002351 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002352}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002353
2354
Guido van Rossum3d602e31996-07-21 02:33:56 +00002355/* power:
2356 *
2357 * power: atom trailer* ('**' factor)*
2358 */
Guido van Rossum47478871996-08-21 14:32:37 +00002359static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002360validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002361{
2362 int pos = 1;
2363 int nch = NCH(tree);
2364 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002365 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002366
2367 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002368 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002369 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002370 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002371 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002372 return (0);
2373 }
2374 for ( ; res && (pos < (nch - 1)); pos += 2)
2375 res = (validate_doublestar(CHILD(tree, pos))
2376 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002377 }
2378 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002379}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002380
2381
Guido van Rossum47478871996-08-21 14:32:37 +00002382static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002383validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002384{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002385 int pos;
2386 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002387 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002388
Fred Drakecff283c2000-08-21 22:24:43 +00002389 if (res && nch < 1)
2390 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002391 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002392 switch (TYPE(CHILD(tree, 0))) {
2393 case LPAR:
2394 res = ((nch <= 3)
2395 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002396
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002397 if (res && (nch == 3)) {
2398 if (TYPE(CHILD(tree, 1))==yield_expr)
2399 res = validate_yield_expr(CHILD(tree, 1));
2400 else
2401 res = validate_testlist_gexp(CHILD(tree, 1));
2402 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002403 break;
2404 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002405 if (nch == 2)
2406 res = validate_ntype(CHILD(tree, 1), RSQB);
2407 else if (nch == 3)
2408 res = (validate_listmaker(CHILD(tree, 1))
2409 && validate_ntype(CHILD(tree, 2), RSQB));
2410 else {
2411 res = 0;
2412 err_string("illegal list display atom");
2413 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002414 break;
2415 case LBRACE:
2416 res = ((nch <= 3)
2417 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002418
Fred Drakeff9ea482000-04-19 13:54:15 +00002419 if (res && (nch == 3))
2420 res = validate_dictmaker(CHILD(tree, 1));
2421 break;
2422 case BACKQUOTE:
2423 res = ((nch == 3)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002424 && validate_testlist1(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002425 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2426 break;
2427 case NAME:
2428 case NUMBER:
2429 res = (nch == 1);
2430 break;
2431 case STRING:
2432 for (pos = 1; res && (pos < nch); ++pos)
2433 res = validate_ntype(CHILD(tree, pos), STRING);
2434 break;
2435 default:
2436 res = 0;
2437 break;
2438 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002439 }
2440 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002441}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002442
2443
Fred Drake85bf3bb2000-08-23 15:35:26 +00002444/* listmaker:
2445 * test ( list_for | (',' test)* [','] )
2446 */
Fred Drakecff283c2000-08-21 22:24:43 +00002447static int
2448validate_listmaker(node *tree)
2449{
2450 int nch = NCH(tree);
2451 int ok = nch;
2452
2453 if (nch == 0)
2454 err_string("missing child nodes of listmaker");
2455 else
2456 ok = validate_test(CHILD(tree, 0));
2457
2458 /*
Raymond Hettinger354433a2004-05-19 08:20:33 +00002459 * list_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002460 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002461 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2462 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002463 else {
2464 /* (',' test)* [','] */
2465 int i = 1;
2466 while (ok && nch - i >= 2) {
2467 ok = (validate_comma(CHILD(tree, i))
2468 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002469 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002470 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002471 if (ok && i == nch-1)
2472 ok = validate_comma(CHILD(tree, i));
2473 else if (i != nch) {
2474 ok = 0;
2475 err_string("illegal trailing nodes for listmaker");
2476 }
Fred Drakecff283c2000-08-21 22:24:43 +00002477 }
2478 return ok;
2479}
2480
Raymond Hettinger354433a2004-05-19 08:20:33 +00002481/* testlist_gexp:
2482 * test ( gen_for | (',' test)* [','] )
2483 */
2484static int
2485validate_testlist_gexp(node *tree)
2486{
2487 int nch = NCH(tree);
2488 int ok = nch;
2489
2490 if (nch == 0)
2491 err_string("missing child nodes of testlist_gexp");
2492 else {
2493 ok = validate_test(CHILD(tree, 0));
2494 }
2495
2496 /*
2497 * gen_for | (',' test)* [',']
2498 */
2499 if (nch == 2 && TYPE(CHILD(tree, 1)) == gen_for)
2500 ok = validate_gen_for(CHILD(tree, 1));
2501 else {
2502 /* (',' test)* [','] */
2503 int i = 1;
2504 while (ok && nch - i >= 2) {
2505 ok = (validate_comma(CHILD(tree, i))
2506 && validate_test(CHILD(tree, i+1)));
2507 i += 2;
2508 }
2509 if (ok && i == nch-1)
2510 ok = validate_comma(CHILD(tree, i));
2511 else if (i != nch) {
2512 ok = 0;
2513 err_string("illegal trailing nodes for testlist_gexp");
2514 }
2515 }
2516 return ok;
2517}
Fred Drakecff283c2000-08-21 22:24:43 +00002518
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002519/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002520 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002521 */
2522static int
2523validate_decorator(node *tree)
2524{
2525 int ok;
2526 int nch = NCH(tree);
2527 ok = (validate_ntype(tree, decorator) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002528 (nch == 3 || nch == 5 || nch == 6) &&
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002529 validate_at(CHILD(tree, 0)) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002530 validate_dotted_name(CHILD(tree, 1)) &&
2531 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002532
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002533 if (ok && nch != 3) {
2534 ok = (validate_lparen(CHILD(tree, 2)) &&
2535 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002536
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002537 if (ok && nch == 6)
2538 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002539 }
2540
2541 return ok;
2542}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002543
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002544/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002545 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002546 */
2547static int
2548validate_decorators(node *tree)
2549{
2550 int i, nch, ok;
2551 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002552 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002553
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002554 for (i = 0; ok && i < nch; ++i)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002555 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002556
2557 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002558}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002559
Guido van Rossum3d602e31996-07-21 02:33:56 +00002560/* funcdef:
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002561 *
2562 * -6 -5 -4 -3 -2 -1
2563 * [decorators] 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002564 */
Guido van Rossum47478871996-08-21 14:32:37 +00002565static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002566validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002567{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002568 int nch = NCH(tree);
2569 int ok = (validate_ntype(tree, funcdef)
2570 && ((nch == 5) || (nch == 6))
2571 && validate_name(RCHILD(tree, -5), "def")
2572 && validate_ntype(RCHILD(tree, -4), NAME)
2573 && validate_colon(RCHILD(tree, -2))
2574 && validate_parameters(RCHILD(tree, -3))
2575 && validate_suite(RCHILD(tree, -1)));
2576
2577 if (ok && (nch == 6))
2578 ok = validate_decorators(CHILD(tree, 0));
2579
2580 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002581}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002582
2583
Guido van Rossum47478871996-08-21 14:32:37 +00002584static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002585validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002586{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002587 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002588 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002589 && ((nch == 3) || (nch == 4))
2590 && validate_name(CHILD(tree, 0), "lambda")
2591 && validate_colon(CHILD(tree, nch - 2))
2592 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002593
Guido van Rossum3d602e31996-07-21 02:33:56 +00002594 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002595 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002596 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002597 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002598
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002599 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002600}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002601
2602
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002603static int
2604validate_old_lambdef(node *tree)
2605{
2606 int nch = NCH(tree);
2607 int res = (validate_ntype(tree, old_lambdef)
2608 && ((nch == 3) || (nch == 4))
2609 && validate_name(CHILD(tree, 0), "lambda")
2610 && validate_colon(CHILD(tree, nch - 2))
2611 && validate_test(CHILD(tree, nch - 1)));
2612
2613 if (res && (nch == 4))
2614 res = validate_varargslist(CHILD(tree, 1));
2615 else if (!res && !PyErr_Occurred())
2616 (void) validate_numnodes(tree, 3, "old_lambdef");
2617
2618 return (res);
2619}
2620
2621
Guido van Rossum3d602e31996-07-21 02:33:56 +00002622/* arglist:
2623 *
Fred Drakecff283c2000-08-21 22:24:43 +00002624 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002625 */
Guido van Rossum47478871996-08-21 14:32:37 +00002626static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002627validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002628{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002629 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002630 int i = 0;
2631 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002632
2633 if (nch <= 0)
2634 /* raise the right error from having an invalid number of children */
2635 return validate_numnodes(tree, nch + 1, "arglist");
2636
Raymond Hettinger354433a2004-05-19 08:20:33 +00002637 if (nch > 1) {
2638 for (i=0; i<nch; i++) {
2639 if (TYPE(CHILD(tree, i)) == argument) {
2640 node *ch = CHILD(tree, i);
2641 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == gen_for) {
2642 err_string("need '(', ')' for generator expression");
2643 return 0;
2644 }
2645 }
2646 }
2647 }
2648
Fred Drakecff283c2000-08-21 22:24:43 +00002649 while (ok && nch-i >= 2) {
2650 /* skip leading (argument ',') */
2651 ok = (validate_argument(CHILD(tree, i))
2652 && validate_comma(CHILD(tree, i+1)));
2653 if (ok)
2654 i += 2;
2655 else
2656 PyErr_Clear();
2657 }
2658 ok = 1;
2659 if (nch-i > 0) {
2660 /*
2661 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002662 */
Fred Drakecff283c2000-08-21 22:24:43 +00002663 int sym = TYPE(CHILD(tree, i));
2664
2665 if (sym == argument) {
2666 ok = validate_argument(CHILD(tree, i));
2667 if (ok && i+1 != nch) {
2668 err_string("illegal arglist specification"
2669 " (extra stuff on end)");
2670 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002671 }
Fred Drakecff283c2000-08-21 22:24:43 +00002672 }
2673 else if (sym == STAR) {
2674 ok = validate_star(CHILD(tree, i));
2675 if (ok && (nch-i == 2))
2676 ok = validate_test(CHILD(tree, i+1));
2677 else if (ok && (nch-i == 5))
2678 ok = (validate_test(CHILD(tree, i+1))
2679 && validate_comma(CHILD(tree, i+2))
2680 && validate_doublestar(CHILD(tree, i+3))
2681 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002682 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002683 err_string("illegal use of '*' in arglist");
2684 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002685 }
2686 }
Fred Drakecff283c2000-08-21 22:24:43 +00002687 else if (sym == DOUBLESTAR) {
2688 if (nch-i == 2)
2689 ok = (validate_doublestar(CHILD(tree, i))
2690 && validate_test(CHILD(tree, i+1)));
2691 else {
2692 err_string("illegal use of '**' in arglist");
2693 ok = 0;
2694 }
2695 }
2696 else {
2697 err_string("illegal arglist specification");
2698 ok = 0;
2699 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002700 }
2701 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002702}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002703
2704
2705
2706/* argument:
2707 *
Raymond Hettinger354433a2004-05-19 08:20:33 +00002708 * [test '='] test [gen_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002709 */
Guido van Rossum47478871996-08-21 14:32:37 +00002710static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002711validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002712{
2713 int nch = NCH(tree);
2714 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002715 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002716 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002717
Raymond Hettinger354433a2004-05-19 08:20:33 +00002718 if (res && (nch == 2))
2719 res = validate_gen_for(CHILD(tree, 1));
2720 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002721 res = (validate_equal(CHILD(tree, 1))
2722 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002723
2724 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002725}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002726
2727
2728
2729/* trailer:
2730 *
Guido van Rossum47478871996-08-21 14:32:37 +00002731 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002732 */
Guido van Rossum47478871996-08-21 14:32:37 +00002733static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002734validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002735{
2736 int nch = NCH(tree);
2737 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002738
2739 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002740 switch (TYPE(CHILD(tree, 0))) {
2741 case LPAR:
2742 res = validate_rparen(CHILD(tree, nch - 1));
2743 if (res && (nch == 3))
2744 res = validate_arglist(CHILD(tree, 1));
2745 break;
2746 case LSQB:
2747 res = (validate_numnodes(tree, 3, "trailer")
2748 && validate_subscriptlist(CHILD(tree, 1))
2749 && validate_ntype(CHILD(tree, 2), RSQB));
2750 break;
2751 case DOT:
2752 res = (validate_numnodes(tree, 2, "trailer")
2753 && validate_ntype(CHILD(tree, 1), NAME));
2754 break;
2755 default:
2756 res = 0;
2757 break;
2758 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002759 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002760 else {
2761 (void) validate_numnodes(tree, 2, "trailer");
2762 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002763 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002764}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002765
2766
Guido van Rossum47478871996-08-21 14:32:37 +00002767/* subscriptlist:
2768 *
2769 * subscript (',' subscript)* [',']
2770 */
2771static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002772validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002773{
2774 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002775 validate_subscript, "subscriptlist"));
2776}
Guido van Rossum47478871996-08-21 14:32:37 +00002777
2778
Guido van Rossum3d602e31996-07-21 02:33:56 +00002779/* subscript:
2780 *
Guido van Rossum47478871996-08-21 14:32:37 +00002781 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002782 */
Guido van Rossum47478871996-08-21 14:32:37 +00002783static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002784validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002785{
Guido van Rossum47478871996-08-21 14:32:37 +00002786 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002787 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002788 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002789
Guido van Rossum47478871996-08-21 14:32:37 +00002790 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002791 if (!PyErr_Occurred())
2792 err_string("invalid number of arguments for subscript node");
2793 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002794 }
Guido van Rossum47478871996-08-21 14:32:37 +00002795 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002796 /* take care of ('.' '.' '.') possibility */
2797 return (validate_numnodes(tree, 3, "subscript")
2798 && validate_dot(CHILD(tree, 0))
2799 && validate_dot(CHILD(tree, 1))
2800 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002801 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002802 if (TYPE(CHILD(tree, 0)) == test)
2803 res = validate_test(CHILD(tree, 0));
2804 else
2805 res = validate_colon(CHILD(tree, 0));
2806 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002807 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002808 /* Must be [test] ':' [test] [sliceop],
2809 * but at least one of the optional components will
2810 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002811 */
2812 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002813 res = validate_test(CHILD(tree, 0));
2814 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002815 }
2816 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002817 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002818 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002819 int rem = nch - ++offset;
2820 if (rem) {
2821 if (TYPE(CHILD(tree, offset)) == test) {
2822 res = validate_test(CHILD(tree, offset));
2823 ++offset;
2824 --rem;
2825 }
2826 if (res && rem)
2827 res = validate_sliceop(CHILD(tree, offset));
2828 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002829 }
2830 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002831}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002832
2833
Guido van Rossum47478871996-08-21 14:32:37 +00002834static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002835validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002836{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002837 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002838 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002839 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002840 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002841 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002842 }
Guido van Rossum47478871996-08-21 14:32:37 +00002843 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002844 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002845 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002846 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002847
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002848 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002849}
Guido van Rossum47478871996-08-21 14:32:37 +00002850
2851
2852static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002853validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002854{
2855 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002856 validate_expr, "exprlist"));
2857}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002858
2859
Guido van Rossum47478871996-08-21 14:32:37 +00002860static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002861validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002862{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002863 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002864 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002865 && (nch >= 3)
2866 && validate_test(CHILD(tree, 0))
2867 && validate_colon(CHILD(tree, 1))
2868 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002869
Guido van Rossum3d602e31996-07-21 02:33:56 +00002870 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002871 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002872 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002873 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002874
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002875 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002876 int pos = 3;
2877 /* ( ',' test ':' test )* */
2878 while (res && (pos < nch)) {
2879 res = (validate_comma(CHILD(tree, pos))
2880 && validate_test(CHILD(tree, pos + 1))
2881 && validate_colon(CHILD(tree, pos + 2))
2882 && validate_test(CHILD(tree, pos + 3)));
2883 pos += 4;
2884 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002885 }
2886 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002887}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002888
2889
Guido van Rossum47478871996-08-21 14:32:37 +00002890static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002891validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002892{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002893 int pos;
2894 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002895 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002896 && (nch >= 2)
2897 && validate_testlist(CHILD(tree, 0))
2898 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002899
Guido van Rossum3d602e31996-07-21 02:33:56 +00002900 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002901 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002902
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002903 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002904}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002905
2906
Guido van Rossum47478871996-08-21 14:32:37 +00002907static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002908validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002909{
Fred Drakeff9ea482000-04-19 13:54:15 +00002910 int nch = 0; /* num. children on current node */
2911 int res = 1; /* result value */
2912 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002913
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002914 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002915 nch = NCH(tree);
2916 next = 0;
2917 switch (TYPE(tree)) {
2918 /*
2919 * Definition nodes.
2920 */
2921 case funcdef:
2922 res = validate_funcdef(tree);
2923 break;
2924 case classdef:
2925 res = validate_class(tree);
2926 break;
2927 /*
2928 * "Trivial" parse tree nodes.
2929 * (Why did I call these trivial?)
2930 */
2931 case stmt:
2932 res = validate_stmt(tree);
2933 break;
2934 case small_stmt:
2935 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002936 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002937 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2938 */
2939 res = validate_small_stmt(tree);
2940 break;
2941 case flow_stmt:
2942 res = (validate_numnodes(tree, 1, "flow_stmt")
2943 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2944 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002945 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002946 || (TYPE(CHILD(tree, 0)) == return_stmt)
2947 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2948 if (res)
2949 next = CHILD(tree, 0);
2950 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002951 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002952 break;
Fred Drake02126f22001-07-17 02:59:15 +00002953 case yield_stmt:
2954 res = validate_yield_stmt(tree);
2955 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002956 /*
2957 * Compound statements.
2958 */
2959 case simple_stmt:
2960 res = validate_simple_stmt(tree);
2961 break;
2962 case compound_stmt:
2963 res = validate_compound_stmt(tree);
2964 break;
2965 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002966 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002967 */
2968 case expr_stmt:
2969 res = validate_expr_stmt(tree);
2970 break;
2971 case print_stmt:
2972 res = validate_print_stmt(tree);
2973 break;
2974 case del_stmt:
2975 res = validate_del_stmt(tree);
2976 break;
2977 case pass_stmt:
2978 res = (validate_numnodes(tree, 1, "pass")
2979 && validate_name(CHILD(tree, 0), "pass"));
2980 break;
2981 case break_stmt:
2982 res = (validate_numnodes(tree, 1, "break")
2983 && validate_name(CHILD(tree, 0), "break"));
2984 break;
2985 case continue_stmt:
2986 res = (validate_numnodes(tree, 1, "continue")
2987 && validate_name(CHILD(tree, 0), "continue"));
2988 break;
2989 case return_stmt:
2990 res = validate_return_stmt(tree);
2991 break;
2992 case raise_stmt:
2993 res = validate_raise_stmt(tree);
2994 break;
2995 case import_stmt:
2996 res = validate_import_stmt(tree);
2997 break;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00002998 case import_name:
2999 res = validate_import_name(tree);
3000 break;
3001 case import_from:
3002 res = validate_import_from(tree);
3003 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003004 case global_stmt:
3005 res = validate_global_stmt(tree);
3006 break;
3007 case exec_stmt:
3008 res = validate_exec_stmt(tree);
3009 break;
3010 case assert_stmt:
3011 res = validate_assert_stmt(tree);
3012 break;
3013 case if_stmt:
3014 res = validate_if(tree);
3015 break;
3016 case while_stmt:
3017 res = validate_while(tree);
3018 break;
3019 case for_stmt:
3020 res = validate_for(tree);
3021 break;
3022 case try_stmt:
3023 res = validate_try(tree);
3024 break;
3025 case suite:
3026 res = validate_suite(tree);
3027 break;
3028 /*
3029 * Expression nodes.
3030 */
3031 case testlist:
3032 res = validate_testlist(tree);
3033 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003034 case yield_expr:
3035 res = validate_yield_expr(tree);
3036 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00003037 case testlist1:
3038 res = validate_testlist1(tree);
3039 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003040 case test:
3041 res = validate_test(tree);
3042 break;
3043 case and_test:
3044 res = validate_and_test(tree);
3045 break;
3046 case not_test:
3047 res = validate_not_test(tree);
3048 break;
3049 case comparison:
3050 res = validate_comparison(tree);
3051 break;
3052 case exprlist:
3053 res = validate_exprlist(tree);
3054 break;
3055 case comp_op:
3056 res = validate_comp_op(tree);
3057 break;
3058 case expr:
3059 res = validate_expr(tree);
3060 break;
3061 case xor_expr:
3062 res = validate_xor_expr(tree);
3063 break;
3064 case and_expr:
3065 res = validate_and_expr(tree);
3066 break;
3067 case shift_expr:
3068 res = validate_shift_expr(tree);
3069 break;
3070 case arith_expr:
3071 res = validate_arith_expr(tree);
3072 break;
3073 case term:
3074 res = validate_term(tree);
3075 break;
3076 case factor:
3077 res = validate_factor(tree);
3078 break;
3079 case power:
3080 res = validate_power(tree);
3081 break;
3082 case atom:
3083 res = validate_atom(tree);
3084 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003085
Fred Drakeff9ea482000-04-19 13:54:15 +00003086 default:
3087 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00003088 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003089 res = 0;
3090 break;
3091 }
3092 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003093 }
3094 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003095}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003096
3097
Guido van Rossum47478871996-08-21 14:32:37 +00003098static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003099validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003100{
3101 int res = validate_eval_input(tree);
3102
3103 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003104 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003105
3106 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003107}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003108
3109
Guido van Rossum3d602e31996-07-21 02:33:56 +00003110/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003111 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003112 */
Guido van Rossum47478871996-08-21 14:32:37 +00003113static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003114validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003115{
Fred Drakec2683dd2001-07-17 19:32:05 +00003116 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003117 int nch = NCH(tree) - 1;
3118 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003119 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003120
Fred Drakec2683dd2001-07-17 19:32:05 +00003121 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003122 if (TYPE(CHILD(tree, j)) == stmt)
3123 res = validate_stmt(CHILD(tree, j));
3124 else
3125 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003126 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003127 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003128 * user. Hopefully, this won't be needed. If a user reports getting
3129 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003130 */
3131 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003132 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003133
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003134 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003135}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003136
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003137static int
3138validate_encoding_decl(node *tree)
3139{
3140 int nch = NCH(tree);
3141 int res = ((nch == 1)
3142 && validate_file_input(CHILD(tree, 0)));
3143
3144 if (!res && !PyErr_Occurred())
3145 err_string("Error Parsing encoding_decl");
3146
3147 return res;
3148}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003149
Fred Drake43f8f9b1998-04-13 16:25:46 +00003150static PyObject*
3151pickle_constructor = NULL;
3152
3153
3154static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003155parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003156{
Fred Drake268397f1998-04-29 14:16:32 +00003157 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003158 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003159 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003160 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003161
Fred Drakec2683dd2001-07-17 19:32:05 +00003162 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003163 PyObject *newargs;
3164 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003165
Fred Drake2a6875e1999-09-20 22:32:18 +00003166 if ((empty_dict = PyDict_New()) == NULL)
3167 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003168 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003169 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003170 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003171 if (tuple != NULL) {
3172 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3173 Py_DECREF(tuple);
3174 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003175 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003176 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003177 }
3178 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003179 Py_XDECREF(empty_dict);
3180
Fred Drake43f8f9b1998-04-13 16:25:46 +00003181 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003182}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003183
3184
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003185/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003186 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003187 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003188 * We'd really have to write a wrapper around it all anyway to allow
3189 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003190 */
3191static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003192 {"ast2tuple", (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 {"ast2list", (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 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003197 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003198 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003199 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003200 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003201 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003202 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003203 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003204 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003205 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003206 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003207 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003208 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003209 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003210 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003211 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003212 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003213 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003214 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003215 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003216 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003217 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003218 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003219 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003220
Fred Drake43f8f9b1998-04-13 16:25:46 +00003221 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003222 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003223 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003224
Fred Drake268397f1998-04-29 14:16:32 +00003225 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003226 };
3227
3228
Mark Hammond62b1ab12002-07-23 06:31:15 +00003229PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003230
Mark Hammond62b1ab12002-07-23 06:31:15 +00003231PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00003232initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003233{
Fred Drake13130bc2001-08-15 16:44:56 +00003234 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003235
3236 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00003237 module = Py_InitModule("parser", parser_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003238 if (module == NULL)
3239 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003240
Fred Drake7a15ba51999-09-09 14:21:52 +00003241 if (parser_error == 0)
3242 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003243
Tim Peters6a627252003-07-21 14:25:23 +00003244 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00003245 /* caller will check PyErr_Occurred() */
3246 return;
Tim Peters6a627252003-07-21 14:25:23 +00003247 /* CAUTION: The code next used to skip bumping the refcount on
3248 * parser_error. That's a disaster if initparser() gets called more
3249 * than once. By incref'ing, we ensure that each module dict that
3250 * gets created owns its reference to the shared parser_error object,
3251 * and the file static parser_error vrbl owns a reference too.
3252 */
3253 Py_INCREF(parser_error);
3254 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3255 return;
3256
Fred Drakec2683dd2001-07-17 19:32:05 +00003257 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003258 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00003259 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003260 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003261
Fred Drake13130bc2001-08-15 16:44:56 +00003262 PyModule_AddStringConstant(module, "__copyright__",
3263 parser_copyright_string);
3264 PyModule_AddStringConstant(module, "__doc__",
3265 parser_doc_string);
3266 PyModule_AddStringConstant(module, "__version__",
3267 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003268
Fred Drake78bdb9b2001-07-19 20:17:15 +00003269 /* Register to support pickling.
3270 * If this fails, the import of this module will fail because an
3271 * exception will be raised here; should we clear the exception?
3272 */
Fred Drake13130bc2001-08-15 16:44:56 +00003273 copyreg = PyImport_ImportModule("copy_reg");
3274 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003275 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003276
Fred Drake13130bc2001-08-15 16:44:56 +00003277 func = PyObject_GetAttrString(copyreg, "pickle");
3278 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3279 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003280 Py_XINCREF(pickle_constructor);
3281 if ((func != NULL) && (pickle_constructor != NULL)
3282 && (pickler != NULL)) {
3283 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003284
Georg Brandl684fd0c2006-05-25 19:15:31 +00003285 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3286 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003287 Py_XDECREF(res);
3288 }
3289 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003290 Py_XDECREF(pickle_constructor);
3291 Py_XDECREF(pickler);
3292 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003293 }
Fred Drakeff9ea482000-04-19 13:54:15 +00003294}