blob: ab41aaf4a47262bfdf34cd52a763a7bb6e6b6aca [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000026 */
27
Fred Drakeff9ea482000-04-19 13:54:15 +000028#include "Python.h" /* general Python API */
29#include "graminit.h" /* symbols defined in the grammar */
30#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000031#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000032#include "token.h" /* token definitions */
33 /* ISTERMINAL() / ISNONTERMINAL() */
34#include "compile.h" /* PyNode_Compile() */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000035
Fred Drake268397f1998-04-29 14:16:32 +000036#ifdef lint
37#include <note.h>
38#else
39#define NOTE(x)
40#endif
41
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000042/* String constants used to initialize module attributes.
43 *
44 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045static char parser_copyright_string[] =
46"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000047University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
48Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
49Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000050
51
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000052PyDoc_STRVAR(parser_doc_string,
53"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000054
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000055static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000056
57
Martin v. Löwis18e16552006-02-15 17:27:45 +000058typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000059typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000060 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000061 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000062
Thomas Wouters7e474022000-07-16 12:04:32 +000063/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000064 * original copyright statement is included below, and continues to apply
65 * in full to the function immediately following. All other material is
66 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
67 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000068 * new naming conventions. Added arguments to provide support for creating
69 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000070 */
71
Guido van Rossum52f2c051993-11-10 12:53:24 +000072
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000073static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000074node2tuple(node *n, /* node to convert */
75 SeqMaker mkseq, /* create sequence */
76 SeqInserter addelem, /* func. to add elem. in seq. */
Thomas Wouters89f507f2006-12-13 04:49:30 +000077 int lineno, /* include line numbers? */
78 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000079{
Guido van Rossum3d602e31996-07-21 02:33:56 +000080 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000081 Py_INCREF(Py_None);
82 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000083 }
84 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000085 int i;
86 PyObject *v;
87 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000088
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000089 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000090 if (v == NULL)
91 return (v);
Christian Heimes217cfd12007-12-02 14:31:20 +000092 w = PyLong_FromLong(TYPE(n));
Fred Drakeff9ea482000-04-19 13:54:15 +000093 if (w == NULL) {
94 Py_DECREF(v);
95 return ((PyObject*) NULL);
96 }
97 (void) addelem(v, 0, w);
98 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +000099 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000100 if (w == NULL) {
101 Py_DECREF(v);
102 return ((PyObject*) NULL);
103 }
104 (void) addelem(v, i+1, w);
105 }
Tim Peters6a627252003-07-21 14:25:23 +0000106
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000107 if (TYPE(n) == encoding_decl)
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000108 (void) addelem(v, i+1, PyUnicode_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000109 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000110 }
111 else if (ISTERMINAL(TYPE(n))) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000112 PyObject *result = mkseq(2 + lineno + col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000113 if (result != NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000114 (void) addelem(result, 0, PyLong_FromLong(TYPE(n)));
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000115 (void) addelem(result, 1, PyUnicode_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000116 if (lineno == 1)
Christian Heimes217cfd12007-12-02 14:31:20 +0000117 (void) addelem(result, 2, PyLong_FromLong(n->n_lineno));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000118 if (col_offset == 1)
Christian Heimes217cfd12007-12-02 14:31:20 +0000119 (void) addelem(result, 3, PyLong_FromLong(n->n_col_offset));
Fred Drakeff9ea482000-04-19 13:54:15 +0000120 }
121 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000122 }
123 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000124 PyErr_SetString(PyExc_SystemError,
125 "unrecognized parse tree node type");
126 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000127 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000128}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000129/*
130 * End of material copyrighted by Stichting Mathematisch Centrum.
131 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000132
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000133
134
135/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000136 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000137 * field of the object type to identify which any given object represents.
138 * These should probably go in an external header to allow other extensions
139 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000140 */
141
Fred Drakec2683dd2001-07-17 19:32:05 +0000142#define PyST_EXPR 1
143#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000144
145
146/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000147 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000148 * naming style, but the code uses the new naming convention.
149 */
150
151static PyObject*
152parser_error = 0;
153
154
Fred Drakec2683dd2001-07-17 19:32:05 +0000155typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000156 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000157 node* st_node; /* the node* returned by the parser */
158 int st_type; /* EXPR or SUITE ? */
159} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000160
161
Jeremy Hylton938ace62002-07-17 16:30:39 +0000162static void parser_free(PyST_Object *st);
163static int parser_compare(PyST_Object *left, PyST_Object *right);
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000164static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
165static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
166static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
167static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
168static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
Fred Drake503d8d61998-04-13 18:45:18 +0000169
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000170#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
171
172static PyMethodDef parser_methods[] = {
173 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
174 PyDoc_STR("Compile this ST object into a code object.")},
175 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
176 PyDoc_STR("Determines if this ST object was created from an expression.")},
177 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
178 PyDoc_STR("Determines if this ST object was created from a suite.")},
179 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
180 PyDoc_STR("Creates a list-tree representation of this ST.")},
181 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
182 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
183
184 {NULL, NULL, 0, NULL}
185};
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000186
Fred Drake268397f1998-04-29 14:16:32 +0000187static
Fred Drakec2683dd2001-07-17 19:32:05 +0000188PyTypeObject PyST_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000189 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000190 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000191 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000192 0, /* tp_itemsize */
193 (destructor)parser_free, /* tp_dealloc */
194 0, /* tp_print */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000195 0, /* tp_getattr */
Fred Drakeff9ea482000-04-19 13:54:15 +0000196 0, /* tp_setattr */
197 (cmpfunc)parser_compare, /* tp_compare */
198 0, /* tp_repr */
199 0, /* tp_as_number */
200 0, /* tp_as_sequence */
201 0, /* tp_as_mapping */
202 0, /* tp_hash */
203 0, /* tp_call */
204 0, /* tp_str */
205 0, /* tp_getattro */
206 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000207
208 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000209 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000210
Fred Drakeff9ea482000-04-19 13:54:15 +0000211 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000212
213 /* __doc__ */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000214 "Intermediate representation of a Python parse tree.",
215 0, /* tp_traverse */
216 0, /* tp_clear */
217 0, /* tp_richcompare */
218 0, /* tp_weaklistoffset */
219 0, /* tp_iter */
220 0, /* tp_iternext */
221 parser_methods, /* tp_methods */
Fred Drakec2683dd2001-07-17 19:32:05 +0000222}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000223
224
225static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000226parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000227{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000228 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000229
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000230 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000231 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000232
233 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000234 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000235
236 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000237 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000238
239 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000240 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000241
242 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000243 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000244
245 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000246 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247
Fred Drakeff9ea482000-04-19 13:54:15 +0000248 if (v != 0)
249 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000250 }
251 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000252}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000253
254
Fred Drakec2683dd2001-07-17 19:32:05 +0000255/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000256 *
257 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
258 * This really just wraps a call to parser_compare_nodes() with some easy
259 * checks and protection code.
260 *
261 */
262static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000263parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000264{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000266 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000267
268 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000269 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000270
Fred Drakec2683dd2001-07-17 19:32:05 +0000271 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000272}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000273
274
Fred Drakec2683dd2001-07-17 19:32:05 +0000275/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000276 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000277 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000278 * 'wrapper' object that holds a node* and allows it to be passed around in
279 * Python code.
280 *
281 */
282static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000283parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000284{
Fred Drakec2683dd2001-07-17 19:32:05 +0000285 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000286
287 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000288 o->st_node = st;
289 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000290 }
Fred Drake268397f1998-04-29 14:16:32 +0000291 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000292 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000293 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000294 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000295}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000296
297
Fred Drakec2683dd2001-07-17 19:32:05 +0000298/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000299 *
300 * This is called by a del statement that reduces the reference count to 0.
301 *
302 */
303static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000304parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000305{
Fred Drakec2683dd2001-07-17 19:32:05 +0000306 PyNode_Free(st->st_node);
307 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000308}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000309
310
Fred Drakec2683dd2001-07-17 19:32:05 +0000311/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000312 *
313 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000314 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000315 *
316 */
317static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000318parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000319{
Guido van Rossum47478871996-08-21 14:32:37 +0000320 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000321 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000322 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000323 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000324
Thomas Wouters89f507f2006-12-13 04:49:30 +0000325 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000326
Martin v. Löwis1a214512008-06-11 05:26:20 +0000327 if (self == NULL || PyModule_Check(self)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000328 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
329 &PyST_Type, &self, &line_option,
330 &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000331 }
Fred Drake503d8d61998-04-13 18:45:18 +0000332 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000333 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
334 &line_option, &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000335 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000336 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000337 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000338 if (line_option != NULL) {
339 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
340 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000341 if (col_option != NULL) {
342 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
343 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000344 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000345 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000346 * since it's known to work already.
347 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000348 res = node2tuple(((PyST_Object*)self)->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000349 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000350 }
351 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000352}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000353
354
Fred Drakec2683dd2001-07-17 19:32:05 +0000355/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000356 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000357 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000358 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000359 *
360 */
361static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000362parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000363{
Guido van Rossum47478871996-08-21 14:32:37 +0000364 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000365 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000366 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000367 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000368
Thomas Wouters89f507f2006-12-13 04:49:30 +0000369 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000370
Martin v. Löwis1a214512008-06-11 05:26:20 +0000371 if (self == NULL || PyModule_Check(self))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000372 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
373 &PyST_Type, &self, &line_option,
374 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000375 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000376 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
377 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000378 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000379 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000380 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000381 if (line_option != 0) {
382 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
383 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000384 if (col_option != NULL) {
385 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
386 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000387 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000388 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000389 * since it's known to work already.
390 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000391 res = node2tuple(self->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000392 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000393 }
394 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000395}
Guido van Rossum47478871996-08-21 14:32:37 +0000396
397
Fred Drakec2683dd2001-07-17 19:32:05 +0000398/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000399 *
400 * This function creates code objects from the parse tree represented by
401 * the passed-in data object. An optional file name is passed in as well.
402 *
403 */
404static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000405parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000406{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000407 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000408 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000409 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000410
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000411 static char *keywords[] = {"ast", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000412
Martin v. Löwis1a214512008-06-11 05:26:20 +0000413 if (self == NULL || PyModule_Check(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000414 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
415 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000416 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000417 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000418 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000419
420 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000421 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000422
423 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000424}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000425
426
427/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
428 * PyObject* parser_issuite(PyObject* self, PyObject* args)
429 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000430 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000431 * a statement suite, respectively. The return is a Python truth value.
432 *
433 */
434static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000435parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000436{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000437 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000438 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000439
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000440 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000441
Martin v. Löwis1a214512008-06-11 05:26:20 +0000442 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000443 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000444 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000445 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000446 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000447
448 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000449 /* Check to see if the ST represents an expression or not. */
450 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000451 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000452 }
453 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000454}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000455
456
457static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000458parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000459{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000460 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000461 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000462
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000463 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000464
Martin v. Löwis1a214512008-06-11 05:26:20 +0000465 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000466 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000467 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000468 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000469 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000470
471 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000472 /* Check to see if the ST represents an expression or not. */
473 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000474 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000475 }
476 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000477}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000478
479
Guido van Rossum3d602e31996-07-21 02:33:56 +0000480/* err_string(char* message)
481 *
482 * Sets the error string for an exception of type ParserError.
483 *
484 */
485static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000486err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000487{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000488 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000489}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000490
491
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000492/* PyObject* parser_do_parse(PyObject* args, int type)
493 *
494 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000495 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000496 *
497 */
498static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000499parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000500{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000501 char* string = 0;
502 PyObject* res = 0;
503
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000504 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000505
506 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000507 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000508 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000509 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000510
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000511 if (n)
512 res = parser_newstobject(n, type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000513 }
514 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000515}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000516
517
518/* PyObject* parser_expr(PyObject* self, PyObject* args)
519 * PyObject* parser_suite(PyObject* self, PyObject* args)
520 *
521 * External interfaces to the parser itself. Which is called determines if
522 * the parser attempts to recognize an expression ('eval' form) or statement
523 * suite ('exec' form). The real work is done by parser_do_parse() above.
524 *
525 */
526static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000527parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000528{
Fred Drake268397f1998-04-29 14:16:32 +0000529 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000530 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000531}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000532
533
534static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000535parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000536{
Fred Drake268397f1998-04-29 14:16:32 +0000537 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000538 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000539}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000540
541
542
Fred Drakec2683dd2001-07-17 19:32:05 +0000543/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000544 * object requires that the input tuple be valid without having to rely on
545 * catching an exception from the compiler. This is done to allow the
546 * compiler itself to remain fast, since most of its input will come from
547 * the parser directly, and therefore be known to be syntactically correct.
548 * This validation is done to ensure that we don't core dump the compile
549 * phase, returning an exception instead.
550 *
551 * Two aspects can be broken out in this code: creating a node tree from
552 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000553 * advantageous to expand the number of ST types to include funcdefs and
554 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000555 * here. They are not necessary, and not quite as useful in a raw form.
556 * For now, let's get expressions and suites working reliably.
557 */
558
559
Jeremy Hylton938ace62002-07-17 16:30:39 +0000560static node* build_node_tree(PyObject *tuple);
561static int validate_expr_tree(node *tree);
562static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000563static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000564
Fred Drakec2683dd2001-07-17 19:32:05 +0000565/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000566 *
567 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000568 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000569 * tuple can be validated. It does this by checking the first code of the
570 * tuple, and, if acceptable, builds the internal representation. If this
571 * step succeeds, the internal representation is validated as fully as
572 * possible with the various validate_*() routines defined below.
573 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000574 * This function must be changed if support is to be added for PyST_FRAGMENT
575 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000576 *
577 */
578static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000579parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000580{
Fred Drake268397f1998-04-29 14:16:32 +0000581 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000582 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000583 PyObject *tuple;
584 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000585
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000586 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000587
Fred Drakec2683dd2001-07-17 19:32:05 +0000588 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000589 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000590 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000591 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000592 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000593 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000594 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000595 }
596 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000597 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000598 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000599 tree = build_node_tree(tuple);
600 if (tree != 0) {
601 int start_sym = TYPE(tree);
602 if (start_sym == eval_input) {
603 /* Might be an eval form. */
604 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000605 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000606 else
607 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000608 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000609 else if (start_sym == file_input) {
610 /* This looks like an exec form so far. */
611 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000612 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000613 else
614 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000615 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000616 else if (start_sym == encoding_decl) {
617 /* This looks like an encoding_decl so far. */
618 if (validate_encoding_decl(tree))
619 st = parser_newstobject(tree, PyST_SUITE);
620 else
621 PyNode_Free(tree);
622 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000623 else {
624 /* This is a fragment, at best. */
625 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000626 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000627 }
Guido van Rossum47478871996-08-21 14:32:37 +0000628 }
Guido van Rossum47478871996-08-21 14:32:37 +0000629 /* Make sure we throw an exception on all errors. We should never
630 * get this, but we'd do well to be sure something is done.
631 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000632 if (st == NULL && !PyErr_Occurred())
633 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000634
Fred Drakec2683dd2001-07-17 19:32:05 +0000635 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000636}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000637
638
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000639/* node* build_node_children()
640 *
641 * Iterate across the children of the current non-terminal node and build
642 * their structures. If successful, return the root of this portion of
643 * the tree, otherwise, 0. Any required exception will be specified already,
644 * and no memory will have been deallocated.
645 *
646 */
647static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000648build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000649{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000650 Py_ssize_t len = PyObject_Size(tuple);
651 Py_ssize_t i;
652 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000653
654 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000655 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000656 PyObject* elem = PySequence_GetItem(tuple, i);
657 int ok = elem != NULL;
658 long type = 0;
659 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000660
Fred Drakeff9ea482000-04-19 13:54:15 +0000661 if (ok)
662 ok = PySequence_Check(elem);
663 if (ok) {
664 PyObject *temp = PySequence_GetItem(elem, 0);
665 if (temp == NULL)
666 ok = 0;
667 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000668 ok = PyLong_Check(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000669 if (ok)
Christian Heimes217cfd12007-12-02 14:31:20 +0000670 type = PyLong_AS_LONG(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000671 Py_DECREF(temp);
672 }
673 }
674 if (!ok) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675 PyObject *err = Py_BuildValue("os", elem,
676 "Illegal node construct.");
677 PyErr_SetObject(parser_error, err);
678 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000679 Py_XDECREF(elem);
680 return (0);
681 }
682 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000683 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000684 PyObject *temp;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000685 const char *temp_str;
Guido van Rossum47478871996-08-21 14:32:37 +0000686
Fred Drake0ac9b072000-09-12 21:58:06 +0000687 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000688 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000689 return 0;
690 }
691 temp = PySequence_GetItem(elem, 1);
692 if (temp == NULL)
693 return 0;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000694 if (!PyUnicode_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000695 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000696 "second item in terminal node must be a string,"
697 " found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000698 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000699 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000700 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000701 return 0;
702 }
703 if (len == 3) {
704 PyObject *o = PySequence_GetItem(elem, 2);
705 if (o != NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000706 if (PyLong_Check(o))
707 *line_num = PyLong_AS_LONG(o);
Fred Drake0ac9b072000-09-12 21:58:06 +0000708 else {
709 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000710 "third item in terminal node must be an"
711 " integer, found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000712 Py_TYPE(temp)->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000713 Py_DECREF(o);
714 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000715 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000716 return 0;
717 }
718 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000719 }
720 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000721 temp_str = PyUnicode_AsStringAndSize(temp, &len);
722 strn = (char *)PyObject_MALLOC(len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000723 if (strn != NULL)
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000724 (void) memcpy(strn, temp_str, len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000725 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000726 }
727 else if (!ISNONTERMINAL(type)) {
728 /*
729 * It has to be one or the other; this is an error.
730 * Throw an exception.
731 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000732 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
733 PyErr_SetObject(parser_error, err);
734 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000735 Py_XDECREF(elem);
736 return (0);
737 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000738 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000739 if (err == E_NOMEM) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000740 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000741 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000742 return (node *) PyErr_NoMemory();
743 }
744 if (err == E_OVERFLOW) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000745 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000746 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000747 PyErr_SetString(PyExc_ValueError,
748 "unsupported number of child nodes");
749 return NULL;
750 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000751
Fred Drakeff9ea482000-04-19 13:54:15 +0000752 if (ISNONTERMINAL(type)) {
753 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000754
Fred Drakeff9ea482000-04-19 13:54:15 +0000755 if (new_child != build_node_children(elem, new_child, line_num)) {
756 Py_XDECREF(elem);
757 return (0);
758 }
759 }
760 else if (type == NEWLINE) { /* It's true: we increment the */
761 ++(*line_num); /* line number *after* the newline! */
762 }
763 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000764 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000765 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000766}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000767
768
769static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000770build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000771{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000772 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000773 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000774 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000775
Guido van Rossum47478871996-08-21 14:32:37 +0000776 if (temp != NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000777 num = PyLong_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000778 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000779 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000780 /*
781 * The tuple is simple, but it doesn't start with a start symbol.
782 * Throw an exception now and be done with it.
783 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000784 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000785 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000786 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000787 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000788 }
789 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000790 /*
791 * Not efficient, but that can be handled later.
792 */
793 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000794 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000795
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000796 if (num == encoding_decl) {
797 encoding = PySequence_GetItem(tuple, 2);
798 /* tuple isn't borrowed anymore here, need to DECREF */
799 tuple = PySequence_GetSlice(tuple, 0, 2);
800 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000801 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000802 if (res != NULL) {
803 if (res != build_node_children(tuple, res, &line_num)) {
804 PyNode_Free(res);
805 res = NULL;
806 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000807 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000808 Py_ssize_t len;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000809 const char *temp;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000810 temp = PyUnicode_AsStringAndSize(encoding, &len);
811 res->n_str = (char *)PyObject_MALLOC(len + 1);
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000812 if (res->n_str != NULL && temp != NULL)
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000813 (void) memcpy(res->n_str, temp, len + 1);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000814 Py_DECREF(encoding);
815 Py_DECREF(tuple);
816 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000817 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000818 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000819 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000820 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000821 * NONTERMINAL, we can't use it. Not sure the implementation
822 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000823 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000824 PyObject *err = Py_BuildValue("os", tuple,
825 "Illegal component tuple.");
826 PyErr_SetObject(parser_error, err);
827 Py_XDECREF(err);
828 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000829
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000830 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000831}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000832
833
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000834/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000835 * Validation routines used within the validation section:
836 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000837static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000838
Fred Drakeff9ea482000-04-19 13:54:15 +0000839#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
840#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
841#define validate_colon(ch) validate_terminal(ch, COLON, ":")
842#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
843#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
844#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
845#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
846#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
847#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
848#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
849#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
850#define validate_star(ch) validate_terminal(ch, STAR, "*")
851#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
852#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
853#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000854#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000855#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000856
Fred Drake0ac9b072000-09-12 21:58:06 +0000857#define VALIDATER(n) static int validate_##n(node *tree)
858
Fred Drakeff9ea482000-04-19 13:54:15 +0000859VALIDATER(node); VALIDATER(small_stmt);
860VALIDATER(class); VALIDATER(node);
861VALIDATER(parameters); VALIDATER(suite);
862VALIDATER(testlist); VALIDATER(varargslist);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000863VALIDATER(vfpdef);
Fred Drakeff9ea482000-04-19 13:54:15 +0000864VALIDATER(stmt); VALIDATER(simple_stmt);
865VALIDATER(expr_stmt); VALIDATER(power);
Guido van Rossum452bf512007-02-09 05:32:43 +0000866VALIDATER(del_stmt);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000867VALIDATER(return_stmt); VALIDATER(raise_stmt);
868VALIDATER(import_stmt); VALIDATER(import_stmt);
869VALIDATER(import_name); VALIDATER(yield_stmt);
870VALIDATER(global_stmt); VALIDATER(assert_stmt);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000871VALIDATER(compound_stmt);
Fred Drakeff9ea482000-04-19 13:54:15 +0000872VALIDATER(while); VALIDATER(for);
873VALIDATER(try); VALIDATER(except_clause);
874VALIDATER(test); VALIDATER(and_test);
875VALIDATER(not_test); VALIDATER(comparison);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000876VALIDATER(comp_op);
Guido van Rossum0368b722007-05-11 16:50:42 +0000877VALIDATER(star_expr); VALIDATER(expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000878VALIDATER(xor_expr); VALIDATER(and_expr);
879VALIDATER(shift_expr); VALIDATER(arith_expr);
880VALIDATER(term); VALIDATER(factor);
881VALIDATER(atom); VALIDATER(lambdef);
882VALIDATER(trailer); VALIDATER(subscript);
883VALIDATER(subscriptlist); VALIDATER(sliceop);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000884VALIDATER(exprlist); VALIDATER(dictorsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000885VALIDATER(arglist); VALIDATER(argument);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000886VALIDATER(testlist1); VALIDATER(comp_for);
887VALIDATER(comp_iter); VALIDATER(comp_if);
888VALIDATER(testlist_comp); VALIDATER(yield_expr);
889VALIDATER(yield_or_testlist); VALIDATER(or_test);
890VALIDATER(test_nocond); VALIDATER(lambdef_nocond);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000891
Fred Drake0ac9b072000-09-12 21:58:06 +0000892#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000893
Fred Drakeff9ea482000-04-19 13:54:15 +0000894#define is_even(n) (((n) & 1) == 0)
895#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000896
897
898static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000899validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000900{
Fred Drake0ac9b072000-09-12 21:58:06 +0000901 if (TYPE(n) != t) {
902 PyErr_Format(parser_error, "Expected node type %d, got %d.",
903 t, TYPE(n));
904 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000905 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000906 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000907}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000908
909
Fred Drakee7ab64e2000-04-25 04:14:46 +0000910/* Verifies that the number of child nodes is exactly 'num', raising
911 * an exception if it isn't. The exception message does not indicate
912 * the exact number of nodes, allowing this to be used to raise the
913 * "right" exception when the wrong number of nodes is present in a
914 * specific variant of a statement's syntax. This is commonly used
915 * in that fashion.
916 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000917static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000918validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000919{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000920 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000921 PyErr_Format(parser_error,
922 "Illegal number of children for %s node.", name);
923 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000924 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000925 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000926}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000927
928
929static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000930validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000931{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000932 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000933 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000934
935 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000936 PyErr_Format(parser_error,
937 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000938 }
939 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000940}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000941
942
Guido van Rossum47478871996-08-21 14:32:37 +0000943/* X (',' X) [',']
944 */
945static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000946validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000947 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000948{
949 int nch = NCH(tree);
950 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000951 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000952
953 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000954 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000955 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000956 if (is_even(nch))
957 res = validate_comma(CHILD(tree, --nch));
958 if (res && nch > 1) {
959 int pos = 1;
960 for ( ; res && pos < nch; pos += 2)
961 res = (validate_comma(CHILD(tree, pos))
962 && vfunc(CHILD(tree, pos + 1)));
963 }
Guido van Rossum47478871996-08-21 14:32:37 +0000964 }
965 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000966}
Guido van Rossum47478871996-08-21 14:32:37 +0000967
968
Fred Drakecff283c2000-08-21 22:24:43 +0000969/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000970 *
971 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000972 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000973 */
Guido van Rossum47478871996-08-21 14:32:37 +0000974static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000975validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000976{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000977 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +0000978 int res = (validate_ntype(tree, classdef) &&
979 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000980
Guido van Rossum3d602e31996-07-21 02:33:56 +0000981 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000982 res = (validate_name(CHILD(tree, 0), "class")
983 && validate_ntype(CHILD(tree, 1), NAME)
984 && validate_colon(CHILD(tree, nch - 2))
985 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000986 }
Brett Cannonf4189912005-04-09 02:30:16 +0000987 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000988 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +0000989 }
Guido van Rossumfc158e22007-11-15 19:17:28 +0000990
Brett Cannonf4189912005-04-09 02:30:16 +0000991 if (res) {
992 if (nch == 7) {
993 res = ((validate_lparen(CHILD(tree, 2)) &&
Guido van Rossumfc158e22007-11-15 19:17:28 +0000994 validate_arglist(CHILD(tree, 3)) &&
Brett Cannonf4189912005-04-09 02:30:16 +0000995 validate_rparen(CHILD(tree, 4))));
996 }
997 else if (nch == 6) {
998 res = (validate_lparen(CHILD(tree,2)) &&
999 validate_rparen(CHILD(tree,3)));
1000 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001001 }
1002 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001003}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001004
1005
Guido van Rossum3d602e31996-07-21 02:33:56 +00001006/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001007 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001008 */
Guido van Rossum47478871996-08-21 14:32:37 +00001009static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001010validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001011{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001012 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001013 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001014 && (nch >= 4)
1015 && validate_name(CHILD(tree, 0), "if")
1016 && validate_test(CHILD(tree, 1))
1017 && validate_colon(CHILD(tree, 2))
1018 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001019
1020 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001021 /* ... 'else' ':' suite */
1022 res = (validate_name(CHILD(tree, nch - 3), "else")
1023 && validate_colon(CHILD(tree, nch - 2))
1024 && validate_suite(CHILD(tree, nch - 1)));
1025 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001026 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001027 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001028 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001029 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001030 /* Will catch the case for nch < 4 */
1031 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001032 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001033 /* ... ('elif' test ':' suite)+ ... */
1034 int j = 4;
1035 while ((j < nch) && res) {
1036 res = (validate_name(CHILD(tree, j), "elif")
1037 && validate_colon(CHILD(tree, j + 2))
1038 && validate_test(CHILD(tree, j + 1))
1039 && validate_suite(CHILD(tree, j + 3)));
1040 j += 4;
1041 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001042 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001043 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001044}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001045
1046
Guido van Rossum3d602e31996-07-21 02:33:56 +00001047/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001048 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001049 *
1050 */
Guido van Rossum47478871996-08-21 14:32:37 +00001051static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001052validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001053{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001054 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001055 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001056
Guido van Rossum3d602e31996-07-21 02:33:56 +00001057 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001058 res = (validate_lparen(CHILD(tree, 0))
1059 && validate_rparen(CHILD(tree, nch - 1)));
1060 if (res && (nch == 3))
1061 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001062 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001063 else {
1064 (void) validate_numnodes(tree, 2, "parameters");
1065 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001066 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001067}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001068
1069
Fred Drakecff283c2000-08-21 22:24:43 +00001070/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001071 *
1072 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001073 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001074 * | NEWLINE INDENT stmt+ DEDENT
1075 */
Guido van Rossum47478871996-08-21 14:32:37 +00001076static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001077validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001078{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001079 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001080 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001081
Guido van Rossum3d602e31996-07-21 02:33:56 +00001082 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001083 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001084 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001085 /* NEWLINE INDENT stmt+ DEDENT */
1086 res = (validate_newline(CHILD(tree, 0))
1087 && validate_indent(CHILD(tree, 1))
1088 && validate_stmt(CHILD(tree, 2))
1089 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001090
Fred Drakeff9ea482000-04-19 13:54:15 +00001091 if (res && (nch > 4)) {
1092 int i = 3;
1093 --nch; /* forget the DEDENT */
1094 for ( ; res && (i < nch); ++i)
1095 res = validate_stmt(CHILD(tree, i));
1096 }
1097 else if (nch < 4)
1098 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001099 }
1100 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001101}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001102
1103
Guido van Rossum47478871996-08-21 14:32:37 +00001104static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001105validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001106{
Guido van Rossum47478871996-08-21 14:32:37 +00001107 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001108 validate_test, "testlist"));
1109}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001110
1111
Guido van Rossum1c917072001-10-15 15:44:05 +00001112static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001113validate_testlist1(node *tree)
1114{
1115 return (validate_repeating_list(tree, testlist1,
1116 validate_test, "testlist1"));
1117}
1118
1119
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001120/* validate either vfpdef or tfpdef.
1121 * vfpdef: NAME
1122 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001123 */
1124static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001125validate_vfpdef(node *tree)
Neal Norwitzc1505362006-12-28 06:47:50 +00001126{
1127 int nch = NCH(tree);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001128 if (TYPE(tree) == vfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001129 return nch == 1 && validate_name(CHILD(tree, 0), NULL);
1130 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001131 else if (TYPE(tree) == tfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001132 if (nch == 1) {
1133 return validate_name(CHILD(tree, 0), NULL);
1134 }
1135 else if (nch == 3) {
1136 return validate_name(CHILD(tree, 0), NULL) &&
1137 validate_colon(CHILD(tree, 1)) &&
1138 validate_test(CHILD(tree, 2));
1139 }
1140 }
1141 return 0;
1142}
1143
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001144/* '*' vfpdef (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
1145 * ..or tfpdef in place of vfpdef. vfpdef: NAME; tfpdef: NAME [':' test]
Fred Drakecff283c2000-08-21 22:24:43 +00001146 */
1147static int
1148validate_varargslist_trailer(node *tree, int start)
1149{
1150 int nch = NCH(tree);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001151 int res = 0, i;
Fred Drakecff283c2000-08-21 22:24:43 +00001152 int sym;
1153
1154 if (nch <= start) {
1155 err_string("expected variable argument trailer for varargslist");
1156 return 0;
1157 }
1158 sym = TYPE(CHILD(tree, start));
1159 if (sym == STAR) {
1160 /*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001161 * '*' vfpdef (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
Fred Drakecff283c2000-08-21 22:24:43 +00001162 */
1163 if (nch-start == 2)
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001164 res = validate_vfpdef(CHILD(tree, start+1));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001165 else if (nch-start == 5 && TYPE(CHILD(tree, start+2)) == COMMA)
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001166 res = (validate_vfpdef(CHILD(tree, start+1))
Fred Drakecff283c2000-08-21 22:24:43 +00001167 && validate_comma(CHILD(tree, start+2))
1168 && validate_doublestar(CHILD(tree, start+3))
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001169 && validate_vfpdef(CHILD(tree, start+4)));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001170 else {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001171 /* skip over vfpdef (',' vfpdef ['=' test])* */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001172 i = start + 1;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001173 if (TYPE(CHILD(tree, i)) == vfpdef ||
1174 TYPE(CHILD(tree, i)) == tfpdef) { /* skip over vfpdef or tfpdef */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001175 i += 1;
1176 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001177 while (res && i+1 < nch) { /* validate (',' vfpdef ['=' test])* */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001178 res = validate_comma(CHILD(tree, i));
Guido van Rossumfc158e22007-11-15 19:17:28 +00001179 if (TYPE(CHILD(tree, i+1)) == DOUBLESTAR)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001180 break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001181 res = res && validate_vfpdef(CHILD(tree, i+1));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001182 if (res && i+2 < nch && TYPE(CHILD(tree, i+2)) == EQUAL) {
Guido van Rossumfc158e22007-11-15 19:17:28 +00001183 res = res && (i+3 < nch)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001184 && validate_test(CHILD(tree, i+3));
1185 i += 4;
1186 }
1187 else {
1188 i += 2;
1189 }
1190 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001191 /* [',' '**' vfpdef] */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001192 if (res && i+1 < nch && TYPE(CHILD(tree, i+1)) == DOUBLESTAR) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001193 res = validate_vfpdef(CHILD(tree, i+2));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001194 }
1195 }
Fred Drakecff283c2000-08-21 22:24:43 +00001196 }
1197 else if (sym == DOUBLESTAR) {
1198 /*
1199 * '**' NAME
1200 */
1201 if (nch-start == 2)
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001202 res = validate_vfpdef(CHILD(tree, start+1));
Fred Drakecff283c2000-08-21 22:24:43 +00001203 }
1204 if (!res)
1205 err_string("illegal variable argument trailer for varargslist");
1206 return res;
1207}
1208
1209
Neal Norwitzc1505362006-12-28 06:47:50 +00001210/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001211 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001212 * Validate typedargslist or varargslist.
1213 *
1214 * typedargslist: ((tfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001215 * ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
1216 * '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001217 * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001218 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001219 * varargslist: ((vfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001220 * ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
1221 * '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001222 * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001223 * vfpdef: NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001224 *
1225 */
Guido van Rossum47478871996-08-21 14:32:37 +00001226static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001227validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001228{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001229 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001230 int res = (TYPE(tree) == varargslist ||
1231 TYPE(tree) == typedargslist) &&
1232 (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001233 int sym;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001234 node *ch;
1235 int i = 0;
Guido van Rossumfc158e22007-11-15 19:17:28 +00001236
Fred Drakeb6429a22000-12-11 22:08:27 +00001237 if (!res)
1238 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001239 if (nch < 1) {
1240 err_string("varargslist missing child nodes");
1241 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001242 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001243 while (i < nch) {
Guido van Rossumfc158e22007-11-15 19:17:28 +00001244 ch = CHILD(tree, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001245 sym = TYPE(ch);
1246 if (sym == vfpdef || sym == tfpdef) {
1247 /* validate (vfpdef ['=' test] ',')+ */
1248 res = validate_vfpdef(ch);
Fred Drakecff283c2000-08-21 22:24:43 +00001249 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001250 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1251 res = (validate_equal(CHILD(tree, i))
1252 && validate_test(CHILD(tree, i+1)));
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001253 if (res)
1254 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001255 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001256 if (res && i < nch) {
1257 res = validate_comma(CHILD(tree, i));
1258 ++i;
Fred Drakecff283c2000-08-21 22:24:43 +00001259 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001260 } else if (sym == DOUBLESTAR || sym == STAR) {
1261 res = validate_varargslist_trailer(tree, i);
1262 break;
1263 } else {
1264 res = 0;
1265 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001266 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001267 }
Fred Drakecff283c2000-08-21 22:24:43 +00001268 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001269}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001270
1271
Nick Coghlan650f0d02007-04-15 12:05:43 +00001272/* comp_iter: comp_for | comp_if
Fred Drakecff283c2000-08-21 22:24:43 +00001273 */
1274static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001275validate_comp_iter(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001276{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001277 int res = (validate_ntype(tree, comp_iter)
1278 && validate_numnodes(tree, 1, "comp_iter"));
1279 if (res && TYPE(CHILD(tree, 0)) == comp_for)
1280 res = validate_comp_for(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001281 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001282 res = validate_comp_if(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001283
1284 return res;
1285}
1286
Nick Coghlan650f0d02007-04-15 12:05:43 +00001287/* comp_for: 'for' exprlist 'in' test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001288 */
1289static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001290validate_comp_for(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001291{
1292 int nch = NCH(tree);
1293 int res;
1294
1295 if (nch == 5)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001296 res = validate_comp_iter(CHILD(tree, 4));
Fred Drakecff283c2000-08-21 22:24:43 +00001297 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001298 res = validate_numnodes(tree, 4, "comp_for");
Raymond Hettinger354433a2004-05-19 08:20:33 +00001299
1300 if (res)
1301 res = (validate_name(CHILD(tree, 0), "for")
1302 && validate_exprlist(CHILD(tree, 1))
1303 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001304 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001305
1306 return res;
1307}
1308
Nick Coghlan650f0d02007-04-15 12:05:43 +00001309/* comp_if: 'if' test_nocond [comp_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001310 */
1311static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001312validate_comp_if(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001313{
1314 int nch = NCH(tree);
1315 int res;
1316
1317 if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001318 res = validate_comp_iter(CHILD(tree, 2));
Fred Drakecff283c2000-08-21 22:24:43 +00001319 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001320 res = validate_numnodes(tree, 2, "comp_if");
Fred Drakecff283c2000-08-21 22:24:43 +00001321
1322 if (res)
1323 res = (validate_name(CHILD(tree, 0), "if")
Nick Coghlan650f0d02007-04-15 12:05:43 +00001324 && validate_test_nocond(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001325
1326 return res;
1327}
1328
1329
Guido van Rossum3d602e31996-07-21 02:33:56 +00001330/* simple_stmt | compound_stmt
1331 *
1332 */
Guido van Rossum47478871996-08-21 14:32:37 +00001333static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001334validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001335{
1336 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001337 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001338
Guido van Rossum3d602e31996-07-21 02:33:56 +00001339 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001340 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001341
Fred Drakeff9ea482000-04-19 13:54:15 +00001342 if (TYPE(tree) == simple_stmt)
1343 res = validate_simple_stmt(tree);
1344 else
1345 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001346 }
1347 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001348}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001349
1350
Guido van Rossum3d602e31996-07-21 02:33:56 +00001351/* small_stmt (';' small_stmt)* [';'] NEWLINE
1352 *
1353 */
Guido van Rossum47478871996-08-21 14:32:37 +00001354static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001355validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001356{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001357 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001358 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001359 && (nch >= 2)
1360 && validate_small_stmt(CHILD(tree, 0))
1361 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001362
Guido van Rossum3d602e31996-07-21 02:33:56 +00001363 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001364 res = validate_numnodes(tree, 2, "simple_stmt");
1365 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001366 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001367 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001368 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001369 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001370
Fred Drakeff9ea482000-04-19 13:54:15 +00001371 for (i = 1; res && (i < nch); i += 2)
1372 res = (validate_semi(CHILD(tree, i))
1373 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001374 }
1375 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001376}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001377
1378
Guido van Rossum47478871996-08-21 14:32:37 +00001379static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001380validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001381{
1382 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001383 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001384
Fred Drake0ac9b072000-09-12 21:58:06 +00001385 if (res) {
1386 int ntype = TYPE(CHILD(tree, 0));
1387
1388 if ( (ntype == expr_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001389 || (ntype == del_stmt)
1390 || (ntype == pass_stmt)
1391 || (ntype == flow_stmt)
1392 || (ntype == import_stmt)
1393 || (ntype == global_stmt)
Georg Brandl7cae87c2006-09-06 06:51:57 +00001394 || (ntype == assert_stmt))
Fred Drake0ac9b072000-09-12 21:58:06 +00001395 res = validate_node(CHILD(tree, 0));
1396 else {
1397 res = 0;
1398 err_string("illegal small_stmt child type");
1399 }
1400 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001401 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001402 res = 0;
1403 PyErr_Format(parser_error,
1404 "Unrecognized child node of small_stmt: %d.",
1405 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001406 }
1407 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001408}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001409
1410
1411/* compound_stmt:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001412 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef | decorated
Guido van Rossum3d602e31996-07-21 02:33:56 +00001413 */
Guido van Rossum47478871996-08-21 14:32:37 +00001414static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001415validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001416{
1417 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001418 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001419 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001420
1421 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001422 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001423
1424 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001425 ntype = TYPE(tree);
1426 if ( (ntype == if_stmt)
1427 || (ntype == while_stmt)
1428 || (ntype == for_stmt)
1429 || (ntype == try_stmt)
1430 || (ntype == funcdef)
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001431 || (ntype == classdef)
1432 || (ntype == decorated))
Fred Drakeff9ea482000-04-19 13:54:15 +00001433 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001434 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001435 res = 0;
1436 PyErr_Format(parser_error,
1437 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001438 }
1439 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001440}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001441
Guido van Rossum47478871996-08-21 14:32:37 +00001442static int
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001443validate_yield_or_testlist(node *tree)
1444{
Guido van Rossumfc158e22007-11-15 19:17:28 +00001445 if (TYPE(tree) == yield_expr)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001446 return validate_yield_expr(tree);
1447 else
1448 return validate_testlist(tree);
1449}
1450
1451static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001452validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001453{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001454 int j;
1455 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001456 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001457 && is_odd(nch)
1458 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001459
Fred Drake28f739a2000-08-25 22:42:40 +00001460 if (res && nch == 3
1461 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001462 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1463 && validate_yield_or_testlist(CHILD(tree, 2));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001464
Fred Drake28f739a2000-08-25 22:42:40 +00001465 if (res) {
1466 char *s = STR(CHILD(CHILD(tree, 1), 0));
1467
1468 res = (strcmp(s, "+=") == 0
1469 || strcmp(s, "-=") == 0
1470 || strcmp(s, "*=") == 0
1471 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001472 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001473 || strcmp(s, "%=") == 0
1474 || strcmp(s, "&=") == 0
1475 || strcmp(s, "|=") == 0
1476 || strcmp(s, "^=") == 0
1477 || strcmp(s, "<<=") == 0
1478 || strcmp(s, ">>=") == 0
1479 || strcmp(s, "**=") == 0);
1480 if (!res)
1481 err_string("illegal augmmented assignment operator");
1482 }
1483 }
1484 else {
1485 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001486 res = validate_equal(CHILD(tree, j))
1487 && validate_yield_or_testlist(CHILD(tree, j + 1));
Fred Drake28f739a2000-08-25 22:42:40 +00001488 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001489 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001490}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001491
1492
Guido van Rossum47478871996-08-21 14:32:37 +00001493static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001494validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001495{
1496 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001497 && validate_name(CHILD(tree, 0), "del")
1498 && validate_exprlist(CHILD(tree, 1)));
1499}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001500
1501
Guido van Rossum47478871996-08-21 14:32:37 +00001502static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001503validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001504{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001505 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001506 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001507 && ((nch == 1) || (nch == 2))
1508 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001509
Guido van Rossum3d602e31996-07-21 02:33:56 +00001510 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001511 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001512
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001513 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001514}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001515
1516
Guido van Rossum47478871996-08-21 14:32:37 +00001517static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001518validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001519{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001520 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001521 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001522 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001523
Guido van Rossum3d602e31996-07-21 02:33:56 +00001524 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001525 res = validate_name(CHILD(tree, 0), "raise");
1526 if (res && (nch >= 2))
1527 res = validate_test(CHILD(tree, 1));
1528 if (res && nch > 2) {
1529 res = (validate_comma(CHILD(tree, 2))
1530 && validate_test(CHILD(tree, 3)));
1531 if (res && (nch > 4))
1532 res = (validate_comma(CHILD(tree, 4))
1533 && validate_test(CHILD(tree, 5)));
1534 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001535 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001536 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001537 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001538 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001539 res = (validate_comma(CHILD(tree, 2))
1540 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001541
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001542 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001543}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001544
1545
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001546/* yield_expr: 'yield' [testlist]
1547 */
1548static int
1549validate_yield_expr(node *tree)
1550{
1551 int nch = NCH(tree);
1552 int res = (validate_ntype(tree, yield_expr)
1553 && ((nch == 1) || (nch == 2))
1554 && validate_name(CHILD(tree, 0), "yield"));
1555
1556 if (res && (nch == 2))
1557 res = validate_testlist(CHILD(tree, 1));
1558
1559 return (res);
1560}
1561
1562
1563/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001564 */
1565static int
1566validate_yield_stmt(node *tree)
1567{
1568 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001569 && validate_numnodes(tree, 1, "yield_stmt")
1570 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001571}
1572
1573
Fred Drakecff283c2000-08-21 22:24:43 +00001574static int
1575validate_import_as_name(node *tree)
1576{
1577 int nch = NCH(tree);
1578 int ok = validate_ntype(tree, import_as_name);
1579
1580 if (ok) {
1581 if (nch == 1)
1582 ok = validate_name(CHILD(tree, 0), NULL);
1583 else if (nch == 3)
1584 ok = (validate_name(CHILD(tree, 0), NULL)
1585 && validate_name(CHILD(tree, 1), "as")
1586 && validate_name(CHILD(tree, 2), NULL));
1587 else
1588 ok = validate_numnodes(tree, 3, "import_as_name");
1589 }
1590 return ok;
1591}
1592
1593
Fred Drake71137082001-01-07 05:59:59 +00001594/* dotted_name: NAME ("." NAME)*
1595 */
1596static int
1597validate_dotted_name(node *tree)
1598{
1599 int nch = NCH(tree);
1600 int res = (validate_ntype(tree, dotted_name)
1601 && is_odd(nch)
1602 && validate_name(CHILD(tree, 0), NULL));
1603 int i;
1604
1605 for (i = 1; res && (i < nch); i += 2) {
1606 res = (validate_dot(CHILD(tree, i))
1607 && validate_name(CHILD(tree, i+1), NULL));
1608 }
1609 return res;
1610}
1611
1612
Fred Drakecff283c2000-08-21 22:24:43 +00001613/* dotted_as_name: dotted_name [NAME NAME]
1614 */
1615static int
1616validate_dotted_as_name(node *tree)
1617{
1618 int nch = NCH(tree);
1619 int res = validate_ntype(tree, dotted_as_name);
1620
1621 if (res) {
1622 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001623 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001624 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001625 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001626 && validate_name(CHILD(tree, 1), "as")
1627 && validate_name(CHILD(tree, 2), NULL));
1628 else {
1629 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001630 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001631 }
1632 }
1633 return res;
1634}
1635
1636
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001637/* dotted_as_name (',' dotted_as_name)* */
1638static int
1639validate_dotted_as_names(node *tree)
1640{
1641 int nch = NCH(tree);
1642 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1643 int i;
1644
1645 for (i = 1; res && (i < nch); i += 2)
1646 res = (validate_comma(CHILD(tree, i))
1647 && validate_dotted_as_name(CHILD(tree, i + 1)));
1648 return (res);
1649}
1650
1651
1652/* import_as_name (',' import_as_name)* [','] */
1653static int
1654validate_import_as_names(node *tree)
1655{
1656 int nch = NCH(tree);
1657 int res = validate_import_as_name(CHILD(tree, 0));
1658 int i;
1659
1660 for (i = 1; res && (i + 1 < nch); i += 2)
1661 res = (validate_comma(CHILD(tree, i))
1662 && validate_import_as_name(CHILD(tree, i + 1)));
1663 return (res);
1664}
1665
1666
1667/* 'import' dotted_as_names */
1668static int
1669validate_import_name(node *tree)
1670{
1671 return (validate_ntype(tree, import_name)
1672 && validate_numnodes(tree, 2, "import_name")
1673 && validate_name(CHILD(tree, 0), "import")
1674 && validate_dotted_as_names(CHILD(tree, 1)));
1675}
1676
Guido van Rossumfc158e22007-11-15 19:17:28 +00001677/* Helper function to count the number of leading dots in
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001678 * 'from ...module import name'
1679 */
1680static int
1681count_from_dots(node *tree)
1682{
1683 int i;
1684 for (i = 0; i < NCH(tree); i++)
1685 if (TYPE(CHILD(tree, i)) != DOT)
1686 break;
1687 return i;
1688}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001689
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001690/* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001691 * import_as_names
Guido van Rossum3d602e31996-07-21 02:33:56 +00001692 */
Guido van Rossum47478871996-08-21 14:32:37 +00001693static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001694validate_import_from(node *tree)
1695{
1696 int nch = NCH(tree);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001697 int ndots = count_from_dots(tree);
1698 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1699 int offset = ndots + havename;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001700 int res = validate_ntype(tree, import_from)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001701 && (nch >= 4 + ndots)
1702 && validate_name(CHILD(tree, 0), "from")
1703 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1704 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001705
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001706 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1707 res = ((nch == offset + 5)
1708 && validate_lparen(CHILD(tree, offset + 2))
1709 && validate_import_as_names(CHILD(tree, offset + 3))
1710 && validate_rparen(CHILD(tree, offset + 4)));
1711 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1712 res = validate_import_as_names(CHILD(tree, offset + 2));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001713 return (res);
1714}
1715
1716
1717/* import_stmt: import_name | import_from */
1718static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001719validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001720{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001721 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001722 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001723
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001724 if (res) {
1725 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001726
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001727 if (ntype == import_name || ntype == import_from)
1728 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001729 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001730 res = 0;
1731 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001732 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001733 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001734 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001735 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001736 PyErr_Format(parser_error,
1737 "Unrecognized child node of import_stmt: %d.",
1738 TYPE(CHILD(tree, 0)));
1739 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001740 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001741}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001742
1743
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001744
1745
Guido van Rossum47478871996-08-21 14:32:37 +00001746static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001747validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001748{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001749 int j;
1750 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001751 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001752 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001753
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001754 if (!res && !PyErr_Occurred())
1755 err_string("illegal global statement");
1756
Guido van Rossum3d602e31996-07-21 02:33:56 +00001757 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001758 res = (validate_name(CHILD(tree, 0), "global")
1759 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001760 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001761 res = (validate_comma(CHILD(tree, j))
1762 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001763
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001764 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001765}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001766
1767
Guido van Rossum925e5471997-04-02 05:32:13 +00001768/* assert_stmt:
1769 *
1770 * 'assert' test [',' test]
1771 */
1772static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001773validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001774{
1775 int nch = NCH(tree);
1776 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001777 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001778 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001779 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001780
1781 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001782 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001783 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001784 res = (validate_comma(CHILD(tree, 2))
1785 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001786
1787 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001788}
Guido van Rossum925e5471997-04-02 05:32:13 +00001789
1790
Guido van Rossum47478871996-08-21 14:32:37 +00001791static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001792validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001793{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001794 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001795 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001796 && ((nch == 4) || (nch == 7))
1797 && validate_name(CHILD(tree, 0), "while")
1798 && validate_test(CHILD(tree, 1))
1799 && validate_colon(CHILD(tree, 2))
1800 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001801
Guido van Rossum3d602e31996-07-21 02:33:56 +00001802 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001803 res = (validate_name(CHILD(tree, 4), "else")
1804 && validate_colon(CHILD(tree, 5))
1805 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001806
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001807 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001808}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001809
1810
Guido van Rossum47478871996-08-21 14:32:37 +00001811static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001812validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001813{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001814 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001815 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001816 && ((nch == 6) || (nch == 9))
1817 && validate_name(CHILD(tree, 0), "for")
1818 && validate_exprlist(CHILD(tree, 1))
1819 && validate_name(CHILD(tree, 2), "in")
1820 && validate_testlist(CHILD(tree, 3))
1821 && validate_colon(CHILD(tree, 4))
1822 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001823
Guido van Rossum3d602e31996-07-21 02:33:56 +00001824 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001825 res = (validate_name(CHILD(tree, 6), "else")
1826 && validate_colon(CHILD(tree, 7))
1827 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001828
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001829 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001830}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001831
1832
Guido van Rossum3d602e31996-07-21 02:33:56 +00001833/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001834 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001835 * | 'try' ':' suite 'finally' ':' suite
1836 *
1837 */
Guido van Rossum47478871996-08-21 14:32:37 +00001838static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001839validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001840{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001841 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001842 int pos = 3;
1843 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001844 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001845
1846 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001847 res = (validate_name(CHILD(tree, 0), "try")
1848 && validate_colon(CHILD(tree, 1))
1849 && validate_suite(CHILD(tree, 2))
1850 && validate_colon(CHILD(tree, nch - 2))
1851 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001852 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001853 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001854 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1855 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001856
1857 PyErr_Format(parser_error,
1858 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001859 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001860 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001861 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001862 res = (validate_except_clause(CHILD(tree, pos))
1863 && validate_colon(CHILD(tree, pos + 1))
1864 && validate_suite(CHILD(tree, pos + 2)));
1865 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001866 }
1867 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001868 res = validate_ntype(CHILD(tree, pos), NAME);
1869 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1870 res = (validate_numnodes(tree, 6, "try/finally")
1871 && validate_colon(CHILD(tree, 4))
1872 && validate_suite(CHILD(tree, 5)));
1873 else if (res) {
1874 if (nch == (pos + 3)) {
1875 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1876 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1877 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001878 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001879 }
1880 else if (nch == (pos + 6)) {
1881 res = (validate_name(CHILD(tree, pos), "except")
1882 && validate_colon(CHILD(tree, pos + 1))
1883 && validate_suite(CHILD(tree, pos + 2))
1884 && validate_name(CHILD(tree, pos + 3), "else"));
1885 }
1886 else
1887 res = validate_numnodes(tree, pos + 3, "try/except");
1888 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001889 }
1890 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001891}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001892
1893
Guido van Rossum47478871996-08-21 14:32:37 +00001894static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001895validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001896{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001897 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001898 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001899 && ((nch == 1) || (nch == 2) || (nch == 4))
1900 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001901
Guido van Rossum3d602e31996-07-21 02:33:56 +00001902 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001903 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001904 if (res && (nch == 4))
Guido van Rossumb940e112007-01-10 16:19:56 +00001905 res = (validate_name(CHILD(tree, 2), "as")
1906 && validate_ntype(CHILD(tree, 3), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001907
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001908 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001909}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001910
1911
Guido van Rossum47478871996-08-21 14:32:37 +00001912static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001913validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001914{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001915 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001916 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001917
Guido van Rossum3d602e31996-07-21 02:33:56 +00001918 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001919 res = ((nch == 1)
1920 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001921 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001922 res = validate_or_test(CHILD(tree, 0));
1923 res = (res && (nch == 1 || (nch == 5 &&
1924 validate_name(CHILD(tree, 1), "if") &&
1925 validate_or_test(CHILD(tree, 2)) &&
1926 validate_name(CHILD(tree, 3), "else") &&
1927 validate_test(CHILD(tree, 4)))));
1928 }
1929 return (res);
1930}
1931
1932static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001933validate_test_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001934{
1935 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001936 int res = validate_ntype(tree, test_nocond) && (nch == 1);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001937
Nick Coghlan650f0d02007-04-15 12:05:43 +00001938 if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
1939 res = (validate_lambdef_nocond(CHILD(tree, 0)));
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001940 else if (res) {
1941 res = (validate_or_test(CHILD(tree, 0)));
1942 }
1943 return (res);
1944}
1945
1946static int
1947validate_or_test(node *tree)
1948{
1949 int nch = NCH(tree);
1950 int res = validate_ntype(tree, or_test) && is_odd(nch);
1951
1952 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001953 int pos;
1954 res = validate_and_test(CHILD(tree, 0));
1955 for (pos = 1; res && (pos < nch); pos += 2)
1956 res = (validate_name(CHILD(tree, pos), "or")
1957 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001958 }
1959 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001960}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001961
1962
Guido van Rossum47478871996-08-21 14:32:37 +00001963static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001964validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001965{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001966 int pos;
1967 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001968 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001969 && is_odd(nch)
1970 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001971
Guido van Rossum3d602e31996-07-21 02:33:56 +00001972 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001973 res = (validate_name(CHILD(tree, pos), "and")
1974 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001975
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001976 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001977}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001978
1979
Guido van Rossum47478871996-08-21 14:32:37 +00001980static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001981validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001982{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001983 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001984 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001985
Guido van Rossum3d602e31996-07-21 02:33:56 +00001986 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001987 if (nch == 2)
1988 res = (validate_name(CHILD(tree, 0), "not")
1989 && validate_not_test(CHILD(tree, 1)));
1990 else if (nch == 1)
1991 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001992 }
1993 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001994}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001995
1996
Guido van Rossum47478871996-08-21 14:32:37 +00001997static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001998validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001999{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002000 int pos;
2001 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002002 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002003 && is_odd(nch)
Guido van Rossum0368b722007-05-11 16:50:42 +00002004 && validate_star_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002005
Guido van Rossum3d602e31996-07-21 02:33:56 +00002006 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002007 res = (validate_comp_op(CHILD(tree, pos))
Guido van Rossum0368b722007-05-11 16:50:42 +00002008 && validate_star_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002009
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002010 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002011}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002012
2013
Guido van Rossum47478871996-08-21 14:32:37 +00002014static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002015validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002016{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002017 int res = 0;
2018 int nch = NCH(tree);
2019
Guido van Rossum3d602e31996-07-21 02:33:56 +00002020 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002021 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002022 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002023 /*
2024 * Only child will be a terminal with a well-defined symbolic name
2025 * or a NAME with a string of either 'is' or 'in'
2026 */
2027 tree = CHILD(tree, 0);
2028 switch (TYPE(tree)) {
2029 case LESS:
2030 case GREATER:
2031 case EQEQUAL:
2032 case EQUAL:
2033 case LESSEQUAL:
2034 case GREATEREQUAL:
2035 case NOTEQUAL:
2036 res = 1;
2037 break;
2038 case NAME:
2039 res = ((strcmp(STR(tree), "in") == 0)
2040 || (strcmp(STR(tree), "is") == 0));
2041 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002042 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002043 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002044 }
2045 break;
2046 default:
Fred Drake661ea262000-10-24 19:57:45 +00002047 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002048 break;
2049 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002050 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002051 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002052 res = (validate_ntype(CHILD(tree, 0), NAME)
2053 && validate_ntype(CHILD(tree, 1), NAME)
2054 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2055 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2056 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2057 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2058 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002059 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002060 }
2061 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002062}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002063
2064
Guido van Rossum47478871996-08-21 14:32:37 +00002065static int
Guido van Rossum0368b722007-05-11 16:50:42 +00002066validate_star_expr(node *tree)
2067{
2068 int res = validate_ntype(tree, star_expr);
2069 if (!res) return res;
2070 if (NCH(tree) == 2) {
2071 return validate_ntype(CHILD(tree, 0), STAR) && \
2072 validate_expr(CHILD(tree, 1));
2073 } else {
2074 return validate_expr(CHILD(tree, 0));
2075 }
2076}
2077
2078
2079static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002080validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002081{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002082 int j;
2083 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002084 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002085 && is_odd(nch)
2086 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002087
Guido van Rossum3d602e31996-07-21 02:33:56 +00002088 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002089 res = (validate_xor_expr(CHILD(tree, j))
2090 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002091
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002092 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002093}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002094
2095
Guido van Rossum47478871996-08-21 14:32:37 +00002096static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002097validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002098{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002099 int j;
2100 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002101 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002102 && is_odd(nch)
2103 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002104
Guido van Rossum3d602e31996-07-21 02:33:56 +00002105 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002106 res = (validate_circumflex(CHILD(tree, j - 1))
2107 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002108
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002109 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002110}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002111
2112
Guido van Rossum47478871996-08-21 14:32:37 +00002113static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002114validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002115{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002116 int pos;
2117 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002118 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002119 && is_odd(nch)
2120 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002121
Guido van Rossum3d602e31996-07-21 02:33:56 +00002122 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002123 res = (validate_ampersand(CHILD(tree, pos))
2124 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002125
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002126 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002127}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002128
2129
2130static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002131validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002132 {
2133 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002134 int nch = NCH(tree);
2135 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002136 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002137
Guido van Rossum3d602e31996-07-21 02:33:56 +00002138 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002139 if (TYPE(CHILD(tree, pos)) != op1)
2140 res = validate_ntype(CHILD(tree, pos), op2);
2141 if (res)
2142 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002143 }
2144 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002145}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002146
2147
Guido van Rossum47478871996-08-21 14:32:37 +00002148static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002149validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002150{
2151 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002152 && validate_chain_two_ops(tree, validate_arith_expr,
2153 LEFTSHIFT, RIGHTSHIFT));
2154}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002155
2156
Guido van Rossum47478871996-08-21 14:32:37 +00002157static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002158validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002159{
2160 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002161 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2162}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002163
2164
Guido van Rossum47478871996-08-21 14:32:37 +00002165static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002166validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002167{
2168 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002169 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002170 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002171 && is_odd(nch)
2172 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002173
Guido van Rossum3d602e31996-07-21 02:33:56 +00002174 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002175 res = (((TYPE(CHILD(tree, pos)) == STAR)
2176 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002177 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002178 || (TYPE(CHILD(tree, pos)) == PERCENT))
2179 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002180
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002181 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002182}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002183
2184
Guido van Rossum3d602e31996-07-21 02:33:56 +00002185/* factor:
2186 *
2187 * factor: ('+'|'-'|'~') factor | power
2188 */
Guido van Rossum47478871996-08-21 14:32:37 +00002189static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002190validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002191{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002192 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002193 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002194 && (((nch == 2)
2195 && ((TYPE(CHILD(tree, 0)) == PLUS)
2196 || (TYPE(CHILD(tree, 0)) == MINUS)
2197 || (TYPE(CHILD(tree, 0)) == TILDE))
2198 && validate_factor(CHILD(tree, 1)))
2199 || ((nch == 1)
2200 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002201 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002202}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002203
2204
Guido van Rossum3d602e31996-07-21 02:33:56 +00002205/* power:
2206 *
2207 * power: atom trailer* ('**' factor)*
2208 */
Guido van Rossum47478871996-08-21 14:32:37 +00002209static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002210validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002211{
2212 int pos = 1;
2213 int nch = NCH(tree);
2214 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002215 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002216
2217 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002218 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002219 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002220 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002221 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002222 return (0);
2223 }
2224 for ( ; res && (pos < (nch - 1)); pos += 2)
2225 res = (validate_doublestar(CHILD(tree, pos))
2226 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002227 }
2228 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002229}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002230
2231
Guido van Rossum47478871996-08-21 14:32:37 +00002232static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002233validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002234{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002235 int pos;
2236 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002237 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002238
Fred Drakecff283c2000-08-21 22:24:43 +00002239 if (res && nch < 1)
2240 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002241 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002242 switch (TYPE(CHILD(tree, 0))) {
2243 case LPAR:
2244 res = ((nch <= 3)
2245 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002246
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002247 if (res && (nch == 3)) {
2248 if (TYPE(CHILD(tree, 1))==yield_expr)
2249 res = validate_yield_expr(CHILD(tree, 1));
2250 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00002251 res = validate_testlist_comp(CHILD(tree, 1));
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002252 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002253 break;
2254 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002255 if (nch == 2)
2256 res = validate_ntype(CHILD(tree, 1), RSQB);
2257 else if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002258 res = (validate_testlist_comp(CHILD(tree, 1))
Fred Drakecff283c2000-08-21 22:24:43 +00002259 && validate_ntype(CHILD(tree, 2), RSQB));
2260 else {
2261 res = 0;
2262 err_string("illegal list display atom");
2263 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002264 break;
2265 case LBRACE:
2266 res = ((nch <= 3)
2267 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002268
Fred Drakeff9ea482000-04-19 13:54:15 +00002269 if (res && (nch == 3))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002270 res = validate_dictorsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002271 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002272 case NAME:
2273 case NUMBER:
2274 res = (nch == 1);
2275 break;
2276 case STRING:
2277 for (pos = 1; res && (pos < nch); ++pos)
2278 res = validate_ntype(CHILD(tree, pos), STRING);
2279 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002280 case DOT:
2281 res = (nch == 3 &&
2282 validate_ntype(CHILD(tree, 1), DOT) &&
2283 validate_ntype(CHILD(tree, 2), DOT));
2284 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002285 default:
2286 res = 0;
2287 break;
2288 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002289 }
2290 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002291}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002292
2293
Nick Coghlan650f0d02007-04-15 12:05:43 +00002294/* testlist_comp:
2295 * test ( comp_for | (',' test)* [','] )
Fred Drake85bf3bb2000-08-23 15:35:26 +00002296 */
Fred Drakecff283c2000-08-21 22:24:43 +00002297static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002298validate_testlist_comp(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00002299{
2300 int nch = NCH(tree);
2301 int ok = nch;
2302
2303 if (nch == 0)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002304 err_string("missing child nodes of testlist_comp");
2305 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002306 ok = validate_test(CHILD(tree, 0));
Nick Coghlan650f0d02007-04-15 12:05:43 +00002307 }
Fred Drakecff283c2000-08-21 22:24:43 +00002308
2309 /*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002310 * comp_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002311 */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002312 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2313 ok = validate_comp_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002314 else {
2315 /* (',' test)* [','] */
2316 int i = 1;
2317 while (ok && nch - i >= 2) {
2318 ok = (validate_comma(CHILD(tree, i))
2319 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002320 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002321 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002322 if (ok && i == nch-1)
2323 ok = validate_comma(CHILD(tree, i));
2324 else if (i != nch) {
2325 ok = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002326 err_string("illegal trailing nodes for testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002327 }
2328 }
2329 return ok;
2330}
Fred Drakecff283c2000-08-21 22:24:43 +00002331
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002332/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002333 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002334 */
2335static int
2336validate_decorator(node *tree)
2337{
2338 int ok;
2339 int nch = NCH(tree);
2340 ok = (validate_ntype(tree, decorator) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002341 (nch == 3 || nch == 5 || nch == 6) &&
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002342 validate_at(CHILD(tree, 0)) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002343 validate_dotted_name(CHILD(tree, 1)) &&
2344 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002345
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002346 if (ok && nch != 3) {
2347 ok = (validate_lparen(CHILD(tree, 2)) &&
2348 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002349
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002350 if (ok && nch == 6)
2351 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002352 }
2353
2354 return ok;
2355}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002356
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002357/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002358 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002359 */
2360static int
2361validate_decorators(node *tree)
2362{
Guido van Rossumfc158e22007-11-15 19:17:28 +00002363 int i, nch, ok;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002364 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002365 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002366
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002367 for (i = 0; ok && i < nch; ++i)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002368 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002369
2370 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002371}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002372
Guido van Rossum3d602e31996-07-21 02:33:56 +00002373/* funcdef:
Guido van Rossumfc158e22007-11-15 19:17:28 +00002374 *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002375 * -5 -4 -3 -2 -1
2376 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002377 */
Guido van Rossum47478871996-08-21 14:32:37 +00002378static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002379validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002380{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002381 int nch = NCH(tree);
2382 int ok = (validate_ntype(tree, funcdef)
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002383 && (nch == 5)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002384 && validate_name(RCHILD(tree, -5), "def")
2385 && validate_ntype(RCHILD(tree, -4), NAME)
2386 && validate_colon(RCHILD(tree, -2))
2387 && validate_parameters(RCHILD(tree, -3))
2388 && validate_suite(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002389 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002390}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002391
2392
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002393/* decorated
2394 * decorators (classdef | funcdef)
2395 */
2396static int
2397validate_decorated(node *tree)
2398{
2399 int nch = NCH(tree);
2400 int ok = (validate_ntype(tree, decorated)
2401 && (nch == 2)
2402 && validate_decorators(RCHILD(tree, -2))
2403 && (validate_funcdef(RCHILD(tree, -1))
2404 || validate_class(RCHILD(tree, -1)))
2405 );
2406 return ok;
2407}
2408
Guido van Rossum47478871996-08-21 14:32:37 +00002409static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002410validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002411{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002412 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002413 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002414 && ((nch == 3) || (nch == 4))
2415 && validate_name(CHILD(tree, 0), "lambda")
2416 && validate_colon(CHILD(tree, nch - 2))
2417 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002418
Guido van Rossum3d602e31996-07-21 02:33:56 +00002419 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002420 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002421 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002422 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002423
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002424 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002425}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002426
2427
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002428static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002429validate_lambdef_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002430{
2431 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002432 int res = (validate_ntype(tree, lambdef_nocond)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002433 && ((nch == 3) || (nch == 4))
2434 && validate_name(CHILD(tree, 0), "lambda")
2435 && validate_colon(CHILD(tree, nch - 2))
2436 && validate_test(CHILD(tree, nch - 1)));
2437
2438 if (res && (nch == 4))
2439 res = validate_varargslist(CHILD(tree, 1));
2440 else if (!res && !PyErr_Occurred())
Nick Coghlan650f0d02007-04-15 12:05:43 +00002441 (void) validate_numnodes(tree, 3, "lambdef_nocond");
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002442
2443 return (res);
2444}
2445
2446
Guido van Rossum3d602e31996-07-21 02:33:56 +00002447/* arglist:
2448 *
Fred Drakecff283c2000-08-21 22:24:43 +00002449 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002450 */
Guido van Rossum47478871996-08-21 14:32:37 +00002451static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002452validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002453{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002454 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002455 int i = 0;
2456 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002457
2458 if (nch <= 0)
2459 /* raise the right error from having an invalid number of children */
2460 return validate_numnodes(tree, nch + 1, "arglist");
2461
Raymond Hettinger354433a2004-05-19 08:20:33 +00002462 if (nch > 1) {
2463 for (i=0; i<nch; i++) {
2464 if (TYPE(CHILD(tree, i)) == argument) {
2465 node *ch = CHILD(tree, i);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002466 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
Raymond Hettinger354433a2004-05-19 08:20:33 +00002467 err_string("need '(', ')' for generator expression");
2468 return 0;
2469 }
2470 }
2471 }
2472 }
2473
Fred Drakecff283c2000-08-21 22:24:43 +00002474 while (ok && nch-i >= 2) {
2475 /* skip leading (argument ',') */
2476 ok = (validate_argument(CHILD(tree, i))
2477 && validate_comma(CHILD(tree, i+1)));
2478 if (ok)
2479 i += 2;
2480 else
2481 PyErr_Clear();
2482 }
2483 ok = 1;
2484 if (nch-i > 0) {
2485 /*
2486 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002487 */
Fred Drakecff283c2000-08-21 22:24:43 +00002488 int sym = TYPE(CHILD(tree, i));
2489
2490 if (sym == argument) {
2491 ok = validate_argument(CHILD(tree, i));
2492 if (ok && i+1 != nch) {
2493 err_string("illegal arglist specification"
2494 " (extra stuff on end)");
2495 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002496 }
Fred Drakecff283c2000-08-21 22:24:43 +00002497 }
2498 else if (sym == STAR) {
2499 ok = validate_star(CHILD(tree, i));
2500 if (ok && (nch-i == 2))
2501 ok = validate_test(CHILD(tree, i+1));
2502 else if (ok && (nch-i == 5))
2503 ok = (validate_test(CHILD(tree, i+1))
2504 && validate_comma(CHILD(tree, i+2))
2505 && validate_doublestar(CHILD(tree, i+3))
2506 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002507 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002508 err_string("illegal use of '*' in arglist");
2509 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002510 }
2511 }
Fred Drakecff283c2000-08-21 22:24:43 +00002512 else if (sym == DOUBLESTAR) {
2513 if (nch-i == 2)
2514 ok = (validate_doublestar(CHILD(tree, i))
2515 && validate_test(CHILD(tree, i+1)));
2516 else {
2517 err_string("illegal use of '**' in arglist");
2518 ok = 0;
2519 }
2520 }
2521 else {
2522 err_string("illegal arglist specification");
2523 ok = 0;
2524 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002525 }
2526 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002527}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002528
2529
2530
2531/* argument:
2532 *
Nick Coghlan650f0d02007-04-15 12:05:43 +00002533 * [test '='] test [comp_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002534 */
Guido van Rossum47478871996-08-21 14:32:37 +00002535static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002536validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002537{
2538 int nch = NCH(tree);
2539 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002540 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002541 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002542
Raymond Hettinger354433a2004-05-19 08:20:33 +00002543 if (res && (nch == 2))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002544 res = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002545 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002546 res = (validate_equal(CHILD(tree, 1))
2547 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002548
2549 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002550}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002551
2552
2553
2554/* trailer:
2555 *
Guido van Rossum47478871996-08-21 14:32:37 +00002556 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002557 */
Guido van Rossum47478871996-08-21 14:32:37 +00002558static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002559validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002560{
2561 int nch = NCH(tree);
2562 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002563
2564 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002565 switch (TYPE(CHILD(tree, 0))) {
2566 case LPAR:
2567 res = validate_rparen(CHILD(tree, nch - 1));
2568 if (res && (nch == 3))
2569 res = validate_arglist(CHILD(tree, 1));
2570 break;
2571 case LSQB:
2572 res = (validate_numnodes(tree, 3, "trailer")
2573 && validate_subscriptlist(CHILD(tree, 1))
2574 && validate_ntype(CHILD(tree, 2), RSQB));
2575 break;
2576 case DOT:
2577 res = (validate_numnodes(tree, 2, "trailer")
2578 && validate_ntype(CHILD(tree, 1), NAME));
2579 break;
2580 default:
2581 res = 0;
2582 break;
2583 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002584 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002585 else {
2586 (void) validate_numnodes(tree, 2, "trailer");
2587 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002588 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002589}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002590
2591
Guido van Rossum47478871996-08-21 14:32:37 +00002592/* subscriptlist:
2593 *
2594 * subscript (',' subscript)* [',']
2595 */
2596static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002597validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002598{
2599 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002600 validate_subscript, "subscriptlist"));
2601}
Guido van Rossum47478871996-08-21 14:32:37 +00002602
2603
Guido van Rossum3d602e31996-07-21 02:33:56 +00002604/* subscript:
2605 *
Guido van Rossum47478871996-08-21 14:32:37 +00002606 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002607 */
Guido van Rossum47478871996-08-21 14:32:37 +00002608static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002609validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002610{
Guido van Rossum47478871996-08-21 14:32:37 +00002611 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002612 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002613 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002614
Guido van Rossum47478871996-08-21 14:32:37 +00002615 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002616 if (!PyErr_Occurred())
2617 err_string("invalid number of arguments for subscript node");
2618 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002619 }
Guido van Rossum47478871996-08-21 14:32:37 +00002620 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002621 /* take care of ('.' '.' '.') possibility */
2622 return (validate_numnodes(tree, 3, "subscript")
2623 && validate_dot(CHILD(tree, 0))
2624 && validate_dot(CHILD(tree, 1))
2625 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002626 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002627 if (TYPE(CHILD(tree, 0)) == test)
2628 res = validate_test(CHILD(tree, 0));
2629 else
2630 res = validate_colon(CHILD(tree, 0));
2631 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002632 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002633 /* Must be [test] ':' [test] [sliceop],
2634 * but at least one of the optional components will
2635 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002636 */
2637 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002638 res = validate_test(CHILD(tree, 0));
2639 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002640 }
2641 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002642 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002643 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002644 int rem = nch - ++offset;
2645 if (rem) {
2646 if (TYPE(CHILD(tree, offset)) == test) {
2647 res = validate_test(CHILD(tree, offset));
2648 ++offset;
2649 --rem;
2650 }
2651 if (res && rem)
2652 res = validate_sliceop(CHILD(tree, offset));
2653 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002654 }
2655 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002656}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002657
2658
Guido van Rossum47478871996-08-21 14:32:37 +00002659static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002660validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002661{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002662 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002663 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002664 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002665 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002666 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002667 }
Guido van Rossum47478871996-08-21 14:32:37 +00002668 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002669 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002670 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002671 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002672
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002673 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002674}
Guido van Rossum47478871996-08-21 14:32:37 +00002675
2676
2677static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002678validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002679{
2680 return (validate_repeating_list(tree, exprlist,
Guido van Rossum0368b722007-05-11 16:50:42 +00002681 validate_star_expr, "exprlist"));
Fred Drakeff9ea482000-04-19 13:54:15 +00002682}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002683
2684
Guido van Rossum47478871996-08-21 14:32:37 +00002685static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002686validate_dictorsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002687{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002688 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002689 int res = (validate_ntype(tree, dictorsetmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002690 && (nch >= 3)
2691 && validate_test(CHILD(tree, 0))
2692 && validate_colon(CHILD(tree, 1))
2693 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002694
Guido van Rossum3d602e31996-07-21 02:33:56 +00002695 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002696 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002697 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002698 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002699
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002700 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002701 int pos = 3;
2702 /* ( ',' test ':' test )* */
2703 while (res && (pos < nch)) {
2704 res = (validate_comma(CHILD(tree, pos))
2705 && validate_test(CHILD(tree, pos + 1))
2706 && validate_colon(CHILD(tree, pos + 2))
2707 && validate_test(CHILD(tree, pos + 3)));
2708 pos += 4;
2709 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002710 }
2711 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002712}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002713
2714
Guido van Rossum47478871996-08-21 14:32:37 +00002715static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002716validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002717{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002718 int pos;
2719 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002720 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002721 && (nch >= 2)
2722 && validate_testlist(CHILD(tree, 0))
2723 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002724
Guido van Rossum3d602e31996-07-21 02:33:56 +00002725 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002726 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002727
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002728 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002729}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002730
2731
Guido van Rossum47478871996-08-21 14:32:37 +00002732static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002733validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002734{
Fred Drakeff9ea482000-04-19 13:54:15 +00002735 int nch = 0; /* num. children on current node */
2736 int res = 1; /* result value */
2737 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002738
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002739 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002740 nch = NCH(tree);
2741 next = 0;
2742 switch (TYPE(tree)) {
2743 /*
2744 * Definition nodes.
2745 */
2746 case funcdef:
2747 res = validate_funcdef(tree);
2748 break;
2749 case classdef:
2750 res = validate_class(tree);
2751 break;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002752 case decorated:
2753 res = validate_decorated(tree);
2754 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002755 /*
2756 * "Trivial" parse tree nodes.
2757 * (Why did I call these trivial?)
2758 */
2759 case stmt:
2760 res = validate_stmt(tree);
2761 break;
2762 case small_stmt:
2763 /*
Guido van Rossum452bf512007-02-09 05:32:43 +00002764 * expr_stmt | del_stmt | pass_stmt | flow_stmt
Georg Brandl7cae87c2006-09-06 06:51:57 +00002765 * | import_stmt | global_stmt | assert_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002766 */
2767 res = validate_small_stmt(tree);
2768 break;
2769 case flow_stmt:
2770 res = (validate_numnodes(tree, 1, "flow_stmt")
2771 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2772 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002773 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002774 || (TYPE(CHILD(tree, 0)) == return_stmt)
2775 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2776 if (res)
2777 next = CHILD(tree, 0);
2778 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002779 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002780 break;
Fred Drake02126f22001-07-17 02:59:15 +00002781 case yield_stmt:
2782 res = validate_yield_stmt(tree);
2783 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002784 /*
2785 * Compound statements.
2786 */
2787 case simple_stmt:
2788 res = validate_simple_stmt(tree);
2789 break;
2790 case compound_stmt:
2791 res = validate_compound_stmt(tree);
2792 break;
2793 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002794 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002795 */
2796 case expr_stmt:
2797 res = validate_expr_stmt(tree);
2798 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002799 case del_stmt:
2800 res = validate_del_stmt(tree);
2801 break;
2802 case pass_stmt:
2803 res = (validate_numnodes(tree, 1, "pass")
2804 && validate_name(CHILD(tree, 0), "pass"));
2805 break;
2806 case break_stmt:
2807 res = (validate_numnodes(tree, 1, "break")
2808 && validate_name(CHILD(tree, 0), "break"));
2809 break;
2810 case continue_stmt:
2811 res = (validate_numnodes(tree, 1, "continue")
2812 && validate_name(CHILD(tree, 0), "continue"));
2813 break;
2814 case return_stmt:
2815 res = validate_return_stmt(tree);
2816 break;
2817 case raise_stmt:
2818 res = validate_raise_stmt(tree);
2819 break;
2820 case import_stmt:
2821 res = validate_import_stmt(tree);
2822 break;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00002823 case import_name:
2824 res = validate_import_name(tree);
2825 break;
2826 case import_from:
2827 res = validate_import_from(tree);
2828 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002829 case global_stmt:
2830 res = validate_global_stmt(tree);
2831 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002832 case assert_stmt:
2833 res = validate_assert_stmt(tree);
2834 break;
2835 case if_stmt:
2836 res = validate_if(tree);
2837 break;
2838 case while_stmt:
2839 res = validate_while(tree);
2840 break;
2841 case for_stmt:
2842 res = validate_for(tree);
2843 break;
2844 case try_stmt:
2845 res = validate_try(tree);
2846 break;
2847 case suite:
2848 res = validate_suite(tree);
2849 break;
2850 /*
2851 * Expression nodes.
2852 */
2853 case testlist:
2854 res = validate_testlist(tree);
2855 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002856 case yield_expr:
2857 res = validate_yield_expr(tree);
2858 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002859 case testlist1:
2860 res = validate_testlist1(tree);
2861 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002862 case test:
2863 res = validate_test(tree);
2864 break;
2865 case and_test:
2866 res = validate_and_test(tree);
2867 break;
2868 case not_test:
2869 res = validate_not_test(tree);
2870 break;
2871 case comparison:
2872 res = validate_comparison(tree);
2873 break;
2874 case exprlist:
2875 res = validate_exprlist(tree);
2876 break;
2877 case comp_op:
2878 res = validate_comp_op(tree);
2879 break;
2880 case expr:
2881 res = validate_expr(tree);
2882 break;
2883 case xor_expr:
2884 res = validate_xor_expr(tree);
2885 break;
2886 case and_expr:
2887 res = validate_and_expr(tree);
2888 break;
2889 case shift_expr:
2890 res = validate_shift_expr(tree);
2891 break;
2892 case arith_expr:
2893 res = validate_arith_expr(tree);
2894 break;
2895 case term:
2896 res = validate_term(tree);
2897 break;
2898 case factor:
2899 res = validate_factor(tree);
2900 break;
2901 case power:
2902 res = validate_power(tree);
2903 break;
2904 case atom:
2905 res = validate_atom(tree);
2906 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002907
Fred Drakeff9ea482000-04-19 13:54:15 +00002908 default:
2909 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002910 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002911 res = 0;
2912 break;
2913 }
2914 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002915 }
2916 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002917}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002918
2919
Guido van Rossum47478871996-08-21 14:32:37 +00002920static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002921validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002922{
2923 int res = validate_eval_input(tree);
2924
2925 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002926 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002927
2928 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002929}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002930
2931
Guido van Rossum3d602e31996-07-21 02:33:56 +00002932/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002933 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002934 */
Guido van Rossum47478871996-08-21 14:32:37 +00002935static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002936validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002937{
Fred Drakec2683dd2001-07-17 19:32:05 +00002938 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002939 int nch = NCH(tree) - 1;
2940 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002941 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002942
Fred Drakec2683dd2001-07-17 19:32:05 +00002943 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002944 if (TYPE(CHILD(tree, j)) == stmt)
2945 res = validate_stmt(CHILD(tree, j));
2946 else
2947 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002948 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002949 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002950 * user. Hopefully, this won't be needed. If a user reports getting
2951 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002952 */
2953 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002954 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002955
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002956 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002957}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002958
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00002959static int
2960validate_encoding_decl(node *tree)
2961{
2962 int nch = NCH(tree);
2963 int res = ((nch == 1)
2964 && validate_file_input(CHILD(tree, 0)));
2965
2966 if (!res && !PyErr_Occurred())
2967 err_string("Error Parsing encoding_decl");
2968
2969 return res;
2970}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002971
Fred Drake43f8f9b1998-04-13 16:25:46 +00002972static PyObject*
2973pickle_constructor = NULL;
2974
2975
2976static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002977parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002978{
Fred Drake268397f1998-04-29 14:16:32 +00002979 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002980 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00002981 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002982 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002983
Fred Drakec2683dd2001-07-17 19:32:05 +00002984 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002985 PyObject *newargs;
2986 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002987
Fred Drake2a6875e1999-09-20 22:32:18 +00002988 if ((empty_dict = PyDict_New()) == NULL)
2989 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002990 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002991 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002992 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002993 if (tuple != NULL) {
2994 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2995 Py_DECREF(tuple);
2996 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002997 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002998 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002999 }
3000 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003001 Py_XDECREF(empty_dict);
3002
Fred Drake43f8f9b1998-04-13 16:25:46 +00003003 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003004}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003005
3006
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003007/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003008 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003009 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003010 * We'd really have to write a wrapper around it all anyway to allow
3011 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003012 */
3013static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003014 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003015 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003016 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003017 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003018 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003019 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003020 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003021 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003022 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003023 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003024 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003025 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003026 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003027 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003028 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003029 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003030 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003031 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003032 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003033 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003034 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003035 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003036 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003037 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003038 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003039 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003040 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003041 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003042
Fred Drake43f8f9b1998-04-13 16:25:46 +00003043 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003044 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003045 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003046
Fred Drake268397f1998-04-29 14:16:32 +00003047 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003048 };
3049
3050
Martin v. Löwis1a214512008-06-11 05:26:20 +00003051
3052static struct PyModuleDef parsermodule = {
3053 PyModuleDef_HEAD_INIT,
3054 "parser",
3055 NULL,
3056 -1,
3057 parser_functions,
3058 NULL,
3059 NULL,
3060 NULL,
3061 NULL
3062};
3063
3064PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003065
Mark Hammond62b1ab12002-07-23 06:31:15 +00003066PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003067PyInit_parser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003068{
Fred Drake13130bc2001-08-15 16:44:56 +00003069 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003070
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00003071 if (PyType_Ready(&PyST_Type) < 0)
3072 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00003073 module = PyModule_Create(&parsermodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003074 if (module == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003075 return NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003076
Fred Drake7a15ba51999-09-09 14:21:52 +00003077 if (parser_error == 0)
3078 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003079
Tim Peters6a627252003-07-21 14:25:23 +00003080 if (parser_error == 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003081 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003082 /* CAUTION: The code next used to skip bumping the refcount on
Martin v. Löwis1a214512008-06-11 05:26:20 +00003083 * parser_error. That's a disaster if PyInit_parser() gets called more
Tim Peters6a627252003-07-21 14:25:23 +00003084 * than once. By incref'ing, we ensure that each module dict that
3085 * gets created owns its reference to the shared parser_error object,
3086 * and the file static parser_error vrbl owns a reference too.
3087 */
3088 Py_INCREF(parser_error);
3089 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003090 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003091
Fred Drakec2683dd2001-07-17 19:32:05 +00003092 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003093 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00003094 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003095 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003096
Fred Drake13130bc2001-08-15 16:44:56 +00003097 PyModule_AddStringConstant(module, "__copyright__",
3098 parser_copyright_string);
3099 PyModule_AddStringConstant(module, "__doc__",
3100 parser_doc_string);
3101 PyModule_AddStringConstant(module, "__version__",
3102 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003103
Fred Drake78bdb9b2001-07-19 20:17:15 +00003104 /* Register to support pickling.
3105 * If this fails, the import of this module will fail because an
3106 * exception will be raised here; should we clear the exception?
3107 */
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003108 copyreg = PyImport_ImportModuleNoBlock("copyreg");
Fred Drake13130bc2001-08-15 16:44:56 +00003109 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003110 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003111
Fred Drake13130bc2001-08-15 16:44:56 +00003112 func = PyObject_GetAttrString(copyreg, "pickle");
3113 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3114 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003115 Py_XINCREF(pickle_constructor);
3116 if ((func != NULL) && (pickle_constructor != NULL)
3117 && (pickler != NULL)) {
3118 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003119
Thomas Wouters477c8d52006-05-27 19:21:47 +00003120 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3121 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003122 Py_XDECREF(res);
3123 }
3124 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003125 Py_XDECREF(pickle_constructor);
3126 Py_XDECREF(pickler);
3127 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003128 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00003129 return module;
Fred Drakeff9ea482000-04-19 13:54:15 +00003130}