blob: 249fc1a73dd34d1fef7fa51b50044c11ae269181 [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000026 */
27
Fred Drakeff9ea482000-04-19 13:54:15 +000028#include "Python.h" /* general Python API */
29#include "graminit.h" /* symbols defined in the grammar */
30#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000031#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000032#include "token.h" /* token definitions */
33 /* ISTERMINAL() / ISNONTERMINAL() */
34#include "compile.h" /* PyNode_Compile() */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000035
Fred Drake268397f1998-04-29 14:16:32 +000036#ifdef lint
37#include <note.h>
38#else
39#define NOTE(x)
40#endif
41
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000042/* String constants used to initialize module attributes.
43 *
44 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045static char parser_copyright_string[] =
46"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000047University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
48Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
49Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000050
51
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000052PyDoc_STRVAR(parser_doc_string,
53"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000054
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000055static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000056
57
Martin v. Löwis18e16552006-02-15 17:27:45 +000058typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000059typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000060 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000061 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000062
Thomas Wouters7e474022000-07-16 12:04:32 +000063/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000064 * original copyright statement is included below, and continues to apply
65 * in full to the function immediately following. All other material is
66 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
67 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000068 * new naming conventions. Added arguments to provide support for creating
69 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000070 */
71
Guido van Rossum52f2c051993-11-10 12:53:24 +000072
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000073static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000074node2tuple(node *n, /* node to convert */
75 SeqMaker mkseq, /* create sequence */
76 SeqInserter addelem, /* func. to add elem. in seq. */
Thomas Wouters89f507f2006-12-13 04:49:30 +000077 int lineno, /* include line numbers? */
78 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000079{
Guido van Rossum3d602e31996-07-21 02:33:56 +000080 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000081 Py_INCREF(Py_None);
82 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000083 }
84 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000085 int i;
86 PyObject *v;
87 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000088
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000089 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000090 if (v == NULL)
91 return (v);
92 w = PyInt_FromLong(TYPE(n));
93 if (w == NULL) {
94 Py_DECREF(v);
95 return ((PyObject*) NULL);
96 }
97 (void) addelem(v, 0, w);
98 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +000099 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000100 if (w == NULL) {
101 Py_DECREF(v);
102 return ((PyObject*) NULL);
103 }
104 (void) addelem(v, i+1, w);
105 }
Tim Peters6a627252003-07-21 14:25:23 +0000106
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000107 if (TYPE(n) == encoding_decl)
108 (void) addelem(v, i+1, PyString_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000109 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000110 }
111 else if (ISTERMINAL(TYPE(n))) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000112 PyObject *result = mkseq(2 + lineno + col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000113 if (result != NULL) {
114 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
115 (void) addelem(result, 1, PyString_FromString(STR(n)));
116 if (lineno == 1)
117 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000118 if (col_offset == 1)
119 (void) addelem(result, 3, PyInt_FromLong(n->n_col_offset));
Fred Drakeff9ea482000-04-19 13:54:15 +0000120 }
121 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000122 }
123 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000124 PyErr_SetString(PyExc_SystemError,
125 "unrecognized parse tree node type");
126 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000127 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000128}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000129/*
130 * End of material copyrighted by Stichting Mathematisch Centrum.
131 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000132
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000133
134
135/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000136 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000137 * field of the object type to identify which any given object represents.
138 * These should probably go in an external header to allow other extensions
139 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000140 */
141
Fred Drakec2683dd2001-07-17 19:32:05 +0000142#define PyST_EXPR 1
143#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000144
145
146/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000147 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000148 * naming style, but the code uses the new naming convention.
149 */
150
151static PyObject*
152parser_error = 0;
153
154
Fred Drakec2683dd2001-07-17 19:32:05 +0000155typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000156 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000157 node* st_node; /* the node* returned by the parser */
158 int st_type; /* EXPR or SUITE ? */
159} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000160
161
Jeremy Hylton938ace62002-07-17 16:30:39 +0000162static void parser_free(PyST_Object *st);
163static int parser_compare(PyST_Object *left, PyST_Object *right);
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000164static PyObject *parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000165
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000166
Fred Drake268397f1998-04-29 14:16:32 +0000167static
Fred Drakec2683dd2001-07-17 19:32:05 +0000168PyTypeObject PyST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000169 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000170 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000171 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000172 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000173 0, /* tp_itemsize */
174 (destructor)parser_free, /* tp_dealloc */
175 0, /* tp_print */
176 parser_getattr, /* tp_getattr */
177 0, /* tp_setattr */
178 (cmpfunc)parser_compare, /* tp_compare */
179 0, /* tp_repr */
180 0, /* tp_as_number */
181 0, /* tp_as_sequence */
182 0, /* tp_as_mapping */
183 0, /* tp_hash */
184 0, /* tp_call */
185 0, /* tp_str */
186 0, /* tp_getattro */
187 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000188
189 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000190 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000191
Fred Drakeff9ea482000-04-19 13:54:15 +0000192 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000193
194 /* __doc__ */
195 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000196}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000197
198
199static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000200parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000201{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000202 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000203
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000204 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000205 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000206
207 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000208 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000209
210 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000211 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000212
213 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000214 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000215
216 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000217 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000218
219 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000220 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000221
Fred Drakeff9ea482000-04-19 13:54:15 +0000222 if (v != 0)
223 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000224 }
225 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000226}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000227
228
Fred Drakec2683dd2001-07-17 19:32:05 +0000229/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000230 *
231 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
232 * This really just wraps a call to parser_compare_nodes() with some easy
233 * checks and protection code.
234 *
235 */
236static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000237parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000238{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000239 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000240 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000241
242 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000243 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000244
Fred Drakec2683dd2001-07-17 19:32:05 +0000245 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000246}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247
248
Fred Drakec2683dd2001-07-17 19:32:05 +0000249/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000250 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000251 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000252 * 'wrapper' object that holds a node* and allows it to be passed around in
253 * Python code.
254 *
255 */
256static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000257parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000258{
Fred Drakec2683dd2001-07-17 19:32:05 +0000259 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000260
261 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000262 o->st_node = st;
263 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000264 }
Fred Drake268397f1998-04-29 14:16:32 +0000265 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000266 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000267 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000268 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000269}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000270
271
Fred Drakec2683dd2001-07-17 19:32:05 +0000272/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000273 *
274 * This is called by a del statement that reduces the reference count to 0.
275 *
276 */
277static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000278parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000279{
Fred Drakec2683dd2001-07-17 19:32:05 +0000280 PyNode_Free(st->st_node);
281 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000282}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000283
284
Fred Drakec2683dd2001-07-17 19:32:05 +0000285/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000286 *
287 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000288 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000289 *
290 */
291static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000292parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000293{
Guido van Rossum47478871996-08-21 14:32:37 +0000294 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000295 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000296 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000297 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000298
Thomas Wouters89f507f2006-12-13 04:49:30 +0000299 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000300
Fred Drake268397f1998-04-29 14:16:32 +0000301 if (self == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000302 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
303 &PyST_Type, &self, &line_option,
304 &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000305 }
Fred Drake503d8d61998-04-13 18:45:18 +0000306 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000307 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
308 &line_option, &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000309 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000310 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000311 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000312 if (line_option != NULL) {
313 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
314 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000315 if (col_option != NULL) {
316 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
317 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000318 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000319 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000320 * since it's known to work already.
321 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000322 res = node2tuple(((PyST_Object*)self)->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000323 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000324 }
325 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000326}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000327
328
Fred Drakec2683dd2001-07-17 19:32:05 +0000329/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000330 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000331 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000332 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000333 *
334 */
335static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000336parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000337{
Guido van Rossum47478871996-08-21 14:32:37 +0000338 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000339 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000340 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000341 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000342
Thomas Wouters89f507f2006-12-13 04:49:30 +0000343 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000344
Fred Drake503d8d61998-04-13 18:45:18 +0000345 if (self == NULL)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000346 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
347 &PyST_Type, &self, &line_option,
348 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000349 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000350 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
351 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000352 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000353 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000354 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000355 if (line_option != 0) {
356 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
357 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000358 if (col_option != NULL) {
359 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
360 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000361 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000362 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000363 * since it's known to work already.
364 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000365 res = node2tuple(self->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000366 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000367 }
368 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000369}
Guido van Rossum47478871996-08-21 14:32:37 +0000370
371
Fred Drakec2683dd2001-07-17 19:32:05 +0000372/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000373 *
374 * This function creates code objects from the parse tree represented by
375 * the passed-in data object. An optional file name is passed in as well.
376 *
377 */
378static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000379parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000380{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000381 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000382 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000383 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000384
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000385 static char *keywords[] = {"ast", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000386
Fred Drake503d8d61998-04-13 18:45:18 +0000387 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000388 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
389 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000390 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000391 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000392 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000393
394 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000395 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000396
397 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000398}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000399
400
401/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
402 * PyObject* parser_issuite(PyObject* self, PyObject* args)
403 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000404 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000405 * a statement suite, respectively. The return is a Python truth value.
406 *
407 */
408static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000409parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000410{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000411 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000412 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000413
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000414 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000415
Fred Drake503d8d61998-04-13 18:45:18 +0000416 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000417 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000418 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000419 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000420 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000421
422 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000423 /* Check to see if the ST represents an expression or not. */
424 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000425 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000426 }
427 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000428}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000429
430
431static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000432parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000433{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000434 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000435 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000436
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000437 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000438
Fred Drake503d8d61998-04-13 18:45:18 +0000439 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000440 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000441 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000442 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000443 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000444
445 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000446 /* Check to see if the ST represents an expression or not. */
447 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000448 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000449 }
450 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000451}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000452
453
Fred Drake7a15ba51999-09-09 14:21:52 +0000454#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
455
Fred Drake503d8d61998-04-13 18:45:18 +0000456static PyMethodDef
457parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000458 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000459 PyDoc_STR("Compile this ST object into a code object.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000460 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000461 PyDoc_STR("Determines if this ST object was created from an expression.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000462 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000463 PyDoc_STR("Determines if this ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000464 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000465 PyDoc_STR("Creates a list-tree representation of this ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000466 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000467 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Fred Drake503d8d61998-04-13 18:45:18 +0000468
Fred Drake268397f1998-04-29 14:16:32 +0000469 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000470};
471
Fred Drake503d8d61998-04-13 18:45:18 +0000472
473static PyObject*
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000474parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000475{
Fred Drake503d8d61998-04-13 18:45:18 +0000476 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000477}
Fred Drake503d8d61998-04-13 18:45:18 +0000478
479
Guido van Rossum3d602e31996-07-21 02:33:56 +0000480/* err_string(char* message)
481 *
482 * Sets the error string for an exception of type ParserError.
483 *
484 */
485static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000486err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000487{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000488 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000489}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000490
491
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000492/* PyObject* parser_do_parse(PyObject* args, int type)
493 *
494 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000495 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000496 *
497 */
498static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000499parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000500{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000501 char* string = 0;
502 PyObject* res = 0;
503
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000504 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000505
506 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000507 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000508 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000509 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000510
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000511 if (n)
512 res = parser_newstobject(n, type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000513 }
514 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000515}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000516
517
518/* PyObject* parser_expr(PyObject* self, PyObject* args)
519 * PyObject* parser_suite(PyObject* self, PyObject* args)
520 *
521 * External interfaces to the parser itself. Which is called determines if
522 * the parser attempts to recognize an expression ('eval' form) or statement
523 * suite ('exec' form). The real work is done by parser_do_parse() above.
524 *
525 */
526static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000527parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000528{
Fred Drake268397f1998-04-29 14:16:32 +0000529 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000530 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000531}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000532
533
534static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000535parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000536{
Fred Drake268397f1998-04-29 14:16:32 +0000537 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000538 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000539}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000540
541
542
Fred Drakec2683dd2001-07-17 19:32:05 +0000543/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000544 * object requires that the input tuple be valid without having to rely on
545 * catching an exception from the compiler. This is done to allow the
546 * compiler itself to remain fast, since most of its input will come from
547 * the parser directly, and therefore be known to be syntactically correct.
548 * This validation is done to ensure that we don't core dump the compile
549 * phase, returning an exception instead.
550 *
551 * Two aspects can be broken out in this code: creating a node tree from
552 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000553 * advantageous to expand the number of ST types to include funcdefs and
554 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000555 * here. They are not necessary, and not quite as useful in a raw form.
556 * For now, let's get expressions and suites working reliably.
557 */
558
559
Jeremy Hylton938ace62002-07-17 16:30:39 +0000560static node* build_node_tree(PyObject *tuple);
561static int validate_expr_tree(node *tree);
562static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000563static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000564
Fred Drakec2683dd2001-07-17 19:32:05 +0000565/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000566 *
567 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000568 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000569 * tuple can be validated. It does this by checking the first code of the
570 * tuple, and, if acceptable, builds the internal representation. If this
571 * step succeeds, the internal representation is validated as fully as
572 * possible with the various validate_*() routines defined below.
573 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000574 * This function must be changed if support is to be added for PyST_FRAGMENT
575 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000576 *
577 */
578static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000579parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000580{
Fred Drake268397f1998-04-29 14:16:32 +0000581 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000582 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000583 PyObject *tuple;
584 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000585
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000586 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000587
Fred Drakec2683dd2001-07-17 19:32:05 +0000588 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000589 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000590 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000591 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000592 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000593 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000594 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000595 }
596 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000597 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000598 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000599 tree = build_node_tree(tuple);
600 if (tree != 0) {
601 int start_sym = TYPE(tree);
602 if (start_sym == eval_input) {
603 /* Might be an eval form. */
604 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000605 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000606 else
607 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000608 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000609 else if (start_sym == file_input) {
610 /* This looks like an exec form so far. */
611 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000612 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000613 else
614 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000615 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000616 else if (start_sym == encoding_decl) {
617 /* This looks like an encoding_decl so far. */
618 if (validate_encoding_decl(tree))
619 st = parser_newstobject(tree, PyST_SUITE);
620 else
621 PyNode_Free(tree);
622 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000623 else {
624 /* This is a fragment, at best. */
625 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000626 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000627 }
Guido van Rossum47478871996-08-21 14:32:37 +0000628 }
Guido van Rossum47478871996-08-21 14:32:37 +0000629 /* Make sure we throw an exception on all errors. We should never
630 * get this, but we'd do well to be sure something is done.
631 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000632 if (st == NULL && !PyErr_Occurred())
633 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000634
Fred Drakec2683dd2001-07-17 19:32:05 +0000635 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000636}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000637
638
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000639/* node* build_node_children()
640 *
641 * Iterate across the children of the current non-terminal node and build
642 * their structures. If successful, return the root of this portion of
643 * the tree, otherwise, 0. Any required exception will be specified already,
644 * and no memory will have been deallocated.
645 *
646 */
647static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000648build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000649{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000650 Py_ssize_t len = PyObject_Size(tuple);
651 Py_ssize_t i;
652 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000653
654 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000655 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000656 PyObject* elem = PySequence_GetItem(tuple, i);
657 int ok = elem != NULL;
658 long type = 0;
659 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000660
Fred Drakeff9ea482000-04-19 13:54:15 +0000661 if (ok)
662 ok = PySequence_Check(elem);
663 if (ok) {
664 PyObject *temp = PySequence_GetItem(elem, 0);
665 if (temp == NULL)
666 ok = 0;
667 else {
668 ok = PyInt_Check(temp);
669 if (ok)
670 type = PyInt_AS_LONG(temp);
671 Py_DECREF(temp);
672 }
673 }
674 if (!ok) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675 PyObject *err = Py_BuildValue("os", elem,
676 "Illegal node construct.");
677 PyErr_SetObject(parser_error, err);
678 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000679 Py_XDECREF(elem);
680 return (0);
681 }
682 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000683 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000684 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000685
Fred Drake0ac9b072000-09-12 21:58:06 +0000686 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000687 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000688 return 0;
689 }
690 temp = PySequence_GetItem(elem, 1);
691 if (temp == NULL)
692 return 0;
693 if (!PyString_Check(temp)) {
694 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000695 "second item in terminal node must be a string,"
696 " found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000697 temp->ob_type->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000698 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000699 return 0;
700 }
701 if (len == 3) {
702 PyObject *o = PySequence_GetItem(elem, 2);
703 if (o != NULL) {
704 if (PyInt_Check(o))
705 *line_num = PyInt_AS_LONG(o);
706 else {
707 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000708 "third item in terminal node must be an"
709 " integer, found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000710 temp->ob_type->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000711 Py_DECREF(o);
712 Py_DECREF(temp);
713 return 0;
714 }
715 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000716 }
717 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000718 len = PyString_GET_SIZE(temp) + 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000719 strn = (char *)PyObject_MALLOC(len);
Fred Drake0ac9b072000-09-12 21:58:06 +0000720 if (strn != NULL)
721 (void) memcpy(strn, PyString_AS_STRING(temp), len);
722 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000723 }
724 else if (!ISNONTERMINAL(type)) {
725 /*
726 * It has to be one or the other; this is an error.
727 * Throw an exception.
728 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000729 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
730 PyErr_SetObject(parser_error, err);
731 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000732 Py_XDECREF(elem);
733 return (0);
734 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000735 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000736 if (err == E_NOMEM) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000737 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000738 return (node *) PyErr_NoMemory();
739 }
740 if (err == E_OVERFLOW) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000741 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000742 PyErr_SetString(PyExc_ValueError,
743 "unsupported number of child nodes");
744 return NULL;
745 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000746
Fred Drakeff9ea482000-04-19 13:54:15 +0000747 if (ISNONTERMINAL(type)) {
748 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000749
Fred Drakeff9ea482000-04-19 13:54:15 +0000750 if (new_child != build_node_children(elem, new_child, line_num)) {
751 Py_XDECREF(elem);
752 return (0);
753 }
754 }
755 else if (type == NEWLINE) { /* It's true: we increment the */
756 ++(*line_num); /* line number *after* the newline! */
757 }
758 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000759 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000760 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000761}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000762
763
764static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000765build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000766{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000767 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000768 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000769 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000770
Guido van Rossum47478871996-08-21 14:32:37 +0000771 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000772 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000773 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000774 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000775 /*
776 * The tuple is simple, but it doesn't start with a start symbol.
777 * Throw an exception now and be done with it.
778 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000779 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000780 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000781 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000782 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000783 }
784 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000785 /*
786 * Not efficient, but that can be handled later.
787 */
788 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000789 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000790
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000791 if (num == encoding_decl) {
792 encoding = PySequence_GetItem(tuple, 2);
793 /* tuple isn't borrowed anymore here, need to DECREF */
794 tuple = PySequence_GetSlice(tuple, 0, 2);
795 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000796 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000797 if (res != NULL) {
798 if (res != build_node_children(tuple, res, &line_num)) {
799 PyNode_Free(res);
800 res = NULL;
801 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000802 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000803 Py_ssize_t len;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000804 len = PyString_GET_SIZE(encoding) + 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000805 res->n_str = (char *)PyObject_MALLOC(len);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000806 if (res->n_str != NULL)
807 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
808 Py_DECREF(encoding);
809 Py_DECREF(tuple);
810 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000811 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000812 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000813 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000814 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000815 * NONTERMINAL, we can't use it. Not sure the implementation
816 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000817 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000818 PyObject *err = Py_BuildValue("os", tuple,
819 "Illegal component tuple.");
820 PyErr_SetObject(parser_error, err);
821 Py_XDECREF(err);
822 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000823
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000824 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000825}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000826
827
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000828/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000829 * Validation routines used within the validation section:
830 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000831static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000832
Fred Drakeff9ea482000-04-19 13:54:15 +0000833#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
834#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
835#define validate_colon(ch) validate_terminal(ch, COLON, ":")
836#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
837#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
838#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
839#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
840#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
841#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
842#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
843#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
844#define validate_star(ch) validate_terminal(ch, STAR, "*")
845#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
846#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
847#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000848#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000849#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000850
Fred Drake0ac9b072000-09-12 21:58:06 +0000851#define VALIDATER(n) static int validate_##n(node *tree)
852
Fred Drakeff9ea482000-04-19 13:54:15 +0000853VALIDATER(node); VALIDATER(small_stmt);
854VALIDATER(class); VALIDATER(node);
855VALIDATER(parameters); VALIDATER(suite);
856VALIDATER(testlist); VALIDATER(varargslist);
Neal Norwitzc1505362006-12-28 06:47:50 +0000857VALIDATER(vfpdef); VALIDATER(vfplist);
Fred Drakeff9ea482000-04-19 13:54:15 +0000858VALIDATER(stmt); VALIDATER(simple_stmt);
859VALIDATER(expr_stmt); VALIDATER(power);
Guido van Rossum452bf512007-02-09 05:32:43 +0000860VALIDATER(del_stmt);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000861VALIDATER(return_stmt); VALIDATER(raise_stmt);
862VALIDATER(import_stmt); VALIDATER(import_stmt);
863VALIDATER(import_name); VALIDATER(yield_stmt);
864VALIDATER(global_stmt); VALIDATER(assert_stmt);
Neal Norwitzc1505362006-12-28 06:47:50 +0000865VALIDATER(compound_stmt); VALIDATER(vname);
Fred Drakeff9ea482000-04-19 13:54:15 +0000866VALIDATER(while); VALIDATER(for);
867VALIDATER(try); VALIDATER(except_clause);
868VALIDATER(test); VALIDATER(and_test);
869VALIDATER(not_test); VALIDATER(comparison);
870VALIDATER(comp_op); VALIDATER(expr);
871VALIDATER(xor_expr); VALIDATER(and_expr);
872VALIDATER(shift_expr); VALIDATER(arith_expr);
873VALIDATER(term); VALIDATER(factor);
874VALIDATER(atom); VALIDATER(lambdef);
875VALIDATER(trailer); VALIDATER(subscript);
876VALIDATER(subscriptlist); VALIDATER(sliceop);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000877VALIDATER(exprlist); VALIDATER(dictorsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000878VALIDATER(arglist); VALIDATER(argument);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000879VALIDATER(testlist1); VALIDATER(comp_for);
880VALIDATER(comp_iter); VALIDATER(comp_if);
881VALIDATER(testlist_comp); VALIDATER(yield_expr);
882VALIDATER(yield_or_testlist); VALIDATER(or_test);
883VALIDATER(test_nocond); VALIDATER(lambdef_nocond);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000884
Fred Drake0ac9b072000-09-12 21:58:06 +0000885#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000886
Fred Drakeff9ea482000-04-19 13:54:15 +0000887#define is_even(n) (((n) & 1) == 0)
888#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000889
890
891static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000892validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000893{
Fred Drake0ac9b072000-09-12 21:58:06 +0000894 if (TYPE(n) != t) {
895 PyErr_Format(parser_error, "Expected node type %d, got %d.",
896 t, TYPE(n));
897 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000898 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000899 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000900}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000901
902
Fred Drakee7ab64e2000-04-25 04:14:46 +0000903/* Verifies that the number of child nodes is exactly 'num', raising
904 * an exception if it isn't. The exception message does not indicate
905 * the exact number of nodes, allowing this to be used to raise the
906 * "right" exception when the wrong number of nodes is present in a
907 * specific variant of a statement's syntax. This is commonly used
908 * in that fashion.
909 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000910static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000911validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000912{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000913 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000914 PyErr_Format(parser_error,
915 "Illegal number of children for %s node.", name);
916 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000917 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000918 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000919}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000920
921
922static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000923validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000924{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000925 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000926 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000927
928 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000929 PyErr_Format(parser_error,
930 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000931 }
932 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000933}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000934
935
Guido van Rossum47478871996-08-21 14:32:37 +0000936/* X (',' X) [',']
937 */
938static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000939validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000940 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000941{
942 int nch = NCH(tree);
943 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000944 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000945
946 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000947 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000948 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000949 if (is_even(nch))
950 res = validate_comma(CHILD(tree, --nch));
951 if (res && nch > 1) {
952 int pos = 1;
953 for ( ; res && pos < nch; pos += 2)
954 res = (validate_comma(CHILD(tree, pos))
955 && vfunc(CHILD(tree, pos + 1)));
956 }
Guido van Rossum47478871996-08-21 14:32:37 +0000957 }
958 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000959}
Guido van Rossum47478871996-08-21 14:32:37 +0000960
961
Fred Drakecff283c2000-08-21 22:24:43 +0000962/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000963 *
964 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000965 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000966 */
Guido van Rossum47478871996-08-21 14:32:37 +0000967static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000968validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000969{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000970 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +0000971 int res = (validate_ntype(tree, classdef) &&
972 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000973
Guido van Rossum3d602e31996-07-21 02:33:56 +0000974 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000975 res = (validate_name(CHILD(tree, 0), "class")
976 && validate_ntype(CHILD(tree, 1), NAME)
977 && validate_colon(CHILD(tree, nch - 2))
978 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000979 }
Brett Cannonf4189912005-04-09 02:30:16 +0000980 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000981 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +0000982 }
983
984 if (res) {
985 if (nch == 7) {
986 res = ((validate_lparen(CHILD(tree, 2)) &&
987 validate_testlist(CHILD(tree, 3)) &&
988 validate_rparen(CHILD(tree, 4))));
989 }
990 else if (nch == 6) {
991 res = (validate_lparen(CHILD(tree,2)) &&
992 validate_rparen(CHILD(tree,3)));
993 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000994 }
995 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000996}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000997
998
Guido van Rossum3d602e31996-07-21 02:33:56 +0000999/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001000 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001001 */
Guido van Rossum47478871996-08-21 14:32:37 +00001002static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001003validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001004{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001005 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001006 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001007 && (nch >= 4)
1008 && validate_name(CHILD(tree, 0), "if")
1009 && validate_test(CHILD(tree, 1))
1010 && validate_colon(CHILD(tree, 2))
1011 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001012
1013 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001014 /* ... 'else' ':' suite */
1015 res = (validate_name(CHILD(tree, nch - 3), "else")
1016 && validate_colon(CHILD(tree, nch - 2))
1017 && validate_suite(CHILD(tree, nch - 1)));
1018 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001019 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001020 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001021 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001022 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001023 /* Will catch the case for nch < 4 */
1024 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001025 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001026 /* ... ('elif' test ':' suite)+ ... */
1027 int j = 4;
1028 while ((j < nch) && res) {
1029 res = (validate_name(CHILD(tree, j), "elif")
1030 && validate_colon(CHILD(tree, j + 2))
1031 && validate_test(CHILD(tree, j + 1))
1032 && validate_suite(CHILD(tree, j + 3)));
1033 j += 4;
1034 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001035 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001036 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001037}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001038
1039
Guido van Rossum3d602e31996-07-21 02:33:56 +00001040/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001041 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001042 *
1043 */
Guido van Rossum47478871996-08-21 14:32:37 +00001044static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001045validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001046{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001047 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001048 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001049
Guido van Rossum3d602e31996-07-21 02:33:56 +00001050 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001051 res = (validate_lparen(CHILD(tree, 0))
1052 && validate_rparen(CHILD(tree, nch - 1)));
1053 if (res && (nch == 3))
1054 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001055 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001056 else {
1057 (void) validate_numnodes(tree, 2, "parameters");
1058 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001059 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001060}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001061
1062
Fred Drakecff283c2000-08-21 22:24:43 +00001063/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001064 *
1065 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001066 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001067 * | NEWLINE INDENT stmt+ DEDENT
1068 */
Guido van Rossum47478871996-08-21 14:32:37 +00001069static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001070validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001071{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001072 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001073 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001074
Guido van Rossum3d602e31996-07-21 02:33:56 +00001075 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001076 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001077 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001078 /* NEWLINE INDENT stmt+ DEDENT */
1079 res = (validate_newline(CHILD(tree, 0))
1080 && validate_indent(CHILD(tree, 1))
1081 && validate_stmt(CHILD(tree, 2))
1082 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001083
Fred Drakeff9ea482000-04-19 13:54:15 +00001084 if (res && (nch > 4)) {
1085 int i = 3;
1086 --nch; /* forget the DEDENT */
1087 for ( ; res && (i < nch); ++i)
1088 res = validate_stmt(CHILD(tree, i));
1089 }
1090 else if (nch < 4)
1091 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001092 }
1093 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001094}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001095
1096
Guido van Rossum47478871996-08-21 14:32:37 +00001097static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001098validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001099{
Guido van Rossum47478871996-08-21 14:32:37 +00001100 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001101 validate_test, "testlist"));
1102}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001103
1104
Guido van Rossum1c917072001-10-15 15:44:05 +00001105static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001106validate_testlist1(node *tree)
1107{
1108 return (validate_repeating_list(tree, testlist1,
1109 validate_test, "testlist1"));
1110}
1111
1112
Neal Norwitzc1505362006-12-28 06:47:50 +00001113/* validate either vname or tname.
1114 * vname: NAME
1115 * tname: NAME [':' test]
1116 */
1117static int
1118validate_vname(node *tree)
1119{
1120 int nch = NCH(tree);
1121 if (TYPE(tree) == vname) {
1122 return nch == 1 && validate_name(CHILD(tree, 0), NULL);
1123 }
1124 else if (TYPE(tree) == tname) {
1125 if (nch == 1) {
1126 return validate_name(CHILD(tree, 0), NULL);
1127 }
1128 else if (nch == 3) {
1129 return validate_name(CHILD(tree, 0), NULL) &&
1130 validate_colon(CHILD(tree, 1)) &&
1131 validate_test(CHILD(tree, 2));
1132 }
1133 }
1134 return 0;
1135}
1136
1137/* '*' vname (',' vname ['=' test])* [',' '**' vname] | '**' vname
1138 * ..or tname in place of vname. vname: NAME; tname: NAME [':' test]
Fred Drakecff283c2000-08-21 22:24:43 +00001139 */
1140static int
1141validate_varargslist_trailer(node *tree, int start)
1142{
1143 int nch = NCH(tree);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001144 int res = 0, i;
Fred Drakecff283c2000-08-21 22:24:43 +00001145 int sym;
1146
1147 if (nch <= start) {
1148 err_string("expected variable argument trailer for varargslist");
1149 return 0;
1150 }
1151 sym = TYPE(CHILD(tree, start));
1152 if (sym == STAR) {
1153 /*
Neal Norwitzc1505362006-12-28 06:47:50 +00001154 * '*' vname (',' vname ['=' test])* [',' '**' vname] | '**' vname
Fred Drakecff283c2000-08-21 22:24:43 +00001155 */
1156 if (nch-start == 2)
Neal Norwitzc1505362006-12-28 06:47:50 +00001157 res = validate_vname(CHILD(tree, start+1));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001158 else if (nch-start == 5 && TYPE(CHILD(tree, start+2)) == COMMA)
Neal Norwitzc1505362006-12-28 06:47:50 +00001159 res = (validate_vname(CHILD(tree, start+1))
Fred Drakecff283c2000-08-21 22:24:43 +00001160 && validate_comma(CHILD(tree, start+2))
1161 && validate_doublestar(CHILD(tree, start+3))
Neal Norwitzc1505362006-12-28 06:47:50 +00001162 && validate_vname(CHILD(tree, start+4)));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001163 else {
Neal Norwitzc1505362006-12-28 06:47:50 +00001164 /* skip over vname (',' vname ['=' test])* */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001165 i = start + 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001166 if (TYPE(CHILD(tree, i)) == vname ||
1167 TYPE(CHILD(tree, i)) == tname) { /* skip over vname or tname */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001168 i += 1;
1169 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001170 while (res && i+1 < nch) { /* validate (',' vname ['=' test])* */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001171 res = validate_comma(CHILD(tree, i));
1172 if (TYPE(CHILD(tree, i+1)) == DOUBLESTAR)
1173 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001174 res = res && validate_vname(CHILD(tree, i+1));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001175 if (res && i+2 < nch && TYPE(CHILD(tree, i+2)) == EQUAL) {
1176 res = res && (i+3 < nch)
1177 && validate_test(CHILD(tree, i+3));
1178 i += 4;
1179 }
1180 else {
1181 i += 2;
1182 }
1183 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001184 /* [',' '**' vname] */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001185 if (res && i+1 < nch && TYPE(CHILD(tree, i+1)) == DOUBLESTAR) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001186 res = validate_vname(CHILD(tree, i+2));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001187 }
1188 }
Fred Drakecff283c2000-08-21 22:24:43 +00001189 }
1190 else if (sym == DOUBLESTAR) {
1191 /*
1192 * '**' NAME
1193 */
1194 if (nch-start == 2)
Neal Norwitzc1505362006-12-28 06:47:50 +00001195 res = validate_vname(CHILD(tree, start+1));
Fred Drakecff283c2000-08-21 22:24:43 +00001196 }
1197 if (!res)
1198 err_string("illegal variable argument trailer for varargslist");
1199 return res;
1200}
1201
1202
Neal Norwitzc1505362006-12-28 06:47:50 +00001203/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001204 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001205 * Validate typedargslist or varargslist.
1206 *
1207 * typedargslist: ((tfpdef ['=' test] ',')*
1208 * ('*' [tname] (',' tname ['=' test])* [',' '**' tname] |
1209 * '**' tname)
1210 * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
1211 * tname: NAME [':' test]
1212 * tfpdef: tname | '(' tfplist ')'
1213 * tfplist: tfpdef (',' tfpdef)* [',']
1214 * varargslist: ((vfpdef ['=' test] ',')*
1215 * ('*' [vname] (',' vname ['=' test])* [',' '**' vname] |
1216 * '**' vname)
1217 * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
1218 * vname: NAME
1219 * vfpdef: vname | '(' vfplist ')'
1220 * vfplist: vfpdef (',' vfpdef)* [',']
Guido van Rossum3d602e31996-07-21 02:33:56 +00001221 *
1222 */
Guido van Rossum47478871996-08-21 14:32:37 +00001223static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001224validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001225{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001226 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001227 int res = (TYPE(tree) == varargslist ||
1228 TYPE(tree) == typedargslist) &&
1229 (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001230 int sym;
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 }
Fred Drakecff283c2000-08-21 22:24:43 +00001237 sym = TYPE(CHILD(tree, 0));
1238 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001239 /* whole thing matches:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001240 * '*' [NAME] (',' NAME ['=' test])* [',' '**' NAME] | '**' NAME
Fred Drakeb6429a22000-12-11 22:08:27 +00001241 */
Fred Drakecff283c2000-08-21 22:24:43 +00001242 res = validate_varargslist_trailer(tree, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001243 else if (sym == vfpdef || sym == tfpdef) {
Fred Drakecff283c2000-08-21 22:24:43 +00001244 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001245
Fred Drakecff283c2000-08-21 22:24:43 +00001246 sym = TYPE(CHILD(tree, nch-1));
Neal Norwitzc1505362006-12-28 06:47:50 +00001247 if (sym == vname || sym == tname) {
Fred Drakecff283c2000-08-21 22:24:43 +00001248 /*
Neal Norwitzc1505362006-12-28 06:47:50 +00001249 * (vfpdef ['=' test] ',')+
1250 * ('*' vname [',' '**' vname]
1251 * | '**' vname)
Fred Drakecff283c2000-08-21 22:24:43 +00001252 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001253 /* skip over (vfpdef ['=' test] ',')+ */
Fred Drakecff283c2000-08-21 22:24:43 +00001254 while (res && (i+2 <= nch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001255 res = validate_vfpdef(CHILD(tree, i));
Fred Drakecff283c2000-08-21 22:24:43 +00001256 ++i;
1257 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1258 res = (validate_equal(CHILD(tree, i))
1259 && validate_test(CHILD(tree, i+1)));
1260 if (res)
1261 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001262 }
Fred Drakecff283c2000-08-21 22:24:43 +00001263 if (res && i < nch) {
1264 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001265 ++i;
1266 if (res && i < nch
1267 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1268 || TYPE(CHILD(tree, i)) == STAR))
1269 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001270 }
1271 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001272 /* .. ('*' [NAME] (',' NAME ['=' test])* [',' '**' NAME] | '**' NAME)
Fred Drakeb6429a22000-12-11 22:08:27 +00001273 * i --^^^
1274 */
Fred Drakecff283c2000-08-21 22:24:43 +00001275 if (res)
1276 res = validate_varargslist_trailer(tree, i);
1277 }
1278 else {
1279 /*
Neal Norwitzc1505362006-12-28 06:47:50 +00001280 * vfpdef ['=' test] (',' vfpdef ['=' test])* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00001281 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001282 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001283 if (sym == COMMA) {
1284 res = validate_comma(CHILD(tree, nch-1));
1285 if (!res)
1286 return 0;
1287 --nch;
1288 }
1289 /*
Neal Norwitzc1505362006-12-28 06:47:50 +00001290 * vfpdef ['=' test] (',' vfpdef ['=' test])*
Fred Drakecff283c2000-08-21 22:24:43 +00001291 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001292 res = validate_vfpdef(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001293 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001294 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1295 res = (validate_equal(CHILD(tree, i))
1296 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001297 i += 2;
1298 }
1299 /*
Neal Norwitzc1505362006-12-28 06:47:50 +00001300 * ... (',' vfpdef ['=' test])*
Fred Drakecff283c2000-08-21 22:24:43 +00001301 * i ---^^^
1302 */
1303 while (res && (nch - i) >= 2) {
1304 res = (validate_comma(CHILD(tree, i))
Neal Norwitzc1505362006-12-28 06:47:50 +00001305 && validate_vfpdef(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001306 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001307 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1308 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001309 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001310 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001311 }
1312 }
1313 if (res && nch - i != 0) {
1314 res = 0;
1315 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001316 }
1317 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001318 }
Fred Drakecff283c2000-08-21 22:24:43 +00001319 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001320}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001321
1322
Nick Coghlan650f0d02007-04-15 12:05:43 +00001323/* comp_iter: comp_for | comp_if
Fred Drakecff283c2000-08-21 22:24:43 +00001324 */
1325static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001326validate_comp_iter(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001327{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001328 int res = (validate_ntype(tree, comp_iter)
1329 && validate_numnodes(tree, 1, "comp_iter"));
1330 if (res && TYPE(CHILD(tree, 0)) == comp_for)
1331 res = validate_comp_for(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001332 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001333 res = validate_comp_if(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001334
1335 return res;
1336}
1337
Nick Coghlan650f0d02007-04-15 12:05:43 +00001338/* comp_for: 'for' exprlist 'in' test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001339 */
1340static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001341validate_comp_for(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001342{
1343 int nch = NCH(tree);
1344 int res;
1345
1346 if (nch == 5)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001347 res = validate_comp_iter(CHILD(tree, 4));
Fred Drakecff283c2000-08-21 22:24:43 +00001348 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001349 res = validate_numnodes(tree, 4, "comp_for");
Raymond Hettinger354433a2004-05-19 08:20:33 +00001350
1351 if (res)
1352 res = (validate_name(CHILD(tree, 0), "for")
1353 && validate_exprlist(CHILD(tree, 1))
1354 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001355 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001356
1357 return res;
1358}
1359
Nick Coghlan650f0d02007-04-15 12:05:43 +00001360/* comp_if: 'if' test_nocond [comp_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001361 */
1362static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001363validate_comp_if(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001364{
1365 int nch = NCH(tree);
1366 int res;
1367
1368 if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001369 res = validate_comp_iter(CHILD(tree, 2));
Fred Drakecff283c2000-08-21 22:24:43 +00001370 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001371 res = validate_numnodes(tree, 2, "comp_if");
Fred Drakecff283c2000-08-21 22:24:43 +00001372
1373 if (res)
1374 res = (validate_name(CHILD(tree, 0), "if")
Nick Coghlan650f0d02007-04-15 12:05:43 +00001375 && validate_test_nocond(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001376
1377 return res;
1378}
1379
1380
Neal Norwitzc1505362006-12-28 06:47:50 +00001381/* validate_vfpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001382 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001383 * Validate vfpdef or tfpdef.
1384 *
1385 * vname: NAME
1386 * vfpdef: vname | '(' vfplist ')'
1387 * vfplist: vfpdef (',' vfpdef)* [',']
1388 *
1389 * tname: NAME [':' test]
1390 * tfpdef: tname | '(' tfplist ')'
1391 * tfplist: tfpdef (',' tfpdef)* [',']
1392 *
Guido van Rossum3d602e31996-07-21 02:33:56 +00001393 */
Guido van Rossum47478871996-08-21 14:32:37 +00001394static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001395validate_vfpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001396{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001397 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001398 int typ = TYPE(tree);
1399 int res = typ == vfpdef || typ == tfpdef;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001400
Guido van Rossum3d602e31996-07-21 02:33:56 +00001401 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001402 if (nch == 1)
Neal Norwitzc1505362006-12-28 06:47:50 +00001403 res = validate_vname(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001404 else if (nch == 3)
1405 res = (validate_lparen(CHILD(tree, 0))
Neal Norwitzc1505362006-12-28 06:47:50 +00001406 && validate_vfplist(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001407 && validate_rparen(CHILD(tree, 2)));
1408 else
1409 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001410 }
1411 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001412}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001413
1414
Guido van Rossum47478871996-08-21 14:32:37 +00001415static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001416validate_vfplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001417{
Neal Norwitzc1505362006-12-28 06:47:50 +00001418 return (validate_repeating_list(tree, vfplist,
1419 validate_vfpdef, "vfplist"));
Fred Drakeff9ea482000-04-19 13:54:15 +00001420}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001421
1422
Guido van Rossum3d602e31996-07-21 02:33:56 +00001423/* simple_stmt | compound_stmt
1424 *
1425 */
Guido van Rossum47478871996-08-21 14:32:37 +00001426static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001427validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001428{
1429 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001430 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001431
Guido van Rossum3d602e31996-07-21 02:33:56 +00001432 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001433 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001434
Fred Drakeff9ea482000-04-19 13:54:15 +00001435 if (TYPE(tree) == simple_stmt)
1436 res = validate_simple_stmt(tree);
1437 else
1438 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001439 }
1440 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001441}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001442
1443
Guido van Rossum3d602e31996-07-21 02:33:56 +00001444/* small_stmt (';' small_stmt)* [';'] NEWLINE
1445 *
1446 */
Guido van Rossum47478871996-08-21 14:32:37 +00001447static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001448validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001449{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001450 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001451 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001452 && (nch >= 2)
1453 && validate_small_stmt(CHILD(tree, 0))
1454 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001455
Guido van Rossum3d602e31996-07-21 02:33:56 +00001456 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001457 res = validate_numnodes(tree, 2, "simple_stmt");
1458 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001459 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001460 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001461 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001462 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001463
Fred Drakeff9ea482000-04-19 13:54:15 +00001464 for (i = 1; res && (i < nch); i += 2)
1465 res = (validate_semi(CHILD(tree, i))
1466 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001467 }
1468 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001469}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001470
1471
Guido van Rossum47478871996-08-21 14:32:37 +00001472static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001473validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001474{
1475 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001476 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001477
Fred Drake0ac9b072000-09-12 21:58:06 +00001478 if (res) {
1479 int ntype = TYPE(CHILD(tree, 0));
1480
1481 if ( (ntype == expr_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001482 || (ntype == del_stmt)
1483 || (ntype == pass_stmt)
1484 || (ntype == flow_stmt)
1485 || (ntype == import_stmt)
1486 || (ntype == global_stmt)
Georg Brandl7cae87c2006-09-06 06:51:57 +00001487 || (ntype == assert_stmt))
Fred Drake0ac9b072000-09-12 21:58:06 +00001488 res = validate_node(CHILD(tree, 0));
1489 else {
1490 res = 0;
1491 err_string("illegal small_stmt child type");
1492 }
1493 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001494 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001495 res = 0;
1496 PyErr_Format(parser_error,
1497 "Unrecognized child node of small_stmt: %d.",
1498 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001499 }
1500 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001501}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001502
1503
1504/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001505 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001506 */
Guido van Rossum47478871996-08-21 14:32:37 +00001507static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001508validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001509{
1510 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001511 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001512 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001513
1514 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001515 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001516
1517 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001518 ntype = TYPE(tree);
1519 if ( (ntype == if_stmt)
1520 || (ntype == while_stmt)
1521 || (ntype == for_stmt)
1522 || (ntype == try_stmt)
1523 || (ntype == funcdef)
1524 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001525 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001526 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001527 res = 0;
1528 PyErr_Format(parser_error,
1529 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001530 }
1531 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001532}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001533
1534
Guido van Rossum47478871996-08-21 14:32:37 +00001535static int
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001536validate_yield_or_testlist(node *tree)
1537{
1538 if (TYPE(tree) == yield_expr)
1539 return validate_yield_expr(tree);
1540 else
1541 return validate_testlist(tree);
1542}
1543
1544static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001545validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001546{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001547 int j;
1548 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001549 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001550 && is_odd(nch)
1551 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001552
Fred Drake28f739a2000-08-25 22:42:40 +00001553 if (res && nch == 3
1554 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001555 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1556 && validate_yield_or_testlist(CHILD(tree, 2));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001557
Fred Drake28f739a2000-08-25 22:42:40 +00001558 if (res) {
1559 char *s = STR(CHILD(CHILD(tree, 1), 0));
1560
1561 res = (strcmp(s, "+=") == 0
1562 || strcmp(s, "-=") == 0
1563 || strcmp(s, "*=") == 0
1564 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001565 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001566 || strcmp(s, "%=") == 0
1567 || strcmp(s, "&=") == 0
1568 || strcmp(s, "|=") == 0
1569 || strcmp(s, "^=") == 0
1570 || strcmp(s, "<<=") == 0
1571 || strcmp(s, ">>=") == 0
1572 || strcmp(s, "**=") == 0);
1573 if (!res)
1574 err_string("illegal augmmented assignment operator");
1575 }
1576 }
1577 else {
1578 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001579 res = validate_equal(CHILD(tree, j))
1580 && validate_yield_or_testlist(CHILD(tree, j + 1));
Fred Drake28f739a2000-08-25 22:42:40 +00001581 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001582 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001583}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001584
1585
Guido van Rossum47478871996-08-21 14:32:37 +00001586static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001587validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001588{
1589 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001590 && validate_name(CHILD(tree, 0), "del")
1591 && validate_exprlist(CHILD(tree, 1)));
1592}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001593
1594
Guido van Rossum47478871996-08-21 14:32:37 +00001595static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001596validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001597{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001598 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001599 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001600 && ((nch == 1) || (nch == 2))
1601 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001602
Guido van Rossum3d602e31996-07-21 02:33:56 +00001603 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001604 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001605
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001606 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001607}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001608
1609
Guido van Rossum47478871996-08-21 14:32:37 +00001610static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001611validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001612{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001613 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001614 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001615 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001616
Guido van Rossum3d602e31996-07-21 02:33:56 +00001617 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001618 res = validate_name(CHILD(tree, 0), "raise");
1619 if (res && (nch >= 2))
1620 res = validate_test(CHILD(tree, 1));
1621 if (res && nch > 2) {
1622 res = (validate_comma(CHILD(tree, 2))
1623 && validate_test(CHILD(tree, 3)));
1624 if (res && (nch > 4))
1625 res = (validate_comma(CHILD(tree, 4))
1626 && validate_test(CHILD(tree, 5)));
1627 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001628 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001629 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001630 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001631 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001632 res = (validate_comma(CHILD(tree, 2))
1633 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001634
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001635 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001636}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001637
1638
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001639/* yield_expr: 'yield' [testlist]
1640 */
1641static int
1642validate_yield_expr(node *tree)
1643{
1644 int nch = NCH(tree);
1645 int res = (validate_ntype(tree, yield_expr)
1646 && ((nch == 1) || (nch == 2))
1647 && validate_name(CHILD(tree, 0), "yield"));
1648
1649 if (res && (nch == 2))
1650 res = validate_testlist(CHILD(tree, 1));
1651
1652 return (res);
1653}
1654
1655
1656/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001657 */
1658static int
1659validate_yield_stmt(node *tree)
1660{
1661 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001662 && validate_numnodes(tree, 1, "yield_stmt")
1663 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001664}
1665
1666
Fred Drakecff283c2000-08-21 22:24:43 +00001667static int
1668validate_import_as_name(node *tree)
1669{
1670 int nch = NCH(tree);
1671 int ok = validate_ntype(tree, import_as_name);
1672
1673 if (ok) {
1674 if (nch == 1)
1675 ok = validate_name(CHILD(tree, 0), NULL);
1676 else if (nch == 3)
1677 ok = (validate_name(CHILD(tree, 0), NULL)
1678 && validate_name(CHILD(tree, 1), "as")
1679 && validate_name(CHILD(tree, 2), NULL));
1680 else
1681 ok = validate_numnodes(tree, 3, "import_as_name");
1682 }
1683 return ok;
1684}
1685
1686
Fred Drake71137082001-01-07 05:59:59 +00001687/* dotted_name: NAME ("." NAME)*
1688 */
1689static int
1690validate_dotted_name(node *tree)
1691{
1692 int nch = NCH(tree);
1693 int res = (validate_ntype(tree, dotted_name)
1694 && is_odd(nch)
1695 && validate_name(CHILD(tree, 0), NULL));
1696 int i;
1697
1698 for (i = 1; res && (i < nch); i += 2) {
1699 res = (validate_dot(CHILD(tree, i))
1700 && validate_name(CHILD(tree, i+1), NULL));
1701 }
1702 return res;
1703}
1704
1705
Fred Drakecff283c2000-08-21 22:24:43 +00001706/* dotted_as_name: dotted_name [NAME NAME]
1707 */
1708static int
1709validate_dotted_as_name(node *tree)
1710{
1711 int nch = NCH(tree);
1712 int res = validate_ntype(tree, dotted_as_name);
1713
1714 if (res) {
1715 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001716 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001717 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001718 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001719 && validate_name(CHILD(tree, 1), "as")
1720 && validate_name(CHILD(tree, 2), NULL));
1721 else {
1722 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001723 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001724 }
1725 }
1726 return res;
1727}
1728
1729
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001730/* dotted_as_name (',' dotted_as_name)* */
1731static int
1732validate_dotted_as_names(node *tree)
1733{
1734 int nch = NCH(tree);
1735 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1736 int i;
1737
1738 for (i = 1; res && (i < nch); i += 2)
1739 res = (validate_comma(CHILD(tree, i))
1740 && validate_dotted_as_name(CHILD(tree, i + 1)));
1741 return (res);
1742}
1743
1744
1745/* import_as_name (',' import_as_name)* [','] */
1746static int
1747validate_import_as_names(node *tree)
1748{
1749 int nch = NCH(tree);
1750 int res = validate_import_as_name(CHILD(tree, 0));
1751 int i;
1752
1753 for (i = 1; res && (i + 1 < nch); i += 2)
1754 res = (validate_comma(CHILD(tree, i))
1755 && validate_import_as_name(CHILD(tree, i + 1)));
1756 return (res);
1757}
1758
1759
1760/* 'import' dotted_as_names */
1761static int
1762validate_import_name(node *tree)
1763{
1764 return (validate_ntype(tree, import_name)
1765 && validate_numnodes(tree, 2, "import_name")
1766 && validate_name(CHILD(tree, 0), "import")
1767 && validate_dotted_as_names(CHILD(tree, 1)));
1768}
1769
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001770/* Helper function to count the number of leading dots in
1771 * 'from ...module import name'
1772 */
1773static int
1774count_from_dots(node *tree)
1775{
1776 int i;
1777 for (i = 0; i < NCH(tree); i++)
1778 if (TYPE(CHILD(tree, i)) != DOT)
1779 break;
1780 return i;
1781}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001782
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001783/* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001784 * import_as_names
Guido van Rossum3d602e31996-07-21 02:33:56 +00001785 */
Guido van Rossum47478871996-08-21 14:32:37 +00001786static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001787validate_import_from(node *tree)
1788{
1789 int nch = NCH(tree);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001790 int ndots = count_from_dots(tree);
1791 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1792 int offset = ndots + havename;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001793 int res = validate_ntype(tree, import_from)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001794 && (nch >= 4 + ndots)
1795 && validate_name(CHILD(tree, 0), "from")
1796 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1797 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001798
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001799 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1800 res = ((nch == offset + 5)
1801 && validate_lparen(CHILD(tree, offset + 2))
1802 && validate_import_as_names(CHILD(tree, offset + 3))
1803 && validate_rparen(CHILD(tree, offset + 4)));
1804 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1805 res = validate_import_as_names(CHILD(tree, offset + 2));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001806 return (res);
1807}
1808
1809
1810/* import_stmt: import_name | import_from */
1811static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001812validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001813{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001814 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001815 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001816
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001817 if (res) {
1818 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001819
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001820 if (ntype == import_name || ntype == import_from)
1821 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001822 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001823 res = 0;
1824 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001825 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001826 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001827 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001828 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001829 PyErr_Format(parser_error,
1830 "Unrecognized child node of import_stmt: %d.",
1831 TYPE(CHILD(tree, 0)));
1832 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001833 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001834}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001835
1836
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001837
1838
Guido van Rossum47478871996-08-21 14:32:37 +00001839static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001840validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001841{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001842 int j;
1843 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001844 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001845 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001846
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001847 if (!res && !PyErr_Occurred())
1848 err_string("illegal global statement");
1849
Guido van Rossum3d602e31996-07-21 02:33:56 +00001850 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001851 res = (validate_name(CHILD(tree, 0), "global")
1852 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001853 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001854 res = (validate_comma(CHILD(tree, j))
1855 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001856
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001857 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001858}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001859
1860
Guido van Rossum925e5471997-04-02 05:32:13 +00001861/* assert_stmt:
1862 *
1863 * 'assert' test [',' test]
1864 */
1865static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001866validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001867{
1868 int nch = NCH(tree);
1869 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001870 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001871 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001872 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001873
1874 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001875 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001876 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001877 res = (validate_comma(CHILD(tree, 2))
1878 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001879
1880 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001881}
Guido van Rossum925e5471997-04-02 05:32:13 +00001882
1883
Guido van Rossum47478871996-08-21 14:32:37 +00001884static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001885validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001886{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001887 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001888 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001889 && ((nch == 4) || (nch == 7))
1890 && validate_name(CHILD(tree, 0), "while")
1891 && validate_test(CHILD(tree, 1))
1892 && validate_colon(CHILD(tree, 2))
1893 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001894
Guido van Rossum3d602e31996-07-21 02:33:56 +00001895 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001896 res = (validate_name(CHILD(tree, 4), "else")
1897 && validate_colon(CHILD(tree, 5))
1898 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001899
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001900 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001901}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001902
1903
Guido van Rossum47478871996-08-21 14:32:37 +00001904static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001905validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001906{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001907 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001908 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001909 && ((nch == 6) || (nch == 9))
1910 && validate_name(CHILD(tree, 0), "for")
1911 && validate_exprlist(CHILD(tree, 1))
1912 && validate_name(CHILD(tree, 2), "in")
1913 && validate_testlist(CHILD(tree, 3))
1914 && validate_colon(CHILD(tree, 4))
1915 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001916
Guido van Rossum3d602e31996-07-21 02:33:56 +00001917 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001918 res = (validate_name(CHILD(tree, 6), "else")
1919 && validate_colon(CHILD(tree, 7))
1920 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001921
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001922 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001923}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001924
1925
Guido van Rossum3d602e31996-07-21 02:33:56 +00001926/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001927 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001928 * | 'try' ':' suite 'finally' ':' suite
1929 *
1930 */
Guido van Rossum47478871996-08-21 14:32:37 +00001931static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001932validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001933{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001934 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001935 int pos = 3;
1936 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001937 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001938
1939 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001940 res = (validate_name(CHILD(tree, 0), "try")
1941 && validate_colon(CHILD(tree, 1))
1942 && validate_suite(CHILD(tree, 2))
1943 && validate_colon(CHILD(tree, nch - 2))
1944 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001945 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001946 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001947 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1948 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001949
1950 PyErr_Format(parser_error,
1951 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001952 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001953 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001954 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001955 res = (validate_except_clause(CHILD(tree, pos))
1956 && validate_colon(CHILD(tree, pos + 1))
1957 && validate_suite(CHILD(tree, pos + 2)));
1958 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001959 }
1960 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001961 res = validate_ntype(CHILD(tree, pos), NAME);
1962 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1963 res = (validate_numnodes(tree, 6, "try/finally")
1964 && validate_colon(CHILD(tree, 4))
1965 && validate_suite(CHILD(tree, 5)));
1966 else if (res) {
1967 if (nch == (pos + 3)) {
1968 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1969 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1970 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001971 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001972 }
1973 else if (nch == (pos + 6)) {
1974 res = (validate_name(CHILD(tree, pos), "except")
1975 && validate_colon(CHILD(tree, pos + 1))
1976 && validate_suite(CHILD(tree, pos + 2))
1977 && validate_name(CHILD(tree, pos + 3), "else"));
1978 }
1979 else
1980 res = validate_numnodes(tree, pos + 3, "try/except");
1981 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001982 }
1983 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001984}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001985
1986
Guido van Rossum47478871996-08-21 14:32:37 +00001987static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001988validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001989{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001990 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001991 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001992 && ((nch == 1) || (nch == 2) || (nch == 4))
1993 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001994
Guido van Rossum3d602e31996-07-21 02:33:56 +00001995 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001996 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001997 if (res && (nch == 4))
Guido van Rossumb940e112007-01-10 16:19:56 +00001998 res = (validate_name(CHILD(tree, 2), "as")
1999 && validate_ntype(CHILD(tree, 3), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002000
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002001 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002002}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002003
2004
Guido van Rossum47478871996-08-21 14:32:37 +00002005static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002006validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002007{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002008 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002009 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002010
Guido van Rossum3d602e31996-07-21 02:33:56 +00002011 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002012 res = ((nch == 1)
2013 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002014 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002015 res = validate_or_test(CHILD(tree, 0));
2016 res = (res && (nch == 1 || (nch == 5 &&
2017 validate_name(CHILD(tree, 1), "if") &&
2018 validate_or_test(CHILD(tree, 2)) &&
2019 validate_name(CHILD(tree, 3), "else") &&
2020 validate_test(CHILD(tree, 4)))));
2021 }
2022 return (res);
2023}
2024
2025static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002026validate_test_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002027{
2028 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002029 int res = validate_ntype(tree, test_nocond) && (nch == 1);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002030
Nick Coghlan650f0d02007-04-15 12:05:43 +00002031 if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
2032 res = (validate_lambdef_nocond(CHILD(tree, 0)));
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002033 else if (res) {
2034 res = (validate_or_test(CHILD(tree, 0)));
2035 }
2036 return (res);
2037}
2038
2039static int
2040validate_or_test(node *tree)
2041{
2042 int nch = NCH(tree);
2043 int res = validate_ntype(tree, or_test) && is_odd(nch);
2044
2045 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002046 int pos;
2047 res = validate_and_test(CHILD(tree, 0));
2048 for (pos = 1; res && (pos < nch); pos += 2)
2049 res = (validate_name(CHILD(tree, pos), "or")
2050 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002051 }
2052 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002053}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002054
2055
Guido van Rossum47478871996-08-21 14:32:37 +00002056static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002057validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002058{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002059 int pos;
2060 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002061 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002062 && is_odd(nch)
2063 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002064
Guido van Rossum3d602e31996-07-21 02:33:56 +00002065 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002066 res = (validate_name(CHILD(tree, pos), "and")
2067 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002068
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002069 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002070}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002071
2072
Guido van Rossum47478871996-08-21 14:32:37 +00002073static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002074validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002075{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002076 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002077 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002078
Guido van Rossum3d602e31996-07-21 02:33:56 +00002079 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002080 if (nch == 2)
2081 res = (validate_name(CHILD(tree, 0), "not")
2082 && validate_not_test(CHILD(tree, 1)));
2083 else if (nch == 1)
2084 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002085 }
2086 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_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002092{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002093 int pos;
2094 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002095 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002096 && is_odd(nch)
2097 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002098
Guido van Rossum3d602e31996-07-21 02:33:56 +00002099 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002100 res = (validate_comp_op(CHILD(tree, pos))
2101 && validate_expr(CHILD(tree, pos + 1)));
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_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002109{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002110 int res = 0;
2111 int nch = NCH(tree);
2112
Guido van Rossum3d602e31996-07-21 02:33:56 +00002113 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002114 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002115 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002116 /*
2117 * Only child will be a terminal with a well-defined symbolic name
2118 * or a NAME with a string of either 'is' or 'in'
2119 */
2120 tree = CHILD(tree, 0);
2121 switch (TYPE(tree)) {
2122 case LESS:
2123 case GREATER:
2124 case EQEQUAL:
2125 case EQUAL:
2126 case LESSEQUAL:
2127 case GREATEREQUAL:
2128 case NOTEQUAL:
2129 res = 1;
2130 break;
2131 case NAME:
2132 res = ((strcmp(STR(tree), "in") == 0)
2133 || (strcmp(STR(tree), "is") == 0));
2134 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002135 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002136 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002137 }
2138 break;
2139 default:
Fred Drake661ea262000-10-24 19:57:45 +00002140 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002141 break;
2142 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002143 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002144 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002145 res = (validate_ntype(CHILD(tree, 0), NAME)
2146 && validate_ntype(CHILD(tree, 1), NAME)
2147 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2148 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2149 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2150 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2151 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002152 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002153 }
2154 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002155}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002156
2157
Guido van Rossum47478871996-08-21 14:32:37 +00002158static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002159validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002160{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002161 int j;
2162 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002163 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002164 && is_odd(nch)
2165 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002166
Guido van Rossum3d602e31996-07-21 02:33:56 +00002167 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002168 res = (validate_xor_expr(CHILD(tree, j))
2169 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002170
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002171 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002172}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002173
2174
Guido van Rossum47478871996-08-21 14:32:37 +00002175static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002176validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002177{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002178 int j;
2179 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002180 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002181 && is_odd(nch)
2182 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002183
Guido van Rossum3d602e31996-07-21 02:33:56 +00002184 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002185 res = (validate_circumflex(CHILD(tree, j - 1))
2186 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002187
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002188 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002189}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002190
2191
Guido van Rossum47478871996-08-21 14:32:37 +00002192static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002193validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002194{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002195 int pos;
2196 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002197 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002198 && is_odd(nch)
2199 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002200
Guido van Rossum3d602e31996-07-21 02:33:56 +00002201 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002202 res = (validate_ampersand(CHILD(tree, pos))
2203 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002204
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002205 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002206}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002207
2208
2209static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002210validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002211 {
2212 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002213 int nch = NCH(tree);
2214 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002215 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002216
Guido van Rossum3d602e31996-07-21 02:33:56 +00002217 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002218 if (TYPE(CHILD(tree, pos)) != op1)
2219 res = validate_ntype(CHILD(tree, pos), op2);
2220 if (res)
2221 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002222 }
2223 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002224}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002225
2226
Guido van Rossum47478871996-08-21 14:32:37 +00002227static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002228validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002229{
2230 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002231 && validate_chain_two_ops(tree, validate_arith_expr,
2232 LEFTSHIFT, RIGHTSHIFT));
2233}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002234
2235
Guido van Rossum47478871996-08-21 14:32:37 +00002236static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002237validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002238{
2239 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002240 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2241}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002242
2243
Guido van Rossum47478871996-08-21 14:32:37 +00002244static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002245validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002246{
2247 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002248 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002249 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002250 && is_odd(nch)
2251 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002252
Guido van Rossum3d602e31996-07-21 02:33:56 +00002253 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002254 res = (((TYPE(CHILD(tree, pos)) == STAR)
2255 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002256 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002257 || (TYPE(CHILD(tree, pos)) == PERCENT))
2258 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002259
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002260 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002261}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002262
2263
Guido van Rossum3d602e31996-07-21 02:33:56 +00002264/* factor:
2265 *
2266 * factor: ('+'|'-'|'~') factor | power
2267 */
Guido van Rossum47478871996-08-21 14:32:37 +00002268static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002269validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002270{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002271 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002272 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002273 && (((nch == 2)
2274 && ((TYPE(CHILD(tree, 0)) == PLUS)
2275 || (TYPE(CHILD(tree, 0)) == MINUS)
2276 || (TYPE(CHILD(tree, 0)) == TILDE))
2277 && validate_factor(CHILD(tree, 1)))
2278 || ((nch == 1)
2279 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002280 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002281}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002282
2283
Guido van Rossum3d602e31996-07-21 02:33:56 +00002284/* power:
2285 *
2286 * power: atom trailer* ('**' factor)*
2287 */
Guido van Rossum47478871996-08-21 14:32:37 +00002288static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002289validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002290{
2291 int pos = 1;
2292 int nch = NCH(tree);
2293 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002294 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002295
2296 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002297 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002298 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002299 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002300 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002301 return (0);
2302 }
2303 for ( ; res && (pos < (nch - 1)); pos += 2)
2304 res = (validate_doublestar(CHILD(tree, pos))
2305 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002306 }
2307 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002308}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002309
2310
Guido van Rossum47478871996-08-21 14:32:37 +00002311static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002312validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002313{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002314 int pos;
2315 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002316 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002317
Fred Drakecff283c2000-08-21 22:24:43 +00002318 if (res && nch < 1)
2319 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002320 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002321 switch (TYPE(CHILD(tree, 0))) {
2322 case LPAR:
2323 res = ((nch <= 3)
2324 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002325
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002326 if (res && (nch == 3)) {
2327 if (TYPE(CHILD(tree, 1))==yield_expr)
2328 res = validate_yield_expr(CHILD(tree, 1));
2329 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00002330 res = validate_testlist_comp(CHILD(tree, 1));
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002331 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002332 break;
2333 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002334 if (nch == 2)
2335 res = validate_ntype(CHILD(tree, 1), RSQB);
2336 else if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002337 res = (validate_testlist_comp(CHILD(tree, 1))
Fred Drakecff283c2000-08-21 22:24:43 +00002338 && validate_ntype(CHILD(tree, 2), RSQB));
2339 else {
2340 res = 0;
2341 err_string("illegal list display atom");
2342 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002343 break;
2344 case LBRACE:
2345 res = ((nch <= 3)
2346 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002347
Fred Drakeff9ea482000-04-19 13:54:15 +00002348 if (res && (nch == 3))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002349 res = validate_dictorsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002350 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002351 case NAME:
2352 case NUMBER:
2353 res = (nch == 1);
2354 break;
2355 case STRING:
2356 for (pos = 1; res && (pos < nch); ++pos)
2357 res = validate_ntype(CHILD(tree, pos), STRING);
2358 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002359 case DOT:
2360 res = (nch == 3 &&
2361 validate_ntype(CHILD(tree, 1), DOT) &&
2362 validate_ntype(CHILD(tree, 2), DOT));
2363 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002364 default:
2365 res = 0;
2366 break;
2367 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002368 }
2369 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002370}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002371
2372
Nick Coghlan650f0d02007-04-15 12:05:43 +00002373/* testlist_comp:
2374 * test ( comp_for | (',' test)* [','] )
Fred Drake85bf3bb2000-08-23 15:35:26 +00002375 */
Fred Drakecff283c2000-08-21 22:24:43 +00002376static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002377validate_testlist_comp(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00002378{
2379 int nch = NCH(tree);
2380 int ok = nch;
2381
2382 if (nch == 0)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002383 err_string("missing child nodes of testlist_comp");
2384 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002385 ok = validate_test(CHILD(tree, 0));
Nick Coghlan650f0d02007-04-15 12:05:43 +00002386 }
Fred Drakecff283c2000-08-21 22:24:43 +00002387
2388 /*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002389 * comp_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002390 */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002391 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2392 ok = validate_comp_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002393 else {
2394 /* (',' test)* [','] */
2395 int i = 1;
2396 while (ok && nch - i >= 2) {
2397 ok = (validate_comma(CHILD(tree, i))
2398 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002399 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002400 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002401 if (ok && i == nch-1)
2402 ok = validate_comma(CHILD(tree, i));
2403 else if (i != nch) {
2404 ok = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002405 err_string("illegal trailing nodes for testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002406 }
2407 }
2408 return ok;
2409}
Fred Drakecff283c2000-08-21 22:24:43 +00002410
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002411/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002412 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002413 */
2414static int
2415validate_decorator(node *tree)
2416{
2417 int ok;
2418 int nch = NCH(tree);
2419 ok = (validate_ntype(tree, decorator) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002420 (nch == 3 || nch == 5 || nch == 6) &&
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002421 validate_at(CHILD(tree, 0)) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002422 validate_dotted_name(CHILD(tree, 1)) &&
2423 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002424
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002425 if (ok && nch != 3) {
2426 ok = (validate_lparen(CHILD(tree, 2)) &&
2427 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002428
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002429 if (ok && nch == 6)
2430 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002431 }
2432
2433 return ok;
2434}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002435
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002436/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002437 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002438 */
2439static int
2440validate_decorators(node *tree)
2441{
2442 int i, nch, ok;
2443 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002444 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002445
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002446 for (i = 0; ok && i < nch; ++i)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002447 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002448
2449 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002450}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002451
Guido van Rossum3d602e31996-07-21 02:33:56 +00002452/* funcdef:
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002453 *
2454 * -6 -5 -4 -3 -2 -1
2455 * [decorators] 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002456 */
Guido van Rossum47478871996-08-21 14:32:37 +00002457static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002458validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002459{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002460 int nch = NCH(tree);
2461 int ok = (validate_ntype(tree, funcdef)
2462 && ((nch == 5) || (nch == 6))
2463 && validate_name(RCHILD(tree, -5), "def")
2464 && validate_ntype(RCHILD(tree, -4), NAME)
2465 && validate_colon(RCHILD(tree, -2))
2466 && validate_parameters(RCHILD(tree, -3))
2467 && validate_suite(RCHILD(tree, -1)));
2468
2469 if (ok && (nch == 6))
2470 ok = validate_decorators(CHILD(tree, 0));
2471
2472 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002473}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002474
2475
Guido van Rossum47478871996-08-21 14:32:37 +00002476static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002477validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002478{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002479 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002480 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002481 && ((nch == 3) || (nch == 4))
2482 && validate_name(CHILD(tree, 0), "lambda")
2483 && validate_colon(CHILD(tree, nch - 2))
2484 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002485
Guido van Rossum3d602e31996-07-21 02:33:56 +00002486 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002487 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002488 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002489 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002490
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002491 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002492}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002493
2494
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002495static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002496validate_lambdef_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002497{
2498 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002499 int res = (validate_ntype(tree, lambdef_nocond)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002500 && ((nch == 3) || (nch == 4))
2501 && validate_name(CHILD(tree, 0), "lambda")
2502 && validate_colon(CHILD(tree, nch - 2))
2503 && validate_test(CHILD(tree, nch - 1)));
2504
2505 if (res && (nch == 4))
2506 res = validate_varargslist(CHILD(tree, 1));
2507 else if (!res && !PyErr_Occurred())
Nick Coghlan650f0d02007-04-15 12:05:43 +00002508 (void) validate_numnodes(tree, 3, "lambdef_nocond");
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002509
2510 return (res);
2511}
2512
2513
Guido van Rossum3d602e31996-07-21 02:33:56 +00002514/* arglist:
2515 *
Fred Drakecff283c2000-08-21 22:24:43 +00002516 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002517 */
Guido van Rossum47478871996-08-21 14:32:37 +00002518static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002519validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002520{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002521 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002522 int i = 0;
2523 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002524
2525 if (nch <= 0)
2526 /* raise the right error from having an invalid number of children */
2527 return validate_numnodes(tree, nch + 1, "arglist");
2528
Raymond Hettinger354433a2004-05-19 08:20:33 +00002529 if (nch > 1) {
2530 for (i=0; i<nch; i++) {
2531 if (TYPE(CHILD(tree, i)) == argument) {
2532 node *ch = CHILD(tree, i);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002533 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
Raymond Hettinger354433a2004-05-19 08:20:33 +00002534 err_string("need '(', ')' for generator expression");
2535 return 0;
2536 }
2537 }
2538 }
2539 }
2540
Fred Drakecff283c2000-08-21 22:24:43 +00002541 while (ok && nch-i >= 2) {
2542 /* skip leading (argument ',') */
2543 ok = (validate_argument(CHILD(tree, i))
2544 && validate_comma(CHILD(tree, i+1)));
2545 if (ok)
2546 i += 2;
2547 else
2548 PyErr_Clear();
2549 }
2550 ok = 1;
2551 if (nch-i > 0) {
2552 /*
2553 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002554 */
Fred Drakecff283c2000-08-21 22:24:43 +00002555 int sym = TYPE(CHILD(tree, i));
2556
2557 if (sym == argument) {
2558 ok = validate_argument(CHILD(tree, i));
2559 if (ok && i+1 != nch) {
2560 err_string("illegal arglist specification"
2561 " (extra stuff on end)");
2562 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002563 }
Fred Drakecff283c2000-08-21 22:24:43 +00002564 }
2565 else if (sym == STAR) {
2566 ok = validate_star(CHILD(tree, i));
2567 if (ok && (nch-i == 2))
2568 ok = validate_test(CHILD(tree, i+1));
2569 else if (ok && (nch-i == 5))
2570 ok = (validate_test(CHILD(tree, i+1))
2571 && validate_comma(CHILD(tree, i+2))
2572 && validate_doublestar(CHILD(tree, i+3))
2573 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002574 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002575 err_string("illegal use of '*' in arglist");
2576 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002577 }
2578 }
Fred Drakecff283c2000-08-21 22:24:43 +00002579 else if (sym == DOUBLESTAR) {
2580 if (nch-i == 2)
2581 ok = (validate_doublestar(CHILD(tree, i))
2582 && validate_test(CHILD(tree, i+1)));
2583 else {
2584 err_string("illegal use of '**' in arglist");
2585 ok = 0;
2586 }
2587 }
2588 else {
2589 err_string("illegal arglist specification");
2590 ok = 0;
2591 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002592 }
2593 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002594}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002595
2596
2597
2598/* argument:
2599 *
Nick Coghlan650f0d02007-04-15 12:05:43 +00002600 * [test '='] test [comp_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002601 */
Guido van Rossum47478871996-08-21 14:32:37 +00002602static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002603validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002604{
2605 int nch = NCH(tree);
2606 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002607 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002608 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002609
Raymond Hettinger354433a2004-05-19 08:20:33 +00002610 if (res && (nch == 2))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002611 res = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002612 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002613 res = (validate_equal(CHILD(tree, 1))
2614 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002615
2616 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002617}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002618
2619
2620
2621/* trailer:
2622 *
Guido van Rossum47478871996-08-21 14:32:37 +00002623 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002624 */
Guido van Rossum47478871996-08-21 14:32:37 +00002625static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002626validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002627{
2628 int nch = NCH(tree);
2629 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002630
2631 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002632 switch (TYPE(CHILD(tree, 0))) {
2633 case LPAR:
2634 res = validate_rparen(CHILD(tree, nch - 1));
2635 if (res && (nch == 3))
2636 res = validate_arglist(CHILD(tree, 1));
2637 break;
2638 case LSQB:
2639 res = (validate_numnodes(tree, 3, "trailer")
2640 && validate_subscriptlist(CHILD(tree, 1))
2641 && validate_ntype(CHILD(tree, 2), RSQB));
2642 break;
2643 case DOT:
2644 res = (validate_numnodes(tree, 2, "trailer")
2645 && validate_ntype(CHILD(tree, 1), NAME));
2646 break;
2647 default:
2648 res = 0;
2649 break;
2650 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002651 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002652 else {
2653 (void) validate_numnodes(tree, 2, "trailer");
2654 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002655 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002656}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002657
2658
Guido van Rossum47478871996-08-21 14:32:37 +00002659/* subscriptlist:
2660 *
2661 * subscript (',' subscript)* [',']
2662 */
2663static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002664validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002665{
2666 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002667 validate_subscript, "subscriptlist"));
2668}
Guido van Rossum47478871996-08-21 14:32:37 +00002669
2670
Guido van Rossum3d602e31996-07-21 02:33:56 +00002671/* subscript:
2672 *
Guido van Rossum47478871996-08-21 14:32:37 +00002673 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002674 */
Guido van Rossum47478871996-08-21 14:32:37 +00002675static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002676validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002677{
Guido van Rossum47478871996-08-21 14:32:37 +00002678 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002679 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002680 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002681
Guido van Rossum47478871996-08-21 14:32:37 +00002682 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002683 if (!PyErr_Occurred())
2684 err_string("invalid number of arguments for subscript node");
2685 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002686 }
Guido van Rossum47478871996-08-21 14:32:37 +00002687 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002688 /* take care of ('.' '.' '.') possibility */
2689 return (validate_numnodes(tree, 3, "subscript")
2690 && validate_dot(CHILD(tree, 0))
2691 && validate_dot(CHILD(tree, 1))
2692 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002693 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002694 if (TYPE(CHILD(tree, 0)) == test)
2695 res = validate_test(CHILD(tree, 0));
2696 else
2697 res = validate_colon(CHILD(tree, 0));
2698 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002699 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002700 /* Must be [test] ':' [test] [sliceop],
2701 * but at least one of the optional components will
2702 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002703 */
2704 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002705 res = validate_test(CHILD(tree, 0));
2706 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002707 }
2708 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002709 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002710 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002711 int rem = nch - ++offset;
2712 if (rem) {
2713 if (TYPE(CHILD(tree, offset)) == test) {
2714 res = validate_test(CHILD(tree, offset));
2715 ++offset;
2716 --rem;
2717 }
2718 if (res && rem)
2719 res = validate_sliceop(CHILD(tree, offset));
2720 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002721 }
2722 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002723}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002724
2725
Guido van Rossum47478871996-08-21 14:32:37 +00002726static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002727validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002728{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002729 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002730 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002731 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002732 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002733 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002734 }
Guido van Rossum47478871996-08-21 14:32:37 +00002735 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002736 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002737 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002738 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002739
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002740 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002741}
Guido van Rossum47478871996-08-21 14:32:37 +00002742
2743
2744static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002745validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002746{
2747 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002748 validate_expr, "exprlist"));
2749}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002750
2751
Guido van Rossum47478871996-08-21 14:32:37 +00002752static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002753validate_dictorsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002754{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002755 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002756 int res = (validate_ntype(tree, dictorsetmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002757 && (nch >= 3)
2758 && validate_test(CHILD(tree, 0))
2759 && validate_colon(CHILD(tree, 1))
2760 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002761
Guido van Rossum3d602e31996-07-21 02:33:56 +00002762 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002763 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002764 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002765 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002766
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002767 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002768 int pos = 3;
2769 /* ( ',' test ':' test )* */
2770 while (res && (pos < nch)) {
2771 res = (validate_comma(CHILD(tree, pos))
2772 && validate_test(CHILD(tree, pos + 1))
2773 && validate_colon(CHILD(tree, pos + 2))
2774 && validate_test(CHILD(tree, pos + 3)));
2775 pos += 4;
2776 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002777 }
2778 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002779}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002780
2781
Guido van Rossum47478871996-08-21 14:32:37 +00002782static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002783validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002784{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002785 int pos;
2786 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002787 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002788 && (nch >= 2)
2789 && validate_testlist(CHILD(tree, 0))
2790 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002791
Guido van Rossum3d602e31996-07-21 02:33:56 +00002792 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002793 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002794
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002795 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002796}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002797
2798
Guido van Rossum47478871996-08-21 14:32:37 +00002799static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002800validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002801{
Fred Drakeff9ea482000-04-19 13:54:15 +00002802 int nch = 0; /* num. children on current node */
2803 int res = 1; /* result value */
2804 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002805
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002806 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002807 nch = NCH(tree);
2808 next = 0;
2809 switch (TYPE(tree)) {
2810 /*
2811 * Definition nodes.
2812 */
2813 case funcdef:
2814 res = validate_funcdef(tree);
2815 break;
2816 case classdef:
2817 res = validate_class(tree);
2818 break;
2819 /*
2820 * "Trivial" parse tree nodes.
2821 * (Why did I call these trivial?)
2822 */
2823 case stmt:
2824 res = validate_stmt(tree);
2825 break;
2826 case small_stmt:
2827 /*
Guido van Rossum452bf512007-02-09 05:32:43 +00002828 * expr_stmt | del_stmt | pass_stmt | flow_stmt
Georg Brandl7cae87c2006-09-06 06:51:57 +00002829 * | import_stmt | global_stmt | assert_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002830 */
2831 res = validate_small_stmt(tree);
2832 break;
2833 case flow_stmt:
2834 res = (validate_numnodes(tree, 1, "flow_stmt")
2835 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2836 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002837 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002838 || (TYPE(CHILD(tree, 0)) == return_stmt)
2839 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2840 if (res)
2841 next = CHILD(tree, 0);
2842 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002843 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002844 break;
Fred Drake02126f22001-07-17 02:59:15 +00002845 case yield_stmt:
2846 res = validate_yield_stmt(tree);
2847 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002848 /*
2849 * Compound statements.
2850 */
2851 case simple_stmt:
2852 res = validate_simple_stmt(tree);
2853 break;
2854 case compound_stmt:
2855 res = validate_compound_stmt(tree);
2856 break;
2857 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002858 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002859 */
2860 case expr_stmt:
2861 res = validate_expr_stmt(tree);
2862 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002863 case del_stmt:
2864 res = validate_del_stmt(tree);
2865 break;
2866 case pass_stmt:
2867 res = (validate_numnodes(tree, 1, "pass")
2868 && validate_name(CHILD(tree, 0), "pass"));
2869 break;
2870 case break_stmt:
2871 res = (validate_numnodes(tree, 1, "break")
2872 && validate_name(CHILD(tree, 0), "break"));
2873 break;
2874 case continue_stmt:
2875 res = (validate_numnodes(tree, 1, "continue")
2876 && validate_name(CHILD(tree, 0), "continue"));
2877 break;
2878 case return_stmt:
2879 res = validate_return_stmt(tree);
2880 break;
2881 case raise_stmt:
2882 res = validate_raise_stmt(tree);
2883 break;
2884 case import_stmt:
2885 res = validate_import_stmt(tree);
2886 break;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00002887 case import_name:
2888 res = validate_import_name(tree);
2889 break;
2890 case import_from:
2891 res = validate_import_from(tree);
2892 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002893 case global_stmt:
2894 res = validate_global_stmt(tree);
2895 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002896 case assert_stmt:
2897 res = validate_assert_stmt(tree);
2898 break;
2899 case if_stmt:
2900 res = validate_if(tree);
2901 break;
2902 case while_stmt:
2903 res = validate_while(tree);
2904 break;
2905 case for_stmt:
2906 res = validate_for(tree);
2907 break;
2908 case try_stmt:
2909 res = validate_try(tree);
2910 break;
2911 case suite:
2912 res = validate_suite(tree);
2913 break;
2914 /*
2915 * Expression nodes.
2916 */
2917 case testlist:
2918 res = validate_testlist(tree);
2919 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002920 case yield_expr:
2921 res = validate_yield_expr(tree);
2922 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002923 case testlist1:
2924 res = validate_testlist1(tree);
2925 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002926 case test:
2927 res = validate_test(tree);
2928 break;
2929 case and_test:
2930 res = validate_and_test(tree);
2931 break;
2932 case not_test:
2933 res = validate_not_test(tree);
2934 break;
2935 case comparison:
2936 res = validate_comparison(tree);
2937 break;
2938 case exprlist:
2939 res = validate_exprlist(tree);
2940 break;
2941 case comp_op:
2942 res = validate_comp_op(tree);
2943 break;
2944 case expr:
2945 res = validate_expr(tree);
2946 break;
2947 case xor_expr:
2948 res = validate_xor_expr(tree);
2949 break;
2950 case and_expr:
2951 res = validate_and_expr(tree);
2952 break;
2953 case shift_expr:
2954 res = validate_shift_expr(tree);
2955 break;
2956 case arith_expr:
2957 res = validate_arith_expr(tree);
2958 break;
2959 case term:
2960 res = validate_term(tree);
2961 break;
2962 case factor:
2963 res = validate_factor(tree);
2964 break;
2965 case power:
2966 res = validate_power(tree);
2967 break;
2968 case atom:
2969 res = validate_atom(tree);
2970 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002971
Fred Drakeff9ea482000-04-19 13:54:15 +00002972 default:
2973 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002974 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002975 res = 0;
2976 break;
2977 }
2978 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002979 }
2980 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002981}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002982
2983
Guido van Rossum47478871996-08-21 14:32:37 +00002984static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002985validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002986{
2987 int res = validate_eval_input(tree);
2988
2989 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002990 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002991
2992 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002993}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002994
2995
Guido van Rossum3d602e31996-07-21 02:33:56 +00002996/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002997 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002998 */
Guido van Rossum47478871996-08-21 14:32:37 +00002999static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003000validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003001{
Fred Drakec2683dd2001-07-17 19:32:05 +00003002 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003003 int nch = NCH(tree) - 1;
3004 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003005 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003006
Fred Drakec2683dd2001-07-17 19:32:05 +00003007 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003008 if (TYPE(CHILD(tree, j)) == stmt)
3009 res = validate_stmt(CHILD(tree, j));
3010 else
3011 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003012 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003013 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003014 * user. Hopefully, this won't be needed. If a user reports getting
3015 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003016 */
3017 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003018 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003019
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003020 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003021}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003022
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003023static int
3024validate_encoding_decl(node *tree)
3025{
3026 int nch = NCH(tree);
3027 int res = ((nch == 1)
3028 && validate_file_input(CHILD(tree, 0)));
3029
3030 if (!res && !PyErr_Occurred())
3031 err_string("Error Parsing encoding_decl");
3032
3033 return res;
3034}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003035
Fred Drake43f8f9b1998-04-13 16:25:46 +00003036static PyObject*
3037pickle_constructor = NULL;
3038
3039
3040static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003041parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003042{
Fred Drake268397f1998-04-29 14:16:32 +00003043 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003044 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003045 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003046 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003047
Fred Drakec2683dd2001-07-17 19:32:05 +00003048 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003049 PyObject *newargs;
3050 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003051
Fred Drake2a6875e1999-09-20 22:32:18 +00003052 if ((empty_dict = PyDict_New()) == NULL)
3053 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003054 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003055 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003056 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003057 if (tuple != NULL) {
3058 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3059 Py_DECREF(tuple);
3060 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003061 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003062 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003063 }
3064 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003065 Py_XDECREF(empty_dict);
3066
Fred Drake43f8f9b1998-04-13 16:25:46 +00003067 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003068}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003069
3070
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003071/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003072 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003073 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003074 * We'd really have to write a wrapper around it all anyway to allow
3075 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003076 */
3077static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003078 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003079 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003080 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003081 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003082 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003083 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003084 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003085 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003086 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003087 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003088 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003089 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003090 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003091 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003092 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003093 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003094 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003095 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003096 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003097 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003098 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003099 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003100 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003101 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003102 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003103 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003104 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003105 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003106
Fred Drake43f8f9b1998-04-13 16:25:46 +00003107 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003108 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003109 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003110
Fred Drake268397f1998-04-29 14:16:32 +00003111 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003112 };
3113
3114
Mark Hammond62b1ab12002-07-23 06:31:15 +00003115PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003116
Mark Hammond62b1ab12002-07-23 06:31:15 +00003117PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00003118initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003119{
Fred Drake13130bc2001-08-15 16:44:56 +00003120 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003121
3122 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00003123 module = Py_InitModule("parser", parser_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003124 if (module == NULL)
3125 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003126
Fred Drake7a15ba51999-09-09 14:21:52 +00003127 if (parser_error == 0)
3128 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003129
Tim Peters6a627252003-07-21 14:25:23 +00003130 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00003131 /* caller will check PyErr_Occurred() */
3132 return;
Tim Peters6a627252003-07-21 14:25:23 +00003133 /* CAUTION: The code next used to skip bumping the refcount on
3134 * parser_error. That's a disaster if initparser() gets called more
3135 * than once. By incref'ing, we ensure that each module dict that
3136 * gets created owns its reference to the shared parser_error object,
3137 * and the file static parser_error vrbl owns a reference too.
3138 */
3139 Py_INCREF(parser_error);
3140 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3141 return;
3142
Fred Drakec2683dd2001-07-17 19:32:05 +00003143 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003144 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00003145 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003146 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003147
Fred Drake13130bc2001-08-15 16:44:56 +00003148 PyModule_AddStringConstant(module, "__copyright__",
3149 parser_copyright_string);
3150 PyModule_AddStringConstant(module, "__doc__",
3151 parser_doc_string);
3152 PyModule_AddStringConstant(module, "__version__",
3153 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003154
Fred Drake78bdb9b2001-07-19 20:17:15 +00003155 /* Register to support pickling.
3156 * If this fails, the import of this module will fail because an
3157 * exception will be raised here; should we clear the exception?
3158 */
Fred Drake13130bc2001-08-15 16:44:56 +00003159 copyreg = PyImport_ImportModule("copy_reg");
3160 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003161 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003162
Fred Drake13130bc2001-08-15 16:44:56 +00003163 func = PyObject_GetAttrString(copyreg, "pickle");
3164 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3165 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003166 Py_XINCREF(pickle_constructor);
3167 if ((func != NULL) && (pickle_constructor != NULL)
3168 && (pickler != NULL)) {
3169 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003170
Thomas Wouters477c8d52006-05-27 19:21:47 +00003171 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3172 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003173 Py_XDECREF(res);
3174 }
3175 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003176 Py_XDECREF(pickle_constructor);
3177 Py_XDECREF(pickler);
3178 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003179 }
Fred Drakeff9ea482000-04-19 13:54:15 +00003180}