blob: 652b07ce80b9e42a235a01918c5f9331bc777d95 [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 */
Benjamin Petersonf216c942008-10-31 02:28:05 +000029#include "Python-ast.h" /* mod_ty */
Fred Drakeff9ea482000-04-19 13:54:15 +000030#include "graminit.h" /* symbols defined in the grammar */
31#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000032#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000033#include "token.h" /* token definitions */
Benjamin Petersonf216c942008-10-31 02:28:05 +000034#include "grammar.h"
35#include "parsetok.h"
Fred Drakeff9ea482000-04-19 13:54:15 +000036 /* ISTERMINAL() / ISNONTERMINAL() */
Benjamin Petersonf216c942008-10-31 02:28:05 +000037#include "compile.h"
38#undef Yield
39#include "ast.h"
40#include "pyarena.h"
41
42extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000043
Fred Drake268397f1998-04-29 14:16:32 +000044#ifdef lint
45#include <note.h>
46#else
47#define NOTE(x)
48#endif
49
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000050/* String constants used to initialize module attributes.
51 *
52 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000053static char parser_copyright_string[] =
54"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000055University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
56Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
57Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000058
59
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000060PyDoc_STRVAR(parser_doc_string,
61"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000062
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000063static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000064
65
Martin v. Löwis18e16552006-02-15 17:27:45 +000066typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000067typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000068 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000069 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000070
Thomas Wouters7e474022000-07-16 12:04:32 +000071/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000072 * original copyright statement is included below, and continues to apply
73 * in full to the function immediately following. All other material is
74 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
75 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000076 * new naming conventions. Added arguments to provide support for creating
77 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000078 */
79
Guido van Rossum52f2c051993-11-10 12:53:24 +000080
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000081static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000082node2tuple(node *n, /* node to convert */
83 SeqMaker mkseq, /* create sequence */
84 SeqInserter addelem, /* func. to add elem. in seq. */
Thomas Wouters89f507f2006-12-13 04:49:30 +000085 int lineno, /* include line numbers? */
86 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000087{
Guido van Rossum3d602e31996-07-21 02:33:56 +000088 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000089 Py_INCREF(Py_None);
90 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000091 }
92 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000093 int i;
94 PyObject *v;
95 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000096
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000097 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000098 if (v == NULL)
99 return (v);
Christian Heimes217cfd12007-12-02 14:31:20 +0000100 w = PyLong_FromLong(TYPE(n));
Fred Drakeff9ea482000-04-19 13:54:15 +0000101 if (w == NULL) {
102 Py_DECREF(v);
103 return ((PyObject*) NULL);
104 }
105 (void) addelem(v, 0, w);
106 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000107 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000108 if (w == NULL) {
109 Py_DECREF(v);
110 return ((PyObject*) NULL);
111 }
112 (void) addelem(v, i+1, w);
113 }
Tim Peters6a627252003-07-21 14:25:23 +0000114
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000115 if (TYPE(n) == encoding_decl)
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000116 (void) addelem(v, i+1, PyUnicode_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000117 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000118 }
119 else if (ISTERMINAL(TYPE(n))) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000120 PyObject *result = mkseq(2 + lineno + col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000121 if (result != NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000122 (void) addelem(result, 0, PyLong_FromLong(TYPE(n)));
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000123 (void) addelem(result, 1, PyUnicode_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000124 if (lineno == 1)
Christian Heimes217cfd12007-12-02 14:31:20 +0000125 (void) addelem(result, 2, PyLong_FromLong(n->n_lineno));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000126 if (col_offset == 1)
Christian Heimes217cfd12007-12-02 14:31:20 +0000127 (void) addelem(result, 3, PyLong_FromLong(n->n_col_offset));
Fred Drakeff9ea482000-04-19 13:54:15 +0000128 }
129 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000130 }
131 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000132 PyErr_SetString(PyExc_SystemError,
133 "unrecognized parse tree node type");
134 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000135 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000136}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000137/*
138 * End of material copyrighted by Stichting Mathematisch Centrum.
139 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000140
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000141
142
143/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000144 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000145 * field of the object type to identify which any given object represents.
146 * These should probably go in an external header to allow other extensions
147 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000148 */
149
Fred Drakec2683dd2001-07-17 19:32:05 +0000150#define PyST_EXPR 1
151#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000152
153
154/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000155 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000156 * naming style, but the code uses the new naming convention.
157 */
158
159static PyObject*
160parser_error = 0;
161
162
Fred Drakec2683dd2001-07-17 19:32:05 +0000163typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000164 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000165 node* st_node; /* the node* returned by the parser */
166 int st_type; /* EXPR or SUITE ? */
Benjamin Petersonf216c942008-10-31 02:28:05 +0000167 PyCompilerFlags st_flags; /* Parser and compiler flags */
Fred Drakec2683dd2001-07-17 19:32:05 +0000168} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000169
170
Jeremy Hylton938ace62002-07-17 16:30:39 +0000171static void parser_free(PyST_Object *st);
172static int parser_compare(PyST_Object *left, PyST_Object *right);
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000173static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
174static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
175static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
176static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
177static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
Fred Drake503d8d61998-04-13 18:45:18 +0000178
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000179#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
180
181static PyMethodDef parser_methods[] = {
182 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
183 PyDoc_STR("Compile this ST object into a code object.")},
184 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
185 PyDoc_STR("Determines if this ST object was created from an expression.")},
186 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
187 PyDoc_STR("Determines if this ST object was created from a suite.")},
188 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
189 PyDoc_STR("Creates a list-tree representation of this ST.")},
190 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
191 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
192
193 {NULL, NULL, 0, NULL}
194};
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000195
Fred Drake268397f1998-04-29 14:16:32 +0000196static
Fred Drakec2683dd2001-07-17 19:32:05 +0000197PyTypeObject PyST_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000198 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000199 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000200 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000201 0, /* tp_itemsize */
202 (destructor)parser_free, /* tp_dealloc */
203 0, /* tp_print */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000204 0, /* tp_getattr */
Fred Drakeff9ea482000-04-19 13:54:15 +0000205 0, /* tp_setattr */
206 (cmpfunc)parser_compare, /* tp_compare */
207 0, /* tp_repr */
208 0, /* tp_as_number */
209 0, /* tp_as_sequence */
210 0, /* tp_as_mapping */
211 0, /* tp_hash */
212 0, /* tp_call */
213 0, /* tp_str */
214 0, /* tp_getattro */
215 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000216
217 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000218 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000219
Fred Drakeff9ea482000-04-19 13:54:15 +0000220 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000221
222 /* __doc__ */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000223 "Intermediate representation of a Python parse tree.",
224 0, /* tp_traverse */
225 0, /* tp_clear */
226 0, /* tp_richcompare */
227 0, /* tp_weaklistoffset */
228 0, /* tp_iter */
229 0, /* tp_iternext */
230 parser_methods, /* tp_methods */
Fred Drakec2683dd2001-07-17 19:32:05 +0000231}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000232
233
234static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000235parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000236{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000237 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000238
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000239 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000240 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000241
242 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000243 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000244
245 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000246 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247
248 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000249 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000250
251 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000252 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000253
254 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000255 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000256
Fred Drakeff9ea482000-04-19 13:54:15 +0000257 if (v != 0)
258 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000259 }
260 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000261}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000262
263
Fred Drakec2683dd2001-07-17 19:32:05 +0000264/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265 *
266 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
267 * This really just wraps a call to parser_compare_nodes() with some easy
268 * checks and protection code.
269 *
270 */
271static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000272parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000273{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000274 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000275 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000276
277 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000278 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000279
Fred Drakec2683dd2001-07-17 19:32:05 +0000280 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000281}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000282
283
Fred Drakec2683dd2001-07-17 19:32:05 +0000284/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000285 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000286 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000287 * 'wrapper' object that holds a node* and allows it to be passed around in
288 * Python code.
289 *
290 */
291static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000292parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000293{
Fred Drakec2683dd2001-07-17 19:32:05 +0000294 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000295
296 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000297 o->st_node = st;
298 o->st_type = type;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000299 o->st_flags.cf_flags = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000300 }
Fred Drake268397f1998-04-29 14:16:32 +0000301 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000302 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000303 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000304 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000305}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000306
307
Fred Drakec2683dd2001-07-17 19:32:05 +0000308/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000309 *
310 * This is called by a del statement that reduces the reference count to 0.
311 *
312 */
313static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000314parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000315{
Fred Drakec2683dd2001-07-17 19:32:05 +0000316 PyNode_Free(st->st_node);
317 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000318}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000319
320
Fred Drakec2683dd2001-07-17 19:32:05 +0000321/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000322 *
323 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000324 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000325 *
326 */
327static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000328parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000329{
Guido van Rossum47478871996-08-21 14:32:37 +0000330 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000331 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000332 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000333 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000334
Georg Brandl30704ea02008-07-23 15:07:12 +0000335 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000336
Martin v. Löwis1a214512008-06-11 05:26:20 +0000337 if (self == NULL || PyModule_Check(self)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000338 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
339 &PyST_Type, &self, &line_option,
340 &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000341 }
Fred Drake503d8d61998-04-13 18:45:18 +0000342 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000343 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
344 &line_option, &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000345 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000346 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000347 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000348 if (line_option != NULL) {
349 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
350 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000351 if (col_option != NULL) {
352 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
353 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000354 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000355 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000356 * since it's known to work already.
357 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000358 res = node2tuple(((PyST_Object*)self)->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000359 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000360 }
361 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000362}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000363
364
Fred Drakec2683dd2001-07-17 19:32:05 +0000365/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000366 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000367 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000368 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000369 *
370 */
371static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000372parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000373{
Guido van Rossum47478871996-08-21 14:32:37 +0000374 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000375 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000376 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000377 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000378
Georg Brandl30704ea02008-07-23 15:07:12 +0000379 static char *keywords[] = {"st", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000380
Martin v. Löwis1a214512008-06-11 05:26:20 +0000381 if (self == NULL || PyModule_Check(self))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000382 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
383 &PyST_Type, &self, &line_option,
384 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000385 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000386 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
387 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000388 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000389 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000390 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000391 if (line_option != 0) {
392 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
393 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000394 if (col_option != NULL) {
395 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
396 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000397 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000398 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000399 * since it's known to work already.
400 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000401 res = node2tuple(self->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000402 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000403 }
404 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000405}
Guido van Rossum47478871996-08-21 14:32:37 +0000406
407
Fred Drakec2683dd2001-07-17 19:32:05 +0000408/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000409 *
410 * This function creates code objects from the parse tree represented by
411 * the passed-in data object. An optional file name is passed in as well.
412 *
413 */
414static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000415parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000416{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000417 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000418 PyArena* arena;
419 mod_ty mod;
Fred Drakec2683dd2001-07-17 19:32:05 +0000420 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000421 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000422
Georg Brandl30704ea02008-07-23 15:07:12 +0000423 static char *keywords[] = {"st", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000424
Martin v. Löwis1a214512008-06-11 05:26:20 +0000425 if (self == NULL || PyModule_Check(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000426 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
427 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000428 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000429 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000430 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000431
Benjamin Petersonf216c942008-10-31 02:28:05 +0000432 if (ok) {
433 arena = PyArena_New();
434 if (arena) {
435 mod = PyAST_FromNode(self->st_node, &(self->st_flags), str, arena);
436 if (mod) {
437 res = (PyObject *)PyAST_Compile(mod, str, &(self->st_flags), arena);
438 }
439 PyArena_Free(arena);
440 }
441 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000442
443 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000444}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000445
446
447/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
448 * PyObject* parser_issuite(PyObject* self, PyObject* args)
449 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000450 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000451 * a statement suite, respectively. The return is a Python truth value.
452 *
453 */
454static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000455parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000456{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000457 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000458 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000459
Georg Brandl30704ea02008-07-23 15:07:12 +0000460 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000461
Martin v. Löwis1a214512008-06-11 05:26:20 +0000462 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000463 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000464 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000465 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000466 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000467
468 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000469 /* Check to see if the ST represents an expression or not. */
470 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000471 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000472 }
473 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000474}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000475
476
477static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000478parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000479{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000480 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000481 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000482
Georg Brandl30704ea02008-07-23 15:07:12 +0000483 static char *keywords[] = {"st", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000484
Martin v. Löwis1a214512008-06-11 05:26:20 +0000485 if (self == NULL || PyModule_Check(self))
Fred Drakeff9ea482000-04-19 13:54:15 +0000486 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000487 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000488 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000489 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000490
491 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000492 /* Check to see if the ST represents an expression or not. */
493 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000494 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000495 }
496 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000497}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000498
499
Guido van Rossum3d602e31996-07-21 02:33:56 +0000500/* err_string(char* message)
501 *
502 * Sets the error string for an exception of type ParserError.
503 *
504 */
505static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000506err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000507{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000508 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000509}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000510
511
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000512/* PyObject* parser_do_parse(PyObject* args, int type)
513 *
514 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000515 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000516 *
517 */
518static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000519parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000520{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000521 char* string = 0;
522 PyObject* res = 0;
Benjamin Petersonf216c942008-10-31 02:28:05 +0000523 int flags = 0;
524 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000525
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000526 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000527
528 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersonf216c942008-10-31 02:28:05 +0000529 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
530 &_PyParser_Grammar,
531 (type == PyST_EXPR)
532 ? eval_input : file_input,
533 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000534
Benjamin Petersonf216c942008-10-31 02:28:05 +0000535 if (n) {
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000536 res = parser_newstobject(n, type);
Benjamin Petersonf216c942008-10-31 02:28:05 +0000537 if (res)
538 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
539 }
540 else
541 PyParser_SetError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000542 }
543 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000544}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000545
546
547/* PyObject* parser_expr(PyObject* self, PyObject* args)
548 * PyObject* parser_suite(PyObject* self, PyObject* args)
549 *
550 * External interfaces to the parser itself. Which is called determines if
551 * the parser attempts to recognize an expression ('eval' form) or statement
552 * suite ('exec' form). The real work is done by parser_do_parse() above.
553 *
554 */
555static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000556parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000557{
Fred Drake268397f1998-04-29 14:16:32 +0000558 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000559 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000560}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000561
562
563static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000564parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000565{
Fred Drake268397f1998-04-29 14:16:32 +0000566 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000567 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000568}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000569
570
571
Fred Drakec2683dd2001-07-17 19:32:05 +0000572/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000573 * object requires that the input tuple be valid without having to rely on
574 * catching an exception from the compiler. This is done to allow the
575 * compiler itself to remain fast, since most of its input will come from
576 * the parser directly, and therefore be known to be syntactically correct.
577 * This validation is done to ensure that we don't core dump the compile
578 * phase, returning an exception instead.
579 *
580 * Two aspects can be broken out in this code: creating a node tree from
581 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000582 * advantageous to expand the number of ST types to include funcdefs and
583 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000584 * here. They are not necessary, and not quite as useful in a raw form.
585 * For now, let's get expressions and suites working reliably.
586 */
587
588
Jeremy Hylton938ace62002-07-17 16:30:39 +0000589static node* build_node_tree(PyObject *tuple);
590static int validate_expr_tree(node *tree);
591static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000592static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000593
Fred Drakec2683dd2001-07-17 19:32:05 +0000594/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000595 *
596 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000597 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000598 * tuple can be validated. It does this by checking the first code of the
599 * tuple, and, if acceptable, builds the internal representation. If this
600 * step succeeds, the internal representation is validated as fully as
601 * possible with the various validate_*() routines defined below.
602 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000603 * This function must be changed if support is to be added for PyST_FRAGMENT
604 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000605 *
606 */
607static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000608parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000609{
Fred Drake268397f1998-04-29 14:16:32 +0000610 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000611 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000612 PyObject *tuple;
613 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000614
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000615 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000616
Fred Drakec2683dd2001-07-17 19:32:05 +0000617 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000618 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000619 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000620 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000621 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000622 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000623 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000624 }
625 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000626 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000627 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000628 tree = build_node_tree(tuple);
629 if (tree != 0) {
630 int start_sym = TYPE(tree);
631 if (start_sym == eval_input) {
632 /* Might be an eval form. */
633 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000634 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000635 else
636 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000637 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000638 else if (start_sym == file_input) {
639 /* This looks like an exec form so far. */
640 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000641 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000642 else
643 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000644 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000645 else if (start_sym == encoding_decl) {
646 /* This looks like an encoding_decl so far. */
647 if (validate_encoding_decl(tree))
648 st = parser_newstobject(tree, PyST_SUITE);
649 else
650 PyNode_Free(tree);
651 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000652 else {
653 /* This is a fragment, at best. */
654 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000655 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000656 }
Guido van Rossum47478871996-08-21 14:32:37 +0000657 }
Guido van Rossum47478871996-08-21 14:32:37 +0000658 /* Make sure we throw an exception on all errors. We should never
659 * get this, but we'd do well to be sure something is done.
660 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000661 if (st == NULL && !PyErr_Occurred())
662 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000663
Fred Drakec2683dd2001-07-17 19:32:05 +0000664 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000665}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000666
667
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000668/* node* build_node_children()
669 *
670 * Iterate across the children of the current non-terminal node and build
671 * their structures. If successful, return the root of this portion of
672 * the tree, otherwise, 0. Any required exception will be specified already,
673 * and no memory will have been deallocated.
674 *
675 */
676static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000677build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000678{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000679 Py_ssize_t len = PyObject_Size(tuple);
680 Py_ssize_t i;
681 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000682
683 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000684 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000685 PyObject* elem = PySequence_GetItem(tuple, i);
686 int ok = elem != NULL;
687 long type = 0;
688 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000689
Fred Drakeff9ea482000-04-19 13:54:15 +0000690 if (ok)
691 ok = PySequence_Check(elem);
692 if (ok) {
693 PyObject *temp = PySequence_GetItem(elem, 0);
694 if (temp == NULL)
695 ok = 0;
696 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000697 ok = PyLong_Check(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000698 if (ok)
Christian Heimes217cfd12007-12-02 14:31:20 +0000699 type = PyLong_AS_LONG(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000700 Py_DECREF(temp);
701 }
702 }
703 if (!ok) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000704 PyObject *err = Py_BuildValue("os", elem,
705 "Illegal node construct.");
706 PyErr_SetObject(parser_error, err);
707 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000708 Py_XDECREF(elem);
709 return (0);
710 }
711 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000712 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000713 PyObject *temp;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000714 const char *temp_str;
Guido van Rossum47478871996-08-21 14:32:37 +0000715
Fred Drake0ac9b072000-09-12 21:58:06 +0000716 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000717 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000718 return 0;
719 }
720 temp = PySequence_GetItem(elem, 1);
721 if (temp == NULL)
722 return 0;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000723 if (!PyUnicode_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000724 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000725 "second item in terminal node must be a string,"
726 " found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000727 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000728 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000729 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000730 return 0;
731 }
732 if (len == 3) {
733 PyObject *o = PySequence_GetItem(elem, 2);
734 if (o != NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000735 if (PyLong_Check(o))
736 *line_num = PyLong_AS_LONG(o);
Fred Drake0ac9b072000-09-12 21:58:06 +0000737 else {
738 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000739 "third item in terminal node must be an"
740 " integer, found %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000741 Py_TYPE(temp)->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000742 Py_DECREF(o);
743 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000744 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000745 return 0;
746 }
747 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000748 }
749 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000750 temp_str = _PyUnicode_AsStringAndSize(temp, &len);
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000751 strn = (char *)PyObject_MALLOC(len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000752 if (strn != NULL)
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000753 (void) memcpy(strn, temp_str, len + 1);
Fred Drake0ac9b072000-09-12 21:58:06 +0000754 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000755 }
756 else if (!ISNONTERMINAL(type)) {
757 /*
758 * It has to be one or the other; this is an error.
759 * Throw an exception.
760 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000761 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
762 PyErr_SetObject(parser_error, err);
763 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000764 Py_XDECREF(elem);
765 return (0);
766 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000767 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000768 if (err == E_NOMEM) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000769 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000770 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000771 return (node *) PyErr_NoMemory();
772 }
773 if (err == E_OVERFLOW) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000774 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000775 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000776 PyErr_SetString(PyExc_ValueError,
777 "unsupported number of child nodes");
778 return NULL;
779 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000780
Fred Drakeff9ea482000-04-19 13:54:15 +0000781 if (ISNONTERMINAL(type)) {
782 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000783
Fred Drakeff9ea482000-04-19 13:54:15 +0000784 if (new_child != build_node_children(elem, new_child, line_num)) {
785 Py_XDECREF(elem);
786 return (0);
787 }
788 }
789 else if (type == NEWLINE) { /* It's true: we increment the */
790 ++(*line_num); /* line number *after* the newline! */
791 }
792 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000793 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000794 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000795}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000796
797
798static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000799build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000800{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000801 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000802 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000803 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000804
Guido van Rossum47478871996-08-21 14:32:37 +0000805 if (temp != NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000806 num = PyLong_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000807 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000808 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000809 /*
810 * The tuple is simple, but it doesn't start with a start symbol.
811 * Throw an exception now and be done with it.
812 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000813 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000814 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000815 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000816 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000817 }
818 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000819 /*
820 * Not efficient, but that can be handled later.
821 */
822 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000823 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000824
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000825 if (num == encoding_decl) {
826 encoding = PySequence_GetItem(tuple, 2);
827 /* tuple isn't borrowed anymore here, need to DECREF */
828 tuple = PySequence_GetSlice(tuple, 0, 2);
829 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000830 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000831 if (res != NULL) {
832 if (res != build_node_children(tuple, res, &line_num)) {
833 PyNode_Free(res);
834 res = NULL;
835 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000836 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000837 Py_ssize_t len;
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000838 const char *temp;
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000839 temp = _PyUnicode_AsStringAndSize(encoding, &len);
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000840 res->n_str = (char *)PyObject_MALLOC(len + 1);
Neal Norwitz3fcbea52007-08-26 04:51:28 +0000841 if (res->n_str != NULL && temp != NULL)
Alexandre Vassalottia85998a2008-05-03 18:24:43 +0000842 (void) memcpy(res->n_str, temp, len + 1);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000843 Py_DECREF(encoding);
844 Py_DECREF(tuple);
845 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000846 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000847 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000848 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000849 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000850 * NONTERMINAL, we can't use it. Not sure the implementation
851 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000852 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000853 PyObject *err = Py_BuildValue("os", tuple,
854 "Illegal component tuple.");
855 PyErr_SetObject(parser_error, err);
856 Py_XDECREF(err);
857 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000858
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000859 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000860}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000861
862
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000863/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000864 * Validation routines used within the validation section:
865 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000866static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000867
Fred Drakeff9ea482000-04-19 13:54:15 +0000868#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
869#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
870#define validate_colon(ch) validate_terminal(ch, COLON, ":")
871#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
872#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
873#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
874#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
875#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
876#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
877#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
878#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
879#define validate_star(ch) validate_terminal(ch, STAR, "*")
880#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
881#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
882#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000883#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000884#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000885
Fred Drake0ac9b072000-09-12 21:58:06 +0000886#define VALIDATER(n) static int validate_##n(node *tree)
887
Fred Drakeff9ea482000-04-19 13:54:15 +0000888VALIDATER(node); VALIDATER(small_stmt);
889VALIDATER(class); VALIDATER(node);
890VALIDATER(parameters); VALIDATER(suite);
891VALIDATER(testlist); VALIDATER(varargslist);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000892VALIDATER(vfpdef);
Fred Drakeff9ea482000-04-19 13:54:15 +0000893VALIDATER(stmt); VALIDATER(simple_stmt);
894VALIDATER(expr_stmt); VALIDATER(power);
Guido van Rossum452bf512007-02-09 05:32:43 +0000895VALIDATER(del_stmt);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000896VALIDATER(return_stmt); VALIDATER(raise_stmt);
897VALIDATER(import_stmt); VALIDATER(import_stmt);
898VALIDATER(import_name); VALIDATER(yield_stmt);
899VALIDATER(global_stmt); VALIDATER(assert_stmt);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000900VALIDATER(compound_stmt);
Fred Drakeff9ea482000-04-19 13:54:15 +0000901VALIDATER(while); VALIDATER(for);
902VALIDATER(try); VALIDATER(except_clause);
903VALIDATER(test); VALIDATER(and_test);
904VALIDATER(not_test); VALIDATER(comparison);
Guido van Rossumfc158e22007-11-15 19:17:28 +0000905VALIDATER(comp_op);
Guido van Rossum0368b722007-05-11 16:50:42 +0000906VALIDATER(star_expr); VALIDATER(expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000907VALIDATER(xor_expr); VALIDATER(and_expr);
908VALIDATER(shift_expr); VALIDATER(arith_expr);
909VALIDATER(term); VALIDATER(factor);
910VALIDATER(atom); VALIDATER(lambdef);
911VALIDATER(trailer); VALIDATER(subscript);
912VALIDATER(subscriptlist); VALIDATER(sliceop);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000913VALIDATER(exprlist); VALIDATER(dictorsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000914VALIDATER(arglist); VALIDATER(argument);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000915VALIDATER(testlist1); VALIDATER(comp_for);
916VALIDATER(comp_iter); VALIDATER(comp_if);
917VALIDATER(testlist_comp); VALIDATER(yield_expr);
918VALIDATER(yield_or_testlist); VALIDATER(or_test);
919VALIDATER(test_nocond); VALIDATER(lambdef_nocond);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000920
Fred Drake0ac9b072000-09-12 21:58:06 +0000921#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000922
Fred Drakeff9ea482000-04-19 13:54:15 +0000923#define is_even(n) (((n) & 1) == 0)
924#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000925
926
927static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000928validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000929{
Fred Drake0ac9b072000-09-12 21:58:06 +0000930 if (TYPE(n) != t) {
931 PyErr_Format(parser_error, "Expected node type %d, got %d.",
932 t, TYPE(n));
933 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000934 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000935 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000936}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000937
938
Fred Drakee7ab64e2000-04-25 04:14:46 +0000939/* Verifies that the number of child nodes is exactly 'num', raising
940 * an exception if it isn't. The exception message does not indicate
941 * the exact number of nodes, allowing this to be used to raise the
942 * "right" exception when the wrong number of nodes is present in a
943 * specific variant of a statement's syntax. This is commonly used
944 * in that fashion.
945 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000946static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000947validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000948{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000949 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000950 PyErr_Format(parser_error,
951 "Illegal number of children for %s node.", name);
952 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000953 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000954 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000955}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000956
957
958static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000959validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000960{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000961 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000962 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000963
964 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000965 PyErr_Format(parser_error,
966 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000967 }
968 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000969}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000970
971
Guido van Rossum47478871996-08-21 14:32:37 +0000972/* X (',' X) [',']
973 */
974static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000975validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000976 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000977{
978 int nch = NCH(tree);
979 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000980 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000981
982 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000983 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000984 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000985 if (is_even(nch))
986 res = validate_comma(CHILD(tree, --nch));
987 if (res && nch > 1) {
988 int pos = 1;
989 for ( ; res && pos < nch; pos += 2)
990 res = (validate_comma(CHILD(tree, pos))
991 && vfunc(CHILD(tree, pos + 1)));
992 }
Guido van Rossum47478871996-08-21 14:32:37 +0000993 }
994 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000995}
Guido van Rossum47478871996-08-21 14:32:37 +0000996
997
Fred Drakecff283c2000-08-21 22:24:43 +0000998/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000999 *
1000 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001001 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00001002 */
Guido van Rossum47478871996-08-21 14:32:37 +00001003static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001004validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001005{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001006 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +00001007 int res = (validate_ntype(tree, classdef) &&
1008 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001009
Guido van Rossum3d602e31996-07-21 02:33:56 +00001010 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001011 res = (validate_name(CHILD(tree, 0), "class")
1012 && validate_ntype(CHILD(tree, 1), NAME)
1013 && validate_colon(CHILD(tree, nch - 2))
1014 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001015 }
Brett Cannonf4189912005-04-09 02:30:16 +00001016 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001017 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +00001018 }
Guido van Rossumfc158e22007-11-15 19:17:28 +00001019
Brett Cannonf4189912005-04-09 02:30:16 +00001020 if (res) {
1021 if (nch == 7) {
1022 res = ((validate_lparen(CHILD(tree, 2)) &&
Guido van Rossumfc158e22007-11-15 19:17:28 +00001023 validate_arglist(CHILD(tree, 3)) &&
Brett Cannonf4189912005-04-09 02:30:16 +00001024 validate_rparen(CHILD(tree, 4))));
1025 }
1026 else if (nch == 6) {
1027 res = (validate_lparen(CHILD(tree,2)) &&
1028 validate_rparen(CHILD(tree,3)));
1029 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001030 }
1031 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001032}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001033
1034
Guido van Rossum3d602e31996-07-21 02:33:56 +00001035/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001036 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001037 */
Guido van Rossum47478871996-08-21 14:32:37 +00001038static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001039validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001040{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001041 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001042 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001043 && (nch >= 4)
1044 && validate_name(CHILD(tree, 0), "if")
1045 && validate_test(CHILD(tree, 1))
1046 && validate_colon(CHILD(tree, 2))
1047 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001048
1049 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001050 /* ... 'else' ':' suite */
1051 res = (validate_name(CHILD(tree, nch - 3), "else")
1052 && validate_colon(CHILD(tree, nch - 2))
1053 && validate_suite(CHILD(tree, nch - 1)));
1054 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001055 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001056 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001057 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001058 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001059 /* Will catch the case for nch < 4 */
1060 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001061 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001062 /* ... ('elif' test ':' suite)+ ... */
1063 int j = 4;
1064 while ((j < nch) && res) {
1065 res = (validate_name(CHILD(tree, j), "elif")
1066 && validate_colon(CHILD(tree, j + 2))
1067 && validate_test(CHILD(tree, j + 1))
1068 && validate_suite(CHILD(tree, j + 3)));
1069 j += 4;
1070 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001071 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001072 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001073}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001074
1075
Guido van Rossum3d602e31996-07-21 02:33:56 +00001076/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001077 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001078 *
1079 */
Guido van Rossum47478871996-08-21 14:32:37 +00001080static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001081validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001082{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001083 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001084 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001085
Guido van Rossum3d602e31996-07-21 02:33:56 +00001086 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001087 res = (validate_lparen(CHILD(tree, 0))
1088 && validate_rparen(CHILD(tree, nch - 1)));
1089 if (res && (nch == 3))
1090 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001091 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001092 else {
1093 (void) validate_numnodes(tree, 2, "parameters");
1094 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001095 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001096}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001097
1098
Fred Drakecff283c2000-08-21 22:24:43 +00001099/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001100 *
1101 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001102 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001103 * | NEWLINE INDENT stmt+ DEDENT
1104 */
Guido van Rossum47478871996-08-21 14:32:37 +00001105static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001106validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001107{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001108 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001109 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001110
Guido van Rossum3d602e31996-07-21 02:33:56 +00001111 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001112 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001113 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001114 /* NEWLINE INDENT stmt+ DEDENT */
1115 res = (validate_newline(CHILD(tree, 0))
1116 && validate_indent(CHILD(tree, 1))
1117 && validate_stmt(CHILD(tree, 2))
1118 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001119
Fred Drakeff9ea482000-04-19 13:54:15 +00001120 if (res && (nch > 4)) {
1121 int i = 3;
1122 --nch; /* forget the DEDENT */
1123 for ( ; res && (i < nch); ++i)
1124 res = validate_stmt(CHILD(tree, i));
1125 }
1126 else if (nch < 4)
1127 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001128 }
1129 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001130}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001131
1132
Guido van Rossum47478871996-08-21 14:32:37 +00001133static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001134validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001135{
Guido van Rossum47478871996-08-21 14:32:37 +00001136 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001137 validate_test, "testlist"));
1138}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001139
1140
Guido van Rossum1c917072001-10-15 15:44:05 +00001141static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001142validate_testlist1(node *tree)
1143{
1144 return (validate_repeating_list(tree, testlist1,
1145 validate_test, "testlist1"));
1146}
1147
1148
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001149/* validate either vfpdef or tfpdef.
1150 * vfpdef: NAME
1151 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001152 */
1153static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001154validate_vfpdef(node *tree)
Neal Norwitzc1505362006-12-28 06:47:50 +00001155{
1156 int nch = NCH(tree);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001157 if (TYPE(tree) == vfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001158 return nch == 1 && validate_name(CHILD(tree, 0), NULL);
1159 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001160 else if (TYPE(tree) == tfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001161 if (nch == 1) {
1162 return validate_name(CHILD(tree, 0), NULL);
1163 }
1164 else if (nch == 3) {
1165 return validate_name(CHILD(tree, 0), NULL) &&
1166 validate_colon(CHILD(tree, 1)) &&
1167 validate_test(CHILD(tree, 2));
1168 }
1169 }
1170 return 0;
1171}
1172
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001173/* '*' vfpdef (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
1174 * ..or tfpdef in place of vfpdef. vfpdef: NAME; tfpdef: NAME [':' test]
Fred Drakecff283c2000-08-21 22:24:43 +00001175 */
1176static int
1177validate_varargslist_trailer(node *tree, int start)
1178{
1179 int nch = NCH(tree);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001180 int res = 0, i;
Fred Drakecff283c2000-08-21 22:24:43 +00001181 int sym;
1182
1183 if (nch <= start) {
1184 err_string("expected variable argument trailer for varargslist");
1185 return 0;
1186 }
1187 sym = TYPE(CHILD(tree, start));
1188 if (sym == STAR) {
1189 /*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001190 * '*' vfpdef (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
Fred Drakecff283c2000-08-21 22:24:43 +00001191 */
1192 if (nch-start == 2)
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001193 res = validate_vfpdef(CHILD(tree, start+1));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001194 else if (nch-start == 5 && TYPE(CHILD(tree, start+2)) == COMMA)
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001195 res = (validate_vfpdef(CHILD(tree, start+1))
Fred Drakecff283c2000-08-21 22:24:43 +00001196 && validate_comma(CHILD(tree, start+2))
1197 && validate_doublestar(CHILD(tree, start+3))
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001198 && validate_vfpdef(CHILD(tree, start+4)));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001199 else {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001200 /* skip over vfpdef (',' vfpdef ['=' test])* */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001201 i = start + 1;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001202 if (TYPE(CHILD(tree, i)) == vfpdef ||
1203 TYPE(CHILD(tree, i)) == tfpdef) { /* skip over vfpdef or tfpdef */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001204 i += 1;
1205 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001206 while (res && i+1 < nch) { /* validate (',' vfpdef ['=' test])* */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001207 res = validate_comma(CHILD(tree, i));
Guido van Rossumfc158e22007-11-15 19:17:28 +00001208 if (TYPE(CHILD(tree, i+1)) == DOUBLESTAR)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001209 break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001210 res = res && validate_vfpdef(CHILD(tree, i+1));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001211 if (res && i+2 < nch && TYPE(CHILD(tree, i+2)) == EQUAL) {
Guido van Rossumfc158e22007-11-15 19:17:28 +00001212 res = res && (i+3 < nch)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001213 && validate_test(CHILD(tree, i+3));
1214 i += 4;
1215 }
1216 else {
1217 i += 2;
1218 }
1219 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001220 /* [',' '**' vfpdef] */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001221 if (res && i+1 < nch && TYPE(CHILD(tree, i+1)) == DOUBLESTAR) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001222 res = validate_vfpdef(CHILD(tree, i+2));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001223 }
1224 }
Fred Drakecff283c2000-08-21 22:24:43 +00001225 }
1226 else if (sym == DOUBLESTAR) {
1227 /*
1228 * '**' NAME
1229 */
1230 if (nch-start == 2)
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001231 res = validate_vfpdef(CHILD(tree, start+1));
Fred Drakecff283c2000-08-21 22:24:43 +00001232 }
1233 if (!res)
1234 err_string("illegal variable argument trailer for varargslist");
1235 return res;
1236}
1237
1238
Neal Norwitzc1505362006-12-28 06:47:50 +00001239/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001240 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001241 * Validate typedargslist or varargslist.
1242 *
1243 * typedargslist: ((tfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001244 * ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
1245 * '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001246 * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001247 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001248 * varargslist: ((vfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001249 * ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
1250 * '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001251 * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001252 * vfpdef: NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001253 *
1254 */
Guido van Rossum47478871996-08-21 14:32:37 +00001255static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001256validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001257{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001258 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001259 int res = (TYPE(tree) == varargslist ||
1260 TYPE(tree) == typedargslist) &&
1261 (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001262 int sym;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001263 node *ch;
1264 int i = 0;
Guido van Rossumfc158e22007-11-15 19:17:28 +00001265
Fred Drakeb6429a22000-12-11 22:08:27 +00001266 if (!res)
1267 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001268 if (nch < 1) {
1269 err_string("varargslist missing child nodes");
1270 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001271 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001272 while (i < nch) {
Guido van Rossumfc158e22007-11-15 19:17:28 +00001273 ch = CHILD(tree, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001274 sym = TYPE(ch);
1275 if (sym == vfpdef || sym == tfpdef) {
1276 /* validate (vfpdef ['=' test] ',')+ */
1277 res = validate_vfpdef(ch);
Fred Drakecff283c2000-08-21 22:24:43 +00001278 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001279 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1280 res = (validate_equal(CHILD(tree, i))
1281 && validate_test(CHILD(tree, i+1)));
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001282 if (res)
1283 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001284 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001285 if (res && i < nch) {
1286 res = validate_comma(CHILD(tree, i));
1287 ++i;
Fred Drakecff283c2000-08-21 22:24:43 +00001288 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001289 } else if (sym == DOUBLESTAR || sym == STAR) {
1290 res = validate_varargslist_trailer(tree, i);
1291 break;
1292 } else {
1293 res = 0;
1294 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001295 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001296 }
Fred Drakecff283c2000-08-21 22:24:43 +00001297 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001298}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001299
1300
Nick Coghlan650f0d02007-04-15 12:05:43 +00001301/* comp_iter: comp_for | comp_if
Fred Drakecff283c2000-08-21 22:24:43 +00001302 */
1303static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001304validate_comp_iter(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001305{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001306 int res = (validate_ntype(tree, comp_iter)
1307 && validate_numnodes(tree, 1, "comp_iter"));
1308 if (res && TYPE(CHILD(tree, 0)) == comp_for)
1309 res = validate_comp_for(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001310 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001311 res = validate_comp_if(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001312
1313 return res;
1314}
1315
Nick Coghlan650f0d02007-04-15 12:05:43 +00001316/* comp_for: 'for' exprlist 'in' test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001317 */
1318static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001319validate_comp_for(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001320{
1321 int nch = NCH(tree);
1322 int res;
1323
1324 if (nch == 5)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001325 res = validate_comp_iter(CHILD(tree, 4));
Fred Drakecff283c2000-08-21 22:24:43 +00001326 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001327 res = validate_numnodes(tree, 4, "comp_for");
Raymond Hettinger354433a2004-05-19 08:20:33 +00001328
1329 if (res)
1330 res = (validate_name(CHILD(tree, 0), "for")
1331 && validate_exprlist(CHILD(tree, 1))
1332 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001333 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001334
1335 return res;
1336}
1337
Nick Coghlan650f0d02007-04-15 12:05:43 +00001338/* comp_if: 'if' test_nocond [comp_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001339 */
1340static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001341validate_comp_if(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001342{
1343 int nch = NCH(tree);
1344 int res;
1345
1346 if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001347 res = validate_comp_iter(CHILD(tree, 2));
Fred Drakecff283c2000-08-21 22:24:43 +00001348 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001349 res = validate_numnodes(tree, 2, "comp_if");
Fred Drakecff283c2000-08-21 22:24:43 +00001350
1351 if (res)
1352 res = (validate_name(CHILD(tree, 0), "if")
Nick Coghlan650f0d02007-04-15 12:05:43 +00001353 && validate_test_nocond(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001354
1355 return res;
1356}
1357
1358
Guido van Rossum3d602e31996-07-21 02:33:56 +00001359/* simple_stmt | compound_stmt
1360 *
1361 */
Guido van Rossum47478871996-08-21 14:32:37 +00001362static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001363validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001364{
1365 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001366 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001367
Guido van Rossum3d602e31996-07-21 02:33:56 +00001368 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001369 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001370
Fred Drakeff9ea482000-04-19 13:54:15 +00001371 if (TYPE(tree) == simple_stmt)
1372 res = validate_simple_stmt(tree);
1373 else
1374 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001375 }
1376 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001377}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001378
1379
Guido van Rossum3d602e31996-07-21 02:33:56 +00001380/* small_stmt (';' small_stmt)* [';'] NEWLINE
1381 *
1382 */
Guido van Rossum47478871996-08-21 14:32:37 +00001383static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001384validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001385{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001386 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001387 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001388 && (nch >= 2)
1389 && validate_small_stmt(CHILD(tree, 0))
1390 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001391
Guido van Rossum3d602e31996-07-21 02:33:56 +00001392 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001393 res = validate_numnodes(tree, 2, "simple_stmt");
1394 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001395 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001396 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001397 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001398 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001399
Fred Drakeff9ea482000-04-19 13:54:15 +00001400 for (i = 1; res && (i < nch); i += 2)
1401 res = (validate_semi(CHILD(tree, i))
1402 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001403 }
1404 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001405}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001406
1407
Guido van Rossum47478871996-08-21 14:32:37 +00001408static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001409validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001410{
1411 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001412 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001413
Fred Drake0ac9b072000-09-12 21:58:06 +00001414 if (res) {
1415 int ntype = TYPE(CHILD(tree, 0));
1416
1417 if ( (ntype == expr_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001418 || (ntype == del_stmt)
1419 || (ntype == pass_stmt)
1420 || (ntype == flow_stmt)
1421 || (ntype == import_stmt)
1422 || (ntype == global_stmt)
Georg Brandl7cae87c2006-09-06 06:51:57 +00001423 || (ntype == assert_stmt))
Fred Drake0ac9b072000-09-12 21:58:06 +00001424 res = validate_node(CHILD(tree, 0));
1425 else {
1426 res = 0;
1427 err_string("illegal small_stmt child type");
1428 }
1429 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001430 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001431 res = 0;
1432 PyErr_Format(parser_error,
1433 "Unrecognized child node of small_stmt: %d.",
1434 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001435 }
1436 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001437}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001438
1439
1440/* compound_stmt:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001441 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef | decorated
Guido van Rossum3d602e31996-07-21 02:33:56 +00001442 */
Guido van Rossum47478871996-08-21 14:32:37 +00001443static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001444validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001445{
1446 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001447 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001448 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001449
1450 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001451 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001452
1453 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001454 ntype = TYPE(tree);
1455 if ( (ntype == if_stmt)
1456 || (ntype == while_stmt)
1457 || (ntype == for_stmt)
1458 || (ntype == try_stmt)
1459 || (ntype == funcdef)
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001460 || (ntype == classdef)
1461 || (ntype == decorated))
Fred Drakeff9ea482000-04-19 13:54:15 +00001462 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001463 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001464 res = 0;
1465 PyErr_Format(parser_error,
1466 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001467 }
1468 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001469}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001470
Guido van Rossum47478871996-08-21 14:32:37 +00001471static int
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001472validate_yield_or_testlist(node *tree)
1473{
Guido van Rossumfc158e22007-11-15 19:17:28 +00001474 if (TYPE(tree) == yield_expr)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001475 return validate_yield_expr(tree);
1476 else
1477 return validate_testlist(tree);
1478}
1479
1480static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001481validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001482{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001483 int j;
1484 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001485 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001486 && is_odd(nch)
1487 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001488
Fred Drake28f739a2000-08-25 22:42:40 +00001489 if (res && nch == 3
1490 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001491 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1492 && validate_yield_or_testlist(CHILD(tree, 2));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001493
Fred Drake28f739a2000-08-25 22:42:40 +00001494 if (res) {
1495 char *s = STR(CHILD(CHILD(tree, 1), 0));
1496
1497 res = (strcmp(s, "+=") == 0
1498 || strcmp(s, "-=") == 0
1499 || strcmp(s, "*=") == 0
1500 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001501 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001502 || strcmp(s, "%=") == 0
1503 || strcmp(s, "&=") == 0
1504 || strcmp(s, "|=") == 0
1505 || strcmp(s, "^=") == 0
1506 || strcmp(s, "<<=") == 0
1507 || strcmp(s, ">>=") == 0
1508 || strcmp(s, "**=") == 0);
1509 if (!res)
1510 err_string("illegal augmmented assignment operator");
1511 }
1512 }
1513 else {
1514 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001515 res = validate_equal(CHILD(tree, j))
1516 && validate_yield_or_testlist(CHILD(tree, j + 1));
Fred Drake28f739a2000-08-25 22:42:40 +00001517 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001518 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001519}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001520
1521
Guido van Rossum47478871996-08-21 14:32:37 +00001522static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001523validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001524{
1525 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001526 && validate_name(CHILD(tree, 0), "del")
1527 && validate_exprlist(CHILD(tree, 1)));
1528}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001529
1530
Guido van Rossum47478871996-08-21 14:32:37 +00001531static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001532validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001533{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001534 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001535 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001536 && ((nch == 1) || (nch == 2))
1537 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001538
Guido van Rossum3d602e31996-07-21 02:33:56 +00001539 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001540 res = validate_testlist(CHILD(tree, 1));
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
Guido van Rossum47478871996-08-21 14:32:37 +00001546static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001547validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001548{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001549 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001550 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001551 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001552
Guido van Rossum3d602e31996-07-21 02:33:56 +00001553 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001554 res = validate_name(CHILD(tree, 0), "raise");
1555 if (res && (nch >= 2))
1556 res = validate_test(CHILD(tree, 1));
1557 if (res && nch > 2) {
1558 res = (validate_comma(CHILD(tree, 2))
1559 && validate_test(CHILD(tree, 3)));
1560 if (res && (nch > 4))
1561 res = (validate_comma(CHILD(tree, 4))
1562 && validate_test(CHILD(tree, 5)));
1563 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001564 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001565 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001566 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001567 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001568 res = (validate_comma(CHILD(tree, 2))
1569 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001570
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001571 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001572}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001573
1574
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001575/* yield_expr: 'yield' [testlist]
1576 */
1577static int
1578validate_yield_expr(node *tree)
1579{
1580 int nch = NCH(tree);
1581 int res = (validate_ntype(tree, yield_expr)
1582 && ((nch == 1) || (nch == 2))
1583 && validate_name(CHILD(tree, 0), "yield"));
1584
1585 if (res && (nch == 2))
1586 res = validate_testlist(CHILD(tree, 1));
1587
1588 return (res);
1589}
1590
1591
1592/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001593 */
1594static int
1595validate_yield_stmt(node *tree)
1596{
1597 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001598 && validate_numnodes(tree, 1, "yield_stmt")
1599 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001600}
1601
1602
Fred Drakecff283c2000-08-21 22:24:43 +00001603static int
1604validate_import_as_name(node *tree)
1605{
1606 int nch = NCH(tree);
1607 int ok = validate_ntype(tree, import_as_name);
1608
1609 if (ok) {
1610 if (nch == 1)
1611 ok = validate_name(CHILD(tree, 0), NULL);
1612 else if (nch == 3)
1613 ok = (validate_name(CHILD(tree, 0), NULL)
1614 && validate_name(CHILD(tree, 1), "as")
1615 && validate_name(CHILD(tree, 2), NULL));
1616 else
1617 ok = validate_numnodes(tree, 3, "import_as_name");
1618 }
1619 return ok;
1620}
1621
1622
Fred Drake71137082001-01-07 05:59:59 +00001623/* dotted_name: NAME ("." NAME)*
1624 */
1625static int
1626validate_dotted_name(node *tree)
1627{
1628 int nch = NCH(tree);
1629 int res = (validate_ntype(tree, dotted_name)
1630 && is_odd(nch)
1631 && validate_name(CHILD(tree, 0), NULL));
1632 int i;
1633
1634 for (i = 1; res && (i < nch); i += 2) {
1635 res = (validate_dot(CHILD(tree, i))
1636 && validate_name(CHILD(tree, i+1), NULL));
1637 }
1638 return res;
1639}
1640
1641
Fred Drakecff283c2000-08-21 22:24:43 +00001642/* dotted_as_name: dotted_name [NAME NAME]
1643 */
1644static int
1645validate_dotted_as_name(node *tree)
1646{
1647 int nch = NCH(tree);
1648 int res = validate_ntype(tree, dotted_as_name);
1649
1650 if (res) {
1651 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001652 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001653 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001654 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001655 && validate_name(CHILD(tree, 1), "as")
1656 && validate_name(CHILD(tree, 2), NULL));
1657 else {
1658 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001659 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001660 }
1661 }
1662 return res;
1663}
1664
1665
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001666/* dotted_as_name (',' dotted_as_name)* */
1667static int
1668validate_dotted_as_names(node *tree)
1669{
1670 int nch = NCH(tree);
1671 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1672 int i;
1673
1674 for (i = 1; res && (i < nch); i += 2)
1675 res = (validate_comma(CHILD(tree, i))
1676 && validate_dotted_as_name(CHILD(tree, i + 1)));
1677 return (res);
1678}
1679
1680
1681/* import_as_name (',' import_as_name)* [','] */
1682static int
1683validate_import_as_names(node *tree)
1684{
1685 int nch = NCH(tree);
1686 int res = validate_import_as_name(CHILD(tree, 0));
1687 int i;
1688
1689 for (i = 1; res && (i + 1 < nch); i += 2)
1690 res = (validate_comma(CHILD(tree, i))
1691 && validate_import_as_name(CHILD(tree, i + 1)));
1692 return (res);
1693}
1694
1695
1696/* 'import' dotted_as_names */
1697static int
1698validate_import_name(node *tree)
1699{
1700 return (validate_ntype(tree, import_name)
1701 && validate_numnodes(tree, 2, "import_name")
1702 && validate_name(CHILD(tree, 0), "import")
1703 && validate_dotted_as_names(CHILD(tree, 1)));
1704}
1705
Guido van Rossumfc158e22007-11-15 19:17:28 +00001706/* Helper function to count the number of leading dots in
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001707 * 'from ...module import name'
1708 */
1709static int
1710count_from_dots(node *tree)
1711{
1712 int i;
1713 for (i = 0; i < NCH(tree); i++)
1714 if (TYPE(CHILD(tree, i)) != DOT)
1715 break;
1716 return i;
1717}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001718
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001719/* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001720 * import_as_names
Guido van Rossum3d602e31996-07-21 02:33:56 +00001721 */
Guido van Rossum47478871996-08-21 14:32:37 +00001722static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001723validate_import_from(node *tree)
1724{
1725 int nch = NCH(tree);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001726 int ndots = count_from_dots(tree);
1727 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1728 int offset = ndots + havename;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001729 int res = validate_ntype(tree, import_from)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001730 && (nch >= 4 + ndots)
1731 && validate_name(CHILD(tree, 0), "from")
1732 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1733 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001734
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001735 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1736 res = ((nch == offset + 5)
1737 && validate_lparen(CHILD(tree, offset + 2))
1738 && validate_import_as_names(CHILD(tree, offset + 3))
1739 && validate_rparen(CHILD(tree, offset + 4)));
1740 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1741 res = validate_import_as_names(CHILD(tree, offset + 2));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001742 return (res);
1743}
1744
1745
1746/* import_stmt: import_name | import_from */
1747static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001748validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001749{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001750 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001751 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001752
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001753 if (res) {
1754 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001755
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001756 if (ntype == import_name || ntype == import_from)
1757 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001758 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001759 res = 0;
1760 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001761 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001762 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001763 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001764 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001765 PyErr_Format(parser_error,
1766 "Unrecognized child node of import_stmt: %d.",
1767 TYPE(CHILD(tree, 0)));
1768 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001769 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001770}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001771
1772
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001773
1774
Guido van Rossum47478871996-08-21 14:32:37 +00001775static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001776validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001777{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001778 int j;
1779 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001780 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001781 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001782
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001783 if (!res && !PyErr_Occurred())
1784 err_string("illegal global statement");
1785
Guido van Rossum3d602e31996-07-21 02:33:56 +00001786 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001787 res = (validate_name(CHILD(tree, 0), "global")
1788 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001789 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001790 res = (validate_comma(CHILD(tree, j))
1791 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001792
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001793 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001794}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001795
1796
Guido van Rossum925e5471997-04-02 05:32:13 +00001797/* assert_stmt:
1798 *
1799 * 'assert' test [',' test]
1800 */
1801static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001802validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001803{
1804 int nch = NCH(tree);
1805 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001806 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001807 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001808 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001809
1810 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001811 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001812 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001813 res = (validate_comma(CHILD(tree, 2))
1814 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001815
1816 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001817}
Guido van Rossum925e5471997-04-02 05:32:13 +00001818
1819
Guido van Rossum47478871996-08-21 14:32:37 +00001820static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001821validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001822{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001823 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001824 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001825 && ((nch == 4) || (nch == 7))
1826 && validate_name(CHILD(tree, 0), "while")
1827 && validate_test(CHILD(tree, 1))
1828 && validate_colon(CHILD(tree, 2))
1829 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001830
Guido van Rossum3d602e31996-07-21 02:33:56 +00001831 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001832 res = (validate_name(CHILD(tree, 4), "else")
1833 && validate_colon(CHILD(tree, 5))
1834 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001835
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001836 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001837}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001838
1839
Guido van Rossum47478871996-08-21 14:32:37 +00001840static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001841validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001842{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001843 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001844 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001845 && ((nch == 6) || (nch == 9))
1846 && validate_name(CHILD(tree, 0), "for")
1847 && validate_exprlist(CHILD(tree, 1))
1848 && validate_name(CHILD(tree, 2), "in")
1849 && validate_testlist(CHILD(tree, 3))
1850 && validate_colon(CHILD(tree, 4))
1851 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001852
Guido van Rossum3d602e31996-07-21 02:33:56 +00001853 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001854 res = (validate_name(CHILD(tree, 6), "else")
1855 && validate_colon(CHILD(tree, 7))
1856 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001857
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001858 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001859}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001860
1861
Guido van Rossum3d602e31996-07-21 02:33:56 +00001862/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001863 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001864 * | 'try' ':' suite 'finally' ':' suite
1865 *
1866 */
Guido van Rossum47478871996-08-21 14:32:37 +00001867static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001868validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001869{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001870 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001871 int pos = 3;
1872 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001873 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001874
1875 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001876 res = (validate_name(CHILD(tree, 0), "try")
1877 && validate_colon(CHILD(tree, 1))
1878 && validate_suite(CHILD(tree, 2))
1879 && validate_colon(CHILD(tree, nch - 2))
1880 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001881 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001882 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001883 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1884 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001885
1886 PyErr_Format(parser_error,
1887 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001888 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001889 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001890 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001891 res = (validate_except_clause(CHILD(tree, pos))
1892 && validate_colon(CHILD(tree, pos + 1))
1893 && validate_suite(CHILD(tree, pos + 2)));
1894 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001895 }
1896 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001897 res = validate_ntype(CHILD(tree, pos), NAME);
1898 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1899 res = (validate_numnodes(tree, 6, "try/finally")
1900 && validate_colon(CHILD(tree, 4))
1901 && validate_suite(CHILD(tree, 5)));
1902 else if (res) {
1903 if (nch == (pos + 3)) {
1904 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1905 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1906 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001907 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001908 }
1909 else if (nch == (pos + 6)) {
1910 res = (validate_name(CHILD(tree, pos), "except")
1911 && validate_colon(CHILD(tree, pos + 1))
1912 && validate_suite(CHILD(tree, pos + 2))
1913 && validate_name(CHILD(tree, pos + 3), "else"));
1914 }
1915 else
1916 res = validate_numnodes(tree, pos + 3, "try/except");
1917 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001918 }
1919 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001920}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001921
1922
Guido van Rossum47478871996-08-21 14:32:37 +00001923static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001924validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001925{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001926 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001927 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001928 && ((nch == 1) || (nch == 2) || (nch == 4))
1929 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001930
Guido van Rossum3d602e31996-07-21 02:33:56 +00001931 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001932 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001933 if (res && (nch == 4))
Guido van Rossumb940e112007-01-10 16:19:56 +00001934 res = (validate_name(CHILD(tree, 2), "as")
1935 && validate_ntype(CHILD(tree, 3), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001936
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001937 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001938}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001939
1940
Guido van Rossum47478871996-08-21 14:32:37 +00001941static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001942validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001943{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001944 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001945 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001946
Guido van Rossum3d602e31996-07-21 02:33:56 +00001947 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001948 res = ((nch == 1)
1949 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001950 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001951 res = validate_or_test(CHILD(tree, 0));
1952 res = (res && (nch == 1 || (nch == 5 &&
1953 validate_name(CHILD(tree, 1), "if") &&
1954 validate_or_test(CHILD(tree, 2)) &&
1955 validate_name(CHILD(tree, 3), "else") &&
1956 validate_test(CHILD(tree, 4)))));
1957 }
1958 return (res);
1959}
1960
1961static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001962validate_test_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001963{
1964 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001965 int res = validate_ntype(tree, test_nocond) && (nch == 1);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001966
Nick Coghlan650f0d02007-04-15 12:05:43 +00001967 if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
1968 res = (validate_lambdef_nocond(CHILD(tree, 0)));
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001969 else if (res) {
1970 res = (validate_or_test(CHILD(tree, 0)));
1971 }
1972 return (res);
1973}
1974
1975static int
1976validate_or_test(node *tree)
1977{
1978 int nch = NCH(tree);
1979 int res = validate_ntype(tree, or_test) && is_odd(nch);
1980
1981 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001982 int pos;
1983 res = validate_and_test(CHILD(tree, 0));
1984 for (pos = 1; res && (pos < nch); pos += 2)
1985 res = (validate_name(CHILD(tree, pos), "or")
1986 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001987 }
1988 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001989}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001990
1991
Guido van Rossum47478871996-08-21 14:32:37 +00001992static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001993validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001994{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001995 int pos;
1996 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001997 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001998 && is_odd(nch)
1999 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002000
Guido van Rossum3d602e31996-07-21 02:33:56 +00002001 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002002 res = (validate_name(CHILD(tree, pos), "and")
2003 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002004
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002005 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002006}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002007
2008
Guido van Rossum47478871996-08-21 14:32:37 +00002009static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002010validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002011{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002012 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002013 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002014
Guido van Rossum3d602e31996-07-21 02:33:56 +00002015 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002016 if (nch == 2)
2017 res = (validate_name(CHILD(tree, 0), "not")
2018 && validate_not_test(CHILD(tree, 1)));
2019 else if (nch == 1)
2020 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002021 }
2022 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002023}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002024
2025
Guido van Rossum47478871996-08-21 14:32:37 +00002026static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002027validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002028{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002029 int pos;
2030 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002031 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002032 && is_odd(nch)
Guido van Rossum0368b722007-05-11 16:50:42 +00002033 && validate_star_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002034
Guido van Rossum3d602e31996-07-21 02:33:56 +00002035 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002036 res = (validate_comp_op(CHILD(tree, pos))
Guido van Rossum0368b722007-05-11 16:50:42 +00002037 && validate_star_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002038
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002039 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002040}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002041
2042
Guido van Rossum47478871996-08-21 14:32:37 +00002043static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002044validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002045{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002046 int res = 0;
2047 int nch = NCH(tree);
2048
Guido van Rossum3d602e31996-07-21 02:33:56 +00002049 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002050 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002051 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002052 /*
2053 * Only child will be a terminal with a well-defined symbolic name
2054 * or a NAME with a string of either 'is' or 'in'
2055 */
2056 tree = CHILD(tree, 0);
2057 switch (TYPE(tree)) {
2058 case LESS:
2059 case GREATER:
2060 case EQEQUAL:
2061 case EQUAL:
2062 case LESSEQUAL:
2063 case GREATEREQUAL:
2064 case NOTEQUAL:
2065 res = 1;
2066 break;
2067 case NAME:
2068 res = ((strcmp(STR(tree), "in") == 0)
2069 || (strcmp(STR(tree), "is") == 0));
2070 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002071 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002072 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002073 }
2074 break;
2075 default:
Fred Drake661ea262000-10-24 19:57:45 +00002076 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002077 break;
2078 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002079 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002080 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002081 res = (validate_ntype(CHILD(tree, 0), NAME)
2082 && validate_ntype(CHILD(tree, 1), NAME)
2083 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2084 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2085 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2086 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2087 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002088 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002089 }
2090 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002091}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002092
2093
Guido van Rossum47478871996-08-21 14:32:37 +00002094static int
Guido van Rossum0368b722007-05-11 16:50:42 +00002095validate_star_expr(node *tree)
2096{
2097 int res = validate_ntype(tree, star_expr);
2098 if (!res) return res;
2099 if (NCH(tree) == 2) {
2100 return validate_ntype(CHILD(tree, 0), STAR) && \
2101 validate_expr(CHILD(tree, 1));
2102 } else {
2103 return validate_expr(CHILD(tree, 0));
2104 }
2105}
2106
2107
2108static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002109validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002110{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002111 int j;
2112 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002113 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002114 && is_odd(nch)
2115 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002116
Guido van Rossum3d602e31996-07-21 02:33:56 +00002117 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002118 res = (validate_xor_expr(CHILD(tree, j))
2119 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002120
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002121 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002122}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002123
2124
Guido van Rossum47478871996-08-21 14:32:37 +00002125static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002126validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002127{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002128 int j;
2129 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002130 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002131 && is_odd(nch)
2132 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002133
Guido van Rossum3d602e31996-07-21 02:33:56 +00002134 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002135 res = (validate_circumflex(CHILD(tree, j - 1))
2136 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002137
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002138 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002139}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002140
2141
Guido van Rossum47478871996-08-21 14:32:37 +00002142static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002143validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002144{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002145 int pos;
2146 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002147 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002148 && is_odd(nch)
2149 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002150
Guido van Rossum3d602e31996-07-21 02:33:56 +00002151 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002152 res = (validate_ampersand(CHILD(tree, pos))
2153 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002154
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002155 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002156}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002157
2158
2159static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002160validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002161 {
2162 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002163 int nch = NCH(tree);
2164 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002165 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002166
Guido van Rossum3d602e31996-07-21 02:33:56 +00002167 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002168 if (TYPE(CHILD(tree, pos)) != op1)
2169 res = validate_ntype(CHILD(tree, pos), op2);
2170 if (res)
2171 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002172 }
2173 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002174}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002175
2176
Guido van Rossum47478871996-08-21 14:32:37 +00002177static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002178validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002179{
2180 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002181 && validate_chain_two_ops(tree, validate_arith_expr,
2182 LEFTSHIFT, RIGHTSHIFT));
2183}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002184
2185
Guido van Rossum47478871996-08-21 14:32:37 +00002186static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002187validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002188{
2189 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002190 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2191}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002192
2193
Guido van Rossum47478871996-08-21 14:32:37 +00002194static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002195validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002196{
2197 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002198 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002199 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002200 && is_odd(nch)
2201 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002202
Guido van Rossum3d602e31996-07-21 02:33:56 +00002203 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002204 res = (((TYPE(CHILD(tree, pos)) == STAR)
2205 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002206 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002207 || (TYPE(CHILD(tree, pos)) == PERCENT))
2208 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002209
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002210 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002211}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002212
2213
Guido van Rossum3d602e31996-07-21 02:33:56 +00002214/* factor:
2215 *
2216 * factor: ('+'|'-'|'~') factor | power
2217 */
Guido van Rossum47478871996-08-21 14:32:37 +00002218static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002219validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002220{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002221 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002222 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002223 && (((nch == 2)
2224 && ((TYPE(CHILD(tree, 0)) == PLUS)
2225 || (TYPE(CHILD(tree, 0)) == MINUS)
2226 || (TYPE(CHILD(tree, 0)) == TILDE))
2227 && validate_factor(CHILD(tree, 1)))
2228 || ((nch == 1)
2229 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002230 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002231}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002232
2233
Guido van Rossum3d602e31996-07-21 02:33:56 +00002234/* power:
2235 *
2236 * power: atom trailer* ('**' factor)*
2237 */
Guido van Rossum47478871996-08-21 14:32:37 +00002238static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002239validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002240{
2241 int pos = 1;
2242 int nch = NCH(tree);
2243 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002244 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002245
2246 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002247 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002248 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002249 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002250 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002251 return (0);
2252 }
2253 for ( ; res && (pos < (nch - 1)); pos += 2)
2254 res = (validate_doublestar(CHILD(tree, pos))
2255 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002256 }
2257 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002258}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002259
2260
Guido van Rossum47478871996-08-21 14:32:37 +00002261static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002262validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002263{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002264 int pos;
2265 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002266 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002267
Fred Drakecff283c2000-08-21 22:24:43 +00002268 if (res && nch < 1)
2269 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002270 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002271 switch (TYPE(CHILD(tree, 0))) {
2272 case LPAR:
2273 res = ((nch <= 3)
2274 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002275
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002276 if (res && (nch == 3)) {
2277 if (TYPE(CHILD(tree, 1))==yield_expr)
2278 res = validate_yield_expr(CHILD(tree, 1));
2279 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00002280 res = validate_testlist_comp(CHILD(tree, 1));
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002281 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002282 break;
2283 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002284 if (nch == 2)
2285 res = validate_ntype(CHILD(tree, 1), RSQB);
2286 else if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002287 res = (validate_testlist_comp(CHILD(tree, 1))
Fred Drakecff283c2000-08-21 22:24:43 +00002288 && validate_ntype(CHILD(tree, 2), RSQB));
2289 else {
2290 res = 0;
2291 err_string("illegal list display atom");
2292 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002293 break;
2294 case LBRACE:
2295 res = ((nch <= 3)
2296 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002297
Fred Drakeff9ea482000-04-19 13:54:15 +00002298 if (res && (nch == 3))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002299 res = validate_dictorsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002300 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002301 case NAME:
2302 case NUMBER:
2303 res = (nch == 1);
2304 break;
2305 case STRING:
2306 for (pos = 1; res && (pos < nch); ++pos)
2307 res = validate_ntype(CHILD(tree, pos), STRING);
2308 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002309 case DOT:
2310 res = (nch == 3 &&
2311 validate_ntype(CHILD(tree, 1), DOT) &&
2312 validate_ntype(CHILD(tree, 2), DOT));
2313 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002314 default:
2315 res = 0;
2316 break;
2317 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002318 }
2319 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002320}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002321
2322
Nick Coghlan650f0d02007-04-15 12:05:43 +00002323/* testlist_comp:
2324 * test ( comp_for | (',' test)* [','] )
Fred Drake85bf3bb2000-08-23 15:35:26 +00002325 */
Fred Drakecff283c2000-08-21 22:24:43 +00002326static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002327validate_testlist_comp(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00002328{
2329 int nch = NCH(tree);
2330 int ok = nch;
2331
2332 if (nch == 0)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002333 err_string("missing child nodes of testlist_comp");
2334 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002335 ok = validate_test(CHILD(tree, 0));
Nick Coghlan650f0d02007-04-15 12:05:43 +00002336 }
Fred Drakecff283c2000-08-21 22:24:43 +00002337
2338 /*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002339 * comp_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002340 */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002341 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2342 ok = validate_comp_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002343 else {
2344 /* (',' test)* [','] */
2345 int i = 1;
2346 while (ok && nch - i >= 2) {
2347 ok = (validate_comma(CHILD(tree, i))
2348 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002349 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002350 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002351 if (ok && i == nch-1)
2352 ok = validate_comma(CHILD(tree, i));
2353 else if (i != nch) {
2354 ok = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002355 err_string("illegal trailing nodes for testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002356 }
2357 }
2358 return ok;
2359}
Fred Drakecff283c2000-08-21 22:24:43 +00002360
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002361/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002362 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002363 */
2364static int
2365validate_decorator(node *tree)
2366{
2367 int ok;
2368 int nch = NCH(tree);
2369 ok = (validate_ntype(tree, decorator) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002370 (nch == 3 || nch == 5 || nch == 6) &&
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002371 validate_at(CHILD(tree, 0)) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002372 validate_dotted_name(CHILD(tree, 1)) &&
2373 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002374
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002375 if (ok && nch != 3) {
2376 ok = (validate_lparen(CHILD(tree, 2)) &&
2377 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002378
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002379 if (ok && nch == 6)
2380 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002381 }
2382
2383 return ok;
2384}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002385
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002386/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002387 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002388 */
2389static int
2390validate_decorators(node *tree)
2391{
Guido van Rossumfc158e22007-11-15 19:17:28 +00002392 int i, nch, ok;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002393 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002394 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002395
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002396 for (i = 0; ok && i < nch; ++i)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002397 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002398
2399 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002400}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002401
Guido van Rossum3d602e31996-07-21 02:33:56 +00002402/* funcdef:
Guido van Rossumfc158e22007-11-15 19:17:28 +00002403 *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002404 * -5 -4 -3 -2 -1
2405 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002406 */
Guido van Rossum47478871996-08-21 14:32:37 +00002407static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002408validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002409{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002410 int nch = NCH(tree);
2411 int ok = (validate_ntype(tree, funcdef)
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002412 && (nch == 5)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002413 && validate_name(RCHILD(tree, -5), "def")
2414 && validate_ntype(RCHILD(tree, -4), NAME)
2415 && validate_colon(RCHILD(tree, -2))
2416 && validate_parameters(RCHILD(tree, -3))
2417 && validate_suite(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002418 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002419}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002420
2421
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002422/* decorated
2423 * decorators (classdef | funcdef)
2424 */
2425static int
2426validate_decorated(node *tree)
2427{
2428 int nch = NCH(tree);
2429 int ok = (validate_ntype(tree, decorated)
2430 && (nch == 2)
2431 && validate_decorators(RCHILD(tree, -2))
2432 && (validate_funcdef(RCHILD(tree, -1))
2433 || validate_class(RCHILD(tree, -1)))
2434 );
2435 return ok;
2436}
2437
Guido van Rossum47478871996-08-21 14:32:37 +00002438static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002439validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002440{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002441 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002442 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002443 && ((nch == 3) || (nch == 4))
2444 && validate_name(CHILD(tree, 0), "lambda")
2445 && validate_colon(CHILD(tree, nch - 2))
2446 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002447
Guido van Rossum3d602e31996-07-21 02:33:56 +00002448 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002449 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002450 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002451 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002452
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002453 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002454}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002455
2456
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002457static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002458validate_lambdef_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002459{
2460 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002461 int res = (validate_ntype(tree, lambdef_nocond)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002462 && ((nch == 3) || (nch == 4))
2463 && validate_name(CHILD(tree, 0), "lambda")
2464 && validate_colon(CHILD(tree, nch - 2))
2465 && validate_test(CHILD(tree, nch - 1)));
2466
2467 if (res && (nch == 4))
2468 res = validate_varargslist(CHILD(tree, 1));
2469 else if (!res && !PyErr_Occurred())
Nick Coghlan650f0d02007-04-15 12:05:43 +00002470 (void) validate_numnodes(tree, 3, "lambdef_nocond");
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002471
2472 return (res);
2473}
2474
2475
Guido van Rossum3d602e31996-07-21 02:33:56 +00002476/* arglist:
2477 *
Fred Drakecff283c2000-08-21 22:24:43 +00002478 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002479 */
Guido van Rossum47478871996-08-21 14:32:37 +00002480static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002481validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002482{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002483 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002484 int i = 0;
2485 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002486
2487 if (nch <= 0)
2488 /* raise the right error from having an invalid number of children */
2489 return validate_numnodes(tree, nch + 1, "arglist");
2490
Raymond Hettinger354433a2004-05-19 08:20:33 +00002491 if (nch > 1) {
2492 for (i=0; i<nch; i++) {
2493 if (TYPE(CHILD(tree, i)) == argument) {
2494 node *ch = CHILD(tree, i);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002495 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
Raymond Hettinger354433a2004-05-19 08:20:33 +00002496 err_string("need '(', ')' for generator expression");
2497 return 0;
2498 }
2499 }
2500 }
2501 }
2502
Fred Drakecff283c2000-08-21 22:24:43 +00002503 while (ok && nch-i >= 2) {
2504 /* skip leading (argument ',') */
2505 ok = (validate_argument(CHILD(tree, i))
2506 && validate_comma(CHILD(tree, i+1)));
2507 if (ok)
2508 i += 2;
2509 else
2510 PyErr_Clear();
2511 }
2512 ok = 1;
2513 if (nch-i > 0) {
2514 /*
2515 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002516 */
Fred Drakecff283c2000-08-21 22:24:43 +00002517 int sym = TYPE(CHILD(tree, i));
2518
2519 if (sym == argument) {
2520 ok = validate_argument(CHILD(tree, i));
2521 if (ok && i+1 != nch) {
2522 err_string("illegal arglist specification"
2523 " (extra stuff on end)");
2524 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002525 }
Fred Drakecff283c2000-08-21 22:24:43 +00002526 }
2527 else if (sym == STAR) {
2528 ok = validate_star(CHILD(tree, i));
2529 if (ok && (nch-i == 2))
2530 ok = validate_test(CHILD(tree, i+1));
2531 else if (ok && (nch-i == 5))
2532 ok = (validate_test(CHILD(tree, i+1))
2533 && validate_comma(CHILD(tree, i+2))
2534 && validate_doublestar(CHILD(tree, i+3))
2535 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002536 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002537 err_string("illegal use of '*' in arglist");
2538 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002539 }
2540 }
Fred Drakecff283c2000-08-21 22:24:43 +00002541 else if (sym == DOUBLESTAR) {
2542 if (nch-i == 2)
2543 ok = (validate_doublestar(CHILD(tree, i))
2544 && validate_test(CHILD(tree, i+1)));
2545 else {
2546 err_string("illegal use of '**' in arglist");
2547 ok = 0;
2548 }
2549 }
2550 else {
2551 err_string("illegal arglist specification");
2552 ok = 0;
2553 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002554 }
2555 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002556}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002557
2558
2559
2560/* argument:
2561 *
Nick Coghlan650f0d02007-04-15 12:05:43 +00002562 * [test '='] test [comp_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002563 */
Guido van Rossum47478871996-08-21 14:32:37 +00002564static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002565validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002566{
2567 int nch = NCH(tree);
2568 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002569 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002570 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002571
Raymond Hettinger354433a2004-05-19 08:20:33 +00002572 if (res && (nch == 2))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002573 res = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002574 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002575 res = (validate_equal(CHILD(tree, 1))
2576 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002577
2578 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002579}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002580
2581
2582
2583/* trailer:
2584 *
Guido van Rossum47478871996-08-21 14:32:37 +00002585 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002586 */
Guido van Rossum47478871996-08-21 14:32:37 +00002587static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002588validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002589{
2590 int nch = NCH(tree);
2591 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002592
2593 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002594 switch (TYPE(CHILD(tree, 0))) {
2595 case LPAR:
2596 res = validate_rparen(CHILD(tree, nch - 1));
2597 if (res && (nch == 3))
2598 res = validate_arglist(CHILD(tree, 1));
2599 break;
2600 case LSQB:
2601 res = (validate_numnodes(tree, 3, "trailer")
2602 && validate_subscriptlist(CHILD(tree, 1))
2603 && validate_ntype(CHILD(tree, 2), RSQB));
2604 break;
2605 case DOT:
2606 res = (validate_numnodes(tree, 2, "trailer")
2607 && validate_ntype(CHILD(tree, 1), NAME));
2608 break;
2609 default:
2610 res = 0;
2611 break;
2612 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002613 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002614 else {
2615 (void) validate_numnodes(tree, 2, "trailer");
2616 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002617 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002618}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002619
2620
Guido van Rossum47478871996-08-21 14:32:37 +00002621/* subscriptlist:
2622 *
2623 * subscript (',' subscript)* [',']
2624 */
2625static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002626validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002627{
2628 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002629 validate_subscript, "subscriptlist"));
2630}
Guido van Rossum47478871996-08-21 14:32:37 +00002631
2632
Guido van Rossum3d602e31996-07-21 02:33:56 +00002633/* subscript:
2634 *
Guido van Rossum47478871996-08-21 14:32:37 +00002635 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002636 */
Guido van Rossum47478871996-08-21 14:32:37 +00002637static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002638validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002639{
Guido van Rossum47478871996-08-21 14:32:37 +00002640 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002641 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002642 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002643
Guido van Rossum47478871996-08-21 14:32:37 +00002644 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002645 if (!PyErr_Occurred())
2646 err_string("invalid number of arguments for subscript node");
2647 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002648 }
Guido van Rossum47478871996-08-21 14:32:37 +00002649 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002650 /* take care of ('.' '.' '.') possibility */
2651 return (validate_numnodes(tree, 3, "subscript")
2652 && validate_dot(CHILD(tree, 0))
2653 && validate_dot(CHILD(tree, 1))
2654 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002655 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002656 if (TYPE(CHILD(tree, 0)) == test)
2657 res = validate_test(CHILD(tree, 0));
2658 else
2659 res = validate_colon(CHILD(tree, 0));
2660 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002661 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002662 /* Must be [test] ':' [test] [sliceop],
2663 * but at least one of the optional components will
2664 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002665 */
2666 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002667 res = validate_test(CHILD(tree, 0));
2668 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002669 }
2670 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002671 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002672 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002673 int rem = nch - ++offset;
2674 if (rem) {
2675 if (TYPE(CHILD(tree, offset)) == test) {
2676 res = validate_test(CHILD(tree, offset));
2677 ++offset;
2678 --rem;
2679 }
2680 if (res && rem)
2681 res = validate_sliceop(CHILD(tree, offset));
2682 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002683 }
2684 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002685}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002686
2687
Guido van Rossum47478871996-08-21 14:32:37 +00002688static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002689validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002690{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002691 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002692 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002693 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002694 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002695 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002696 }
Guido van Rossum47478871996-08-21 14:32:37 +00002697 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002698 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002699 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002700 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002701
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002702 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002703}
Guido van Rossum47478871996-08-21 14:32:37 +00002704
2705
2706static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002707validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002708{
2709 return (validate_repeating_list(tree, exprlist,
Guido van Rossum0368b722007-05-11 16:50:42 +00002710 validate_star_expr, "exprlist"));
Fred Drakeff9ea482000-04-19 13:54:15 +00002711}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002712
2713
Guido van Rossum47478871996-08-21 14:32:37 +00002714static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002715validate_dictorsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002716{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002717 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002718 int res = (validate_ntype(tree, dictorsetmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002719 && (nch >= 3)
2720 && validate_test(CHILD(tree, 0))
2721 && validate_colon(CHILD(tree, 1))
2722 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002723
Guido van Rossum3d602e31996-07-21 02:33:56 +00002724 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002725 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002726 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002727 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002728
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002729 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002730 int pos = 3;
2731 /* ( ',' test ':' test )* */
2732 while (res && (pos < nch)) {
2733 res = (validate_comma(CHILD(tree, pos))
2734 && validate_test(CHILD(tree, pos + 1))
2735 && validate_colon(CHILD(tree, pos + 2))
2736 && validate_test(CHILD(tree, pos + 3)));
2737 pos += 4;
2738 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002739 }
2740 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002741}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002742
2743
Guido van Rossum47478871996-08-21 14:32:37 +00002744static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002745validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002746{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002747 int pos;
2748 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002749 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002750 && (nch >= 2)
2751 && validate_testlist(CHILD(tree, 0))
2752 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002753
Guido van Rossum3d602e31996-07-21 02:33:56 +00002754 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002755 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002756
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002757 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002758}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002759
2760
Guido van Rossum47478871996-08-21 14:32:37 +00002761static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002762validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002763{
Fred Drakeff9ea482000-04-19 13:54:15 +00002764 int nch = 0; /* num. children on current node */
2765 int res = 1; /* result value */
2766 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002767
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002768 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002769 nch = NCH(tree);
2770 next = 0;
2771 switch (TYPE(tree)) {
2772 /*
2773 * Definition nodes.
2774 */
2775 case funcdef:
2776 res = validate_funcdef(tree);
2777 break;
2778 case classdef:
2779 res = validate_class(tree);
2780 break;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002781 case decorated:
2782 res = validate_decorated(tree);
2783 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002784 /*
2785 * "Trivial" parse tree nodes.
2786 * (Why did I call these trivial?)
2787 */
2788 case stmt:
2789 res = validate_stmt(tree);
2790 break;
2791 case small_stmt:
2792 /*
Guido van Rossum452bf512007-02-09 05:32:43 +00002793 * expr_stmt | del_stmt | pass_stmt | flow_stmt
Georg Brandl7cae87c2006-09-06 06:51:57 +00002794 * | import_stmt | global_stmt | assert_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002795 */
2796 res = validate_small_stmt(tree);
2797 break;
2798 case flow_stmt:
2799 res = (validate_numnodes(tree, 1, "flow_stmt")
2800 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2801 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002802 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002803 || (TYPE(CHILD(tree, 0)) == return_stmt)
2804 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2805 if (res)
2806 next = CHILD(tree, 0);
2807 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002808 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002809 break;
Fred Drake02126f22001-07-17 02:59:15 +00002810 case yield_stmt:
2811 res = validate_yield_stmt(tree);
2812 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002813 /*
2814 * Compound statements.
2815 */
2816 case simple_stmt:
2817 res = validate_simple_stmt(tree);
2818 break;
2819 case compound_stmt:
2820 res = validate_compound_stmt(tree);
2821 break;
2822 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002823 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002824 */
2825 case expr_stmt:
2826 res = validate_expr_stmt(tree);
2827 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002828 case del_stmt:
2829 res = validate_del_stmt(tree);
2830 break;
2831 case pass_stmt:
2832 res = (validate_numnodes(tree, 1, "pass")
2833 && validate_name(CHILD(tree, 0), "pass"));
2834 break;
2835 case break_stmt:
2836 res = (validate_numnodes(tree, 1, "break")
2837 && validate_name(CHILD(tree, 0), "break"));
2838 break;
2839 case continue_stmt:
2840 res = (validate_numnodes(tree, 1, "continue")
2841 && validate_name(CHILD(tree, 0), "continue"));
2842 break;
2843 case return_stmt:
2844 res = validate_return_stmt(tree);
2845 break;
2846 case raise_stmt:
2847 res = validate_raise_stmt(tree);
2848 break;
2849 case import_stmt:
2850 res = validate_import_stmt(tree);
2851 break;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00002852 case import_name:
2853 res = validate_import_name(tree);
2854 break;
2855 case import_from:
2856 res = validate_import_from(tree);
2857 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002858 case global_stmt:
2859 res = validate_global_stmt(tree);
2860 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002861 case assert_stmt:
2862 res = validate_assert_stmt(tree);
2863 break;
2864 case if_stmt:
2865 res = validate_if(tree);
2866 break;
2867 case while_stmt:
2868 res = validate_while(tree);
2869 break;
2870 case for_stmt:
2871 res = validate_for(tree);
2872 break;
2873 case try_stmt:
2874 res = validate_try(tree);
2875 break;
2876 case suite:
2877 res = validate_suite(tree);
2878 break;
2879 /*
2880 * Expression nodes.
2881 */
2882 case testlist:
2883 res = validate_testlist(tree);
2884 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002885 case yield_expr:
2886 res = validate_yield_expr(tree);
2887 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002888 case testlist1:
2889 res = validate_testlist1(tree);
2890 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002891 case test:
2892 res = validate_test(tree);
2893 break;
2894 case and_test:
2895 res = validate_and_test(tree);
2896 break;
2897 case not_test:
2898 res = validate_not_test(tree);
2899 break;
2900 case comparison:
2901 res = validate_comparison(tree);
2902 break;
2903 case exprlist:
2904 res = validate_exprlist(tree);
2905 break;
2906 case comp_op:
2907 res = validate_comp_op(tree);
2908 break;
2909 case expr:
2910 res = validate_expr(tree);
2911 break;
2912 case xor_expr:
2913 res = validate_xor_expr(tree);
2914 break;
2915 case and_expr:
2916 res = validate_and_expr(tree);
2917 break;
2918 case shift_expr:
2919 res = validate_shift_expr(tree);
2920 break;
2921 case arith_expr:
2922 res = validate_arith_expr(tree);
2923 break;
2924 case term:
2925 res = validate_term(tree);
2926 break;
2927 case factor:
2928 res = validate_factor(tree);
2929 break;
2930 case power:
2931 res = validate_power(tree);
2932 break;
2933 case atom:
2934 res = validate_atom(tree);
2935 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002936
Fred Drakeff9ea482000-04-19 13:54:15 +00002937 default:
2938 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002939 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002940 res = 0;
2941 break;
2942 }
2943 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002944 }
2945 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002946}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002947
2948
Guido van Rossum47478871996-08-21 14:32:37 +00002949static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002950validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002951{
2952 int res = validate_eval_input(tree);
2953
2954 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002955 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002956
2957 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002958}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002959
2960
Guido van Rossum3d602e31996-07-21 02:33:56 +00002961/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002962 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002963 */
Guido van Rossum47478871996-08-21 14:32:37 +00002964static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002965validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002966{
Fred Drakec2683dd2001-07-17 19:32:05 +00002967 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002968 int nch = NCH(tree) - 1;
2969 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002970 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002971
Fred Drakec2683dd2001-07-17 19:32:05 +00002972 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002973 if (TYPE(CHILD(tree, j)) == stmt)
2974 res = validate_stmt(CHILD(tree, j));
2975 else
2976 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002977 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002978 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002979 * user. Hopefully, this won't be needed. If a user reports getting
2980 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002981 */
2982 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002983 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002984
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002985 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002986}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002987
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00002988static int
2989validate_encoding_decl(node *tree)
2990{
2991 int nch = NCH(tree);
2992 int res = ((nch == 1)
2993 && validate_file_input(CHILD(tree, 0)));
2994
2995 if (!res && !PyErr_Occurred())
2996 err_string("Error Parsing encoding_decl");
2997
2998 return res;
2999}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003000
Fred Drake43f8f9b1998-04-13 16:25:46 +00003001static PyObject*
3002pickle_constructor = NULL;
3003
3004
3005static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003006parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003007{
Fred Drake268397f1998-04-29 14:16:32 +00003008 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003009 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003010 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003011 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003012
Fred Drakec2683dd2001-07-17 19:32:05 +00003013 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003014 PyObject *newargs;
3015 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003016
Fred Drake2a6875e1999-09-20 22:32:18 +00003017 if ((empty_dict = PyDict_New()) == NULL)
3018 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003019 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003020 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003021 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003022 if (tuple != NULL) {
3023 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3024 Py_DECREF(tuple);
3025 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003026 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003027 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003028 }
3029 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003030 Py_XDECREF(empty_dict);
3031
Fred Drake43f8f9b1998-04-13 16:25:46 +00003032 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003033}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003034
3035
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003036/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003037 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003038 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003039 * We'd really have to write a wrapper around it all anyway to allow
3040 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003041 */
3042static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003043 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003044 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003045 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003046 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003047 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003048 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003049 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003050 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003051 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003052 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003053 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003054 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003055 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003056 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003057 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003058 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003059 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003060 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003061
Fred Drake43f8f9b1998-04-13 16:25:46 +00003062 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003063 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003064 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003065
Fred Drake268397f1998-04-29 14:16:32 +00003066 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003067 };
3068
3069
Martin v. Löwis1a214512008-06-11 05:26:20 +00003070
3071static struct PyModuleDef parsermodule = {
3072 PyModuleDef_HEAD_INIT,
3073 "parser",
3074 NULL,
3075 -1,
3076 parser_functions,
3077 NULL,
3078 NULL,
3079 NULL,
3080 NULL
3081};
3082
3083PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003084
Mark Hammond62b1ab12002-07-23 06:31:15 +00003085PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003086PyInit_parser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003087{
Fred Drake13130bc2001-08-15 16:44:56 +00003088 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003089
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00003090 if (PyType_Ready(&PyST_Type) < 0)
3091 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00003092 module = PyModule_Create(&parsermodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003093 if (module == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003094 return NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003095
Fred Drake7a15ba51999-09-09 14:21:52 +00003096 if (parser_error == 0)
3097 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003098
Tim Peters6a627252003-07-21 14:25:23 +00003099 if (parser_error == 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003100 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003101 /* CAUTION: The code next used to skip bumping the refcount on
Martin v. Löwis1a214512008-06-11 05:26:20 +00003102 * parser_error. That's a disaster if PyInit_parser() gets called more
Tim Peters6a627252003-07-21 14:25:23 +00003103 * than once. By incref'ing, we ensure that each module dict that
3104 * gets created owns its reference to the shared parser_error object,
3105 * and the file static parser_error vrbl owns a reference too.
3106 */
3107 Py_INCREF(parser_error);
3108 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003109 return NULL;
Tim Peters6a627252003-07-21 14:25:23 +00003110
Fred Drakec2683dd2001-07-17 19:32:05 +00003111 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003112 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003113
Fred Drake13130bc2001-08-15 16:44:56 +00003114 PyModule_AddStringConstant(module, "__copyright__",
3115 parser_copyright_string);
3116 PyModule_AddStringConstant(module, "__doc__",
3117 parser_doc_string);
3118 PyModule_AddStringConstant(module, "__version__",
3119 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003120
Fred Drake78bdb9b2001-07-19 20:17:15 +00003121 /* Register to support pickling.
3122 * If this fails, the import of this module will fail because an
3123 * exception will be raised here; should we clear the exception?
3124 */
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003125 copyreg = PyImport_ImportModuleNoBlock("copyreg");
Fred Drake13130bc2001-08-15 16:44:56 +00003126 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003127 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003128
Fred Drake13130bc2001-08-15 16:44:56 +00003129 func = PyObject_GetAttrString(copyreg, "pickle");
3130 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3131 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003132 Py_XINCREF(pickle_constructor);
3133 if ((func != NULL) && (pickle_constructor != NULL)
3134 && (pickler != NULL)) {
3135 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003136
Thomas Wouters477c8d52006-05-27 19:21:47 +00003137 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3138 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003139 Py_XDECREF(res);
3140 }
3141 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003142 Py_XDECREF(pickle_constructor);
3143 Py_XDECREF(pickler);
3144 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003145 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00003146 return module;
Fred Drakeff9ea482000-04-19 13:54:15 +00003147}