blob: 000e63970cb5c66418084d571e167418d4ff46e6 [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000026 */
27
Fred Drakeff9ea482000-04-19 13:54:15 +000028#include "Python.h" /* general Python API */
29#include "graminit.h" /* symbols defined in the grammar */
30#include "node.h" /* internal parser structure */
31#include "token.h" /* token definitions */
32 /* ISTERMINAL() / ISNONTERMINAL() */
33#include "compile.h" /* PyNode_Compile() */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000034
Fred Drake268397f1998-04-29 14:16:32 +000035#ifdef lint
36#include <note.h>
37#else
38#define NOTE(x)
39#endif
40
Guido van Rossum19efc5f1998-04-28 16:10:19 +000041#ifdef macintosh
Fred Drakeff9ea482000-04-19 13:54:15 +000042char *strdup(char *);
Guido van Rossum19efc5f1998-04-28 16:10:19 +000043#endif
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000044
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000045/* String constants used to initialize module attributes.
46 *
47 */
48static char*
49parser_copyright_string
Guido van Rossum2a288461996-08-21 21:55:43 +000050= "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
51University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
52Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
53Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000054
55
56static char*
57parser_doc_string
58= "This is an interface to Python's internal parser.";
59
60static char*
Fred Drakecff283c2000-08-21 22:24:43 +000061parser_version_string = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000062
63
Fred Drakeff9ea482000-04-19 13:54:15 +000064typedef PyObject* (*SeqMaker) (int length);
65typedef int (*SeqInserter) (PyObject* sequence,
66 int index,
67 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000068
Thomas Wouters7e474022000-07-16 12:04:32 +000069/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000070 * original copyright statement is included below, and continues to apply
71 * in full to the function immediately following. All other material is
72 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
73 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000074 * new naming conventions. Added arguments to provide support for creating
75 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000076 */
77
Guido van Rossum52f2c051993-11-10 12:53:24 +000078
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000079static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000080node2tuple(node *n, /* node to convert */
81 SeqMaker mkseq, /* create sequence */
82 SeqInserter addelem, /* func. to add elem. in seq. */
83 int lineno) /* include line numbers? */
Guido van Rossum47478871996-08-21 14:32:37 +000084{
Guido van Rossum3d602e31996-07-21 02:33:56 +000085 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000086 Py_INCREF(Py_None);
87 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000088 }
89 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000090 int i;
91 PyObject *v;
92 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000093
Fred Drakeff9ea482000-04-19 13:54:15 +000094 v = mkseq(1 + NCH(n));
95 if (v == NULL)
96 return (v);
97 w = PyInt_FromLong(TYPE(n));
98 if (w == NULL) {
99 Py_DECREF(v);
100 return ((PyObject*) NULL);
101 }
102 (void) addelem(v, 0, w);
103 for (i = 0; i < NCH(n); i++) {
104 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno);
105 if (w == NULL) {
106 Py_DECREF(v);
107 return ((PyObject*) NULL);
108 }
109 (void) addelem(v, i+1, w);
110 }
111 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000112 }
113 else if (ISTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000114 PyObject *result = mkseq(2 + lineno);
115 if (result != NULL) {
116 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
117 (void) addelem(result, 1, PyString_FromString(STR(n)));
118 if (lineno == 1)
119 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
120 }
121 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000122 }
123 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000124 PyErr_SetString(PyExc_SystemError,
125 "unrecognized parse tree node type");
126 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000127 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000128}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000129/*
130 * End of material copyrighted by Stichting Mathematisch Centrum.
131 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000132
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000133
134
135/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000136 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000137 * field of the object type to identify which any given object represents.
138 * These should probably go in an external header to allow other extensions
139 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000140 */
141
Fred Drakec2683dd2001-07-17 19:32:05 +0000142#define PyST_EXPR 1
143#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000144
145
146/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000147 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000148 * naming style, but the code uses the new naming convention.
149 */
150
151static PyObject*
152parser_error = 0;
153
154
Fred Drakec2683dd2001-07-17 19:32:05 +0000155typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000156 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000157 node* st_node; /* the node* returned by the parser */
158 int st_type; /* EXPR or SUITE ? */
159} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000160
161
Fred Drake268397f1998-04-29 14:16:32 +0000162staticforward void
Fred Drakec2683dd2001-07-17 19:32:05 +0000163parser_free(PyST_Object *st);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000164
Fred Drake268397f1998-04-29 14:16:32 +0000165staticforward int
Fred Drakec2683dd2001-07-17 19:32:05 +0000166parser_compare(PyST_Object *left, PyST_Object *right);
Fred Drake268397f1998-04-29 14:16:32 +0000167
168staticforward PyObject *
Fred Drakeff9ea482000-04-19 13:54:15 +0000169parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000170
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000171
Fred Drake268397f1998-04-29 14:16:32 +0000172static
Fred Drakec2683dd2001-07-17 19:32:05 +0000173PyTypeObject PyST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000174 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000175 0,
Fred Drakec2683dd2001-07-17 19:32:05 +0000176 "st", /* tp_name */
177 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000178 0, /* tp_itemsize */
179 (destructor)parser_free, /* tp_dealloc */
180 0, /* tp_print */
181 parser_getattr, /* tp_getattr */
182 0, /* tp_setattr */
183 (cmpfunc)parser_compare, /* tp_compare */
184 0, /* tp_repr */
185 0, /* tp_as_number */
186 0, /* tp_as_sequence */
187 0, /* tp_as_mapping */
188 0, /* tp_hash */
189 0, /* tp_call */
190 0, /* tp_str */
191 0, /* tp_getattro */
192 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000193
194 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000195 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000196
Fred Drakeff9ea482000-04-19 13:54:15 +0000197 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000198
199 /* __doc__ */
200 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000201}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000202
203
204static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000205parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000206{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000207 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000208
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000209 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000210 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000211
212 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000213 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000214
215 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000216 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000217
218 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000219 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000220
221 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000222 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000223
224 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000225 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000226
Fred Drakeff9ea482000-04-19 13:54:15 +0000227 if (v != 0)
228 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000229 }
230 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000231}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000232
233
Fred Drakec2683dd2001-07-17 19:32:05 +0000234/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000235 *
236 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
237 * This really just wraps a call to parser_compare_nodes() with some easy
238 * checks and protection code.
239 *
240 */
241static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000242parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000243{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000244 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000245 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000246
247 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000248 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000249
Fred Drakec2683dd2001-07-17 19:32:05 +0000250 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000251}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000252
253
Fred Drakec2683dd2001-07-17 19:32:05 +0000254/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000255 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000256 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000257 * 'wrapper' object that holds a node* and allows it to be passed around in
258 * Python code.
259 *
260 */
261static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000262parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000263{
Fred Drakec2683dd2001-07-17 19:32:05 +0000264 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265
266 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000267 o->st_node = st;
268 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000269 }
Fred Drake268397f1998-04-29 14:16:32 +0000270 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000271 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000272 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000273 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000274}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000275
276
Fred Drakec2683dd2001-07-17 19:32:05 +0000277/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000278 *
279 * This is called by a del statement that reduces the reference count to 0.
280 *
281 */
282static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000283parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000284{
Fred Drakec2683dd2001-07-17 19:32:05 +0000285 PyNode_Free(st->st_node);
286 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000287}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000288
289
Fred Drakec2683dd2001-07-17 19:32:05 +0000290/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000291 *
292 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000293 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000294 *
295 */
296static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000297parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000298{
Guido van Rossum47478871996-08-21 14:32:37 +0000299 PyObject *line_option = 0;
300 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000301 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000302
Fred Drake7a15ba51999-09-09 14:21:52 +0000303 static char *keywords[] = {"ast", "line_info", NULL};
304
Fred Drake268397f1998-04-29 14:16:32 +0000305 if (self == NULL) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000306 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2tuple", keywords,
307 &PyST_Type, &self, &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000308 }
Fred Drake503d8d61998-04-13 18:45:18 +0000309 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000310 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:totuple", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000311 &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000312 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000313 int lineno = 0;
314 if (line_option != NULL) {
315 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
316 }
317 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000318 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000319 * since it's known to work already.
320 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000321 res = node2tuple(((PyST_Object*)self)->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000322 PyTuple_New, PyTuple_SetItem, lineno);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000323 }
324 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000325}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000326
327
Fred Drakec2683dd2001-07-17 19:32:05 +0000328/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000329 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000330 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000331 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000332 *
333 */
334static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000335parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000336{
Guido van Rossum47478871996-08-21 14:32:37 +0000337 PyObject *line_option = 0;
338 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000339 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000340
Fred Drake7a15ba51999-09-09 14:21:52 +0000341 static char *keywords[] = {"ast", "line_info", NULL};
342
Fred Drake503d8d61998-04-13 18:45:18 +0000343 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000344 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2list", keywords,
345 &PyST_Type, &self, &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000346 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000347 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:tolist", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000348 &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000349 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000350 int lineno = 0;
351 if (line_option != 0) {
352 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
353 }
354 /*
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(self->st_node,
Fred Drakeff9ea482000-04-19 13:54:15 +0000359 PyList_New, PyList_SetItem, lineno);
Guido van Rossum47478871996-08-21 14:32:37 +0000360 }
361 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000362}
Guido van Rossum47478871996-08-21 14:32:37 +0000363
364
Fred Drakec2683dd2001-07-17 19:32:05 +0000365/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000366 *
367 * This function creates code objects from the parse tree represented by
368 * the passed-in data object. An optional file name is passed in as well.
369 *
370 */
371static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000372parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000373{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000374 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000375 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000376 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000377
Fred Drake7a15ba51999-09-09 14:21:52 +0000378 static char *keywords[] = {"ast", "filename", NULL};
379
Fred Drake503d8d61998-04-13 18:45:18 +0000380 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000381 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
382 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000383 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000384 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000385 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000386
387 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000388 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000389
390 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000391}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000392
393
394/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
395 * PyObject* parser_issuite(PyObject* self, PyObject* args)
396 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000397 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000398 * a statement suite, respectively. The return is a Python truth value.
399 *
400 */
401static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000402parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000403{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000404 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000405 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000406
Fred Drake7a15ba51999-09-09 14:21:52 +0000407 static char *keywords[] = {"ast", NULL};
408
Fred Drake503d8d61998-04-13 18:45:18 +0000409 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000410 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000411 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000412 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000413 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000414
415 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000416 /* Check to see if the ST represents an expression or not. */
417 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000418 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000419 }
420 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000421}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000422
423
424static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000425parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000426{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000427 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000428 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000429
Fred Drake7a15ba51999-09-09 14:21:52 +0000430 static char *keywords[] = {"ast", NULL};
431
Fred Drake503d8d61998-04-13 18:45:18 +0000432 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000433 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000434 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000435 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000436 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000437
438 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000439 /* Check to see if the ST represents an expression or not. */
440 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000441 Py_INCREF(res);
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
Fred Drake7a15ba51999-09-09 14:21:52 +0000447#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
448
Fred Drake503d8d61998-04-13 18:45:18 +0000449static PyMethodDef
450parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000451 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
452 "Compile this ST object into a code object."},
Fred Drakeff9ea482000-04-19 13:54:15 +0000453 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Fred Drakec2683dd2001-07-17 19:32:05 +0000454 "Determines if this ST object was created from an expression."},
Fred Drakeff9ea482000-04-19 13:54:15 +0000455 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Fred Drakec2683dd2001-07-17 19:32:05 +0000456 "Determines if this ST object was created from a suite."},
457 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
458 "Creates a list-tree representation of this ST."},
459 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
460 "Creates a tuple-tree representation of this ST."},
Fred Drake503d8d61998-04-13 18:45:18 +0000461
Fred Drake268397f1998-04-29 14:16:32 +0000462 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000463};
464
Fred Drake503d8d61998-04-13 18:45:18 +0000465
466static PyObject*
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000467parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000468{
Fred Drake503d8d61998-04-13 18:45:18 +0000469 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000470}
Fred Drake503d8d61998-04-13 18:45:18 +0000471
472
Guido van Rossum3d602e31996-07-21 02:33:56 +0000473/* err_string(char* message)
474 *
475 * Sets the error string for an exception of type ParserError.
476 *
477 */
478static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000479err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000480{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000481 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000482}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000483
484
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000485/* PyObject* parser_do_parse(PyObject* args, int type)
486 *
487 * Internal function to actually execute the parse and return the result if
488 * successful, or set an exception if not.
489 *
490 */
491static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000492parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000493{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000494 char* string = 0;
495 PyObject* res = 0;
496
Fred Drake7a15ba51999-09-09 14:21:52 +0000497 static char *keywords[] = {"source", NULL};
498
499 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000500 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000501 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000502 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000503
Fred Drakeff9ea482000-04-19 13:54:15 +0000504 if (n != 0)
Fred Drakec2683dd2001-07-17 19:32:05 +0000505 res = parser_newstobject(n, type);
Fred Drakeff9ea482000-04-19 13:54:15 +0000506 else
Fred Drake661ea262000-10-24 19:57:45 +0000507 err_string("could not parse string");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000508 }
509 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000510}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000511
512
513/* PyObject* parser_expr(PyObject* self, PyObject* args)
514 * PyObject* parser_suite(PyObject* self, PyObject* args)
515 *
516 * External interfaces to the parser itself. Which is called determines if
517 * the parser attempts to recognize an expression ('eval' form) or statement
518 * suite ('exec' form). The real work is done by parser_do_parse() above.
519 *
520 */
521static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000522parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000523{
Fred Drake268397f1998-04-29 14:16:32 +0000524 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000525 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000526}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000527
528
529static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000530parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000531{
Fred Drake268397f1998-04-29 14:16:32 +0000532 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000533 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000534}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000535
536
537
Fred Drakec2683dd2001-07-17 19:32:05 +0000538/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000539 * object requires that the input tuple be valid without having to rely on
540 * catching an exception from the compiler. This is done to allow the
541 * compiler itself to remain fast, since most of its input will come from
542 * the parser directly, and therefore be known to be syntactically correct.
543 * This validation is done to ensure that we don't core dump the compile
544 * phase, returning an exception instead.
545 *
546 * Two aspects can be broken out in this code: creating a node tree from
547 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000548 * advantageous to expand the number of ST types to include funcdefs and
549 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000550 * here. They are not necessary, and not quite as useful in a raw form.
551 * For now, let's get expressions and suites working reliably.
552 */
553
554
Fred Drakeff9ea482000-04-19 13:54:15 +0000555staticforward node* build_node_tree(PyObject *tuple);
556staticforward int validate_expr_tree(node *tree);
557staticforward int validate_file_input(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000558
559
Fred Drakec2683dd2001-07-17 19:32:05 +0000560/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000561 *
562 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000563 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000564 * tuple can be validated. It does this by checking the first code of the
565 * tuple, and, if acceptable, builds the internal representation. If this
566 * step succeeds, the internal representation is validated as fully as
567 * possible with the various validate_*() routines defined below.
568 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000569 * This function must be changed if support is to be added for PyST_FRAGMENT
570 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000571 *
572 */
573static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000574parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000575{
Fred Drake268397f1998-04-29 14:16:32 +0000576 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000577 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000578 PyObject *tuple;
579 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000580
Fred Drake7a15ba51999-09-09 14:21:52 +0000581 static char *keywords[] = {"sequence", NULL};
582
Fred Drakec2683dd2001-07-17 19:32:05 +0000583 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000584 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000585 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000586 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000587 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000588 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000589 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000590 }
591 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000592 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000593 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000594 tree = build_node_tree(tuple);
595 if (tree != 0) {
596 int start_sym = TYPE(tree);
597 if (start_sym == eval_input) {
598 /* Might be an eval form. */
599 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000600 st = parser_newstobject(tree, PyST_EXPR);
Fred Drakeff9ea482000-04-19 13:54:15 +0000601 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000602 else if (start_sym == file_input) {
603 /* This looks like an exec form so far. */
604 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000605 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake0ac9b072000-09-12 21:58:06 +0000606 }
607 else {
608 /* This is a fragment, at best. */
609 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000610 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000611 }
Guido van Rossum47478871996-08-21 14:32:37 +0000612 }
Guido van Rossum47478871996-08-21 14:32:37 +0000613 /* Make sure we throw an exception on all errors. We should never
614 * get this, but we'd do well to be sure something is done.
615 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000616 if (st == NULL && !PyErr_Occurred())
617 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000618
Fred Drakec2683dd2001-07-17 19:32:05 +0000619 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000620}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000621
622
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000623/* node* build_node_children()
624 *
625 * Iterate across the children of the current non-terminal node and build
626 * their structures. If successful, return the root of this portion of
627 * the tree, otherwise, 0. Any required exception will be specified already,
628 * and no memory will have been deallocated.
629 *
630 */
631static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000632build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000633{
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000634 int len = PyObject_Size(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000635 int i;
636
637 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000638 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000639 PyObject* elem = PySequence_GetItem(tuple, i);
640 int ok = elem != NULL;
641 long type = 0;
642 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000643
Fred Drakeff9ea482000-04-19 13:54:15 +0000644 if (ok)
645 ok = PySequence_Check(elem);
646 if (ok) {
647 PyObject *temp = PySequence_GetItem(elem, 0);
648 if (temp == NULL)
649 ok = 0;
650 else {
651 ok = PyInt_Check(temp);
652 if (ok)
653 type = PyInt_AS_LONG(temp);
654 Py_DECREF(temp);
655 }
656 }
657 if (!ok) {
658 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000659 Py_BuildValue("os", elem,
Fred Drakeff9ea482000-04-19 13:54:15 +0000660 "Illegal node construct."));
661 Py_XDECREF(elem);
662 return (0);
663 }
664 if (ISTERMINAL(type)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000665 int len = PyObject_Size(elem);
666 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000667
Fred Drake0ac9b072000-09-12 21:58:06 +0000668 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000669 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000670 return 0;
671 }
672 temp = PySequence_GetItem(elem, 1);
673 if (temp == NULL)
674 return 0;
675 if (!PyString_Check(temp)) {
676 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000677 "second item in terminal node must be a string,"
678 " found %s",
Fred Drake0ac9b072000-09-12 21:58:06 +0000679 ((PyTypeObject*)PyObject_Type(temp))->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000680 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000681 return 0;
682 }
683 if (len == 3) {
684 PyObject *o = PySequence_GetItem(elem, 2);
685 if (o != NULL) {
686 if (PyInt_Check(o))
687 *line_num = PyInt_AS_LONG(o);
688 else {
689 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000690 "third item in terminal node must be an"
691 " integer, found %s",
Fred Drake0ac9b072000-09-12 21:58:06 +0000692 ((PyTypeObject*)PyObject_Type(temp))->tp_name);
693 Py_DECREF(o);
694 Py_DECREF(temp);
695 return 0;
696 }
697 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000698 }
699 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000700 len = PyString_GET_SIZE(temp) + 1;
701 strn = (char *)PyMem_MALLOC(len);
702 if (strn != NULL)
703 (void) memcpy(strn, PyString_AS_STRING(temp), len);
704 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000705 }
706 else if (!ISNONTERMINAL(type)) {
707 /*
708 * It has to be one or the other; this is an error.
709 * Throw an exception.
710 */
711 PyErr_SetObject(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000712 Py_BuildValue("os", elem, "unknown node type."));
Fred Drakeff9ea482000-04-19 13:54:15 +0000713 Py_XDECREF(elem);
714 return (0);
715 }
716 PyNode_AddChild(root, type, strn, *line_num);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000717
Fred Drakeff9ea482000-04-19 13:54:15 +0000718 if (ISNONTERMINAL(type)) {
719 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000720
Fred Drakeff9ea482000-04-19 13:54:15 +0000721 if (new_child != build_node_children(elem, new_child, line_num)) {
722 Py_XDECREF(elem);
723 return (0);
724 }
725 }
726 else if (type == NEWLINE) { /* It's true: we increment the */
727 ++(*line_num); /* line number *after* the newline! */
728 }
729 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000730 }
731 return (root);
Fred Drakeff9ea482000-04-19 13:54:15 +0000732}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000733
734
735static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000736build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000737{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000738 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000739 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000740 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000741
Guido van Rossum47478871996-08-21 14:32:37 +0000742 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000743 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000744 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000745 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000746 /*
747 * The tuple is simple, but it doesn't start with a start symbol.
748 * Throw an exception now and be done with it.
749 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000750 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000751 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000752 PyErr_SetObject(parser_error, tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000753 }
754 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000755 /*
756 * Not efficient, but that can be handled later.
757 */
758 int line_num = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000759
Fred Drakeff9ea482000-04-19 13:54:15 +0000760 res = PyNode_New(num);
761 if (res != build_node_children(tuple, res, &line_num)) {
762 PyNode_Free(res);
763 res = 0;
764 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000765 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000766 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000767 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000768 * NONTERMINAL, we can't use it. Not sure the implementation
769 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000770 */
771 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000772 Py_BuildValue("os", tuple,
Fred Drakeff9ea482000-04-19 13:54:15 +0000773 "Illegal component tuple."));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000774
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000775 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000776}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000777
778
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000779/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000780 * Validation routines used within the validation section:
781 */
Fred Drakeff9ea482000-04-19 13:54:15 +0000782staticforward int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000783
Fred Drakeff9ea482000-04-19 13:54:15 +0000784#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
785#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
786#define validate_colon(ch) validate_terminal(ch, COLON, ":")
787#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
788#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
789#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
790#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
791#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
792#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
793#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
794#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
795#define validate_star(ch) validate_terminal(ch, STAR, "*")
796#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
797#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
798#define validate_dot(ch) validate_terminal(ch, DOT, ".")
799#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000800
Fred Drake0ac9b072000-09-12 21:58:06 +0000801#define VALIDATER(n) static int validate_##n(node *tree)
802
Fred Drakeff9ea482000-04-19 13:54:15 +0000803VALIDATER(node); VALIDATER(small_stmt);
804VALIDATER(class); VALIDATER(node);
805VALIDATER(parameters); VALIDATER(suite);
806VALIDATER(testlist); VALIDATER(varargslist);
807VALIDATER(fpdef); VALIDATER(fplist);
808VALIDATER(stmt); VALIDATER(simple_stmt);
809VALIDATER(expr_stmt); VALIDATER(power);
810VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000811VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000812VALIDATER(raise_stmt); VALIDATER(import_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000813VALIDATER(global_stmt); VALIDATER(list_if);
814VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000815VALIDATER(exec_stmt); VALIDATER(compound_stmt);
816VALIDATER(while); VALIDATER(for);
817VALIDATER(try); VALIDATER(except_clause);
818VALIDATER(test); VALIDATER(and_test);
819VALIDATER(not_test); VALIDATER(comparison);
820VALIDATER(comp_op); VALIDATER(expr);
821VALIDATER(xor_expr); VALIDATER(and_expr);
822VALIDATER(shift_expr); VALIDATER(arith_expr);
823VALIDATER(term); VALIDATER(factor);
824VALIDATER(atom); VALIDATER(lambdef);
825VALIDATER(trailer); VALIDATER(subscript);
826VALIDATER(subscriptlist); VALIDATER(sliceop);
827VALIDATER(exprlist); VALIDATER(dictmaker);
828VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000829VALIDATER(listmaker); VALIDATER(yield_stmt);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000830
Fred Drake0ac9b072000-09-12 21:58:06 +0000831#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000832
Fred Drakeff9ea482000-04-19 13:54:15 +0000833#define is_even(n) (((n) & 1) == 0)
834#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000835
836
837static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000838validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000839{
Fred Drake0ac9b072000-09-12 21:58:06 +0000840 if (TYPE(n) != t) {
841 PyErr_Format(parser_error, "Expected node type %d, got %d.",
842 t, TYPE(n));
843 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000844 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000845 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000846}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000847
848
Fred Drakee7ab64e2000-04-25 04:14:46 +0000849/* Verifies that the number of child nodes is exactly 'num', raising
850 * an exception if it isn't. The exception message does not indicate
851 * the exact number of nodes, allowing this to be used to raise the
852 * "right" exception when the wrong number of nodes is present in a
853 * specific variant of a statement's syntax. This is commonly used
854 * in that fashion.
855 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000856static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000857validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000858{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000859 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000860 PyErr_Format(parser_error,
861 "Illegal number of children for %s node.", name);
862 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000863 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000864 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000865}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000866
867
868static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000869validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000870{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000871 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000872 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000873
874 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000875 PyErr_Format(parser_error,
876 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000877 }
878 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000879}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000880
881
Guido van Rossum47478871996-08-21 14:32:37 +0000882/* X (',' X) [',']
883 */
884static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000885validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000886 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000887{
888 int nch = NCH(tree);
889 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000890 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000891
892 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000893 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000894 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000895 if (is_even(nch))
896 res = validate_comma(CHILD(tree, --nch));
897 if (res && nch > 1) {
898 int pos = 1;
899 for ( ; res && pos < nch; pos += 2)
900 res = (validate_comma(CHILD(tree, pos))
901 && vfunc(CHILD(tree, pos + 1)));
902 }
Guido van Rossum47478871996-08-21 14:32:37 +0000903 }
904 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000905}
Guido van Rossum47478871996-08-21 14:32:37 +0000906
907
Fred Drakecff283c2000-08-21 22:24:43 +0000908/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000909 *
910 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000911 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000912 */
Guido van Rossum47478871996-08-21 14:32:37 +0000913static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000914validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000915{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000916 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000917 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000918
Guido van Rossum3d602e31996-07-21 02:33:56 +0000919 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000920 res = (validate_name(CHILD(tree, 0), "class")
921 && validate_ntype(CHILD(tree, 1), NAME)
922 && validate_colon(CHILD(tree, nch - 2))
923 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000924 }
925 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000926 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000927 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000928 res = (validate_lparen(CHILD(tree, 2))
929 && validate_testlist(CHILD(tree, 3))
930 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000931 }
932 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000933}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000934
935
Guido van Rossum3d602e31996-07-21 02:33:56 +0000936/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +0000937 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +0000938 */
Guido van Rossum47478871996-08-21 14:32:37 +0000939static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000940validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000941{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000942 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000943 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +0000944 && (nch >= 4)
945 && validate_name(CHILD(tree, 0), "if")
946 && validate_test(CHILD(tree, 1))
947 && validate_colon(CHILD(tree, 2))
948 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000949
950 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000951 /* ... 'else' ':' suite */
952 res = (validate_name(CHILD(tree, nch - 3), "else")
953 && validate_colon(CHILD(tree, nch - 2))
954 && validate_suite(CHILD(tree, nch - 1)));
955 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000956 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000957 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000958 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000959 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +0000960 /* Will catch the case for nch < 4 */
961 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000962 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000963 /* ... ('elif' test ':' suite)+ ... */
964 int j = 4;
965 while ((j < nch) && res) {
966 res = (validate_name(CHILD(tree, j), "elif")
967 && validate_colon(CHILD(tree, j + 2))
968 && validate_test(CHILD(tree, j + 1))
969 && validate_suite(CHILD(tree, j + 3)));
970 j += 4;
971 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000972 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000973 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000974}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000975
976
Guido van Rossum3d602e31996-07-21 02:33:56 +0000977/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +0000978 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +0000979 *
980 */
Guido van Rossum47478871996-08-21 14:32:37 +0000981static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000982validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000983{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000984 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000985 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000986
Guido van Rossum3d602e31996-07-21 02:33:56 +0000987 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000988 res = (validate_lparen(CHILD(tree, 0))
989 && validate_rparen(CHILD(tree, nch - 1)));
990 if (res && (nch == 3))
991 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000992 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000993 else {
994 (void) validate_numnodes(tree, 2, "parameters");
995 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000996 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000997}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000998
999
Fred Drakecff283c2000-08-21 22:24:43 +00001000/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001001 *
1002 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001003 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001004 * | NEWLINE INDENT stmt+ DEDENT
1005 */
Guido van Rossum47478871996-08-21 14:32:37 +00001006static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001007validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001008{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001009 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001010 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001011
Guido van Rossum3d602e31996-07-21 02:33:56 +00001012 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001013 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001014 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001015 /* NEWLINE INDENT stmt+ DEDENT */
1016 res = (validate_newline(CHILD(tree, 0))
1017 && validate_indent(CHILD(tree, 1))
1018 && validate_stmt(CHILD(tree, 2))
1019 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001020
Fred Drakeff9ea482000-04-19 13:54:15 +00001021 if (res && (nch > 4)) {
1022 int i = 3;
1023 --nch; /* forget the DEDENT */
1024 for ( ; res && (i < nch); ++i)
1025 res = validate_stmt(CHILD(tree, i));
1026 }
1027 else if (nch < 4)
1028 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001029 }
1030 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001031}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001032
1033
Guido van Rossum47478871996-08-21 14:32:37 +00001034static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001035validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001036{
Guido van Rossum47478871996-08-21 14:32:37 +00001037 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001038 validate_test, "testlist"));
1039}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001040
1041
Guido van Rossum1c917072001-10-15 15:44:05 +00001042static int
1043validate_testlist_safe(node *tree)
1044{
1045 return (validate_repeating_list(tree, testlist_safe,
1046 validate_test, "testlist_safe"));
1047}
1048
1049
Fred Drakecff283c2000-08-21 22:24:43 +00001050/* '*' NAME [',' '**' NAME] | '**' NAME
1051 */
1052static int
1053validate_varargslist_trailer(node *tree, int start)
1054{
1055 int nch = NCH(tree);
1056 int res = 0;
1057 int sym;
1058
1059 if (nch <= start) {
1060 err_string("expected variable argument trailer for varargslist");
1061 return 0;
1062 }
1063 sym = TYPE(CHILD(tree, start));
1064 if (sym == STAR) {
1065 /*
1066 * ('*' NAME [',' '**' NAME]
1067 */
1068 if (nch-start == 2)
1069 res = validate_name(CHILD(tree, start+1), NULL);
1070 else if (nch-start == 5)
1071 res = (validate_name(CHILD(tree, start+1), NULL)
1072 && validate_comma(CHILD(tree, start+2))
1073 && validate_doublestar(CHILD(tree, start+3))
1074 && validate_name(CHILD(tree, start+4), NULL));
1075 }
1076 else if (sym == DOUBLESTAR) {
1077 /*
1078 * '**' NAME
1079 */
1080 if (nch-start == 2)
1081 res = validate_name(CHILD(tree, start+1), NULL);
1082 }
1083 if (!res)
1084 err_string("illegal variable argument trailer for varargslist");
1085 return res;
1086}
1087
1088
1089/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001090 *
1091 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001092 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001093 * ('*' NAME [',' '**' NAME]
1094 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001095 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1096 *
1097 */
Guido van Rossum47478871996-08-21 14:32:37 +00001098static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001099validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001100{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001101 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001102 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001103 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001104
Fred Drakeb6429a22000-12-11 22:08:27 +00001105 if (!res)
1106 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001107 if (nch < 1) {
1108 err_string("varargslist missing child nodes");
1109 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001110 }
Fred Drakecff283c2000-08-21 22:24:43 +00001111 sym = TYPE(CHILD(tree, 0));
1112 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001113 /* whole thing matches:
1114 * '*' NAME [',' '**' NAME] | '**' NAME
1115 */
Fred Drakecff283c2000-08-21 22:24:43 +00001116 res = validate_varargslist_trailer(tree, 0);
1117 else if (sym == fpdef) {
1118 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001119
Fred Drakecff283c2000-08-21 22:24:43 +00001120 sym = TYPE(CHILD(tree, nch-1));
1121 if (sym == NAME) {
1122 /*
1123 * (fpdef ['=' test] ',')+
1124 * ('*' NAME [',' '**' NAME]
1125 * | '**' NAME)
1126 */
1127 /* skip over (fpdef ['=' test] ',')+ */
1128 while (res && (i+2 <= nch)) {
1129 res = validate_fpdef(CHILD(tree, i));
1130 ++i;
1131 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1132 res = (validate_equal(CHILD(tree, i))
1133 && validate_test(CHILD(tree, i+1)));
1134 if (res)
1135 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001136 }
Fred Drakecff283c2000-08-21 22:24:43 +00001137 if (res && i < nch) {
1138 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001139 ++i;
1140 if (res && i < nch
1141 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1142 || TYPE(CHILD(tree, i)) == STAR))
1143 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001144 }
1145 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001146 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1147 * i --^^^
1148 */
Fred Drakecff283c2000-08-21 22:24:43 +00001149 if (res)
1150 res = validate_varargslist_trailer(tree, i);
1151 }
1152 else {
1153 /*
1154 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1155 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001156 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001157 if (sym == COMMA) {
1158 res = validate_comma(CHILD(tree, nch-1));
1159 if (!res)
1160 return 0;
1161 --nch;
1162 }
1163 /*
1164 * fpdef ['=' test] (',' fpdef ['=' test])*
1165 */
1166 res = validate_fpdef(CHILD(tree, 0));
1167 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001168 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1169 res = (validate_equal(CHILD(tree, i))
1170 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001171 i += 2;
1172 }
1173 /*
1174 * ... (',' fpdef ['=' test])*
1175 * i ---^^^
1176 */
1177 while (res && (nch - i) >= 2) {
1178 res = (validate_comma(CHILD(tree, i))
1179 && validate_fpdef(CHILD(tree, i+1)));
1180 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001181 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1182 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001183 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001184 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001185 }
1186 }
1187 if (res && nch - i != 0) {
1188 res = 0;
1189 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001190 }
1191 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001192 }
Fred Drakecff283c2000-08-21 22:24:43 +00001193 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001194}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001195
1196
Fred Drakecff283c2000-08-21 22:24:43 +00001197/* list_iter: list_for | list_if
1198 */
1199static int
1200validate_list_iter(node *tree)
1201{
1202 int res = (validate_ntype(tree, list_iter)
1203 && validate_numnodes(tree, 1, "list_iter"));
1204 if (res && TYPE(CHILD(tree, 0)) == list_for)
1205 res = validate_list_for(CHILD(tree, 0));
1206 else
1207 res = validate_list_if(CHILD(tree, 0));
1208
1209 return res;
1210}
1211
1212/* list_for: 'for' exprlist 'in' testlist [list_iter]
1213 */
1214static int
1215validate_list_for(node *tree)
1216{
1217 int nch = NCH(tree);
1218 int res;
1219
1220 if (nch == 5)
1221 res = validate_list_iter(CHILD(tree, 4));
1222 else
1223 res = validate_numnodes(tree, 4, "list_for");
1224
1225 if (res)
1226 res = (validate_name(CHILD(tree, 0), "for")
1227 && validate_exprlist(CHILD(tree, 1))
1228 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001229 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001230
1231 return res;
1232}
1233
1234/* list_if: 'if' test [list_iter]
1235 */
1236static int
1237validate_list_if(node *tree)
1238{
1239 int nch = NCH(tree);
1240 int res;
1241
1242 if (nch == 3)
1243 res = validate_list_iter(CHILD(tree, 2));
1244 else
1245 res = validate_numnodes(tree, 2, "list_if");
1246
1247 if (res)
1248 res = (validate_name(CHILD(tree, 0), "if")
1249 && validate_test(CHILD(tree, 1)));
1250
1251 return res;
1252}
1253
1254
1255/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001256 *
1257 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001258 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001259 * | '(' fplist ')'
1260 */
Guido van Rossum47478871996-08-21 14:32:37 +00001261static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001262validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001263{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001264 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001265 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001266
Guido van Rossum3d602e31996-07-21 02:33:56 +00001267 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001268 if (nch == 1)
1269 res = validate_ntype(CHILD(tree, 0), NAME);
1270 else if (nch == 3)
1271 res = (validate_lparen(CHILD(tree, 0))
1272 && validate_fplist(CHILD(tree, 1))
1273 && validate_rparen(CHILD(tree, 2)));
1274 else
1275 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001276 }
1277 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001278}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001279
1280
Guido van Rossum47478871996-08-21 14:32:37 +00001281static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001282validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001283{
Guido van Rossum47478871996-08-21 14:32:37 +00001284 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001285 validate_fpdef, "fplist"));
1286}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001287
1288
Guido van Rossum3d602e31996-07-21 02:33:56 +00001289/* simple_stmt | compound_stmt
1290 *
1291 */
Guido van Rossum47478871996-08-21 14:32:37 +00001292static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001293validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001294{
1295 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001296 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001297
Guido van Rossum3d602e31996-07-21 02:33:56 +00001298 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001299 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001300
Fred Drakeff9ea482000-04-19 13:54:15 +00001301 if (TYPE(tree) == simple_stmt)
1302 res = validate_simple_stmt(tree);
1303 else
1304 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001305 }
1306 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001307}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001308
1309
Guido van Rossum3d602e31996-07-21 02:33:56 +00001310/* small_stmt (';' small_stmt)* [';'] NEWLINE
1311 *
1312 */
Guido van Rossum47478871996-08-21 14:32:37 +00001313static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001314validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001315{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001316 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001317 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001318 && (nch >= 2)
1319 && validate_small_stmt(CHILD(tree, 0))
1320 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001321
Guido van Rossum3d602e31996-07-21 02:33:56 +00001322 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001323 res = validate_numnodes(tree, 2, "simple_stmt");
1324 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001325 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001326 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001327 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001328 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001329
Fred Drakeff9ea482000-04-19 13:54:15 +00001330 for (i = 1; res && (i < nch); i += 2)
1331 res = (validate_semi(CHILD(tree, i))
1332 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001333 }
1334 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001335}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001336
1337
Guido van Rossum47478871996-08-21 14:32:37 +00001338static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001339validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001340{
1341 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001342 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001343
Fred Drake0ac9b072000-09-12 21:58:06 +00001344 if (res) {
1345 int ntype = TYPE(CHILD(tree, 0));
1346
1347 if ( (ntype == expr_stmt)
1348 || (ntype == print_stmt)
1349 || (ntype == del_stmt)
1350 || (ntype == pass_stmt)
1351 || (ntype == flow_stmt)
1352 || (ntype == import_stmt)
1353 || (ntype == global_stmt)
1354 || (ntype == assert_stmt)
1355 || (ntype == exec_stmt))
1356 res = validate_node(CHILD(tree, 0));
1357 else {
1358 res = 0;
1359 err_string("illegal small_stmt child type");
1360 }
1361 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001362 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001363 res = 0;
1364 PyErr_Format(parser_error,
1365 "Unrecognized child node of small_stmt: %d.",
1366 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001367 }
1368 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001369}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001370
1371
1372/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001373 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001374 */
Guido van Rossum47478871996-08-21 14:32:37 +00001375static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001376validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001377{
1378 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001379 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001380 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001381
1382 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001383 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001384
1385 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001386 ntype = TYPE(tree);
1387 if ( (ntype == if_stmt)
1388 || (ntype == while_stmt)
1389 || (ntype == for_stmt)
1390 || (ntype == try_stmt)
1391 || (ntype == funcdef)
1392 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001393 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001394 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001395 res = 0;
1396 PyErr_Format(parser_error,
1397 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001398 }
1399 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001400}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001401
1402
Guido van Rossum47478871996-08-21 14:32:37 +00001403static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001404validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001405{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001406 int j;
1407 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001408 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001409 && is_odd(nch)
1410 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001411
Fred Drake28f739a2000-08-25 22:42:40 +00001412 if (res && nch == 3
1413 && TYPE(CHILD(tree, 1)) == augassign) {
1414 res = (validate_numnodes(CHILD(tree, 1), 1, "augassign")
1415 && validate_testlist(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001416
Fred Drake28f739a2000-08-25 22:42:40 +00001417 if (res) {
1418 char *s = STR(CHILD(CHILD(tree, 1), 0));
1419
1420 res = (strcmp(s, "+=") == 0
1421 || strcmp(s, "-=") == 0
1422 || strcmp(s, "*=") == 0
1423 || strcmp(s, "/=") == 0
1424 || strcmp(s, "%=") == 0
1425 || strcmp(s, "&=") == 0
1426 || strcmp(s, "|=") == 0
1427 || strcmp(s, "^=") == 0
1428 || strcmp(s, "<<=") == 0
1429 || strcmp(s, ">>=") == 0
1430 || strcmp(s, "**=") == 0);
1431 if (!res)
1432 err_string("illegal augmmented assignment operator");
1433 }
1434 }
1435 else {
1436 for (j = 1; res && (j < nch); j += 2)
1437 res = (validate_equal(CHILD(tree, j))
1438 && validate_testlist(CHILD(tree, j + 1)));
1439 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001440 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001441}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001442
1443
Guido van Rossum3d602e31996-07-21 02:33:56 +00001444/* print_stmt:
1445 *
Fred Drakecff283c2000-08-21 22:24:43 +00001446 * 'print' ( [ test (',' test)* [','] ]
1447 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001448 */
Guido van Rossum47478871996-08-21 14:32:37 +00001449static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001450validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001451{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001452 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001453 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001454 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001455 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001456
Fred Drakecff283c2000-08-21 22:24:43 +00001457 if (res && nch > 1) {
1458 int sym = TYPE(CHILD(tree, 1));
1459 int i = 1;
1460 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001461
Fred Drakecff283c2000-08-21 22:24:43 +00001462 if (sym == test)
1463 res = validate_test(CHILD(tree, i++));
1464 else {
1465 if (nch < 3)
1466 res = validate_numnodes(tree, 3, "print_stmt");
1467 else {
1468 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1469 && validate_test(CHILD(tree, i+1)));
1470 i += 2;
1471 allow_trailing_comma = 0;
1472 }
1473 }
1474 if (res) {
1475 /* ... (',' test)* [','] */
1476 while (res && i+2 <= nch) {
1477 res = (validate_comma(CHILD(tree, i))
1478 && validate_test(CHILD(tree, i+1)));
1479 allow_trailing_comma = 1;
1480 i += 2;
1481 }
1482 if (res && !allow_trailing_comma)
1483 res = validate_numnodes(tree, i, "print_stmt");
1484 else if (res && i < nch)
1485 res = validate_comma(CHILD(tree, i));
1486 }
1487 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001488 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001489}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001490
1491
Guido van Rossum47478871996-08-21 14:32:37 +00001492static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001493validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001494{
1495 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001496 && validate_name(CHILD(tree, 0), "del")
1497 && validate_exprlist(CHILD(tree, 1)));
1498}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001499
1500
Guido van Rossum47478871996-08-21 14:32:37 +00001501static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001502validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001503{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001504 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001505 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001506 && ((nch == 1) || (nch == 2))
1507 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001508
Guido van Rossum3d602e31996-07-21 02:33:56 +00001509 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001510 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001511
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001512 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001513}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001514
1515
Guido van Rossum47478871996-08-21 14:32:37 +00001516static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001517validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001518{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001519 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001520 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001521 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001522
Guido van Rossum3d602e31996-07-21 02:33:56 +00001523 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001524 res = validate_name(CHILD(tree, 0), "raise");
1525 if (res && (nch >= 2))
1526 res = validate_test(CHILD(tree, 1));
1527 if (res && nch > 2) {
1528 res = (validate_comma(CHILD(tree, 2))
1529 && validate_test(CHILD(tree, 3)));
1530 if (res && (nch > 4))
1531 res = (validate_comma(CHILD(tree, 4))
1532 && validate_test(CHILD(tree, 5)));
1533 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001534 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001535 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001536 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001537 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001538 res = (validate_comma(CHILD(tree, 2))
1539 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001540
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001541 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001542}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001543
1544
Fred Drake02126f22001-07-17 02:59:15 +00001545/* yield_stmt: 'yield' testlist
1546 */
1547static int
1548validate_yield_stmt(node *tree)
1549{
1550 return (validate_ntype(tree, yield_stmt)
1551 && validate_numnodes(tree, 2, "yield_stmt")
1552 && validate_name(CHILD(tree, 0), "yield")
1553 && validate_testlist(CHILD(tree, 1)));
1554}
1555
1556
Fred Drakecff283c2000-08-21 22:24:43 +00001557static int
1558validate_import_as_name(node *tree)
1559{
1560 int nch = NCH(tree);
1561 int ok = validate_ntype(tree, import_as_name);
1562
1563 if (ok) {
1564 if (nch == 1)
1565 ok = validate_name(CHILD(tree, 0), NULL);
1566 else if (nch == 3)
1567 ok = (validate_name(CHILD(tree, 0), NULL)
1568 && validate_name(CHILD(tree, 1), "as")
1569 && validate_name(CHILD(tree, 2), NULL));
1570 else
1571 ok = validate_numnodes(tree, 3, "import_as_name");
1572 }
1573 return ok;
1574}
1575
1576
Fred Drake71137082001-01-07 05:59:59 +00001577/* dotted_name: NAME ("." NAME)*
1578 */
1579static int
1580validate_dotted_name(node *tree)
1581{
1582 int nch = NCH(tree);
1583 int res = (validate_ntype(tree, dotted_name)
1584 && is_odd(nch)
1585 && validate_name(CHILD(tree, 0), NULL));
1586 int i;
1587
1588 for (i = 1; res && (i < nch); i += 2) {
1589 res = (validate_dot(CHILD(tree, i))
1590 && validate_name(CHILD(tree, i+1), NULL));
1591 }
1592 return res;
1593}
1594
1595
Fred Drakecff283c2000-08-21 22:24:43 +00001596/* dotted_as_name: dotted_name [NAME NAME]
1597 */
1598static int
1599validate_dotted_as_name(node *tree)
1600{
1601 int nch = NCH(tree);
1602 int res = validate_ntype(tree, dotted_as_name);
1603
1604 if (res) {
1605 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001606 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001607 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001608 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001609 && validate_name(CHILD(tree, 1), "as")
1610 && validate_name(CHILD(tree, 2), NULL));
1611 else {
1612 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001613 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001614 }
1615 }
1616 return res;
1617}
1618
1619
Guido van Rossum3d602e31996-07-21 02:33:56 +00001620/* import_stmt:
1621 *
Fred Drakecff283c2000-08-21 22:24:43 +00001622 * 'import' dotted_as_name (',' dotted_as_name)*
1623 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001624 */
Guido van Rossum47478871996-08-21 14:32:37 +00001625static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001626validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001627{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001628 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001629 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001630 && (nch >= 2) && is_even(nch)
Fred Drakecff283c2000-08-21 22:24:43 +00001631 && validate_ntype(CHILD(tree, 0), NAME));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001632
1633 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001634 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001635
Fred Drakecff283c2000-08-21 22:24:43 +00001636 res = validate_dotted_as_name(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00001637 for (j = 2; res && (j < nch); j += 2)
1638 res = (validate_comma(CHILD(tree, j))
Fred Drake71137082001-01-07 05:59:59 +00001639 && validate_dotted_as_name(CHILD(tree, j + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001640 }
Fred Drakecff283c2000-08-21 22:24:43 +00001641 else if (res && (res = validate_name(CHILD(tree, 0), "from"))) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001642 res = ((nch >= 4) && is_even(nch)
Fred Drake71137082001-01-07 05:59:59 +00001643 && validate_dotted_name(CHILD(tree, 1))
1644 && validate_name(CHILD(tree, 2), "import"));
Fred Drakeff9ea482000-04-19 13:54:15 +00001645 if (nch == 4) {
Fred Drakecff283c2000-08-21 22:24:43 +00001646 if (TYPE(CHILD(tree, 3)) == import_as_name)
1647 res = validate_import_as_name(CHILD(tree, 3));
1648 else
1649 res = validate_star(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001650 }
1651 else {
Fred Drakecff283c2000-08-21 22:24:43 +00001652 /* 'from' dotted_name 'import' import_as_name
1653 * (',' import_as_name)+
1654 */
Fred Drakeff9ea482000-04-19 13:54:15 +00001655 int j;
Fred Drakecff283c2000-08-21 22:24:43 +00001656 res = validate_import_as_name(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001657 for (j = 4; res && (j < nch); j += 2)
1658 res = (validate_comma(CHILD(tree, j))
Fred Drakecff283c2000-08-21 22:24:43 +00001659 && validate_import_as_name(CHILD(tree, j + 1)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001660 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001661 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001662 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001663 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001664
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001665 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001666}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001667
1668
Guido van Rossum47478871996-08-21 14:32:37 +00001669static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001670validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001671{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001672 int j;
1673 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001674 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001675 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001676
Guido van Rossum3d602e31996-07-21 02:33:56 +00001677 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001678 res = (validate_name(CHILD(tree, 0), "global")
1679 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001680 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001681 res = (validate_comma(CHILD(tree, j))
1682 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001683
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001684 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001685}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001686
1687
Guido van Rossum3d602e31996-07-21 02:33:56 +00001688/* exec_stmt:
1689 *
1690 * 'exec' expr ['in' test [',' test]]
1691 */
Guido van Rossum47478871996-08-21 14:32:37 +00001692static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001693validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001694{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001695 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001696 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001697 && ((nch == 2) || (nch == 4) || (nch == 6))
1698 && validate_name(CHILD(tree, 0), "exec")
1699 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001700
Guido van Rossum3d602e31996-07-21 02:33:56 +00001701 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001702 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001703 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001704 res = (validate_name(CHILD(tree, 2), "in")
1705 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001706 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001707 res = (validate_comma(CHILD(tree, 4))
1708 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001709
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001710 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001711}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001712
1713
Guido van Rossum925e5471997-04-02 05:32:13 +00001714/* assert_stmt:
1715 *
1716 * 'assert' test [',' test]
1717 */
1718static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001719validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001720{
1721 int nch = NCH(tree);
1722 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001723 && ((nch == 2) || (nch == 4))
1724 && (validate_name(CHILD(tree, 0), "__assert__") ||
1725 validate_name(CHILD(tree, 0), "assert"))
1726 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001727
1728 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001729 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001730 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001731 res = (validate_comma(CHILD(tree, 2))
1732 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001733
1734 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001735}
Guido van Rossum925e5471997-04-02 05:32:13 +00001736
1737
Guido van Rossum47478871996-08-21 14:32:37 +00001738static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001739validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001740{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001741 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001742 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001743 && ((nch == 4) || (nch == 7))
1744 && validate_name(CHILD(tree, 0), "while")
1745 && validate_test(CHILD(tree, 1))
1746 && validate_colon(CHILD(tree, 2))
1747 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001748
Guido van Rossum3d602e31996-07-21 02:33:56 +00001749 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001750 res = (validate_name(CHILD(tree, 4), "else")
1751 && validate_colon(CHILD(tree, 5))
1752 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001753
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001754 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001755}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001756
1757
Guido van Rossum47478871996-08-21 14:32:37 +00001758static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001759validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001760{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001761 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001762 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001763 && ((nch == 6) || (nch == 9))
1764 && validate_name(CHILD(tree, 0), "for")
1765 && validate_exprlist(CHILD(tree, 1))
1766 && validate_name(CHILD(tree, 2), "in")
1767 && validate_testlist(CHILD(tree, 3))
1768 && validate_colon(CHILD(tree, 4))
1769 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001770
Guido van Rossum3d602e31996-07-21 02:33:56 +00001771 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001772 res = (validate_name(CHILD(tree, 6), "else")
1773 && validate_colon(CHILD(tree, 7))
1774 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001775
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001776 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001777}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001778
1779
Guido van Rossum3d602e31996-07-21 02:33:56 +00001780/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001781 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001782 * | 'try' ':' suite 'finally' ':' suite
1783 *
1784 */
Guido van Rossum47478871996-08-21 14:32:37 +00001785static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001786validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001787{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001788 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001789 int pos = 3;
1790 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001791 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001792
1793 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001794 res = (validate_name(CHILD(tree, 0), "try")
1795 && validate_colon(CHILD(tree, 1))
1796 && validate_suite(CHILD(tree, 2))
1797 && validate_colon(CHILD(tree, nch - 2))
1798 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001799 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001800 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001801 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1802 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001803
1804 PyErr_Format(parser_error,
1805 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001806 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001807 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001808 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001809 res = (validate_except_clause(CHILD(tree, pos))
1810 && validate_colon(CHILD(tree, pos + 1))
1811 && validate_suite(CHILD(tree, pos + 2)));
1812 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001813 }
1814 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001815 res = validate_ntype(CHILD(tree, pos), NAME);
1816 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1817 res = (validate_numnodes(tree, 6, "try/finally")
1818 && validate_colon(CHILD(tree, 4))
1819 && validate_suite(CHILD(tree, 5)));
1820 else if (res) {
1821 if (nch == (pos + 3)) {
1822 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1823 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1824 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001825 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001826 }
1827 else if (nch == (pos + 6)) {
1828 res = (validate_name(CHILD(tree, pos), "except")
1829 && validate_colon(CHILD(tree, pos + 1))
1830 && validate_suite(CHILD(tree, pos + 2))
1831 && validate_name(CHILD(tree, pos + 3), "else"));
1832 }
1833 else
1834 res = validate_numnodes(tree, pos + 3, "try/except");
1835 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001836 }
1837 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001838}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001839
1840
Guido van Rossum47478871996-08-21 14:32:37 +00001841static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001842validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001843{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001844 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001845 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001846 && ((nch == 1) || (nch == 2) || (nch == 4))
1847 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001848
Guido van Rossum3d602e31996-07-21 02:33:56 +00001849 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001850 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001851 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001852 res = (validate_comma(CHILD(tree, 2))
1853 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001854
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001855 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001856}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001857
1858
Guido van Rossum47478871996-08-21 14:32:37 +00001859static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001860validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001861{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001862 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001863 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001864
Guido van Rossum3d602e31996-07-21 02:33:56 +00001865 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001866 res = ((nch == 1)
1867 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001868 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001869 int pos;
1870 res = validate_and_test(CHILD(tree, 0));
1871 for (pos = 1; res && (pos < nch); pos += 2)
1872 res = (validate_name(CHILD(tree, pos), "or")
1873 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001874 }
1875 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001876}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001877
1878
Guido van Rossum47478871996-08-21 14:32:37 +00001879static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001880validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001881{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001882 int pos;
1883 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001884 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001885 && is_odd(nch)
1886 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001887
Guido van Rossum3d602e31996-07-21 02:33:56 +00001888 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001889 res = (validate_name(CHILD(tree, pos), "and")
1890 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001891
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001892 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001893}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001894
1895
Guido van Rossum47478871996-08-21 14:32:37 +00001896static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001897validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001898{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001899 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001900 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001901
Guido van Rossum3d602e31996-07-21 02:33:56 +00001902 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001903 if (nch == 2)
1904 res = (validate_name(CHILD(tree, 0), "not")
1905 && validate_not_test(CHILD(tree, 1)));
1906 else if (nch == 1)
1907 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001908 }
1909 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001910}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001911
1912
Guido van Rossum47478871996-08-21 14:32:37 +00001913static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001914validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001915{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001916 int pos;
1917 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001918 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001919 && is_odd(nch)
1920 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001921
Guido van Rossum3d602e31996-07-21 02:33:56 +00001922 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001923 res = (validate_comp_op(CHILD(tree, pos))
1924 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001925
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001926 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001927}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001928
1929
Guido van Rossum47478871996-08-21 14:32:37 +00001930static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001931validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001932{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001933 int res = 0;
1934 int nch = NCH(tree);
1935
Guido van Rossum3d602e31996-07-21 02:33:56 +00001936 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001937 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001938 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001939 /*
1940 * Only child will be a terminal with a well-defined symbolic name
1941 * or a NAME with a string of either 'is' or 'in'
1942 */
1943 tree = CHILD(tree, 0);
1944 switch (TYPE(tree)) {
1945 case LESS:
1946 case GREATER:
1947 case EQEQUAL:
1948 case EQUAL:
1949 case LESSEQUAL:
1950 case GREATEREQUAL:
1951 case NOTEQUAL:
1952 res = 1;
1953 break;
1954 case NAME:
1955 res = ((strcmp(STR(tree), "in") == 0)
1956 || (strcmp(STR(tree), "is") == 0));
1957 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001958 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00001959 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00001960 }
1961 break;
1962 default:
Fred Drake661ea262000-10-24 19:57:45 +00001963 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001964 break;
1965 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001966 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00001967 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001968 res = (validate_ntype(CHILD(tree, 0), NAME)
1969 && validate_ntype(CHILD(tree, 1), NAME)
1970 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
1971 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
1972 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
1973 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
1974 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001975 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001976 }
1977 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001978}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001979
1980
Guido van Rossum47478871996-08-21 14:32:37 +00001981static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001982validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001983{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001984 int j;
1985 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001986 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001987 && is_odd(nch)
1988 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001989
Guido van Rossum3d602e31996-07-21 02:33:56 +00001990 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001991 res = (validate_xor_expr(CHILD(tree, j))
1992 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001993
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001994 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001995}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001996
1997
Guido van Rossum47478871996-08-21 14:32:37 +00001998static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001999validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002000{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002001 int j;
2002 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002003 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002004 && is_odd(nch)
2005 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002006
Guido van Rossum3d602e31996-07-21 02:33:56 +00002007 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002008 res = (validate_circumflex(CHILD(tree, j - 1))
2009 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002010
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002011 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002012}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002013
2014
Guido van Rossum47478871996-08-21 14:32:37 +00002015static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002016validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002017{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002018 int pos;
2019 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002020 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002021 && is_odd(nch)
2022 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002023
Guido van Rossum3d602e31996-07-21 02:33:56 +00002024 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002025 res = (validate_ampersand(CHILD(tree, pos))
2026 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002027
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002028 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002029}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002030
2031
2032static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002033validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002034 {
2035 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002036 int nch = NCH(tree);
2037 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002038 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002039
Guido van Rossum3d602e31996-07-21 02:33:56 +00002040 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002041 if (TYPE(CHILD(tree, pos)) != op1)
2042 res = validate_ntype(CHILD(tree, pos), op2);
2043 if (res)
2044 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002045 }
2046 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002047}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002048
2049
Guido van Rossum47478871996-08-21 14:32:37 +00002050static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002051validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002052{
2053 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002054 && validate_chain_two_ops(tree, validate_arith_expr,
2055 LEFTSHIFT, RIGHTSHIFT));
2056}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002057
2058
Guido van Rossum47478871996-08-21 14:32:37 +00002059static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002060validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002061{
2062 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002063 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2064}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002065
2066
Guido van Rossum47478871996-08-21 14:32:37 +00002067static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002068validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002069{
2070 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002071 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002072 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002073 && is_odd(nch)
2074 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002075
Guido van Rossum3d602e31996-07-21 02:33:56 +00002076 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002077 res = (((TYPE(CHILD(tree, pos)) == STAR)
2078 || (TYPE(CHILD(tree, pos)) == SLASH)
2079 || (TYPE(CHILD(tree, pos)) == PERCENT))
2080 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002081
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002082 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002083}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002084
2085
Guido van Rossum3d602e31996-07-21 02:33:56 +00002086/* factor:
2087 *
2088 * factor: ('+'|'-'|'~') factor | power
2089 */
Guido van Rossum47478871996-08-21 14:32:37 +00002090static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002091validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002092{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002093 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002094 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002095 && (((nch == 2)
2096 && ((TYPE(CHILD(tree, 0)) == PLUS)
2097 || (TYPE(CHILD(tree, 0)) == MINUS)
2098 || (TYPE(CHILD(tree, 0)) == TILDE))
2099 && validate_factor(CHILD(tree, 1)))
2100 || ((nch == 1)
2101 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002102 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002103}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002104
2105
Guido van Rossum3d602e31996-07-21 02:33:56 +00002106/* power:
2107 *
2108 * power: atom trailer* ('**' factor)*
2109 */
Guido van Rossum47478871996-08-21 14:32:37 +00002110static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002111validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002112{
2113 int pos = 1;
2114 int nch = NCH(tree);
2115 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002116 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002117
2118 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002119 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002120 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002121 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002122 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002123 return (0);
2124 }
2125 for ( ; res && (pos < (nch - 1)); pos += 2)
2126 res = (validate_doublestar(CHILD(tree, pos))
2127 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002128 }
2129 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002130}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002131
2132
Guido van Rossum47478871996-08-21 14:32:37 +00002133static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002134validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002135{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002136 int pos;
2137 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002138 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002139
Fred Drakecff283c2000-08-21 22:24:43 +00002140 if (res && nch < 1)
2141 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002142 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002143 switch (TYPE(CHILD(tree, 0))) {
2144 case LPAR:
2145 res = ((nch <= 3)
2146 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002147
Fred Drakeff9ea482000-04-19 13:54:15 +00002148 if (res && (nch == 3))
2149 res = validate_testlist(CHILD(tree, 1));
2150 break;
2151 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002152 if (nch == 2)
2153 res = validate_ntype(CHILD(tree, 1), RSQB);
2154 else if (nch == 3)
2155 res = (validate_listmaker(CHILD(tree, 1))
2156 && validate_ntype(CHILD(tree, 2), RSQB));
2157 else {
2158 res = 0;
2159 err_string("illegal list display atom");
2160 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002161 break;
2162 case LBRACE:
2163 res = ((nch <= 3)
2164 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002165
Fred Drakeff9ea482000-04-19 13:54:15 +00002166 if (res && (nch == 3))
2167 res = validate_dictmaker(CHILD(tree, 1));
2168 break;
2169 case BACKQUOTE:
2170 res = ((nch == 3)
2171 && validate_testlist(CHILD(tree, 1))
2172 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2173 break;
2174 case NAME:
2175 case NUMBER:
2176 res = (nch == 1);
2177 break;
2178 case STRING:
2179 for (pos = 1; res && (pos < nch); ++pos)
2180 res = validate_ntype(CHILD(tree, pos), STRING);
2181 break;
2182 default:
2183 res = 0;
2184 break;
2185 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002186 }
2187 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002188}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002189
2190
Fred Drake85bf3bb2000-08-23 15:35:26 +00002191/* listmaker:
2192 * test ( list_for | (',' test)* [','] )
2193 */
Fred Drakecff283c2000-08-21 22:24:43 +00002194static int
2195validate_listmaker(node *tree)
2196{
2197 int nch = NCH(tree);
2198 int ok = nch;
2199
2200 if (nch == 0)
2201 err_string("missing child nodes of listmaker");
2202 else
2203 ok = validate_test(CHILD(tree, 0));
2204
2205 /*
2206 * list_iter | (',' test)* [',']
2207 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002208 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2209 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002210 else {
2211 /* (',' test)* [','] */
2212 int i = 1;
2213 while (ok && nch - i >= 2) {
2214 ok = (validate_comma(CHILD(tree, i))
2215 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002216 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002217 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002218 if (ok && i == nch-1)
2219 ok = validate_comma(CHILD(tree, i));
2220 else if (i != nch) {
2221 ok = 0;
2222 err_string("illegal trailing nodes for listmaker");
2223 }
Fred Drakecff283c2000-08-21 22:24:43 +00002224 }
2225 return ok;
2226}
2227
2228
Guido van Rossum3d602e31996-07-21 02:33:56 +00002229/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002230 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002231 *
2232 */
Guido van Rossum47478871996-08-21 14:32:37 +00002233static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002234validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002235{
2236 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002237 && validate_numnodes(tree, 5, "funcdef")
2238 && validate_name(CHILD(tree, 0), "def")
2239 && validate_ntype(CHILD(tree, 1), NAME)
2240 && validate_colon(CHILD(tree, 3))
2241 && validate_parameters(CHILD(tree, 2))
2242 && validate_suite(CHILD(tree, 4)));
2243}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002244
2245
Guido van Rossum47478871996-08-21 14:32:37 +00002246static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002247validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002248{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002249 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002250 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002251 && ((nch == 3) || (nch == 4))
2252 && validate_name(CHILD(tree, 0), "lambda")
2253 && validate_colon(CHILD(tree, nch - 2))
2254 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002255
Guido van Rossum3d602e31996-07-21 02:33:56 +00002256 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002257 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002258 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002259 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002260
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002261 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002262}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002263
2264
Guido van Rossum3d602e31996-07-21 02:33:56 +00002265/* arglist:
2266 *
Fred Drakecff283c2000-08-21 22:24:43 +00002267 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002268 */
Guido van Rossum47478871996-08-21 14:32:37 +00002269static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002270validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002271{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002272 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002273 int i = 0;
2274 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002275
2276 if (nch <= 0)
2277 /* raise the right error from having an invalid number of children */
2278 return validate_numnodes(tree, nch + 1, "arglist");
2279
Fred Drakecff283c2000-08-21 22:24:43 +00002280 while (ok && nch-i >= 2) {
2281 /* skip leading (argument ',') */
2282 ok = (validate_argument(CHILD(tree, i))
2283 && validate_comma(CHILD(tree, i+1)));
2284 if (ok)
2285 i += 2;
2286 else
2287 PyErr_Clear();
2288 }
2289 ok = 1;
2290 if (nch-i > 0) {
2291 /*
2292 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002293 */
Fred Drakecff283c2000-08-21 22:24:43 +00002294 int sym = TYPE(CHILD(tree, i));
2295
2296 if (sym == argument) {
2297 ok = validate_argument(CHILD(tree, i));
2298 if (ok && i+1 != nch) {
2299 err_string("illegal arglist specification"
2300 " (extra stuff on end)");
2301 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002302 }
Fred Drakecff283c2000-08-21 22:24:43 +00002303 }
2304 else if (sym == STAR) {
2305 ok = validate_star(CHILD(tree, i));
2306 if (ok && (nch-i == 2))
2307 ok = validate_test(CHILD(tree, i+1));
2308 else if (ok && (nch-i == 5))
2309 ok = (validate_test(CHILD(tree, i+1))
2310 && validate_comma(CHILD(tree, i+2))
2311 && validate_doublestar(CHILD(tree, i+3))
2312 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002313 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002314 err_string("illegal use of '*' in arglist");
2315 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002316 }
2317 }
Fred Drakecff283c2000-08-21 22:24:43 +00002318 else if (sym == DOUBLESTAR) {
2319 if (nch-i == 2)
2320 ok = (validate_doublestar(CHILD(tree, i))
2321 && validate_test(CHILD(tree, i+1)));
2322 else {
2323 err_string("illegal use of '**' in arglist");
2324 ok = 0;
2325 }
2326 }
2327 else {
2328 err_string("illegal arglist specification");
2329 ok = 0;
2330 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002331 }
2332 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002333}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002334
2335
2336
2337/* argument:
2338 *
2339 * [test '='] test
2340 */
Guido van Rossum47478871996-08-21 14:32:37 +00002341static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002342validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002343{
2344 int nch = NCH(tree);
2345 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002346 && ((nch == 1) || (nch == 3))
2347 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002348
2349 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002350 res = (validate_equal(CHILD(tree, 1))
2351 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002352
2353 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002354}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002355
2356
2357
2358/* trailer:
2359 *
Guido van Rossum47478871996-08-21 14:32:37 +00002360 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002361 */
Guido van Rossum47478871996-08-21 14:32:37 +00002362static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002363validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002364{
2365 int nch = NCH(tree);
2366 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002367
2368 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002369 switch (TYPE(CHILD(tree, 0))) {
2370 case LPAR:
2371 res = validate_rparen(CHILD(tree, nch - 1));
2372 if (res && (nch == 3))
2373 res = validate_arglist(CHILD(tree, 1));
2374 break;
2375 case LSQB:
2376 res = (validate_numnodes(tree, 3, "trailer")
2377 && validate_subscriptlist(CHILD(tree, 1))
2378 && validate_ntype(CHILD(tree, 2), RSQB));
2379 break;
2380 case DOT:
2381 res = (validate_numnodes(tree, 2, "trailer")
2382 && validate_ntype(CHILD(tree, 1), NAME));
2383 break;
2384 default:
2385 res = 0;
2386 break;
2387 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002388 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002389 else {
2390 (void) validate_numnodes(tree, 2, "trailer");
2391 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002392 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002393}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002394
2395
Guido van Rossum47478871996-08-21 14:32:37 +00002396/* subscriptlist:
2397 *
2398 * subscript (',' subscript)* [',']
2399 */
2400static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002401validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002402{
2403 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002404 validate_subscript, "subscriptlist"));
2405}
Guido van Rossum47478871996-08-21 14:32:37 +00002406
2407
Guido van Rossum3d602e31996-07-21 02:33:56 +00002408/* subscript:
2409 *
Guido van Rossum47478871996-08-21 14:32:37 +00002410 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002411 */
Guido van Rossum47478871996-08-21 14:32:37 +00002412static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002413validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002414{
Guido van Rossum47478871996-08-21 14:32:37 +00002415 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002416 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002417 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002418
Guido van Rossum47478871996-08-21 14:32:37 +00002419 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002420 if (!PyErr_Occurred())
2421 err_string("invalid number of arguments for subscript node");
2422 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002423 }
Guido van Rossum47478871996-08-21 14:32:37 +00002424 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002425 /* take care of ('.' '.' '.') possibility */
2426 return (validate_numnodes(tree, 3, "subscript")
2427 && validate_dot(CHILD(tree, 0))
2428 && validate_dot(CHILD(tree, 1))
2429 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002430 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002431 if (TYPE(CHILD(tree, 0)) == test)
2432 res = validate_test(CHILD(tree, 0));
2433 else
2434 res = validate_colon(CHILD(tree, 0));
2435 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002436 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002437 /* Must be [test] ':' [test] [sliceop],
2438 * but at least one of the optional components will
2439 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002440 */
2441 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002442 res = validate_test(CHILD(tree, 0));
2443 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002444 }
2445 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002446 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002447 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002448 int rem = nch - ++offset;
2449 if (rem) {
2450 if (TYPE(CHILD(tree, offset)) == test) {
2451 res = validate_test(CHILD(tree, offset));
2452 ++offset;
2453 --rem;
2454 }
2455 if (res && rem)
2456 res = validate_sliceop(CHILD(tree, offset));
2457 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002458 }
2459 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002460}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002461
2462
Guido van Rossum47478871996-08-21 14:32:37 +00002463static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002464validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002465{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002466 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002467 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002468 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002469 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002470 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002471 }
Guido van Rossum47478871996-08-21 14:32:37 +00002472 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002473 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002474 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002475 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002476
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002477 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002478}
Guido van Rossum47478871996-08-21 14:32:37 +00002479
2480
2481static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002482validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002483{
2484 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002485 validate_expr, "exprlist"));
2486}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002487
2488
Guido van Rossum47478871996-08-21 14:32:37 +00002489static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002490validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002491{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002492 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002493 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002494 && (nch >= 3)
2495 && validate_test(CHILD(tree, 0))
2496 && validate_colon(CHILD(tree, 1))
2497 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002498
Guido van Rossum3d602e31996-07-21 02:33:56 +00002499 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002500 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002501 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002502 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002503
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002504 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002505 int pos = 3;
2506 /* ( ',' test ':' test )* */
2507 while (res && (pos < nch)) {
2508 res = (validate_comma(CHILD(tree, pos))
2509 && validate_test(CHILD(tree, pos + 1))
2510 && validate_colon(CHILD(tree, pos + 2))
2511 && validate_test(CHILD(tree, pos + 3)));
2512 pos += 4;
2513 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002514 }
2515 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002516}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002517
2518
Guido van Rossum47478871996-08-21 14:32:37 +00002519static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002520validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002521{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002522 int pos;
2523 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002524 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002525 && (nch >= 2)
2526 && validate_testlist(CHILD(tree, 0))
2527 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002528
Guido van Rossum3d602e31996-07-21 02:33:56 +00002529 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002530 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002531
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002532 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002533}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002534
2535
Guido van Rossum47478871996-08-21 14:32:37 +00002536static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002537validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002538{
Fred Drakeff9ea482000-04-19 13:54:15 +00002539 int nch = 0; /* num. children on current node */
2540 int res = 1; /* result value */
2541 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002542
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002543 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002544 nch = NCH(tree);
2545 next = 0;
2546 switch (TYPE(tree)) {
2547 /*
2548 * Definition nodes.
2549 */
2550 case funcdef:
2551 res = validate_funcdef(tree);
2552 break;
2553 case classdef:
2554 res = validate_class(tree);
2555 break;
2556 /*
2557 * "Trivial" parse tree nodes.
2558 * (Why did I call these trivial?)
2559 */
2560 case stmt:
2561 res = validate_stmt(tree);
2562 break;
2563 case small_stmt:
2564 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002565 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002566 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2567 */
2568 res = validate_small_stmt(tree);
2569 break;
2570 case flow_stmt:
2571 res = (validate_numnodes(tree, 1, "flow_stmt")
2572 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2573 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002574 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002575 || (TYPE(CHILD(tree, 0)) == return_stmt)
2576 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2577 if (res)
2578 next = CHILD(tree, 0);
2579 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002580 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002581 break;
Fred Drake02126f22001-07-17 02:59:15 +00002582 case yield_stmt:
2583 res = validate_yield_stmt(tree);
2584 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002585 /*
2586 * Compound statements.
2587 */
2588 case simple_stmt:
2589 res = validate_simple_stmt(tree);
2590 break;
2591 case compound_stmt:
2592 res = validate_compound_stmt(tree);
2593 break;
2594 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002595 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002596 */
2597 case expr_stmt:
2598 res = validate_expr_stmt(tree);
2599 break;
2600 case print_stmt:
2601 res = validate_print_stmt(tree);
2602 break;
2603 case del_stmt:
2604 res = validate_del_stmt(tree);
2605 break;
2606 case pass_stmt:
2607 res = (validate_numnodes(tree, 1, "pass")
2608 && validate_name(CHILD(tree, 0), "pass"));
2609 break;
2610 case break_stmt:
2611 res = (validate_numnodes(tree, 1, "break")
2612 && validate_name(CHILD(tree, 0), "break"));
2613 break;
2614 case continue_stmt:
2615 res = (validate_numnodes(tree, 1, "continue")
2616 && validate_name(CHILD(tree, 0), "continue"));
2617 break;
2618 case return_stmt:
2619 res = validate_return_stmt(tree);
2620 break;
2621 case raise_stmt:
2622 res = validate_raise_stmt(tree);
2623 break;
2624 case import_stmt:
2625 res = validate_import_stmt(tree);
2626 break;
2627 case global_stmt:
2628 res = validate_global_stmt(tree);
2629 break;
2630 case exec_stmt:
2631 res = validate_exec_stmt(tree);
2632 break;
2633 case assert_stmt:
2634 res = validate_assert_stmt(tree);
2635 break;
2636 case if_stmt:
2637 res = validate_if(tree);
2638 break;
2639 case while_stmt:
2640 res = validate_while(tree);
2641 break;
2642 case for_stmt:
2643 res = validate_for(tree);
2644 break;
2645 case try_stmt:
2646 res = validate_try(tree);
2647 break;
2648 case suite:
2649 res = validate_suite(tree);
2650 break;
2651 /*
2652 * Expression nodes.
2653 */
2654 case testlist:
2655 res = validate_testlist(tree);
2656 break;
2657 case test:
2658 res = validate_test(tree);
2659 break;
2660 case and_test:
2661 res = validate_and_test(tree);
2662 break;
2663 case not_test:
2664 res = validate_not_test(tree);
2665 break;
2666 case comparison:
2667 res = validate_comparison(tree);
2668 break;
2669 case exprlist:
2670 res = validate_exprlist(tree);
2671 break;
2672 case comp_op:
2673 res = validate_comp_op(tree);
2674 break;
2675 case expr:
2676 res = validate_expr(tree);
2677 break;
2678 case xor_expr:
2679 res = validate_xor_expr(tree);
2680 break;
2681 case and_expr:
2682 res = validate_and_expr(tree);
2683 break;
2684 case shift_expr:
2685 res = validate_shift_expr(tree);
2686 break;
2687 case arith_expr:
2688 res = validate_arith_expr(tree);
2689 break;
2690 case term:
2691 res = validate_term(tree);
2692 break;
2693 case factor:
2694 res = validate_factor(tree);
2695 break;
2696 case power:
2697 res = validate_power(tree);
2698 break;
2699 case atom:
2700 res = validate_atom(tree);
2701 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002702
Fred Drakeff9ea482000-04-19 13:54:15 +00002703 default:
2704 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002705 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002706 res = 0;
2707 break;
2708 }
2709 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002710 }
2711 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002712}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002713
2714
Guido van Rossum47478871996-08-21 14:32:37 +00002715static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002716validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002717{
2718 int res = validate_eval_input(tree);
2719
2720 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002721 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002722
2723 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002724}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002725
2726
Guido van Rossum3d602e31996-07-21 02:33:56 +00002727/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002728 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002729 */
Guido van Rossum47478871996-08-21 14:32:37 +00002730static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002731validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002732{
Fred Drakec2683dd2001-07-17 19:32:05 +00002733 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002734 int nch = NCH(tree) - 1;
2735 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002736 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002737
Fred Drakec2683dd2001-07-17 19:32:05 +00002738 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002739 if (TYPE(CHILD(tree, j)) == stmt)
2740 res = validate_stmt(CHILD(tree, j));
2741 else
2742 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002743 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002744 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002745 * user. Hopefully, this won't be needed. If a user reports getting
2746 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002747 */
2748 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002749 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002750
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002751 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002752}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002753
2754
Fred Drake43f8f9b1998-04-13 16:25:46 +00002755static PyObject*
2756pickle_constructor = NULL;
2757
2758
2759static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002760parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002761{
Fred Drake268397f1998-04-29 14:16:32 +00002762 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002763 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00002764 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002765 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002766
Fred Drakec2683dd2001-07-17 19:32:05 +00002767 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002768 PyObject *newargs;
2769 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002770
Fred Drake2a6875e1999-09-20 22:32:18 +00002771 if ((empty_dict = PyDict_New()) == NULL)
2772 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002773 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002774 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002775 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002776 if (tuple != NULL) {
2777 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2778 Py_DECREF(tuple);
2779 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002780 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002781 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002782 }
2783 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002784 Py_XDECREF(empty_dict);
2785
Fred Drake43f8f9b1998-04-13 16:25:46 +00002786 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002787}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002788
2789
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002790/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00002791 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002792 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002793 * We'd really have to write a wrapper around it all anyway to allow
2794 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002795 */
2796static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00002797 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
2798 "Creates a tuple-tree representation of an ST."},
2799 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
2800 "Creates a list-tree representation of an ST."},
2801 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
2802 "Compiles an ST object into a code object."},
2803 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
2804 "Compiles an ST object into a code object."},
2805 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
2806 "Creates an ST object from an expression."},
2807 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
2808 "Determines if an ST object was created from an expression."},
2809 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
2810 "Determines if an ST object was created from a suite."},
2811 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
2812 "Creates an ST object from a suite."},
2813 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2814 "Creates an ST object from a tree representation."},
2815 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2816 "Creates an ST object from a tree representation."},
2817 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
2818 "Creates a tuple-tree representation of an ST."},
2819 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
2820 "Creates a list-tree representation of an ST."},
2821 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2822 "Creates an ST object from a tree representation."},
2823 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2824 "Creates an ST object from a tree representation."},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002825
Fred Drake43f8f9b1998-04-13 16:25:46 +00002826 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00002827 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Fred Drakec2683dd2001-07-17 19:32:05 +00002828 "Returns the pickle magic to allow ST objects to be pickled."},
Fred Drake43f8f9b1998-04-13 16:25:46 +00002829
Fred Drake268397f1998-04-29 14:16:32 +00002830 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002831 };
2832
2833
Tim Peters6d7c4422000-08-26 07:38:06 +00002834DL_EXPORT(void) initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00002835
Guido van Rossum3886bb61998-12-04 18:50:17 +00002836DL_EXPORT(void)
Thomas Wouters5c669862000-07-24 15:49:08 +00002837initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00002838{
Fred Drake13130bc2001-08-15 16:44:56 +00002839 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00002840
2841 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002842 module = Py_InitModule("parser", parser_functions);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002843
Fred Drake7a15ba51999-09-09 14:21:52 +00002844 if (parser_error == 0)
2845 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002846
2847 if ((parser_error == 0)
Fred Drake13130bc2001-08-15 16:44:56 +00002848 || (PyModule_AddObject(module, "ParserError", parser_error) != 0)) {
Fred Drakec2683dd2001-07-17 19:32:05 +00002849 /* caller will check PyErr_Occurred() */
2850 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002851 }
Fred Drakec2683dd2001-07-17 19:32:05 +00002852 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002853 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00002854 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002855 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002856
Fred Drake13130bc2001-08-15 16:44:56 +00002857 PyModule_AddStringConstant(module, "__copyright__",
2858 parser_copyright_string);
2859 PyModule_AddStringConstant(module, "__doc__",
2860 parser_doc_string);
2861 PyModule_AddStringConstant(module, "__version__",
2862 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002863
Fred Drake78bdb9b2001-07-19 20:17:15 +00002864 /* Register to support pickling.
2865 * If this fails, the import of this module will fail because an
2866 * exception will be raised here; should we clear the exception?
2867 */
Fred Drake13130bc2001-08-15 16:44:56 +00002868 copyreg = PyImport_ImportModule("copy_reg");
2869 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002870 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002871
Fred Drake13130bc2001-08-15 16:44:56 +00002872 func = PyObject_GetAttrString(copyreg, "pickle");
2873 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
2874 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00002875 Py_XINCREF(pickle_constructor);
2876 if ((func != NULL) && (pickle_constructor != NULL)
2877 && (pickler != NULL)) {
2878 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002879
Fred Drakec2683dd2001-07-17 19:32:05 +00002880 res = PyObject_CallFunction(func, "OOO", &PyST_Type, pickler,
2881 pickle_constructor);
Fred Drakeff9ea482000-04-19 13:54:15 +00002882 Py_XDECREF(res);
2883 }
2884 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00002885 Py_XDECREF(pickle_constructor);
2886 Py_XDECREF(pickler);
2887 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002888 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002889}