blob: f170c51b2e466bff9867c58879019d83f5874476 [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000026 */
27
Fred Drakeff9ea482000-04-19 13:54:15 +000028#include "Python.h" /* general Python API */
29#include "graminit.h" /* symbols defined in the grammar */
30#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000031#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000032#include "token.h" /* token definitions */
33 /* ISTERMINAL() / ISNONTERMINAL() */
34#include "compile.h" /* PyNode_Compile() */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000035
Fred Drake268397f1998-04-29 14:16:32 +000036#ifdef lint
37#include <note.h>
38#else
39#define NOTE(x)
40#endif
41
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000042/* String constants used to initialize module attributes.
43 *
44 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045static char parser_copyright_string[] =
46"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000047University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
48Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
49Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000050
51
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000052PyDoc_STRVAR(parser_doc_string,
53"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000054
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000055static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000056
57
Martin v. Löwis18e16552006-02-15 17:27:45 +000058typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000059typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000060 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000061 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000062
Thomas Wouters7e474022000-07-16 12:04:32 +000063/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000064 * original copyright statement is included below, and continues to apply
65 * in full to the function immediately following. All other material is
66 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
67 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000068 * new naming conventions. Added arguments to provide support for creating
69 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000070 */
71
Guido van Rossum52f2c051993-11-10 12:53:24 +000072
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000073static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000074node2tuple(node *n, /* node to convert */
75 SeqMaker mkseq, /* create sequence */
76 SeqInserter addelem, /* func. to add elem. in seq. */
Thomas Wouters89f507f2006-12-13 04:49:30 +000077 int lineno, /* include line numbers? */
78 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000079{
Guido van Rossum3d602e31996-07-21 02:33:56 +000080 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000081 Py_INCREF(Py_None);
82 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000083 }
84 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000085 int i;
86 PyObject *v;
87 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000088
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000089 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000090 if (v == NULL)
91 return (v);
92 w = PyInt_FromLong(TYPE(n));
93 if (w == NULL) {
94 Py_DECREF(v);
95 return ((PyObject*) NULL);
96 }
97 (void) addelem(v, 0, w);
98 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +000099 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000100 if (w == NULL) {
101 Py_DECREF(v);
102 return ((PyObject*) NULL);
103 }
104 (void) addelem(v, i+1, w);
105 }
Tim Peters6a627252003-07-21 14:25:23 +0000106
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000107 if (TYPE(n) == encoding_decl)
108 (void) addelem(v, i+1, PyString_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000109 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000110 }
111 else if (ISTERMINAL(TYPE(n))) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000112 PyObject *result = mkseq(2 + lineno + col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000113 if (result != NULL) {
114 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
115 (void) addelem(result, 1, PyString_FromString(STR(n)));
116 if (lineno == 1)
117 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000118 if (col_offset == 1)
119 (void) addelem(result, 3, PyInt_FromLong(n->n_col_offset));
Fred Drakeff9ea482000-04-19 13:54:15 +0000120 }
121 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000122 }
123 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000124 PyErr_SetString(PyExc_SystemError,
125 "unrecognized parse tree node type");
126 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000127 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000128}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000129/*
130 * End of material copyrighted by Stichting Mathematisch Centrum.
131 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000132
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000133
134
135/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000136 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000137 * field of the object type to identify which any given object represents.
138 * These should probably go in an external header to allow other extensions
139 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000140 */
141
Fred Drakec2683dd2001-07-17 19:32:05 +0000142#define PyST_EXPR 1
143#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000144
145
146/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000147 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000148 * naming style, but the code uses the new naming convention.
149 */
150
151static PyObject*
152parser_error = 0;
153
154
Fred Drakec2683dd2001-07-17 19:32:05 +0000155typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000156 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000157 node* st_node; /* the node* returned by the parser */
158 int st_type; /* EXPR or SUITE ? */
159} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000160
161
Jeremy Hylton938ace62002-07-17 16:30:39 +0000162static void parser_free(PyST_Object *st);
163static int parser_compare(PyST_Object *left, PyST_Object *right);
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000164static PyObject *parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000165
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000166
Fred Drake268397f1998-04-29 14:16:32 +0000167static
Fred Drakec2683dd2001-07-17 19:32:05 +0000168PyTypeObject PyST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000169 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000170 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000171 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000172 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000173 0, /* tp_itemsize */
174 (destructor)parser_free, /* tp_dealloc */
175 0, /* tp_print */
176 parser_getattr, /* tp_getattr */
177 0, /* tp_setattr */
178 (cmpfunc)parser_compare, /* tp_compare */
179 0, /* tp_repr */
180 0, /* tp_as_number */
181 0, /* tp_as_sequence */
182 0, /* tp_as_mapping */
183 0, /* tp_hash */
184 0, /* tp_call */
185 0, /* tp_str */
186 0, /* tp_getattro */
187 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000188
189 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000190 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000191
Fred Drakeff9ea482000-04-19 13:54:15 +0000192 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000193
194 /* __doc__ */
195 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000196}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000197
198
199static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000200parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000201{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000202 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000203
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000204 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000205 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000206
207 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000208 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000209
210 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000211 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000212
213 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000214 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000215
216 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000217 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000218
219 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000220 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000221
Fred Drakeff9ea482000-04-19 13:54:15 +0000222 if (v != 0)
223 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000224 }
225 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000226}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000227
228
Fred Drakec2683dd2001-07-17 19:32:05 +0000229/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000230 *
231 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
232 * This really just wraps a call to parser_compare_nodes() with some easy
233 * checks and protection code.
234 *
235 */
236static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000237parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000238{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000239 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000240 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000241
242 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000243 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000244
Fred Drakec2683dd2001-07-17 19:32:05 +0000245 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000246}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247
248
Fred Drakec2683dd2001-07-17 19:32:05 +0000249/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000250 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000251 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000252 * 'wrapper' object that holds a node* and allows it to be passed around in
253 * Python code.
254 *
255 */
256static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000257parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000258{
Fred Drakec2683dd2001-07-17 19:32:05 +0000259 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000260
261 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000262 o->st_node = st;
263 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000264 }
Fred Drake268397f1998-04-29 14:16:32 +0000265 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000266 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000267 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000268 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000269}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000270
271
Fred Drakec2683dd2001-07-17 19:32:05 +0000272/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000273 *
274 * This is called by a del statement that reduces the reference count to 0.
275 *
276 */
277static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000278parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000279{
Fred Drakec2683dd2001-07-17 19:32:05 +0000280 PyNode_Free(st->st_node);
281 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000282}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000283
284
Fred Drakec2683dd2001-07-17 19:32:05 +0000285/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000286 *
287 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000288 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000289 *
290 */
291static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000292parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000293{
Guido van Rossum47478871996-08-21 14:32:37 +0000294 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000295 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000296 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000297 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000298
Thomas Wouters89f507f2006-12-13 04:49:30 +0000299 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000300
Fred Drake268397f1998-04-29 14:16:32 +0000301 if (self == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000302 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
303 &PyST_Type, &self, &line_option,
304 &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000305 }
Fred Drake503d8d61998-04-13 18:45:18 +0000306 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000307 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
308 &line_option, &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000309 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000310 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000311 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000312 if (line_option != NULL) {
313 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
314 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000315 if (col_option != NULL) {
316 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
317 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000318 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000319 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000320 * since it's known to work already.
321 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000322 res = node2tuple(((PyST_Object*)self)->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000323 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000324 }
325 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000326}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000327
328
Fred Drakec2683dd2001-07-17 19:32:05 +0000329/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000330 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000331 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000332 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000333 *
334 */
335static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000336parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000337{
Guido van Rossum47478871996-08-21 14:32:37 +0000338 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000339 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000340 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000341 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000342
Thomas Wouters89f507f2006-12-13 04:49:30 +0000343 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000344
Fred Drake503d8d61998-04-13 18:45:18 +0000345 if (self == NULL)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000346 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
347 &PyST_Type, &self, &line_option,
348 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000349 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000350 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
351 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000352 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000353 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000354 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000355 if (line_option != 0) {
356 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
357 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000358 if (col_option != NULL) {
359 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
360 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000361 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000362 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000363 * since it's known to work already.
364 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000365 res = node2tuple(self->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000366 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000367 }
368 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000369}
Guido van Rossum47478871996-08-21 14:32:37 +0000370
371
Fred Drakec2683dd2001-07-17 19:32:05 +0000372/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000373 *
374 * This function creates code objects from the parse tree represented by
375 * the passed-in data object. An optional file name is passed in as well.
376 *
377 */
378static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000379parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000380{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000381 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000382 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000383 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000384
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000385 static char *keywords[] = {"ast", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000386
Fred Drake503d8d61998-04-13 18:45:18 +0000387 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000388 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
389 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000390 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000391 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000392 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000393
394 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000395 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000396
397 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000398}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000399
400
401/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
402 * PyObject* parser_issuite(PyObject* self, PyObject* args)
403 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000404 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000405 * a statement suite, respectively. The return is a Python truth value.
406 *
407 */
408static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000409parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000410{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000411 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000412 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000413
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000414 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000415
Fred Drake503d8d61998-04-13 18:45:18 +0000416 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000417 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000418 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000419 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000420 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000421
422 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000423 /* Check to see if the ST represents an expression or not. */
424 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000425 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000426 }
427 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000428}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000429
430
431static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000432parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000433{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000434 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000435 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000436
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000437 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000438
Fred Drake503d8d61998-04-13 18:45:18 +0000439 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000440 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000441 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000442 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000443 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000444
445 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000446 /* Check to see if the ST represents an expression or not. */
447 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000448 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000449 }
450 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000451}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000452
453
Fred Drake7a15ba51999-09-09 14:21:52 +0000454#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
455
Fred Drake503d8d61998-04-13 18:45:18 +0000456static PyMethodDef
457parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000458 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000459 PyDoc_STR("Compile this ST object into a code object.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000460 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000461 PyDoc_STR("Determines if this ST object was created from an expression.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000462 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000463 PyDoc_STR("Determines if this ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000464 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000465 PyDoc_STR("Creates a list-tree representation of this ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000466 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000467 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Fred Drake503d8d61998-04-13 18:45:18 +0000468
Fred Drake268397f1998-04-29 14:16:32 +0000469 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000470};
471
Fred Drake503d8d61998-04-13 18:45:18 +0000472
473static PyObject*
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000474parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000475{
Fred Drake503d8d61998-04-13 18:45:18 +0000476 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000477}
Fred Drake503d8d61998-04-13 18:45:18 +0000478
479
Guido van Rossum3d602e31996-07-21 02:33:56 +0000480/* err_string(char* message)
481 *
482 * Sets the error string for an exception of type ParserError.
483 *
484 */
485static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000486err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000487{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000488 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000489}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000490
491
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000492/* PyObject* parser_do_parse(PyObject* args, int type)
493 *
494 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000495 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000496 *
497 */
498static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000499parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000500{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000501 char* string = 0;
502 PyObject* res = 0;
503
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000504 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000505
506 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000507 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000508 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000509 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000510
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000511 if (n)
512 res = parser_newstobject(n, type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000513 }
514 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000515}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000516
517
518/* PyObject* parser_expr(PyObject* self, PyObject* args)
519 * PyObject* parser_suite(PyObject* self, PyObject* args)
520 *
521 * External interfaces to the parser itself. Which is called determines if
522 * the parser attempts to recognize an expression ('eval' form) or statement
523 * suite ('exec' form). The real work is done by parser_do_parse() above.
524 *
525 */
526static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000527parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000528{
Fred Drake268397f1998-04-29 14:16:32 +0000529 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000530 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000531}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000532
533
534static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000535parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000536{
Fred Drake268397f1998-04-29 14:16:32 +0000537 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000538 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000539}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000540
541
542
Fred Drakec2683dd2001-07-17 19:32:05 +0000543/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000544 * object requires that the input tuple be valid without having to rely on
545 * catching an exception from the compiler. This is done to allow the
546 * compiler itself to remain fast, since most of its input will come from
547 * the parser directly, and therefore be known to be syntactically correct.
548 * This validation is done to ensure that we don't core dump the compile
549 * phase, returning an exception instead.
550 *
551 * Two aspects can be broken out in this code: creating a node tree from
552 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000553 * advantageous to expand the number of ST types to include funcdefs and
554 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000555 * here. They are not necessary, and not quite as useful in a raw form.
556 * For now, let's get expressions and suites working reliably.
557 */
558
559
Jeremy Hylton938ace62002-07-17 16:30:39 +0000560static node* build_node_tree(PyObject *tuple);
561static int validate_expr_tree(node *tree);
562static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000563static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000564
Fred Drakec2683dd2001-07-17 19:32:05 +0000565/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000566 *
567 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000568 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000569 * tuple can be validated. It does this by checking the first code of the
570 * tuple, and, if acceptable, builds the internal representation. If this
571 * step succeeds, the internal representation is validated as fully as
572 * possible with the various validate_*() routines defined below.
573 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000574 * This function must be changed if support is to be added for PyST_FRAGMENT
575 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000576 *
577 */
578static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000579parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000580{
Fred Drake268397f1998-04-29 14:16:32 +0000581 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000582 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000583 PyObject *tuple;
584 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000585
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000586 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000587
Fred Drakec2683dd2001-07-17 19:32:05 +0000588 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000589 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000590 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000591 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000592 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000593 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000594 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000595 }
596 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000597 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000598 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000599 tree = build_node_tree(tuple);
600 if (tree != 0) {
601 int start_sym = TYPE(tree);
602 if (start_sym == eval_input) {
603 /* Might be an eval form. */
604 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000605 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000606 else
607 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000608 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000609 else if (start_sym == file_input) {
610 /* This looks like an exec form so far. */
611 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000612 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000613 else
614 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000615 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000616 else if (start_sym == encoding_decl) {
617 /* This looks like an encoding_decl so far. */
618 if (validate_encoding_decl(tree))
619 st = parser_newstobject(tree, PyST_SUITE);
620 else
621 PyNode_Free(tree);
622 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000623 else {
624 /* This is a fragment, at best. */
625 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000626 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000627 }
Guido van Rossum47478871996-08-21 14:32:37 +0000628 }
Guido van Rossum47478871996-08-21 14:32:37 +0000629 /* Make sure we throw an exception on all errors. We should never
630 * get this, but we'd do well to be sure something is done.
631 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000632 if (st == NULL && !PyErr_Occurred())
633 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000634
Fred Drakec2683dd2001-07-17 19:32:05 +0000635 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000636}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000637
638
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000639/* node* build_node_children()
640 *
641 * Iterate across the children of the current non-terminal node and build
642 * their structures. If successful, return the root of this portion of
643 * the tree, otherwise, 0. Any required exception will be specified already,
644 * and no memory will have been deallocated.
645 *
646 */
647static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000648build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000649{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000650 Py_ssize_t len = PyObject_Size(tuple);
651 Py_ssize_t i;
652 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000653
654 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000655 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000656 PyObject* elem = PySequence_GetItem(tuple, i);
657 int ok = elem != NULL;
658 long type = 0;
659 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000660
Fred Drakeff9ea482000-04-19 13:54:15 +0000661 if (ok)
662 ok = PySequence_Check(elem);
663 if (ok) {
664 PyObject *temp = PySequence_GetItem(elem, 0);
665 if (temp == NULL)
666 ok = 0;
667 else {
668 ok = PyInt_Check(temp);
669 if (ok)
670 type = PyInt_AS_LONG(temp);
671 Py_DECREF(temp);
672 }
673 }
674 if (!ok) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675 PyObject *err = Py_BuildValue("os", elem,
676 "Illegal node construct.");
677 PyErr_SetObject(parser_error, err);
678 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000679 Py_XDECREF(elem);
680 return (0);
681 }
682 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000683 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000684 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000685
Fred Drake0ac9b072000-09-12 21:58:06 +0000686 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000687 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000688 return 0;
689 }
690 temp = PySequence_GetItem(elem, 1);
691 if (temp == NULL)
692 return 0;
693 if (!PyString_Check(temp)) {
694 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000695 "second item in terminal node must be a string,"
696 " found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000697 temp->ob_type->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000698 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000699 return 0;
700 }
701 if (len == 3) {
702 PyObject *o = PySequence_GetItem(elem, 2);
703 if (o != NULL) {
704 if (PyInt_Check(o))
705 *line_num = PyInt_AS_LONG(o);
706 else {
707 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000708 "third item in terminal node must be an"
709 " integer, found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000710 temp->ob_type->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000711 Py_DECREF(o);
712 Py_DECREF(temp);
713 return 0;
714 }
715 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000716 }
717 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000718 len = PyString_GET_SIZE(temp) + 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000719 strn = (char *)PyObject_MALLOC(len);
Fred Drake0ac9b072000-09-12 21:58:06 +0000720 if (strn != NULL)
721 (void) memcpy(strn, PyString_AS_STRING(temp), len);
722 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000723 }
724 else if (!ISNONTERMINAL(type)) {
725 /*
726 * It has to be one or the other; this is an error.
727 * Throw an exception.
728 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000729 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
730 PyErr_SetObject(parser_error, err);
731 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000732 Py_XDECREF(elem);
733 return (0);
734 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000735 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000736 if (err == E_NOMEM) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000737 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000738 return (node *) PyErr_NoMemory();
739 }
740 if (err == E_OVERFLOW) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000741 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000742 PyErr_SetString(PyExc_ValueError,
743 "unsupported number of child nodes");
744 return NULL;
745 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000746
Fred Drakeff9ea482000-04-19 13:54:15 +0000747 if (ISNONTERMINAL(type)) {
748 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000749
Fred Drakeff9ea482000-04-19 13:54:15 +0000750 if (new_child != build_node_children(elem, new_child, line_num)) {
751 Py_XDECREF(elem);
752 return (0);
753 }
754 }
755 else if (type == NEWLINE) { /* It's true: we increment the */
756 ++(*line_num); /* line number *after* the newline! */
757 }
758 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000759 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000760 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000761}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000762
763
764static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000765build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000766{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000767 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000768 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000769 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000770
Guido van Rossum47478871996-08-21 14:32:37 +0000771 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000772 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000773 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000774 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000775 /*
776 * The tuple is simple, but it doesn't start with a start symbol.
777 * Throw an exception now and be done with it.
778 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000779 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000780 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000781 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000782 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000783 }
784 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000785 /*
786 * Not efficient, but that can be handled later.
787 */
788 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000789 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000790
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000791 if (num == encoding_decl) {
792 encoding = PySequence_GetItem(tuple, 2);
793 /* tuple isn't borrowed anymore here, need to DECREF */
794 tuple = PySequence_GetSlice(tuple, 0, 2);
795 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000796 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000797 if (res != NULL) {
798 if (res != build_node_children(tuple, res, &line_num)) {
799 PyNode_Free(res);
800 res = NULL;
801 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000802 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000803 Py_ssize_t len;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000804 len = PyString_GET_SIZE(encoding) + 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000805 res->n_str = (char *)PyObject_MALLOC(len);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000806 if (res->n_str != NULL)
807 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
808 Py_DECREF(encoding);
809 Py_DECREF(tuple);
810 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000811 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000812 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000813 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000814 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000815 * NONTERMINAL, we can't use it. Not sure the implementation
816 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000817 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000818 PyObject *err = Py_BuildValue("os", tuple,
819 "Illegal component tuple.");
820 PyErr_SetObject(parser_error, err);
821 Py_XDECREF(err);
822 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000823
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000824 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000825}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000826
827
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000828/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000829 * Validation routines used within the validation section:
830 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000831static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000832
Fred Drakeff9ea482000-04-19 13:54:15 +0000833#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
834#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
835#define validate_colon(ch) validate_terminal(ch, COLON, ":")
836#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
837#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
838#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
839#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
840#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
841#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
842#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
843#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
844#define validate_star(ch) validate_terminal(ch, STAR, "*")
845#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
846#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
847#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000848#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000849#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000850
Fred Drake0ac9b072000-09-12 21:58:06 +0000851#define VALIDATER(n) static int validate_##n(node *tree)
852
Fred Drakeff9ea482000-04-19 13:54:15 +0000853VALIDATER(node); VALIDATER(small_stmt);
854VALIDATER(class); VALIDATER(node);
855VALIDATER(parameters); VALIDATER(suite);
856VALIDATER(testlist); VALIDATER(varargslist);
Neal Norwitzc1505362006-12-28 06:47:50 +0000857VALIDATER(vfpdef); VALIDATER(vfplist);
Fred Drakeff9ea482000-04-19 13:54:15 +0000858VALIDATER(stmt); VALIDATER(simple_stmt);
859VALIDATER(expr_stmt); VALIDATER(power);
Guido van Rossum452bf512007-02-09 05:32:43 +0000860VALIDATER(del_stmt);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000861VALIDATER(return_stmt); VALIDATER(raise_stmt);
862VALIDATER(import_stmt); VALIDATER(import_stmt);
863VALIDATER(import_name); VALIDATER(yield_stmt);
864VALIDATER(global_stmt); VALIDATER(assert_stmt);
Neal Norwitzc1505362006-12-28 06:47:50 +0000865VALIDATER(compound_stmt); VALIDATER(vname);
Fred Drakeff9ea482000-04-19 13:54:15 +0000866VALIDATER(while); VALIDATER(for);
867VALIDATER(try); VALIDATER(except_clause);
868VALIDATER(test); VALIDATER(and_test);
869VALIDATER(not_test); VALIDATER(comparison);
Guido van Rossum0368b722007-05-11 16:50:42 +0000870VALIDATER(comp_op);
871VALIDATER(star_expr); VALIDATER(expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000872VALIDATER(xor_expr); VALIDATER(and_expr);
873VALIDATER(shift_expr); VALIDATER(arith_expr);
874VALIDATER(term); VALIDATER(factor);
875VALIDATER(atom); VALIDATER(lambdef);
876VALIDATER(trailer); VALIDATER(subscript);
877VALIDATER(subscriptlist); VALIDATER(sliceop);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000878VALIDATER(exprlist); VALIDATER(dictorsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000879VALIDATER(arglist); VALIDATER(argument);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000880VALIDATER(testlist1); VALIDATER(comp_for);
881VALIDATER(comp_iter); VALIDATER(comp_if);
882VALIDATER(testlist_comp); VALIDATER(yield_expr);
883VALIDATER(yield_or_testlist); VALIDATER(or_test);
884VALIDATER(test_nocond); VALIDATER(lambdef_nocond);
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
Neal Norwitzc1505362006-12-28 06:47:50 +00001114/* validate either vname or tname.
1115 * vname: NAME
1116 * tname: NAME [':' test]
1117 */
1118static int
1119validate_vname(node *tree)
1120{
1121 int nch = NCH(tree);
1122 if (TYPE(tree) == vname) {
1123 return nch == 1 && validate_name(CHILD(tree, 0), NULL);
1124 }
1125 else if (TYPE(tree) == tname) {
1126 if (nch == 1) {
1127 return validate_name(CHILD(tree, 0), NULL);
1128 }
1129 else if (nch == 3) {
1130 return validate_name(CHILD(tree, 0), NULL) &&
1131 validate_colon(CHILD(tree, 1)) &&
1132 validate_test(CHILD(tree, 2));
1133 }
1134 }
1135 return 0;
1136}
1137
1138/* '*' vname (',' vname ['=' test])* [',' '**' vname] | '**' vname
1139 * ..or tname in place of vname. vname: NAME; tname: NAME [':' test]
Fred Drakecff283c2000-08-21 22:24:43 +00001140 */
1141static int
1142validate_varargslist_trailer(node *tree, int start)
1143{
1144 int nch = NCH(tree);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001145 int res = 0, i;
Fred Drakecff283c2000-08-21 22:24:43 +00001146 int sym;
1147
1148 if (nch <= start) {
1149 err_string("expected variable argument trailer for varargslist");
1150 return 0;
1151 }
1152 sym = TYPE(CHILD(tree, start));
1153 if (sym == STAR) {
1154 /*
Neal Norwitzc1505362006-12-28 06:47:50 +00001155 * '*' vname (',' vname ['=' test])* [',' '**' vname] | '**' vname
Fred Drakecff283c2000-08-21 22:24:43 +00001156 */
1157 if (nch-start == 2)
Neal Norwitzc1505362006-12-28 06:47:50 +00001158 res = validate_vname(CHILD(tree, start+1));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001159 else if (nch-start == 5 && TYPE(CHILD(tree, start+2)) == COMMA)
Neal Norwitzc1505362006-12-28 06:47:50 +00001160 res = (validate_vname(CHILD(tree, start+1))
Fred Drakecff283c2000-08-21 22:24:43 +00001161 && validate_comma(CHILD(tree, start+2))
1162 && validate_doublestar(CHILD(tree, start+3))
Neal Norwitzc1505362006-12-28 06:47:50 +00001163 && validate_vname(CHILD(tree, start+4)));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001164 else {
Neal Norwitzc1505362006-12-28 06:47:50 +00001165 /* skip over vname (',' vname ['=' test])* */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001166 i = start + 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001167 if (TYPE(CHILD(tree, i)) == vname ||
1168 TYPE(CHILD(tree, i)) == tname) { /* skip over vname or tname */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001169 i += 1;
1170 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001171 while (res && i+1 < nch) { /* validate (',' vname ['=' test])* */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001172 res = validate_comma(CHILD(tree, i));
1173 if (TYPE(CHILD(tree, i+1)) == DOUBLESTAR)
1174 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001175 res = res && validate_vname(CHILD(tree, i+1));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001176 if (res && i+2 < nch && TYPE(CHILD(tree, i+2)) == EQUAL) {
1177 res = res && (i+3 < nch)
1178 && validate_test(CHILD(tree, i+3));
1179 i += 4;
1180 }
1181 else {
1182 i += 2;
1183 }
1184 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001185 /* [',' '**' vname] */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001186 if (res && i+1 < nch && TYPE(CHILD(tree, i+1)) == DOUBLESTAR) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001187 res = validate_vname(CHILD(tree, i+2));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001188 }
1189 }
Fred Drakecff283c2000-08-21 22:24:43 +00001190 }
1191 else if (sym == DOUBLESTAR) {
1192 /*
1193 * '**' NAME
1194 */
1195 if (nch-start == 2)
Neal Norwitzc1505362006-12-28 06:47:50 +00001196 res = validate_vname(CHILD(tree, start+1));
Fred Drakecff283c2000-08-21 22:24:43 +00001197 }
1198 if (!res)
1199 err_string("illegal variable argument trailer for varargslist");
1200 return res;
1201}
1202
1203
Neal Norwitzc1505362006-12-28 06:47:50 +00001204/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001205 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001206 * Validate typedargslist or varargslist.
1207 *
1208 * typedargslist: ((tfpdef ['=' test] ',')*
1209 * ('*' [tname] (',' tname ['=' test])* [',' '**' tname] |
1210 * '**' tname)
1211 * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
1212 * tname: NAME [':' test]
1213 * tfpdef: tname | '(' tfplist ')'
1214 * tfplist: tfpdef (',' tfpdef)* [',']
1215 * varargslist: ((vfpdef ['=' test] ',')*
1216 * ('*' [vname] (',' vname ['=' test])* [',' '**' vname] |
1217 * '**' vname)
1218 * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
1219 * vname: NAME
1220 * vfpdef: vname | '(' vfplist ')'
1221 * vfplist: vfpdef (',' vfpdef)* [',']
Guido van Rossum3d602e31996-07-21 02:33:56 +00001222 *
1223 */
Guido van Rossum47478871996-08-21 14:32:37 +00001224static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001225validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001226{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001227 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001228 int res = (TYPE(tree) == varargslist ||
1229 TYPE(tree) == typedargslist) &&
1230 (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001231 int sym;
Fred Drakeb6429a22000-12-11 22:08:27 +00001232 if (!res)
1233 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001234 if (nch < 1) {
1235 err_string("varargslist missing child nodes");
1236 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001237 }
Fred Drakecff283c2000-08-21 22:24:43 +00001238 sym = TYPE(CHILD(tree, 0));
1239 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001240 /* whole thing matches:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001241 * '*' [NAME] (',' NAME ['=' test])* [',' '**' NAME] | '**' NAME
Fred Drakeb6429a22000-12-11 22:08:27 +00001242 */
Fred Drakecff283c2000-08-21 22:24:43 +00001243 res = validate_varargslist_trailer(tree, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001244 else if (sym == vfpdef || sym == tfpdef) {
Fred Drakecff283c2000-08-21 22:24:43 +00001245 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001246
Fred Drakecff283c2000-08-21 22:24:43 +00001247 sym = TYPE(CHILD(tree, nch-1));
Neal Norwitzc1505362006-12-28 06:47:50 +00001248 if (sym == vname || sym == tname) {
Fred Drakecff283c2000-08-21 22:24:43 +00001249 /*
Neal Norwitzc1505362006-12-28 06:47:50 +00001250 * (vfpdef ['=' test] ',')+
1251 * ('*' vname [',' '**' vname]
1252 * | '**' vname)
Fred Drakecff283c2000-08-21 22:24:43 +00001253 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001254 /* skip over (vfpdef ['=' test] ',')+ */
Fred Drakecff283c2000-08-21 22:24:43 +00001255 while (res && (i+2 <= nch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001256 res = validate_vfpdef(CHILD(tree, i));
Fred Drakecff283c2000-08-21 22:24:43 +00001257 ++i;
1258 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1259 res = (validate_equal(CHILD(tree, i))
1260 && validate_test(CHILD(tree, i+1)));
1261 if (res)
1262 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001263 }
Fred Drakecff283c2000-08-21 22:24:43 +00001264 if (res && i < nch) {
1265 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001266 ++i;
1267 if (res && i < nch
1268 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1269 || TYPE(CHILD(tree, i)) == STAR))
1270 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001271 }
1272 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001273 /* .. ('*' [NAME] (',' NAME ['=' test])* [',' '**' NAME] | '**' NAME)
Fred Drakeb6429a22000-12-11 22:08:27 +00001274 * i --^^^
1275 */
Fred Drakecff283c2000-08-21 22:24:43 +00001276 if (res)
1277 res = validate_varargslist_trailer(tree, i);
1278 }
1279 else {
1280 /*
Neal Norwitzc1505362006-12-28 06:47:50 +00001281 * vfpdef ['=' test] (',' vfpdef ['=' test])* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00001282 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001283 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001284 if (sym == COMMA) {
1285 res = validate_comma(CHILD(tree, nch-1));
1286 if (!res)
1287 return 0;
1288 --nch;
1289 }
1290 /*
Neal Norwitzc1505362006-12-28 06:47:50 +00001291 * vfpdef ['=' test] (',' vfpdef ['=' test])*
Fred Drakecff283c2000-08-21 22:24:43 +00001292 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001293 res = validate_vfpdef(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001294 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001295 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1296 res = (validate_equal(CHILD(tree, i))
1297 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001298 i += 2;
1299 }
1300 /*
Neal Norwitzc1505362006-12-28 06:47:50 +00001301 * ... (',' vfpdef ['=' test])*
Fred Drakecff283c2000-08-21 22:24:43 +00001302 * i ---^^^
1303 */
1304 while (res && (nch - i) >= 2) {
1305 res = (validate_comma(CHILD(tree, i))
Neal Norwitzc1505362006-12-28 06:47:50 +00001306 && validate_vfpdef(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001307 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001308 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1309 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001310 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001311 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001312 }
1313 }
1314 if (res && nch - i != 0) {
1315 res = 0;
1316 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001317 }
1318 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001319 }
Fred Drakecff283c2000-08-21 22:24:43 +00001320 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001321}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001322
1323
Nick Coghlan650f0d02007-04-15 12:05:43 +00001324/* comp_iter: comp_for | comp_if
Fred Drakecff283c2000-08-21 22:24:43 +00001325 */
1326static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001327validate_comp_iter(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001328{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001329 int res = (validate_ntype(tree, comp_iter)
1330 && validate_numnodes(tree, 1, "comp_iter"));
1331 if (res && TYPE(CHILD(tree, 0)) == comp_for)
1332 res = validate_comp_for(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001333 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001334 res = validate_comp_if(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001335
1336 return res;
1337}
1338
Nick Coghlan650f0d02007-04-15 12:05:43 +00001339/* comp_for: 'for' exprlist 'in' test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001340 */
1341static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001342validate_comp_for(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001343{
1344 int nch = NCH(tree);
1345 int res;
1346
1347 if (nch == 5)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001348 res = validate_comp_iter(CHILD(tree, 4));
Fred Drakecff283c2000-08-21 22:24:43 +00001349 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001350 res = validate_numnodes(tree, 4, "comp_for");
Raymond Hettinger354433a2004-05-19 08:20:33 +00001351
1352 if (res)
1353 res = (validate_name(CHILD(tree, 0), "for")
1354 && validate_exprlist(CHILD(tree, 1))
1355 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001356 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001357
1358 return res;
1359}
1360
Nick Coghlan650f0d02007-04-15 12:05:43 +00001361/* comp_if: 'if' test_nocond [comp_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001362 */
1363static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001364validate_comp_if(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001365{
1366 int nch = NCH(tree);
1367 int res;
1368
1369 if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001370 res = validate_comp_iter(CHILD(tree, 2));
Fred Drakecff283c2000-08-21 22:24:43 +00001371 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001372 res = validate_numnodes(tree, 2, "comp_if");
Fred Drakecff283c2000-08-21 22:24:43 +00001373
1374 if (res)
1375 res = (validate_name(CHILD(tree, 0), "if")
Nick Coghlan650f0d02007-04-15 12:05:43 +00001376 && validate_test_nocond(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001377
1378 return res;
1379}
1380
1381
Neal Norwitzc1505362006-12-28 06:47:50 +00001382/* validate_vfpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001383 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001384 * Validate vfpdef or tfpdef.
1385 *
1386 * vname: NAME
1387 * vfpdef: vname | '(' vfplist ')'
1388 * vfplist: vfpdef (',' vfpdef)* [',']
1389 *
1390 * tname: NAME [':' test]
1391 * tfpdef: tname | '(' tfplist ')'
1392 * tfplist: tfpdef (',' tfpdef)* [',']
1393 *
Guido van Rossum3d602e31996-07-21 02:33:56 +00001394 */
Guido van Rossum47478871996-08-21 14:32:37 +00001395static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001396validate_vfpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001397{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001398 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001399 int typ = TYPE(tree);
1400 int res = typ == vfpdef || typ == tfpdef;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001401
Guido van Rossum3d602e31996-07-21 02:33:56 +00001402 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001403 if (nch == 1)
Neal Norwitzc1505362006-12-28 06:47:50 +00001404 res = validate_vname(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001405 else if (nch == 3)
1406 res = (validate_lparen(CHILD(tree, 0))
Neal Norwitzc1505362006-12-28 06:47:50 +00001407 && validate_vfplist(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001408 && validate_rparen(CHILD(tree, 2)));
1409 else
1410 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001411 }
1412 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001413}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001414
1415
Guido van Rossum47478871996-08-21 14:32:37 +00001416static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001417validate_vfplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001418{
Neal Norwitzc1505362006-12-28 06:47:50 +00001419 return (validate_repeating_list(tree, vfplist,
1420 validate_vfpdef, "vfplist"));
Fred Drakeff9ea482000-04-19 13:54:15 +00001421}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001422
1423
Guido van Rossum3d602e31996-07-21 02:33:56 +00001424/* simple_stmt | compound_stmt
1425 *
1426 */
Guido van Rossum47478871996-08-21 14:32:37 +00001427static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001428validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001429{
1430 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001431 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001432
Guido van Rossum3d602e31996-07-21 02:33:56 +00001433 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001434 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001435
Fred Drakeff9ea482000-04-19 13:54:15 +00001436 if (TYPE(tree) == simple_stmt)
1437 res = validate_simple_stmt(tree);
1438 else
1439 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001440 }
1441 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001442}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001443
1444
Guido van Rossum3d602e31996-07-21 02:33:56 +00001445/* small_stmt (';' small_stmt)* [';'] NEWLINE
1446 *
1447 */
Guido van Rossum47478871996-08-21 14:32:37 +00001448static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001449validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001450{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001451 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001452 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001453 && (nch >= 2)
1454 && validate_small_stmt(CHILD(tree, 0))
1455 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001456
Guido van Rossum3d602e31996-07-21 02:33:56 +00001457 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001458 res = validate_numnodes(tree, 2, "simple_stmt");
1459 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001460 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001461 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001462 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001463 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001464
Fred Drakeff9ea482000-04-19 13:54:15 +00001465 for (i = 1; res && (i < nch); i += 2)
1466 res = (validate_semi(CHILD(tree, i))
1467 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001468 }
1469 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001470}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001471
1472
Guido van Rossum47478871996-08-21 14:32:37 +00001473static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001474validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001475{
1476 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001477 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001478
Fred Drake0ac9b072000-09-12 21:58:06 +00001479 if (res) {
1480 int ntype = TYPE(CHILD(tree, 0));
1481
1482 if ( (ntype == expr_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001483 || (ntype == del_stmt)
1484 || (ntype == pass_stmt)
1485 || (ntype == flow_stmt)
1486 || (ntype == import_stmt)
1487 || (ntype == global_stmt)
Georg Brandl7cae87c2006-09-06 06:51:57 +00001488 || (ntype == assert_stmt))
Fred Drake0ac9b072000-09-12 21:58:06 +00001489 res = validate_node(CHILD(tree, 0));
1490 else {
1491 res = 0;
1492 err_string("illegal small_stmt child type");
1493 }
1494 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001495 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001496 res = 0;
1497 PyErr_Format(parser_error,
1498 "Unrecognized child node of small_stmt: %d.",
1499 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001500 }
1501 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001502}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001503
1504
1505/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001506 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001507 */
Guido van Rossum47478871996-08-21 14:32:37 +00001508static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001509validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001510{
1511 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001512 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001513 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001514
1515 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001516 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001517
1518 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001519 ntype = TYPE(tree);
1520 if ( (ntype == if_stmt)
1521 || (ntype == while_stmt)
1522 || (ntype == for_stmt)
1523 || (ntype == try_stmt)
1524 || (ntype == funcdef)
1525 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001526 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001527 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001528 res = 0;
1529 PyErr_Format(parser_error,
1530 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001531 }
1532 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001533}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001534
1535
Guido van Rossum47478871996-08-21 14:32:37 +00001536static int
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001537validate_yield_or_testlist(node *tree)
1538{
1539 if (TYPE(tree) == yield_expr)
1540 return validate_yield_expr(tree);
1541 else
1542 return validate_testlist(tree);
1543}
1544
1545static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001546validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001547{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001548 int j;
1549 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001550 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001551 && is_odd(nch)
1552 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001553
Fred Drake28f739a2000-08-25 22:42:40 +00001554 if (res && nch == 3
1555 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001556 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1557 && validate_yield_or_testlist(CHILD(tree, 2));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001558
Fred Drake28f739a2000-08-25 22:42:40 +00001559 if (res) {
1560 char *s = STR(CHILD(CHILD(tree, 1), 0));
1561
1562 res = (strcmp(s, "+=") == 0
1563 || strcmp(s, "-=") == 0
1564 || strcmp(s, "*=") == 0
1565 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001566 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001567 || strcmp(s, "%=") == 0
1568 || strcmp(s, "&=") == 0
1569 || strcmp(s, "|=") == 0
1570 || strcmp(s, "^=") == 0
1571 || strcmp(s, "<<=") == 0
1572 || strcmp(s, ">>=") == 0
1573 || strcmp(s, "**=") == 0);
1574 if (!res)
1575 err_string("illegal augmmented assignment operator");
1576 }
1577 }
1578 else {
1579 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001580 res = validate_equal(CHILD(tree, j))
1581 && validate_yield_or_testlist(CHILD(tree, j + 1));
Fred Drake28f739a2000-08-25 22:42:40 +00001582 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001583 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001584}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001585
1586
Guido van Rossum47478871996-08-21 14:32:37 +00001587static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001588validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001589{
1590 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001591 && validate_name(CHILD(tree, 0), "del")
1592 && validate_exprlist(CHILD(tree, 1)));
1593}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001594
1595
Guido van Rossum47478871996-08-21 14:32:37 +00001596static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001597validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001598{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001599 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001600 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001601 && ((nch == 1) || (nch == 2))
1602 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001603
Guido van Rossum3d602e31996-07-21 02:33:56 +00001604 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001605 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001606
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001607 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001608}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001609
1610
Guido van Rossum47478871996-08-21 14:32:37 +00001611static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001612validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001613{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001614 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001615 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001616 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001617
Guido van Rossum3d602e31996-07-21 02:33:56 +00001618 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001619 res = validate_name(CHILD(tree, 0), "raise");
1620 if (res && (nch >= 2))
1621 res = validate_test(CHILD(tree, 1));
1622 if (res && nch > 2) {
1623 res = (validate_comma(CHILD(tree, 2))
1624 && validate_test(CHILD(tree, 3)));
1625 if (res && (nch > 4))
1626 res = (validate_comma(CHILD(tree, 4))
1627 && validate_test(CHILD(tree, 5)));
1628 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001629 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001630 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001631 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001632 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001633 res = (validate_comma(CHILD(tree, 2))
1634 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001635
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001636 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001637}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001638
1639
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001640/* yield_expr: 'yield' [testlist]
1641 */
1642static int
1643validate_yield_expr(node *tree)
1644{
1645 int nch = NCH(tree);
1646 int res = (validate_ntype(tree, yield_expr)
1647 && ((nch == 1) || (nch == 2))
1648 && validate_name(CHILD(tree, 0), "yield"));
1649
1650 if (res && (nch == 2))
1651 res = validate_testlist(CHILD(tree, 1));
1652
1653 return (res);
1654}
1655
1656
1657/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001658 */
1659static int
1660validate_yield_stmt(node *tree)
1661{
1662 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001663 && validate_numnodes(tree, 1, "yield_stmt")
1664 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001665}
1666
1667
Fred Drakecff283c2000-08-21 22:24:43 +00001668static int
1669validate_import_as_name(node *tree)
1670{
1671 int nch = NCH(tree);
1672 int ok = validate_ntype(tree, import_as_name);
1673
1674 if (ok) {
1675 if (nch == 1)
1676 ok = validate_name(CHILD(tree, 0), NULL);
1677 else if (nch == 3)
1678 ok = (validate_name(CHILD(tree, 0), NULL)
1679 && validate_name(CHILD(tree, 1), "as")
1680 && validate_name(CHILD(tree, 2), NULL));
1681 else
1682 ok = validate_numnodes(tree, 3, "import_as_name");
1683 }
1684 return ok;
1685}
1686
1687
Fred Drake71137082001-01-07 05:59:59 +00001688/* dotted_name: NAME ("." NAME)*
1689 */
1690static int
1691validate_dotted_name(node *tree)
1692{
1693 int nch = NCH(tree);
1694 int res = (validate_ntype(tree, dotted_name)
1695 && is_odd(nch)
1696 && validate_name(CHILD(tree, 0), NULL));
1697 int i;
1698
1699 for (i = 1; res && (i < nch); i += 2) {
1700 res = (validate_dot(CHILD(tree, i))
1701 && validate_name(CHILD(tree, i+1), NULL));
1702 }
1703 return res;
1704}
1705
1706
Fred Drakecff283c2000-08-21 22:24:43 +00001707/* dotted_as_name: dotted_name [NAME NAME]
1708 */
1709static int
1710validate_dotted_as_name(node *tree)
1711{
1712 int nch = NCH(tree);
1713 int res = validate_ntype(tree, dotted_as_name);
1714
1715 if (res) {
1716 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001717 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001718 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001719 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001720 && validate_name(CHILD(tree, 1), "as")
1721 && validate_name(CHILD(tree, 2), NULL));
1722 else {
1723 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001724 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001725 }
1726 }
1727 return res;
1728}
1729
1730
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001731/* dotted_as_name (',' dotted_as_name)* */
1732static int
1733validate_dotted_as_names(node *tree)
1734{
1735 int nch = NCH(tree);
1736 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1737 int i;
1738
1739 for (i = 1; res && (i < nch); i += 2)
1740 res = (validate_comma(CHILD(tree, i))
1741 && validate_dotted_as_name(CHILD(tree, i + 1)));
1742 return (res);
1743}
1744
1745
1746/* import_as_name (',' import_as_name)* [','] */
1747static int
1748validate_import_as_names(node *tree)
1749{
1750 int nch = NCH(tree);
1751 int res = validate_import_as_name(CHILD(tree, 0));
1752 int i;
1753
1754 for (i = 1; res && (i + 1 < nch); i += 2)
1755 res = (validate_comma(CHILD(tree, i))
1756 && validate_import_as_name(CHILD(tree, i + 1)));
1757 return (res);
1758}
1759
1760
1761/* 'import' dotted_as_names */
1762static int
1763validate_import_name(node *tree)
1764{
1765 return (validate_ntype(tree, import_name)
1766 && validate_numnodes(tree, 2, "import_name")
1767 && validate_name(CHILD(tree, 0), "import")
1768 && validate_dotted_as_names(CHILD(tree, 1)));
1769}
1770
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001771/* Helper function to count the number of leading dots in
1772 * 'from ...module import name'
1773 */
1774static int
1775count_from_dots(node *tree)
1776{
1777 int i;
1778 for (i = 0; i < NCH(tree); i++)
1779 if (TYPE(CHILD(tree, i)) != DOT)
1780 break;
1781 return i;
1782}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001783
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001784/* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001785 * import_as_names
Guido van Rossum3d602e31996-07-21 02:33:56 +00001786 */
Guido van Rossum47478871996-08-21 14:32:37 +00001787static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001788validate_import_from(node *tree)
1789{
1790 int nch = NCH(tree);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001791 int ndots = count_from_dots(tree);
1792 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1793 int offset = ndots + havename;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001794 int res = validate_ntype(tree, import_from)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001795 && (nch >= 4 + ndots)
1796 && validate_name(CHILD(tree, 0), "from")
1797 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1798 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001799
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001800 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1801 res = ((nch == offset + 5)
1802 && validate_lparen(CHILD(tree, offset + 2))
1803 && validate_import_as_names(CHILD(tree, offset + 3))
1804 && validate_rparen(CHILD(tree, offset + 4)));
1805 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1806 res = validate_import_as_names(CHILD(tree, offset + 2));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001807 return (res);
1808}
1809
1810
1811/* import_stmt: import_name | import_from */
1812static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001813validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001814{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001815 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001816 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001817
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001818 if (res) {
1819 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001820
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001821 if (ntype == import_name || ntype == import_from)
1822 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001823 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001824 res = 0;
1825 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001826 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001827 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001828 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001829 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001830 PyErr_Format(parser_error,
1831 "Unrecognized child node of import_stmt: %d.",
1832 TYPE(CHILD(tree, 0)));
1833 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001834 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001835}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001836
1837
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001838
1839
Guido van Rossum47478871996-08-21 14:32:37 +00001840static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001841validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001842{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001843 int j;
1844 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001845 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001846 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001847
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001848 if (!res && !PyErr_Occurred())
1849 err_string("illegal global statement");
1850
Guido van Rossum3d602e31996-07-21 02:33:56 +00001851 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001852 res = (validate_name(CHILD(tree, 0), "global")
1853 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001854 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001855 res = (validate_comma(CHILD(tree, j))
1856 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001857
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001858 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001859}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001860
1861
Guido van Rossum925e5471997-04-02 05:32:13 +00001862/* assert_stmt:
1863 *
1864 * 'assert' test [',' test]
1865 */
1866static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001867validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001868{
1869 int nch = NCH(tree);
1870 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001871 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001872 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001873 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001874
1875 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001876 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001877 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001878 res = (validate_comma(CHILD(tree, 2))
1879 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001880
1881 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001882}
Guido van Rossum925e5471997-04-02 05:32:13 +00001883
1884
Guido van Rossum47478871996-08-21 14:32:37 +00001885static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001886validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001887{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001888 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001889 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001890 && ((nch == 4) || (nch == 7))
1891 && validate_name(CHILD(tree, 0), "while")
1892 && validate_test(CHILD(tree, 1))
1893 && validate_colon(CHILD(tree, 2))
1894 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001895
Guido van Rossum3d602e31996-07-21 02:33:56 +00001896 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001897 res = (validate_name(CHILD(tree, 4), "else")
1898 && validate_colon(CHILD(tree, 5))
1899 && validate_suite(CHILD(tree, 6)));
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 Rossum47478871996-08-21 14:32:37 +00001905static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001906validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001907{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001908 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001909 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001910 && ((nch == 6) || (nch == 9))
1911 && validate_name(CHILD(tree, 0), "for")
1912 && validate_exprlist(CHILD(tree, 1))
1913 && validate_name(CHILD(tree, 2), "in")
1914 && validate_testlist(CHILD(tree, 3))
1915 && validate_colon(CHILD(tree, 4))
1916 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001917
Guido van Rossum3d602e31996-07-21 02:33:56 +00001918 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001919 res = (validate_name(CHILD(tree, 6), "else")
1920 && validate_colon(CHILD(tree, 7))
1921 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001922
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001923 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001924}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001925
1926
Guido van Rossum3d602e31996-07-21 02:33:56 +00001927/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001928 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001929 * | 'try' ':' suite 'finally' ':' suite
1930 *
1931 */
Guido van Rossum47478871996-08-21 14:32:37 +00001932static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001933validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001934{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001935 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001936 int pos = 3;
1937 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001938 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001939
1940 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001941 res = (validate_name(CHILD(tree, 0), "try")
1942 && validate_colon(CHILD(tree, 1))
1943 && validate_suite(CHILD(tree, 2))
1944 && validate_colon(CHILD(tree, nch - 2))
1945 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001946 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001947 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001948 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1949 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001950
1951 PyErr_Format(parser_error,
1952 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001953 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001954 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001955 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001956 res = (validate_except_clause(CHILD(tree, pos))
1957 && validate_colon(CHILD(tree, pos + 1))
1958 && validate_suite(CHILD(tree, pos + 2)));
1959 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001960 }
1961 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001962 res = validate_ntype(CHILD(tree, pos), NAME);
1963 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1964 res = (validate_numnodes(tree, 6, "try/finally")
1965 && validate_colon(CHILD(tree, 4))
1966 && validate_suite(CHILD(tree, 5)));
1967 else if (res) {
1968 if (nch == (pos + 3)) {
1969 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1970 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1971 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001972 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001973 }
1974 else if (nch == (pos + 6)) {
1975 res = (validate_name(CHILD(tree, pos), "except")
1976 && validate_colon(CHILD(tree, pos + 1))
1977 && validate_suite(CHILD(tree, pos + 2))
1978 && validate_name(CHILD(tree, pos + 3), "else"));
1979 }
1980 else
1981 res = validate_numnodes(tree, pos + 3, "try/except");
1982 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001983 }
1984 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001985}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001986
1987
Guido van Rossum47478871996-08-21 14:32:37 +00001988static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001989validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001990{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001991 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001992 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001993 && ((nch == 1) || (nch == 2) || (nch == 4))
1994 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001995
Guido van Rossum3d602e31996-07-21 02:33:56 +00001996 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001997 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001998 if (res && (nch == 4))
Guido van Rossumb940e112007-01-10 16:19:56 +00001999 res = (validate_name(CHILD(tree, 2), "as")
2000 && validate_ntype(CHILD(tree, 3), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002001
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002002 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002003}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002004
2005
Guido van Rossum47478871996-08-21 14:32:37 +00002006static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002007validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002008{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002009 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002010 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002011
Guido van Rossum3d602e31996-07-21 02:33:56 +00002012 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002013 res = ((nch == 1)
2014 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002015 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002016 res = validate_or_test(CHILD(tree, 0));
2017 res = (res && (nch == 1 || (nch == 5 &&
2018 validate_name(CHILD(tree, 1), "if") &&
2019 validate_or_test(CHILD(tree, 2)) &&
2020 validate_name(CHILD(tree, 3), "else") &&
2021 validate_test(CHILD(tree, 4)))));
2022 }
2023 return (res);
2024}
2025
2026static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002027validate_test_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002028{
2029 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002030 int res = validate_ntype(tree, test_nocond) && (nch == 1);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002031
Nick Coghlan650f0d02007-04-15 12:05:43 +00002032 if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
2033 res = (validate_lambdef_nocond(CHILD(tree, 0)));
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002034 else if (res) {
2035 res = (validate_or_test(CHILD(tree, 0)));
2036 }
2037 return (res);
2038}
2039
2040static int
2041validate_or_test(node *tree)
2042{
2043 int nch = NCH(tree);
2044 int res = validate_ntype(tree, or_test) && is_odd(nch);
2045
2046 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002047 int pos;
2048 res = validate_and_test(CHILD(tree, 0));
2049 for (pos = 1; res && (pos < nch); pos += 2)
2050 res = (validate_name(CHILD(tree, pos), "or")
2051 && validate_and_test(CHILD(tree, pos + 1)));
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_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002059{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002060 int pos;
2061 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002062 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002063 && is_odd(nch)
2064 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002065
Guido van Rossum3d602e31996-07-21 02:33:56 +00002066 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002067 res = (validate_name(CHILD(tree, pos), "and")
2068 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002069
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002070 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002071}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002072
2073
Guido van Rossum47478871996-08-21 14:32:37 +00002074static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002075validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002076{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002077 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002078 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002079
Guido van Rossum3d602e31996-07-21 02:33:56 +00002080 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002081 if (nch == 2)
2082 res = (validate_name(CHILD(tree, 0), "not")
2083 && validate_not_test(CHILD(tree, 1)));
2084 else if (nch == 1)
2085 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002086 }
2087 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002088}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002089
2090
Guido van Rossum47478871996-08-21 14:32:37 +00002091static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002092validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002093{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002094 int pos;
2095 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002096 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002097 && is_odd(nch)
Guido van Rossum0368b722007-05-11 16:50:42 +00002098 && validate_star_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002099
Guido van Rossum3d602e31996-07-21 02:33:56 +00002100 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002101 res = (validate_comp_op(CHILD(tree, pos))
Guido van Rossum0368b722007-05-11 16:50:42 +00002102 && validate_star_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002103
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002104 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002105}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002106
2107
Guido van Rossum47478871996-08-21 14:32:37 +00002108static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002109validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002110{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002111 int res = 0;
2112 int nch = NCH(tree);
2113
Guido van Rossum3d602e31996-07-21 02:33:56 +00002114 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002115 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002116 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002117 /*
2118 * Only child will be a terminal with a well-defined symbolic name
2119 * or a NAME with a string of either 'is' or 'in'
2120 */
2121 tree = CHILD(tree, 0);
2122 switch (TYPE(tree)) {
2123 case LESS:
2124 case GREATER:
2125 case EQEQUAL:
2126 case EQUAL:
2127 case LESSEQUAL:
2128 case GREATEREQUAL:
2129 case NOTEQUAL:
2130 res = 1;
2131 break;
2132 case NAME:
2133 res = ((strcmp(STR(tree), "in") == 0)
2134 || (strcmp(STR(tree), "is") == 0));
2135 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002136 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002137 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002138 }
2139 break;
2140 default:
Fred Drake661ea262000-10-24 19:57:45 +00002141 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002142 break;
2143 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002144 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002145 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002146 res = (validate_ntype(CHILD(tree, 0), NAME)
2147 && validate_ntype(CHILD(tree, 1), NAME)
2148 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2149 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2150 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2151 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2152 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002153 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002154 }
2155 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002156}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002157
2158
Guido van Rossum47478871996-08-21 14:32:37 +00002159static int
Guido van Rossum0368b722007-05-11 16:50:42 +00002160validate_star_expr(node *tree)
2161{
2162 int res = validate_ntype(tree, star_expr);
2163 if (!res) return res;
2164 if (NCH(tree) == 2) {
2165 return validate_ntype(CHILD(tree, 0), STAR) && \
2166 validate_expr(CHILD(tree, 1));
2167 } else {
2168 return validate_expr(CHILD(tree, 0));
2169 }
2170}
2171
2172
2173static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002174validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002175{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002176 int j;
2177 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002178 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002179 && is_odd(nch)
2180 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002181
Guido van Rossum3d602e31996-07-21 02:33:56 +00002182 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002183 res = (validate_xor_expr(CHILD(tree, j))
2184 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002185
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002186 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002187}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002188
2189
Guido van Rossum47478871996-08-21 14:32:37 +00002190static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002191validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002192{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002193 int j;
2194 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002195 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002196 && is_odd(nch)
2197 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002198
Guido van Rossum3d602e31996-07-21 02:33:56 +00002199 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002200 res = (validate_circumflex(CHILD(tree, j - 1))
2201 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002202
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002203 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002204}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002205
2206
Guido van Rossum47478871996-08-21 14:32:37 +00002207static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002208validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002209{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002210 int pos;
2211 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002212 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002213 && is_odd(nch)
2214 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002215
Guido van Rossum3d602e31996-07-21 02:33:56 +00002216 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002217 res = (validate_ampersand(CHILD(tree, pos))
2218 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002219
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002220 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002221}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002222
2223
2224static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002225validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002226 {
2227 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002228 int nch = NCH(tree);
2229 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002230 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002231
Guido van Rossum3d602e31996-07-21 02:33:56 +00002232 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002233 if (TYPE(CHILD(tree, pos)) != op1)
2234 res = validate_ntype(CHILD(tree, pos), op2);
2235 if (res)
2236 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002237 }
2238 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002239}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002240
2241
Guido van Rossum47478871996-08-21 14:32:37 +00002242static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002243validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002244{
2245 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002246 && validate_chain_two_ops(tree, validate_arith_expr,
2247 LEFTSHIFT, RIGHTSHIFT));
2248}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002249
2250
Guido van Rossum47478871996-08-21 14:32:37 +00002251static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002252validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002253{
2254 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002255 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2256}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002257
2258
Guido van Rossum47478871996-08-21 14:32:37 +00002259static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002260validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002261{
2262 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002263 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002264 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002265 && is_odd(nch)
2266 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002267
Guido van Rossum3d602e31996-07-21 02:33:56 +00002268 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002269 res = (((TYPE(CHILD(tree, pos)) == STAR)
2270 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002271 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002272 || (TYPE(CHILD(tree, pos)) == PERCENT))
2273 && validate_factor(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
Guido van Rossum3d602e31996-07-21 02:33:56 +00002279/* factor:
2280 *
2281 * factor: ('+'|'-'|'~') factor | power
2282 */
Guido van Rossum47478871996-08-21 14:32:37 +00002283static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002284validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002285{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002286 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002287 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002288 && (((nch == 2)
2289 && ((TYPE(CHILD(tree, 0)) == PLUS)
2290 || (TYPE(CHILD(tree, 0)) == MINUS)
2291 || (TYPE(CHILD(tree, 0)) == TILDE))
2292 && validate_factor(CHILD(tree, 1)))
2293 || ((nch == 1)
2294 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002295 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002296}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002297
2298
Guido van Rossum3d602e31996-07-21 02:33:56 +00002299/* power:
2300 *
2301 * power: atom trailer* ('**' factor)*
2302 */
Guido van Rossum47478871996-08-21 14:32:37 +00002303static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002304validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002305{
2306 int pos = 1;
2307 int nch = NCH(tree);
2308 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002309 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002310
2311 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002312 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002313 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002314 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002315 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002316 return (0);
2317 }
2318 for ( ; res && (pos < (nch - 1)); pos += 2)
2319 res = (validate_doublestar(CHILD(tree, pos))
2320 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002321 }
2322 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002323}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002324
2325
Guido van Rossum47478871996-08-21 14:32:37 +00002326static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002327validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002328{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002329 int pos;
2330 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002331 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002332
Fred Drakecff283c2000-08-21 22:24:43 +00002333 if (res && nch < 1)
2334 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002335 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002336 switch (TYPE(CHILD(tree, 0))) {
2337 case LPAR:
2338 res = ((nch <= 3)
2339 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002340
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002341 if (res && (nch == 3)) {
2342 if (TYPE(CHILD(tree, 1))==yield_expr)
2343 res = validate_yield_expr(CHILD(tree, 1));
2344 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00002345 res = validate_testlist_comp(CHILD(tree, 1));
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002346 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002347 break;
2348 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002349 if (nch == 2)
2350 res = validate_ntype(CHILD(tree, 1), RSQB);
2351 else if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002352 res = (validate_testlist_comp(CHILD(tree, 1))
Fred Drakecff283c2000-08-21 22:24:43 +00002353 && validate_ntype(CHILD(tree, 2), RSQB));
2354 else {
2355 res = 0;
2356 err_string("illegal list display atom");
2357 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002358 break;
2359 case LBRACE:
2360 res = ((nch <= 3)
2361 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002362
Fred Drakeff9ea482000-04-19 13:54:15 +00002363 if (res && (nch == 3))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002364 res = validate_dictorsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002365 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002366 case NAME:
2367 case NUMBER:
2368 res = (nch == 1);
2369 break;
2370 case STRING:
2371 for (pos = 1; res && (pos < nch); ++pos)
2372 res = validate_ntype(CHILD(tree, pos), STRING);
2373 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002374 case DOT:
2375 res = (nch == 3 &&
2376 validate_ntype(CHILD(tree, 1), DOT) &&
2377 validate_ntype(CHILD(tree, 2), DOT));
2378 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002379 default:
2380 res = 0;
2381 break;
2382 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002383 }
2384 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002385}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002386
2387
Nick Coghlan650f0d02007-04-15 12:05:43 +00002388/* testlist_comp:
2389 * test ( comp_for | (',' test)* [','] )
Fred Drake85bf3bb2000-08-23 15:35:26 +00002390 */
Fred Drakecff283c2000-08-21 22:24:43 +00002391static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002392validate_testlist_comp(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00002393{
2394 int nch = NCH(tree);
2395 int ok = nch;
2396
2397 if (nch == 0)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002398 err_string("missing child nodes of testlist_comp");
2399 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002400 ok = validate_test(CHILD(tree, 0));
Nick Coghlan650f0d02007-04-15 12:05:43 +00002401 }
Fred Drakecff283c2000-08-21 22:24:43 +00002402
2403 /*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002404 * comp_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002405 */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002406 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2407 ok = validate_comp_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002408 else {
2409 /* (',' test)* [','] */
2410 int i = 1;
2411 while (ok && nch - i >= 2) {
2412 ok = (validate_comma(CHILD(tree, i))
2413 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002414 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002415 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002416 if (ok && i == nch-1)
2417 ok = validate_comma(CHILD(tree, i));
2418 else if (i != nch) {
2419 ok = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002420 err_string("illegal trailing nodes for testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002421 }
2422 }
2423 return ok;
2424}
Fred Drakecff283c2000-08-21 22:24:43 +00002425
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002426/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002427 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002428 */
2429static int
2430validate_decorator(node *tree)
2431{
2432 int ok;
2433 int nch = NCH(tree);
2434 ok = (validate_ntype(tree, decorator) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002435 (nch == 3 || nch == 5 || nch == 6) &&
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002436 validate_at(CHILD(tree, 0)) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002437 validate_dotted_name(CHILD(tree, 1)) &&
2438 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002439
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002440 if (ok && nch != 3) {
2441 ok = (validate_lparen(CHILD(tree, 2)) &&
2442 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002443
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002444 if (ok && nch == 6)
2445 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002446 }
2447
2448 return ok;
2449}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002450
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002451/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002452 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002453 */
2454static int
2455validate_decorators(node *tree)
2456{
2457 int i, nch, ok;
2458 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002459 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002460
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002461 for (i = 0; ok && i < nch; ++i)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002462 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002463
2464 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002465}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002466
Guido van Rossum3d602e31996-07-21 02:33:56 +00002467/* funcdef:
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002468 *
2469 * -6 -5 -4 -3 -2 -1
2470 * [decorators] 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002471 */
Guido van Rossum47478871996-08-21 14:32:37 +00002472static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002473validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002474{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002475 int nch = NCH(tree);
2476 int ok = (validate_ntype(tree, funcdef)
2477 && ((nch == 5) || (nch == 6))
2478 && validate_name(RCHILD(tree, -5), "def")
2479 && validate_ntype(RCHILD(tree, -4), NAME)
2480 && validate_colon(RCHILD(tree, -2))
2481 && validate_parameters(RCHILD(tree, -3))
2482 && validate_suite(RCHILD(tree, -1)));
2483
2484 if (ok && (nch == 6))
2485 ok = validate_decorators(CHILD(tree, 0));
2486
2487 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002488}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002489
2490
Guido van Rossum47478871996-08-21 14:32:37 +00002491static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002492validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002493{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002494 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002495 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002496 && ((nch == 3) || (nch == 4))
2497 && validate_name(CHILD(tree, 0), "lambda")
2498 && validate_colon(CHILD(tree, nch - 2))
2499 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002500
Guido van Rossum3d602e31996-07-21 02:33:56 +00002501 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002502 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002503 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002504 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002505
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002506 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002507}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002508
2509
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002510static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002511validate_lambdef_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002512{
2513 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002514 int res = (validate_ntype(tree, lambdef_nocond)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002515 && ((nch == 3) || (nch == 4))
2516 && validate_name(CHILD(tree, 0), "lambda")
2517 && validate_colon(CHILD(tree, nch - 2))
2518 && validate_test(CHILD(tree, nch - 1)));
2519
2520 if (res && (nch == 4))
2521 res = validate_varargslist(CHILD(tree, 1));
2522 else if (!res && !PyErr_Occurred())
Nick Coghlan650f0d02007-04-15 12:05:43 +00002523 (void) validate_numnodes(tree, 3, "lambdef_nocond");
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002524
2525 return (res);
2526}
2527
2528
Guido van Rossum3d602e31996-07-21 02:33:56 +00002529/* arglist:
2530 *
Fred Drakecff283c2000-08-21 22:24:43 +00002531 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002532 */
Guido van Rossum47478871996-08-21 14:32:37 +00002533static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002534validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002535{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002536 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002537 int i = 0;
2538 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002539
2540 if (nch <= 0)
2541 /* raise the right error from having an invalid number of children */
2542 return validate_numnodes(tree, nch + 1, "arglist");
2543
Raymond Hettinger354433a2004-05-19 08:20:33 +00002544 if (nch > 1) {
2545 for (i=0; i<nch; i++) {
2546 if (TYPE(CHILD(tree, i)) == argument) {
2547 node *ch = CHILD(tree, i);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002548 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
Raymond Hettinger354433a2004-05-19 08:20:33 +00002549 err_string("need '(', ')' for generator expression");
2550 return 0;
2551 }
2552 }
2553 }
2554 }
2555
Fred Drakecff283c2000-08-21 22:24:43 +00002556 while (ok && nch-i >= 2) {
2557 /* skip leading (argument ',') */
2558 ok = (validate_argument(CHILD(tree, i))
2559 && validate_comma(CHILD(tree, i+1)));
2560 if (ok)
2561 i += 2;
2562 else
2563 PyErr_Clear();
2564 }
2565 ok = 1;
2566 if (nch-i > 0) {
2567 /*
2568 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002569 */
Fred Drakecff283c2000-08-21 22:24:43 +00002570 int sym = TYPE(CHILD(tree, i));
2571
2572 if (sym == argument) {
2573 ok = validate_argument(CHILD(tree, i));
2574 if (ok && i+1 != nch) {
2575 err_string("illegal arglist specification"
2576 " (extra stuff on end)");
2577 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002578 }
Fred Drakecff283c2000-08-21 22:24:43 +00002579 }
2580 else if (sym == STAR) {
2581 ok = validate_star(CHILD(tree, i));
2582 if (ok && (nch-i == 2))
2583 ok = validate_test(CHILD(tree, i+1));
2584 else if (ok && (nch-i == 5))
2585 ok = (validate_test(CHILD(tree, i+1))
2586 && validate_comma(CHILD(tree, i+2))
2587 && validate_doublestar(CHILD(tree, i+3))
2588 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002589 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002590 err_string("illegal use of '*' in arglist");
2591 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002592 }
2593 }
Fred Drakecff283c2000-08-21 22:24:43 +00002594 else if (sym == DOUBLESTAR) {
2595 if (nch-i == 2)
2596 ok = (validate_doublestar(CHILD(tree, i))
2597 && validate_test(CHILD(tree, i+1)));
2598 else {
2599 err_string("illegal use of '**' in arglist");
2600 ok = 0;
2601 }
2602 }
2603 else {
2604 err_string("illegal arglist specification");
2605 ok = 0;
2606 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002607 }
2608 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002609}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002610
2611
2612
2613/* argument:
2614 *
Nick Coghlan650f0d02007-04-15 12:05:43 +00002615 * [test '='] test [comp_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002616 */
Guido van Rossum47478871996-08-21 14:32:37 +00002617static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002618validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002619{
2620 int nch = NCH(tree);
2621 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002622 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002623 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002624
Raymond Hettinger354433a2004-05-19 08:20:33 +00002625 if (res && (nch == 2))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002626 res = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002627 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002628 res = (validate_equal(CHILD(tree, 1))
2629 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002630
2631 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002632}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002633
2634
2635
2636/* trailer:
2637 *
Guido van Rossum47478871996-08-21 14:32:37 +00002638 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002639 */
Guido van Rossum47478871996-08-21 14:32:37 +00002640static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002641validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002642{
2643 int nch = NCH(tree);
2644 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002645
2646 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002647 switch (TYPE(CHILD(tree, 0))) {
2648 case LPAR:
2649 res = validate_rparen(CHILD(tree, nch - 1));
2650 if (res && (nch == 3))
2651 res = validate_arglist(CHILD(tree, 1));
2652 break;
2653 case LSQB:
2654 res = (validate_numnodes(tree, 3, "trailer")
2655 && validate_subscriptlist(CHILD(tree, 1))
2656 && validate_ntype(CHILD(tree, 2), RSQB));
2657 break;
2658 case DOT:
2659 res = (validate_numnodes(tree, 2, "trailer")
2660 && validate_ntype(CHILD(tree, 1), NAME));
2661 break;
2662 default:
2663 res = 0;
2664 break;
2665 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002666 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002667 else {
2668 (void) validate_numnodes(tree, 2, "trailer");
2669 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002670 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002671}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002672
2673
Guido van Rossum47478871996-08-21 14:32:37 +00002674/* subscriptlist:
2675 *
2676 * subscript (',' subscript)* [',']
2677 */
2678static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002679validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002680{
2681 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002682 validate_subscript, "subscriptlist"));
2683}
Guido van Rossum47478871996-08-21 14:32:37 +00002684
2685
Guido van Rossum3d602e31996-07-21 02:33:56 +00002686/* subscript:
2687 *
Guido van Rossum47478871996-08-21 14:32:37 +00002688 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002689 */
Guido van Rossum47478871996-08-21 14:32:37 +00002690static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002691validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002692{
Guido van Rossum47478871996-08-21 14:32:37 +00002693 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002694 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002695 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002696
Guido van Rossum47478871996-08-21 14:32:37 +00002697 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002698 if (!PyErr_Occurred())
2699 err_string("invalid number of arguments for subscript node");
2700 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002701 }
Guido van Rossum47478871996-08-21 14:32:37 +00002702 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002703 /* take care of ('.' '.' '.') possibility */
2704 return (validate_numnodes(tree, 3, "subscript")
2705 && validate_dot(CHILD(tree, 0))
2706 && validate_dot(CHILD(tree, 1))
2707 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002708 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002709 if (TYPE(CHILD(tree, 0)) == test)
2710 res = validate_test(CHILD(tree, 0));
2711 else
2712 res = validate_colon(CHILD(tree, 0));
2713 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002714 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002715 /* Must be [test] ':' [test] [sliceop],
2716 * but at least one of the optional components will
2717 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002718 */
2719 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002720 res = validate_test(CHILD(tree, 0));
2721 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002722 }
2723 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002724 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002725 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002726 int rem = nch - ++offset;
2727 if (rem) {
2728 if (TYPE(CHILD(tree, offset)) == test) {
2729 res = validate_test(CHILD(tree, offset));
2730 ++offset;
2731 --rem;
2732 }
2733 if (res && rem)
2734 res = validate_sliceop(CHILD(tree, offset));
2735 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002736 }
2737 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002738}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002739
2740
Guido van Rossum47478871996-08-21 14:32:37 +00002741static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002742validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002743{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002744 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002745 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002746 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002747 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002748 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002749 }
Guido van Rossum47478871996-08-21 14:32:37 +00002750 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002751 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002752 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002753 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002754
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002755 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002756}
Guido van Rossum47478871996-08-21 14:32:37 +00002757
2758
2759static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002760validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002761{
2762 return (validate_repeating_list(tree, exprlist,
Guido van Rossum0368b722007-05-11 16:50:42 +00002763 validate_star_expr, "exprlist"));
Fred Drakeff9ea482000-04-19 13:54:15 +00002764}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002765
2766
Guido van Rossum47478871996-08-21 14:32:37 +00002767static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002768validate_dictorsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002769{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002770 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002771 int res = (validate_ntype(tree, dictorsetmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002772 && (nch >= 3)
2773 && validate_test(CHILD(tree, 0))
2774 && validate_colon(CHILD(tree, 1))
2775 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002776
Guido van Rossum3d602e31996-07-21 02:33:56 +00002777 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002778 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002779 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002780 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002781
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002782 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002783 int pos = 3;
2784 /* ( ',' test ':' test )* */
2785 while (res && (pos < nch)) {
2786 res = (validate_comma(CHILD(tree, pos))
2787 && validate_test(CHILD(tree, pos + 1))
2788 && validate_colon(CHILD(tree, pos + 2))
2789 && validate_test(CHILD(tree, pos + 3)));
2790 pos += 4;
2791 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002792 }
2793 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002794}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002795
2796
Guido van Rossum47478871996-08-21 14:32:37 +00002797static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002798validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002799{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002800 int pos;
2801 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002802 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002803 && (nch >= 2)
2804 && validate_testlist(CHILD(tree, 0))
2805 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002806
Guido van Rossum3d602e31996-07-21 02:33:56 +00002807 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002808 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002809
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002810 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002811}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002812
2813
Guido van Rossum47478871996-08-21 14:32:37 +00002814static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002815validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002816{
Fred Drakeff9ea482000-04-19 13:54:15 +00002817 int nch = 0; /* num. children on current node */
2818 int res = 1; /* result value */
2819 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002820
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002821 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002822 nch = NCH(tree);
2823 next = 0;
2824 switch (TYPE(tree)) {
2825 /*
2826 * Definition nodes.
2827 */
2828 case funcdef:
2829 res = validate_funcdef(tree);
2830 break;
2831 case classdef:
2832 res = validate_class(tree);
2833 break;
2834 /*
2835 * "Trivial" parse tree nodes.
2836 * (Why did I call these trivial?)
2837 */
2838 case stmt:
2839 res = validate_stmt(tree);
2840 break;
2841 case small_stmt:
2842 /*
Guido van Rossum452bf512007-02-09 05:32:43 +00002843 * expr_stmt | del_stmt | pass_stmt | flow_stmt
Georg Brandl7cae87c2006-09-06 06:51:57 +00002844 * | import_stmt | global_stmt | assert_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002845 */
2846 res = validate_small_stmt(tree);
2847 break;
2848 case flow_stmt:
2849 res = (validate_numnodes(tree, 1, "flow_stmt")
2850 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2851 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002852 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002853 || (TYPE(CHILD(tree, 0)) == return_stmt)
2854 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2855 if (res)
2856 next = CHILD(tree, 0);
2857 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002858 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002859 break;
Fred Drake02126f22001-07-17 02:59:15 +00002860 case yield_stmt:
2861 res = validate_yield_stmt(tree);
2862 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002863 /*
2864 * Compound statements.
2865 */
2866 case simple_stmt:
2867 res = validate_simple_stmt(tree);
2868 break;
2869 case compound_stmt:
2870 res = validate_compound_stmt(tree);
2871 break;
2872 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002873 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002874 */
2875 case expr_stmt:
2876 res = validate_expr_stmt(tree);
2877 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002878 case del_stmt:
2879 res = validate_del_stmt(tree);
2880 break;
2881 case pass_stmt:
2882 res = (validate_numnodes(tree, 1, "pass")
2883 && validate_name(CHILD(tree, 0), "pass"));
2884 break;
2885 case break_stmt:
2886 res = (validate_numnodes(tree, 1, "break")
2887 && validate_name(CHILD(tree, 0), "break"));
2888 break;
2889 case continue_stmt:
2890 res = (validate_numnodes(tree, 1, "continue")
2891 && validate_name(CHILD(tree, 0), "continue"));
2892 break;
2893 case return_stmt:
2894 res = validate_return_stmt(tree);
2895 break;
2896 case raise_stmt:
2897 res = validate_raise_stmt(tree);
2898 break;
2899 case import_stmt:
2900 res = validate_import_stmt(tree);
2901 break;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00002902 case import_name:
2903 res = validate_import_name(tree);
2904 break;
2905 case import_from:
2906 res = validate_import_from(tree);
2907 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002908 case global_stmt:
2909 res = validate_global_stmt(tree);
2910 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002911 case assert_stmt:
2912 res = validate_assert_stmt(tree);
2913 break;
2914 case if_stmt:
2915 res = validate_if(tree);
2916 break;
2917 case while_stmt:
2918 res = validate_while(tree);
2919 break;
2920 case for_stmt:
2921 res = validate_for(tree);
2922 break;
2923 case try_stmt:
2924 res = validate_try(tree);
2925 break;
2926 case suite:
2927 res = validate_suite(tree);
2928 break;
2929 /*
2930 * Expression nodes.
2931 */
2932 case testlist:
2933 res = validate_testlist(tree);
2934 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002935 case yield_expr:
2936 res = validate_yield_expr(tree);
2937 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002938 case testlist1:
2939 res = validate_testlist1(tree);
2940 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002941 case test:
2942 res = validate_test(tree);
2943 break;
2944 case and_test:
2945 res = validate_and_test(tree);
2946 break;
2947 case not_test:
2948 res = validate_not_test(tree);
2949 break;
2950 case comparison:
2951 res = validate_comparison(tree);
2952 break;
2953 case exprlist:
2954 res = validate_exprlist(tree);
2955 break;
2956 case comp_op:
2957 res = validate_comp_op(tree);
2958 break;
2959 case expr:
2960 res = validate_expr(tree);
2961 break;
2962 case xor_expr:
2963 res = validate_xor_expr(tree);
2964 break;
2965 case and_expr:
2966 res = validate_and_expr(tree);
2967 break;
2968 case shift_expr:
2969 res = validate_shift_expr(tree);
2970 break;
2971 case arith_expr:
2972 res = validate_arith_expr(tree);
2973 break;
2974 case term:
2975 res = validate_term(tree);
2976 break;
2977 case factor:
2978 res = validate_factor(tree);
2979 break;
2980 case power:
2981 res = validate_power(tree);
2982 break;
2983 case atom:
2984 res = validate_atom(tree);
2985 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002986
Fred Drakeff9ea482000-04-19 13:54:15 +00002987 default:
2988 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002989 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002990 res = 0;
2991 break;
2992 }
2993 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002994 }
2995 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002996}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002997
2998
Guido van Rossum47478871996-08-21 14:32:37 +00002999static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003000validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003001{
3002 int res = validate_eval_input(tree);
3003
3004 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003005 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003006
3007 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003008}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003009
3010
Guido van Rossum3d602e31996-07-21 02:33:56 +00003011/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003012 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003013 */
Guido van Rossum47478871996-08-21 14:32:37 +00003014static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003015validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003016{
Fred Drakec2683dd2001-07-17 19:32:05 +00003017 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003018 int nch = NCH(tree) - 1;
3019 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003020 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003021
Fred Drakec2683dd2001-07-17 19:32:05 +00003022 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003023 if (TYPE(CHILD(tree, j)) == stmt)
3024 res = validate_stmt(CHILD(tree, j));
3025 else
3026 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003027 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003028 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003029 * user. Hopefully, this won't be needed. If a user reports getting
3030 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003031 */
3032 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003033 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003034
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003035 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003036}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003037
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003038static int
3039validate_encoding_decl(node *tree)
3040{
3041 int nch = NCH(tree);
3042 int res = ((nch == 1)
3043 && validate_file_input(CHILD(tree, 0)));
3044
3045 if (!res && !PyErr_Occurred())
3046 err_string("Error Parsing encoding_decl");
3047
3048 return res;
3049}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003050
Fred Drake43f8f9b1998-04-13 16:25:46 +00003051static PyObject*
3052pickle_constructor = NULL;
3053
3054
3055static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003056parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003057{
Fred Drake268397f1998-04-29 14:16:32 +00003058 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003059 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003060 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003061 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003062
Fred Drakec2683dd2001-07-17 19:32:05 +00003063 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003064 PyObject *newargs;
3065 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003066
Fred Drake2a6875e1999-09-20 22:32:18 +00003067 if ((empty_dict = PyDict_New()) == NULL)
3068 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003069 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003070 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003071 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003072 if (tuple != NULL) {
3073 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3074 Py_DECREF(tuple);
3075 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003076 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003077 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003078 }
3079 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003080 Py_XDECREF(empty_dict);
3081
Fred Drake43f8f9b1998-04-13 16:25:46 +00003082 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003083}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003084
3085
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003086/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003087 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003088 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003089 * We'd really have to write a wrapper around it all anyway to allow
3090 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003091 */
3092static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003093 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003094 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003095 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003096 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003097 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003098 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003099 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003100 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003101 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003102 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003103 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003104 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003105 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003106 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003107 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003108 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003109 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003110 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003111 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003112 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003113 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003114 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003115 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003116 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003117 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003118 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003119 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003120 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003121
Fred Drake43f8f9b1998-04-13 16:25:46 +00003122 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003123 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003124 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003125
Fred Drake268397f1998-04-29 14:16:32 +00003126 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003127 };
3128
3129
Mark Hammond62b1ab12002-07-23 06:31:15 +00003130PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003131
Mark Hammond62b1ab12002-07-23 06:31:15 +00003132PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00003133initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003134{
Fred Drake13130bc2001-08-15 16:44:56 +00003135 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003136
3137 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00003138 module = Py_InitModule("parser", parser_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003139 if (module == NULL)
3140 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003141
Fred Drake7a15ba51999-09-09 14:21:52 +00003142 if (parser_error == 0)
3143 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003144
Tim Peters6a627252003-07-21 14:25:23 +00003145 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00003146 /* caller will check PyErr_Occurred() */
3147 return;
Tim Peters6a627252003-07-21 14:25:23 +00003148 /* CAUTION: The code next used to skip bumping the refcount on
3149 * parser_error. That's a disaster if initparser() gets called more
3150 * than once. By incref'ing, we ensure that each module dict that
3151 * gets created owns its reference to the shared parser_error object,
3152 * and the file static parser_error vrbl owns a reference too.
3153 */
3154 Py_INCREF(parser_error);
3155 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3156 return;
3157
Fred Drakec2683dd2001-07-17 19:32:05 +00003158 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003159 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00003160 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003161 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003162
Fred Drake13130bc2001-08-15 16:44:56 +00003163 PyModule_AddStringConstant(module, "__copyright__",
3164 parser_copyright_string);
3165 PyModule_AddStringConstant(module, "__doc__",
3166 parser_doc_string);
3167 PyModule_AddStringConstant(module, "__version__",
3168 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003169
Fred Drake78bdb9b2001-07-19 20:17:15 +00003170 /* Register to support pickling.
3171 * If this fails, the import of this module will fail because an
3172 * exception will be raised here; should we clear the exception?
3173 */
Fred Drake13130bc2001-08-15 16:44:56 +00003174 copyreg = PyImport_ImportModule("copy_reg");
3175 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003176 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003177
Fred Drake13130bc2001-08-15 16:44:56 +00003178 func = PyObject_GetAttrString(copyreg, "pickle");
3179 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3180 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003181 Py_XINCREF(pickle_constructor);
3182 if ((func != NULL) && (pickle_constructor != NULL)
3183 && (pickler != NULL)) {
3184 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003185
Thomas Wouters477c8d52006-05-27 19:21:47 +00003186 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3187 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003188 Py_XDECREF(res);
3189 }
3190 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003191 Py_XDECREF(pickle_constructor);
3192 Py_XDECREF(pickler);
3193 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003194 }
Fred Drakeff9ea482000-04-19 13:54:15 +00003195}