blob: 74af7fe19f4b33c5b7dc9fbd9868423a6bf45415 [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. */
77 int lineno) /* include line numbers? */
Guido van Rossum47478871996-08-21 14:32:37 +000078{
Guido van Rossum3d602e31996-07-21 02:33:56 +000079 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000080 Py_INCREF(Py_None);
81 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000082 }
83 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000084 int i;
85 PyObject *v;
86 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000087
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000088 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000089 if (v == NULL)
90 return (v);
91 w = PyInt_FromLong(TYPE(n));
92 if (w == NULL) {
93 Py_DECREF(v);
94 return ((PyObject*) NULL);
95 }
96 (void) addelem(v, 0, w);
97 for (i = 0; i < NCH(n); i++) {
98 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno);
99 if (w == NULL) {
100 Py_DECREF(v);
101 return ((PyObject*) NULL);
102 }
103 (void) addelem(v, i+1, w);
104 }
Tim Peters6a627252003-07-21 14:25:23 +0000105
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000106 if (TYPE(n) == encoding_decl)
107 (void) addelem(v, i+1, PyString_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000108 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000109 }
110 else if (ISTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000111 PyObject *result = mkseq(2 + lineno);
112 if (result != NULL) {
113 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
114 (void) addelem(result, 1, PyString_FromString(STR(n)));
115 if (lineno == 1)
116 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
117 }
118 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000119 }
120 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000121 PyErr_SetString(PyExc_SystemError,
122 "unrecognized parse tree node type");
123 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000124 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000125}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000126/*
127 * End of material copyrighted by Stichting Mathematisch Centrum.
128 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000129
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000130
131
132/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000133 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000134 * field of the object type to identify which any given object represents.
135 * These should probably go in an external header to allow other extensions
136 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000137 */
138
Fred Drakec2683dd2001-07-17 19:32:05 +0000139#define PyST_EXPR 1
140#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000141
142
143/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000144 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000145 * naming style, but the code uses the new naming convention.
146 */
147
148static PyObject*
149parser_error = 0;
150
151
Fred Drakec2683dd2001-07-17 19:32:05 +0000152typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000153 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000154 node* st_node; /* the node* returned by the parser */
155 int st_type; /* EXPR or SUITE ? */
156} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000157
158
Jeremy Hylton938ace62002-07-17 16:30:39 +0000159static void parser_free(PyST_Object *st);
160static int parser_compare(PyST_Object *left, PyST_Object *right);
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000161static PyObject *parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000162
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000163
Fred Drake268397f1998-04-29 14:16:32 +0000164static
Fred Drakec2683dd2001-07-17 19:32:05 +0000165PyTypeObject PyST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000166 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000167 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000168 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000169 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000170 0, /* tp_itemsize */
171 (destructor)parser_free, /* tp_dealloc */
172 0, /* tp_print */
173 parser_getattr, /* tp_getattr */
174 0, /* tp_setattr */
175 (cmpfunc)parser_compare, /* tp_compare */
176 0, /* tp_repr */
177 0, /* tp_as_number */
178 0, /* tp_as_sequence */
179 0, /* tp_as_mapping */
180 0, /* tp_hash */
181 0, /* tp_call */
182 0, /* tp_str */
183 0, /* tp_getattro */
184 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000185
186 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000187 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000188
Fred Drakeff9ea482000-04-19 13:54:15 +0000189 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000190
191 /* __doc__ */
192 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000193}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000194
195
196static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000197parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000198{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000199 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000200
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000201 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000202 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000203
204 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000205 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000206
207 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000208 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000209
210 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000211 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000212
213 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000214 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000215
216 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000217 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000218
Fred Drakeff9ea482000-04-19 13:54:15 +0000219 if (v != 0)
220 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000221 }
222 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000223}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000224
225
Fred Drakec2683dd2001-07-17 19:32:05 +0000226/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000227 *
228 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
229 * This really just wraps a call to parser_compare_nodes() with some easy
230 * checks and protection code.
231 *
232 */
233static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000234parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000235{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000236 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000237 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000238
239 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000240 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000241
Fred Drakec2683dd2001-07-17 19:32:05 +0000242 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000243}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000244
245
Fred Drakec2683dd2001-07-17 19:32:05 +0000246/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000248 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000249 * 'wrapper' object that holds a node* and allows it to be passed around in
250 * Python code.
251 *
252 */
253static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000254parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000255{
Fred Drakec2683dd2001-07-17 19:32:05 +0000256 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000257
258 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000259 o->st_node = st;
260 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000261 }
Fred Drake268397f1998-04-29 14:16:32 +0000262 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000263 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000264 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000266}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000267
268
Fred Drakec2683dd2001-07-17 19:32:05 +0000269/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000270 *
271 * This is called by a del statement that reduces the reference count to 0.
272 *
273 */
274static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000275parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000276{
Fred Drakec2683dd2001-07-17 19:32:05 +0000277 PyNode_Free(st->st_node);
278 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000279}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000280
281
Fred Drakec2683dd2001-07-17 19:32:05 +0000282/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000283 *
284 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000285 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000286 *
287 */
288static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000289parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000290{
Guido van Rossum47478871996-08-21 14:32:37 +0000291 PyObject *line_option = 0;
292 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000293 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000294
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000295 static char *keywords[] = {"ast", "line_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000296
Fred Drake268397f1998-04-29 14:16:32 +0000297 if (self == NULL) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000298 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2tuple", keywords,
299 &PyST_Type, &self, &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000300 }
Fred Drake503d8d61998-04-13 18:45:18 +0000301 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000302 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:totuple", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000303 &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000304 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000305 int lineno = 0;
306 if (line_option != NULL) {
307 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
308 }
309 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000310 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000311 * since it's known to work already.
312 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000313 res = node2tuple(((PyST_Object*)self)->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000314 PyTuple_New, PyTuple_SetItem, lineno);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000315 }
316 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000317}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000318
319
Fred Drakec2683dd2001-07-17 19:32:05 +0000320/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000321 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000322 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000323 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000324 *
325 */
326static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000327parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000328{
Guido van Rossum47478871996-08-21 14:32:37 +0000329 PyObject *line_option = 0;
330 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000331 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000332
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000333 static char *keywords[] = {"ast", "line_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000334
Fred Drake503d8d61998-04-13 18:45:18 +0000335 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000336 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2list", keywords,
337 &PyST_Type, &self, &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000338 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000339 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:tolist", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000340 &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000341 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000342 int lineno = 0;
343 if (line_option != 0) {
344 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
345 }
346 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000347 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000348 * since it's known to work already.
349 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000350 res = node2tuple(self->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000351 PyList_New, PyList_SetItem, lineno);
Guido van Rossum47478871996-08-21 14:32:37 +0000352 }
353 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000354}
Guido van Rossum47478871996-08-21 14:32:37 +0000355
356
Fred Drakec2683dd2001-07-17 19:32:05 +0000357/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000358 *
359 * This function creates code objects from the parse tree represented by
360 * the passed-in data object. An optional file name is passed in as well.
361 *
362 */
363static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000364parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000365{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000366 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000367 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000368 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000369
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000370 static char *keywords[] = {"ast", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000371
Fred Drake503d8d61998-04-13 18:45:18 +0000372 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000373 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
374 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000375 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000376 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000377 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000378
379 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000380 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000381
382 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000383}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000384
385
386/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
387 * PyObject* parser_issuite(PyObject* self, PyObject* args)
388 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000389 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000390 * a statement suite, respectively. The return is a Python truth value.
391 *
392 */
393static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000394parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000395{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000396 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000397 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000398
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000399 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000400
Fred Drake503d8d61998-04-13 18:45:18 +0000401 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000402 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000403 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000404 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000405 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000406
407 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000408 /* Check to see if the ST represents an expression or not. */
409 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000410 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000411 }
412 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000413}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000414
415
416static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000417parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000418{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000419 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000420 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000421
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000422 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000423
Fred Drake503d8d61998-04-13 18:45:18 +0000424 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000425 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000426 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000427 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000428 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000429
430 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000431 /* Check to see if the ST represents an expression or not. */
432 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000433 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000434 }
435 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000436}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000437
438
Fred Drake7a15ba51999-09-09 14:21:52 +0000439#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
440
Fred Drake503d8d61998-04-13 18:45:18 +0000441static PyMethodDef
442parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000443 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000444 PyDoc_STR("Compile this ST object into a code object.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000445 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000446 PyDoc_STR("Determines if this ST object was created from an expression.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000447 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000448 PyDoc_STR("Determines if this ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000449 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000450 PyDoc_STR("Creates a list-tree representation of this ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000451 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000452 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Fred Drake503d8d61998-04-13 18:45:18 +0000453
Fred Drake268397f1998-04-29 14:16:32 +0000454 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000455};
456
Fred Drake503d8d61998-04-13 18:45:18 +0000457
458static PyObject*
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000459parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000460{
Fred Drake503d8d61998-04-13 18:45:18 +0000461 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000462}
Fred Drake503d8d61998-04-13 18:45:18 +0000463
464
Guido van Rossum3d602e31996-07-21 02:33:56 +0000465/* err_string(char* message)
466 *
467 * Sets the error string for an exception of type ParserError.
468 *
469 */
470static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000471err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000472{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000473 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000474}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000475
476
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000477/* PyObject* parser_do_parse(PyObject* args, int type)
478 *
479 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000480 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000481 *
482 */
483static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000484parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000485{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000486 char* string = 0;
487 PyObject* res = 0;
488
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000489 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000490
491 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000492 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000493 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000494 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000495
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000496 if (n)
497 res = parser_newstobject(n, type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000498 }
499 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000500}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000501
502
503/* PyObject* parser_expr(PyObject* self, PyObject* args)
504 * PyObject* parser_suite(PyObject* self, PyObject* args)
505 *
506 * External interfaces to the parser itself. Which is called determines if
507 * the parser attempts to recognize an expression ('eval' form) or statement
508 * suite ('exec' form). The real work is done by parser_do_parse() above.
509 *
510 */
511static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000512parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000513{
Fred Drake268397f1998-04-29 14:16:32 +0000514 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000515 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000516}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000517
518
519static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000520parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000521{
Fred Drake268397f1998-04-29 14:16:32 +0000522 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000523 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000524}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000525
526
527
Fred Drakec2683dd2001-07-17 19:32:05 +0000528/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000529 * object requires that the input tuple be valid without having to rely on
530 * catching an exception from the compiler. This is done to allow the
531 * compiler itself to remain fast, since most of its input will come from
532 * the parser directly, and therefore be known to be syntactically correct.
533 * This validation is done to ensure that we don't core dump the compile
534 * phase, returning an exception instead.
535 *
536 * Two aspects can be broken out in this code: creating a node tree from
537 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000538 * advantageous to expand the number of ST types to include funcdefs and
539 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000540 * here. They are not necessary, and not quite as useful in a raw form.
541 * For now, let's get expressions and suites working reliably.
542 */
543
544
Jeremy Hylton938ace62002-07-17 16:30:39 +0000545static node* build_node_tree(PyObject *tuple);
546static int validate_expr_tree(node *tree);
547static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000548static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000549
Fred Drakec2683dd2001-07-17 19:32:05 +0000550/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000551 *
552 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000553 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000554 * tuple can be validated. It does this by checking the first code of the
555 * tuple, and, if acceptable, builds the internal representation. If this
556 * step succeeds, the internal representation is validated as fully as
557 * possible with the various validate_*() routines defined below.
558 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000559 * This function must be changed if support is to be added for PyST_FRAGMENT
560 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000561 *
562 */
563static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000564parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000565{
Fred Drake268397f1998-04-29 14:16:32 +0000566 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000567 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000568 PyObject *tuple;
569 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000570
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000571 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000572
Fred Drakec2683dd2001-07-17 19:32:05 +0000573 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000574 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000575 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000576 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000577 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000578 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000579 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000580 }
581 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000582 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000583 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000584 tree = build_node_tree(tuple);
585 if (tree != 0) {
586 int start_sym = TYPE(tree);
587 if (start_sym == eval_input) {
588 /* Might be an eval form. */
589 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000590 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000591 else
592 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000593 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000594 else if (start_sym == file_input) {
595 /* This looks like an exec form so far. */
596 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000597 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000598 else
599 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000600 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000601 else if (start_sym == encoding_decl) {
602 /* This looks like an encoding_decl so far. */
603 if (validate_encoding_decl(tree))
604 st = parser_newstobject(tree, PyST_SUITE);
605 else
606 PyNode_Free(tree);
607 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000608 else {
609 /* This is a fragment, at best. */
610 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000611 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000612 }
Guido van Rossum47478871996-08-21 14:32:37 +0000613 }
Guido van Rossum47478871996-08-21 14:32:37 +0000614 /* Make sure we throw an exception on all errors. We should never
615 * get this, but we'd do well to be sure something is done.
616 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000617 if (st == NULL && !PyErr_Occurred())
618 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000619
Fred Drakec2683dd2001-07-17 19:32:05 +0000620 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000621}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000622
623
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000624/* node* build_node_children()
625 *
626 * Iterate across the children of the current non-terminal node and build
627 * their structures. If successful, return the root of this portion of
628 * the tree, otherwise, 0. Any required exception will be specified already,
629 * and no memory will have been deallocated.
630 *
631 */
632static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000633build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000634{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000635 Py_ssize_t len = PyObject_Size(tuple);
636 Py_ssize_t i;
637 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000638
639 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000640 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000641 PyObject* elem = PySequence_GetItem(tuple, i);
642 int ok = elem != NULL;
643 long type = 0;
644 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000645
Fred Drakeff9ea482000-04-19 13:54:15 +0000646 if (ok)
647 ok = PySequence_Check(elem);
648 if (ok) {
649 PyObject *temp = PySequence_GetItem(elem, 0);
650 if (temp == NULL)
651 ok = 0;
652 else {
653 ok = PyInt_Check(temp);
654 if (ok)
655 type = PyInt_AS_LONG(temp);
656 Py_DECREF(temp);
657 }
658 }
659 if (!ok) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000660 PyObject *err = Py_BuildValue("os", elem,
661 "Illegal node construct.");
662 PyErr_SetObject(parser_error, err);
663 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000664 Py_XDECREF(elem);
665 return (0);
666 }
667 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000668 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000669 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000670
Fred Drake0ac9b072000-09-12 21:58:06 +0000671 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000672 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000673 return 0;
674 }
675 temp = PySequence_GetItem(elem, 1);
676 if (temp == NULL)
677 return 0;
678 if (!PyString_Check(temp)) {
679 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000680 "second item in terminal node must be a string,"
681 " found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000682 temp->ob_type->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000683 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000684 return 0;
685 }
686 if (len == 3) {
687 PyObject *o = PySequence_GetItem(elem, 2);
688 if (o != NULL) {
689 if (PyInt_Check(o))
690 *line_num = PyInt_AS_LONG(o);
691 else {
692 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000693 "third item in terminal node must be an"
694 " integer, found %s",
Guido van Rossumfc296462003-04-09 17:53:22 +0000695 temp->ob_type->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000696 Py_DECREF(o);
697 Py_DECREF(temp);
698 return 0;
699 }
700 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000701 }
702 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000703 len = PyString_GET_SIZE(temp) + 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000704 strn = (char *)PyObject_MALLOC(len);
Fred Drake0ac9b072000-09-12 21:58:06 +0000705 if (strn != NULL)
706 (void) memcpy(strn, PyString_AS_STRING(temp), len);
707 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000708 }
709 else if (!ISNONTERMINAL(type)) {
710 /*
711 * It has to be one or the other; this is an error.
712 * Throw an exception.
713 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000714 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
715 PyErr_SetObject(parser_error, err);
716 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000717 Py_XDECREF(elem);
718 return (0);
719 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000720 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000721 if (err == E_NOMEM) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000722 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000723 return (node *) PyErr_NoMemory();
724 }
725 if (err == E_OVERFLOW) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000726 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000727 PyErr_SetString(PyExc_ValueError,
728 "unsupported number of child nodes");
729 return NULL;
730 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000731
Fred Drakeff9ea482000-04-19 13:54:15 +0000732 if (ISNONTERMINAL(type)) {
733 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000734
Fred Drakeff9ea482000-04-19 13:54:15 +0000735 if (new_child != build_node_children(elem, new_child, line_num)) {
736 Py_XDECREF(elem);
737 return (0);
738 }
739 }
740 else if (type == NEWLINE) { /* It's true: we increment the */
741 ++(*line_num); /* line number *after* the newline! */
742 }
743 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000744 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000745 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000746}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000747
748
749static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000750build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000751{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000752 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000753 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000754 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000755
Guido van Rossum47478871996-08-21 14:32:37 +0000756 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000757 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000758 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000759 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000760 /*
761 * The tuple is simple, but it doesn't start with a start symbol.
762 * Throw an exception now and be done with it.
763 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000764 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000765 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000766 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000767 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000768 }
769 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000770 /*
771 * Not efficient, but that can be handled later.
772 */
773 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000774 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000775
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000776 if (num == encoding_decl) {
777 encoding = PySequence_GetItem(tuple, 2);
778 /* tuple isn't borrowed anymore here, need to DECREF */
779 tuple = PySequence_GetSlice(tuple, 0, 2);
780 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000781 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000782 if (res != NULL) {
783 if (res != build_node_children(tuple, res, &line_num)) {
784 PyNode_Free(res);
785 res = NULL;
786 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000787 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000788 Py_ssize_t len;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000789 len = PyString_GET_SIZE(encoding) + 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000790 res->n_str = (char *)PyObject_MALLOC(len);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000791 if (res->n_str != NULL)
792 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
793 Py_DECREF(encoding);
794 Py_DECREF(tuple);
795 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000796 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000797 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000798 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000799 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000800 * NONTERMINAL, we can't use it. Not sure the implementation
801 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000802 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000803 PyObject *err = Py_BuildValue("os", tuple,
804 "Illegal component tuple.");
805 PyErr_SetObject(parser_error, err);
806 Py_XDECREF(err);
807 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000808
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000809 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000810}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000811
812
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000813/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000814 * Validation routines used within the validation section:
815 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000816static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000817
Fred Drakeff9ea482000-04-19 13:54:15 +0000818#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
819#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
820#define validate_colon(ch) validate_terminal(ch, COLON, ":")
821#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
822#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
823#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
824#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
825#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
826#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
827#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
828#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
829#define validate_star(ch) validate_terminal(ch, STAR, "*")
830#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
831#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
832#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000833#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000834#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000835
Fred Drake0ac9b072000-09-12 21:58:06 +0000836#define VALIDATER(n) static int validate_##n(node *tree)
837
Fred Drakeff9ea482000-04-19 13:54:15 +0000838VALIDATER(node); VALIDATER(small_stmt);
839VALIDATER(class); VALIDATER(node);
840VALIDATER(parameters); VALIDATER(suite);
841VALIDATER(testlist); VALIDATER(varargslist);
842VALIDATER(fpdef); VALIDATER(fplist);
843VALIDATER(stmt); VALIDATER(simple_stmt);
844VALIDATER(expr_stmt); VALIDATER(power);
845VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000846VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000847VALIDATER(raise_stmt); VALIDATER(import_stmt);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000848VALIDATER(import_name); VALIDATER(import_from);
Fred Drakecff283c2000-08-21 22:24:43 +0000849VALIDATER(global_stmt); VALIDATER(list_if);
850VALIDATER(assert_stmt); VALIDATER(list_for);
Georg Brandl7cae87c2006-09-06 06:51:57 +0000851VALIDATER(compound_stmt);
Fred Drakeff9ea482000-04-19 13:54:15 +0000852VALIDATER(while); VALIDATER(for);
853VALIDATER(try); VALIDATER(except_clause);
854VALIDATER(test); VALIDATER(and_test);
855VALIDATER(not_test); VALIDATER(comparison);
856VALIDATER(comp_op); VALIDATER(expr);
857VALIDATER(xor_expr); VALIDATER(and_expr);
858VALIDATER(shift_expr); VALIDATER(arith_expr);
859VALIDATER(term); VALIDATER(factor);
860VALIDATER(atom); VALIDATER(lambdef);
861VALIDATER(trailer); VALIDATER(subscript);
862VALIDATER(subscriptlist); VALIDATER(sliceop);
Neal Norwitz6309f2d2006-08-29 05:53:33 +0000863VALIDATER(exprlist); VALIDATER(dictsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000864VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000865VALIDATER(listmaker); VALIDATER(yield_stmt);
Raymond Hettinger354433a2004-05-19 08:20:33 +0000866VALIDATER(testlist1); VALIDATER(gen_for);
867VALIDATER(gen_iter); VALIDATER(gen_if);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000868VALIDATER(testlist_gexp); VALIDATER(yield_expr);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +0000869VALIDATER(yield_or_testlist); VALIDATER(or_test);
870VALIDATER(old_test); VALIDATER(old_lambdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000871
Fred Drake0ac9b072000-09-12 21:58:06 +0000872#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000873
Fred Drakeff9ea482000-04-19 13:54:15 +0000874#define is_even(n) (((n) & 1) == 0)
875#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000876
877
878static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000879validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000880{
Fred Drake0ac9b072000-09-12 21:58:06 +0000881 if (TYPE(n) != t) {
882 PyErr_Format(parser_error, "Expected node type %d, got %d.",
883 t, TYPE(n));
884 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000885 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000886 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000887}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000888
889
Fred Drakee7ab64e2000-04-25 04:14:46 +0000890/* Verifies that the number of child nodes is exactly 'num', raising
891 * an exception if it isn't. The exception message does not indicate
892 * the exact number of nodes, allowing this to be used to raise the
893 * "right" exception when the wrong number of nodes is present in a
894 * specific variant of a statement's syntax. This is commonly used
895 * in that fashion.
896 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000897static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000898validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000899{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000900 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000901 PyErr_Format(parser_error,
902 "Illegal number of children for %s node.", name);
903 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000904 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000905 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000906}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000907
908
909static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000910validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000911{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000912 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000913 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000914
915 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000916 PyErr_Format(parser_error,
917 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000918 }
919 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000920}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000921
922
Guido van Rossum47478871996-08-21 14:32:37 +0000923/* X (',' X) [',']
924 */
925static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000926validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000927 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000928{
929 int nch = NCH(tree);
930 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000931 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000932
933 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000934 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000935 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000936 if (is_even(nch))
937 res = validate_comma(CHILD(tree, --nch));
938 if (res && nch > 1) {
939 int pos = 1;
940 for ( ; res && pos < nch; pos += 2)
941 res = (validate_comma(CHILD(tree, pos))
942 && vfunc(CHILD(tree, pos + 1)));
943 }
Guido van Rossum47478871996-08-21 14:32:37 +0000944 }
945 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000946}
Guido van Rossum47478871996-08-21 14:32:37 +0000947
948
Fred Drakecff283c2000-08-21 22:24:43 +0000949/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000950 *
951 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000952 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000953 */
Guido van Rossum47478871996-08-21 14:32:37 +0000954static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000955validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000956{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000957 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +0000958 int res = (validate_ntype(tree, classdef) &&
959 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000960
Guido van Rossum3d602e31996-07-21 02:33:56 +0000961 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000962 res = (validate_name(CHILD(tree, 0), "class")
963 && validate_ntype(CHILD(tree, 1), NAME)
964 && validate_colon(CHILD(tree, nch - 2))
965 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000966 }
Brett Cannonf4189912005-04-09 02:30:16 +0000967 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000968 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +0000969 }
970
971 if (res) {
972 if (nch == 7) {
973 res = ((validate_lparen(CHILD(tree, 2)) &&
974 validate_testlist(CHILD(tree, 3)) &&
975 validate_rparen(CHILD(tree, 4))));
976 }
977 else if (nch == 6) {
978 res = (validate_lparen(CHILD(tree,2)) &&
979 validate_rparen(CHILD(tree,3)));
980 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000981 }
982 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000983}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000984
985
Guido van Rossum3d602e31996-07-21 02:33:56 +0000986/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +0000987 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +0000988 */
Guido van Rossum47478871996-08-21 14:32:37 +0000989static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000990validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000991{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000992 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000993 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +0000994 && (nch >= 4)
995 && validate_name(CHILD(tree, 0), "if")
996 && validate_test(CHILD(tree, 1))
997 && validate_colon(CHILD(tree, 2))
998 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000999
1000 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001001 /* ... 'else' ':' suite */
1002 res = (validate_name(CHILD(tree, nch - 3), "else")
1003 && validate_colon(CHILD(tree, nch - 2))
1004 && validate_suite(CHILD(tree, nch - 1)));
1005 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001006 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001007 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001008 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001009 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001010 /* Will catch the case for nch < 4 */
1011 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001012 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001013 /* ... ('elif' test ':' suite)+ ... */
1014 int j = 4;
1015 while ((j < nch) && res) {
1016 res = (validate_name(CHILD(tree, j), "elif")
1017 && validate_colon(CHILD(tree, j + 2))
1018 && validate_test(CHILD(tree, j + 1))
1019 && validate_suite(CHILD(tree, j + 3)));
1020 j += 4;
1021 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001022 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001023 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001024}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001025
1026
Guido van Rossum3d602e31996-07-21 02:33:56 +00001027/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001028 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001029 *
1030 */
Guido van Rossum47478871996-08-21 14:32:37 +00001031static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001032validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001033{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001034 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001035 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001036
Guido van Rossum3d602e31996-07-21 02:33:56 +00001037 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001038 res = (validate_lparen(CHILD(tree, 0))
1039 && validate_rparen(CHILD(tree, nch - 1)));
1040 if (res && (nch == 3))
1041 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001042 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001043 else {
1044 (void) validate_numnodes(tree, 2, "parameters");
1045 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001046 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001047}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001048
1049
Fred Drakecff283c2000-08-21 22:24:43 +00001050/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001051 *
1052 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001053 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001054 * | NEWLINE INDENT stmt+ DEDENT
1055 */
Guido van Rossum47478871996-08-21 14:32:37 +00001056static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001057validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001058{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001059 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001060 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001061
Guido van Rossum3d602e31996-07-21 02:33:56 +00001062 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001063 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001064 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001065 /* NEWLINE INDENT stmt+ DEDENT */
1066 res = (validate_newline(CHILD(tree, 0))
1067 && validate_indent(CHILD(tree, 1))
1068 && validate_stmt(CHILD(tree, 2))
1069 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001070
Fred Drakeff9ea482000-04-19 13:54:15 +00001071 if (res && (nch > 4)) {
1072 int i = 3;
1073 --nch; /* forget the DEDENT */
1074 for ( ; res && (i < nch); ++i)
1075 res = validate_stmt(CHILD(tree, i));
1076 }
1077 else if (nch < 4)
1078 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001079 }
1080 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001081}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001082
1083
Guido van Rossum47478871996-08-21 14:32:37 +00001084static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001085validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001086{
Guido van Rossum47478871996-08-21 14:32:37 +00001087 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001088 validate_test, "testlist"));
1089}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001090
1091
Guido van Rossum1c917072001-10-15 15:44:05 +00001092static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001093validate_testlist1(node *tree)
1094{
1095 return (validate_repeating_list(tree, testlist1,
1096 validate_test, "testlist1"));
1097}
1098
1099
1100static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001101validate_testlist_safe(node *tree)
1102{
1103 return (validate_repeating_list(tree, testlist_safe,
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001104 validate_old_test, "testlist_safe"));
Guido van Rossum1c917072001-10-15 15:44:05 +00001105}
1106
1107
Fred Drakecff283c2000-08-21 22:24:43 +00001108/* '*' NAME [',' '**' NAME] | '**' NAME
1109 */
1110static int
1111validate_varargslist_trailer(node *tree, int start)
1112{
1113 int nch = NCH(tree);
1114 int res = 0;
1115 int sym;
1116
1117 if (nch <= start) {
1118 err_string("expected variable argument trailer for varargslist");
1119 return 0;
1120 }
1121 sym = TYPE(CHILD(tree, start));
1122 if (sym == STAR) {
1123 /*
1124 * ('*' NAME [',' '**' NAME]
1125 */
1126 if (nch-start == 2)
1127 res = validate_name(CHILD(tree, start+1), NULL);
1128 else if (nch-start == 5)
1129 res = (validate_name(CHILD(tree, start+1), NULL)
1130 && validate_comma(CHILD(tree, start+2))
1131 && validate_doublestar(CHILD(tree, start+3))
1132 && validate_name(CHILD(tree, start+4), NULL));
1133 }
1134 else if (sym == DOUBLESTAR) {
1135 /*
1136 * '**' NAME
1137 */
1138 if (nch-start == 2)
1139 res = validate_name(CHILD(tree, start+1), NULL);
1140 }
1141 if (!res)
1142 err_string("illegal variable argument trailer for varargslist");
1143 return res;
1144}
1145
1146
1147/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001148 *
1149 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001150 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001151 * ('*' NAME [',' '**' NAME]
1152 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001153 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1154 *
1155 */
Guido van Rossum47478871996-08-21 14:32:37 +00001156static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001157validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001158{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001159 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001160 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001161 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001162
Fred Drakeb6429a22000-12-11 22:08:27 +00001163 if (!res)
1164 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001165 if (nch < 1) {
1166 err_string("varargslist missing child nodes");
1167 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001168 }
Fred Drakecff283c2000-08-21 22:24:43 +00001169 sym = TYPE(CHILD(tree, 0));
1170 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001171 /* whole thing matches:
1172 * '*' NAME [',' '**' NAME] | '**' NAME
1173 */
Fred Drakecff283c2000-08-21 22:24:43 +00001174 res = validate_varargslist_trailer(tree, 0);
1175 else if (sym == fpdef) {
1176 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001177
Fred Drakecff283c2000-08-21 22:24:43 +00001178 sym = TYPE(CHILD(tree, nch-1));
1179 if (sym == NAME) {
1180 /*
1181 * (fpdef ['=' test] ',')+
1182 * ('*' NAME [',' '**' NAME]
1183 * | '**' NAME)
1184 */
1185 /* skip over (fpdef ['=' test] ',')+ */
1186 while (res && (i+2 <= nch)) {
1187 res = validate_fpdef(CHILD(tree, i));
1188 ++i;
1189 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1190 res = (validate_equal(CHILD(tree, i))
1191 && validate_test(CHILD(tree, i+1)));
1192 if (res)
1193 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001194 }
Fred Drakecff283c2000-08-21 22:24:43 +00001195 if (res && i < nch) {
1196 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001197 ++i;
1198 if (res && i < nch
1199 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1200 || TYPE(CHILD(tree, i)) == STAR))
1201 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001202 }
1203 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001204 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1205 * i --^^^
1206 */
Fred Drakecff283c2000-08-21 22:24:43 +00001207 if (res)
1208 res = validate_varargslist_trailer(tree, i);
1209 }
1210 else {
1211 /*
1212 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1213 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001214 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001215 if (sym == COMMA) {
1216 res = validate_comma(CHILD(tree, nch-1));
1217 if (!res)
1218 return 0;
1219 --nch;
1220 }
1221 /*
1222 * fpdef ['=' test] (',' fpdef ['=' test])*
1223 */
1224 res = validate_fpdef(CHILD(tree, 0));
1225 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001226 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1227 res = (validate_equal(CHILD(tree, i))
1228 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001229 i += 2;
1230 }
1231 /*
1232 * ... (',' fpdef ['=' test])*
1233 * i ---^^^
1234 */
1235 while (res && (nch - i) >= 2) {
1236 res = (validate_comma(CHILD(tree, i))
1237 && validate_fpdef(CHILD(tree, i+1)));
1238 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001239 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1240 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001241 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001242 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001243 }
1244 }
1245 if (res && nch - i != 0) {
1246 res = 0;
1247 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001248 }
1249 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001250 }
Fred Drakecff283c2000-08-21 22:24:43 +00001251 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001252}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001253
1254
Fred Drakecff283c2000-08-21 22:24:43 +00001255/* list_iter: list_for | list_if
1256 */
1257static int
1258validate_list_iter(node *tree)
1259{
1260 int res = (validate_ntype(tree, list_iter)
1261 && validate_numnodes(tree, 1, "list_iter"));
1262 if (res && TYPE(CHILD(tree, 0)) == list_for)
1263 res = validate_list_for(CHILD(tree, 0));
1264 else
1265 res = validate_list_if(CHILD(tree, 0));
1266
1267 return res;
1268}
1269
Raymond Hettinger354433a2004-05-19 08:20:33 +00001270/* gen_iter: gen_for | gen_if
1271 */
1272static int
1273validate_gen_iter(node *tree)
1274{
1275 int res = (validate_ntype(tree, gen_iter)
1276 && validate_numnodes(tree, 1, "gen_iter"));
1277 if (res && TYPE(CHILD(tree, 0)) == gen_for)
1278 res = validate_gen_for(CHILD(tree, 0));
1279 else
1280 res = validate_gen_if(CHILD(tree, 0));
1281
1282 return res;
1283}
1284
Fred Drakecff283c2000-08-21 22:24:43 +00001285/* list_for: 'for' exprlist 'in' testlist [list_iter]
1286 */
1287static int
1288validate_list_for(node *tree)
1289{
1290 int nch = NCH(tree);
1291 int res;
1292
1293 if (nch == 5)
1294 res = validate_list_iter(CHILD(tree, 4));
1295 else
1296 res = validate_numnodes(tree, 4, "list_for");
1297
1298 if (res)
1299 res = (validate_name(CHILD(tree, 0), "for")
1300 && validate_exprlist(CHILD(tree, 1))
1301 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001302 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001303
1304 return res;
1305}
1306
Raymond Hettinger354433a2004-05-19 08:20:33 +00001307/* gen_for: 'for' exprlist 'in' test [gen_iter]
1308 */
1309static int
1310validate_gen_for(node *tree)
1311{
1312 int nch = NCH(tree);
1313 int res;
1314
1315 if (nch == 5)
1316 res = validate_gen_iter(CHILD(tree, 4));
1317 else
1318 res = validate_numnodes(tree, 4, "gen_for");
1319
1320 if (res)
1321 res = (validate_name(CHILD(tree, 0), "for")
1322 && validate_exprlist(CHILD(tree, 1))
1323 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001324 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001325
1326 return res;
1327}
1328
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001329/* list_if: 'if' old_test [list_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001330 */
1331static int
1332validate_list_if(node *tree)
1333{
1334 int nch = NCH(tree);
1335 int res;
1336
1337 if (nch == 3)
1338 res = validate_list_iter(CHILD(tree, 2));
1339 else
1340 res = validate_numnodes(tree, 2, "list_if");
1341
1342 if (res)
1343 res = (validate_name(CHILD(tree, 0), "if")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001344 && validate_old_test(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001345
1346 return res;
1347}
1348
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001349/* gen_if: 'if' old_test [gen_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001350 */
1351static int
1352validate_gen_if(node *tree)
1353{
1354 int nch = NCH(tree);
1355 int res;
1356
1357 if (nch == 3)
1358 res = validate_gen_iter(CHILD(tree, 2));
1359 else
1360 res = validate_numnodes(tree, 2, "gen_if");
1361
1362 if (res)
1363 res = (validate_name(CHILD(tree, 0), "if")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001364 && validate_old_test(CHILD(tree, 1)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001365
1366 return res;
1367}
Fred Drakecff283c2000-08-21 22:24:43 +00001368
1369/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001370 *
1371 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001372 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001373 * | '(' fplist ')'
1374 */
Guido van Rossum47478871996-08-21 14:32:37 +00001375static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001376validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001377{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001378 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001379 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001380
Guido van Rossum3d602e31996-07-21 02:33:56 +00001381 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001382 if (nch == 1)
1383 res = validate_ntype(CHILD(tree, 0), NAME);
1384 else if (nch == 3)
1385 res = (validate_lparen(CHILD(tree, 0))
1386 && validate_fplist(CHILD(tree, 1))
1387 && validate_rparen(CHILD(tree, 2)));
1388 else
1389 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001390 }
1391 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001392}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001393
1394
Guido van Rossum47478871996-08-21 14:32:37 +00001395static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001396validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001397{
Guido van Rossum47478871996-08-21 14:32:37 +00001398 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001399 validate_fpdef, "fplist"));
1400}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001401
1402
Guido van Rossum3d602e31996-07-21 02:33:56 +00001403/* simple_stmt | compound_stmt
1404 *
1405 */
Guido van Rossum47478871996-08-21 14:32:37 +00001406static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001407validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001408{
1409 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001410 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001411
Guido van Rossum3d602e31996-07-21 02:33:56 +00001412 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001413 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001414
Fred Drakeff9ea482000-04-19 13:54:15 +00001415 if (TYPE(tree) == simple_stmt)
1416 res = validate_simple_stmt(tree);
1417 else
1418 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001419 }
1420 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001421}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001422
1423
Guido van Rossum3d602e31996-07-21 02:33:56 +00001424/* small_stmt (';' small_stmt)* [';'] NEWLINE
1425 *
1426 */
Guido van Rossum47478871996-08-21 14:32:37 +00001427static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001428validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001429{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001430 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001431 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001432 && (nch >= 2)
1433 && validate_small_stmt(CHILD(tree, 0))
1434 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001435
Guido van Rossum3d602e31996-07-21 02:33:56 +00001436 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001437 res = validate_numnodes(tree, 2, "simple_stmt");
1438 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001439 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001440 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001441 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001442 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001443
Fred Drakeff9ea482000-04-19 13:54:15 +00001444 for (i = 1; res && (i < nch); i += 2)
1445 res = (validate_semi(CHILD(tree, i))
1446 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001447 }
1448 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001449}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001450
1451
Guido van Rossum47478871996-08-21 14:32:37 +00001452static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001453validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001454{
1455 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001456 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001457
Fred Drake0ac9b072000-09-12 21:58:06 +00001458 if (res) {
1459 int ntype = TYPE(CHILD(tree, 0));
1460
1461 if ( (ntype == expr_stmt)
1462 || (ntype == print_stmt)
1463 || (ntype == del_stmt)
1464 || (ntype == pass_stmt)
1465 || (ntype == flow_stmt)
1466 || (ntype == import_stmt)
1467 || (ntype == global_stmt)
Georg Brandl7cae87c2006-09-06 06:51:57 +00001468 || (ntype == assert_stmt))
Fred Drake0ac9b072000-09-12 21:58:06 +00001469 res = validate_node(CHILD(tree, 0));
1470 else {
1471 res = 0;
1472 err_string("illegal small_stmt child type");
1473 }
1474 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001475 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001476 res = 0;
1477 PyErr_Format(parser_error,
1478 "Unrecognized child node of small_stmt: %d.",
1479 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001480 }
1481 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001482}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001483
1484
1485/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001486 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001487 */
Guido van Rossum47478871996-08-21 14:32:37 +00001488static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001489validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001490{
1491 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001492 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001493 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001494
1495 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001496 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001497
1498 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001499 ntype = TYPE(tree);
1500 if ( (ntype == if_stmt)
1501 || (ntype == while_stmt)
1502 || (ntype == for_stmt)
1503 || (ntype == try_stmt)
1504 || (ntype == funcdef)
1505 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001506 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001507 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001508 res = 0;
1509 PyErr_Format(parser_error,
1510 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001511 }
1512 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001513}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001514
1515
Guido van Rossum47478871996-08-21 14:32:37 +00001516static int
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001517validate_yield_or_testlist(node *tree)
1518{
1519 if (TYPE(tree) == yield_expr)
1520 return validate_yield_expr(tree);
1521 else
1522 return validate_testlist(tree);
1523}
1524
1525static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001526validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001527{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001528 int j;
1529 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001530 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001531 && is_odd(nch)
1532 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001533
Fred Drake28f739a2000-08-25 22:42:40 +00001534 if (res && nch == 3
1535 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001536 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1537 && validate_yield_or_testlist(CHILD(tree, 2));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001538
Fred Drake28f739a2000-08-25 22:42:40 +00001539 if (res) {
1540 char *s = STR(CHILD(CHILD(tree, 1), 0));
1541
1542 res = (strcmp(s, "+=") == 0
1543 || strcmp(s, "-=") == 0
1544 || strcmp(s, "*=") == 0
1545 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001546 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001547 || strcmp(s, "%=") == 0
1548 || strcmp(s, "&=") == 0
1549 || strcmp(s, "|=") == 0
1550 || strcmp(s, "^=") == 0
1551 || strcmp(s, "<<=") == 0
1552 || strcmp(s, ">>=") == 0
1553 || strcmp(s, "**=") == 0);
1554 if (!res)
1555 err_string("illegal augmmented assignment operator");
1556 }
1557 }
1558 else {
1559 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001560 res = validate_equal(CHILD(tree, j))
1561 && validate_yield_or_testlist(CHILD(tree, j + 1));
Fred Drake28f739a2000-08-25 22:42:40 +00001562 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001563 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001564}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001565
1566
Guido van Rossum3d602e31996-07-21 02:33:56 +00001567/* print_stmt:
1568 *
Fred Drakecff283c2000-08-21 22:24:43 +00001569 * 'print' ( [ test (',' test)* [','] ]
1570 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001571 */
Guido van Rossum47478871996-08-21 14:32:37 +00001572static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001573validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001574{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001575 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001576 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001577 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001578 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001579
Fred Drakecff283c2000-08-21 22:24:43 +00001580 if (res && nch > 1) {
1581 int sym = TYPE(CHILD(tree, 1));
1582 int i = 1;
1583 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001584
Fred Drakecff283c2000-08-21 22:24:43 +00001585 if (sym == test)
1586 res = validate_test(CHILD(tree, i++));
1587 else {
1588 if (nch < 3)
1589 res = validate_numnodes(tree, 3, "print_stmt");
1590 else {
1591 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1592 && validate_test(CHILD(tree, i+1)));
1593 i += 2;
1594 allow_trailing_comma = 0;
1595 }
1596 }
1597 if (res) {
1598 /* ... (',' test)* [','] */
1599 while (res && i+2 <= nch) {
1600 res = (validate_comma(CHILD(tree, i))
1601 && validate_test(CHILD(tree, i+1)));
1602 allow_trailing_comma = 1;
1603 i += 2;
1604 }
1605 if (res && !allow_trailing_comma)
1606 res = validate_numnodes(tree, i, "print_stmt");
1607 else if (res && i < nch)
1608 res = validate_comma(CHILD(tree, i));
1609 }
1610 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001611 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001612}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001613
1614
Guido van Rossum47478871996-08-21 14:32:37 +00001615static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001616validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001617{
1618 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001619 && validate_name(CHILD(tree, 0), "del")
1620 && validate_exprlist(CHILD(tree, 1)));
1621}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001622
1623
Guido van Rossum47478871996-08-21 14:32:37 +00001624static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001625validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001626{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001627 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001628 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001629 && ((nch == 1) || (nch == 2))
1630 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001631
Guido van Rossum3d602e31996-07-21 02:33:56 +00001632 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001633 res = validate_testlist(CHILD(tree, 1));
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
Guido van Rossum47478871996-08-21 14:32:37 +00001639static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001640validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001641{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001642 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001643 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001644 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001645
Guido van Rossum3d602e31996-07-21 02:33:56 +00001646 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001647 res = validate_name(CHILD(tree, 0), "raise");
1648 if (res && (nch >= 2))
1649 res = validate_test(CHILD(tree, 1));
1650 if (res && nch > 2) {
1651 res = (validate_comma(CHILD(tree, 2))
1652 && validate_test(CHILD(tree, 3)));
1653 if (res && (nch > 4))
1654 res = (validate_comma(CHILD(tree, 4))
1655 && validate_test(CHILD(tree, 5)));
1656 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001657 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001658 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001659 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001660 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001661 res = (validate_comma(CHILD(tree, 2))
1662 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001663
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001664 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001665}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001666
1667
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001668/* yield_expr: 'yield' [testlist]
1669 */
1670static int
1671validate_yield_expr(node *tree)
1672{
1673 int nch = NCH(tree);
1674 int res = (validate_ntype(tree, yield_expr)
1675 && ((nch == 1) || (nch == 2))
1676 && validate_name(CHILD(tree, 0), "yield"));
1677
1678 if (res && (nch == 2))
1679 res = validate_testlist(CHILD(tree, 1));
1680
1681 return (res);
1682}
1683
1684
1685/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001686 */
1687static int
1688validate_yield_stmt(node *tree)
1689{
1690 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001691 && validate_numnodes(tree, 1, "yield_stmt")
1692 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001693}
1694
1695
Fred Drakecff283c2000-08-21 22:24:43 +00001696static int
1697validate_import_as_name(node *tree)
1698{
1699 int nch = NCH(tree);
1700 int ok = validate_ntype(tree, import_as_name);
1701
1702 if (ok) {
1703 if (nch == 1)
1704 ok = validate_name(CHILD(tree, 0), NULL);
1705 else if (nch == 3)
1706 ok = (validate_name(CHILD(tree, 0), NULL)
1707 && validate_name(CHILD(tree, 1), "as")
1708 && validate_name(CHILD(tree, 2), NULL));
1709 else
1710 ok = validate_numnodes(tree, 3, "import_as_name");
1711 }
1712 return ok;
1713}
1714
1715
Fred Drake71137082001-01-07 05:59:59 +00001716/* dotted_name: NAME ("." NAME)*
1717 */
1718static int
1719validate_dotted_name(node *tree)
1720{
1721 int nch = NCH(tree);
1722 int res = (validate_ntype(tree, dotted_name)
1723 && is_odd(nch)
1724 && validate_name(CHILD(tree, 0), NULL));
1725 int i;
1726
1727 for (i = 1; res && (i < nch); i += 2) {
1728 res = (validate_dot(CHILD(tree, i))
1729 && validate_name(CHILD(tree, i+1), NULL));
1730 }
1731 return res;
1732}
1733
1734
Fred Drakecff283c2000-08-21 22:24:43 +00001735/* dotted_as_name: dotted_name [NAME NAME]
1736 */
1737static int
1738validate_dotted_as_name(node *tree)
1739{
1740 int nch = NCH(tree);
1741 int res = validate_ntype(tree, dotted_as_name);
1742
1743 if (res) {
1744 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001745 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001746 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001747 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001748 && validate_name(CHILD(tree, 1), "as")
1749 && validate_name(CHILD(tree, 2), NULL));
1750 else {
1751 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001752 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001753 }
1754 }
1755 return res;
1756}
1757
1758
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001759/* dotted_as_name (',' dotted_as_name)* */
1760static int
1761validate_dotted_as_names(node *tree)
1762{
1763 int nch = NCH(tree);
1764 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1765 int i;
1766
1767 for (i = 1; res && (i < nch); i += 2)
1768 res = (validate_comma(CHILD(tree, i))
1769 && validate_dotted_as_name(CHILD(tree, i + 1)));
1770 return (res);
1771}
1772
1773
1774/* import_as_name (',' import_as_name)* [','] */
1775static int
1776validate_import_as_names(node *tree)
1777{
1778 int nch = NCH(tree);
1779 int res = validate_import_as_name(CHILD(tree, 0));
1780 int i;
1781
1782 for (i = 1; res && (i + 1 < nch); i += 2)
1783 res = (validate_comma(CHILD(tree, i))
1784 && validate_import_as_name(CHILD(tree, i + 1)));
1785 return (res);
1786}
1787
1788
1789/* 'import' dotted_as_names */
1790static int
1791validate_import_name(node *tree)
1792{
1793 return (validate_ntype(tree, import_name)
1794 && validate_numnodes(tree, 2, "import_name")
1795 && validate_name(CHILD(tree, 0), "import")
1796 && validate_dotted_as_names(CHILD(tree, 1)));
1797}
1798
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001799/* Helper function to count the number of leading dots in
1800 * 'from ...module import name'
1801 */
1802static int
1803count_from_dots(node *tree)
1804{
1805 int i;
1806 for (i = 0; i < NCH(tree); i++)
1807 if (TYPE(CHILD(tree, i)) != DOT)
1808 break;
1809 return i;
1810}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001811
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001812/* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001813 * import_as_names
Guido van Rossum3d602e31996-07-21 02:33:56 +00001814 */
Guido van Rossum47478871996-08-21 14:32:37 +00001815static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001816validate_import_from(node *tree)
1817{
1818 int nch = NCH(tree);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001819 int ndots = count_from_dots(tree);
1820 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1821 int offset = ndots + havename;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001822 int res = validate_ntype(tree, import_from)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001823 && (nch >= 4 + ndots)
1824 && validate_name(CHILD(tree, 0), "from")
1825 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1826 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001827
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001828 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1829 res = ((nch == offset + 5)
1830 && validate_lparen(CHILD(tree, offset + 2))
1831 && validate_import_as_names(CHILD(tree, offset + 3))
1832 && validate_rparen(CHILD(tree, offset + 4)));
1833 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1834 res = validate_import_as_names(CHILD(tree, offset + 2));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001835 return (res);
1836}
1837
1838
1839/* import_stmt: import_name | import_from */
1840static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001841validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001842{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001843 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001844 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001845
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001846 if (res) {
1847 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001848
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001849 if (ntype == import_name || ntype == import_from)
1850 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001851 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001852 res = 0;
1853 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001854 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001855 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001856 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001857 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001858 PyErr_Format(parser_error,
1859 "Unrecognized child node of import_stmt: %d.",
1860 TYPE(CHILD(tree, 0)));
1861 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001862 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001863}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001864
1865
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001866
1867
Guido van Rossum47478871996-08-21 14:32:37 +00001868static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001869validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001870{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001871 int j;
1872 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001873 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001874 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001875
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001876 if (!res && !PyErr_Occurred())
1877 err_string("illegal global statement");
1878
Guido van Rossum3d602e31996-07-21 02:33:56 +00001879 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001880 res = (validate_name(CHILD(tree, 0), "global")
1881 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001882 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001883 res = (validate_comma(CHILD(tree, j))
1884 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001885
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001886 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001887}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001888
1889
Guido van Rossum925e5471997-04-02 05:32:13 +00001890/* assert_stmt:
1891 *
1892 * 'assert' test [',' test]
1893 */
1894static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001895validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001896{
1897 int nch = NCH(tree);
1898 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001899 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001900 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001901 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001902
1903 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001904 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001905 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001906 res = (validate_comma(CHILD(tree, 2))
1907 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001908
1909 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001910}
Guido van Rossum925e5471997-04-02 05:32:13 +00001911
1912
Guido van Rossum47478871996-08-21 14:32:37 +00001913static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001914validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001915{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001916 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001917 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001918 && ((nch == 4) || (nch == 7))
1919 && validate_name(CHILD(tree, 0), "while")
1920 && validate_test(CHILD(tree, 1))
1921 && validate_colon(CHILD(tree, 2))
1922 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001923
Guido van Rossum3d602e31996-07-21 02:33:56 +00001924 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001925 res = (validate_name(CHILD(tree, 4), "else")
1926 && validate_colon(CHILD(tree, 5))
1927 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001928
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001929 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001930}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001931
1932
Guido van Rossum47478871996-08-21 14:32:37 +00001933static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001934validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001935{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001936 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001937 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001938 && ((nch == 6) || (nch == 9))
1939 && validate_name(CHILD(tree, 0), "for")
1940 && validate_exprlist(CHILD(tree, 1))
1941 && validate_name(CHILD(tree, 2), "in")
1942 && validate_testlist(CHILD(tree, 3))
1943 && validate_colon(CHILD(tree, 4))
1944 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001945
Guido van Rossum3d602e31996-07-21 02:33:56 +00001946 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001947 res = (validate_name(CHILD(tree, 6), "else")
1948 && validate_colon(CHILD(tree, 7))
1949 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001950
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001951 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001952}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001953
1954
Guido van Rossum3d602e31996-07-21 02:33:56 +00001955/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001956 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001957 * | 'try' ':' suite 'finally' ':' suite
1958 *
1959 */
Guido van Rossum47478871996-08-21 14:32:37 +00001960static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001961validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001962{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001963 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001964 int pos = 3;
1965 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001966 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001967
1968 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001969 res = (validate_name(CHILD(tree, 0), "try")
1970 && validate_colon(CHILD(tree, 1))
1971 && validate_suite(CHILD(tree, 2))
1972 && validate_colon(CHILD(tree, nch - 2))
1973 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001974 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001975 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001976 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1977 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001978
1979 PyErr_Format(parser_error,
1980 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001981 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001982 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001983 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001984 res = (validate_except_clause(CHILD(tree, pos))
1985 && validate_colon(CHILD(tree, pos + 1))
1986 && validate_suite(CHILD(tree, pos + 2)));
1987 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001988 }
1989 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001990 res = validate_ntype(CHILD(tree, pos), NAME);
1991 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1992 res = (validate_numnodes(tree, 6, "try/finally")
1993 && validate_colon(CHILD(tree, 4))
1994 && validate_suite(CHILD(tree, 5)));
1995 else if (res) {
1996 if (nch == (pos + 3)) {
1997 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1998 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1999 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00002000 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00002001 }
2002 else if (nch == (pos + 6)) {
2003 res = (validate_name(CHILD(tree, pos), "except")
2004 && validate_colon(CHILD(tree, pos + 1))
2005 && validate_suite(CHILD(tree, pos + 2))
2006 && validate_name(CHILD(tree, pos + 3), "else"));
2007 }
2008 else
2009 res = validate_numnodes(tree, pos + 3, "try/except");
2010 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002011 }
2012 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002013}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002014
2015
Guido van Rossum47478871996-08-21 14:32:37 +00002016static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002017validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002018{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002019 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002020 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00002021 && ((nch == 1) || (nch == 2) || (nch == 4))
2022 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002023
Guido van Rossum3d602e31996-07-21 02:33:56 +00002024 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002025 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002026 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002027 res = (validate_comma(CHILD(tree, 2))
2028 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002029
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002030 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002031}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002032
2033
Guido van Rossum47478871996-08-21 14:32:37 +00002034static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002035validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002036{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002037 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002038 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002039
Guido van Rossum3d602e31996-07-21 02:33:56 +00002040 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002041 res = ((nch == 1)
2042 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002043 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002044 res = validate_or_test(CHILD(tree, 0));
2045 res = (res && (nch == 1 || (nch == 5 &&
2046 validate_name(CHILD(tree, 1), "if") &&
2047 validate_or_test(CHILD(tree, 2)) &&
2048 validate_name(CHILD(tree, 3), "else") &&
2049 validate_test(CHILD(tree, 4)))));
2050 }
2051 return (res);
2052}
2053
2054static int
2055validate_old_test(node *tree)
2056{
2057 int nch = NCH(tree);
2058 int res = validate_ntype(tree, old_test) && (nch == 1);
2059
2060 if (res && (TYPE(CHILD(tree, 0)) == old_lambdef))
2061 res = (validate_old_lambdef(CHILD(tree, 0)));
2062 else if (res) {
2063 res = (validate_or_test(CHILD(tree, 0)));
2064 }
2065 return (res);
2066}
2067
2068static int
2069validate_or_test(node *tree)
2070{
2071 int nch = NCH(tree);
2072 int res = validate_ntype(tree, or_test) && is_odd(nch);
2073
2074 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002075 int pos;
2076 res = validate_and_test(CHILD(tree, 0));
2077 for (pos = 1; res && (pos < nch); pos += 2)
2078 res = (validate_name(CHILD(tree, pos), "or")
2079 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002080 }
2081 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002082}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002083
2084
Guido van Rossum47478871996-08-21 14:32:37 +00002085static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002086validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002087{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002088 int pos;
2089 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002090 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002091 && is_odd(nch)
2092 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002093
Guido van Rossum3d602e31996-07-21 02:33:56 +00002094 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002095 res = (validate_name(CHILD(tree, pos), "and")
2096 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002097
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002098 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002099}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002100
2101
Guido van Rossum47478871996-08-21 14:32:37 +00002102static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002103validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002104{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002105 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002106 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002107
Guido van Rossum3d602e31996-07-21 02:33:56 +00002108 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002109 if (nch == 2)
2110 res = (validate_name(CHILD(tree, 0), "not")
2111 && validate_not_test(CHILD(tree, 1)));
2112 else if (nch == 1)
2113 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002114 }
2115 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002116}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002117
2118
Guido van Rossum47478871996-08-21 14:32:37 +00002119static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002120validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002121{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002122 int pos;
2123 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002124 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002125 && is_odd(nch)
2126 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002127
Guido van Rossum3d602e31996-07-21 02:33:56 +00002128 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002129 res = (validate_comp_op(CHILD(tree, pos))
2130 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002131
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002132 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002133}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002134
2135
Guido van Rossum47478871996-08-21 14:32:37 +00002136static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002137validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002138{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002139 int res = 0;
2140 int nch = NCH(tree);
2141
Guido van Rossum3d602e31996-07-21 02:33:56 +00002142 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002143 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002144 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002145 /*
2146 * Only child will be a terminal with a well-defined symbolic name
2147 * or a NAME with a string of either 'is' or 'in'
2148 */
2149 tree = CHILD(tree, 0);
2150 switch (TYPE(tree)) {
2151 case LESS:
2152 case GREATER:
2153 case EQEQUAL:
2154 case EQUAL:
2155 case LESSEQUAL:
2156 case GREATEREQUAL:
2157 case NOTEQUAL:
2158 res = 1;
2159 break;
2160 case NAME:
2161 res = ((strcmp(STR(tree), "in") == 0)
2162 || (strcmp(STR(tree), "is") == 0));
2163 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002164 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002165 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002166 }
2167 break;
2168 default:
Fred Drake661ea262000-10-24 19:57:45 +00002169 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002170 break;
2171 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002172 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002173 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002174 res = (validate_ntype(CHILD(tree, 0), NAME)
2175 && validate_ntype(CHILD(tree, 1), NAME)
2176 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2177 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2178 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2179 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2180 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002181 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002182 }
2183 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002184}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002185
2186
Guido van Rossum47478871996-08-21 14:32:37 +00002187static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002188validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002189{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002190 int j;
2191 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002192 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002193 && is_odd(nch)
2194 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002195
Guido van Rossum3d602e31996-07-21 02:33:56 +00002196 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002197 res = (validate_xor_expr(CHILD(tree, j))
2198 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002199
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002200 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002201}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002202
2203
Guido van Rossum47478871996-08-21 14:32:37 +00002204static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002205validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002206{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002207 int j;
2208 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002209 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002210 && is_odd(nch)
2211 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002212
Guido van Rossum3d602e31996-07-21 02:33:56 +00002213 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002214 res = (validate_circumflex(CHILD(tree, j - 1))
2215 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002216
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002217 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002218}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002219
2220
Guido van Rossum47478871996-08-21 14:32:37 +00002221static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002222validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002223{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002224 int pos;
2225 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002226 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002227 && is_odd(nch)
2228 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002229
Guido van Rossum3d602e31996-07-21 02:33:56 +00002230 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002231 res = (validate_ampersand(CHILD(tree, pos))
2232 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002233
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002234 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002235}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002236
2237
2238static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002239validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002240 {
2241 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002242 int nch = NCH(tree);
2243 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002244 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002245
Guido van Rossum3d602e31996-07-21 02:33:56 +00002246 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002247 if (TYPE(CHILD(tree, pos)) != op1)
2248 res = validate_ntype(CHILD(tree, pos), op2);
2249 if (res)
2250 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002251 }
2252 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002253}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002254
2255
Guido van Rossum47478871996-08-21 14:32:37 +00002256static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002257validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002258{
2259 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002260 && validate_chain_two_ops(tree, validate_arith_expr,
2261 LEFTSHIFT, RIGHTSHIFT));
2262}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002263
2264
Guido van Rossum47478871996-08-21 14:32:37 +00002265static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002266validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002267{
2268 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002269 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2270}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002271
2272
Guido van Rossum47478871996-08-21 14:32:37 +00002273static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002274validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002275{
2276 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002277 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002278 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002279 && is_odd(nch)
2280 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002281
Guido van Rossum3d602e31996-07-21 02:33:56 +00002282 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002283 res = (((TYPE(CHILD(tree, pos)) == STAR)
2284 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002285 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002286 || (TYPE(CHILD(tree, pos)) == PERCENT))
2287 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002288
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002289 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002290}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002291
2292
Guido van Rossum3d602e31996-07-21 02:33:56 +00002293/* factor:
2294 *
2295 * factor: ('+'|'-'|'~') factor | power
2296 */
Guido van Rossum47478871996-08-21 14:32:37 +00002297static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002298validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002299{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002300 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002301 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002302 && (((nch == 2)
2303 && ((TYPE(CHILD(tree, 0)) == PLUS)
2304 || (TYPE(CHILD(tree, 0)) == MINUS)
2305 || (TYPE(CHILD(tree, 0)) == TILDE))
2306 && validate_factor(CHILD(tree, 1)))
2307 || ((nch == 1)
2308 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002309 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002310}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002311
2312
Guido van Rossum3d602e31996-07-21 02:33:56 +00002313/* power:
2314 *
2315 * power: atom trailer* ('**' factor)*
2316 */
Guido van Rossum47478871996-08-21 14:32:37 +00002317static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002318validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002319{
2320 int pos = 1;
2321 int nch = NCH(tree);
2322 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002323 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002324
2325 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002326 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002327 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002328 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002329 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002330 return (0);
2331 }
2332 for ( ; res && (pos < (nch - 1)); pos += 2)
2333 res = (validate_doublestar(CHILD(tree, pos))
2334 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002335 }
2336 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002337}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002338
2339
Guido van Rossum47478871996-08-21 14:32:37 +00002340static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002341validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002342{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002343 int pos;
2344 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002345 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002346
Fred Drakecff283c2000-08-21 22:24:43 +00002347 if (res && nch < 1)
2348 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002349 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002350 switch (TYPE(CHILD(tree, 0))) {
2351 case LPAR:
2352 res = ((nch <= 3)
2353 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002354
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002355 if (res && (nch == 3)) {
2356 if (TYPE(CHILD(tree, 1))==yield_expr)
2357 res = validate_yield_expr(CHILD(tree, 1));
2358 else
2359 res = validate_testlist_gexp(CHILD(tree, 1));
2360 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002361 break;
2362 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002363 if (nch == 2)
2364 res = validate_ntype(CHILD(tree, 1), RSQB);
2365 else if (nch == 3)
2366 res = (validate_listmaker(CHILD(tree, 1))
2367 && validate_ntype(CHILD(tree, 2), RSQB));
2368 else {
2369 res = 0;
2370 err_string("illegal list display atom");
2371 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002372 break;
2373 case LBRACE:
2374 res = ((nch <= 3)
2375 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002376
Fred Drakeff9ea482000-04-19 13:54:15 +00002377 if (res && (nch == 3))
Neal Norwitz6309f2d2006-08-29 05:53:33 +00002378 res = validate_dictsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002379 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002380 case NAME:
2381 case NUMBER:
2382 res = (nch == 1);
2383 break;
2384 case STRING:
2385 for (pos = 1; res && (pos < nch); ++pos)
2386 res = validate_ntype(CHILD(tree, pos), STRING);
2387 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002388 case DOT:
2389 res = (nch == 3 &&
2390 validate_ntype(CHILD(tree, 1), DOT) &&
2391 validate_ntype(CHILD(tree, 2), DOT));
2392 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002393 default:
2394 res = 0;
2395 break;
2396 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002397 }
2398 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002399}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002400
2401
Fred Drake85bf3bb2000-08-23 15:35:26 +00002402/* listmaker:
2403 * test ( list_for | (',' test)* [','] )
2404 */
Fred Drakecff283c2000-08-21 22:24:43 +00002405static int
2406validate_listmaker(node *tree)
2407{
2408 int nch = NCH(tree);
2409 int ok = nch;
2410
2411 if (nch == 0)
2412 err_string("missing child nodes of listmaker");
2413 else
2414 ok = validate_test(CHILD(tree, 0));
2415
2416 /*
Raymond Hettinger354433a2004-05-19 08:20:33 +00002417 * list_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002418 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002419 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2420 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002421 else {
2422 /* (',' test)* [','] */
2423 int i = 1;
2424 while (ok && nch - i >= 2) {
2425 ok = (validate_comma(CHILD(tree, i))
2426 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002427 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002428 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002429 if (ok && i == nch-1)
2430 ok = validate_comma(CHILD(tree, i));
2431 else if (i != nch) {
2432 ok = 0;
2433 err_string("illegal trailing nodes for listmaker");
2434 }
Fred Drakecff283c2000-08-21 22:24:43 +00002435 }
2436 return ok;
2437}
2438
Raymond Hettinger354433a2004-05-19 08:20:33 +00002439/* testlist_gexp:
2440 * test ( gen_for | (',' test)* [','] )
2441 */
2442static int
2443validate_testlist_gexp(node *tree)
2444{
2445 int nch = NCH(tree);
2446 int ok = nch;
2447
2448 if (nch == 0)
2449 err_string("missing child nodes of testlist_gexp");
2450 else {
2451 ok = validate_test(CHILD(tree, 0));
2452 }
2453
2454 /*
2455 * gen_for | (',' test)* [',']
2456 */
2457 if (nch == 2 && TYPE(CHILD(tree, 1)) == gen_for)
2458 ok = validate_gen_for(CHILD(tree, 1));
2459 else {
2460 /* (',' test)* [','] */
2461 int i = 1;
2462 while (ok && nch - i >= 2) {
2463 ok = (validate_comma(CHILD(tree, i))
2464 && validate_test(CHILD(tree, i+1)));
2465 i += 2;
2466 }
2467 if (ok && i == nch-1)
2468 ok = validate_comma(CHILD(tree, i));
2469 else if (i != nch) {
2470 ok = 0;
2471 err_string("illegal trailing nodes for testlist_gexp");
2472 }
2473 }
2474 return ok;
2475}
Fred Drakecff283c2000-08-21 22:24:43 +00002476
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002477/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002478 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002479 */
2480static int
2481validate_decorator(node *tree)
2482{
2483 int ok;
2484 int nch = NCH(tree);
2485 ok = (validate_ntype(tree, decorator) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002486 (nch == 3 || nch == 5 || nch == 6) &&
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002487 validate_at(CHILD(tree, 0)) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002488 validate_dotted_name(CHILD(tree, 1)) &&
2489 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002490
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002491 if (ok && nch != 3) {
2492 ok = (validate_lparen(CHILD(tree, 2)) &&
2493 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002494
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002495 if (ok && nch == 6)
2496 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002497 }
2498
2499 return ok;
2500}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002501
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002502/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002503 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002504 */
2505static int
2506validate_decorators(node *tree)
2507{
2508 int i, nch, ok;
2509 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002510 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002511
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002512 for (i = 0; ok && i < nch; ++i)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002513 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002514
2515 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002516}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002517
Guido van Rossum3d602e31996-07-21 02:33:56 +00002518/* funcdef:
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002519 *
2520 * -6 -5 -4 -3 -2 -1
2521 * [decorators] 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002522 */
Guido van Rossum47478871996-08-21 14:32:37 +00002523static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002524validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002525{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002526 int nch = NCH(tree);
2527 int ok = (validate_ntype(tree, funcdef)
2528 && ((nch == 5) || (nch == 6))
2529 && validate_name(RCHILD(tree, -5), "def")
2530 && validate_ntype(RCHILD(tree, -4), NAME)
2531 && validate_colon(RCHILD(tree, -2))
2532 && validate_parameters(RCHILD(tree, -3))
2533 && validate_suite(RCHILD(tree, -1)));
2534
2535 if (ok && (nch == 6))
2536 ok = validate_decorators(CHILD(tree, 0));
2537
2538 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002539}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002540
2541
Guido van Rossum47478871996-08-21 14:32:37 +00002542static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002543validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002544{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002545 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002546 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002547 && ((nch == 3) || (nch == 4))
2548 && validate_name(CHILD(tree, 0), "lambda")
2549 && validate_colon(CHILD(tree, nch - 2))
2550 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002551
Guido van Rossum3d602e31996-07-21 02:33:56 +00002552 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002553 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002554 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002555 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002556
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002557 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002558}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002559
2560
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002561static int
2562validate_old_lambdef(node *tree)
2563{
2564 int nch = NCH(tree);
2565 int res = (validate_ntype(tree, old_lambdef)
2566 && ((nch == 3) || (nch == 4))
2567 && validate_name(CHILD(tree, 0), "lambda")
2568 && validate_colon(CHILD(tree, nch - 2))
2569 && validate_test(CHILD(tree, nch - 1)));
2570
2571 if (res && (nch == 4))
2572 res = validate_varargslist(CHILD(tree, 1));
2573 else if (!res && !PyErr_Occurred())
2574 (void) validate_numnodes(tree, 3, "old_lambdef");
2575
2576 return (res);
2577}
2578
2579
Guido van Rossum3d602e31996-07-21 02:33:56 +00002580/* arglist:
2581 *
Fred Drakecff283c2000-08-21 22:24:43 +00002582 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002583 */
Guido van Rossum47478871996-08-21 14:32:37 +00002584static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002585validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002586{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002587 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002588 int i = 0;
2589 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002590
2591 if (nch <= 0)
2592 /* raise the right error from having an invalid number of children */
2593 return validate_numnodes(tree, nch + 1, "arglist");
2594
Raymond Hettinger354433a2004-05-19 08:20:33 +00002595 if (nch > 1) {
2596 for (i=0; i<nch; i++) {
2597 if (TYPE(CHILD(tree, i)) == argument) {
2598 node *ch = CHILD(tree, i);
2599 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == gen_for) {
2600 err_string("need '(', ')' for generator expression");
2601 return 0;
2602 }
2603 }
2604 }
2605 }
2606
Fred Drakecff283c2000-08-21 22:24:43 +00002607 while (ok && nch-i >= 2) {
2608 /* skip leading (argument ',') */
2609 ok = (validate_argument(CHILD(tree, i))
2610 && validate_comma(CHILD(tree, i+1)));
2611 if (ok)
2612 i += 2;
2613 else
2614 PyErr_Clear();
2615 }
2616 ok = 1;
2617 if (nch-i > 0) {
2618 /*
2619 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002620 */
Fred Drakecff283c2000-08-21 22:24:43 +00002621 int sym = TYPE(CHILD(tree, i));
2622
2623 if (sym == argument) {
2624 ok = validate_argument(CHILD(tree, i));
2625 if (ok && i+1 != nch) {
2626 err_string("illegal arglist specification"
2627 " (extra stuff on end)");
2628 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002629 }
Fred Drakecff283c2000-08-21 22:24:43 +00002630 }
2631 else if (sym == STAR) {
2632 ok = validate_star(CHILD(tree, i));
2633 if (ok && (nch-i == 2))
2634 ok = validate_test(CHILD(tree, i+1));
2635 else if (ok && (nch-i == 5))
2636 ok = (validate_test(CHILD(tree, i+1))
2637 && validate_comma(CHILD(tree, i+2))
2638 && validate_doublestar(CHILD(tree, i+3))
2639 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002640 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002641 err_string("illegal use of '*' in arglist");
2642 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002643 }
2644 }
Fred Drakecff283c2000-08-21 22:24:43 +00002645 else if (sym == DOUBLESTAR) {
2646 if (nch-i == 2)
2647 ok = (validate_doublestar(CHILD(tree, i))
2648 && validate_test(CHILD(tree, i+1)));
2649 else {
2650 err_string("illegal use of '**' in arglist");
2651 ok = 0;
2652 }
2653 }
2654 else {
2655 err_string("illegal arglist specification");
2656 ok = 0;
2657 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002658 }
2659 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002660}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002661
2662
2663
2664/* argument:
2665 *
Raymond Hettinger354433a2004-05-19 08:20:33 +00002666 * [test '='] test [gen_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002667 */
Guido van Rossum47478871996-08-21 14:32:37 +00002668static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002669validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002670{
2671 int nch = NCH(tree);
2672 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002673 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002674 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002675
Raymond Hettinger354433a2004-05-19 08:20:33 +00002676 if (res && (nch == 2))
2677 res = validate_gen_for(CHILD(tree, 1));
2678 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002679 res = (validate_equal(CHILD(tree, 1))
2680 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002681
2682 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002683}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002684
2685
2686
2687/* trailer:
2688 *
Guido van Rossum47478871996-08-21 14:32:37 +00002689 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002690 */
Guido van Rossum47478871996-08-21 14:32:37 +00002691static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002692validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002693{
2694 int nch = NCH(tree);
2695 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002696
2697 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002698 switch (TYPE(CHILD(tree, 0))) {
2699 case LPAR:
2700 res = validate_rparen(CHILD(tree, nch - 1));
2701 if (res && (nch == 3))
2702 res = validate_arglist(CHILD(tree, 1));
2703 break;
2704 case LSQB:
2705 res = (validate_numnodes(tree, 3, "trailer")
2706 && validate_subscriptlist(CHILD(tree, 1))
2707 && validate_ntype(CHILD(tree, 2), RSQB));
2708 break;
2709 case DOT:
2710 res = (validate_numnodes(tree, 2, "trailer")
2711 && validate_ntype(CHILD(tree, 1), NAME));
2712 break;
2713 default:
2714 res = 0;
2715 break;
2716 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002717 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002718 else {
2719 (void) validate_numnodes(tree, 2, "trailer");
2720 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002721 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002722}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002723
2724
Guido van Rossum47478871996-08-21 14:32:37 +00002725/* subscriptlist:
2726 *
2727 * subscript (',' subscript)* [',']
2728 */
2729static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002730validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002731{
2732 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002733 validate_subscript, "subscriptlist"));
2734}
Guido van Rossum47478871996-08-21 14:32:37 +00002735
2736
Guido van Rossum3d602e31996-07-21 02:33:56 +00002737/* subscript:
2738 *
Guido van Rossum47478871996-08-21 14:32:37 +00002739 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002740 */
Guido van Rossum47478871996-08-21 14:32:37 +00002741static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002742validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002743{
Guido van Rossum47478871996-08-21 14:32:37 +00002744 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002745 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002746 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002747
Guido van Rossum47478871996-08-21 14:32:37 +00002748 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002749 if (!PyErr_Occurred())
2750 err_string("invalid number of arguments for subscript node");
2751 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002752 }
Guido van Rossum47478871996-08-21 14:32:37 +00002753 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002754 /* take care of ('.' '.' '.') possibility */
2755 return (validate_numnodes(tree, 3, "subscript")
2756 && validate_dot(CHILD(tree, 0))
2757 && validate_dot(CHILD(tree, 1))
2758 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002759 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002760 if (TYPE(CHILD(tree, 0)) == test)
2761 res = validate_test(CHILD(tree, 0));
2762 else
2763 res = validate_colon(CHILD(tree, 0));
2764 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002765 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002766 /* Must be [test] ':' [test] [sliceop],
2767 * but at least one of the optional components will
2768 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002769 */
2770 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002771 res = validate_test(CHILD(tree, 0));
2772 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002773 }
2774 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002775 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002776 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002777 int rem = nch - ++offset;
2778 if (rem) {
2779 if (TYPE(CHILD(tree, offset)) == test) {
2780 res = validate_test(CHILD(tree, offset));
2781 ++offset;
2782 --rem;
2783 }
2784 if (res && rem)
2785 res = validate_sliceop(CHILD(tree, offset));
2786 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002787 }
2788 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002789}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002790
2791
Guido van Rossum47478871996-08-21 14:32:37 +00002792static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002793validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002794{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002795 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002796 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002797 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002798 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002799 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002800 }
Guido van Rossum47478871996-08-21 14:32:37 +00002801 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002802 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002803 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002804 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002805
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002806 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002807}
Guido van Rossum47478871996-08-21 14:32:37 +00002808
2809
2810static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002811validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002812{
2813 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002814 validate_expr, "exprlist"));
2815}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002816
2817
Guido van Rossum47478871996-08-21 14:32:37 +00002818static int
Neal Norwitz6309f2d2006-08-29 05:53:33 +00002819validate_dictsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002820{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002821 int nch = NCH(tree);
Neal Norwitz6309f2d2006-08-29 05:53:33 +00002822 int res = (validate_ntype(tree, dictsetmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002823 && (nch >= 3)
2824 && validate_test(CHILD(tree, 0))
2825 && validate_colon(CHILD(tree, 1))
2826 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002827
Guido van Rossum3d602e31996-07-21 02:33:56 +00002828 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002829 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002830 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002831 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002832
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002833 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002834 int pos = 3;
2835 /* ( ',' test ':' test )* */
2836 while (res && (pos < nch)) {
2837 res = (validate_comma(CHILD(tree, pos))
2838 && validate_test(CHILD(tree, pos + 1))
2839 && validate_colon(CHILD(tree, pos + 2))
2840 && validate_test(CHILD(tree, pos + 3)));
2841 pos += 4;
2842 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002843 }
2844 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002845}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002846
2847
Guido van Rossum47478871996-08-21 14:32:37 +00002848static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002849validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002850{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002851 int pos;
2852 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002853 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002854 && (nch >= 2)
2855 && validate_testlist(CHILD(tree, 0))
2856 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002857
Guido van Rossum3d602e31996-07-21 02:33:56 +00002858 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002859 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002860
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002861 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002862}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002863
2864
Guido van Rossum47478871996-08-21 14:32:37 +00002865static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002866validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002867{
Fred Drakeff9ea482000-04-19 13:54:15 +00002868 int nch = 0; /* num. children on current node */
2869 int res = 1; /* result value */
2870 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002871
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002872 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002873 nch = NCH(tree);
2874 next = 0;
2875 switch (TYPE(tree)) {
2876 /*
2877 * Definition nodes.
2878 */
2879 case funcdef:
2880 res = validate_funcdef(tree);
2881 break;
2882 case classdef:
2883 res = validate_class(tree);
2884 break;
2885 /*
2886 * "Trivial" parse tree nodes.
2887 * (Why did I call these trivial?)
2888 */
2889 case stmt:
2890 res = validate_stmt(tree);
2891 break;
2892 case small_stmt:
2893 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002894 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Georg Brandl7cae87c2006-09-06 06:51:57 +00002895 * | import_stmt | global_stmt | assert_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002896 */
2897 res = validate_small_stmt(tree);
2898 break;
2899 case flow_stmt:
2900 res = (validate_numnodes(tree, 1, "flow_stmt")
2901 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2902 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002903 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002904 || (TYPE(CHILD(tree, 0)) == return_stmt)
2905 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2906 if (res)
2907 next = CHILD(tree, 0);
2908 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002909 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002910 break;
Fred Drake02126f22001-07-17 02:59:15 +00002911 case yield_stmt:
2912 res = validate_yield_stmt(tree);
2913 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002914 /*
2915 * Compound statements.
2916 */
2917 case simple_stmt:
2918 res = validate_simple_stmt(tree);
2919 break;
2920 case compound_stmt:
2921 res = validate_compound_stmt(tree);
2922 break;
2923 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002924 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002925 */
2926 case expr_stmt:
2927 res = validate_expr_stmt(tree);
2928 break;
2929 case print_stmt:
2930 res = validate_print_stmt(tree);
2931 break;
2932 case del_stmt:
2933 res = validate_del_stmt(tree);
2934 break;
2935 case pass_stmt:
2936 res = (validate_numnodes(tree, 1, "pass")
2937 && validate_name(CHILD(tree, 0), "pass"));
2938 break;
2939 case break_stmt:
2940 res = (validate_numnodes(tree, 1, "break")
2941 && validate_name(CHILD(tree, 0), "break"));
2942 break;
2943 case continue_stmt:
2944 res = (validate_numnodes(tree, 1, "continue")
2945 && validate_name(CHILD(tree, 0), "continue"));
2946 break;
2947 case return_stmt:
2948 res = validate_return_stmt(tree);
2949 break;
2950 case raise_stmt:
2951 res = validate_raise_stmt(tree);
2952 break;
2953 case import_stmt:
2954 res = validate_import_stmt(tree);
2955 break;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00002956 case import_name:
2957 res = validate_import_name(tree);
2958 break;
2959 case import_from:
2960 res = validate_import_from(tree);
2961 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002962 case global_stmt:
2963 res = validate_global_stmt(tree);
2964 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002965 case assert_stmt:
2966 res = validate_assert_stmt(tree);
2967 break;
2968 case if_stmt:
2969 res = validate_if(tree);
2970 break;
2971 case while_stmt:
2972 res = validate_while(tree);
2973 break;
2974 case for_stmt:
2975 res = validate_for(tree);
2976 break;
2977 case try_stmt:
2978 res = validate_try(tree);
2979 break;
2980 case suite:
2981 res = validate_suite(tree);
2982 break;
2983 /*
2984 * Expression nodes.
2985 */
2986 case testlist:
2987 res = validate_testlist(tree);
2988 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002989 case yield_expr:
2990 res = validate_yield_expr(tree);
2991 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002992 case testlist1:
2993 res = validate_testlist1(tree);
2994 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002995 case test:
2996 res = validate_test(tree);
2997 break;
2998 case and_test:
2999 res = validate_and_test(tree);
3000 break;
3001 case not_test:
3002 res = validate_not_test(tree);
3003 break;
3004 case comparison:
3005 res = validate_comparison(tree);
3006 break;
3007 case exprlist:
3008 res = validate_exprlist(tree);
3009 break;
3010 case comp_op:
3011 res = validate_comp_op(tree);
3012 break;
3013 case expr:
3014 res = validate_expr(tree);
3015 break;
3016 case xor_expr:
3017 res = validate_xor_expr(tree);
3018 break;
3019 case and_expr:
3020 res = validate_and_expr(tree);
3021 break;
3022 case shift_expr:
3023 res = validate_shift_expr(tree);
3024 break;
3025 case arith_expr:
3026 res = validate_arith_expr(tree);
3027 break;
3028 case term:
3029 res = validate_term(tree);
3030 break;
3031 case factor:
3032 res = validate_factor(tree);
3033 break;
3034 case power:
3035 res = validate_power(tree);
3036 break;
3037 case atom:
3038 res = validate_atom(tree);
3039 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003040
Fred Drakeff9ea482000-04-19 13:54:15 +00003041 default:
3042 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00003043 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003044 res = 0;
3045 break;
3046 }
3047 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003048 }
3049 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003050}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003051
3052
Guido van Rossum47478871996-08-21 14:32:37 +00003053static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003054validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003055{
3056 int res = validate_eval_input(tree);
3057
3058 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003059 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003060
3061 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003062}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003063
3064
Guido van Rossum3d602e31996-07-21 02:33:56 +00003065/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003066 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003067 */
Guido van Rossum47478871996-08-21 14:32:37 +00003068static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003069validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003070{
Fred Drakec2683dd2001-07-17 19:32:05 +00003071 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003072 int nch = NCH(tree) - 1;
3073 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003074 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003075
Fred Drakec2683dd2001-07-17 19:32:05 +00003076 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003077 if (TYPE(CHILD(tree, j)) == stmt)
3078 res = validate_stmt(CHILD(tree, j));
3079 else
3080 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003081 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003082 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003083 * user. Hopefully, this won't be needed. If a user reports getting
3084 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003085 */
3086 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003087 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003088
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003089 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003090}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003091
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003092static int
3093validate_encoding_decl(node *tree)
3094{
3095 int nch = NCH(tree);
3096 int res = ((nch == 1)
3097 && validate_file_input(CHILD(tree, 0)));
3098
3099 if (!res && !PyErr_Occurred())
3100 err_string("Error Parsing encoding_decl");
3101
3102 return res;
3103}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003104
Fred Drake43f8f9b1998-04-13 16:25:46 +00003105static PyObject*
3106pickle_constructor = NULL;
3107
3108
3109static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003110parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003111{
Fred Drake268397f1998-04-29 14:16:32 +00003112 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003113 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003114 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003115 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003116
Fred Drakec2683dd2001-07-17 19:32:05 +00003117 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003118 PyObject *newargs;
3119 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003120
Fred Drake2a6875e1999-09-20 22:32:18 +00003121 if ((empty_dict = PyDict_New()) == NULL)
3122 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003123 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003124 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003125 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003126 if (tuple != NULL) {
3127 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3128 Py_DECREF(tuple);
3129 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003130 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003131 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003132 }
3133 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003134 Py_XDECREF(empty_dict);
3135
Fred Drake43f8f9b1998-04-13 16:25:46 +00003136 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003137}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003138
3139
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003140/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003141 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003142 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003143 * We'd really have to write a wrapper around it all anyway to allow
3144 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003145 */
3146static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003147 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003148 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003149 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003150 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003151 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003152 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003153 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003154 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003155 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003156 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003157 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003158 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003159 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003160 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003161 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003162 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003163 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003164 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003165 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003166 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003167 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003168 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003169 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003170 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003171 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003172 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003173 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003174 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003175
Fred Drake43f8f9b1998-04-13 16:25:46 +00003176 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003177 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003178 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003179
Fred Drake268397f1998-04-29 14:16:32 +00003180 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003181 };
3182
3183
Mark Hammond62b1ab12002-07-23 06:31:15 +00003184PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003185
Mark Hammond62b1ab12002-07-23 06:31:15 +00003186PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00003187initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003188{
Fred Drake13130bc2001-08-15 16:44:56 +00003189 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003190
3191 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00003192 module = Py_InitModule("parser", parser_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003193 if (module == NULL)
3194 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003195
Fred Drake7a15ba51999-09-09 14:21:52 +00003196 if (parser_error == 0)
3197 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003198
Tim Peters6a627252003-07-21 14:25:23 +00003199 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00003200 /* caller will check PyErr_Occurred() */
3201 return;
Tim Peters6a627252003-07-21 14:25:23 +00003202 /* CAUTION: The code next used to skip bumping the refcount on
3203 * parser_error. That's a disaster if initparser() gets called more
3204 * than once. By incref'ing, we ensure that each module dict that
3205 * gets created owns its reference to the shared parser_error object,
3206 * and the file static parser_error vrbl owns a reference too.
3207 */
3208 Py_INCREF(parser_error);
3209 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3210 return;
3211
Fred Drakec2683dd2001-07-17 19:32:05 +00003212 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003213 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00003214 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003215 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003216
Fred Drake13130bc2001-08-15 16:44:56 +00003217 PyModule_AddStringConstant(module, "__copyright__",
3218 parser_copyright_string);
3219 PyModule_AddStringConstant(module, "__doc__",
3220 parser_doc_string);
3221 PyModule_AddStringConstant(module, "__version__",
3222 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003223
Fred Drake78bdb9b2001-07-19 20:17:15 +00003224 /* Register to support pickling.
3225 * If this fails, the import of this module will fail because an
3226 * exception will be raised here; should we clear the exception?
3227 */
Fred Drake13130bc2001-08-15 16:44:56 +00003228 copyreg = PyImport_ImportModule("copy_reg");
3229 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003230 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003231
Fred Drake13130bc2001-08-15 16:44:56 +00003232 func = PyObject_GetAttrString(copyreg, "pickle");
3233 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3234 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003235 Py_XINCREF(pickle_constructor);
3236 if ((func != NULL) && (pickle_constructor != NULL)
3237 && (pickler != NULL)) {
3238 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003239
Thomas Wouters477c8d52006-05-27 19:21:47 +00003240 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3241 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003242 Py_XDECREF(res);
3243 }
3244 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003245 Py_XDECREF(pickle_constructor);
3246 Py_XDECREF(pickler);
3247 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003248 }
Fred Drakeff9ea482000-04-19 13:54:15 +00003249}