blob: f795088273e935ddaf526f59377e768d176f29ab [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000026 */
27
Fred Drakeff9ea482000-04-19 13:54:15 +000028#include "Python.h" /* general Python API */
29#include "graminit.h" /* symbols defined in the grammar */
30#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000031#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000032#include "token.h" /* token definitions */
33 /* ISTERMINAL() / ISNONTERMINAL() */
34#include "compile.h" /* PyNode_Compile() */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000035
Fred Drake268397f1998-04-29 14:16:32 +000036#ifdef lint
37#include <note.h>
38#else
39#define NOTE(x)
40#endif
41
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000042/* String constants used to initialize module attributes.
43 *
44 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045static char parser_copyright_string[] =
46"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000047University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
48Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
49Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000050
51
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000052PyDoc_STRVAR(parser_doc_string,
53"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000054
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000055static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000056
57
Martin v. Löwis18e16552006-02-15 17:27:45 +000058typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000059typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000060 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000061 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000062
Thomas Wouters7e474022000-07-16 12:04:32 +000063/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000064 * original copyright statement is included below, and continues to apply
65 * in full to the function immediately following. All other material is
66 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
67 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000068 * new naming conventions. Added arguments to provide support for creating
69 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000070 */
71
Guido van Rossum52f2c051993-11-10 12:53:24 +000072
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000073static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000074node2tuple(node *n, /* node to convert */
75 SeqMaker mkseq, /* create sequence */
76 SeqInserter addelem, /* func. to add elem. in seq. */
Thomas Wouters89f507f2006-12-13 04:49:30 +000077 int lineno, /* include line numbers? */
78 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000079{
Guido van Rossum3d602e31996-07-21 02:33:56 +000080 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000081 Py_INCREF(Py_None);
82 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000083 }
84 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000085 int i;
86 PyObject *v;
87 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000088
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000089 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000090 if (v == NULL)
91 return (v);
92 w = PyInt_FromLong(TYPE(n));
93 if (w == NULL) {
94 Py_DECREF(v);
95 return ((PyObject*) NULL);
96 }
97 (void) addelem(v, 0, w);
98 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +000099 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000100 if (w == NULL) {
101 Py_DECREF(v);
102 return ((PyObject*) NULL);
103 }
104 (void) addelem(v, i+1, w);
105 }
Tim Peters6a627252003-07-21 14:25:23 +0000106
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000107 if (TYPE(n) == encoding_decl)
108 (void) addelem(v, i+1, PyString_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000109 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000110 }
111 else if (ISTERMINAL(TYPE(n))) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000112 PyObject *result = mkseq(2 + lineno + col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000113 if (result != NULL) {
114 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
115 (void) addelem(result, 1, PyString_FromString(STR(n)));
116 if (lineno == 1)
117 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000118 if (col_offset == 1)
119 (void) addelem(result, 3, PyInt_FromLong(n->n_col_offset));
Fred Drakeff9ea482000-04-19 13:54:15 +0000120 }
121 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000122 }
123 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000124 PyErr_SetString(PyExc_SystemError,
125 "unrecognized parse tree node type");
126 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000127 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000128}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000129/*
130 * End of material copyrighted by Stichting Mathematisch Centrum.
131 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000132
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000133
134
135/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000136 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000137 * field of the object type to identify which any given object represents.
138 * These should probably go in an external header to allow other extensions
139 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000140 */
141
Fred Drakec2683dd2001-07-17 19:32:05 +0000142#define PyST_EXPR 1
143#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000144
145
146/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000147 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000148 * naming style, but the code uses the new naming convention.
149 */
150
151static PyObject*
152parser_error = 0;
153
154
Fred Drakec2683dd2001-07-17 19:32:05 +0000155typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000156 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000157 node* st_node; /* the node* returned by the parser */
158 int st_type; /* EXPR or SUITE ? */
159} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000160
161
Jeremy Hylton938ace62002-07-17 16:30:39 +0000162static void parser_free(PyST_Object *st);
163static int parser_compare(PyST_Object *left, PyST_Object *right);
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000164static PyObject *parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000165
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000166
Fred Drake268397f1998-04-29 14:16:32 +0000167static
Fred Drakec2683dd2001-07-17 19:32:05 +0000168PyTypeObject PyST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000169 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000170 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000171 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000172 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000173 0, /* tp_itemsize */
174 (destructor)parser_free, /* tp_dealloc */
175 0, /* tp_print */
176 parser_getattr, /* tp_getattr */
177 0, /* tp_setattr */
178 (cmpfunc)parser_compare, /* tp_compare */
179 0, /* tp_repr */
180 0, /* tp_as_number */
181 0, /* tp_as_sequence */
182 0, /* tp_as_mapping */
183 0, /* tp_hash */
184 0, /* tp_call */
185 0, /* tp_str */
186 0, /* tp_getattro */
187 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000188
189 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000190 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000191
Fred Drakeff9ea482000-04-19 13:54:15 +0000192 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000193
194 /* __doc__ */
195 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000196}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000197
198
199static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000200parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000201{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000202 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000203
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000204 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000205 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000206
207 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000208 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000209
210 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000211 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000212
213 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000214 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000215
216 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000217 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000218
219 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000220 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000221
Fred Drakeff9ea482000-04-19 13:54:15 +0000222 if (v != 0)
223 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000224 }
225 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000226}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000227
228
Fred Drakec2683dd2001-07-17 19:32:05 +0000229/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000230 *
231 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
232 * This really just wraps a call to parser_compare_nodes() with some easy
233 * checks and protection code.
234 *
235 */
236static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000237parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000238{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000239 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000240 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000241
242 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000243 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000244
Fred Drakec2683dd2001-07-17 19:32:05 +0000245 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000246}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247
248
Fred Drakec2683dd2001-07-17 19:32:05 +0000249/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000250 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000251 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000252 * 'wrapper' object that holds a node* and allows it to be passed around in
253 * Python code.
254 *
255 */
256static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000257parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000258{
Fred Drakec2683dd2001-07-17 19:32:05 +0000259 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000260
261 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000262 o->st_node = st;
263 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000264 }
Fred Drake268397f1998-04-29 14:16:32 +0000265 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000266 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000267 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000268 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000269}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000270
271
Fred Drakec2683dd2001-07-17 19:32:05 +0000272/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000273 *
274 * This is called by a del statement that reduces the reference count to 0.
275 *
276 */
277static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000278parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000279{
Fred Drakec2683dd2001-07-17 19:32:05 +0000280 PyNode_Free(st->st_node);
281 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000282}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000283
284
Fred Drakec2683dd2001-07-17 19:32:05 +0000285/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000286 *
287 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000288 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000289 *
290 */
291static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000292parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000293{
Guido van Rossum47478871996-08-21 14:32:37 +0000294 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000295 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000296 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000297 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000298
Thomas Wouters89f507f2006-12-13 04:49:30 +0000299 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000300
Fred Drake268397f1998-04-29 14:16:32 +0000301 if (self == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000302 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
303 &PyST_Type, &self, &line_option,
304 &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000305 }
Fred Drake503d8d61998-04-13 18:45:18 +0000306 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000307 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
308 &line_option, &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000309 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000310 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000311 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000312 if (line_option != NULL) {
313 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
314 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000315 if (col_option != NULL) {
316 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
317 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000318 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000319 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000320 * since it's known to work already.
321 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000322 res = node2tuple(((PyST_Object*)self)->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000323 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000324 }
325 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000326}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000327
328
Fred Drakec2683dd2001-07-17 19:32:05 +0000329/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000330 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000331 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000332 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000333 *
334 */
335static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000336parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000337{
Guido van Rossum47478871996-08-21 14:32:37 +0000338 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000339 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000340 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000341 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000342
Thomas Wouters89f507f2006-12-13 04:49:30 +0000343 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000344
Fred Drake503d8d61998-04-13 18:45:18 +0000345 if (self == NULL)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000346 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
347 &PyST_Type, &self, &line_option,
348 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000349 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000350 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
351 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000352 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000353 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000354 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000355 if (line_option != 0) {
356 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
357 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000358 if (col_option != NULL) {
359 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
360 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000361 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000362 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000363 * since it's known to work already.
364 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000365 res = node2tuple(self->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000366 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000367 }
368 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000369}
Guido van Rossum47478871996-08-21 14:32:37 +0000370
371
Fred Drakec2683dd2001-07-17 19:32:05 +0000372/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000373 *
374 * This function creates code objects from the parse tree represented by
375 * the passed-in data object. An optional file name is passed in as well.
376 *
377 */
378static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000379parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000380{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000381 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000382 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000383 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000384
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000385 static char *keywords[] = {"ast", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000386
Fred Drake503d8d61998-04-13 18:45:18 +0000387 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000388 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
389 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000390 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000391 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000392 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000393
394 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000395 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000396
397 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000398}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000399
400
401/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
402 * PyObject* parser_issuite(PyObject* self, PyObject* args)
403 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000404 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000405 * a statement suite, respectively. The return is a Python truth value.
406 *
407 */
408static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000409parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000410{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000411 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000412 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000413
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000414 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000415
Fred Drake503d8d61998-04-13 18:45:18 +0000416 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000417 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000418 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000419 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000420 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000421
422 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000423 /* Check to see if the ST represents an expression or not. */
424 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000425 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000426 }
427 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000428}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000429
430
431static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000432parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000433{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000434 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000435 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000436
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000437 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000438
Fred Drake503d8d61998-04-13 18:45:18 +0000439 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000440 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000441 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000442 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000443 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000444
445 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000446 /* Check to see if the ST represents an expression or not. */
447 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000448 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000449 }
450 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000451}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000452
453
Fred Drake7a15ba51999-09-09 14:21:52 +0000454#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
455
Fred Drake503d8d61998-04-13 18:45:18 +0000456static PyMethodDef
457parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000458 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000459 PyDoc_STR("Compile this ST object into a code object.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000460 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000461 PyDoc_STR("Determines if this ST object was created from an expression.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000462 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000463 PyDoc_STR("Determines if this ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000464 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000465 PyDoc_STR("Creates a list-tree representation of this ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000466 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000467 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Fred Drake503d8d61998-04-13 18:45:18 +0000468
Fred Drake268397f1998-04-29 14:16:32 +0000469 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000470};
471
Fred Drake503d8d61998-04-13 18:45:18 +0000472
473static PyObject*
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000474parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000475{
Fred Drake503d8d61998-04-13 18:45:18 +0000476 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000477}
Fred Drake503d8d61998-04-13 18:45:18 +0000478
479
Guido van Rossum3d602e31996-07-21 02:33:56 +0000480/* err_string(char* message)
481 *
482 * Sets the error string for an exception of type ParserError.
483 *
484 */
485static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000486err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000487{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000488 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000489}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000490
491
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000492/* PyObject* parser_do_parse(PyObject* args, int type)
493 *
494 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000495 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000496 *
497 */
498static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000499parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000500{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000501 char* string = 0;
502 PyObject* res = 0;
503
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000504 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000505
506 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000507 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000508 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000509 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000510
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000511 if (n)
512 res = parser_newstobject(n, type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000513 }
514 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000515}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000516
517
518/* PyObject* parser_expr(PyObject* self, PyObject* args)
519 * PyObject* parser_suite(PyObject* self, PyObject* args)
520 *
521 * External interfaces to the parser itself. Which is called determines if
522 * the parser attempts to recognize an expression ('eval' form) or statement
523 * suite ('exec' form). The real work is done by parser_do_parse() above.
524 *
525 */
526static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000527parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000528{
Fred Drake268397f1998-04-29 14:16:32 +0000529 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000530 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000531}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000532
533
534static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000535parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000536{
Fred Drake268397f1998-04-29 14:16:32 +0000537 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000538 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000539}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000540
541
542
Fred Drakec2683dd2001-07-17 19:32:05 +0000543/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000544 * object requires that the input tuple be valid without having to rely on
545 * catching an exception from the compiler. This is done to allow the
546 * compiler itself to remain fast, since most of its input will come from
547 * the parser directly, and therefore be known to be syntactically correct.
548 * This validation is done to ensure that we don't core dump the compile
549 * phase, returning an exception instead.
550 *
551 * Two aspects can be broken out in this code: creating a node tree from
552 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000553 * advantageous to expand the number of ST types to include funcdefs and
554 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000555 * here. They are not necessary, and not quite as useful in a raw form.
556 * For now, let's get expressions and suites working reliably.
557 */
558
559
Jeremy Hylton938ace62002-07-17 16:30:39 +0000560static node* build_node_tree(PyObject *tuple);
561static int validate_expr_tree(node *tree);
562static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000563static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000564
Fred Drakec2683dd2001-07-17 19:32:05 +0000565/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000566 *
567 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000568 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000569 * tuple can be validated. It does this by checking the first code of the
570 * tuple, and, if acceptable, builds the internal representation. If this
571 * step succeeds, the internal representation is validated as fully as
572 * possible with the various validate_*() routines defined below.
573 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000574 * This function must be changed if support is to be added for PyST_FRAGMENT
575 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000576 *
577 */
578static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000579parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000580{
Fred Drake268397f1998-04-29 14:16:32 +0000581 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000582 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000583 PyObject *tuple;
584 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000585
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000586 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000587
Fred Drakec2683dd2001-07-17 19:32:05 +0000588 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000589 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000590 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000591 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000592 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000593 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000594 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000595 }
596 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000597 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000598 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000599 tree = build_node_tree(tuple);
600 if (tree != 0) {
601 int start_sym = TYPE(tree);
602 if (start_sym == eval_input) {
603 /* Might be an eval form. */
604 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000605 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000606 else
607 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000608 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000609 else if (start_sym == file_input) {
610 /* This looks like an exec form so far. */
611 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000612 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000613 else
614 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000615 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000616 else if (start_sym == encoding_decl) {
617 /* This looks like an encoding_decl so far. */
618 if (validate_encoding_decl(tree))
619 st = parser_newstobject(tree, PyST_SUITE);
620 else
621 PyNode_Free(tree);
622 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000623 else {
624 /* This is a fragment, at best. */
625 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000626 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000627 }
Guido van Rossum47478871996-08-21 14:32:37 +0000628 }
Guido van Rossum47478871996-08-21 14:32:37 +0000629 /* Make sure we throw an exception on all errors. We should never
630 * get this, but we'd do well to be sure something is done.
631 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000632 if (st == NULL && !PyErr_Occurred())
633 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000634
Fred Drakec2683dd2001-07-17 19:32:05 +0000635 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000636}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000637
638
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000639/* node* build_node_children()
640 *
641 * Iterate across the children of the current non-terminal node and build
642 * their structures. If successful, return the root of this portion of
643 * the tree, otherwise, 0. Any required exception will be specified already,
644 * and no memory will have been deallocated.
645 *
646 */
647static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000648build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000649{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000650 Py_ssize_t len = PyObject_Size(tuple);
651 Py_ssize_t i;
652 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000653
654 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000655 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000656 PyObject* elem = PySequence_GetItem(tuple, i);
657 int ok = elem != NULL;
658 long type = 0;
659 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000660
Fred Drakeff9ea482000-04-19 13:54:15 +0000661 if (ok)
662 ok = PySequence_Check(elem);
663 if (ok) {
664 PyObject *temp = PySequence_GetItem(elem, 0);
665 if (temp == NULL)
666 ok = 0;
667 else {
668 ok = PyInt_Check(temp);
669 if (ok)
670 type = PyInt_AS_LONG(temp);
671 Py_DECREF(temp);
672 }
673 }
674 if (!ok) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675 PyObject *err = Py_BuildValue("os", elem,
676 "Illegal node construct.");
677 PyErr_SetObject(parser_error, err);
678 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000679 Py_XDECREF(elem);
680 return (0);
681 }
682 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000683 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000684 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000685
Fred Drake0ac9b072000-09-12 21:58:06 +0000686 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000687 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000688 return 0;
689 }
690 temp = PySequence_GetItem(elem, 1);
691 if (temp == NULL)
692 return 0;
693 if (!PyString_Check(temp)) {
694 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000695 "second item in terminal node must be a string,"
696 " found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000697 temp->ob_type->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000698 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000699 return 0;
700 }
701 if (len == 3) {
702 PyObject *o = PySequence_GetItem(elem, 2);
703 if (o != NULL) {
704 if (PyInt_Check(o))
705 *line_num = PyInt_AS_LONG(o);
706 else {
707 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000708 "third item in terminal node must be an"
709 " integer, found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000710 temp->ob_type->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000711 Py_DECREF(o);
712 Py_DECREF(temp);
713 return 0;
714 }
715 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000716 }
717 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000718 len = PyString_GET_SIZE(temp) + 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000719 strn = (char *)PyObject_MALLOC(len);
Fred Drake0ac9b072000-09-12 21:58:06 +0000720 if (strn != NULL)
721 (void) memcpy(strn, PyString_AS_STRING(temp), len);
722 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000723 }
724 else if (!ISNONTERMINAL(type)) {
725 /*
726 * It has to be one or the other; this is an error.
727 * Throw an exception.
728 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000729 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
730 PyErr_SetObject(parser_error, err);
731 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000732 Py_XDECREF(elem);
733 return (0);
734 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000735 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000736 if (err == E_NOMEM) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000737 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000738 return (node *) PyErr_NoMemory();
739 }
740 if (err == E_OVERFLOW) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000741 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000742 PyErr_SetString(PyExc_ValueError,
743 "unsupported number of child nodes");
744 return NULL;
745 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000746
Fred Drakeff9ea482000-04-19 13:54:15 +0000747 if (ISNONTERMINAL(type)) {
748 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000749
Fred Drakeff9ea482000-04-19 13:54:15 +0000750 if (new_child != build_node_children(elem, new_child, line_num)) {
751 Py_XDECREF(elem);
752 return (0);
753 }
754 }
755 else if (type == NEWLINE) { /* It's true: we increment the */
756 ++(*line_num); /* line number *after* the newline! */
757 }
758 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000759 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000760 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000761}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000762
763
764static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000765build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000766{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000767 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000768 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000769 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000770
Guido van Rossum47478871996-08-21 14:32:37 +0000771 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000772 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000773 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000774 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000775 /*
776 * The tuple is simple, but it doesn't start with a start symbol.
777 * Throw an exception now and be done with it.
778 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000779 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000780 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000781 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000782 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000783 }
784 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000785 /*
786 * Not efficient, but that can be handled later.
787 */
788 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000789 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000790
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000791 if (num == encoding_decl) {
792 encoding = PySequence_GetItem(tuple, 2);
793 /* tuple isn't borrowed anymore here, need to DECREF */
794 tuple = PySequence_GetSlice(tuple, 0, 2);
795 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000796 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000797 if (res != NULL) {
798 if (res != build_node_children(tuple, res, &line_num)) {
799 PyNode_Free(res);
800 res = NULL;
801 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000802 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000803 Py_ssize_t len;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000804 len = PyString_GET_SIZE(encoding) + 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000805 res->n_str = (char *)PyObject_MALLOC(len);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000806 if (res->n_str != NULL)
807 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
808 Py_DECREF(encoding);
809 Py_DECREF(tuple);
810 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000811 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000812 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000813 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000814 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000815 * NONTERMINAL, we can't use it. Not sure the implementation
816 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000817 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000818 PyObject *err = Py_BuildValue("os", tuple,
819 "Illegal component tuple.");
820 PyErr_SetObject(parser_error, err);
821 Py_XDECREF(err);
822 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000823
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000824 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000825}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000826
827
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000828/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000829 * Validation routines used within the validation section:
830 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000831static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000832
Fred Drakeff9ea482000-04-19 13:54:15 +0000833#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
834#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
835#define validate_colon(ch) validate_terminal(ch, COLON, ":")
836#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
837#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
838#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
839#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
840#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
841#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
842#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
843#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
844#define validate_star(ch) validate_terminal(ch, STAR, "*")
845#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
846#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
847#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000848#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000849#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000850
Fred Drake0ac9b072000-09-12 21:58:06 +0000851#define VALIDATER(n) static int validate_##n(node *tree)
852
Fred Drakeff9ea482000-04-19 13:54:15 +0000853VALIDATER(node); VALIDATER(small_stmt);
854VALIDATER(class); VALIDATER(node);
855VALIDATER(parameters); VALIDATER(suite);
856VALIDATER(testlist); VALIDATER(varargslist);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000857VALIDATER(vfpdef);
Fred Drakeff9ea482000-04-19 13:54:15 +0000858VALIDATER(stmt); VALIDATER(simple_stmt);
859VALIDATER(expr_stmt); VALIDATER(power);
Guido van Rossum452bf512007-02-09 05:32:43 +0000860VALIDATER(del_stmt);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000861VALIDATER(return_stmt); VALIDATER(raise_stmt);
862VALIDATER(import_stmt); VALIDATER(import_stmt);
863VALIDATER(import_name); VALIDATER(yield_stmt);
864VALIDATER(global_stmt); VALIDATER(assert_stmt);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000865VALIDATER(compound_stmt);
Fred Drakeff9ea482000-04-19 13:54:15 +0000866VALIDATER(while); VALIDATER(for);
867VALIDATER(try); VALIDATER(except_clause);
868VALIDATER(test); VALIDATER(and_test);
869VALIDATER(not_test); VALIDATER(comparison);
Guido van Rossum0368b722007-05-11 16:50:42 +0000870VALIDATER(comp_op);
871VALIDATER(star_expr); VALIDATER(expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000872VALIDATER(xor_expr); VALIDATER(and_expr);
873VALIDATER(shift_expr); VALIDATER(arith_expr);
874VALIDATER(term); VALIDATER(factor);
875VALIDATER(atom); VALIDATER(lambdef);
876VALIDATER(trailer); VALIDATER(subscript);
877VALIDATER(subscriptlist); VALIDATER(sliceop);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000878VALIDATER(exprlist); VALIDATER(dictorsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000879VALIDATER(arglist); VALIDATER(argument);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000880VALIDATER(testlist1); VALIDATER(comp_for);
881VALIDATER(comp_iter); VALIDATER(comp_if);
882VALIDATER(testlist_comp); VALIDATER(yield_expr);
883VALIDATER(yield_or_testlist); VALIDATER(or_test);
884VALIDATER(test_nocond); VALIDATER(lambdef_nocond);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000885
Fred Drake0ac9b072000-09-12 21:58:06 +0000886#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000887
Fred Drakeff9ea482000-04-19 13:54:15 +0000888#define is_even(n) (((n) & 1) == 0)
889#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000890
891
892static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000893validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000894{
Fred Drake0ac9b072000-09-12 21:58:06 +0000895 if (TYPE(n) != t) {
896 PyErr_Format(parser_error, "Expected node type %d, got %d.",
897 t, TYPE(n));
898 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000899 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000900 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000901}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000902
903
Fred Drakee7ab64e2000-04-25 04:14:46 +0000904/* Verifies that the number of child nodes is exactly 'num', raising
905 * an exception if it isn't. The exception message does not indicate
906 * the exact number of nodes, allowing this to be used to raise the
907 * "right" exception when the wrong number of nodes is present in a
908 * specific variant of a statement's syntax. This is commonly used
909 * in that fashion.
910 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000911static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000912validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000913{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000914 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000915 PyErr_Format(parser_error,
916 "Illegal number of children for %s node.", name);
917 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000918 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000919 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000920}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000921
922
923static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000924validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000925{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000926 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000927 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000928
929 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000930 PyErr_Format(parser_error,
931 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000932 }
933 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000934}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000935
936
Guido van Rossum47478871996-08-21 14:32:37 +0000937/* X (',' X) [',']
938 */
939static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000940validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000941 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000942{
943 int nch = NCH(tree);
944 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000945 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000946
947 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000948 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000949 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000950 if (is_even(nch))
951 res = validate_comma(CHILD(tree, --nch));
952 if (res && nch > 1) {
953 int pos = 1;
954 for ( ; res && pos < nch; pos += 2)
955 res = (validate_comma(CHILD(tree, pos))
956 && vfunc(CHILD(tree, pos + 1)));
957 }
Guido van Rossum47478871996-08-21 14:32:37 +0000958 }
959 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000960}
Guido van Rossum47478871996-08-21 14:32:37 +0000961
962
Fred Drakecff283c2000-08-21 22:24:43 +0000963/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000964 *
965 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000966 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000967 */
Guido van Rossum47478871996-08-21 14:32:37 +0000968static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000969validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000970{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000971 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +0000972 int res = (validate_ntype(tree, classdef) &&
973 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000974
Guido van Rossum3d602e31996-07-21 02:33:56 +0000975 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000976 res = (validate_name(CHILD(tree, 0), "class")
977 && validate_ntype(CHILD(tree, 1), NAME)
978 && validate_colon(CHILD(tree, nch - 2))
979 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000980 }
Brett Cannonf4189912005-04-09 02:30:16 +0000981 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000982 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +0000983 }
984
985 if (res) {
986 if (nch == 7) {
987 res = ((validate_lparen(CHILD(tree, 2)) &&
988 validate_testlist(CHILD(tree, 3)) &&
989 validate_rparen(CHILD(tree, 4))));
990 }
991 else if (nch == 6) {
992 res = (validate_lparen(CHILD(tree,2)) &&
993 validate_rparen(CHILD(tree,3)));
994 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000995 }
996 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000997}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000998
999
Guido van Rossum3d602e31996-07-21 02:33:56 +00001000/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001001 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001002 */
Guido van Rossum47478871996-08-21 14:32:37 +00001003static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001004validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001005{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001006 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001007 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001008 && (nch >= 4)
1009 && validate_name(CHILD(tree, 0), "if")
1010 && validate_test(CHILD(tree, 1))
1011 && validate_colon(CHILD(tree, 2))
1012 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001013
1014 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001015 /* ... 'else' ':' suite */
1016 res = (validate_name(CHILD(tree, nch - 3), "else")
1017 && validate_colon(CHILD(tree, nch - 2))
1018 && validate_suite(CHILD(tree, nch - 1)));
1019 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001020 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001021 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001022 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001023 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001024 /* Will catch the case for nch < 4 */
1025 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001026 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001027 /* ... ('elif' test ':' suite)+ ... */
1028 int j = 4;
1029 while ((j < nch) && res) {
1030 res = (validate_name(CHILD(tree, j), "elif")
1031 && validate_colon(CHILD(tree, j + 2))
1032 && validate_test(CHILD(tree, j + 1))
1033 && validate_suite(CHILD(tree, j + 3)));
1034 j += 4;
1035 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001036 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001037 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001038}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001039
1040
Guido van Rossum3d602e31996-07-21 02:33:56 +00001041/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001042 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001043 *
1044 */
Guido van Rossum47478871996-08-21 14:32:37 +00001045static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001046validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001047{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001048 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001049 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001050
Guido van Rossum3d602e31996-07-21 02:33:56 +00001051 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001052 res = (validate_lparen(CHILD(tree, 0))
1053 && validate_rparen(CHILD(tree, nch - 1)));
1054 if (res && (nch == 3))
1055 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001056 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001057 else {
1058 (void) validate_numnodes(tree, 2, "parameters");
1059 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001060 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001061}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001062
1063
Fred Drakecff283c2000-08-21 22:24:43 +00001064/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001065 *
1066 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001067 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001068 * | NEWLINE INDENT stmt+ DEDENT
1069 */
Guido van Rossum47478871996-08-21 14:32:37 +00001070static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001071validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001072{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001073 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001074 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001075
Guido van Rossum3d602e31996-07-21 02:33:56 +00001076 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001077 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001078 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001079 /* NEWLINE INDENT stmt+ DEDENT */
1080 res = (validate_newline(CHILD(tree, 0))
1081 && validate_indent(CHILD(tree, 1))
1082 && validate_stmt(CHILD(tree, 2))
1083 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001084
Fred Drakeff9ea482000-04-19 13:54:15 +00001085 if (res && (nch > 4)) {
1086 int i = 3;
1087 --nch; /* forget the DEDENT */
1088 for ( ; res && (i < nch); ++i)
1089 res = validate_stmt(CHILD(tree, i));
1090 }
1091 else if (nch < 4)
1092 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001093 }
1094 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001095}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001096
1097
Guido van Rossum47478871996-08-21 14:32:37 +00001098static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001099validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001100{
Guido van Rossum47478871996-08-21 14:32:37 +00001101 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001102 validate_test, "testlist"));
1103}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001104
1105
Guido van Rossum1c917072001-10-15 15:44:05 +00001106static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001107validate_testlist1(node *tree)
1108{
1109 return (validate_repeating_list(tree, testlist1,
1110 validate_test, "testlist1"));
1111}
1112
1113
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001114/* validate either vfpdef or tfpdef.
1115 * vfpdef: NAME
1116 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001117 */
1118static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001119validate_vfpdef(node *tree)
Neal Norwitzc1505362006-12-28 06:47:50 +00001120{
1121 int nch = NCH(tree);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001122 if (TYPE(tree) == vfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001123 return nch == 1 && validate_name(CHILD(tree, 0), NULL);
1124 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001125 else if (TYPE(tree) == tfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001126 if (nch == 1) {
1127 return validate_name(CHILD(tree, 0), NULL);
1128 }
1129 else if (nch == 3) {
1130 return validate_name(CHILD(tree, 0), NULL) &&
1131 validate_colon(CHILD(tree, 1)) &&
1132 validate_test(CHILD(tree, 2));
1133 }
1134 }
1135 return 0;
1136}
1137
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001138/* '*' vfpdef (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
1139 * ..or tfpdef in place of vfpdef. vfpdef: NAME; tfpdef: NAME [':' test]
Fred Drakecff283c2000-08-21 22:24:43 +00001140 */
1141static int
1142validate_varargslist_trailer(node *tree, int start)
1143{
1144 int nch = NCH(tree);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001145 int res = 0, i;
Fred Drakecff283c2000-08-21 22:24:43 +00001146 int sym;
1147
1148 if (nch <= start) {
1149 err_string("expected variable argument trailer for varargslist");
1150 return 0;
1151 }
1152 sym = TYPE(CHILD(tree, start));
1153 if (sym == STAR) {
1154 /*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001155 * '*' vfpdef (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
Fred Drakecff283c2000-08-21 22:24:43 +00001156 */
1157 if (nch-start == 2)
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001158 res = validate_vfpdef(CHILD(tree, start+1));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001159 else if (nch-start == 5 && TYPE(CHILD(tree, start+2)) == COMMA)
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001160 res = (validate_vfpdef(CHILD(tree, start+1))
Fred Drakecff283c2000-08-21 22:24:43 +00001161 && validate_comma(CHILD(tree, start+2))
1162 && validate_doublestar(CHILD(tree, start+3))
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001163 && validate_vfpdef(CHILD(tree, start+4)));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001164 else {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001165 /* skip over vfpdef (',' vfpdef ['=' test])* */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001166 i = start + 1;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001167 if (TYPE(CHILD(tree, i)) == vfpdef ||
1168 TYPE(CHILD(tree, i)) == tfpdef) { /* skip over vfpdef or tfpdef */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001169 i += 1;
1170 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001171 while (res && i+1 < nch) { /* validate (',' vfpdef ['=' test])* */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001172 res = validate_comma(CHILD(tree, i));
1173 if (TYPE(CHILD(tree, i+1)) == DOUBLESTAR)
1174 break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001175 res = res && validate_vfpdef(CHILD(tree, i+1));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001176 if (res && i+2 < nch && TYPE(CHILD(tree, i+2)) == EQUAL) {
1177 res = res && (i+3 < nch)
1178 && validate_test(CHILD(tree, i+3));
1179 i += 4;
1180 }
1181 else {
1182 i += 2;
1183 }
1184 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001185 /* [',' '**' vfpdef] */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001186 if (res && i+1 < nch && TYPE(CHILD(tree, i+1)) == DOUBLESTAR) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001187 res = validate_vfpdef(CHILD(tree, i+2));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001188 }
1189 }
Fred Drakecff283c2000-08-21 22:24:43 +00001190 }
1191 else if (sym == DOUBLESTAR) {
1192 /*
1193 * '**' NAME
1194 */
1195 if (nch-start == 2)
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001196 res = validate_vfpdef(CHILD(tree, start+1));
Fred Drakecff283c2000-08-21 22:24:43 +00001197 }
1198 if (!res)
1199 err_string("illegal variable argument trailer for varargslist");
1200 return res;
1201}
1202
1203
Neal Norwitzc1505362006-12-28 06:47:50 +00001204/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001205 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001206 * Validate typedargslist or varargslist.
1207 *
1208 * typedargslist: ((tfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001209 * ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
1210 * '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001211 * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001212 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001213 * varargslist: ((vfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001214 * ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
1215 * '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001216 * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001217 * vfpdef: NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001218 *
1219 */
Guido van Rossum47478871996-08-21 14:32:37 +00001220static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001221validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001222{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001223 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001224 int res = (TYPE(tree) == varargslist ||
1225 TYPE(tree) == typedargslist) &&
1226 (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001227 int sym;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001228 node *ch;
1229 int i = 0;
1230
Fred Drakeb6429a22000-12-11 22:08:27 +00001231 if (!res)
1232 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001233 if (nch < 1) {
1234 err_string("varargslist missing child nodes");
1235 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001236 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001237 while (i < nch) {
1238 ch = CHILD(tree, i);
1239 sym = TYPE(ch);
1240 if (sym == vfpdef || sym == tfpdef) {
1241 /* validate (vfpdef ['=' test] ',')+ */
1242 res = validate_vfpdef(ch);
Fred Drakecff283c2000-08-21 22:24:43 +00001243 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001244 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1245 res = (validate_equal(CHILD(tree, i))
1246 && validate_test(CHILD(tree, i+1)));
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001247 if (res)
1248 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001249 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001250 if (res && i < nch) {
1251 res = validate_comma(CHILD(tree, i));
1252 ++i;
Fred Drakecff283c2000-08-21 22:24:43 +00001253 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001254 } else if (sym == DOUBLESTAR || sym == STAR) {
1255 res = validate_varargslist_trailer(tree, i);
1256 break;
1257 } else {
1258 res = 0;
1259 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001260 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001261 }
Fred Drakecff283c2000-08-21 22:24:43 +00001262 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001263}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001264
1265
Nick Coghlan650f0d02007-04-15 12:05:43 +00001266/* comp_iter: comp_for | comp_if
Fred Drakecff283c2000-08-21 22:24:43 +00001267 */
1268static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001269validate_comp_iter(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001270{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001271 int res = (validate_ntype(tree, comp_iter)
1272 && validate_numnodes(tree, 1, "comp_iter"));
1273 if (res && TYPE(CHILD(tree, 0)) == comp_for)
1274 res = validate_comp_for(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001275 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001276 res = validate_comp_if(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001277
1278 return res;
1279}
1280
Nick Coghlan650f0d02007-04-15 12:05:43 +00001281/* comp_for: 'for' exprlist 'in' test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001282 */
1283static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001284validate_comp_for(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001285{
1286 int nch = NCH(tree);
1287 int res;
1288
1289 if (nch == 5)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001290 res = validate_comp_iter(CHILD(tree, 4));
Fred Drakecff283c2000-08-21 22:24:43 +00001291 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001292 res = validate_numnodes(tree, 4, "comp_for");
Raymond Hettinger354433a2004-05-19 08:20:33 +00001293
1294 if (res)
1295 res = (validate_name(CHILD(tree, 0), "for")
1296 && validate_exprlist(CHILD(tree, 1))
1297 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001298 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001299
1300 return res;
1301}
1302
Nick Coghlan650f0d02007-04-15 12:05:43 +00001303/* comp_if: 'if' test_nocond [comp_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001304 */
1305static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001306validate_comp_if(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001307{
1308 int nch = NCH(tree);
1309 int res;
1310
1311 if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001312 res = validate_comp_iter(CHILD(tree, 2));
Fred Drakecff283c2000-08-21 22:24:43 +00001313 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001314 res = validate_numnodes(tree, 2, "comp_if");
Fred Drakecff283c2000-08-21 22:24:43 +00001315
1316 if (res)
1317 res = (validate_name(CHILD(tree, 0), "if")
Nick Coghlan650f0d02007-04-15 12:05:43 +00001318 && validate_test_nocond(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001319
1320 return res;
1321}
1322
1323
Guido van Rossum3d602e31996-07-21 02:33:56 +00001324/* simple_stmt | compound_stmt
1325 *
1326 */
Guido van Rossum47478871996-08-21 14:32:37 +00001327static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001328validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001329{
1330 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001331 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001332
Guido van Rossum3d602e31996-07-21 02:33:56 +00001333 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001334 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001335
Fred Drakeff9ea482000-04-19 13:54:15 +00001336 if (TYPE(tree) == simple_stmt)
1337 res = validate_simple_stmt(tree);
1338 else
1339 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001340 }
1341 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001342}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001343
1344
Guido van Rossum3d602e31996-07-21 02:33:56 +00001345/* small_stmt (';' small_stmt)* [';'] NEWLINE
1346 *
1347 */
Guido van Rossum47478871996-08-21 14:32:37 +00001348static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001349validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001350{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001351 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001352 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001353 && (nch >= 2)
1354 && validate_small_stmt(CHILD(tree, 0))
1355 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001356
Guido van Rossum3d602e31996-07-21 02:33:56 +00001357 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001358 res = validate_numnodes(tree, 2, "simple_stmt");
1359 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001360 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001361 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001362 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001363 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001364
Fred Drakeff9ea482000-04-19 13:54:15 +00001365 for (i = 1; res && (i < nch); i += 2)
1366 res = (validate_semi(CHILD(tree, i))
1367 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001368 }
1369 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001370}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001371
1372
Guido van Rossum47478871996-08-21 14:32:37 +00001373static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001374validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001375{
1376 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001377 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001378
Fred Drake0ac9b072000-09-12 21:58:06 +00001379 if (res) {
1380 int ntype = TYPE(CHILD(tree, 0));
1381
1382 if ( (ntype == expr_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001383 || (ntype == del_stmt)
1384 || (ntype == pass_stmt)
1385 || (ntype == flow_stmt)
1386 || (ntype == import_stmt)
1387 || (ntype == global_stmt)
Georg Brandl7cae87c2006-09-06 06:51:57 +00001388 || (ntype == assert_stmt))
Fred Drake0ac9b072000-09-12 21:58:06 +00001389 res = validate_node(CHILD(tree, 0));
1390 else {
1391 res = 0;
1392 err_string("illegal small_stmt child type");
1393 }
1394 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001395 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001396 res = 0;
1397 PyErr_Format(parser_error,
1398 "Unrecognized child node of small_stmt: %d.",
1399 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001400 }
1401 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001402}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001403
1404
1405/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001406 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001407 */
Guido van Rossum47478871996-08-21 14:32:37 +00001408static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001409validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001410{
1411 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001412 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001413 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001414
1415 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001416 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001417
1418 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001419 ntype = TYPE(tree);
1420 if ( (ntype == if_stmt)
1421 || (ntype == while_stmt)
1422 || (ntype == for_stmt)
1423 || (ntype == try_stmt)
1424 || (ntype == funcdef)
1425 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001426 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001427 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001428 res = 0;
1429 PyErr_Format(parser_error,
1430 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001431 }
1432 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001433}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001434
1435
Guido van Rossum47478871996-08-21 14:32:37 +00001436static int
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001437validate_yield_or_testlist(node *tree)
1438{
1439 if (TYPE(tree) == yield_expr)
1440 return validate_yield_expr(tree);
1441 else
1442 return validate_testlist(tree);
1443}
1444
1445static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001446validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001447{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001448 int j;
1449 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001450 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001451 && is_odd(nch)
1452 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001453
Fred Drake28f739a2000-08-25 22:42:40 +00001454 if (res && nch == 3
1455 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001456 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1457 && validate_yield_or_testlist(CHILD(tree, 2));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001458
Fred Drake28f739a2000-08-25 22:42:40 +00001459 if (res) {
1460 char *s = STR(CHILD(CHILD(tree, 1), 0));
1461
1462 res = (strcmp(s, "+=") == 0
1463 || strcmp(s, "-=") == 0
1464 || strcmp(s, "*=") == 0
1465 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001466 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001467 || strcmp(s, "%=") == 0
1468 || strcmp(s, "&=") == 0
1469 || strcmp(s, "|=") == 0
1470 || strcmp(s, "^=") == 0
1471 || strcmp(s, "<<=") == 0
1472 || strcmp(s, ">>=") == 0
1473 || strcmp(s, "**=") == 0);
1474 if (!res)
1475 err_string("illegal augmmented assignment operator");
1476 }
1477 }
1478 else {
1479 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001480 res = validate_equal(CHILD(tree, j))
1481 && validate_yield_or_testlist(CHILD(tree, j + 1));
Fred Drake28f739a2000-08-25 22:42:40 +00001482 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001483 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001484}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001485
1486
Guido van Rossum47478871996-08-21 14:32:37 +00001487static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001488validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001489{
1490 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001491 && validate_name(CHILD(tree, 0), "del")
1492 && validate_exprlist(CHILD(tree, 1)));
1493}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001494
1495
Guido van Rossum47478871996-08-21 14:32:37 +00001496static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001497validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001498{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001499 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001500 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001501 && ((nch == 1) || (nch == 2))
1502 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001503
Guido van Rossum3d602e31996-07-21 02:33:56 +00001504 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001505 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001506
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001507 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001508}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001509
1510
Guido van Rossum47478871996-08-21 14:32:37 +00001511static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001512validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001513{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001514 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001515 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001516 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001517
Guido van Rossum3d602e31996-07-21 02:33:56 +00001518 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001519 res = validate_name(CHILD(tree, 0), "raise");
1520 if (res && (nch >= 2))
1521 res = validate_test(CHILD(tree, 1));
1522 if (res && nch > 2) {
1523 res = (validate_comma(CHILD(tree, 2))
1524 && validate_test(CHILD(tree, 3)));
1525 if (res && (nch > 4))
1526 res = (validate_comma(CHILD(tree, 4))
1527 && validate_test(CHILD(tree, 5)));
1528 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001529 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001530 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001531 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001532 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001533 res = (validate_comma(CHILD(tree, 2))
1534 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001535
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001536 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001537}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001538
1539
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001540/* yield_expr: 'yield' [testlist]
1541 */
1542static int
1543validate_yield_expr(node *tree)
1544{
1545 int nch = NCH(tree);
1546 int res = (validate_ntype(tree, yield_expr)
1547 && ((nch == 1) || (nch == 2))
1548 && validate_name(CHILD(tree, 0), "yield"));
1549
1550 if (res && (nch == 2))
1551 res = validate_testlist(CHILD(tree, 1));
1552
1553 return (res);
1554}
1555
1556
1557/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001558 */
1559static int
1560validate_yield_stmt(node *tree)
1561{
1562 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001563 && validate_numnodes(tree, 1, "yield_stmt")
1564 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001565}
1566
1567
Fred Drakecff283c2000-08-21 22:24:43 +00001568static int
1569validate_import_as_name(node *tree)
1570{
1571 int nch = NCH(tree);
1572 int ok = validate_ntype(tree, import_as_name);
1573
1574 if (ok) {
1575 if (nch == 1)
1576 ok = validate_name(CHILD(tree, 0), NULL);
1577 else if (nch == 3)
1578 ok = (validate_name(CHILD(tree, 0), NULL)
1579 && validate_name(CHILD(tree, 1), "as")
1580 && validate_name(CHILD(tree, 2), NULL));
1581 else
1582 ok = validate_numnodes(tree, 3, "import_as_name");
1583 }
1584 return ok;
1585}
1586
1587
Fred Drake71137082001-01-07 05:59:59 +00001588/* dotted_name: NAME ("." NAME)*
1589 */
1590static int
1591validate_dotted_name(node *tree)
1592{
1593 int nch = NCH(tree);
1594 int res = (validate_ntype(tree, dotted_name)
1595 && is_odd(nch)
1596 && validate_name(CHILD(tree, 0), NULL));
1597 int i;
1598
1599 for (i = 1; res && (i < nch); i += 2) {
1600 res = (validate_dot(CHILD(tree, i))
1601 && validate_name(CHILD(tree, i+1), NULL));
1602 }
1603 return res;
1604}
1605
1606
Fred Drakecff283c2000-08-21 22:24:43 +00001607/* dotted_as_name: dotted_name [NAME NAME]
1608 */
1609static int
1610validate_dotted_as_name(node *tree)
1611{
1612 int nch = NCH(tree);
1613 int res = validate_ntype(tree, dotted_as_name);
1614
1615 if (res) {
1616 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001617 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001618 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001619 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001620 && validate_name(CHILD(tree, 1), "as")
1621 && validate_name(CHILD(tree, 2), NULL));
1622 else {
1623 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001624 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001625 }
1626 }
1627 return res;
1628}
1629
1630
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001631/* dotted_as_name (',' dotted_as_name)* */
1632static int
1633validate_dotted_as_names(node *tree)
1634{
1635 int nch = NCH(tree);
1636 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1637 int i;
1638
1639 for (i = 1; res && (i < nch); i += 2)
1640 res = (validate_comma(CHILD(tree, i))
1641 && validate_dotted_as_name(CHILD(tree, i + 1)));
1642 return (res);
1643}
1644
1645
1646/* import_as_name (',' import_as_name)* [','] */
1647static int
1648validate_import_as_names(node *tree)
1649{
1650 int nch = NCH(tree);
1651 int res = validate_import_as_name(CHILD(tree, 0));
1652 int i;
1653
1654 for (i = 1; res && (i + 1 < nch); i += 2)
1655 res = (validate_comma(CHILD(tree, i))
1656 && validate_import_as_name(CHILD(tree, i + 1)));
1657 return (res);
1658}
1659
1660
1661/* 'import' dotted_as_names */
1662static int
1663validate_import_name(node *tree)
1664{
1665 return (validate_ntype(tree, import_name)
1666 && validate_numnodes(tree, 2, "import_name")
1667 && validate_name(CHILD(tree, 0), "import")
1668 && validate_dotted_as_names(CHILD(tree, 1)));
1669}
1670
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001671/* Helper function to count the number of leading dots in
1672 * 'from ...module import name'
1673 */
1674static int
1675count_from_dots(node *tree)
1676{
1677 int i;
1678 for (i = 0; i < NCH(tree); i++)
1679 if (TYPE(CHILD(tree, i)) != DOT)
1680 break;
1681 return i;
1682}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001683
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001684/* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001685 * import_as_names
Guido van Rossum3d602e31996-07-21 02:33:56 +00001686 */
Guido van Rossum47478871996-08-21 14:32:37 +00001687static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001688validate_import_from(node *tree)
1689{
1690 int nch = NCH(tree);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001691 int ndots = count_from_dots(tree);
1692 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1693 int offset = ndots + havename;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001694 int res = validate_ntype(tree, import_from)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001695 && (nch >= 4 + ndots)
1696 && validate_name(CHILD(tree, 0), "from")
1697 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1698 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001699
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001700 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1701 res = ((nch == offset + 5)
1702 && validate_lparen(CHILD(tree, offset + 2))
1703 && validate_import_as_names(CHILD(tree, offset + 3))
1704 && validate_rparen(CHILD(tree, offset + 4)));
1705 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1706 res = validate_import_as_names(CHILD(tree, offset + 2));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001707 return (res);
1708}
1709
1710
1711/* import_stmt: import_name | import_from */
1712static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001713validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001714{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001715 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001716 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001717
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001718 if (res) {
1719 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001720
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001721 if (ntype == import_name || ntype == import_from)
1722 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001723 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001724 res = 0;
1725 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001726 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001727 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001728 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001729 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001730 PyErr_Format(parser_error,
1731 "Unrecognized child node of import_stmt: %d.",
1732 TYPE(CHILD(tree, 0)));
1733 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001734 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001735}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001736
1737
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001738
1739
Guido van Rossum47478871996-08-21 14:32:37 +00001740static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001741validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001742{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001743 int j;
1744 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001745 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001746 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001747
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001748 if (!res && !PyErr_Occurred())
1749 err_string("illegal global statement");
1750
Guido van Rossum3d602e31996-07-21 02:33:56 +00001751 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001752 res = (validate_name(CHILD(tree, 0), "global")
1753 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001754 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001755 res = (validate_comma(CHILD(tree, j))
1756 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001757
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001758 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001759}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001760
1761
Guido van Rossum925e5471997-04-02 05:32:13 +00001762/* assert_stmt:
1763 *
1764 * 'assert' test [',' test]
1765 */
1766static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001767validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001768{
1769 int nch = NCH(tree);
1770 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001771 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001772 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001773 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001774
1775 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001776 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001777 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001778 res = (validate_comma(CHILD(tree, 2))
1779 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001780
1781 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001782}
Guido van Rossum925e5471997-04-02 05:32:13 +00001783
1784
Guido van Rossum47478871996-08-21 14:32:37 +00001785static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001786validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001787{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001788 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001789 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001790 && ((nch == 4) || (nch == 7))
1791 && validate_name(CHILD(tree, 0), "while")
1792 && validate_test(CHILD(tree, 1))
1793 && validate_colon(CHILD(tree, 2))
1794 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001795
Guido van Rossum3d602e31996-07-21 02:33:56 +00001796 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001797 res = (validate_name(CHILD(tree, 4), "else")
1798 && validate_colon(CHILD(tree, 5))
1799 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001800
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001801 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001802}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001803
1804
Guido van Rossum47478871996-08-21 14:32:37 +00001805static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001806validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001807{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001808 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001809 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001810 && ((nch == 6) || (nch == 9))
1811 && validate_name(CHILD(tree, 0), "for")
1812 && validate_exprlist(CHILD(tree, 1))
1813 && validate_name(CHILD(tree, 2), "in")
1814 && validate_testlist(CHILD(tree, 3))
1815 && validate_colon(CHILD(tree, 4))
1816 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001817
Guido van Rossum3d602e31996-07-21 02:33:56 +00001818 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001819 res = (validate_name(CHILD(tree, 6), "else")
1820 && validate_colon(CHILD(tree, 7))
1821 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001822
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001823 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001824}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001825
1826
Guido van Rossum3d602e31996-07-21 02:33:56 +00001827/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001828 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001829 * | 'try' ':' suite 'finally' ':' suite
1830 *
1831 */
Guido van Rossum47478871996-08-21 14:32:37 +00001832static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001833validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001834{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001835 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001836 int pos = 3;
1837 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001838 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001839
1840 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001841 res = (validate_name(CHILD(tree, 0), "try")
1842 && validate_colon(CHILD(tree, 1))
1843 && validate_suite(CHILD(tree, 2))
1844 && validate_colon(CHILD(tree, nch - 2))
1845 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001846 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001847 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001848 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1849 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001850
1851 PyErr_Format(parser_error,
1852 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001853 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001854 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001855 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001856 res = (validate_except_clause(CHILD(tree, pos))
1857 && validate_colon(CHILD(tree, pos + 1))
1858 && validate_suite(CHILD(tree, pos + 2)));
1859 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001860 }
1861 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001862 res = validate_ntype(CHILD(tree, pos), NAME);
1863 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1864 res = (validate_numnodes(tree, 6, "try/finally")
1865 && validate_colon(CHILD(tree, 4))
1866 && validate_suite(CHILD(tree, 5)));
1867 else if (res) {
1868 if (nch == (pos + 3)) {
1869 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1870 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1871 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001872 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001873 }
1874 else if (nch == (pos + 6)) {
1875 res = (validate_name(CHILD(tree, pos), "except")
1876 && validate_colon(CHILD(tree, pos + 1))
1877 && validate_suite(CHILD(tree, pos + 2))
1878 && validate_name(CHILD(tree, pos + 3), "else"));
1879 }
1880 else
1881 res = validate_numnodes(tree, pos + 3, "try/except");
1882 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001883 }
1884 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001885}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001886
1887
Guido van Rossum47478871996-08-21 14:32:37 +00001888static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001889validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001890{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001891 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001892 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001893 && ((nch == 1) || (nch == 2) || (nch == 4))
1894 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001895
Guido van Rossum3d602e31996-07-21 02:33:56 +00001896 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001897 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001898 if (res && (nch == 4))
Guido van Rossumb940e112007-01-10 16:19:56 +00001899 res = (validate_name(CHILD(tree, 2), "as")
1900 && validate_ntype(CHILD(tree, 3), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001901
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001902 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001903}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001904
1905
Guido van Rossum47478871996-08-21 14:32:37 +00001906static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001907validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001908{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001909 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001910 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001911
Guido van Rossum3d602e31996-07-21 02:33:56 +00001912 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001913 res = ((nch == 1)
1914 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001915 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001916 res = validate_or_test(CHILD(tree, 0));
1917 res = (res && (nch == 1 || (nch == 5 &&
1918 validate_name(CHILD(tree, 1), "if") &&
1919 validate_or_test(CHILD(tree, 2)) &&
1920 validate_name(CHILD(tree, 3), "else") &&
1921 validate_test(CHILD(tree, 4)))));
1922 }
1923 return (res);
1924}
1925
1926static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001927validate_test_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001928{
1929 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001930 int res = validate_ntype(tree, test_nocond) && (nch == 1);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001931
Nick Coghlan650f0d02007-04-15 12:05:43 +00001932 if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
1933 res = (validate_lambdef_nocond(CHILD(tree, 0)));
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001934 else if (res) {
1935 res = (validate_or_test(CHILD(tree, 0)));
1936 }
1937 return (res);
1938}
1939
1940static int
1941validate_or_test(node *tree)
1942{
1943 int nch = NCH(tree);
1944 int res = validate_ntype(tree, or_test) && is_odd(nch);
1945
1946 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001947 int pos;
1948 res = validate_and_test(CHILD(tree, 0));
1949 for (pos = 1; res && (pos < nch); pos += 2)
1950 res = (validate_name(CHILD(tree, pos), "or")
1951 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001952 }
1953 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001954}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001955
1956
Guido van Rossum47478871996-08-21 14:32:37 +00001957static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001958validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001959{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001960 int pos;
1961 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001962 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001963 && is_odd(nch)
1964 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001965
Guido van Rossum3d602e31996-07-21 02:33:56 +00001966 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001967 res = (validate_name(CHILD(tree, pos), "and")
1968 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001969
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001970 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001971}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001972
1973
Guido van Rossum47478871996-08-21 14:32:37 +00001974static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001975validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001976{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001977 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001978 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001979
Guido van Rossum3d602e31996-07-21 02:33:56 +00001980 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001981 if (nch == 2)
1982 res = (validate_name(CHILD(tree, 0), "not")
1983 && validate_not_test(CHILD(tree, 1)));
1984 else if (nch == 1)
1985 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001986 }
1987 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001988}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001989
1990
Guido van Rossum47478871996-08-21 14:32:37 +00001991static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001992validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001993{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001994 int pos;
1995 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001996 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001997 && is_odd(nch)
Guido van Rossum0368b722007-05-11 16:50:42 +00001998 && validate_star_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001999
Guido van Rossum3d602e31996-07-21 02:33:56 +00002000 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002001 res = (validate_comp_op(CHILD(tree, pos))
Guido van Rossum0368b722007-05-11 16:50:42 +00002002 && validate_star_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002003
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002004 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002005}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002006
2007
Guido van Rossum47478871996-08-21 14:32:37 +00002008static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002009validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002010{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002011 int res = 0;
2012 int nch = NCH(tree);
2013
Guido van Rossum3d602e31996-07-21 02:33:56 +00002014 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002015 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002016 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002017 /*
2018 * Only child will be a terminal with a well-defined symbolic name
2019 * or a NAME with a string of either 'is' or 'in'
2020 */
2021 tree = CHILD(tree, 0);
2022 switch (TYPE(tree)) {
2023 case LESS:
2024 case GREATER:
2025 case EQEQUAL:
2026 case EQUAL:
2027 case LESSEQUAL:
2028 case GREATEREQUAL:
2029 case NOTEQUAL:
2030 res = 1;
2031 break;
2032 case NAME:
2033 res = ((strcmp(STR(tree), "in") == 0)
2034 || (strcmp(STR(tree), "is") == 0));
2035 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002036 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002037 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002038 }
2039 break;
2040 default:
Fred Drake661ea262000-10-24 19:57:45 +00002041 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002042 break;
2043 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002044 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002045 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002046 res = (validate_ntype(CHILD(tree, 0), NAME)
2047 && validate_ntype(CHILD(tree, 1), NAME)
2048 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2049 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2050 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2051 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2052 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002053 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002054 }
2055 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002056}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002057
2058
Guido van Rossum47478871996-08-21 14:32:37 +00002059static int
Guido van Rossum0368b722007-05-11 16:50:42 +00002060validate_star_expr(node *tree)
2061{
2062 int res = validate_ntype(tree, star_expr);
2063 if (!res) return res;
2064 if (NCH(tree) == 2) {
2065 return validate_ntype(CHILD(tree, 0), STAR) && \
2066 validate_expr(CHILD(tree, 1));
2067 } else {
2068 return validate_expr(CHILD(tree, 0));
2069 }
2070}
2071
2072
2073static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002074validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002075{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002076 int j;
2077 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002078 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002079 && is_odd(nch)
2080 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002081
Guido van Rossum3d602e31996-07-21 02:33:56 +00002082 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002083 res = (validate_xor_expr(CHILD(tree, j))
2084 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002085
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002086 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002087}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002088
2089
Guido van Rossum47478871996-08-21 14:32:37 +00002090static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002091validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002092{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002093 int j;
2094 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002095 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002096 && is_odd(nch)
2097 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002098
Guido van Rossum3d602e31996-07-21 02:33:56 +00002099 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002100 res = (validate_circumflex(CHILD(tree, j - 1))
2101 && validate_and_expr(CHILD(tree, j)));
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_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002109{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002110 int pos;
2111 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002112 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002113 && is_odd(nch)
2114 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002115
Guido van Rossum3d602e31996-07-21 02:33:56 +00002116 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002117 res = (validate_ampersand(CHILD(tree, pos))
2118 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002119
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002120 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002121}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002122
2123
2124static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002125validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002126 {
2127 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002128 int nch = NCH(tree);
2129 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002130 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002131
Guido van Rossum3d602e31996-07-21 02:33:56 +00002132 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002133 if (TYPE(CHILD(tree, pos)) != op1)
2134 res = validate_ntype(CHILD(tree, pos), op2);
2135 if (res)
2136 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002137 }
2138 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002139}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002140
2141
Guido van Rossum47478871996-08-21 14:32:37 +00002142static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002143validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002144{
2145 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002146 && validate_chain_two_ops(tree, validate_arith_expr,
2147 LEFTSHIFT, RIGHTSHIFT));
2148}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002149
2150
Guido van Rossum47478871996-08-21 14:32:37 +00002151static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002152validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002153{
2154 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002155 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2156}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002157
2158
Guido van Rossum47478871996-08-21 14:32:37 +00002159static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002160validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002161{
2162 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002163 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002164 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002165 && is_odd(nch)
2166 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002167
Guido van Rossum3d602e31996-07-21 02:33:56 +00002168 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002169 res = (((TYPE(CHILD(tree, pos)) == STAR)
2170 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002171 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002172 || (TYPE(CHILD(tree, pos)) == PERCENT))
2173 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002174
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002175 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002176}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002177
2178
Guido van Rossum3d602e31996-07-21 02:33:56 +00002179/* factor:
2180 *
2181 * factor: ('+'|'-'|'~') factor | power
2182 */
Guido van Rossum47478871996-08-21 14:32:37 +00002183static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002184validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002185{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002186 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002187 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002188 && (((nch == 2)
2189 && ((TYPE(CHILD(tree, 0)) == PLUS)
2190 || (TYPE(CHILD(tree, 0)) == MINUS)
2191 || (TYPE(CHILD(tree, 0)) == TILDE))
2192 && validate_factor(CHILD(tree, 1)))
2193 || ((nch == 1)
2194 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002195 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002196}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002197
2198
Guido van Rossum3d602e31996-07-21 02:33:56 +00002199/* power:
2200 *
2201 * power: atom trailer* ('**' factor)*
2202 */
Guido van Rossum47478871996-08-21 14:32:37 +00002203static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002204validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002205{
2206 int pos = 1;
2207 int nch = NCH(tree);
2208 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002209 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002210
2211 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002212 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002213 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002214 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002215 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002216 return (0);
2217 }
2218 for ( ; res && (pos < (nch - 1)); pos += 2)
2219 res = (validate_doublestar(CHILD(tree, pos))
2220 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002221 }
2222 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002223}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002224
2225
Guido van Rossum47478871996-08-21 14:32:37 +00002226static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002227validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002228{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002229 int pos;
2230 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002231 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002232
Fred Drakecff283c2000-08-21 22:24:43 +00002233 if (res && nch < 1)
2234 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002235 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002236 switch (TYPE(CHILD(tree, 0))) {
2237 case LPAR:
2238 res = ((nch <= 3)
2239 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002240
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002241 if (res && (nch == 3)) {
2242 if (TYPE(CHILD(tree, 1))==yield_expr)
2243 res = validate_yield_expr(CHILD(tree, 1));
2244 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00002245 res = validate_testlist_comp(CHILD(tree, 1));
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002246 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002247 break;
2248 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002249 if (nch == 2)
2250 res = validate_ntype(CHILD(tree, 1), RSQB);
2251 else if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002252 res = (validate_testlist_comp(CHILD(tree, 1))
Fred Drakecff283c2000-08-21 22:24:43 +00002253 && validate_ntype(CHILD(tree, 2), RSQB));
2254 else {
2255 res = 0;
2256 err_string("illegal list display atom");
2257 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002258 break;
2259 case LBRACE:
2260 res = ((nch <= 3)
2261 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002262
Fred Drakeff9ea482000-04-19 13:54:15 +00002263 if (res && (nch == 3))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002264 res = validate_dictorsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002265 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002266 case NAME:
2267 case NUMBER:
2268 res = (nch == 1);
2269 break;
2270 case STRING:
2271 for (pos = 1; res && (pos < nch); ++pos)
2272 res = validate_ntype(CHILD(tree, pos), STRING);
2273 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002274 case DOT:
2275 res = (nch == 3 &&
2276 validate_ntype(CHILD(tree, 1), DOT) &&
2277 validate_ntype(CHILD(tree, 2), DOT));
2278 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002279 default:
2280 res = 0;
2281 break;
2282 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002283 }
2284 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002285}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002286
2287
Nick Coghlan650f0d02007-04-15 12:05:43 +00002288/* testlist_comp:
2289 * test ( comp_for | (',' test)* [','] )
Fred Drake85bf3bb2000-08-23 15:35:26 +00002290 */
Fred Drakecff283c2000-08-21 22:24:43 +00002291static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002292validate_testlist_comp(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00002293{
2294 int nch = NCH(tree);
2295 int ok = nch;
2296
2297 if (nch == 0)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002298 err_string("missing child nodes of testlist_comp");
2299 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002300 ok = validate_test(CHILD(tree, 0));
Nick Coghlan650f0d02007-04-15 12:05:43 +00002301 }
Fred Drakecff283c2000-08-21 22:24:43 +00002302
2303 /*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002304 * comp_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002305 */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002306 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2307 ok = validate_comp_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002308 else {
2309 /* (',' test)* [','] */
2310 int i = 1;
2311 while (ok && nch - i >= 2) {
2312 ok = (validate_comma(CHILD(tree, i))
2313 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002314 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002315 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002316 if (ok && i == nch-1)
2317 ok = validate_comma(CHILD(tree, i));
2318 else if (i != nch) {
2319 ok = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002320 err_string("illegal trailing nodes for testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002321 }
2322 }
2323 return ok;
2324}
Fred Drakecff283c2000-08-21 22:24:43 +00002325
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002326/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002327 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002328 */
2329static int
2330validate_decorator(node *tree)
2331{
2332 int ok;
2333 int nch = NCH(tree);
2334 ok = (validate_ntype(tree, decorator) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002335 (nch == 3 || nch == 5 || nch == 6) &&
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002336 validate_at(CHILD(tree, 0)) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002337 validate_dotted_name(CHILD(tree, 1)) &&
2338 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002339
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002340 if (ok && nch != 3) {
2341 ok = (validate_lparen(CHILD(tree, 2)) &&
2342 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002343
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002344 if (ok && nch == 6)
2345 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002346 }
2347
2348 return ok;
2349}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002350
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002351/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002352 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002353 */
2354static int
2355validate_decorators(node *tree)
2356{
2357 int i, nch, ok;
2358 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002359 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002360
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002361 for (i = 0; ok && i < nch; ++i)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002362 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002363
2364 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002365}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002366
Guido van Rossum3d602e31996-07-21 02:33:56 +00002367/* funcdef:
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002368 *
2369 * -6 -5 -4 -3 -2 -1
2370 * [decorators] 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002371 */
Guido van Rossum47478871996-08-21 14:32:37 +00002372static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002373validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002374{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002375 int nch = NCH(tree);
2376 int ok = (validate_ntype(tree, funcdef)
2377 && ((nch == 5) || (nch == 6))
2378 && validate_name(RCHILD(tree, -5), "def")
2379 && validate_ntype(RCHILD(tree, -4), NAME)
2380 && validate_colon(RCHILD(tree, -2))
2381 && validate_parameters(RCHILD(tree, -3))
2382 && validate_suite(RCHILD(tree, -1)));
2383
2384 if (ok && (nch == 6))
2385 ok = validate_decorators(CHILD(tree, 0));
2386
2387 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002388}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002389
2390
Guido van Rossum47478871996-08-21 14:32:37 +00002391static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002392validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002393{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002394 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002395 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002396 && ((nch == 3) || (nch == 4))
2397 && validate_name(CHILD(tree, 0), "lambda")
2398 && validate_colon(CHILD(tree, nch - 2))
2399 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002400
Guido van Rossum3d602e31996-07-21 02:33:56 +00002401 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002402 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002403 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002404 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002405
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002406 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002407}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002408
2409
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002410static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002411validate_lambdef_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002412{
2413 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002414 int res = (validate_ntype(tree, lambdef_nocond)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002415 && ((nch == 3) || (nch == 4))
2416 && validate_name(CHILD(tree, 0), "lambda")
2417 && validate_colon(CHILD(tree, nch - 2))
2418 && validate_test(CHILD(tree, nch - 1)));
2419
2420 if (res && (nch == 4))
2421 res = validate_varargslist(CHILD(tree, 1));
2422 else if (!res && !PyErr_Occurred())
Nick Coghlan650f0d02007-04-15 12:05:43 +00002423 (void) validate_numnodes(tree, 3, "lambdef_nocond");
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002424
2425 return (res);
2426}
2427
2428
Guido van Rossum3d602e31996-07-21 02:33:56 +00002429/* arglist:
2430 *
Fred Drakecff283c2000-08-21 22:24:43 +00002431 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002432 */
Guido van Rossum47478871996-08-21 14:32:37 +00002433static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002434validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002435{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002436 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002437 int i = 0;
2438 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002439
2440 if (nch <= 0)
2441 /* raise the right error from having an invalid number of children */
2442 return validate_numnodes(tree, nch + 1, "arglist");
2443
Raymond Hettinger354433a2004-05-19 08:20:33 +00002444 if (nch > 1) {
2445 for (i=0; i<nch; i++) {
2446 if (TYPE(CHILD(tree, i)) == argument) {
2447 node *ch = CHILD(tree, i);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002448 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
Raymond Hettinger354433a2004-05-19 08:20:33 +00002449 err_string("need '(', ')' for generator expression");
2450 return 0;
2451 }
2452 }
2453 }
2454 }
2455
Fred Drakecff283c2000-08-21 22:24:43 +00002456 while (ok && nch-i >= 2) {
2457 /* skip leading (argument ',') */
2458 ok = (validate_argument(CHILD(tree, i))
2459 && validate_comma(CHILD(tree, i+1)));
2460 if (ok)
2461 i += 2;
2462 else
2463 PyErr_Clear();
2464 }
2465 ok = 1;
2466 if (nch-i > 0) {
2467 /*
2468 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002469 */
Fred Drakecff283c2000-08-21 22:24:43 +00002470 int sym = TYPE(CHILD(tree, i));
2471
2472 if (sym == argument) {
2473 ok = validate_argument(CHILD(tree, i));
2474 if (ok && i+1 != nch) {
2475 err_string("illegal arglist specification"
2476 " (extra stuff on end)");
2477 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002478 }
Fred Drakecff283c2000-08-21 22:24:43 +00002479 }
2480 else if (sym == STAR) {
2481 ok = validate_star(CHILD(tree, i));
2482 if (ok && (nch-i == 2))
2483 ok = validate_test(CHILD(tree, i+1));
2484 else if (ok && (nch-i == 5))
2485 ok = (validate_test(CHILD(tree, i+1))
2486 && validate_comma(CHILD(tree, i+2))
2487 && validate_doublestar(CHILD(tree, i+3))
2488 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002489 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002490 err_string("illegal use of '*' in arglist");
2491 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002492 }
2493 }
Fred Drakecff283c2000-08-21 22:24:43 +00002494 else if (sym == DOUBLESTAR) {
2495 if (nch-i == 2)
2496 ok = (validate_doublestar(CHILD(tree, i))
2497 && validate_test(CHILD(tree, i+1)));
2498 else {
2499 err_string("illegal use of '**' in arglist");
2500 ok = 0;
2501 }
2502 }
2503 else {
2504 err_string("illegal arglist specification");
2505 ok = 0;
2506 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002507 }
2508 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002509}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002510
2511
2512
2513/* argument:
2514 *
Nick Coghlan650f0d02007-04-15 12:05:43 +00002515 * [test '='] test [comp_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002516 */
Guido van Rossum47478871996-08-21 14:32:37 +00002517static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002518validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002519{
2520 int nch = NCH(tree);
2521 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002522 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002523 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002524
Raymond Hettinger354433a2004-05-19 08:20:33 +00002525 if (res && (nch == 2))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002526 res = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002527 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002528 res = (validate_equal(CHILD(tree, 1))
2529 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002530
2531 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002532}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002533
2534
2535
2536/* trailer:
2537 *
Guido van Rossum47478871996-08-21 14:32:37 +00002538 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002539 */
Guido van Rossum47478871996-08-21 14:32:37 +00002540static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002541validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002542{
2543 int nch = NCH(tree);
2544 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002545
2546 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002547 switch (TYPE(CHILD(tree, 0))) {
2548 case LPAR:
2549 res = validate_rparen(CHILD(tree, nch - 1));
2550 if (res && (nch == 3))
2551 res = validate_arglist(CHILD(tree, 1));
2552 break;
2553 case LSQB:
2554 res = (validate_numnodes(tree, 3, "trailer")
2555 && validate_subscriptlist(CHILD(tree, 1))
2556 && validate_ntype(CHILD(tree, 2), RSQB));
2557 break;
2558 case DOT:
2559 res = (validate_numnodes(tree, 2, "trailer")
2560 && validate_ntype(CHILD(tree, 1), NAME));
2561 break;
2562 default:
2563 res = 0;
2564 break;
2565 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002566 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002567 else {
2568 (void) validate_numnodes(tree, 2, "trailer");
2569 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002570 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002571}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002572
2573
Guido van Rossum47478871996-08-21 14:32:37 +00002574/* subscriptlist:
2575 *
2576 * subscript (',' subscript)* [',']
2577 */
2578static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002579validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002580{
2581 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002582 validate_subscript, "subscriptlist"));
2583}
Guido van Rossum47478871996-08-21 14:32:37 +00002584
2585
Guido van Rossum3d602e31996-07-21 02:33:56 +00002586/* subscript:
2587 *
Guido van Rossum47478871996-08-21 14:32:37 +00002588 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002589 */
Guido van Rossum47478871996-08-21 14:32:37 +00002590static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002591validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002592{
Guido van Rossum47478871996-08-21 14:32:37 +00002593 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002594 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002595 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002596
Guido van Rossum47478871996-08-21 14:32:37 +00002597 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002598 if (!PyErr_Occurred())
2599 err_string("invalid number of arguments for subscript node");
2600 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002601 }
Guido van Rossum47478871996-08-21 14:32:37 +00002602 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002603 /* take care of ('.' '.' '.') possibility */
2604 return (validate_numnodes(tree, 3, "subscript")
2605 && validate_dot(CHILD(tree, 0))
2606 && validate_dot(CHILD(tree, 1))
2607 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002608 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002609 if (TYPE(CHILD(tree, 0)) == test)
2610 res = validate_test(CHILD(tree, 0));
2611 else
2612 res = validate_colon(CHILD(tree, 0));
2613 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002614 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002615 /* Must be [test] ':' [test] [sliceop],
2616 * but at least one of the optional components will
2617 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002618 */
2619 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002620 res = validate_test(CHILD(tree, 0));
2621 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002622 }
2623 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002624 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002625 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002626 int rem = nch - ++offset;
2627 if (rem) {
2628 if (TYPE(CHILD(tree, offset)) == test) {
2629 res = validate_test(CHILD(tree, offset));
2630 ++offset;
2631 --rem;
2632 }
2633 if (res && rem)
2634 res = validate_sliceop(CHILD(tree, offset));
2635 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002636 }
2637 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002638}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002639
2640
Guido van Rossum47478871996-08-21 14:32:37 +00002641static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002642validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002643{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002644 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002645 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002646 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002647 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002648 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002649 }
Guido van Rossum47478871996-08-21 14:32:37 +00002650 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002651 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002652 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002653 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002654
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002655 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002656}
Guido van Rossum47478871996-08-21 14:32:37 +00002657
2658
2659static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002660validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002661{
2662 return (validate_repeating_list(tree, exprlist,
Guido van Rossum0368b722007-05-11 16:50:42 +00002663 validate_star_expr, "exprlist"));
Fred Drakeff9ea482000-04-19 13:54:15 +00002664}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002665
2666
Guido van Rossum47478871996-08-21 14:32:37 +00002667static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002668validate_dictorsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002669{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002670 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002671 int res = (validate_ntype(tree, dictorsetmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002672 && (nch >= 3)
2673 && validate_test(CHILD(tree, 0))
2674 && validate_colon(CHILD(tree, 1))
2675 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002676
Guido van Rossum3d602e31996-07-21 02:33:56 +00002677 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002678 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002679 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002680 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002681
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002682 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002683 int pos = 3;
2684 /* ( ',' test ':' test )* */
2685 while (res && (pos < nch)) {
2686 res = (validate_comma(CHILD(tree, pos))
2687 && validate_test(CHILD(tree, pos + 1))
2688 && validate_colon(CHILD(tree, pos + 2))
2689 && validate_test(CHILD(tree, pos + 3)));
2690 pos += 4;
2691 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002692 }
2693 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002694}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002695
2696
Guido van Rossum47478871996-08-21 14:32:37 +00002697static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002698validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002699{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002700 int pos;
2701 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002702 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002703 && (nch >= 2)
2704 && validate_testlist(CHILD(tree, 0))
2705 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002706
Guido van Rossum3d602e31996-07-21 02:33:56 +00002707 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002708 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002709
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002710 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002711}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002712
2713
Guido van Rossum47478871996-08-21 14:32:37 +00002714static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002715validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002716{
Fred Drakeff9ea482000-04-19 13:54:15 +00002717 int nch = 0; /* num. children on current node */
2718 int res = 1; /* result value */
2719 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002720
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002721 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002722 nch = NCH(tree);
2723 next = 0;
2724 switch (TYPE(tree)) {
2725 /*
2726 * Definition nodes.
2727 */
2728 case funcdef:
2729 res = validate_funcdef(tree);
2730 break;
2731 case classdef:
2732 res = validate_class(tree);
2733 break;
2734 /*
2735 * "Trivial" parse tree nodes.
2736 * (Why did I call these trivial?)
2737 */
2738 case stmt:
2739 res = validate_stmt(tree);
2740 break;
2741 case small_stmt:
2742 /*
Guido van Rossum452bf512007-02-09 05:32:43 +00002743 * expr_stmt | del_stmt | pass_stmt | flow_stmt
Georg Brandl7cae87c2006-09-06 06:51:57 +00002744 * | import_stmt | global_stmt | assert_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002745 */
2746 res = validate_small_stmt(tree);
2747 break;
2748 case flow_stmt:
2749 res = (validate_numnodes(tree, 1, "flow_stmt")
2750 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2751 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002752 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002753 || (TYPE(CHILD(tree, 0)) == return_stmt)
2754 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2755 if (res)
2756 next = CHILD(tree, 0);
2757 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002758 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002759 break;
Fred Drake02126f22001-07-17 02:59:15 +00002760 case yield_stmt:
2761 res = validate_yield_stmt(tree);
2762 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002763 /*
2764 * Compound statements.
2765 */
2766 case simple_stmt:
2767 res = validate_simple_stmt(tree);
2768 break;
2769 case compound_stmt:
2770 res = validate_compound_stmt(tree);
2771 break;
2772 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002773 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002774 */
2775 case expr_stmt:
2776 res = validate_expr_stmt(tree);
2777 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002778 case del_stmt:
2779 res = validate_del_stmt(tree);
2780 break;
2781 case pass_stmt:
2782 res = (validate_numnodes(tree, 1, "pass")
2783 && validate_name(CHILD(tree, 0), "pass"));
2784 break;
2785 case break_stmt:
2786 res = (validate_numnodes(tree, 1, "break")
2787 && validate_name(CHILD(tree, 0), "break"));
2788 break;
2789 case continue_stmt:
2790 res = (validate_numnodes(tree, 1, "continue")
2791 && validate_name(CHILD(tree, 0), "continue"));
2792 break;
2793 case return_stmt:
2794 res = validate_return_stmt(tree);
2795 break;
2796 case raise_stmt:
2797 res = validate_raise_stmt(tree);
2798 break;
2799 case import_stmt:
2800 res = validate_import_stmt(tree);
2801 break;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00002802 case import_name:
2803 res = validate_import_name(tree);
2804 break;
2805 case import_from:
2806 res = validate_import_from(tree);
2807 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002808 case global_stmt:
2809 res = validate_global_stmt(tree);
2810 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002811 case assert_stmt:
2812 res = validate_assert_stmt(tree);
2813 break;
2814 case if_stmt:
2815 res = validate_if(tree);
2816 break;
2817 case while_stmt:
2818 res = validate_while(tree);
2819 break;
2820 case for_stmt:
2821 res = validate_for(tree);
2822 break;
2823 case try_stmt:
2824 res = validate_try(tree);
2825 break;
2826 case suite:
2827 res = validate_suite(tree);
2828 break;
2829 /*
2830 * Expression nodes.
2831 */
2832 case testlist:
2833 res = validate_testlist(tree);
2834 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002835 case yield_expr:
2836 res = validate_yield_expr(tree);
2837 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002838 case testlist1:
2839 res = validate_testlist1(tree);
2840 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002841 case test:
2842 res = validate_test(tree);
2843 break;
2844 case and_test:
2845 res = validate_and_test(tree);
2846 break;
2847 case not_test:
2848 res = validate_not_test(tree);
2849 break;
2850 case comparison:
2851 res = validate_comparison(tree);
2852 break;
2853 case exprlist:
2854 res = validate_exprlist(tree);
2855 break;
2856 case comp_op:
2857 res = validate_comp_op(tree);
2858 break;
2859 case expr:
2860 res = validate_expr(tree);
2861 break;
2862 case xor_expr:
2863 res = validate_xor_expr(tree);
2864 break;
2865 case and_expr:
2866 res = validate_and_expr(tree);
2867 break;
2868 case shift_expr:
2869 res = validate_shift_expr(tree);
2870 break;
2871 case arith_expr:
2872 res = validate_arith_expr(tree);
2873 break;
2874 case term:
2875 res = validate_term(tree);
2876 break;
2877 case factor:
2878 res = validate_factor(tree);
2879 break;
2880 case power:
2881 res = validate_power(tree);
2882 break;
2883 case atom:
2884 res = validate_atom(tree);
2885 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002886
Fred Drakeff9ea482000-04-19 13:54:15 +00002887 default:
2888 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002889 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002890 res = 0;
2891 break;
2892 }
2893 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002894 }
2895 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002896}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002897
2898
Guido van Rossum47478871996-08-21 14:32:37 +00002899static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002900validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002901{
2902 int res = validate_eval_input(tree);
2903
2904 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002905 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002906
2907 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002908}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002909
2910
Guido van Rossum3d602e31996-07-21 02:33:56 +00002911/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002912 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002913 */
Guido van Rossum47478871996-08-21 14:32:37 +00002914static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002915validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002916{
Fred Drakec2683dd2001-07-17 19:32:05 +00002917 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002918 int nch = NCH(tree) - 1;
2919 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002920 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002921
Fred Drakec2683dd2001-07-17 19:32:05 +00002922 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002923 if (TYPE(CHILD(tree, j)) == stmt)
2924 res = validate_stmt(CHILD(tree, j));
2925 else
2926 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002927 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002928 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002929 * user. Hopefully, this won't be needed. If a user reports getting
2930 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002931 */
2932 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002933 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002934
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002935 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002936}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002937
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00002938static int
2939validate_encoding_decl(node *tree)
2940{
2941 int nch = NCH(tree);
2942 int res = ((nch == 1)
2943 && validate_file_input(CHILD(tree, 0)));
2944
2945 if (!res && !PyErr_Occurred())
2946 err_string("Error Parsing encoding_decl");
2947
2948 return res;
2949}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002950
Fred Drake43f8f9b1998-04-13 16:25:46 +00002951static PyObject*
2952pickle_constructor = NULL;
2953
2954
2955static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002956parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002957{
Fred Drake268397f1998-04-29 14:16:32 +00002958 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002959 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00002960 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002961 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002962
Fred Drakec2683dd2001-07-17 19:32:05 +00002963 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002964 PyObject *newargs;
2965 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002966
Fred Drake2a6875e1999-09-20 22:32:18 +00002967 if ((empty_dict = PyDict_New()) == NULL)
2968 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002969 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002970 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002971 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002972 if (tuple != NULL) {
2973 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2974 Py_DECREF(tuple);
2975 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002976 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002977 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002978 }
2979 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002980 Py_XDECREF(empty_dict);
2981
Fred Drake43f8f9b1998-04-13 16:25:46 +00002982 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002983}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002984
2985
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002986/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00002987 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002988 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002989 * We'd really have to write a wrapper around it all anyway to allow
2990 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002991 */
2992static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00002993 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002994 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002995 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002996 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002997 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00002998 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00002999 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003000 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003001 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003002 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003003 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003004 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003005 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003006 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003007 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003008 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003009 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003010 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003011 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003012 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003013 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003014 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003015 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003016 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003017 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003018 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003019 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003020 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003021
Fred Drake43f8f9b1998-04-13 16:25:46 +00003022 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003023 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003024 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003025
Fred Drake268397f1998-04-29 14:16:32 +00003026 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003027 };
3028
3029
Mark Hammond62b1ab12002-07-23 06:31:15 +00003030PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003031
Mark Hammond62b1ab12002-07-23 06:31:15 +00003032PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00003033initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003034{
Fred Drake13130bc2001-08-15 16:44:56 +00003035 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003036
3037 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00003038 module = Py_InitModule("parser", parser_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003039 if (module == NULL)
3040 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003041
Fred Drake7a15ba51999-09-09 14:21:52 +00003042 if (parser_error == 0)
3043 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003044
Tim Peters6a627252003-07-21 14:25:23 +00003045 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00003046 /* caller will check PyErr_Occurred() */
3047 return;
Tim Peters6a627252003-07-21 14:25:23 +00003048 /* CAUTION: The code next used to skip bumping the refcount on
3049 * parser_error. That's a disaster if initparser() gets called more
3050 * than once. By incref'ing, we ensure that each module dict that
3051 * gets created owns its reference to the shared parser_error object,
3052 * and the file static parser_error vrbl owns a reference too.
3053 */
3054 Py_INCREF(parser_error);
3055 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3056 return;
3057
Fred Drakec2683dd2001-07-17 19:32:05 +00003058 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003059 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00003060 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003061 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003062
Fred Drake13130bc2001-08-15 16:44:56 +00003063 PyModule_AddStringConstant(module, "__copyright__",
3064 parser_copyright_string);
3065 PyModule_AddStringConstant(module, "__doc__",
3066 parser_doc_string);
3067 PyModule_AddStringConstant(module, "__version__",
3068 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003069
Fred Drake78bdb9b2001-07-19 20:17:15 +00003070 /* Register to support pickling.
3071 * If this fails, the import of this module will fail because an
3072 * exception will be raised here; should we clear the exception?
3073 */
Fred Drake13130bc2001-08-15 16:44:56 +00003074 copyreg = PyImport_ImportModule("copy_reg");
3075 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003076 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003077
Fred Drake13130bc2001-08-15 16:44:56 +00003078 func = PyObject_GetAttrString(copyreg, "pickle");
3079 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3080 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003081 Py_XINCREF(pickle_constructor);
3082 if ((func != NULL) && (pickle_constructor != NULL)
3083 && (pickler != NULL)) {
3084 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003085
Thomas Wouters477c8d52006-05-27 19:21:47 +00003086 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3087 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003088 Py_XDECREF(res);
3089 }
3090 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003091 Py_XDECREF(pickle_constructor);
3092 Py_XDECREF(pickler);
3093 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003094 }
Fred Drakeff9ea482000-04-19 13:54:15 +00003095}