blob: 5764c24b8a454932d43d43de763370742046868a [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 = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000169 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000170 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000171 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000172 0, /* tp_itemsize */
173 (destructor)parser_free, /* tp_dealloc */
174 0, /* tp_print */
175 parser_getattr, /* tp_getattr */
176 0, /* tp_setattr */
177 (cmpfunc)parser_compare, /* tp_compare */
178 0, /* tp_repr */
179 0, /* tp_as_number */
180 0, /* tp_as_sequence */
181 0, /* tp_as_mapping */
182 0, /* tp_hash */
183 0, /* tp_call */
184 0, /* tp_str */
185 0, /* tp_getattro */
186 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000187
188 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000189 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000190
Fred Drakeff9ea482000-04-19 13:54:15 +0000191 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000192
193 /* __doc__ */
194 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000195}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000196
197
198static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000199parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000200{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000201 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000202
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000203 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000204 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000205
206 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000207 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000208
209 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000210 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000211
212 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000213 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000214
215 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000216 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000217
218 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000219 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000220
Fred Drakeff9ea482000-04-19 13:54:15 +0000221 if (v != 0)
222 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000223 }
224 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000225}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000226
227
Fred Drakec2683dd2001-07-17 19:32:05 +0000228/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000229 *
230 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
231 * This really just wraps a call to parser_compare_nodes() with some easy
232 * checks and protection code.
233 *
234 */
235static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000236parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000237{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000238 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000239 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000240
241 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000242 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000243
Fred Drakec2683dd2001-07-17 19:32:05 +0000244 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000245}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000246
247
Fred Drakec2683dd2001-07-17 19:32:05 +0000248/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000249 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000250 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000251 * 'wrapper' object that holds a node* and allows it to be passed around in
252 * Python code.
253 *
254 */
255static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000256parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000257{
Fred Drakec2683dd2001-07-17 19:32:05 +0000258 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000259
260 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000261 o->st_node = st;
262 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000263 }
Fred Drake268397f1998-04-29 14:16:32 +0000264 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000265 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000266 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000267 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000268}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000269
270
Fred Drakec2683dd2001-07-17 19:32:05 +0000271/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000272 *
273 * This is called by a del statement that reduces the reference count to 0.
274 *
275 */
276static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000277parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000278{
Fred Drakec2683dd2001-07-17 19:32:05 +0000279 PyNode_Free(st->st_node);
280 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000281}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000282
283
Fred Drakec2683dd2001-07-17 19:32:05 +0000284/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000285 *
286 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000287 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000288 *
289 */
290static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000291parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000292{
Guido van Rossum47478871996-08-21 14:32:37 +0000293 PyObject *line_option = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000294 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000295 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000296 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000297
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000298 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000299
Fred Drake268397f1998-04-29 14:16:32 +0000300 if (self == NULL) {
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000301 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
302 &PyST_Type, &self, &line_option,
303 &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000304 }
Fred Drake503d8d61998-04-13 18:45:18 +0000305 else
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000306 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
307 &line_option, &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000308 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000309 int lineno = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000310 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000311 if (line_option != NULL) {
312 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
313 }
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000314 if (col_option != NULL) {
315 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
316 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000317 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000318 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000319 * since it's known to work already.
320 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000321 res = node2tuple(((PyST_Object*)self)->st_node,
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000322 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000323 }
324 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000325}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000326
327
Fred Drakec2683dd2001-07-17 19:32:05 +0000328/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000329 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000330 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000331 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000332 *
333 */
334static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000335parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000336{
Guido van Rossum47478871996-08-21 14:32:37 +0000337 PyObject *line_option = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000338 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000339 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000340 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000341
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000342 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000343
Fred Drake503d8d61998-04-13 18:45:18 +0000344 if (self == NULL)
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000345 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
346 &PyST_Type, &self, &line_option,
347 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000348 else
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000349 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
350 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000351 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000352 int lineno = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000353 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000354 if (line_option != 0) {
355 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
356 }
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000357 if (col_option != NULL) {
358 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
359 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000360 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000361 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000362 * since it's known to work already.
363 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000364 res = node2tuple(self->st_node,
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000365 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000366 }
367 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000368}
Guido van Rossum47478871996-08-21 14:32:37 +0000369
370
Fred Drakec2683dd2001-07-17 19:32:05 +0000371/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000372 *
373 * This function creates code objects from the parse tree represented by
374 * the passed-in data object. An optional file name is passed in as well.
375 *
376 */
377static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000378parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000379{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000380 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000381 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000382 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000383
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000384 static char *keywords[] = {"ast", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000385
Fred Drake503d8d61998-04-13 18:45:18 +0000386 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000387 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
388 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000389 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000390 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000391 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000392
393 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000394 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000395
396 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000397}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000398
399
400/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
401 * PyObject* parser_issuite(PyObject* self, PyObject* args)
402 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000403 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000404 * a statement suite, respectively. The return is a Python truth value.
405 *
406 */
407static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000408parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000409{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000410 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000411 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000412
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000413 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000414
Fred Drake503d8d61998-04-13 18:45:18 +0000415 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000416 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000417 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000418 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000419 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000420
421 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000422 /* Check to see if the ST represents an expression or not. */
423 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000424 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000425 }
426 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000427}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000428
429
430static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000431parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000432{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000433 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000434 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000435
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000436 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000437
Fred Drake503d8d61998-04-13 18:45:18 +0000438 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000439 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000440 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000441 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000442 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000443
444 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000445 /* Check to see if the ST represents an expression or not. */
446 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000447 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000448 }
449 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000450}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000451
452
Fred Drake7a15ba51999-09-09 14:21:52 +0000453#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
454
Fred Drake503d8d61998-04-13 18:45:18 +0000455static PyMethodDef
456parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000457 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000458 PyDoc_STR("Compile this ST object into a code object.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000459 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000460 PyDoc_STR("Determines if this ST object was created from an expression.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000461 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000462 PyDoc_STR("Determines if this ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000463 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000464 PyDoc_STR("Creates a list-tree representation of this ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000465 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000466 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Fred Drake503d8d61998-04-13 18:45:18 +0000467
Fred Drake268397f1998-04-29 14:16:32 +0000468 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000469};
470
Fred Drake503d8d61998-04-13 18:45:18 +0000471
472static PyObject*
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000473parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000474{
Fred Drake503d8d61998-04-13 18:45:18 +0000475 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000476}
Fred Drake503d8d61998-04-13 18:45:18 +0000477
478
Guido van Rossum3d602e31996-07-21 02:33:56 +0000479/* err_string(char* message)
480 *
481 * Sets the error string for an exception of type ParserError.
482 *
483 */
484static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000485err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000486{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000487 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000488}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000489
490
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000491/* PyObject* parser_do_parse(PyObject* args, int type)
492 *
493 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000494 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000495 *
496 */
497static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000498parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000499{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000500 char* string = 0;
501 PyObject* res = 0;
502
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000503 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000504
505 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000506 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000507 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000508 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000509
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000510 if (n)
511 res = parser_newstobject(n, type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000512 }
513 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000514}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000515
516
517/* PyObject* parser_expr(PyObject* self, PyObject* args)
518 * PyObject* parser_suite(PyObject* self, PyObject* args)
519 *
520 * External interfaces to the parser itself. Which is called determines if
521 * the parser attempts to recognize an expression ('eval' form) or statement
522 * suite ('exec' form). The real work is done by parser_do_parse() above.
523 *
524 */
525static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000526parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000527{
Fred Drake268397f1998-04-29 14:16:32 +0000528 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000529 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000530}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000531
532
533static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000534parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000535{
Fred Drake268397f1998-04-29 14:16:32 +0000536 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000537 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000538}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000539
540
541
Fred Drakec2683dd2001-07-17 19:32:05 +0000542/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000543 * object requires that the input tuple be valid without having to rely on
544 * catching an exception from the compiler. This is done to allow the
545 * compiler itself to remain fast, since most of its input will come from
546 * the parser directly, and therefore be known to be syntactically correct.
547 * This validation is done to ensure that we don't core dump the compile
548 * phase, returning an exception instead.
549 *
550 * Two aspects can be broken out in this code: creating a node tree from
551 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000552 * advantageous to expand the number of ST types to include funcdefs and
553 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000554 * here. They are not necessary, and not quite as useful in a raw form.
555 * For now, let's get expressions and suites working reliably.
556 */
557
558
Jeremy Hylton938ace62002-07-17 16:30:39 +0000559static node* build_node_tree(PyObject *tuple);
560static int validate_expr_tree(node *tree);
561static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000562static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000563
Fred Drakec2683dd2001-07-17 19:32:05 +0000564/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000565 *
566 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000567 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000568 * tuple can be validated. It does this by checking the first code of the
569 * tuple, and, if acceptable, builds the internal representation. If this
570 * step succeeds, the internal representation is validated as fully as
571 * possible with the various validate_*() routines defined below.
572 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000573 * This function must be changed if support is to be added for PyST_FRAGMENT
574 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000575 *
576 */
577static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000578parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000579{
Fred Drake268397f1998-04-29 14:16:32 +0000580 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000581 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000582 PyObject *tuple;
583 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000584
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000585 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000586
Fred Drakec2683dd2001-07-17 19:32:05 +0000587 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000588 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000589 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000590 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000591 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000592 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000593 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000594 }
595 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000596 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000597 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000598 tree = build_node_tree(tuple);
599 if (tree != 0) {
600 int start_sym = TYPE(tree);
601 if (start_sym == eval_input) {
602 /* Might be an eval form. */
603 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000604 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000605 else
606 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000607 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000608 else if (start_sym == file_input) {
609 /* This looks like an exec form so far. */
610 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000611 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000612 else
613 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000614 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000615 else if (start_sym == encoding_decl) {
616 /* This looks like an encoding_decl so far. */
617 if (validate_encoding_decl(tree))
618 st = parser_newstobject(tree, PyST_SUITE);
619 else
620 PyNode_Free(tree);
621 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000622 else {
623 /* This is a fragment, at best. */
624 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000625 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000626 }
Guido van Rossum47478871996-08-21 14:32:37 +0000627 }
Guido van Rossum47478871996-08-21 14:32:37 +0000628 /* Make sure we throw an exception on all errors. We should never
629 * get this, but we'd do well to be sure something is done.
630 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000631 if (st == NULL && !PyErr_Occurred())
632 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000633
Fred Drakec2683dd2001-07-17 19:32:05 +0000634 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000635}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000636
637
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000638/* node* build_node_children()
639 *
640 * Iterate across the children of the current non-terminal node and build
641 * their structures. If successful, return the root of this portion of
642 * the tree, otherwise, 0. Any required exception will be specified already,
643 * and no memory will have been deallocated.
644 *
645 */
646static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000647build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000648{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000649 Py_ssize_t len = PyObject_Size(tuple);
650 Py_ssize_t i;
651 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000652
653 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000654 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000655 PyObject* elem = PySequence_GetItem(tuple, i);
656 int ok = elem != NULL;
657 long type = 0;
658 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000659
Fred Drakeff9ea482000-04-19 13:54:15 +0000660 if (ok)
661 ok = PySequence_Check(elem);
662 if (ok) {
663 PyObject *temp = PySequence_GetItem(elem, 0);
664 if (temp == NULL)
665 ok = 0;
666 else {
667 ok = PyInt_Check(temp);
668 if (ok)
669 type = PyInt_AS_LONG(temp);
670 Py_DECREF(temp);
671 }
672 }
673 if (!ok) {
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000674 PyObject *err = Py_BuildValue("os", elem,
675 "Illegal node construct.");
676 PyErr_SetObject(parser_error, err);
677 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000678 Py_XDECREF(elem);
679 return (0);
680 }
681 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000682 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000683 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000684
Fred Drake0ac9b072000-09-12 21:58:06 +0000685 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000686 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000687 return 0;
688 }
689 temp = PySequence_GetItem(elem, 1);
690 if (temp == NULL)
691 return 0;
692 if (!PyString_Check(temp)) {
693 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000694 "second item in terminal node must be a string,"
695 " found %s",
Christian Heimese93237d2007-12-19 02:37:44 +0000696 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000697 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000698 return 0;
699 }
700 if (len == 3) {
701 PyObject *o = PySequence_GetItem(elem, 2);
702 if (o != NULL) {
703 if (PyInt_Check(o))
704 *line_num = PyInt_AS_LONG(o);
705 else {
706 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000707 "third item in terminal node must be an"
708 " integer, found %s",
Christian Heimese93237d2007-12-19 02:37:44 +0000709 Py_TYPE(temp)->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000710 Py_DECREF(o);
711 Py_DECREF(temp);
712 return 0;
713 }
714 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000715 }
716 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000717 len = PyString_GET_SIZE(temp) + 1;
Tim Petersc9d78aa2006-03-26 23:27:58 +0000718 strn = (char *)PyObject_MALLOC(len);
Fred Drake0ac9b072000-09-12 21:58:06 +0000719 if (strn != NULL)
720 (void) memcpy(strn, PyString_AS_STRING(temp), len);
721 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000722 }
723 else if (!ISNONTERMINAL(type)) {
724 /*
725 * It has to be one or the other; this is an error.
726 * Throw an exception.
727 */
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000728 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
729 PyErr_SetObject(parser_error, err);
730 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000731 Py_XDECREF(elem);
732 return (0);
733 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000734 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000735 if (err == E_NOMEM) {
Tim Petersc9d78aa2006-03-26 23:27:58 +0000736 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000737 return (node *) PyErr_NoMemory();
738 }
739 if (err == E_OVERFLOW) {
Tim Petersc9d78aa2006-03-26 23:27:58 +0000740 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000741 PyErr_SetString(PyExc_ValueError,
742 "unsupported number of child nodes");
743 return NULL;
744 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000745
Fred Drakeff9ea482000-04-19 13:54:15 +0000746 if (ISNONTERMINAL(type)) {
747 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000748
Fred Drakeff9ea482000-04-19 13:54:15 +0000749 if (new_child != build_node_children(elem, new_child, line_num)) {
750 Py_XDECREF(elem);
751 return (0);
752 }
753 }
754 else if (type == NEWLINE) { /* It's true: we increment the */
755 ++(*line_num); /* line number *after* the newline! */
756 }
757 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000758 }
Tim Petersc9d78aa2006-03-26 23:27:58 +0000759 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000760}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000761
762
763static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000764build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000765{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000766 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000767 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000768 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000769
Guido van Rossum47478871996-08-21 14:32:37 +0000770 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000771 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000772 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000773 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000774 /*
775 * The tuple is simple, but it doesn't start with a start symbol.
776 * Throw an exception now and be done with it.
777 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000778 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000779 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000780 PyErr_SetObject(parser_error, tuple);
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000781 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000782 }
783 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000784 /*
785 * Not efficient, but that can be handled later.
786 */
787 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000788 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000789
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000790 if (num == encoding_decl) {
791 encoding = PySequence_GetItem(tuple, 2);
792 /* tuple isn't borrowed anymore here, need to DECREF */
793 tuple = PySequence_GetSlice(tuple, 0, 2);
794 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000795 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000796 if (res != NULL) {
797 if (res != build_node_children(tuple, res, &line_num)) {
798 PyNode_Free(res);
799 res = NULL;
800 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000801 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000802 Py_ssize_t len;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000803 len = PyString_GET_SIZE(encoding) + 1;
Tim Petersc9d78aa2006-03-26 23:27:58 +0000804 res->n_str = (char *)PyObject_MALLOC(len);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000805 if (res->n_str != NULL)
806 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
807 Py_DECREF(encoding);
808 Py_DECREF(tuple);
809 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000810 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000811 }
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000812 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000813 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000814 * NONTERMINAL, we can't use it. Not sure the implementation
815 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000816 */
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000817 PyObject *err = Py_BuildValue("os", tuple,
818 "Illegal component tuple.");
819 PyErr_SetObject(parser_error, err);
820 Py_XDECREF(err);
821 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000822
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000823 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000824}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000825
826
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000827/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000828 * Validation routines used within the validation section:
829 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000830static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000831
Fred Drakeff9ea482000-04-19 13:54:15 +0000832#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
833#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
834#define validate_colon(ch) validate_terminal(ch, COLON, ":")
835#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
836#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
837#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
838#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
839#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
840#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
841#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
842#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
843#define validate_star(ch) validate_terminal(ch, STAR, "*")
844#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
845#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
846#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000847#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000848#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000849
Fred Drake0ac9b072000-09-12 21:58:06 +0000850#define VALIDATER(n) static int validate_##n(node *tree)
851
Fred Drakeff9ea482000-04-19 13:54:15 +0000852VALIDATER(node); VALIDATER(small_stmt);
853VALIDATER(class); VALIDATER(node);
854VALIDATER(parameters); VALIDATER(suite);
855VALIDATER(testlist); VALIDATER(varargslist);
856VALIDATER(fpdef); VALIDATER(fplist);
857VALIDATER(stmt); VALIDATER(simple_stmt);
858VALIDATER(expr_stmt); VALIDATER(power);
859VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000860VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000861VALIDATER(raise_stmt); VALIDATER(import_stmt);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000862VALIDATER(import_name); VALIDATER(import_from);
Fred Drakecff283c2000-08-21 22:24:43 +0000863VALIDATER(global_stmt); VALIDATER(list_if);
864VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000865VALIDATER(exec_stmt); VALIDATER(compound_stmt);
866VALIDATER(while); VALIDATER(for);
867VALIDATER(try); VALIDATER(except_clause);
868VALIDATER(test); VALIDATER(and_test);
869VALIDATER(not_test); VALIDATER(comparison);
870VALIDATER(comp_op); VALIDATER(expr);
871VALIDATER(xor_expr); VALIDATER(and_expr);
872VALIDATER(shift_expr); VALIDATER(arith_expr);
873VALIDATER(term); VALIDATER(factor);
874VALIDATER(atom); VALIDATER(lambdef);
875VALIDATER(trailer); VALIDATER(subscript);
876VALIDATER(subscriptlist); VALIDATER(sliceop);
877VALIDATER(exprlist); VALIDATER(dictmaker);
878VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000879VALIDATER(listmaker); VALIDATER(yield_stmt);
Raymond Hettinger354433a2004-05-19 08:20:33 +0000880VALIDATER(testlist1); VALIDATER(gen_for);
881VALIDATER(gen_iter); VALIDATER(gen_if);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000882VALIDATER(testlist_gexp); VALIDATER(yield_expr);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +0000883VALIDATER(yield_or_testlist); VALIDATER(or_test);
884VALIDATER(old_test); VALIDATER(old_lambdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000885
Fred Drake0ac9b072000-09-12 21:58:06 +0000886#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000887
Fred Drakeff9ea482000-04-19 13:54:15 +0000888#define is_even(n) (((n) & 1) == 0)
889#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000890
891
892static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000893validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000894{
Fred Drake0ac9b072000-09-12 21:58:06 +0000895 if (TYPE(n) != t) {
896 PyErr_Format(parser_error, "Expected node type %d, got %d.",
897 t, TYPE(n));
898 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000899 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000900 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000901}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000902
903
Fred Drakee7ab64e2000-04-25 04:14:46 +0000904/* Verifies that the number of child nodes is exactly 'num', raising
905 * an exception if it isn't. The exception message does not indicate
906 * the exact number of nodes, allowing this to be used to raise the
907 * "right" exception when the wrong number of nodes is present in a
908 * specific variant of a statement's syntax. This is commonly used
909 * in that fashion.
910 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000911static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000912validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000913{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000914 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000915 PyErr_Format(parser_error,
916 "Illegal number of children for %s node.", name);
917 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000918 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000919 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000920}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000921
922
923static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000924validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000925{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000926 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000927 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000928
929 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000930 PyErr_Format(parser_error,
931 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000932 }
933 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000934}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000935
936
Guido van Rossum47478871996-08-21 14:32:37 +0000937/* X (',' X) [',']
938 */
939static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000940validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000941 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000942{
943 int nch = NCH(tree);
944 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000945 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000946
947 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000948 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000949 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000950 if (is_even(nch))
951 res = validate_comma(CHILD(tree, --nch));
952 if (res && nch > 1) {
953 int pos = 1;
954 for ( ; res && pos < nch; pos += 2)
955 res = (validate_comma(CHILD(tree, pos))
956 && vfunc(CHILD(tree, pos + 1)));
957 }
Guido van Rossum47478871996-08-21 14:32:37 +0000958 }
959 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000960}
Guido van Rossum47478871996-08-21 14:32:37 +0000961
962
Fred Drakecff283c2000-08-21 22:24:43 +0000963/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000964 *
965 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000966 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000967 */
Guido van Rossum47478871996-08-21 14:32:37 +0000968static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000969validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000970{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000971 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +0000972 int res = (validate_ntype(tree, classdef) &&
973 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000974
Guido van Rossum3d602e31996-07-21 02:33:56 +0000975 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000976 res = (validate_name(CHILD(tree, 0), "class")
977 && validate_ntype(CHILD(tree, 1), NAME)
978 && validate_colon(CHILD(tree, nch - 2))
979 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000980 }
Brett Cannonf4189912005-04-09 02:30:16 +0000981 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000982 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +0000983 }
984
985 if (res) {
986 if (nch == 7) {
987 res = ((validate_lparen(CHILD(tree, 2)) &&
988 validate_testlist(CHILD(tree, 3)) &&
989 validate_rparen(CHILD(tree, 4))));
990 }
991 else if (nch == 6) {
992 res = (validate_lparen(CHILD(tree,2)) &&
993 validate_rparen(CHILD(tree,3)));
994 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000995 }
996 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000997}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000998
999
Guido van Rossum3d602e31996-07-21 02:33:56 +00001000/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001001 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001002 */
Guido van Rossum47478871996-08-21 14:32:37 +00001003static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001004validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001005{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001006 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001007 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001008 && (nch >= 4)
1009 && validate_name(CHILD(tree, 0), "if")
1010 && validate_test(CHILD(tree, 1))
1011 && validate_colon(CHILD(tree, 2))
1012 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001013
1014 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001015 /* ... 'else' ':' suite */
1016 res = (validate_name(CHILD(tree, nch - 3), "else")
1017 && validate_colon(CHILD(tree, nch - 2))
1018 && validate_suite(CHILD(tree, nch - 1)));
1019 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001020 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001021 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001022 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001023 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001024 /* Will catch the case for nch < 4 */
1025 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001026 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001027 /* ... ('elif' test ':' suite)+ ... */
1028 int j = 4;
1029 while ((j < nch) && res) {
1030 res = (validate_name(CHILD(tree, j), "elif")
1031 && validate_colon(CHILD(tree, j + 2))
1032 && validate_test(CHILD(tree, j + 1))
1033 && validate_suite(CHILD(tree, j + 3)));
1034 j += 4;
1035 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001036 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001037 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001038}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001039
1040
Guido van Rossum3d602e31996-07-21 02:33:56 +00001041/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001042 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001043 *
1044 */
Guido van Rossum47478871996-08-21 14:32:37 +00001045static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001046validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001047{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001048 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001049 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001050
Guido van Rossum3d602e31996-07-21 02:33:56 +00001051 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001052 res = (validate_lparen(CHILD(tree, 0))
1053 && validate_rparen(CHILD(tree, nch - 1)));
1054 if (res && (nch == 3))
1055 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001056 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001057 else {
1058 (void) validate_numnodes(tree, 2, "parameters");
1059 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001060 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001061}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001062
1063
Fred Drakecff283c2000-08-21 22:24:43 +00001064/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001065 *
1066 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001067 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001068 * | NEWLINE INDENT stmt+ DEDENT
1069 */
Guido van Rossum47478871996-08-21 14:32:37 +00001070static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001071validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001072{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001073 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001074 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001075
Guido van Rossum3d602e31996-07-21 02:33:56 +00001076 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001077 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001078 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001079 /* NEWLINE INDENT stmt+ DEDENT */
1080 res = (validate_newline(CHILD(tree, 0))
1081 && validate_indent(CHILD(tree, 1))
1082 && validate_stmt(CHILD(tree, 2))
1083 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001084
Fred Drakeff9ea482000-04-19 13:54:15 +00001085 if (res && (nch > 4)) {
1086 int i = 3;
1087 --nch; /* forget the DEDENT */
1088 for ( ; res && (i < nch); ++i)
1089 res = validate_stmt(CHILD(tree, i));
1090 }
1091 else if (nch < 4)
1092 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001093 }
1094 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001095}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001096
1097
Guido van Rossum47478871996-08-21 14:32:37 +00001098static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001099validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001100{
Guido van Rossum47478871996-08-21 14:32:37 +00001101 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001102 validate_test, "testlist"));
1103}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001104
1105
Guido van Rossum1c917072001-10-15 15:44:05 +00001106static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001107validate_testlist1(node *tree)
1108{
1109 return (validate_repeating_list(tree, testlist1,
1110 validate_test, "testlist1"));
1111}
1112
1113
1114static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001115validate_testlist_safe(node *tree)
1116{
1117 return (validate_repeating_list(tree, testlist_safe,
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001118 validate_old_test, "testlist_safe"));
Guido van Rossum1c917072001-10-15 15:44:05 +00001119}
1120
1121
Fred Drakecff283c2000-08-21 22:24:43 +00001122/* '*' NAME [',' '**' NAME] | '**' NAME
1123 */
1124static int
1125validate_varargslist_trailer(node *tree, int start)
1126{
1127 int nch = NCH(tree);
1128 int res = 0;
1129 int sym;
1130
1131 if (nch <= start) {
1132 err_string("expected variable argument trailer for varargslist");
1133 return 0;
1134 }
1135 sym = TYPE(CHILD(tree, start));
1136 if (sym == STAR) {
1137 /*
1138 * ('*' NAME [',' '**' NAME]
1139 */
1140 if (nch-start == 2)
1141 res = validate_name(CHILD(tree, start+1), NULL);
1142 else if (nch-start == 5)
1143 res = (validate_name(CHILD(tree, start+1), NULL)
1144 && validate_comma(CHILD(tree, start+2))
1145 && validate_doublestar(CHILD(tree, start+3))
1146 && validate_name(CHILD(tree, start+4), NULL));
1147 }
1148 else if (sym == DOUBLESTAR) {
1149 /*
1150 * '**' NAME
1151 */
1152 if (nch-start == 2)
1153 res = validate_name(CHILD(tree, start+1), NULL);
1154 }
1155 if (!res)
1156 err_string("illegal variable argument trailer for varargslist");
1157 return res;
1158}
1159
1160
1161/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001162 *
1163 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001164 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001165 * ('*' NAME [',' '**' NAME]
1166 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001167 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1168 *
1169 */
Guido van Rossum47478871996-08-21 14:32:37 +00001170static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001171validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001172{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001173 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001174 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001175 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001176
Fred Drakeb6429a22000-12-11 22:08:27 +00001177 if (!res)
1178 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001179 if (nch < 1) {
1180 err_string("varargslist missing child nodes");
1181 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001182 }
Fred Drakecff283c2000-08-21 22:24:43 +00001183 sym = TYPE(CHILD(tree, 0));
1184 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001185 /* whole thing matches:
1186 * '*' NAME [',' '**' NAME] | '**' NAME
1187 */
Fred Drakecff283c2000-08-21 22:24:43 +00001188 res = validate_varargslist_trailer(tree, 0);
1189 else if (sym == fpdef) {
1190 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001191
Fred Drakecff283c2000-08-21 22:24:43 +00001192 sym = TYPE(CHILD(tree, nch-1));
1193 if (sym == NAME) {
1194 /*
1195 * (fpdef ['=' test] ',')+
1196 * ('*' NAME [',' '**' NAME]
1197 * | '**' NAME)
1198 */
1199 /* skip over (fpdef ['=' test] ',')+ */
1200 while (res && (i+2 <= nch)) {
1201 res = validate_fpdef(CHILD(tree, i));
1202 ++i;
1203 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1204 res = (validate_equal(CHILD(tree, i))
1205 && validate_test(CHILD(tree, i+1)));
1206 if (res)
1207 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001208 }
Fred Drakecff283c2000-08-21 22:24:43 +00001209 if (res && i < nch) {
1210 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001211 ++i;
1212 if (res && i < nch
1213 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1214 || TYPE(CHILD(tree, i)) == STAR))
1215 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001216 }
1217 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001218 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1219 * i --^^^
1220 */
Fred Drakecff283c2000-08-21 22:24:43 +00001221 if (res)
1222 res = validate_varargslist_trailer(tree, i);
1223 }
1224 else {
1225 /*
1226 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1227 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001228 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001229 if (sym == COMMA) {
1230 res = validate_comma(CHILD(tree, nch-1));
1231 if (!res)
1232 return 0;
1233 --nch;
1234 }
1235 /*
1236 * fpdef ['=' test] (',' fpdef ['=' test])*
1237 */
1238 res = validate_fpdef(CHILD(tree, 0));
1239 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001240 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1241 res = (validate_equal(CHILD(tree, i))
1242 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001243 i += 2;
1244 }
1245 /*
1246 * ... (',' fpdef ['=' test])*
1247 * i ---^^^
1248 */
1249 while (res && (nch - i) >= 2) {
1250 res = (validate_comma(CHILD(tree, i))
1251 && validate_fpdef(CHILD(tree, i+1)));
1252 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001253 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1254 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001255 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001256 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001257 }
1258 }
1259 if (res && nch - i != 0) {
1260 res = 0;
1261 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001262 }
1263 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001264 }
Fred Drakecff283c2000-08-21 22:24:43 +00001265 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001266}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001267
1268
Fred Drakecff283c2000-08-21 22:24:43 +00001269/* list_iter: list_for | list_if
1270 */
1271static int
1272validate_list_iter(node *tree)
1273{
1274 int res = (validate_ntype(tree, list_iter)
1275 && validate_numnodes(tree, 1, "list_iter"));
1276 if (res && TYPE(CHILD(tree, 0)) == list_for)
1277 res = validate_list_for(CHILD(tree, 0));
1278 else
1279 res = validate_list_if(CHILD(tree, 0));
1280
1281 return res;
1282}
1283
Raymond Hettinger354433a2004-05-19 08:20:33 +00001284/* gen_iter: gen_for | gen_if
1285 */
1286static int
1287validate_gen_iter(node *tree)
1288{
1289 int res = (validate_ntype(tree, gen_iter)
1290 && validate_numnodes(tree, 1, "gen_iter"));
1291 if (res && TYPE(CHILD(tree, 0)) == gen_for)
1292 res = validate_gen_for(CHILD(tree, 0));
1293 else
1294 res = validate_gen_if(CHILD(tree, 0));
1295
1296 return res;
1297}
1298
Fred Drakecff283c2000-08-21 22:24:43 +00001299/* list_for: 'for' exprlist 'in' testlist [list_iter]
1300 */
1301static int
1302validate_list_for(node *tree)
1303{
1304 int nch = NCH(tree);
1305 int res;
1306
1307 if (nch == 5)
1308 res = validate_list_iter(CHILD(tree, 4));
1309 else
1310 res = validate_numnodes(tree, 4, "list_for");
1311
1312 if (res)
1313 res = (validate_name(CHILD(tree, 0), "for")
1314 && validate_exprlist(CHILD(tree, 1))
1315 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001316 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001317
1318 return res;
1319}
1320
Raymond Hettinger354433a2004-05-19 08:20:33 +00001321/* gen_for: 'for' exprlist 'in' test [gen_iter]
1322 */
1323static int
1324validate_gen_for(node *tree)
1325{
1326 int nch = NCH(tree);
1327 int res;
1328
1329 if (nch == 5)
1330 res = validate_gen_iter(CHILD(tree, 4));
1331 else
1332 res = validate_numnodes(tree, 4, "gen_for");
1333
1334 if (res)
1335 res = (validate_name(CHILD(tree, 0), "for")
1336 && validate_exprlist(CHILD(tree, 1))
1337 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001338 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001339
1340 return res;
1341}
1342
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001343/* list_if: 'if' old_test [list_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001344 */
1345static int
1346validate_list_if(node *tree)
1347{
1348 int nch = NCH(tree);
1349 int res;
1350
1351 if (nch == 3)
1352 res = validate_list_iter(CHILD(tree, 2));
1353 else
1354 res = validate_numnodes(tree, 2, "list_if");
1355
1356 if (res)
1357 res = (validate_name(CHILD(tree, 0), "if")
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001358 && validate_old_test(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001359
1360 return res;
1361}
1362
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001363/* gen_if: 'if' old_test [gen_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001364 */
1365static int
1366validate_gen_if(node *tree)
1367{
1368 int nch = NCH(tree);
1369 int res;
1370
1371 if (nch == 3)
1372 res = validate_gen_iter(CHILD(tree, 2));
1373 else
1374 res = validate_numnodes(tree, 2, "gen_if");
1375
1376 if (res)
1377 res = (validate_name(CHILD(tree, 0), "if")
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001378 && validate_old_test(CHILD(tree, 1)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001379
1380 return res;
1381}
Fred Drakecff283c2000-08-21 22:24:43 +00001382
1383/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001384 *
1385 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001386 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001387 * | '(' fplist ')'
1388 */
Guido van Rossum47478871996-08-21 14:32:37 +00001389static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001390validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001391{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001392 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001393 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001394
Guido van Rossum3d602e31996-07-21 02:33:56 +00001395 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001396 if (nch == 1)
1397 res = validate_ntype(CHILD(tree, 0), NAME);
1398 else if (nch == 3)
1399 res = (validate_lparen(CHILD(tree, 0))
1400 && validate_fplist(CHILD(tree, 1))
1401 && validate_rparen(CHILD(tree, 2)));
1402 else
1403 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001404 }
1405 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001406}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001407
1408
Guido van Rossum47478871996-08-21 14:32:37 +00001409static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001410validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001411{
Guido van Rossum47478871996-08-21 14:32:37 +00001412 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001413 validate_fpdef, "fplist"));
1414}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001415
1416
Guido van Rossum3d602e31996-07-21 02:33:56 +00001417/* simple_stmt | compound_stmt
1418 *
1419 */
Guido van Rossum47478871996-08-21 14:32:37 +00001420static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001421validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001422{
1423 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001424 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001425
Guido van Rossum3d602e31996-07-21 02:33:56 +00001426 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001427 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001428
Fred Drakeff9ea482000-04-19 13:54:15 +00001429 if (TYPE(tree) == simple_stmt)
1430 res = validate_simple_stmt(tree);
1431 else
1432 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001433 }
1434 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001435}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001436
1437
Guido van Rossum3d602e31996-07-21 02:33:56 +00001438/* small_stmt (';' small_stmt)* [';'] NEWLINE
1439 *
1440 */
Guido van Rossum47478871996-08-21 14:32:37 +00001441static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001442validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001443{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001444 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001445 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001446 && (nch >= 2)
1447 && validate_small_stmt(CHILD(tree, 0))
1448 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001449
Guido van Rossum3d602e31996-07-21 02:33:56 +00001450 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001451 res = validate_numnodes(tree, 2, "simple_stmt");
1452 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001453 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001454 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001455 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001456 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001457
Fred Drakeff9ea482000-04-19 13:54:15 +00001458 for (i = 1; res && (i < nch); i += 2)
1459 res = (validate_semi(CHILD(tree, i))
1460 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001461 }
1462 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001463}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001464
1465
Guido van Rossum47478871996-08-21 14:32:37 +00001466static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001467validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001468{
1469 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001470 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001471
Fred Drake0ac9b072000-09-12 21:58:06 +00001472 if (res) {
1473 int ntype = TYPE(CHILD(tree, 0));
1474
1475 if ( (ntype == expr_stmt)
1476 || (ntype == print_stmt)
1477 || (ntype == del_stmt)
1478 || (ntype == pass_stmt)
1479 || (ntype == flow_stmt)
1480 || (ntype == import_stmt)
1481 || (ntype == global_stmt)
1482 || (ntype == assert_stmt)
1483 || (ntype == exec_stmt))
1484 res = validate_node(CHILD(tree, 0));
1485 else {
1486 res = 0;
1487 err_string("illegal small_stmt child type");
1488 }
1489 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001490 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001491 res = 0;
1492 PyErr_Format(parser_error,
1493 "Unrecognized child node of small_stmt: %d.",
1494 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001495 }
1496 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001497}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001498
1499
1500/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001501 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001502 */
Guido van Rossum47478871996-08-21 14:32:37 +00001503static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001504validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001505{
1506 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001507 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001508 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001509
1510 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001511 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001512
1513 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001514 ntype = TYPE(tree);
1515 if ( (ntype == if_stmt)
1516 || (ntype == while_stmt)
1517 || (ntype == for_stmt)
1518 || (ntype == try_stmt)
1519 || (ntype == funcdef)
1520 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001521 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001522 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001523 res = 0;
1524 PyErr_Format(parser_error,
1525 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001526 }
1527 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001528}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001529
1530
Guido van Rossum47478871996-08-21 14:32:37 +00001531static int
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001532validate_yield_or_testlist(node *tree)
1533{
1534 if (TYPE(tree) == yield_expr)
1535 return validate_yield_expr(tree);
1536 else
1537 return validate_testlist(tree);
1538}
1539
1540static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001541validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001542{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001543 int j;
1544 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001545 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001546 && is_odd(nch)
1547 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001548
Fred Drake28f739a2000-08-25 22:42:40 +00001549 if (res && nch == 3
1550 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001551 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1552 && validate_yield_or_testlist(CHILD(tree, 2));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001553
Fred Drake28f739a2000-08-25 22:42:40 +00001554 if (res) {
1555 char *s = STR(CHILD(CHILD(tree, 1), 0));
1556
1557 res = (strcmp(s, "+=") == 0
1558 || strcmp(s, "-=") == 0
1559 || strcmp(s, "*=") == 0
1560 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001561 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001562 || strcmp(s, "%=") == 0
1563 || strcmp(s, "&=") == 0
1564 || strcmp(s, "|=") == 0
1565 || strcmp(s, "^=") == 0
1566 || strcmp(s, "<<=") == 0
1567 || strcmp(s, ">>=") == 0
1568 || strcmp(s, "**=") == 0);
1569 if (!res)
1570 err_string("illegal augmmented assignment operator");
1571 }
1572 }
1573 else {
1574 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001575 res = validate_equal(CHILD(tree, j))
1576 && validate_yield_or_testlist(CHILD(tree, j + 1));
Fred Drake28f739a2000-08-25 22:42:40 +00001577 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001578 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001579}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001580
1581
Guido van Rossum3d602e31996-07-21 02:33:56 +00001582/* print_stmt:
1583 *
Fred Drakecff283c2000-08-21 22:24:43 +00001584 * 'print' ( [ test (',' test)* [','] ]
1585 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001586 */
Guido van Rossum47478871996-08-21 14:32:37 +00001587static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001588validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001589{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001590 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001591 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001592 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001593 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001594
Fred Drakecff283c2000-08-21 22:24:43 +00001595 if (res && nch > 1) {
1596 int sym = TYPE(CHILD(tree, 1));
1597 int i = 1;
1598 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001599
Fred Drakecff283c2000-08-21 22:24:43 +00001600 if (sym == test)
1601 res = validate_test(CHILD(tree, i++));
1602 else {
1603 if (nch < 3)
1604 res = validate_numnodes(tree, 3, "print_stmt");
1605 else {
1606 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1607 && validate_test(CHILD(tree, i+1)));
1608 i += 2;
1609 allow_trailing_comma = 0;
1610 }
1611 }
1612 if (res) {
1613 /* ... (',' test)* [','] */
1614 while (res && i+2 <= nch) {
1615 res = (validate_comma(CHILD(tree, i))
1616 && validate_test(CHILD(tree, i+1)));
1617 allow_trailing_comma = 1;
1618 i += 2;
1619 }
1620 if (res && !allow_trailing_comma)
1621 res = validate_numnodes(tree, i, "print_stmt");
1622 else if (res && i < nch)
1623 res = validate_comma(CHILD(tree, i));
1624 }
1625 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001626 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001627}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001628
1629
Guido van Rossum47478871996-08-21 14:32:37 +00001630static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001631validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001632{
1633 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001634 && validate_name(CHILD(tree, 0), "del")
1635 && validate_exprlist(CHILD(tree, 1)));
1636}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001637
1638
Guido van Rossum47478871996-08-21 14:32:37 +00001639static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001640validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001641{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001642 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001643 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001644 && ((nch == 1) || (nch == 2))
1645 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001646
Guido van Rossum3d602e31996-07-21 02:33:56 +00001647 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001648 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001649
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001650 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001651}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001652
1653
Guido van Rossum47478871996-08-21 14:32:37 +00001654static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001655validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001656{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001657 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001658 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001659 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001660
Guido van Rossum3d602e31996-07-21 02:33:56 +00001661 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001662 res = validate_name(CHILD(tree, 0), "raise");
1663 if (res && (nch >= 2))
1664 res = validate_test(CHILD(tree, 1));
1665 if (res && nch > 2) {
1666 res = (validate_comma(CHILD(tree, 2))
1667 && validate_test(CHILD(tree, 3)));
1668 if (res && (nch > 4))
1669 res = (validate_comma(CHILD(tree, 4))
1670 && validate_test(CHILD(tree, 5)));
1671 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001672 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001673 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001674 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001675 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001676 res = (validate_comma(CHILD(tree, 2))
1677 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001678
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001679 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001680}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001681
1682
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001683/* yield_expr: 'yield' [testlist]
1684 */
1685static int
1686validate_yield_expr(node *tree)
1687{
1688 int nch = NCH(tree);
1689 int res = (validate_ntype(tree, yield_expr)
1690 && ((nch == 1) || (nch == 2))
1691 && validate_name(CHILD(tree, 0), "yield"));
1692
1693 if (res && (nch == 2))
1694 res = validate_testlist(CHILD(tree, 1));
1695
1696 return (res);
1697}
1698
1699
1700/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001701 */
1702static int
1703validate_yield_stmt(node *tree)
1704{
1705 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001706 && validate_numnodes(tree, 1, "yield_stmt")
1707 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001708}
1709
1710
Fred Drakecff283c2000-08-21 22:24:43 +00001711static int
1712validate_import_as_name(node *tree)
1713{
1714 int nch = NCH(tree);
1715 int ok = validate_ntype(tree, import_as_name);
1716
1717 if (ok) {
1718 if (nch == 1)
1719 ok = validate_name(CHILD(tree, 0), NULL);
1720 else if (nch == 3)
1721 ok = (validate_name(CHILD(tree, 0), NULL)
1722 && validate_name(CHILD(tree, 1), "as")
1723 && validate_name(CHILD(tree, 2), NULL));
1724 else
1725 ok = validate_numnodes(tree, 3, "import_as_name");
1726 }
1727 return ok;
1728}
1729
1730
Fred Drake71137082001-01-07 05:59:59 +00001731/* dotted_name: NAME ("." NAME)*
1732 */
1733static int
1734validate_dotted_name(node *tree)
1735{
1736 int nch = NCH(tree);
1737 int res = (validate_ntype(tree, dotted_name)
1738 && is_odd(nch)
1739 && validate_name(CHILD(tree, 0), NULL));
1740 int i;
1741
1742 for (i = 1; res && (i < nch); i += 2) {
1743 res = (validate_dot(CHILD(tree, i))
1744 && validate_name(CHILD(tree, i+1), NULL));
1745 }
1746 return res;
1747}
1748
1749
Fred Drakecff283c2000-08-21 22:24:43 +00001750/* dotted_as_name: dotted_name [NAME NAME]
1751 */
1752static int
1753validate_dotted_as_name(node *tree)
1754{
1755 int nch = NCH(tree);
1756 int res = validate_ntype(tree, dotted_as_name);
1757
1758 if (res) {
1759 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001760 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001761 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001762 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001763 && validate_name(CHILD(tree, 1), "as")
1764 && validate_name(CHILD(tree, 2), NULL));
1765 else {
1766 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001767 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001768 }
1769 }
1770 return res;
1771}
1772
1773
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001774/* dotted_as_name (',' dotted_as_name)* */
1775static int
1776validate_dotted_as_names(node *tree)
1777{
1778 int nch = NCH(tree);
1779 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1780 int i;
1781
1782 for (i = 1; res && (i < nch); i += 2)
1783 res = (validate_comma(CHILD(tree, i))
1784 && validate_dotted_as_name(CHILD(tree, i + 1)));
1785 return (res);
1786}
1787
1788
1789/* import_as_name (',' import_as_name)* [','] */
1790static int
1791validate_import_as_names(node *tree)
1792{
1793 int nch = NCH(tree);
1794 int res = validate_import_as_name(CHILD(tree, 0));
1795 int i;
1796
1797 for (i = 1; res && (i + 1 < nch); i += 2)
1798 res = (validate_comma(CHILD(tree, i))
1799 && validate_import_as_name(CHILD(tree, i + 1)));
1800 return (res);
1801}
1802
1803
1804/* 'import' dotted_as_names */
1805static int
1806validate_import_name(node *tree)
1807{
1808 return (validate_ntype(tree, import_name)
1809 && validate_numnodes(tree, 2, "import_name")
1810 && validate_name(CHILD(tree, 0), "import")
1811 && validate_dotted_as_names(CHILD(tree, 1)));
1812}
1813
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001814/* Helper function to count the number of leading dots in
1815 * 'from ...module import name'
1816 */
1817static int
1818count_from_dots(node *tree)
1819{
1820 int i;
1821 for (i = 0; i < NCH(tree); i++)
1822 if (TYPE(CHILD(tree, i)) != DOT)
1823 break;
1824 return i;
1825}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001826
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001827/* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001828 * import_as_names
Guido van Rossum3d602e31996-07-21 02:33:56 +00001829 */
Guido van Rossum47478871996-08-21 14:32:37 +00001830static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001831validate_import_from(node *tree)
1832{
1833 int nch = NCH(tree);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001834 int ndots = count_from_dots(tree);
1835 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1836 int offset = ndots + havename;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001837 int res = validate_ntype(tree, import_from)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001838 && (nch >= 4 + ndots)
1839 && validate_name(CHILD(tree, 0), "from")
1840 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1841 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001842
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001843 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1844 res = ((nch == offset + 5)
1845 && validate_lparen(CHILD(tree, offset + 2))
1846 && validate_import_as_names(CHILD(tree, offset + 3))
1847 && validate_rparen(CHILD(tree, offset + 4)));
1848 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1849 res = validate_import_as_names(CHILD(tree, offset + 2));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001850 return (res);
1851}
1852
1853
1854/* import_stmt: import_name | import_from */
1855static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001856validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001857{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001858 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001859 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001860
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001861 if (res) {
1862 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001863
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001864 if (ntype == import_name || ntype == import_from)
1865 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001866 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001867 res = 0;
1868 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001869 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001870 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001871 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001872 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001873 PyErr_Format(parser_error,
1874 "Unrecognized child node of import_stmt: %d.",
1875 TYPE(CHILD(tree, 0)));
1876 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001877 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001878}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001879
1880
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001881
1882
Guido van Rossum47478871996-08-21 14:32:37 +00001883static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001884validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001885{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001886 int j;
1887 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001888 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001889 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001890
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001891 if (!res && !PyErr_Occurred())
1892 err_string("illegal global statement");
1893
Guido van Rossum3d602e31996-07-21 02:33:56 +00001894 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001895 res = (validate_name(CHILD(tree, 0), "global")
1896 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001897 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001898 res = (validate_comma(CHILD(tree, j))
1899 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001900
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001901 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001902}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001903
1904
Guido van Rossum3d602e31996-07-21 02:33:56 +00001905/* exec_stmt:
1906 *
1907 * 'exec' expr ['in' test [',' test]]
1908 */
Guido van Rossum47478871996-08-21 14:32:37 +00001909static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001910validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001911{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001912 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001913 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001914 && ((nch == 2) || (nch == 4) || (nch == 6))
1915 && validate_name(CHILD(tree, 0), "exec")
1916 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001917
Guido van Rossum3d602e31996-07-21 02:33:56 +00001918 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001919 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001920 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001921 res = (validate_name(CHILD(tree, 2), "in")
1922 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001923 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001924 res = (validate_comma(CHILD(tree, 4))
1925 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001926
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001927 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001928}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001929
1930
Guido van Rossum925e5471997-04-02 05:32:13 +00001931/* assert_stmt:
1932 *
1933 * 'assert' test [',' test]
1934 */
1935static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001936validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001937{
1938 int nch = NCH(tree);
1939 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001940 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001941 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001942 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001943
1944 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001945 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001946 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001947 res = (validate_comma(CHILD(tree, 2))
1948 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001949
1950 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001951}
Guido van Rossum925e5471997-04-02 05:32:13 +00001952
1953
Guido van Rossum47478871996-08-21 14:32:37 +00001954static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001955validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001956{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001957 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001958 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001959 && ((nch == 4) || (nch == 7))
1960 && validate_name(CHILD(tree, 0), "while")
1961 && validate_test(CHILD(tree, 1))
1962 && validate_colon(CHILD(tree, 2))
1963 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001964
Guido van Rossum3d602e31996-07-21 02:33:56 +00001965 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001966 res = (validate_name(CHILD(tree, 4), "else")
1967 && validate_colon(CHILD(tree, 5))
1968 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001969
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001970 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001971}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001972
1973
Guido van Rossum47478871996-08-21 14:32:37 +00001974static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001975validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001976{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001977 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001978 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001979 && ((nch == 6) || (nch == 9))
1980 && validate_name(CHILD(tree, 0), "for")
1981 && validate_exprlist(CHILD(tree, 1))
1982 && validate_name(CHILD(tree, 2), "in")
1983 && validate_testlist(CHILD(tree, 3))
1984 && validate_colon(CHILD(tree, 4))
1985 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001986
Guido van Rossum3d602e31996-07-21 02:33:56 +00001987 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001988 res = (validate_name(CHILD(tree, 6), "else")
1989 && validate_colon(CHILD(tree, 7))
1990 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001991
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001992 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001993}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001994
1995
Guido van Rossum3d602e31996-07-21 02:33:56 +00001996/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001997 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001998 * | 'try' ':' suite 'finally' ':' suite
1999 *
2000 */
Guido van Rossum47478871996-08-21 14:32:37 +00002001static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002002validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002003{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002004 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002005 int pos = 3;
2006 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002007 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002008
2009 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002010 res = (validate_name(CHILD(tree, 0), "try")
2011 && validate_colon(CHILD(tree, 1))
2012 && validate_suite(CHILD(tree, 2))
2013 && validate_colon(CHILD(tree, nch - 2))
2014 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00002015 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00002016 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00002017 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
2018 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00002019
2020 PyErr_Format(parser_error,
2021 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002022 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002023 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002024 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002025 res = (validate_except_clause(CHILD(tree, pos))
2026 && validate_colon(CHILD(tree, pos + 1))
2027 && validate_suite(CHILD(tree, pos + 2)));
2028 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002029 }
2030 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002031 res = validate_ntype(CHILD(tree, pos), NAME);
2032 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
2033 res = (validate_numnodes(tree, 6, "try/finally")
2034 && validate_colon(CHILD(tree, 4))
2035 && validate_suite(CHILD(tree, 5)));
2036 else if (res) {
2037 if (nch == (pos + 3)) {
2038 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
2039 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
2040 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00002041 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00002042 }
2043 else if (nch == (pos + 6)) {
2044 res = (validate_name(CHILD(tree, pos), "except")
2045 && validate_colon(CHILD(tree, pos + 1))
2046 && validate_suite(CHILD(tree, pos + 2))
2047 && validate_name(CHILD(tree, pos + 3), "else"));
2048 }
2049 else
2050 res = validate_numnodes(tree, pos + 3, "try/except");
2051 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002052 }
2053 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002054}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002055
2056
Guido van Rossum47478871996-08-21 14:32:37 +00002057static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002058validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002059{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002060 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002061 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00002062 && ((nch == 1) || (nch == 2) || (nch == 4))
2063 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002064
Guido van Rossum3d602e31996-07-21 02:33:56 +00002065 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002066 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002067 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002068 res = (validate_comma(CHILD(tree, 2))
2069 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002070
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002071 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002072}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002073
2074
Guido van Rossum47478871996-08-21 14:32:37 +00002075static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002076validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002077{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002078 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002079 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002080
Guido van Rossum3d602e31996-07-21 02:33:56 +00002081 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002082 res = ((nch == 1)
2083 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002084 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002085 res = validate_or_test(CHILD(tree, 0));
2086 res = (res && (nch == 1 || (nch == 5 &&
2087 validate_name(CHILD(tree, 1), "if") &&
2088 validate_or_test(CHILD(tree, 2)) &&
2089 validate_name(CHILD(tree, 3), "else") &&
2090 validate_test(CHILD(tree, 4)))));
2091 }
2092 return (res);
2093}
2094
2095static int
2096validate_old_test(node *tree)
2097{
2098 int nch = NCH(tree);
2099 int res = validate_ntype(tree, old_test) && (nch == 1);
2100
2101 if (res && (TYPE(CHILD(tree, 0)) == old_lambdef))
2102 res = (validate_old_lambdef(CHILD(tree, 0)));
2103 else if (res) {
2104 res = (validate_or_test(CHILD(tree, 0)));
2105 }
2106 return (res);
2107}
2108
2109static int
2110validate_or_test(node *tree)
2111{
2112 int nch = NCH(tree);
2113 int res = validate_ntype(tree, or_test) && is_odd(nch);
2114
2115 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002116 int pos;
2117 res = validate_and_test(CHILD(tree, 0));
2118 for (pos = 1; res && (pos < nch); pos += 2)
2119 res = (validate_name(CHILD(tree, pos), "or")
2120 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002121 }
2122 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002123}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002124
2125
Guido van Rossum47478871996-08-21 14:32:37 +00002126static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002127validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002128{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002129 int pos;
2130 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002131 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002132 && is_odd(nch)
2133 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002134
Guido van Rossum3d602e31996-07-21 02:33:56 +00002135 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002136 res = (validate_name(CHILD(tree, pos), "and")
2137 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002138
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002139 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002140}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002141
2142
Guido van Rossum47478871996-08-21 14:32:37 +00002143static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002144validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002145{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002146 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002147 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002148
Guido van Rossum3d602e31996-07-21 02:33:56 +00002149 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002150 if (nch == 2)
2151 res = (validate_name(CHILD(tree, 0), "not")
2152 && validate_not_test(CHILD(tree, 1)));
2153 else if (nch == 1)
2154 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002155 }
2156 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002157}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002158
2159
Guido van Rossum47478871996-08-21 14:32:37 +00002160static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002161validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002162{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002163 int pos;
2164 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002165 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002166 && is_odd(nch)
2167 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002168
Guido van Rossum3d602e31996-07-21 02:33:56 +00002169 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002170 res = (validate_comp_op(CHILD(tree, pos))
2171 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002172
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002173 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002174}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002175
2176
Guido van Rossum47478871996-08-21 14:32:37 +00002177static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002178validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002179{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002180 int res = 0;
2181 int nch = NCH(tree);
2182
Guido van Rossum3d602e31996-07-21 02:33:56 +00002183 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002184 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002185 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002186 /*
2187 * Only child will be a terminal with a well-defined symbolic name
2188 * or a NAME with a string of either 'is' or 'in'
2189 */
2190 tree = CHILD(tree, 0);
2191 switch (TYPE(tree)) {
2192 case LESS:
2193 case GREATER:
2194 case EQEQUAL:
2195 case EQUAL:
2196 case LESSEQUAL:
2197 case GREATEREQUAL:
2198 case NOTEQUAL:
2199 res = 1;
2200 break;
2201 case NAME:
2202 res = ((strcmp(STR(tree), "in") == 0)
2203 || (strcmp(STR(tree), "is") == 0));
2204 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002205 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002206 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002207 }
2208 break;
2209 default:
Fred Drake661ea262000-10-24 19:57:45 +00002210 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002211 break;
2212 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002213 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002214 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002215 res = (validate_ntype(CHILD(tree, 0), NAME)
2216 && validate_ntype(CHILD(tree, 1), NAME)
2217 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2218 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2219 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2220 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2221 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002222 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002223 }
2224 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002225}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002226
2227
Guido van Rossum47478871996-08-21 14:32:37 +00002228static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002229validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002230{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002231 int j;
2232 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002233 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002234 && is_odd(nch)
2235 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002236
Guido van Rossum3d602e31996-07-21 02:33:56 +00002237 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002238 res = (validate_xor_expr(CHILD(tree, j))
2239 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002240
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002241 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002242}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002243
2244
Guido van Rossum47478871996-08-21 14:32:37 +00002245static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002246validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002247{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002248 int j;
2249 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002250 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002251 && is_odd(nch)
2252 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002253
Guido van Rossum3d602e31996-07-21 02:33:56 +00002254 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002255 res = (validate_circumflex(CHILD(tree, j - 1))
2256 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002257
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002258 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002259}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002260
2261
Guido van Rossum47478871996-08-21 14:32:37 +00002262static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002263validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002264{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002265 int pos;
2266 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002267 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002268 && is_odd(nch)
2269 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002270
Guido van Rossum3d602e31996-07-21 02:33:56 +00002271 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002272 res = (validate_ampersand(CHILD(tree, pos))
2273 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002274
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002275 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002276}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002277
2278
2279static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002280validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002281 {
2282 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002283 int nch = NCH(tree);
2284 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002285 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002286
Guido van Rossum3d602e31996-07-21 02:33:56 +00002287 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002288 if (TYPE(CHILD(tree, pos)) != op1)
2289 res = validate_ntype(CHILD(tree, pos), op2);
2290 if (res)
2291 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002292 }
2293 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002294}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002295
2296
Guido van Rossum47478871996-08-21 14:32:37 +00002297static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002298validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002299{
2300 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002301 && validate_chain_two_ops(tree, validate_arith_expr,
2302 LEFTSHIFT, RIGHTSHIFT));
2303}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002304
2305
Guido van Rossum47478871996-08-21 14:32:37 +00002306static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002307validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002308{
2309 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002310 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2311}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002312
2313
Guido van Rossum47478871996-08-21 14:32:37 +00002314static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002315validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002316{
2317 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002318 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002319 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002320 && is_odd(nch)
2321 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002322
Guido van Rossum3d602e31996-07-21 02:33:56 +00002323 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002324 res = (((TYPE(CHILD(tree, pos)) == STAR)
2325 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002326 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002327 || (TYPE(CHILD(tree, pos)) == PERCENT))
2328 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002329
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002330 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002331}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002332
2333
Guido van Rossum3d602e31996-07-21 02:33:56 +00002334/* factor:
2335 *
2336 * factor: ('+'|'-'|'~') factor | power
2337 */
Guido van Rossum47478871996-08-21 14:32:37 +00002338static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002339validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002340{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002341 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002342 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002343 && (((nch == 2)
2344 && ((TYPE(CHILD(tree, 0)) == PLUS)
2345 || (TYPE(CHILD(tree, 0)) == MINUS)
2346 || (TYPE(CHILD(tree, 0)) == TILDE))
2347 && validate_factor(CHILD(tree, 1)))
2348 || ((nch == 1)
2349 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002350 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002351}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002352
2353
Guido van Rossum3d602e31996-07-21 02:33:56 +00002354/* power:
2355 *
2356 * power: atom trailer* ('**' factor)*
2357 */
Guido van Rossum47478871996-08-21 14:32:37 +00002358static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002359validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002360{
2361 int pos = 1;
2362 int nch = NCH(tree);
2363 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002364 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002365
2366 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002367 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002368 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002369 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002370 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002371 return (0);
2372 }
2373 for ( ; res && (pos < (nch - 1)); pos += 2)
2374 res = (validate_doublestar(CHILD(tree, pos))
2375 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002376 }
2377 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002378}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002379
2380
Guido van Rossum47478871996-08-21 14:32:37 +00002381static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002382validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002383{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002384 int pos;
2385 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002386 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002387
Fred Drakecff283c2000-08-21 22:24:43 +00002388 if (res && nch < 1)
2389 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002390 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002391 switch (TYPE(CHILD(tree, 0))) {
2392 case LPAR:
2393 res = ((nch <= 3)
2394 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002395
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002396 if (res && (nch == 3)) {
2397 if (TYPE(CHILD(tree, 1))==yield_expr)
2398 res = validate_yield_expr(CHILD(tree, 1));
2399 else
2400 res = validate_testlist_gexp(CHILD(tree, 1));
2401 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002402 break;
2403 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002404 if (nch == 2)
2405 res = validate_ntype(CHILD(tree, 1), RSQB);
2406 else if (nch == 3)
2407 res = (validate_listmaker(CHILD(tree, 1))
2408 && validate_ntype(CHILD(tree, 2), RSQB));
2409 else {
2410 res = 0;
2411 err_string("illegal list display atom");
2412 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002413 break;
2414 case LBRACE:
2415 res = ((nch <= 3)
2416 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002417
Fred Drakeff9ea482000-04-19 13:54:15 +00002418 if (res && (nch == 3))
2419 res = validate_dictmaker(CHILD(tree, 1));
2420 break;
2421 case BACKQUOTE:
2422 res = ((nch == 3)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002423 && validate_testlist1(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002424 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2425 break;
2426 case NAME:
2427 case NUMBER:
2428 res = (nch == 1);
2429 break;
2430 case STRING:
2431 for (pos = 1; res && (pos < nch); ++pos)
2432 res = validate_ntype(CHILD(tree, pos), STRING);
2433 break;
2434 default:
2435 res = 0;
2436 break;
2437 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002438 }
2439 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002440}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002441
2442
Fred Drake85bf3bb2000-08-23 15:35:26 +00002443/* listmaker:
2444 * test ( list_for | (',' test)* [','] )
2445 */
Fred Drakecff283c2000-08-21 22:24:43 +00002446static int
2447validate_listmaker(node *tree)
2448{
2449 int nch = NCH(tree);
2450 int ok = nch;
2451
2452 if (nch == 0)
2453 err_string("missing child nodes of listmaker");
2454 else
2455 ok = validate_test(CHILD(tree, 0));
2456
2457 /*
Raymond Hettinger354433a2004-05-19 08:20:33 +00002458 * list_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002459 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002460 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2461 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002462 else {
2463 /* (',' test)* [','] */
2464 int i = 1;
2465 while (ok && nch - i >= 2) {
2466 ok = (validate_comma(CHILD(tree, i))
2467 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002468 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002469 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002470 if (ok && i == nch-1)
2471 ok = validate_comma(CHILD(tree, i));
2472 else if (i != nch) {
2473 ok = 0;
2474 err_string("illegal trailing nodes for listmaker");
2475 }
Fred Drakecff283c2000-08-21 22:24:43 +00002476 }
2477 return ok;
2478}
2479
Raymond Hettinger354433a2004-05-19 08:20:33 +00002480/* testlist_gexp:
2481 * test ( gen_for | (',' test)* [','] )
2482 */
2483static int
2484validate_testlist_gexp(node *tree)
2485{
2486 int nch = NCH(tree);
2487 int ok = nch;
2488
2489 if (nch == 0)
2490 err_string("missing child nodes of testlist_gexp");
2491 else {
2492 ok = validate_test(CHILD(tree, 0));
2493 }
2494
2495 /*
2496 * gen_for | (',' test)* [',']
2497 */
2498 if (nch == 2 && TYPE(CHILD(tree, 1)) == gen_for)
2499 ok = validate_gen_for(CHILD(tree, 1));
2500 else {
2501 /* (',' test)* [','] */
2502 int i = 1;
2503 while (ok && nch - i >= 2) {
2504 ok = (validate_comma(CHILD(tree, i))
2505 && validate_test(CHILD(tree, i+1)));
2506 i += 2;
2507 }
2508 if (ok && i == nch-1)
2509 ok = validate_comma(CHILD(tree, i));
2510 else if (i != nch) {
2511 ok = 0;
2512 err_string("illegal trailing nodes for testlist_gexp");
2513 }
2514 }
2515 return ok;
2516}
Fred Drakecff283c2000-08-21 22:24:43 +00002517
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002518/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002519 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002520 */
2521static int
2522validate_decorator(node *tree)
2523{
2524 int ok;
2525 int nch = NCH(tree);
2526 ok = (validate_ntype(tree, decorator) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002527 (nch == 3 || nch == 5 || nch == 6) &&
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002528 validate_at(CHILD(tree, 0)) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002529 validate_dotted_name(CHILD(tree, 1)) &&
2530 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002531
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002532 if (ok && nch != 3) {
2533 ok = (validate_lparen(CHILD(tree, 2)) &&
2534 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002535
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002536 if (ok && nch == 6)
2537 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002538 }
2539
2540 return ok;
2541}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002542
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002543/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002544 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002545 */
2546static int
2547validate_decorators(node *tree)
2548{
2549 int i, nch, ok;
2550 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002551 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002552
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002553 for (i = 0; ok && i < nch; ++i)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002554 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002555
2556 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002557}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002558
Guido van Rossum3d602e31996-07-21 02:33:56 +00002559/* funcdef:
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002560 *
2561 * -6 -5 -4 -3 -2 -1
2562 * [decorators] 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002563 */
Guido van Rossum47478871996-08-21 14:32:37 +00002564static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002565validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002566{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002567 int nch = NCH(tree);
2568 int ok = (validate_ntype(tree, funcdef)
2569 && ((nch == 5) || (nch == 6))
2570 && validate_name(RCHILD(tree, -5), "def")
2571 && validate_ntype(RCHILD(tree, -4), NAME)
2572 && validate_colon(RCHILD(tree, -2))
2573 && validate_parameters(RCHILD(tree, -3))
2574 && validate_suite(RCHILD(tree, -1)));
2575
2576 if (ok && (nch == 6))
2577 ok = validate_decorators(CHILD(tree, 0));
2578
2579 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002580}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002581
2582
Guido van Rossum47478871996-08-21 14:32:37 +00002583static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002584validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002585{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002586 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002587 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002588 && ((nch == 3) || (nch == 4))
2589 && validate_name(CHILD(tree, 0), "lambda")
2590 && validate_colon(CHILD(tree, nch - 2))
2591 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002592
Guido van Rossum3d602e31996-07-21 02:33:56 +00002593 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002594 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002595 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002596 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002597
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002598 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002599}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002600
2601
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002602static int
2603validate_old_lambdef(node *tree)
2604{
2605 int nch = NCH(tree);
2606 int res = (validate_ntype(tree, old_lambdef)
2607 && ((nch == 3) || (nch == 4))
2608 && validate_name(CHILD(tree, 0), "lambda")
2609 && validate_colon(CHILD(tree, nch - 2))
2610 && validate_test(CHILD(tree, nch - 1)));
2611
2612 if (res && (nch == 4))
2613 res = validate_varargslist(CHILD(tree, 1));
2614 else if (!res && !PyErr_Occurred())
2615 (void) validate_numnodes(tree, 3, "old_lambdef");
2616
2617 return (res);
2618}
2619
2620
Guido van Rossum3d602e31996-07-21 02:33:56 +00002621/* arglist:
2622 *
Fred Drakecff283c2000-08-21 22:24:43 +00002623 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002624 */
Guido van Rossum47478871996-08-21 14:32:37 +00002625static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002626validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002627{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002628 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002629 int i = 0;
2630 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002631
2632 if (nch <= 0)
2633 /* raise the right error from having an invalid number of children */
2634 return validate_numnodes(tree, nch + 1, "arglist");
2635
Raymond Hettinger354433a2004-05-19 08:20:33 +00002636 if (nch > 1) {
2637 for (i=0; i<nch; i++) {
2638 if (TYPE(CHILD(tree, i)) == argument) {
2639 node *ch = CHILD(tree, i);
2640 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == gen_for) {
2641 err_string("need '(', ')' for generator expression");
2642 return 0;
2643 }
2644 }
2645 }
2646 }
2647
Fred Drakecff283c2000-08-21 22:24:43 +00002648 while (ok && nch-i >= 2) {
2649 /* skip leading (argument ',') */
2650 ok = (validate_argument(CHILD(tree, i))
2651 && validate_comma(CHILD(tree, i+1)));
2652 if (ok)
2653 i += 2;
2654 else
2655 PyErr_Clear();
2656 }
2657 ok = 1;
2658 if (nch-i > 0) {
2659 /*
2660 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002661 */
Fred Drakecff283c2000-08-21 22:24:43 +00002662 int sym = TYPE(CHILD(tree, i));
2663
2664 if (sym == argument) {
2665 ok = validate_argument(CHILD(tree, i));
2666 if (ok && i+1 != nch) {
2667 err_string("illegal arglist specification"
2668 " (extra stuff on end)");
2669 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002670 }
Fred Drakecff283c2000-08-21 22:24:43 +00002671 }
2672 else if (sym == STAR) {
2673 ok = validate_star(CHILD(tree, i));
2674 if (ok && (nch-i == 2))
2675 ok = validate_test(CHILD(tree, i+1));
2676 else if (ok && (nch-i == 5))
2677 ok = (validate_test(CHILD(tree, i+1))
2678 && validate_comma(CHILD(tree, i+2))
2679 && validate_doublestar(CHILD(tree, i+3))
2680 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002681 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002682 err_string("illegal use of '*' in arglist");
2683 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002684 }
2685 }
Fred Drakecff283c2000-08-21 22:24:43 +00002686 else if (sym == DOUBLESTAR) {
2687 if (nch-i == 2)
2688 ok = (validate_doublestar(CHILD(tree, i))
2689 && validate_test(CHILD(tree, i+1)));
2690 else {
2691 err_string("illegal use of '**' in arglist");
2692 ok = 0;
2693 }
2694 }
2695 else {
2696 err_string("illegal arglist specification");
2697 ok = 0;
2698 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002699 }
2700 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002701}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002702
2703
2704
2705/* argument:
2706 *
Raymond Hettinger354433a2004-05-19 08:20:33 +00002707 * [test '='] test [gen_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002708 */
Guido van Rossum47478871996-08-21 14:32:37 +00002709static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002710validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002711{
2712 int nch = NCH(tree);
2713 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002714 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002715 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002716
Raymond Hettinger354433a2004-05-19 08:20:33 +00002717 if (res && (nch == 2))
2718 res = validate_gen_for(CHILD(tree, 1));
2719 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002720 res = (validate_equal(CHILD(tree, 1))
2721 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002722
2723 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002724}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002725
2726
2727
2728/* trailer:
2729 *
Guido van Rossum47478871996-08-21 14:32:37 +00002730 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002731 */
Guido van Rossum47478871996-08-21 14:32:37 +00002732static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002733validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002734{
2735 int nch = NCH(tree);
2736 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002737
2738 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002739 switch (TYPE(CHILD(tree, 0))) {
2740 case LPAR:
2741 res = validate_rparen(CHILD(tree, nch - 1));
2742 if (res && (nch == 3))
2743 res = validate_arglist(CHILD(tree, 1));
2744 break;
2745 case LSQB:
2746 res = (validate_numnodes(tree, 3, "trailer")
2747 && validate_subscriptlist(CHILD(tree, 1))
2748 && validate_ntype(CHILD(tree, 2), RSQB));
2749 break;
2750 case DOT:
2751 res = (validate_numnodes(tree, 2, "trailer")
2752 && validate_ntype(CHILD(tree, 1), NAME));
2753 break;
2754 default:
2755 res = 0;
2756 break;
2757 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002758 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002759 else {
2760 (void) validate_numnodes(tree, 2, "trailer");
2761 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002762 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002763}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002764
2765
Guido van Rossum47478871996-08-21 14:32:37 +00002766/* subscriptlist:
2767 *
2768 * subscript (',' subscript)* [',']
2769 */
2770static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002771validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002772{
2773 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002774 validate_subscript, "subscriptlist"));
2775}
Guido van Rossum47478871996-08-21 14:32:37 +00002776
2777
Guido van Rossum3d602e31996-07-21 02:33:56 +00002778/* subscript:
2779 *
Guido van Rossum47478871996-08-21 14:32:37 +00002780 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002781 */
Guido van Rossum47478871996-08-21 14:32:37 +00002782static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002783validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002784{
Guido van Rossum47478871996-08-21 14:32:37 +00002785 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002786 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002787 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002788
Guido van Rossum47478871996-08-21 14:32:37 +00002789 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002790 if (!PyErr_Occurred())
2791 err_string("invalid number of arguments for subscript node");
2792 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002793 }
Guido van Rossum47478871996-08-21 14:32:37 +00002794 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002795 /* take care of ('.' '.' '.') possibility */
2796 return (validate_numnodes(tree, 3, "subscript")
2797 && validate_dot(CHILD(tree, 0))
2798 && validate_dot(CHILD(tree, 1))
2799 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002800 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002801 if (TYPE(CHILD(tree, 0)) == test)
2802 res = validate_test(CHILD(tree, 0));
2803 else
2804 res = validate_colon(CHILD(tree, 0));
2805 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002806 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002807 /* Must be [test] ':' [test] [sliceop],
2808 * but at least one of the optional components will
2809 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002810 */
2811 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002812 res = validate_test(CHILD(tree, 0));
2813 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002814 }
2815 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002816 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002817 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002818 int rem = nch - ++offset;
2819 if (rem) {
2820 if (TYPE(CHILD(tree, offset)) == test) {
2821 res = validate_test(CHILD(tree, offset));
2822 ++offset;
2823 --rem;
2824 }
2825 if (res && rem)
2826 res = validate_sliceop(CHILD(tree, offset));
2827 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002828 }
2829 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002830}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002831
2832
Guido van Rossum47478871996-08-21 14:32:37 +00002833static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002834validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002835{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002836 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002837 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002838 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002839 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002840 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002841 }
Guido van Rossum47478871996-08-21 14:32:37 +00002842 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002843 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002844 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002845 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002846
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002847 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002848}
Guido van Rossum47478871996-08-21 14:32:37 +00002849
2850
2851static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002852validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002853{
2854 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002855 validate_expr, "exprlist"));
2856}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002857
2858
Guido van Rossum47478871996-08-21 14:32:37 +00002859static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002860validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002861{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002862 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002863 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002864 && (nch >= 3)
2865 && validate_test(CHILD(tree, 0))
2866 && validate_colon(CHILD(tree, 1))
2867 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002868
Guido van Rossum3d602e31996-07-21 02:33:56 +00002869 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002870 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002871 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002872 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002873
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002874 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002875 int pos = 3;
2876 /* ( ',' test ':' test )* */
2877 while (res && (pos < nch)) {
2878 res = (validate_comma(CHILD(tree, pos))
2879 && validate_test(CHILD(tree, pos + 1))
2880 && validate_colon(CHILD(tree, pos + 2))
2881 && validate_test(CHILD(tree, pos + 3)));
2882 pos += 4;
2883 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002884 }
2885 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002886}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002887
2888
Guido van Rossum47478871996-08-21 14:32:37 +00002889static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002890validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002891{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002892 int pos;
2893 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002894 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002895 && (nch >= 2)
2896 && validate_testlist(CHILD(tree, 0))
2897 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002898
Guido van Rossum3d602e31996-07-21 02:33:56 +00002899 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002900 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002901
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002902 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002903}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002904
2905
Guido van Rossum47478871996-08-21 14:32:37 +00002906static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002907validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002908{
Fred Drakeff9ea482000-04-19 13:54:15 +00002909 int nch = 0; /* num. children on current node */
2910 int res = 1; /* result value */
2911 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002912
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002913 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002914 nch = NCH(tree);
2915 next = 0;
2916 switch (TYPE(tree)) {
2917 /*
2918 * Definition nodes.
2919 */
2920 case funcdef:
2921 res = validate_funcdef(tree);
2922 break;
2923 case classdef:
2924 res = validate_class(tree);
2925 break;
2926 /*
2927 * "Trivial" parse tree nodes.
2928 * (Why did I call these trivial?)
2929 */
2930 case stmt:
2931 res = validate_stmt(tree);
2932 break;
2933 case small_stmt:
2934 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002935 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002936 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2937 */
2938 res = validate_small_stmt(tree);
2939 break;
2940 case flow_stmt:
2941 res = (validate_numnodes(tree, 1, "flow_stmt")
2942 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2943 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002944 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002945 || (TYPE(CHILD(tree, 0)) == return_stmt)
2946 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2947 if (res)
2948 next = CHILD(tree, 0);
2949 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002950 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002951 break;
Fred Drake02126f22001-07-17 02:59:15 +00002952 case yield_stmt:
2953 res = validate_yield_stmt(tree);
2954 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002955 /*
2956 * Compound statements.
2957 */
2958 case simple_stmt:
2959 res = validate_simple_stmt(tree);
2960 break;
2961 case compound_stmt:
2962 res = validate_compound_stmt(tree);
2963 break;
2964 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002965 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002966 */
2967 case expr_stmt:
2968 res = validate_expr_stmt(tree);
2969 break;
2970 case print_stmt:
2971 res = validate_print_stmt(tree);
2972 break;
2973 case del_stmt:
2974 res = validate_del_stmt(tree);
2975 break;
2976 case pass_stmt:
2977 res = (validate_numnodes(tree, 1, "pass")
2978 && validate_name(CHILD(tree, 0), "pass"));
2979 break;
2980 case break_stmt:
2981 res = (validate_numnodes(tree, 1, "break")
2982 && validate_name(CHILD(tree, 0), "break"));
2983 break;
2984 case continue_stmt:
2985 res = (validate_numnodes(tree, 1, "continue")
2986 && validate_name(CHILD(tree, 0), "continue"));
2987 break;
2988 case return_stmt:
2989 res = validate_return_stmt(tree);
2990 break;
2991 case raise_stmt:
2992 res = validate_raise_stmt(tree);
2993 break;
2994 case import_stmt:
2995 res = validate_import_stmt(tree);
2996 break;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00002997 case import_name:
2998 res = validate_import_name(tree);
2999 break;
3000 case import_from:
3001 res = validate_import_from(tree);
3002 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003003 case global_stmt:
3004 res = validate_global_stmt(tree);
3005 break;
3006 case exec_stmt:
3007 res = validate_exec_stmt(tree);
3008 break;
3009 case assert_stmt:
3010 res = validate_assert_stmt(tree);
3011 break;
3012 case if_stmt:
3013 res = validate_if(tree);
3014 break;
3015 case while_stmt:
3016 res = validate_while(tree);
3017 break;
3018 case for_stmt:
3019 res = validate_for(tree);
3020 break;
3021 case try_stmt:
3022 res = validate_try(tree);
3023 break;
3024 case suite:
3025 res = validate_suite(tree);
3026 break;
3027 /*
3028 * Expression nodes.
3029 */
3030 case testlist:
3031 res = validate_testlist(tree);
3032 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003033 case yield_expr:
3034 res = validate_yield_expr(tree);
3035 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00003036 case testlist1:
3037 res = validate_testlist1(tree);
3038 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003039 case test:
3040 res = validate_test(tree);
3041 break;
3042 case and_test:
3043 res = validate_and_test(tree);
3044 break;
3045 case not_test:
3046 res = validate_not_test(tree);
3047 break;
3048 case comparison:
3049 res = validate_comparison(tree);
3050 break;
3051 case exprlist:
3052 res = validate_exprlist(tree);
3053 break;
3054 case comp_op:
3055 res = validate_comp_op(tree);
3056 break;
3057 case expr:
3058 res = validate_expr(tree);
3059 break;
3060 case xor_expr:
3061 res = validate_xor_expr(tree);
3062 break;
3063 case and_expr:
3064 res = validate_and_expr(tree);
3065 break;
3066 case shift_expr:
3067 res = validate_shift_expr(tree);
3068 break;
3069 case arith_expr:
3070 res = validate_arith_expr(tree);
3071 break;
3072 case term:
3073 res = validate_term(tree);
3074 break;
3075 case factor:
3076 res = validate_factor(tree);
3077 break;
3078 case power:
3079 res = validate_power(tree);
3080 break;
3081 case atom:
3082 res = validate_atom(tree);
3083 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003084
Fred Drakeff9ea482000-04-19 13:54:15 +00003085 default:
3086 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00003087 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003088 res = 0;
3089 break;
3090 }
3091 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003092 }
3093 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003094}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003095
3096
Guido van Rossum47478871996-08-21 14:32:37 +00003097static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003098validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003099{
3100 int res = validate_eval_input(tree);
3101
3102 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003103 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003104
3105 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003106}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003107
3108
Guido van Rossum3d602e31996-07-21 02:33:56 +00003109/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003110 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003111 */
Guido van Rossum47478871996-08-21 14:32:37 +00003112static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003113validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003114{
Fred Drakec2683dd2001-07-17 19:32:05 +00003115 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003116 int nch = NCH(tree) - 1;
3117 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003118 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003119
Fred Drakec2683dd2001-07-17 19:32:05 +00003120 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003121 if (TYPE(CHILD(tree, j)) == stmt)
3122 res = validate_stmt(CHILD(tree, j));
3123 else
3124 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003125 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003126 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003127 * user. Hopefully, this won't be needed. If a user reports getting
3128 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003129 */
3130 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003131 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003132
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003133 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003134}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003135
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003136static int
3137validate_encoding_decl(node *tree)
3138{
3139 int nch = NCH(tree);
3140 int res = ((nch == 1)
3141 && validate_file_input(CHILD(tree, 0)));
3142
3143 if (!res && !PyErr_Occurred())
3144 err_string("Error Parsing encoding_decl");
3145
3146 return res;
3147}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003148
Fred Drake43f8f9b1998-04-13 16:25:46 +00003149static PyObject*
3150pickle_constructor = NULL;
3151
3152
3153static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003154parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003155{
Fred Drake268397f1998-04-29 14:16:32 +00003156 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003157 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003158 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003159 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003160
Fred Drakec2683dd2001-07-17 19:32:05 +00003161 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003162 PyObject *newargs;
3163 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003164
Fred Drake2a6875e1999-09-20 22:32:18 +00003165 if ((empty_dict = PyDict_New()) == NULL)
3166 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003167 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003168 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003169 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003170 if (tuple != NULL) {
3171 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3172 Py_DECREF(tuple);
3173 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003174 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003175 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003176 }
3177 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003178 Py_XDECREF(empty_dict);
3179
Fred Drake43f8f9b1998-04-13 16:25:46 +00003180 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003181}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003182
3183
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003184/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003185 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003186 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003187 * We'd really have to write a wrapper around it all anyway to allow
3188 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003189 */
3190static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003191 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003192 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003193 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003194 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003195 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003196 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003197 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003198 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003199 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003200 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003201 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003202 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003203 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003204 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003205 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003206 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003207 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003208 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003209 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003210 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003211 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003212 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003213 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003214 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003215 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003216 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003217 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003218 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003219
Fred Drake43f8f9b1998-04-13 16:25:46 +00003220 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003221 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003222 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003223
Fred Drake268397f1998-04-29 14:16:32 +00003224 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003225 };
3226
3227
Mark Hammond62b1ab12002-07-23 06:31:15 +00003228PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003229
Mark Hammond62b1ab12002-07-23 06:31:15 +00003230PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00003231initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003232{
Fred Drake13130bc2001-08-15 16:44:56 +00003233 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003234
Christian Heimese93237d2007-12-19 02:37:44 +00003235 Py_TYPE(&PyST_Type) = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00003236 module = Py_InitModule("parser", parser_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003237 if (module == NULL)
3238 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003239
Fred Drake7a15ba51999-09-09 14:21:52 +00003240 if (parser_error == 0)
3241 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003242
Tim Peters6a627252003-07-21 14:25:23 +00003243 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00003244 /* caller will check PyErr_Occurred() */
3245 return;
Tim Peters6a627252003-07-21 14:25:23 +00003246 /* CAUTION: The code next used to skip bumping the refcount on
3247 * parser_error. That's a disaster if initparser() gets called more
3248 * than once. By incref'ing, we ensure that each module dict that
3249 * gets created owns its reference to the shared parser_error object,
3250 * and the file static parser_error vrbl owns a reference too.
3251 */
3252 Py_INCREF(parser_error);
3253 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3254 return;
3255
Fred Drakec2683dd2001-07-17 19:32:05 +00003256 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003257 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00003258 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003259 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003260
Fred Drake13130bc2001-08-15 16:44:56 +00003261 PyModule_AddStringConstant(module, "__copyright__",
3262 parser_copyright_string);
3263 PyModule_AddStringConstant(module, "__doc__",
3264 parser_doc_string);
3265 PyModule_AddStringConstant(module, "__version__",
3266 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003267
Fred Drake78bdb9b2001-07-19 20:17:15 +00003268 /* Register to support pickling.
3269 * If this fails, the import of this module will fail because an
3270 * exception will be raised here; should we clear the exception?
3271 */
Christian Heimes000a0742008-01-03 22:16:32 +00003272 copyreg = PyImport_ImportModuleNoBlock("copy_reg");
Fred Drake13130bc2001-08-15 16:44:56 +00003273 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003274 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003275
Fred Drake13130bc2001-08-15 16:44:56 +00003276 func = PyObject_GetAttrString(copyreg, "pickle");
3277 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3278 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003279 Py_XINCREF(pickle_constructor);
3280 if ((func != NULL) && (pickle_constructor != NULL)
3281 && (pickler != NULL)) {
3282 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003283
Georg Brandl684fd0c2006-05-25 19:15:31 +00003284 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3285 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003286 Py_XDECREF(res);
3287 }
3288 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003289 Py_XDECREF(pickle_constructor);
3290 Py_XDECREF(pickler);
3291 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003292 }
Fred Drakeff9ea482000-04-19 13:54:15 +00003293}