blob: 915bbdc7b2e29db3bef9fa94166803ef02508c9c [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000026 */
27
Fred Drakeff9ea482000-04-19 13:54:15 +000028#include "Python.h" /* general Python API */
29#include "graminit.h" /* symbols defined in the grammar */
30#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000031#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000032#include "token.h" /* token definitions */
33 /* ISTERMINAL() / ISNONTERMINAL() */
34#include "compile.h" /* PyNode_Compile() */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000035
Fred Drake268397f1998-04-29 14:16:32 +000036#ifdef lint
37#include <note.h>
38#else
39#define NOTE(x)
40#endif
41
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000042/* String constants used to initialize module attributes.
43 *
44 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045static char parser_copyright_string[] =
46"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000047University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
48Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
49Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000050
51
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000052PyDoc_STRVAR(parser_doc_string,
53"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000054
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000055static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000056
57
Martin v. Löwis18e16552006-02-15 17:27:45 +000058typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000059typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000060 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000061 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000062
Thomas Wouters7e474022000-07-16 12:04:32 +000063/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000064 * original copyright statement is included below, and continues to apply
65 * in full to the function immediately following. All other material is
66 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
67 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000068 * new naming conventions. Added arguments to provide support for creating
69 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000070 */
71
Guido van Rossum52f2c051993-11-10 12:53:24 +000072
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000073static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000074node2tuple(node *n, /* node to convert */
75 SeqMaker mkseq, /* create sequence */
76 SeqInserter addelem, /* func. to add elem. in seq. */
Jeremy Hylton60e96f62006-08-22 20:46:00 +000077 int lineno, /* include line numbers? */
78 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000079{
Guido van Rossum3d602e31996-07-21 02:33:56 +000080 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000081 Py_INCREF(Py_None);
82 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000083 }
84 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000085 int i;
86 PyObject *v;
87 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000088
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000089 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000090 if (v == NULL)
91 return (v);
92 w = PyInt_FromLong(TYPE(n));
93 if (w == NULL) {
94 Py_DECREF(v);
95 return ((PyObject*) NULL);
96 }
97 (void) addelem(v, 0, w);
98 for (i = 0; i < NCH(n); i++) {
Jeremy Hylton60e96f62006-08-22 20:46:00 +000099 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000100 if (w == NULL) {
101 Py_DECREF(v);
102 return ((PyObject*) NULL);
103 }
104 (void) addelem(v, i+1, w);
105 }
Tim Peters6a627252003-07-21 14:25:23 +0000106
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000107 if (TYPE(n) == encoding_decl)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000108 (void) addelem(v, i+1, PyString_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000109 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000110 }
111 else if (ISTERMINAL(TYPE(n))) {
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000112 PyObject *result = mkseq(2 + lineno + col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000113 if (result != NULL) {
114 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000115 (void) addelem(result, 1, PyString_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000116 if (lineno == 1)
117 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000118 if (col_offset == 1)
119 (void) addelem(result, 3, PyInt_FromLong(n->n_col_offset));
Fred Drakeff9ea482000-04-19 13:54:15 +0000120 }
121 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000122 }
123 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000124 PyErr_SetString(PyExc_SystemError,
125 "unrecognized parse tree node type");
126 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000127 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000128}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000129/*
130 * End of material copyrighted by Stichting Mathematisch Centrum.
131 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000132
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000133
134
135/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000136 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000137 * field of the object type to identify which any given object represents.
138 * These should probably go in an external header to allow other extensions
139 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000140 */
141
Fred Drakec2683dd2001-07-17 19:32:05 +0000142#define PyST_EXPR 1
143#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000144
145
146/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000147 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000148 * naming style, but the code uses the new naming convention.
149 */
150
151static PyObject*
152parser_error = 0;
153
154
Fred Drakec2683dd2001-07-17 19:32:05 +0000155typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000156 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000157 node* st_node; /* the node* returned by the parser */
158 int st_type; /* EXPR or SUITE ? */
159} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000160
161
Jeremy Hylton938ace62002-07-17 16:30:39 +0000162static void parser_free(PyST_Object *st);
163static int parser_compare(PyST_Object *left, PyST_Object *right);
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000164static PyObject *parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000165
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000166
Fred Drake268397f1998-04-29 14:16:32 +0000167static
Fred Drakec2683dd2001-07-17 19:32:05 +0000168PyTypeObject PyST_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000169 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000170 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000171 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000172 0, /* tp_itemsize */
173 (destructor)parser_free, /* tp_dealloc */
174 0, /* tp_print */
175 parser_getattr, /* tp_getattr */
176 0, /* tp_setattr */
177 (cmpfunc)parser_compare, /* tp_compare */
178 0, /* tp_repr */
179 0, /* tp_as_number */
180 0, /* tp_as_sequence */
181 0, /* tp_as_mapping */
182 0, /* tp_hash */
183 0, /* tp_call */
184 0, /* tp_str */
185 0, /* tp_getattro */
186 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000187
188 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000189 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000190
Fred Drakeff9ea482000-04-19 13:54:15 +0000191 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000192
193 /* __doc__ */
194 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000195}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000196
197
198static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000199parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000200{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000201 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000202
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000203 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000204 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000205
206 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000207 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000208
209 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000210 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000211
212 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000213 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000214
215 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000216 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000217
218 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000219 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000220
Fred Drakeff9ea482000-04-19 13:54:15 +0000221 if (v != 0)
222 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000223 }
224 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000225}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000226
227
Fred Drakec2683dd2001-07-17 19:32:05 +0000228/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000229 *
230 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
231 * This really just wraps a call to parser_compare_nodes() with some easy
232 * checks and protection code.
233 *
234 */
235static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000236parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000237{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000238 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000239 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000240
241 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000242 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000243
Fred Drakec2683dd2001-07-17 19:32:05 +0000244 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000245}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000246
247
Fred Drakec2683dd2001-07-17 19:32:05 +0000248/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000249 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000250 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000251 * 'wrapper' object that holds a node* and allows it to be passed around in
252 * Python code.
253 *
254 */
255static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000256parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000257{
Fred Drakec2683dd2001-07-17 19:32:05 +0000258 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000259
260 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000261 o->st_node = st;
262 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000263 }
Fred Drake268397f1998-04-29 14:16:32 +0000264 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000265 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000266 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000267 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000268}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000269
270
Fred Drakec2683dd2001-07-17 19:32:05 +0000271/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000272 *
273 * This is called by a del statement that reduces the reference count to 0.
274 *
275 */
276static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000277parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000278{
Fred Drakec2683dd2001-07-17 19:32:05 +0000279 PyNode_Free(st->st_node);
280 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000281}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000282
283
Fred Drakec2683dd2001-07-17 19:32:05 +0000284/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000285 *
286 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000287 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000288 *
289 */
290static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000291parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000292{
Guido van Rossum47478871996-08-21 14:32:37 +0000293 PyObject *line_option = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000294 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000295 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000296 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000297
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000298 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000299
Fred Drake268397f1998-04-29 14:16:32 +0000300 if (self == NULL) {
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000301 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
302 &PyST_Type, &self, &line_option,
303 &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000304 }
Fred Drake503d8d61998-04-13 18:45:18 +0000305 else
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000306 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
307 &line_option, &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000308 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000309 int lineno = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000310 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000311 if (line_option != NULL) {
312 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
313 }
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000314 if (col_option != NULL) {
315 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
316 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000317 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000318 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000319 * since it's known to work already.
320 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000321 res = node2tuple(((PyST_Object*)self)->st_node,
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000322 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000323 }
324 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000325}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000326
Georg Brandlf9efabb2008-07-23 15:16:45 +0000327static PyObject*
328parser_ast2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
329{
330 if (PyErr_WarnPy3k("ast2tuple is removed in 3.x; use st2tuple", 1) < 0)
331 return NULL;
332 return parser_st2tuple(self, args, kw);
333}
334
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000335
Fred Drakec2683dd2001-07-17 19:32:05 +0000336/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000337 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000338 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000339 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000340 *
341 */
342static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000343parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000344{
Guido van Rossum47478871996-08-21 14:32:37 +0000345 PyObject *line_option = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000346 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000347 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000348 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000349
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000350 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000351
Fred Drake503d8d61998-04-13 18:45:18 +0000352 if (self == NULL)
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000353 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
354 &PyST_Type, &self, &line_option,
355 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000356 else
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000357 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
358 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000359 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000360 int lineno = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000361 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000362 if (line_option != 0) {
363 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
364 }
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000365 if (col_option != NULL) {
366 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
367 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000368 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000369 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000370 * since it's known to work already.
371 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000372 res = node2tuple(self->st_node,
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000373 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000374 }
375 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000376}
Guido van Rossum47478871996-08-21 14:32:37 +0000377
Georg Brandlf9efabb2008-07-23 15:16:45 +0000378static PyObject*
379parser_ast2list(PyST_Object *self, PyObject *args, PyObject *kw)
380{
381 if (PyErr_WarnPy3k("ast2list is removed in 3.x; use st2list", 1) < 0)
382 return NULL;
383 return parser_st2list(self, args, kw);
384}
385
Guido van Rossum47478871996-08-21 14:32:37 +0000386
Fred Drakec2683dd2001-07-17 19:32:05 +0000387/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000388 *
389 * This function creates code objects from the parse tree represented by
390 * the passed-in data object. An optional file name is passed in as well.
391 *
392 */
393static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000394parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000395{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000396 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000397 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000398 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000399
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000400 static char *keywords[] = {"ast", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000401
Fred Drake503d8d61998-04-13 18:45:18 +0000402 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000403 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
404 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000405 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000406 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000407 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000408
409 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000410 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000411
412 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000413}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000414
Georg Brandlf9efabb2008-07-23 15:16:45 +0000415static PyObject*
416parser_compileast(PyST_Object *self, PyObject *args, PyObject *kw)
417{
418 if (PyErr_WarnPy3k("compileast is removed in 3.x; use compilest", 1) < 0)
419 return NULL;
420 return parser_compilest(self, args, kw);
421}
422
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000423
424/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
425 * PyObject* parser_issuite(PyObject* self, PyObject* args)
426 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000427 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000428 * a statement suite, respectively. The return is a Python truth value.
429 *
430 */
431static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000432parser_isexpr(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!:isexpr", 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, ":isexpr", &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_True : Py_False;
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
454static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000455parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000456{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000457 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000458 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000459
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000460 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000461
Fred Drake503d8d61998-04-13 18:45:18 +0000462 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000463 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000464 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000465 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000466 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000467
468 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000469 /* Check to see if the ST represents an expression or not. */
470 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000471 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000472 }
473 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000474}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000475
476
Fred Drake7a15ba51999-09-09 14:21:52 +0000477#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
478
Fred Drake503d8d61998-04-13 18:45:18 +0000479static PyMethodDef
480parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000481 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000482 PyDoc_STR("Compile this ST object into a code object.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000483 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000484 PyDoc_STR("Determines if this ST object was created from an expression.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000485 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000486 PyDoc_STR("Determines if this ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000487 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000488 PyDoc_STR("Creates a list-tree representation of this ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000489 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000490 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Fred Drake503d8d61998-04-13 18:45:18 +0000491
Fred Drake268397f1998-04-29 14:16:32 +0000492 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000493};
494
Fred Drake503d8d61998-04-13 18:45:18 +0000495
496static PyObject*
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000497parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000498{
Fred Drake503d8d61998-04-13 18:45:18 +0000499 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000500}
Fred Drake503d8d61998-04-13 18:45:18 +0000501
502
Guido van Rossum3d602e31996-07-21 02:33:56 +0000503/* err_string(char* message)
504 *
505 * Sets the error string for an exception of type ParserError.
506 *
507 */
508static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000509err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000510{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000511 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000512}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000513
514
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000515/* PyObject* parser_do_parse(PyObject* args, int type)
516 *
517 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000518 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000519 *
520 */
521static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000522parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000523{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000524 char* string = 0;
525 PyObject* res = 0;
526
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000527 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000528
529 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000530 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000531 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000532 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000533
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000534 if (n)
535 res = parser_newstobject(n, type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000536 }
537 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000538}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000539
540
541/* PyObject* parser_expr(PyObject* self, PyObject* args)
542 * PyObject* parser_suite(PyObject* self, PyObject* args)
543 *
544 * External interfaces to the parser itself. Which is called determines if
545 * the parser attempts to recognize an expression ('eval' form) or statement
546 * suite ('exec' form). The real work is done by parser_do_parse() above.
547 *
548 */
549static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000550parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000551{
Fred Drake268397f1998-04-29 14:16:32 +0000552 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000553 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000554}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000555
556
557static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000558parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000559{
Fred Drake268397f1998-04-29 14:16:32 +0000560 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000561 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000562}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000563
564
565
Fred Drakec2683dd2001-07-17 19:32:05 +0000566/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000567 * object requires that the input tuple be valid without having to rely on
568 * catching an exception from the compiler. This is done to allow the
569 * compiler itself to remain fast, since most of its input will come from
570 * the parser directly, and therefore be known to be syntactically correct.
571 * This validation is done to ensure that we don't core dump the compile
572 * phase, returning an exception instead.
573 *
574 * Two aspects can be broken out in this code: creating a node tree from
575 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000576 * advantageous to expand the number of ST types to include funcdefs and
577 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000578 * here. They are not necessary, and not quite as useful in a raw form.
579 * For now, let's get expressions and suites working reliably.
580 */
581
582
Jeremy Hylton938ace62002-07-17 16:30:39 +0000583static node* build_node_tree(PyObject *tuple);
584static int validate_expr_tree(node *tree);
585static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000586static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000587
Fred Drakec2683dd2001-07-17 19:32:05 +0000588/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000589 *
590 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000591 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000592 * tuple can be validated. It does this by checking the first code of the
593 * tuple, and, if acceptable, builds the internal representation. If this
594 * step succeeds, the internal representation is validated as fully as
595 * possible with the various validate_*() routines defined below.
596 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000597 * This function must be changed if support is to be added for PyST_FRAGMENT
598 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000599 *
600 */
601static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000602parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000603{
Fred Drake268397f1998-04-29 14:16:32 +0000604 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000605 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000606 PyObject *tuple;
607 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000608
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000609 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000610
Fred Drakec2683dd2001-07-17 19:32:05 +0000611 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000612 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000613 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000614 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000615 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000616 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000617 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000618 }
619 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000620 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000621 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000622 tree = build_node_tree(tuple);
623 if (tree != 0) {
624 int start_sym = TYPE(tree);
625 if (start_sym == eval_input) {
626 /* Might be an eval form. */
627 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000628 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000629 else
630 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000631 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000632 else if (start_sym == file_input) {
633 /* This looks like an exec form so far. */
634 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000635 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000636 else
637 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000638 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000639 else if (start_sym == encoding_decl) {
640 /* This looks like an encoding_decl so far. */
641 if (validate_encoding_decl(tree))
642 st = parser_newstobject(tree, PyST_SUITE);
643 else
644 PyNode_Free(tree);
645 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000646 else {
647 /* This is a fragment, at best. */
648 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000649 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000650 }
Guido van Rossum47478871996-08-21 14:32:37 +0000651 }
Guido van Rossum47478871996-08-21 14:32:37 +0000652 /* Make sure we throw an exception on all errors. We should never
653 * get this, but we'd do well to be sure something is done.
654 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000655 if (st == NULL && !PyErr_Occurred())
656 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000657
Fred Drakec2683dd2001-07-17 19:32:05 +0000658 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000659}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000660
Georg Brandlf9efabb2008-07-23 15:16:45 +0000661static PyObject*
662parser_tuple2ast(PyST_Object *self, PyObject *args, PyObject *kw)
663{
664 if (PyErr_WarnPy3k("tuple2ast is removed in 3.x; use tuple2st", 1) < 0)
665 return NULL;
666 return parser_tuple2st(self, args, kw);
667}
668
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000669
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000670/* node* build_node_children()
671 *
672 * Iterate across the children of the current non-terminal node and build
673 * their structures. If successful, return the root of this portion of
674 * the tree, otherwise, 0. Any required exception will be specified already,
675 * and no memory will have been deallocated.
676 *
677 */
678static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000679build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000680{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000681 Py_ssize_t len = PyObject_Size(tuple);
682 Py_ssize_t i;
683 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000684
685 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000686 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000687 PyObject* elem = PySequence_GetItem(tuple, i);
688 int ok = elem != NULL;
689 long type = 0;
690 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000691
Fred Drakeff9ea482000-04-19 13:54:15 +0000692 if (ok)
693 ok = PySequence_Check(elem);
694 if (ok) {
695 PyObject *temp = PySequence_GetItem(elem, 0);
696 if (temp == NULL)
697 ok = 0;
698 else {
699 ok = PyInt_Check(temp);
700 if (ok)
701 type = PyInt_AS_LONG(temp);
702 Py_DECREF(temp);
703 }
704 }
705 if (!ok) {
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000706 PyObject *err = Py_BuildValue("os", elem,
707 "Illegal node construct.");
708 PyErr_SetObject(parser_error, err);
709 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000710 Py_XDECREF(elem);
711 return (0);
712 }
713 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000714 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000715 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000716
Fred Drake0ac9b072000-09-12 21:58:06 +0000717 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000718 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000719 return 0;
720 }
721 temp = PySequence_GetItem(elem, 1);
722 if (temp == NULL)
723 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000724 if (!PyString_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000725 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000726 "second item in terminal node must be a string,"
727 " found %s",
Christian Heimese93237d2007-12-19 02:37:44 +0000728 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000729 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000730 return 0;
731 }
732 if (len == 3) {
733 PyObject *o = PySequence_GetItem(elem, 2);
734 if (o != NULL) {
735 if (PyInt_Check(o))
736 *line_num = PyInt_AS_LONG(o);
737 else {
738 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000739 "third item in terminal node must be an"
740 " integer, found %s",
Christian Heimese93237d2007-12-19 02:37:44 +0000741 Py_TYPE(temp)->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000742 Py_DECREF(o);
743 Py_DECREF(temp);
744 return 0;
745 }
746 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000747 }
748 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000749 len = PyString_GET_SIZE(temp) + 1;
Tim Petersc9d78aa2006-03-26 23:27:58 +0000750 strn = (char *)PyObject_MALLOC(len);
Fred Drake0ac9b072000-09-12 21:58:06 +0000751 if (strn != NULL)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000752 (void) memcpy(strn, PyString_AS_STRING(temp), len);
Fred Drake0ac9b072000-09-12 21:58:06 +0000753 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000754 }
755 else if (!ISNONTERMINAL(type)) {
756 /*
757 * It has to be one or the other; this is an error.
758 * Throw an exception.
759 */
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000760 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
761 PyErr_SetObject(parser_error, err);
762 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000763 Py_XDECREF(elem);
764 return (0);
765 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000766 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000767 if (err == E_NOMEM) {
Tim Petersc9d78aa2006-03-26 23:27:58 +0000768 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000769 return (node *) PyErr_NoMemory();
770 }
771 if (err == E_OVERFLOW) {
Tim Petersc9d78aa2006-03-26 23:27:58 +0000772 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000773 PyErr_SetString(PyExc_ValueError,
774 "unsupported number of child nodes");
775 return NULL;
776 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000777
Fred Drakeff9ea482000-04-19 13:54:15 +0000778 if (ISNONTERMINAL(type)) {
779 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000780
Fred Drakeff9ea482000-04-19 13:54:15 +0000781 if (new_child != build_node_children(elem, new_child, line_num)) {
782 Py_XDECREF(elem);
783 return (0);
784 }
785 }
786 else if (type == NEWLINE) { /* It's true: we increment the */
787 ++(*line_num); /* line number *after* the newline! */
788 }
789 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000790 }
Tim Petersc9d78aa2006-03-26 23:27:58 +0000791 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000792}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000793
794
795static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000796build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000797{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000798 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000799 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000800 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000801
Guido van Rossum47478871996-08-21 14:32:37 +0000802 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000803 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000804 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000805 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000806 /*
807 * The tuple is simple, but it doesn't start with a start symbol.
808 * Throw an exception now and be done with it.
809 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000810 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000811 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000812 PyErr_SetObject(parser_error, tuple);
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000813 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000814 }
815 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000816 /*
817 * Not efficient, but that can be handled later.
818 */
819 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000820 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000821
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000822 if (num == encoding_decl) {
823 encoding = PySequence_GetItem(tuple, 2);
824 /* tuple isn't borrowed anymore here, need to DECREF */
825 tuple = PySequence_GetSlice(tuple, 0, 2);
826 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000827 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000828 if (res != NULL) {
829 if (res != build_node_children(tuple, res, &line_num)) {
830 PyNode_Free(res);
831 res = NULL;
832 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000833 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000834 Py_ssize_t len;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000835 len = PyString_GET_SIZE(encoding) + 1;
Tim Petersc9d78aa2006-03-26 23:27:58 +0000836 res->n_str = (char *)PyObject_MALLOC(len);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000837 if (res->n_str != NULL)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000838 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000839 Py_DECREF(encoding);
840 Py_DECREF(tuple);
841 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000842 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000843 }
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000844 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000845 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000846 * NONTERMINAL, we can't use it. Not sure the implementation
847 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000848 */
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000849 PyObject *err = Py_BuildValue("os", tuple,
850 "Illegal component tuple.");
851 PyErr_SetObject(parser_error, err);
852 Py_XDECREF(err);
853 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000854
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000855 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000856}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000857
858
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000859/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000860 * Validation routines used within the validation section:
861 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000862static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000863
Fred Drakeff9ea482000-04-19 13:54:15 +0000864#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
865#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
866#define validate_colon(ch) validate_terminal(ch, COLON, ":")
867#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
868#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
869#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
870#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
871#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
872#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
873#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
874#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
875#define validate_star(ch) validate_terminal(ch, STAR, "*")
876#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
877#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
878#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000879#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000880#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000881
Fred Drake0ac9b072000-09-12 21:58:06 +0000882#define VALIDATER(n) static int validate_##n(node *tree)
883
Fred Drakeff9ea482000-04-19 13:54:15 +0000884VALIDATER(node); VALIDATER(small_stmt);
885VALIDATER(class); VALIDATER(node);
886VALIDATER(parameters); VALIDATER(suite);
887VALIDATER(testlist); VALIDATER(varargslist);
888VALIDATER(fpdef); VALIDATER(fplist);
889VALIDATER(stmt); VALIDATER(simple_stmt);
890VALIDATER(expr_stmt); VALIDATER(power);
891VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000892VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000893VALIDATER(raise_stmt); VALIDATER(import_stmt);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000894VALIDATER(import_name); VALIDATER(import_from);
Fred Drakecff283c2000-08-21 22:24:43 +0000895VALIDATER(global_stmt); VALIDATER(list_if);
896VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000897VALIDATER(exec_stmt); VALIDATER(compound_stmt);
898VALIDATER(while); VALIDATER(for);
899VALIDATER(try); VALIDATER(except_clause);
900VALIDATER(test); VALIDATER(and_test);
901VALIDATER(not_test); VALIDATER(comparison);
902VALIDATER(comp_op); VALIDATER(expr);
903VALIDATER(xor_expr); VALIDATER(and_expr);
904VALIDATER(shift_expr); VALIDATER(arith_expr);
905VALIDATER(term); VALIDATER(factor);
906VALIDATER(atom); VALIDATER(lambdef);
907VALIDATER(trailer); VALIDATER(subscript);
908VALIDATER(subscriptlist); VALIDATER(sliceop);
909VALIDATER(exprlist); VALIDATER(dictmaker);
910VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000911VALIDATER(listmaker); VALIDATER(yield_stmt);
Raymond Hettinger354433a2004-05-19 08:20:33 +0000912VALIDATER(testlist1); VALIDATER(gen_for);
913VALIDATER(gen_iter); VALIDATER(gen_if);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000914VALIDATER(testlist_gexp); VALIDATER(yield_expr);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +0000915VALIDATER(yield_or_testlist); VALIDATER(or_test);
916VALIDATER(old_test); VALIDATER(old_lambdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000917
Fred Drake0ac9b072000-09-12 21:58:06 +0000918#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000919
Fred Drakeff9ea482000-04-19 13:54:15 +0000920#define is_even(n) (((n) & 1) == 0)
921#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000922
923
924static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000925validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000926{
Fred Drake0ac9b072000-09-12 21:58:06 +0000927 if (TYPE(n) != t) {
928 PyErr_Format(parser_error, "Expected node type %d, got %d.",
929 t, TYPE(n));
930 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000931 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000932 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000933}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000934
935
Fred Drakee7ab64e2000-04-25 04:14:46 +0000936/* Verifies that the number of child nodes is exactly 'num', raising
937 * an exception if it isn't. The exception message does not indicate
938 * the exact number of nodes, allowing this to be used to raise the
939 * "right" exception when the wrong number of nodes is present in a
940 * specific variant of a statement's syntax. This is commonly used
941 * in that fashion.
942 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000943static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000944validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000945{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000946 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000947 PyErr_Format(parser_error,
948 "Illegal number of children for %s node.", name);
949 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000950 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000951 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000952}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000953
954
955static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000956validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000957{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000958 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000959 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000960
961 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000962 PyErr_Format(parser_error,
963 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000964 }
965 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000966}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000967
968
Guido van Rossum47478871996-08-21 14:32:37 +0000969/* X (',' X) [',']
970 */
971static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000972validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000973 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000974{
975 int nch = NCH(tree);
976 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000977 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000978
979 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000980 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000981 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000982 if (is_even(nch))
983 res = validate_comma(CHILD(tree, --nch));
984 if (res && nch > 1) {
985 int pos = 1;
986 for ( ; res && pos < nch; pos += 2)
987 res = (validate_comma(CHILD(tree, pos))
988 && vfunc(CHILD(tree, pos + 1)));
989 }
Guido van Rossum47478871996-08-21 14:32:37 +0000990 }
991 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000992}
Guido van Rossum47478871996-08-21 14:32:37 +0000993
994
Fred Drakecff283c2000-08-21 22:24:43 +0000995/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000996 *
997 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000998 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000999 */
Guido van Rossum47478871996-08-21 14:32:37 +00001000static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001001validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001002{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001003 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +00001004 int res = (validate_ntype(tree, classdef) &&
1005 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001006
Guido van Rossum3d602e31996-07-21 02:33:56 +00001007 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001008 res = (validate_name(CHILD(tree, 0), "class")
1009 && validate_ntype(CHILD(tree, 1), NAME)
1010 && validate_colon(CHILD(tree, nch - 2))
1011 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001012 }
Brett Cannonf4189912005-04-09 02:30:16 +00001013 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001014 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +00001015 }
1016
1017 if (res) {
1018 if (nch == 7) {
1019 res = ((validate_lparen(CHILD(tree, 2)) &&
1020 validate_testlist(CHILD(tree, 3)) &&
1021 validate_rparen(CHILD(tree, 4))));
1022 }
1023 else if (nch == 6) {
1024 res = (validate_lparen(CHILD(tree,2)) &&
1025 validate_rparen(CHILD(tree,3)));
1026 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001027 }
1028 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001029}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001030
1031
Guido van Rossum3d602e31996-07-21 02:33:56 +00001032/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001033 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001034 */
Guido van Rossum47478871996-08-21 14:32:37 +00001035static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001036validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001037{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001038 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001039 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001040 && (nch >= 4)
1041 && validate_name(CHILD(tree, 0), "if")
1042 && validate_test(CHILD(tree, 1))
1043 && validate_colon(CHILD(tree, 2))
1044 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001045
1046 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001047 /* ... 'else' ':' suite */
1048 res = (validate_name(CHILD(tree, nch - 3), "else")
1049 && validate_colon(CHILD(tree, nch - 2))
1050 && validate_suite(CHILD(tree, nch - 1)));
1051 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001052 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001053 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001054 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001055 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001056 /* Will catch the case for nch < 4 */
1057 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001058 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001059 /* ... ('elif' test ':' suite)+ ... */
1060 int j = 4;
1061 while ((j < nch) && res) {
1062 res = (validate_name(CHILD(tree, j), "elif")
1063 && validate_colon(CHILD(tree, j + 2))
1064 && validate_test(CHILD(tree, j + 1))
1065 && validate_suite(CHILD(tree, j + 3)));
1066 j += 4;
1067 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001068 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001069 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001070}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001071
1072
Guido van Rossum3d602e31996-07-21 02:33:56 +00001073/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001074 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001075 *
1076 */
Guido van Rossum47478871996-08-21 14:32:37 +00001077static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001078validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001079{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001080 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001081 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001082
Guido van Rossum3d602e31996-07-21 02:33:56 +00001083 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001084 res = (validate_lparen(CHILD(tree, 0))
1085 && validate_rparen(CHILD(tree, nch - 1)));
1086 if (res && (nch == 3))
1087 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001088 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001089 else {
1090 (void) validate_numnodes(tree, 2, "parameters");
1091 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001092 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001093}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001094
1095
Fred Drakecff283c2000-08-21 22:24:43 +00001096/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001097 *
1098 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001099 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001100 * | NEWLINE INDENT stmt+ DEDENT
1101 */
Guido van Rossum47478871996-08-21 14:32:37 +00001102static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001103validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001104{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001105 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001106 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001107
Guido van Rossum3d602e31996-07-21 02:33:56 +00001108 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001109 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001110 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001111 /* NEWLINE INDENT stmt+ DEDENT */
1112 res = (validate_newline(CHILD(tree, 0))
1113 && validate_indent(CHILD(tree, 1))
1114 && validate_stmt(CHILD(tree, 2))
1115 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001116
Fred Drakeff9ea482000-04-19 13:54:15 +00001117 if (res && (nch > 4)) {
1118 int i = 3;
1119 --nch; /* forget the DEDENT */
1120 for ( ; res && (i < nch); ++i)
1121 res = validate_stmt(CHILD(tree, i));
1122 }
1123 else if (nch < 4)
1124 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001125 }
1126 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001127}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001128
1129
Guido van Rossum47478871996-08-21 14:32:37 +00001130static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001131validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001132{
Guido van Rossum47478871996-08-21 14:32:37 +00001133 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001134 validate_test, "testlist"));
1135}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001136
1137
Guido van Rossum1c917072001-10-15 15:44:05 +00001138static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001139validate_testlist1(node *tree)
1140{
1141 return (validate_repeating_list(tree, testlist1,
1142 validate_test, "testlist1"));
1143}
1144
1145
1146static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001147validate_testlist_safe(node *tree)
1148{
1149 return (validate_repeating_list(tree, testlist_safe,
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001150 validate_old_test, "testlist_safe"));
Guido van Rossum1c917072001-10-15 15:44:05 +00001151}
1152
1153
Fred Drakecff283c2000-08-21 22:24:43 +00001154/* '*' NAME [',' '**' NAME] | '**' NAME
1155 */
1156static int
1157validate_varargslist_trailer(node *tree, int start)
1158{
1159 int nch = NCH(tree);
1160 int res = 0;
1161 int sym;
1162
1163 if (nch <= start) {
1164 err_string("expected variable argument trailer for varargslist");
1165 return 0;
1166 }
1167 sym = TYPE(CHILD(tree, start));
1168 if (sym == STAR) {
1169 /*
1170 * ('*' NAME [',' '**' NAME]
1171 */
1172 if (nch-start == 2)
1173 res = validate_name(CHILD(tree, start+1), NULL);
1174 else if (nch-start == 5)
1175 res = (validate_name(CHILD(tree, start+1), NULL)
1176 && validate_comma(CHILD(tree, start+2))
1177 && validate_doublestar(CHILD(tree, start+3))
1178 && validate_name(CHILD(tree, start+4), NULL));
1179 }
1180 else if (sym == DOUBLESTAR) {
1181 /*
1182 * '**' NAME
1183 */
1184 if (nch-start == 2)
1185 res = validate_name(CHILD(tree, start+1), NULL);
1186 }
1187 if (!res)
1188 err_string("illegal variable argument trailer for varargslist");
1189 return res;
1190}
1191
1192
1193/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001194 *
1195 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001196 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001197 * ('*' NAME [',' '**' NAME]
1198 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001199 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1200 *
1201 */
Guido van Rossum47478871996-08-21 14:32:37 +00001202static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001203validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001204{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001205 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001206 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001207 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001208
Fred Drakeb6429a22000-12-11 22:08:27 +00001209 if (!res)
1210 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001211 if (nch < 1) {
1212 err_string("varargslist missing child nodes");
1213 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001214 }
Fred Drakecff283c2000-08-21 22:24:43 +00001215 sym = TYPE(CHILD(tree, 0));
1216 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001217 /* whole thing matches:
1218 * '*' NAME [',' '**' NAME] | '**' NAME
1219 */
Fred Drakecff283c2000-08-21 22:24:43 +00001220 res = validate_varargslist_trailer(tree, 0);
1221 else if (sym == fpdef) {
1222 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001223
Fred Drakecff283c2000-08-21 22:24:43 +00001224 sym = TYPE(CHILD(tree, nch-1));
1225 if (sym == NAME) {
1226 /*
1227 * (fpdef ['=' test] ',')+
1228 * ('*' NAME [',' '**' NAME]
1229 * | '**' NAME)
1230 */
1231 /* skip over (fpdef ['=' test] ',')+ */
1232 while (res && (i+2 <= nch)) {
1233 res = validate_fpdef(CHILD(tree, i));
1234 ++i;
1235 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1236 res = (validate_equal(CHILD(tree, i))
1237 && validate_test(CHILD(tree, i+1)));
1238 if (res)
1239 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001240 }
Fred Drakecff283c2000-08-21 22:24:43 +00001241 if (res && i < nch) {
1242 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001243 ++i;
1244 if (res && i < nch
1245 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1246 || TYPE(CHILD(tree, i)) == STAR))
1247 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001248 }
1249 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001250 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1251 * i --^^^
1252 */
Fred Drakecff283c2000-08-21 22:24:43 +00001253 if (res)
1254 res = validate_varargslist_trailer(tree, i);
1255 }
1256 else {
1257 /*
1258 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1259 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001260 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001261 if (sym == COMMA) {
1262 res = validate_comma(CHILD(tree, nch-1));
1263 if (!res)
1264 return 0;
1265 --nch;
1266 }
1267 /*
1268 * fpdef ['=' test] (',' fpdef ['=' test])*
1269 */
1270 res = validate_fpdef(CHILD(tree, 0));
1271 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001272 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1273 res = (validate_equal(CHILD(tree, i))
1274 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001275 i += 2;
1276 }
1277 /*
1278 * ... (',' fpdef ['=' test])*
1279 * i ---^^^
1280 */
1281 while (res && (nch - i) >= 2) {
1282 res = (validate_comma(CHILD(tree, i))
1283 && validate_fpdef(CHILD(tree, i+1)));
1284 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001285 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1286 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001287 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001288 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001289 }
1290 }
1291 if (res && nch - i != 0) {
1292 res = 0;
1293 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001294 }
1295 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001296 }
Fred Drakecff283c2000-08-21 22:24:43 +00001297 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001298}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001299
1300
Fred Drakecff283c2000-08-21 22:24:43 +00001301/* list_iter: list_for | list_if
1302 */
1303static int
1304validate_list_iter(node *tree)
1305{
1306 int res = (validate_ntype(tree, list_iter)
1307 && validate_numnodes(tree, 1, "list_iter"));
1308 if (res && TYPE(CHILD(tree, 0)) == list_for)
1309 res = validate_list_for(CHILD(tree, 0));
1310 else
1311 res = validate_list_if(CHILD(tree, 0));
1312
1313 return res;
1314}
1315
Raymond Hettinger354433a2004-05-19 08:20:33 +00001316/* gen_iter: gen_for | gen_if
1317 */
1318static int
1319validate_gen_iter(node *tree)
1320{
1321 int res = (validate_ntype(tree, gen_iter)
1322 && validate_numnodes(tree, 1, "gen_iter"));
1323 if (res && TYPE(CHILD(tree, 0)) == gen_for)
1324 res = validate_gen_for(CHILD(tree, 0));
1325 else
1326 res = validate_gen_if(CHILD(tree, 0));
1327
1328 return res;
1329}
1330
Fred Drakecff283c2000-08-21 22:24:43 +00001331/* list_for: 'for' exprlist 'in' testlist [list_iter]
1332 */
1333static int
1334validate_list_for(node *tree)
1335{
1336 int nch = NCH(tree);
1337 int res;
1338
1339 if (nch == 5)
1340 res = validate_list_iter(CHILD(tree, 4));
1341 else
1342 res = validate_numnodes(tree, 4, "list_for");
1343
1344 if (res)
1345 res = (validate_name(CHILD(tree, 0), "for")
1346 && validate_exprlist(CHILD(tree, 1))
1347 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001348 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001349
1350 return res;
1351}
1352
Raymond Hettinger354433a2004-05-19 08:20:33 +00001353/* gen_for: 'for' exprlist 'in' test [gen_iter]
1354 */
1355static int
1356validate_gen_for(node *tree)
1357{
1358 int nch = NCH(tree);
1359 int res;
1360
1361 if (nch == 5)
1362 res = validate_gen_iter(CHILD(tree, 4));
1363 else
1364 res = validate_numnodes(tree, 4, "gen_for");
1365
1366 if (res)
1367 res = (validate_name(CHILD(tree, 0), "for")
1368 && validate_exprlist(CHILD(tree, 1))
1369 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001370 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001371
1372 return res;
1373}
1374
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001375/* list_if: 'if' old_test [list_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001376 */
1377static int
1378validate_list_if(node *tree)
1379{
1380 int nch = NCH(tree);
1381 int res;
1382
1383 if (nch == 3)
1384 res = validate_list_iter(CHILD(tree, 2));
1385 else
1386 res = validate_numnodes(tree, 2, "list_if");
1387
1388 if (res)
1389 res = (validate_name(CHILD(tree, 0), "if")
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001390 && validate_old_test(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001391
1392 return res;
1393}
1394
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001395/* gen_if: 'if' old_test [gen_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001396 */
1397static int
1398validate_gen_if(node *tree)
1399{
1400 int nch = NCH(tree);
1401 int res;
1402
1403 if (nch == 3)
1404 res = validate_gen_iter(CHILD(tree, 2));
1405 else
1406 res = validate_numnodes(tree, 2, "gen_if");
1407
1408 if (res)
1409 res = (validate_name(CHILD(tree, 0), "if")
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001410 && validate_old_test(CHILD(tree, 1)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001411
1412 return res;
1413}
Fred Drakecff283c2000-08-21 22:24:43 +00001414
1415/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001416 *
1417 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001418 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001419 * | '(' fplist ')'
1420 */
Guido van Rossum47478871996-08-21 14:32:37 +00001421static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001422validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001423{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001424 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001425 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001426
Guido van Rossum3d602e31996-07-21 02:33:56 +00001427 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001428 if (nch == 1)
1429 res = validate_ntype(CHILD(tree, 0), NAME);
1430 else if (nch == 3)
1431 res = (validate_lparen(CHILD(tree, 0))
1432 && validate_fplist(CHILD(tree, 1))
1433 && validate_rparen(CHILD(tree, 2)));
1434 else
1435 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001436 }
1437 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001438}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001439
1440
Guido van Rossum47478871996-08-21 14:32:37 +00001441static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001442validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001443{
Guido van Rossum47478871996-08-21 14:32:37 +00001444 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001445 validate_fpdef, "fplist"));
1446}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001447
1448
Guido van Rossum3d602e31996-07-21 02:33:56 +00001449/* simple_stmt | compound_stmt
1450 *
1451 */
Guido van Rossum47478871996-08-21 14:32:37 +00001452static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001453validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001454{
1455 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001456 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001457
Guido van Rossum3d602e31996-07-21 02:33:56 +00001458 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001459 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001460
Fred Drakeff9ea482000-04-19 13:54:15 +00001461 if (TYPE(tree) == simple_stmt)
1462 res = validate_simple_stmt(tree);
1463 else
1464 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001465 }
1466 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001467}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001468
1469
Guido van Rossum3d602e31996-07-21 02:33:56 +00001470/* small_stmt (';' small_stmt)* [';'] NEWLINE
1471 *
1472 */
Guido van Rossum47478871996-08-21 14:32:37 +00001473static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001474validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001475{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001476 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001477 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001478 && (nch >= 2)
1479 && validate_small_stmt(CHILD(tree, 0))
1480 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001481
Guido van Rossum3d602e31996-07-21 02:33:56 +00001482 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001483 res = validate_numnodes(tree, 2, "simple_stmt");
1484 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001485 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001486 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001487 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001488 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001489
Fred Drakeff9ea482000-04-19 13:54:15 +00001490 for (i = 1; res && (i < nch); i += 2)
1491 res = (validate_semi(CHILD(tree, i))
1492 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001493 }
1494 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001495}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001496
1497
Guido van Rossum47478871996-08-21 14:32:37 +00001498static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001499validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001500{
1501 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001502 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001503
Fred Drake0ac9b072000-09-12 21:58:06 +00001504 if (res) {
1505 int ntype = TYPE(CHILD(tree, 0));
1506
1507 if ( (ntype == expr_stmt)
1508 || (ntype == print_stmt)
1509 || (ntype == del_stmt)
1510 || (ntype == pass_stmt)
1511 || (ntype == flow_stmt)
1512 || (ntype == import_stmt)
1513 || (ntype == global_stmt)
1514 || (ntype == assert_stmt)
1515 || (ntype == exec_stmt))
1516 res = validate_node(CHILD(tree, 0));
1517 else {
1518 res = 0;
1519 err_string("illegal small_stmt child type");
1520 }
1521 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001522 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001523 res = 0;
1524 PyErr_Format(parser_error,
1525 "Unrecognized child node of small_stmt: %d.",
1526 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001527 }
1528 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001529}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001530
1531
1532/* compound_stmt:
Christian Heimes5224d282008-02-23 15:01:05 +00001533 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef | decorated
Guido van Rossum3d602e31996-07-21 02:33:56 +00001534 */
Guido van Rossum47478871996-08-21 14:32:37 +00001535static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001536validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001537{
1538 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001539 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001540 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001541
1542 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001543 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001544
1545 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001546 ntype = TYPE(tree);
1547 if ( (ntype == if_stmt)
1548 || (ntype == while_stmt)
1549 || (ntype == for_stmt)
1550 || (ntype == try_stmt)
1551 || (ntype == funcdef)
Christian Heimes5224d282008-02-23 15:01:05 +00001552 || (ntype == classdef)
1553 || (ntype == decorated))
Fred Drakeff9ea482000-04-19 13:54:15 +00001554 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001555 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001556 res = 0;
1557 PyErr_Format(parser_error,
1558 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001559 }
1560 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001561}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001562
Guido van Rossum47478871996-08-21 14:32:37 +00001563static int
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001564validate_yield_or_testlist(node *tree)
1565{
1566 if (TYPE(tree) == yield_expr)
1567 return validate_yield_expr(tree);
1568 else
1569 return validate_testlist(tree);
1570}
1571
1572static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001573validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001574{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001575 int j;
1576 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001577 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001578 && is_odd(nch)
1579 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001580
Fred Drake28f739a2000-08-25 22:42:40 +00001581 if (res && nch == 3
1582 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001583 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1584 && validate_yield_or_testlist(CHILD(tree, 2));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001585
Fred Drake28f739a2000-08-25 22:42:40 +00001586 if (res) {
1587 char *s = STR(CHILD(CHILD(tree, 1), 0));
1588
1589 res = (strcmp(s, "+=") == 0
1590 || strcmp(s, "-=") == 0
1591 || strcmp(s, "*=") == 0
1592 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001593 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001594 || strcmp(s, "%=") == 0
1595 || strcmp(s, "&=") == 0
1596 || strcmp(s, "|=") == 0
1597 || strcmp(s, "^=") == 0
1598 || strcmp(s, "<<=") == 0
1599 || strcmp(s, ">>=") == 0
1600 || strcmp(s, "**=") == 0);
1601 if (!res)
1602 err_string("illegal augmmented assignment operator");
1603 }
1604 }
1605 else {
1606 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001607 res = validate_equal(CHILD(tree, j))
1608 && validate_yield_or_testlist(CHILD(tree, j + 1));
Fred Drake28f739a2000-08-25 22:42:40 +00001609 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001610 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001611}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001612
1613
Guido van Rossum3d602e31996-07-21 02:33:56 +00001614/* print_stmt:
1615 *
Fred Drakecff283c2000-08-21 22:24:43 +00001616 * 'print' ( [ test (',' test)* [','] ]
1617 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001618 */
Guido van Rossum47478871996-08-21 14:32:37 +00001619static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001620validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001621{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001622 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001623 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001624 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001625 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001626
Fred Drakecff283c2000-08-21 22:24:43 +00001627 if (res && nch > 1) {
1628 int sym = TYPE(CHILD(tree, 1));
1629 int i = 1;
1630 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001631
Fred Drakecff283c2000-08-21 22:24:43 +00001632 if (sym == test)
1633 res = validate_test(CHILD(tree, i++));
1634 else {
1635 if (nch < 3)
1636 res = validate_numnodes(tree, 3, "print_stmt");
1637 else {
1638 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1639 && validate_test(CHILD(tree, i+1)));
1640 i += 2;
1641 allow_trailing_comma = 0;
1642 }
1643 }
1644 if (res) {
1645 /* ... (',' test)* [','] */
1646 while (res && i+2 <= nch) {
1647 res = (validate_comma(CHILD(tree, i))
1648 && validate_test(CHILD(tree, i+1)));
1649 allow_trailing_comma = 1;
1650 i += 2;
1651 }
1652 if (res && !allow_trailing_comma)
1653 res = validate_numnodes(tree, i, "print_stmt");
1654 else if (res && i < nch)
1655 res = validate_comma(CHILD(tree, i));
1656 }
1657 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001658 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001659}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001660
1661
Guido van Rossum47478871996-08-21 14:32:37 +00001662static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001663validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001664{
1665 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001666 && validate_name(CHILD(tree, 0), "del")
1667 && validate_exprlist(CHILD(tree, 1)));
1668}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001669
1670
Guido van Rossum47478871996-08-21 14:32:37 +00001671static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001672validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001673{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001674 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001675 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001676 && ((nch == 1) || (nch == 2))
1677 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001678
Guido van Rossum3d602e31996-07-21 02:33:56 +00001679 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001680 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001681
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001682 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001683}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001684
1685
Guido van Rossum47478871996-08-21 14:32:37 +00001686static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001687validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001688{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001689 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001690 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001691 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001692
Guido van Rossum3d602e31996-07-21 02:33:56 +00001693 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001694 res = validate_name(CHILD(tree, 0), "raise");
1695 if (res && (nch >= 2))
1696 res = validate_test(CHILD(tree, 1));
1697 if (res && nch > 2) {
1698 res = (validate_comma(CHILD(tree, 2))
1699 && validate_test(CHILD(tree, 3)));
1700 if (res && (nch > 4))
1701 res = (validate_comma(CHILD(tree, 4))
1702 && validate_test(CHILD(tree, 5)));
1703 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001704 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001705 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001706 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001707 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001708 res = (validate_comma(CHILD(tree, 2))
1709 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001710
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001711 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001712}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001713
1714
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001715/* yield_expr: 'yield' [testlist]
1716 */
1717static int
1718validate_yield_expr(node *tree)
1719{
1720 int nch = NCH(tree);
1721 int res = (validate_ntype(tree, yield_expr)
1722 && ((nch == 1) || (nch == 2))
1723 && validate_name(CHILD(tree, 0), "yield"));
1724
1725 if (res && (nch == 2))
1726 res = validate_testlist(CHILD(tree, 1));
1727
1728 return (res);
1729}
1730
1731
1732/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001733 */
1734static int
1735validate_yield_stmt(node *tree)
1736{
1737 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001738 && validate_numnodes(tree, 1, "yield_stmt")
1739 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001740}
1741
1742
Fred Drakecff283c2000-08-21 22:24:43 +00001743static int
1744validate_import_as_name(node *tree)
1745{
1746 int nch = NCH(tree);
1747 int ok = validate_ntype(tree, import_as_name);
1748
1749 if (ok) {
1750 if (nch == 1)
1751 ok = validate_name(CHILD(tree, 0), NULL);
1752 else if (nch == 3)
1753 ok = (validate_name(CHILD(tree, 0), NULL)
1754 && validate_name(CHILD(tree, 1), "as")
1755 && validate_name(CHILD(tree, 2), NULL));
1756 else
1757 ok = validate_numnodes(tree, 3, "import_as_name");
1758 }
1759 return ok;
1760}
1761
1762
Fred Drake71137082001-01-07 05:59:59 +00001763/* dotted_name: NAME ("." NAME)*
1764 */
1765static int
1766validate_dotted_name(node *tree)
1767{
1768 int nch = NCH(tree);
1769 int res = (validate_ntype(tree, dotted_name)
1770 && is_odd(nch)
1771 && validate_name(CHILD(tree, 0), NULL));
1772 int i;
1773
1774 for (i = 1; res && (i < nch); i += 2) {
1775 res = (validate_dot(CHILD(tree, i))
1776 && validate_name(CHILD(tree, i+1), NULL));
1777 }
1778 return res;
1779}
1780
1781
Fred Drakecff283c2000-08-21 22:24:43 +00001782/* dotted_as_name: dotted_name [NAME NAME]
1783 */
1784static int
1785validate_dotted_as_name(node *tree)
1786{
1787 int nch = NCH(tree);
1788 int res = validate_ntype(tree, dotted_as_name);
1789
1790 if (res) {
1791 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001792 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001793 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001794 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001795 && validate_name(CHILD(tree, 1), "as")
1796 && validate_name(CHILD(tree, 2), NULL));
1797 else {
1798 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001799 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001800 }
1801 }
1802 return res;
1803}
1804
1805
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001806/* dotted_as_name (',' dotted_as_name)* */
1807static int
1808validate_dotted_as_names(node *tree)
1809{
1810 int nch = NCH(tree);
1811 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1812 int i;
1813
1814 for (i = 1; res && (i < nch); i += 2)
1815 res = (validate_comma(CHILD(tree, i))
1816 && validate_dotted_as_name(CHILD(tree, i + 1)));
1817 return (res);
1818}
1819
1820
1821/* import_as_name (',' import_as_name)* [','] */
1822static int
1823validate_import_as_names(node *tree)
1824{
1825 int nch = NCH(tree);
1826 int res = validate_import_as_name(CHILD(tree, 0));
1827 int i;
1828
1829 for (i = 1; res && (i + 1 < nch); i += 2)
1830 res = (validate_comma(CHILD(tree, i))
1831 && validate_import_as_name(CHILD(tree, i + 1)));
1832 return (res);
1833}
1834
1835
1836/* 'import' dotted_as_names */
1837static int
1838validate_import_name(node *tree)
1839{
1840 return (validate_ntype(tree, import_name)
1841 && validate_numnodes(tree, 2, "import_name")
1842 && validate_name(CHILD(tree, 0), "import")
1843 && validate_dotted_as_names(CHILD(tree, 1)));
1844}
1845
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001846/* Helper function to count the number of leading dots in
1847 * 'from ...module import name'
1848 */
1849static int
1850count_from_dots(node *tree)
1851{
1852 int i;
1853 for (i = 0; i < NCH(tree); i++)
1854 if (TYPE(CHILD(tree, i)) != DOT)
1855 break;
1856 return i;
1857}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001858
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001859/* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001860 * import_as_names
Guido van Rossum3d602e31996-07-21 02:33:56 +00001861 */
Guido van Rossum47478871996-08-21 14:32:37 +00001862static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001863validate_import_from(node *tree)
1864{
1865 int nch = NCH(tree);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001866 int ndots = count_from_dots(tree);
1867 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1868 int offset = ndots + havename;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001869 int res = validate_ntype(tree, import_from)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001870 && (nch >= 4 + ndots)
1871 && validate_name(CHILD(tree, 0), "from")
1872 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1873 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001874
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001875 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1876 res = ((nch == offset + 5)
1877 && validate_lparen(CHILD(tree, offset + 2))
1878 && validate_import_as_names(CHILD(tree, offset + 3))
1879 && validate_rparen(CHILD(tree, offset + 4)));
1880 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1881 res = validate_import_as_names(CHILD(tree, offset + 2));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001882 return (res);
1883}
1884
1885
1886/* import_stmt: import_name | import_from */
1887static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001888validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001889{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001890 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001891 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001892
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001893 if (res) {
1894 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001895
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001896 if (ntype == import_name || ntype == import_from)
1897 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001898 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001899 res = 0;
1900 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001901 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001902 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001903 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001904 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001905 PyErr_Format(parser_error,
1906 "Unrecognized child node of import_stmt: %d.",
1907 TYPE(CHILD(tree, 0)));
1908 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001909 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001910}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001911
1912
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001913
1914
Guido van Rossum47478871996-08-21 14:32:37 +00001915static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001916validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001917{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001918 int j;
1919 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001920 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001921 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001922
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001923 if (!res && !PyErr_Occurred())
1924 err_string("illegal global statement");
1925
Guido van Rossum3d602e31996-07-21 02:33:56 +00001926 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001927 res = (validate_name(CHILD(tree, 0), "global")
1928 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001929 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001930 res = (validate_comma(CHILD(tree, j))
1931 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001932
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001933 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001934}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001935
1936
Guido van Rossum3d602e31996-07-21 02:33:56 +00001937/* exec_stmt:
1938 *
1939 * 'exec' expr ['in' test [',' test]]
1940 */
Guido van Rossum47478871996-08-21 14:32:37 +00001941static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001942validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001943{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001944 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001945 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001946 && ((nch == 2) || (nch == 4) || (nch == 6))
1947 && validate_name(CHILD(tree, 0), "exec")
1948 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001949
Guido van Rossum3d602e31996-07-21 02:33:56 +00001950 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001951 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001952 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001953 res = (validate_name(CHILD(tree, 2), "in")
1954 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001955 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001956 res = (validate_comma(CHILD(tree, 4))
1957 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001958
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001959 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001960}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001961
1962
Guido van Rossum925e5471997-04-02 05:32:13 +00001963/* assert_stmt:
1964 *
1965 * 'assert' test [',' test]
1966 */
1967static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001968validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001969{
1970 int nch = NCH(tree);
1971 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001972 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001973 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001974 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001975
1976 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001977 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001978 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001979 res = (validate_comma(CHILD(tree, 2))
1980 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001981
1982 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001983}
Guido van Rossum925e5471997-04-02 05:32:13 +00001984
1985
Guido van Rossum47478871996-08-21 14:32:37 +00001986static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001987validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001988{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001989 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001990 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001991 && ((nch == 4) || (nch == 7))
1992 && validate_name(CHILD(tree, 0), "while")
1993 && validate_test(CHILD(tree, 1))
1994 && validate_colon(CHILD(tree, 2))
1995 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001996
Guido van Rossum3d602e31996-07-21 02:33:56 +00001997 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001998 res = (validate_name(CHILD(tree, 4), "else")
1999 && validate_colon(CHILD(tree, 5))
2000 && validate_suite(CHILD(tree, 6)));
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_for(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, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002011 && ((nch == 6) || (nch == 9))
2012 && validate_name(CHILD(tree, 0), "for")
2013 && validate_exprlist(CHILD(tree, 1))
2014 && validate_name(CHILD(tree, 2), "in")
2015 && validate_testlist(CHILD(tree, 3))
2016 && validate_colon(CHILD(tree, 4))
2017 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002018
Guido van Rossum3d602e31996-07-21 02:33:56 +00002019 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00002020 res = (validate_name(CHILD(tree, 6), "else")
2021 && validate_colon(CHILD(tree, 7))
2022 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002023
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002024 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002025}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002026
2027
Guido van Rossum3d602e31996-07-21 02:33:56 +00002028/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00002029 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002030 * | 'try' ':' suite 'finally' ':' suite
2031 *
2032 */
Guido van Rossum47478871996-08-21 14:32:37 +00002033static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002034validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002035{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002036 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002037 int pos = 3;
2038 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002039 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002040
2041 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002042 res = (validate_name(CHILD(tree, 0), "try")
2043 && validate_colon(CHILD(tree, 1))
2044 && validate_suite(CHILD(tree, 2))
2045 && validate_colon(CHILD(tree, nch - 2))
2046 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00002047 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00002048 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00002049 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
2050 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00002051
2052 PyErr_Format(parser_error,
2053 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002054 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002055 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002056 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002057 res = (validate_except_clause(CHILD(tree, pos))
2058 && validate_colon(CHILD(tree, pos + 1))
2059 && validate_suite(CHILD(tree, pos + 2)));
2060 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002061 }
2062 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002063 res = validate_ntype(CHILD(tree, pos), NAME);
2064 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
2065 res = (validate_numnodes(tree, 6, "try/finally")
2066 && validate_colon(CHILD(tree, 4))
2067 && validate_suite(CHILD(tree, 5)));
2068 else if (res) {
2069 if (nch == (pos + 3)) {
2070 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
2071 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
2072 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00002073 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00002074 }
2075 else if (nch == (pos + 6)) {
2076 res = (validate_name(CHILD(tree, pos), "except")
2077 && validate_colon(CHILD(tree, pos + 1))
2078 && validate_suite(CHILD(tree, pos + 2))
2079 && validate_name(CHILD(tree, pos + 3), "else"));
2080 }
2081 else
2082 res = validate_numnodes(tree, pos + 3, "try/except");
2083 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002084 }
2085 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002086}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002087
2088
Guido van Rossum47478871996-08-21 14:32:37 +00002089static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002090validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002091{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002092 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002093 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00002094 && ((nch == 1) || (nch == 2) || (nch == 4))
2095 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002096
Guido van Rossum3d602e31996-07-21 02:33:56 +00002097 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002098 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002099 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002100 res = (validate_comma(CHILD(tree, 2))
2101 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002102
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002103 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002104}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002105
2106
Guido van Rossum47478871996-08-21 14:32:37 +00002107static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002108validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002109{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002110 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002111 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002112
Guido van Rossum3d602e31996-07-21 02:33:56 +00002113 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002114 res = ((nch == 1)
2115 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002116 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002117 res = validate_or_test(CHILD(tree, 0));
2118 res = (res && (nch == 1 || (nch == 5 &&
2119 validate_name(CHILD(tree, 1), "if") &&
2120 validate_or_test(CHILD(tree, 2)) &&
2121 validate_name(CHILD(tree, 3), "else") &&
2122 validate_test(CHILD(tree, 4)))));
2123 }
2124 return (res);
2125}
2126
2127static int
2128validate_old_test(node *tree)
2129{
2130 int nch = NCH(tree);
2131 int res = validate_ntype(tree, old_test) && (nch == 1);
2132
2133 if (res && (TYPE(CHILD(tree, 0)) == old_lambdef))
2134 res = (validate_old_lambdef(CHILD(tree, 0)));
2135 else if (res) {
2136 res = (validate_or_test(CHILD(tree, 0)));
2137 }
2138 return (res);
2139}
2140
2141static int
2142validate_or_test(node *tree)
2143{
2144 int nch = NCH(tree);
2145 int res = validate_ntype(tree, or_test) && is_odd(nch);
2146
2147 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002148 int pos;
2149 res = validate_and_test(CHILD(tree, 0));
2150 for (pos = 1; res && (pos < nch); pos += 2)
2151 res = (validate_name(CHILD(tree, pos), "or")
2152 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002153 }
2154 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002155}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002156
2157
Guido van Rossum47478871996-08-21 14:32:37 +00002158static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002159validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002160{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002161 int pos;
2162 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002163 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002164 && is_odd(nch)
2165 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002166
Guido van Rossum3d602e31996-07-21 02:33:56 +00002167 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002168 res = (validate_name(CHILD(tree, pos), "and")
2169 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002170
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002171 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002172}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002173
2174
Guido van Rossum47478871996-08-21 14:32:37 +00002175static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002176validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002177{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002178 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002179 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002180
Guido van Rossum3d602e31996-07-21 02:33:56 +00002181 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002182 if (nch == 2)
2183 res = (validate_name(CHILD(tree, 0), "not")
2184 && validate_not_test(CHILD(tree, 1)));
2185 else if (nch == 1)
2186 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002187 }
2188 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002189}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002190
2191
Guido van Rossum47478871996-08-21 14:32:37 +00002192static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002193validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002194{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002195 int pos;
2196 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002197 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002198 && is_odd(nch)
2199 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002200
Guido van Rossum3d602e31996-07-21 02:33:56 +00002201 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002202 res = (validate_comp_op(CHILD(tree, pos))
2203 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002204
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002205 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002206}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002207
2208
Guido van Rossum47478871996-08-21 14:32:37 +00002209static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002210validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002211{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002212 int res = 0;
2213 int nch = NCH(tree);
2214
Guido van Rossum3d602e31996-07-21 02:33:56 +00002215 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002216 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002217 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002218 /*
2219 * Only child will be a terminal with a well-defined symbolic name
2220 * or a NAME with a string of either 'is' or 'in'
2221 */
2222 tree = CHILD(tree, 0);
2223 switch (TYPE(tree)) {
2224 case LESS:
2225 case GREATER:
2226 case EQEQUAL:
2227 case EQUAL:
2228 case LESSEQUAL:
2229 case GREATEREQUAL:
2230 case NOTEQUAL:
2231 res = 1;
2232 break;
2233 case NAME:
2234 res = ((strcmp(STR(tree), "in") == 0)
2235 || (strcmp(STR(tree), "is") == 0));
2236 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002237 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002238 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002239 }
2240 break;
2241 default:
Fred Drake661ea262000-10-24 19:57:45 +00002242 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002243 break;
2244 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002245 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002246 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002247 res = (validate_ntype(CHILD(tree, 0), NAME)
2248 && validate_ntype(CHILD(tree, 1), NAME)
2249 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2250 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2251 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2252 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2253 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002254 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002255 }
2256 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002257}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002258
2259
Guido van Rossum47478871996-08-21 14:32:37 +00002260static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002261validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002262{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002263 int j;
2264 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002265 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002266 && is_odd(nch)
2267 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002268
Guido van Rossum3d602e31996-07-21 02:33:56 +00002269 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002270 res = (validate_xor_expr(CHILD(tree, j))
2271 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002272
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002273 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002274}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002275
2276
Guido van Rossum47478871996-08-21 14:32:37 +00002277static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002278validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002279{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002280 int j;
2281 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002282 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002283 && is_odd(nch)
2284 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002285
Guido van Rossum3d602e31996-07-21 02:33:56 +00002286 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002287 res = (validate_circumflex(CHILD(tree, j - 1))
2288 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002289
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002290 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002291}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002292
2293
Guido van Rossum47478871996-08-21 14:32:37 +00002294static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002295validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002296{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002297 int pos;
2298 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002299 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002300 && is_odd(nch)
2301 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002302
Guido van Rossum3d602e31996-07-21 02:33:56 +00002303 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002304 res = (validate_ampersand(CHILD(tree, pos))
2305 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002306
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002307 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002308}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002309
2310
2311static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002312validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002313 {
2314 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002315 int nch = NCH(tree);
2316 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002317 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002318
Guido van Rossum3d602e31996-07-21 02:33:56 +00002319 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002320 if (TYPE(CHILD(tree, pos)) != op1)
2321 res = validate_ntype(CHILD(tree, pos), op2);
2322 if (res)
2323 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002324 }
2325 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002326}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002327
2328
Guido van Rossum47478871996-08-21 14:32:37 +00002329static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002330validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002331{
2332 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002333 && validate_chain_two_ops(tree, validate_arith_expr,
2334 LEFTSHIFT, RIGHTSHIFT));
2335}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002336
2337
Guido van Rossum47478871996-08-21 14:32:37 +00002338static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002339validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002340{
2341 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002342 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2343}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002344
2345
Guido van Rossum47478871996-08-21 14:32:37 +00002346static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002347validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002348{
2349 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002350 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002351 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002352 && is_odd(nch)
2353 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002354
Guido van Rossum3d602e31996-07-21 02:33:56 +00002355 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002356 res = (((TYPE(CHILD(tree, pos)) == STAR)
2357 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002358 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002359 || (TYPE(CHILD(tree, pos)) == PERCENT))
2360 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002361
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002362 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002363}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002364
2365
Guido van Rossum3d602e31996-07-21 02:33:56 +00002366/* factor:
2367 *
2368 * factor: ('+'|'-'|'~') factor | power
2369 */
Guido van Rossum47478871996-08-21 14:32:37 +00002370static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002371validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002372{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002373 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002374 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002375 && (((nch == 2)
2376 && ((TYPE(CHILD(tree, 0)) == PLUS)
2377 || (TYPE(CHILD(tree, 0)) == MINUS)
2378 || (TYPE(CHILD(tree, 0)) == TILDE))
2379 && validate_factor(CHILD(tree, 1)))
2380 || ((nch == 1)
2381 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002382 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002383}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002384
2385
Guido van Rossum3d602e31996-07-21 02:33:56 +00002386/* power:
2387 *
2388 * power: atom trailer* ('**' factor)*
2389 */
Guido van Rossum47478871996-08-21 14:32:37 +00002390static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002391validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002392{
2393 int pos = 1;
2394 int nch = NCH(tree);
2395 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002396 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002397
2398 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002399 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002400 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002401 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002402 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002403 return (0);
2404 }
2405 for ( ; res && (pos < (nch - 1)); pos += 2)
2406 res = (validate_doublestar(CHILD(tree, pos))
2407 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002408 }
2409 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002410}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002411
2412
Guido van Rossum47478871996-08-21 14:32:37 +00002413static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002414validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002415{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002416 int pos;
2417 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002418 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002419
Fred Drakecff283c2000-08-21 22:24:43 +00002420 if (res && nch < 1)
2421 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002422 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002423 switch (TYPE(CHILD(tree, 0))) {
2424 case LPAR:
2425 res = ((nch <= 3)
2426 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002427
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002428 if (res && (nch == 3)) {
2429 if (TYPE(CHILD(tree, 1))==yield_expr)
2430 res = validate_yield_expr(CHILD(tree, 1));
2431 else
2432 res = validate_testlist_gexp(CHILD(tree, 1));
2433 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002434 break;
2435 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002436 if (nch == 2)
2437 res = validate_ntype(CHILD(tree, 1), RSQB);
2438 else if (nch == 3)
2439 res = (validate_listmaker(CHILD(tree, 1))
2440 && validate_ntype(CHILD(tree, 2), RSQB));
2441 else {
2442 res = 0;
2443 err_string("illegal list display atom");
2444 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002445 break;
2446 case LBRACE:
2447 res = ((nch <= 3)
2448 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002449
Fred Drakeff9ea482000-04-19 13:54:15 +00002450 if (res && (nch == 3))
2451 res = validate_dictmaker(CHILD(tree, 1));
2452 break;
2453 case BACKQUOTE:
2454 res = ((nch == 3)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002455 && validate_testlist1(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002456 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2457 break;
2458 case NAME:
2459 case NUMBER:
2460 res = (nch == 1);
2461 break;
2462 case STRING:
2463 for (pos = 1; res && (pos < nch); ++pos)
2464 res = validate_ntype(CHILD(tree, pos), STRING);
2465 break;
2466 default:
2467 res = 0;
2468 break;
2469 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002470 }
2471 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002472}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002473
2474
Fred Drake85bf3bb2000-08-23 15:35:26 +00002475/* listmaker:
2476 * test ( list_for | (',' test)* [','] )
2477 */
Fred Drakecff283c2000-08-21 22:24:43 +00002478static int
2479validate_listmaker(node *tree)
2480{
2481 int nch = NCH(tree);
2482 int ok = nch;
2483
2484 if (nch == 0)
2485 err_string("missing child nodes of listmaker");
2486 else
2487 ok = validate_test(CHILD(tree, 0));
2488
2489 /*
Raymond Hettinger354433a2004-05-19 08:20:33 +00002490 * list_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002491 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002492 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2493 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002494 else {
2495 /* (',' test)* [','] */
2496 int i = 1;
2497 while (ok && nch - i >= 2) {
2498 ok = (validate_comma(CHILD(tree, i))
2499 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002500 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002501 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002502 if (ok && i == nch-1)
2503 ok = validate_comma(CHILD(tree, i));
2504 else if (i != nch) {
2505 ok = 0;
2506 err_string("illegal trailing nodes for listmaker");
2507 }
Fred Drakecff283c2000-08-21 22:24:43 +00002508 }
2509 return ok;
2510}
2511
Raymond Hettinger354433a2004-05-19 08:20:33 +00002512/* testlist_gexp:
2513 * test ( gen_for | (',' test)* [','] )
2514 */
2515static int
2516validate_testlist_gexp(node *tree)
2517{
2518 int nch = NCH(tree);
2519 int ok = nch;
2520
2521 if (nch == 0)
2522 err_string("missing child nodes of testlist_gexp");
2523 else {
2524 ok = validate_test(CHILD(tree, 0));
2525 }
2526
2527 /*
2528 * gen_for | (',' test)* [',']
2529 */
2530 if (nch == 2 && TYPE(CHILD(tree, 1)) == gen_for)
2531 ok = validate_gen_for(CHILD(tree, 1));
2532 else {
2533 /* (',' test)* [','] */
2534 int i = 1;
2535 while (ok && nch - i >= 2) {
2536 ok = (validate_comma(CHILD(tree, i))
2537 && validate_test(CHILD(tree, i+1)));
2538 i += 2;
2539 }
2540 if (ok && i == nch-1)
2541 ok = validate_comma(CHILD(tree, i));
2542 else if (i != nch) {
2543 ok = 0;
2544 err_string("illegal trailing nodes for testlist_gexp");
2545 }
2546 }
2547 return ok;
2548}
Fred Drakecff283c2000-08-21 22:24:43 +00002549
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002550/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002551 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002552 */
2553static int
2554validate_decorator(node *tree)
2555{
2556 int ok;
2557 int nch = NCH(tree);
2558 ok = (validate_ntype(tree, decorator) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002559 (nch == 3 || nch == 5 || nch == 6) &&
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002560 validate_at(CHILD(tree, 0)) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002561 validate_dotted_name(CHILD(tree, 1)) &&
2562 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002563
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002564 if (ok && nch != 3) {
2565 ok = (validate_lparen(CHILD(tree, 2)) &&
2566 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002567
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002568 if (ok && nch == 6)
2569 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002570 }
2571
2572 return ok;
2573}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002574
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002575/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002576 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002577 */
2578static int
2579validate_decorators(node *tree)
2580{
2581 int i, nch, ok;
2582 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002583 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002584
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002585 for (i = 0; ok && i < nch; ++i)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002586 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002587
2588 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002589}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002590
Guido van Rossum3d602e31996-07-21 02:33:56 +00002591/* funcdef:
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002592 *
Christian Heimes5224d282008-02-23 15:01:05 +00002593 * -5 -4 -3 -2 -1
2594 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002595 */
Guido van Rossum47478871996-08-21 14:32:37 +00002596static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002597validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002598{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002599 int nch = NCH(tree);
2600 int ok = (validate_ntype(tree, funcdef)
Christian Heimes5224d282008-02-23 15:01:05 +00002601 && (nch == 5)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002602 && validate_name(RCHILD(tree, -5), "def")
2603 && validate_ntype(RCHILD(tree, -4), NAME)
2604 && validate_colon(RCHILD(tree, -2))
2605 && validate_parameters(RCHILD(tree, -3))
2606 && validate_suite(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002607 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002608}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002609
2610
Christian Heimes5224d282008-02-23 15:01:05 +00002611/* decorated
2612 * decorators (classdef | funcdef)
2613 */
2614static int
2615validate_decorated(node *tree)
2616{
2617 int nch = NCH(tree);
2618 int ok = (validate_ntype(tree, decorated)
2619 && (nch == 2)
2620 && validate_decorators(RCHILD(tree, -2))
2621 && (validate_funcdef(RCHILD(tree, -1))
2622 || validate_class(RCHILD(tree, -1)))
2623 );
2624 return ok;
2625}
2626
Guido van Rossum47478871996-08-21 14:32:37 +00002627static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002628validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002629{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002630 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002631 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002632 && ((nch == 3) || (nch == 4))
2633 && validate_name(CHILD(tree, 0), "lambda")
2634 && validate_colon(CHILD(tree, nch - 2))
2635 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002636
Guido van Rossum3d602e31996-07-21 02:33:56 +00002637 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002638 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002639 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002640 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002641
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002642 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002643}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002644
2645
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002646static int
2647validate_old_lambdef(node *tree)
2648{
2649 int nch = NCH(tree);
2650 int res = (validate_ntype(tree, old_lambdef)
2651 && ((nch == 3) || (nch == 4))
2652 && validate_name(CHILD(tree, 0), "lambda")
2653 && validate_colon(CHILD(tree, nch - 2))
2654 && validate_test(CHILD(tree, nch - 1)));
2655
2656 if (res && (nch == 4))
2657 res = validate_varargslist(CHILD(tree, 1));
2658 else if (!res && !PyErr_Occurred())
2659 (void) validate_numnodes(tree, 3, "old_lambdef");
2660
2661 return (res);
2662}
2663
2664
Guido van Rossum3d602e31996-07-21 02:33:56 +00002665/* arglist:
2666 *
Fred Drakecff283c2000-08-21 22:24:43 +00002667 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002668 */
Guido van Rossum47478871996-08-21 14:32:37 +00002669static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002670validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002671{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002672 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002673 int i = 0;
2674 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002675
2676 if (nch <= 0)
2677 /* raise the right error from having an invalid number of children */
2678 return validate_numnodes(tree, nch + 1, "arglist");
2679
Raymond Hettinger354433a2004-05-19 08:20:33 +00002680 if (nch > 1) {
2681 for (i=0; i<nch; i++) {
2682 if (TYPE(CHILD(tree, i)) == argument) {
2683 node *ch = CHILD(tree, i);
2684 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == gen_for) {
2685 err_string("need '(', ')' for generator expression");
2686 return 0;
2687 }
2688 }
2689 }
2690 }
2691
Fred Drakecff283c2000-08-21 22:24:43 +00002692 while (ok && nch-i >= 2) {
2693 /* skip leading (argument ',') */
2694 ok = (validate_argument(CHILD(tree, i))
2695 && validate_comma(CHILD(tree, i+1)));
2696 if (ok)
2697 i += 2;
2698 else
2699 PyErr_Clear();
2700 }
2701 ok = 1;
2702 if (nch-i > 0) {
2703 /*
2704 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002705 */
Fred Drakecff283c2000-08-21 22:24:43 +00002706 int sym = TYPE(CHILD(tree, i));
2707
2708 if (sym == argument) {
2709 ok = validate_argument(CHILD(tree, i));
2710 if (ok && i+1 != nch) {
2711 err_string("illegal arglist specification"
2712 " (extra stuff on end)");
2713 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002714 }
Fred Drakecff283c2000-08-21 22:24:43 +00002715 }
2716 else if (sym == STAR) {
2717 ok = validate_star(CHILD(tree, i));
2718 if (ok && (nch-i == 2))
2719 ok = validate_test(CHILD(tree, i+1));
2720 else if (ok && (nch-i == 5))
2721 ok = (validate_test(CHILD(tree, i+1))
2722 && validate_comma(CHILD(tree, i+2))
2723 && validate_doublestar(CHILD(tree, i+3))
2724 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002725 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002726 err_string("illegal use of '*' in arglist");
2727 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002728 }
2729 }
Fred Drakecff283c2000-08-21 22:24:43 +00002730 else if (sym == DOUBLESTAR) {
2731 if (nch-i == 2)
2732 ok = (validate_doublestar(CHILD(tree, i))
2733 && validate_test(CHILD(tree, i+1)));
2734 else {
2735 err_string("illegal use of '**' in arglist");
2736 ok = 0;
2737 }
2738 }
2739 else {
2740 err_string("illegal arglist specification");
2741 ok = 0;
2742 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002743 }
2744 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002745}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002746
2747
2748
2749/* argument:
2750 *
Raymond Hettinger354433a2004-05-19 08:20:33 +00002751 * [test '='] test [gen_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002752 */
Guido van Rossum47478871996-08-21 14:32:37 +00002753static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002754validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002755{
2756 int nch = NCH(tree);
2757 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002758 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002759 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002760
Raymond Hettinger354433a2004-05-19 08:20:33 +00002761 if (res && (nch == 2))
2762 res = validate_gen_for(CHILD(tree, 1));
2763 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002764 res = (validate_equal(CHILD(tree, 1))
2765 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002766
2767 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002768}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002769
2770
2771
2772/* trailer:
2773 *
Guido van Rossum47478871996-08-21 14:32:37 +00002774 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002775 */
Guido van Rossum47478871996-08-21 14:32:37 +00002776static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002777validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002778{
2779 int nch = NCH(tree);
2780 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002781
2782 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002783 switch (TYPE(CHILD(tree, 0))) {
2784 case LPAR:
2785 res = validate_rparen(CHILD(tree, nch - 1));
2786 if (res && (nch == 3))
2787 res = validate_arglist(CHILD(tree, 1));
2788 break;
2789 case LSQB:
2790 res = (validate_numnodes(tree, 3, "trailer")
2791 && validate_subscriptlist(CHILD(tree, 1))
2792 && validate_ntype(CHILD(tree, 2), RSQB));
2793 break;
2794 case DOT:
2795 res = (validate_numnodes(tree, 2, "trailer")
2796 && validate_ntype(CHILD(tree, 1), NAME));
2797 break;
2798 default:
2799 res = 0;
2800 break;
2801 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002802 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002803 else {
2804 (void) validate_numnodes(tree, 2, "trailer");
2805 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002806 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002807}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002808
2809
Guido van Rossum47478871996-08-21 14:32:37 +00002810/* subscriptlist:
2811 *
2812 * subscript (',' subscript)* [',']
2813 */
2814static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002815validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002816{
2817 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002818 validate_subscript, "subscriptlist"));
2819}
Guido van Rossum47478871996-08-21 14:32:37 +00002820
2821
Guido van Rossum3d602e31996-07-21 02:33:56 +00002822/* subscript:
2823 *
Guido van Rossum47478871996-08-21 14:32:37 +00002824 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002825 */
Guido van Rossum47478871996-08-21 14:32:37 +00002826static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002827validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002828{
Guido van Rossum47478871996-08-21 14:32:37 +00002829 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002830 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002831 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002832
Guido van Rossum47478871996-08-21 14:32:37 +00002833 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002834 if (!PyErr_Occurred())
2835 err_string("invalid number of arguments for subscript node");
2836 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002837 }
Guido van Rossum47478871996-08-21 14:32:37 +00002838 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002839 /* take care of ('.' '.' '.') possibility */
2840 return (validate_numnodes(tree, 3, "subscript")
2841 && validate_dot(CHILD(tree, 0))
2842 && validate_dot(CHILD(tree, 1))
2843 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002844 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002845 if (TYPE(CHILD(tree, 0)) == test)
2846 res = validate_test(CHILD(tree, 0));
2847 else
2848 res = validate_colon(CHILD(tree, 0));
2849 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002850 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002851 /* Must be [test] ':' [test] [sliceop],
2852 * but at least one of the optional components will
2853 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002854 */
2855 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002856 res = validate_test(CHILD(tree, 0));
2857 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002858 }
2859 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002860 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002861 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002862 int rem = nch - ++offset;
2863 if (rem) {
2864 if (TYPE(CHILD(tree, offset)) == test) {
2865 res = validate_test(CHILD(tree, offset));
2866 ++offset;
2867 --rem;
2868 }
2869 if (res && rem)
2870 res = validate_sliceop(CHILD(tree, offset));
2871 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002872 }
2873 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002874}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002875
2876
Guido van Rossum47478871996-08-21 14:32:37 +00002877static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002878validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002879{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002880 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002881 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002882 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002883 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002884 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002885 }
Guido van Rossum47478871996-08-21 14:32:37 +00002886 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002887 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002888 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002889 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002890
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002891 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002892}
Guido van Rossum47478871996-08-21 14:32:37 +00002893
2894
2895static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002896validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002897{
2898 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002899 validate_expr, "exprlist"));
2900}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002901
2902
Guido van Rossum47478871996-08-21 14:32:37 +00002903static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002904validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002905{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002906 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002907 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002908 && (nch >= 3)
2909 && validate_test(CHILD(tree, 0))
2910 && validate_colon(CHILD(tree, 1))
2911 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002912
Guido van Rossum3d602e31996-07-21 02:33:56 +00002913 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002914 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002915 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002916 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002917
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002918 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002919 int pos = 3;
2920 /* ( ',' test ':' test )* */
2921 while (res && (pos < nch)) {
2922 res = (validate_comma(CHILD(tree, pos))
2923 && validate_test(CHILD(tree, pos + 1))
2924 && validate_colon(CHILD(tree, pos + 2))
2925 && validate_test(CHILD(tree, pos + 3)));
2926 pos += 4;
2927 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002928 }
2929 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002930}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002931
2932
Guido van Rossum47478871996-08-21 14:32:37 +00002933static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002934validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002935{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002936 int pos;
2937 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002938 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002939 && (nch >= 2)
2940 && validate_testlist(CHILD(tree, 0))
2941 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002942
Guido van Rossum3d602e31996-07-21 02:33:56 +00002943 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002944 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002945
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002946 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002947}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002948
2949
Guido van Rossum47478871996-08-21 14:32:37 +00002950static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002951validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002952{
Fred Drakeff9ea482000-04-19 13:54:15 +00002953 int nch = 0; /* num. children on current node */
2954 int res = 1; /* result value */
2955 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002956
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002957 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002958 nch = NCH(tree);
2959 next = 0;
2960 switch (TYPE(tree)) {
2961 /*
2962 * Definition nodes.
2963 */
2964 case funcdef:
2965 res = validate_funcdef(tree);
2966 break;
2967 case classdef:
2968 res = validate_class(tree);
2969 break;
Christian Heimes5224d282008-02-23 15:01:05 +00002970 case decorated:
2971 res = validate_decorated(tree);
2972 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002973 /*
2974 * "Trivial" parse tree nodes.
2975 * (Why did I call these trivial?)
2976 */
2977 case stmt:
2978 res = validate_stmt(tree);
2979 break;
2980 case small_stmt:
2981 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002982 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002983 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2984 */
2985 res = validate_small_stmt(tree);
2986 break;
2987 case flow_stmt:
2988 res = (validate_numnodes(tree, 1, "flow_stmt")
2989 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2990 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002991 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002992 || (TYPE(CHILD(tree, 0)) == return_stmt)
2993 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2994 if (res)
2995 next = CHILD(tree, 0);
2996 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002997 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002998 break;
Fred Drake02126f22001-07-17 02:59:15 +00002999 case yield_stmt:
3000 res = validate_yield_stmt(tree);
3001 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003002 /*
3003 * Compound statements.
3004 */
3005 case simple_stmt:
3006 res = validate_simple_stmt(tree);
3007 break;
3008 case compound_stmt:
3009 res = validate_compound_stmt(tree);
3010 break;
3011 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00003012 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00003013 */
3014 case expr_stmt:
3015 res = validate_expr_stmt(tree);
3016 break;
3017 case print_stmt:
3018 res = validate_print_stmt(tree);
3019 break;
3020 case del_stmt:
3021 res = validate_del_stmt(tree);
3022 break;
3023 case pass_stmt:
3024 res = (validate_numnodes(tree, 1, "pass")
3025 && validate_name(CHILD(tree, 0), "pass"));
3026 break;
3027 case break_stmt:
3028 res = (validate_numnodes(tree, 1, "break")
3029 && validate_name(CHILD(tree, 0), "break"));
3030 break;
3031 case continue_stmt:
3032 res = (validate_numnodes(tree, 1, "continue")
3033 && validate_name(CHILD(tree, 0), "continue"));
3034 break;
3035 case return_stmt:
3036 res = validate_return_stmt(tree);
3037 break;
3038 case raise_stmt:
3039 res = validate_raise_stmt(tree);
3040 break;
3041 case import_stmt:
3042 res = validate_import_stmt(tree);
3043 break;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00003044 case import_name:
3045 res = validate_import_name(tree);
3046 break;
3047 case import_from:
3048 res = validate_import_from(tree);
3049 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003050 case global_stmt:
3051 res = validate_global_stmt(tree);
3052 break;
3053 case exec_stmt:
3054 res = validate_exec_stmt(tree);
3055 break;
3056 case assert_stmt:
3057 res = validate_assert_stmt(tree);
3058 break;
3059 case if_stmt:
3060 res = validate_if(tree);
3061 break;
3062 case while_stmt:
3063 res = validate_while(tree);
3064 break;
3065 case for_stmt:
3066 res = validate_for(tree);
3067 break;
3068 case try_stmt:
3069 res = validate_try(tree);
3070 break;
3071 case suite:
3072 res = validate_suite(tree);
3073 break;
3074 /*
3075 * Expression nodes.
3076 */
3077 case testlist:
3078 res = validate_testlist(tree);
3079 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003080 case yield_expr:
3081 res = validate_yield_expr(tree);
3082 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00003083 case testlist1:
3084 res = validate_testlist1(tree);
3085 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003086 case test:
3087 res = validate_test(tree);
3088 break;
3089 case and_test:
3090 res = validate_and_test(tree);
3091 break;
3092 case not_test:
3093 res = validate_not_test(tree);
3094 break;
3095 case comparison:
3096 res = validate_comparison(tree);
3097 break;
3098 case exprlist:
3099 res = validate_exprlist(tree);
3100 break;
3101 case comp_op:
3102 res = validate_comp_op(tree);
3103 break;
3104 case expr:
3105 res = validate_expr(tree);
3106 break;
3107 case xor_expr:
3108 res = validate_xor_expr(tree);
3109 break;
3110 case and_expr:
3111 res = validate_and_expr(tree);
3112 break;
3113 case shift_expr:
3114 res = validate_shift_expr(tree);
3115 break;
3116 case arith_expr:
3117 res = validate_arith_expr(tree);
3118 break;
3119 case term:
3120 res = validate_term(tree);
3121 break;
3122 case factor:
3123 res = validate_factor(tree);
3124 break;
3125 case power:
3126 res = validate_power(tree);
3127 break;
3128 case atom:
3129 res = validate_atom(tree);
3130 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003131
Fred Drakeff9ea482000-04-19 13:54:15 +00003132 default:
3133 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00003134 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003135 res = 0;
3136 break;
3137 }
3138 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003139 }
3140 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003141}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003142
3143
Guido van Rossum47478871996-08-21 14:32:37 +00003144static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003145validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003146{
3147 int res = validate_eval_input(tree);
3148
3149 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003150 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003151
3152 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003153}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003154
3155
Guido van Rossum3d602e31996-07-21 02:33:56 +00003156/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003157 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003158 */
Guido van Rossum47478871996-08-21 14:32:37 +00003159static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003160validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003161{
Fred Drakec2683dd2001-07-17 19:32:05 +00003162 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003163 int nch = NCH(tree) - 1;
3164 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003165 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003166
Fred Drakec2683dd2001-07-17 19:32:05 +00003167 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003168 if (TYPE(CHILD(tree, j)) == stmt)
3169 res = validate_stmt(CHILD(tree, j));
3170 else
3171 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003172 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003173 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003174 * user. Hopefully, this won't be needed. If a user reports getting
3175 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003176 */
3177 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003178 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003179
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003180 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003181}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003182
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003183static int
3184validate_encoding_decl(node *tree)
3185{
3186 int nch = NCH(tree);
3187 int res = ((nch == 1)
3188 && validate_file_input(CHILD(tree, 0)));
3189
3190 if (!res && !PyErr_Occurred())
3191 err_string("Error Parsing encoding_decl");
3192
3193 return res;
3194}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003195
Fred Drake43f8f9b1998-04-13 16:25:46 +00003196static PyObject*
3197pickle_constructor = NULL;
3198
3199
3200static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003201parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003202{
Fred Drake268397f1998-04-29 14:16:32 +00003203 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003204 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003205 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003206 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003207
Fred Drakec2683dd2001-07-17 19:32:05 +00003208 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003209 PyObject *newargs;
3210 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003211
Fred Drake2a6875e1999-09-20 22:32:18 +00003212 if ((empty_dict = PyDict_New()) == NULL)
3213 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003214 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003215 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003216 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003217 if (tuple != NULL) {
3218 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3219 Py_DECREF(tuple);
3220 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003221 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003222 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003223 }
3224 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003225 Py_XDECREF(empty_dict);
3226
Fred Drake43f8f9b1998-04-13 16:25:46 +00003227 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003228}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003229
3230
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003231/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003232 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003233 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003234 * We'd really have to write a wrapper around it all anyway to allow
3235 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003236 */
3237static PyMethodDef parser_functions[] = {
Georg Brandlf9efabb2008-07-23 15:16:45 +00003238 {"ast2tuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003239 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Georg Brandlf9efabb2008-07-23 15:16:45 +00003240 {"ast2list", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003241 PyDoc_STR("Creates a list-tree representation of an ST.")},
Georg Brandlf9efabb2008-07-23 15:16:45 +00003242 {"compileast", (PyCFunction)parser_compileast,PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003243 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003244 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003245 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003246 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003247 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003248 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003249 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003250 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003251 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003252 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003253 PyDoc_STR("Creates an ST object from a suite.")},
Georg Brandlf9efabb2008-07-23 15:16:45 +00003254 {"sequence2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003255 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003256 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003257 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003258 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003259 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003260 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003261 PyDoc_STR("Creates a list-tree representation of an ST.")},
Georg Brandlf9efabb2008-07-23 15:16:45 +00003262 {"tuple2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003263 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003264 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003265 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003266
Fred Drake43f8f9b1998-04-13 16:25:46 +00003267 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003268 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003269 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003270
Fred Drake268397f1998-04-29 14:16:32 +00003271 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003272 };
3273
3274
Mark Hammond62b1ab12002-07-23 06:31:15 +00003275PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003276
Mark Hammond62b1ab12002-07-23 06:31:15 +00003277PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00003278initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003279{
Fred Drake13130bc2001-08-15 16:44:56 +00003280 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003281
Christian Heimese93237d2007-12-19 02:37:44 +00003282 Py_TYPE(&PyST_Type) = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00003283 module = Py_InitModule("parser", parser_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003284 if (module == NULL)
3285 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003286
Fred Drake7a15ba51999-09-09 14:21:52 +00003287 if (parser_error == 0)
3288 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003289
Tim Peters6a627252003-07-21 14:25:23 +00003290 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00003291 /* caller will check PyErr_Occurred() */
3292 return;
Tim Peters6a627252003-07-21 14:25:23 +00003293 /* CAUTION: The code next used to skip bumping the refcount on
3294 * parser_error. That's a disaster if initparser() gets called more
3295 * than once. By incref'ing, we ensure that each module dict that
3296 * gets created owns its reference to the shared parser_error object,
3297 * and the file static parser_error vrbl owns a reference too.
3298 */
3299 Py_INCREF(parser_error);
3300 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3301 return;
3302
Fred Drakec2683dd2001-07-17 19:32:05 +00003303 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003304 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00003305 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003306 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003307
Fred Drake13130bc2001-08-15 16:44:56 +00003308 PyModule_AddStringConstant(module, "__copyright__",
3309 parser_copyright_string);
3310 PyModule_AddStringConstant(module, "__doc__",
3311 parser_doc_string);
3312 PyModule_AddStringConstant(module, "__version__",
3313 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003314
Fred Drake78bdb9b2001-07-19 20:17:15 +00003315 /* Register to support pickling.
3316 * If this fails, the import of this module will fail because an
3317 * exception will be raised here; should we clear the exception?
3318 */
Georg Brandldffbf5f2008-05-20 07:49:57 +00003319 copyreg = PyImport_ImportModuleNoBlock("copy_reg");
Fred Drake13130bc2001-08-15 16:44:56 +00003320 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003321 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003322
Fred Drake13130bc2001-08-15 16:44:56 +00003323 func = PyObject_GetAttrString(copyreg, "pickle");
3324 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3325 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003326 Py_XINCREF(pickle_constructor);
3327 if ((func != NULL) && (pickle_constructor != NULL)
3328 && (pickler != NULL)) {
3329 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003330
Georg Brandl684fd0c2006-05-25 19:15:31 +00003331 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3332 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003333 Py_XDECREF(res);
3334 }
3335 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003336 Py_XDECREF(pickle_constructor);
3337 Py_XDECREF(pickler);
3338 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003339 }
Fred Drakeff9ea482000-04-19 13:54:15 +00003340}