blob: bc82ce502ddf9700c41d8e0763a4b46449ce821f [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
Fred Drakecff283c2000-08-21 22:24:43 +00001042/* '*' NAME [',' '**' NAME] | '**' NAME
1043 */
1044static int
1045validate_varargslist_trailer(node *tree, int start)
1046{
1047 int nch = NCH(tree);
1048 int res = 0;
1049 int sym;
1050
1051 if (nch <= start) {
1052 err_string("expected variable argument trailer for varargslist");
1053 return 0;
1054 }
1055 sym = TYPE(CHILD(tree, start));
1056 if (sym == STAR) {
1057 /*
1058 * ('*' NAME [',' '**' NAME]
1059 */
1060 if (nch-start == 2)
1061 res = validate_name(CHILD(tree, start+1), NULL);
1062 else if (nch-start == 5)
1063 res = (validate_name(CHILD(tree, start+1), NULL)
1064 && validate_comma(CHILD(tree, start+2))
1065 && validate_doublestar(CHILD(tree, start+3))
1066 && validate_name(CHILD(tree, start+4), NULL));
1067 }
1068 else if (sym == DOUBLESTAR) {
1069 /*
1070 * '**' NAME
1071 */
1072 if (nch-start == 2)
1073 res = validate_name(CHILD(tree, start+1), NULL);
1074 }
1075 if (!res)
1076 err_string("illegal variable argument trailer for varargslist");
1077 return res;
1078}
1079
1080
1081/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001082 *
1083 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001084 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001085 * ('*' NAME [',' '**' NAME]
1086 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001087 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1088 *
1089 */
Guido van Rossum47478871996-08-21 14:32:37 +00001090static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001091validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001092{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001093 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001094 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001095 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001096
Fred Drakeb6429a22000-12-11 22:08:27 +00001097 if (!res)
1098 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001099 if (nch < 1) {
1100 err_string("varargslist missing child nodes");
1101 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001102 }
Fred Drakecff283c2000-08-21 22:24:43 +00001103 sym = TYPE(CHILD(tree, 0));
1104 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001105 /* whole thing matches:
1106 * '*' NAME [',' '**' NAME] | '**' NAME
1107 */
Fred Drakecff283c2000-08-21 22:24:43 +00001108 res = validate_varargslist_trailer(tree, 0);
1109 else if (sym == fpdef) {
1110 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001111
Fred Drakecff283c2000-08-21 22:24:43 +00001112 sym = TYPE(CHILD(tree, nch-1));
1113 if (sym == NAME) {
1114 /*
1115 * (fpdef ['=' test] ',')+
1116 * ('*' NAME [',' '**' NAME]
1117 * | '**' NAME)
1118 */
1119 /* skip over (fpdef ['=' test] ',')+ */
1120 while (res && (i+2 <= nch)) {
1121 res = validate_fpdef(CHILD(tree, i));
1122 ++i;
1123 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1124 res = (validate_equal(CHILD(tree, i))
1125 && validate_test(CHILD(tree, i+1)));
1126 if (res)
1127 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001128 }
Fred Drakecff283c2000-08-21 22:24:43 +00001129 if (res && i < nch) {
1130 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001131 ++i;
1132 if (res && i < nch
1133 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1134 || TYPE(CHILD(tree, i)) == STAR))
1135 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001136 }
1137 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001138 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1139 * i --^^^
1140 */
Fred Drakecff283c2000-08-21 22:24:43 +00001141 if (res)
1142 res = validate_varargslist_trailer(tree, i);
1143 }
1144 else {
1145 /*
1146 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1147 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001148 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001149 if (sym == COMMA) {
1150 res = validate_comma(CHILD(tree, nch-1));
1151 if (!res)
1152 return 0;
1153 --nch;
1154 }
1155 /*
1156 * fpdef ['=' test] (',' fpdef ['=' test])*
1157 */
1158 res = validate_fpdef(CHILD(tree, 0));
1159 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001160 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1161 res = (validate_equal(CHILD(tree, i))
1162 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001163 i += 2;
1164 }
1165 /*
1166 * ... (',' fpdef ['=' test])*
1167 * i ---^^^
1168 */
1169 while (res && (nch - i) >= 2) {
1170 res = (validate_comma(CHILD(tree, i))
1171 && validate_fpdef(CHILD(tree, i+1)));
1172 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001173 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1174 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001175 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001176 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001177 }
1178 }
1179 if (res && nch - i != 0) {
1180 res = 0;
1181 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001182 }
1183 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001184 }
Fred Drakecff283c2000-08-21 22:24:43 +00001185 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001186}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001187
1188
Fred Drakecff283c2000-08-21 22:24:43 +00001189/* list_iter: list_for | list_if
1190 */
1191static int
1192validate_list_iter(node *tree)
1193{
1194 int res = (validate_ntype(tree, list_iter)
1195 && validate_numnodes(tree, 1, "list_iter"));
1196 if (res && TYPE(CHILD(tree, 0)) == list_for)
1197 res = validate_list_for(CHILD(tree, 0));
1198 else
1199 res = validate_list_if(CHILD(tree, 0));
1200
1201 return res;
1202}
1203
1204/* list_for: 'for' exprlist 'in' testlist [list_iter]
1205 */
1206static int
1207validate_list_for(node *tree)
1208{
1209 int nch = NCH(tree);
1210 int res;
1211
1212 if (nch == 5)
1213 res = validate_list_iter(CHILD(tree, 4));
1214 else
1215 res = validate_numnodes(tree, 4, "list_for");
1216
1217 if (res)
1218 res = (validate_name(CHILD(tree, 0), "for")
1219 && validate_exprlist(CHILD(tree, 1))
1220 && validate_name(CHILD(tree, 2), "in")
1221 && validate_testlist(CHILD(tree, 3)));
1222
1223 return res;
1224}
1225
1226/* list_if: 'if' test [list_iter]
1227 */
1228static int
1229validate_list_if(node *tree)
1230{
1231 int nch = NCH(tree);
1232 int res;
1233
1234 if (nch == 3)
1235 res = validate_list_iter(CHILD(tree, 2));
1236 else
1237 res = validate_numnodes(tree, 2, "list_if");
1238
1239 if (res)
1240 res = (validate_name(CHILD(tree, 0), "if")
1241 && validate_test(CHILD(tree, 1)));
1242
1243 return res;
1244}
1245
1246
1247/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001248 *
1249 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001250 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001251 * | '(' fplist ')'
1252 */
Guido van Rossum47478871996-08-21 14:32:37 +00001253static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001254validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001255{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001256 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001257 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001258
Guido van Rossum3d602e31996-07-21 02:33:56 +00001259 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001260 if (nch == 1)
1261 res = validate_ntype(CHILD(tree, 0), NAME);
1262 else if (nch == 3)
1263 res = (validate_lparen(CHILD(tree, 0))
1264 && validate_fplist(CHILD(tree, 1))
1265 && validate_rparen(CHILD(tree, 2)));
1266 else
1267 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001268 }
1269 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001270}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001271
1272
Guido van Rossum47478871996-08-21 14:32:37 +00001273static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001274validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001275{
Guido van Rossum47478871996-08-21 14:32:37 +00001276 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001277 validate_fpdef, "fplist"));
1278}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001279
1280
Guido van Rossum3d602e31996-07-21 02:33:56 +00001281/* simple_stmt | compound_stmt
1282 *
1283 */
Guido van Rossum47478871996-08-21 14:32:37 +00001284static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001285validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001286{
1287 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001288 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001289
Guido van Rossum3d602e31996-07-21 02:33:56 +00001290 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001291 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001292
Fred Drakeff9ea482000-04-19 13:54:15 +00001293 if (TYPE(tree) == simple_stmt)
1294 res = validate_simple_stmt(tree);
1295 else
1296 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001297 }
1298 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001299}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001300
1301
Guido van Rossum3d602e31996-07-21 02:33:56 +00001302/* small_stmt (';' small_stmt)* [';'] NEWLINE
1303 *
1304 */
Guido van Rossum47478871996-08-21 14:32:37 +00001305static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001306validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001307{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001308 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001309 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001310 && (nch >= 2)
1311 && validate_small_stmt(CHILD(tree, 0))
1312 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001313
Guido van Rossum3d602e31996-07-21 02:33:56 +00001314 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001315 res = validate_numnodes(tree, 2, "simple_stmt");
1316 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001317 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001318 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001319 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001320 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001321
Fred Drakeff9ea482000-04-19 13:54:15 +00001322 for (i = 1; res && (i < nch); i += 2)
1323 res = (validate_semi(CHILD(tree, i))
1324 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001325 }
1326 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001327}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001328
1329
Guido van Rossum47478871996-08-21 14:32:37 +00001330static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001331validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001332{
1333 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001334 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001335
Fred Drake0ac9b072000-09-12 21:58:06 +00001336 if (res) {
1337 int ntype = TYPE(CHILD(tree, 0));
1338
1339 if ( (ntype == expr_stmt)
1340 || (ntype == print_stmt)
1341 || (ntype == del_stmt)
1342 || (ntype == pass_stmt)
1343 || (ntype == flow_stmt)
1344 || (ntype == import_stmt)
1345 || (ntype == global_stmt)
1346 || (ntype == assert_stmt)
1347 || (ntype == exec_stmt))
1348 res = validate_node(CHILD(tree, 0));
1349 else {
1350 res = 0;
1351 err_string("illegal small_stmt child type");
1352 }
1353 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001354 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001355 res = 0;
1356 PyErr_Format(parser_error,
1357 "Unrecognized child node of small_stmt: %d.",
1358 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001359 }
1360 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001361}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001362
1363
1364/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001365 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001366 */
Guido van Rossum47478871996-08-21 14:32:37 +00001367static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001368validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001369{
1370 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001371 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001372 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001373
1374 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001375 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001376
1377 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001378 ntype = TYPE(tree);
1379 if ( (ntype == if_stmt)
1380 || (ntype == while_stmt)
1381 || (ntype == for_stmt)
1382 || (ntype == try_stmt)
1383 || (ntype == funcdef)
1384 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001385 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001386 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001387 res = 0;
1388 PyErr_Format(parser_error,
1389 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001390 }
1391 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001392}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001393
1394
Guido van Rossum47478871996-08-21 14:32:37 +00001395static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001396validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001397{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001398 int j;
1399 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001400 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001401 && is_odd(nch)
1402 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001403
Fred Drake28f739a2000-08-25 22:42:40 +00001404 if (res && nch == 3
1405 && TYPE(CHILD(tree, 1)) == augassign) {
1406 res = (validate_numnodes(CHILD(tree, 1), 1, "augassign")
1407 && validate_testlist(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001408
Fred Drake28f739a2000-08-25 22:42:40 +00001409 if (res) {
1410 char *s = STR(CHILD(CHILD(tree, 1), 0));
1411
1412 res = (strcmp(s, "+=") == 0
1413 || strcmp(s, "-=") == 0
1414 || strcmp(s, "*=") == 0
1415 || strcmp(s, "/=") == 0
1416 || strcmp(s, "%=") == 0
1417 || strcmp(s, "&=") == 0
1418 || strcmp(s, "|=") == 0
1419 || strcmp(s, "^=") == 0
1420 || strcmp(s, "<<=") == 0
1421 || strcmp(s, ">>=") == 0
1422 || strcmp(s, "**=") == 0);
1423 if (!res)
1424 err_string("illegal augmmented assignment operator");
1425 }
1426 }
1427 else {
1428 for (j = 1; res && (j < nch); j += 2)
1429 res = (validate_equal(CHILD(tree, j))
1430 && validate_testlist(CHILD(tree, j + 1)));
1431 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001432 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001433}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001434
1435
Guido van Rossum3d602e31996-07-21 02:33:56 +00001436/* print_stmt:
1437 *
Fred Drakecff283c2000-08-21 22:24:43 +00001438 * 'print' ( [ test (',' test)* [','] ]
1439 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001440 */
Guido van Rossum47478871996-08-21 14:32:37 +00001441static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001442validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001443{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001444 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001445 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001446 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001447 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001448
Fred Drakecff283c2000-08-21 22:24:43 +00001449 if (res && nch > 1) {
1450 int sym = TYPE(CHILD(tree, 1));
1451 int i = 1;
1452 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001453
Fred Drakecff283c2000-08-21 22:24:43 +00001454 if (sym == test)
1455 res = validate_test(CHILD(tree, i++));
1456 else {
1457 if (nch < 3)
1458 res = validate_numnodes(tree, 3, "print_stmt");
1459 else {
1460 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1461 && validate_test(CHILD(tree, i+1)));
1462 i += 2;
1463 allow_trailing_comma = 0;
1464 }
1465 }
1466 if (res) {
1467 /* ... (',' test)* [','] */
1468 while (res && i+2 <= nch) {
1469 res = (validate_comma(CHILD(tree, i))
1470 && validate_test(CHILD(tree, i+1)));
1471 allow_trailing_comma = 1;
1472 i += 2;
1473 }
1474 if (res && !allow_trailing_comma)
1475 res = validate_numnodes(tree, i, "print_stmt");
1476 else if (res && i < nch)
1477 res = validate_comma(CHILD(tree, i));
1478 }
1479 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001480 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001481}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001482
1483
Guido van Rossum47478871996-08-21 14:32:37 +00001484static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001485validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001486{
1487 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001488 && validate_name(CHILD(tree, 0), "del")
1489 && validate_exprlist(CHILD(tree, 1)));
1490}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001491
1492
Guido van Rossum47478871996-08-21 14:32:37 +00001493static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001494validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001495{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001496 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001497 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001498 && ((nch == 1) || (nch == 2))
1499 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001500
Guido van Rossum3d602e31996-07-21 02:33:56 +00001501 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001502 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001503
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001504 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001505}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001506
1507
Guido van Rossum47478871996-08-21 14:32:37 +00001508static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001509validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001510{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001511 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001512 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001513 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001514
Guido van Rossum3d602e31996-07-21 02:33:56 +00001515 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001516 res = validate_name(CHILD(tree, 0), "raise");
1517 if (res && (nch >= 2))
1518 res = validate_test(CHILD(tree, 1));
1519 if (res && nch > 2) {
1520 res = (validate_comma(CHILD(tree, 2))
1521 && validate_test(CHILD(tree, 3)));
1522 if (res && (nch > 4))
1523 res = (validate_comma(CHILD(tree, 4))
1524 && validate_test(CHILD(tree, 5)));
1525 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001526 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001527 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001528 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001529 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001530 res = (validate_comma(CHILD(tree, 2))
1531 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001532
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001533 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001534}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001535
1536
Fred Drake02126f22001-07-17 02:59:15 +00001537/* yield_stmt: 'yield' testlist
1538 */
1539static int
1540validate_yield_stmt(node *tree)
1541{
1542 return (validate_ntype(tree, yield_stmt)
1543 && validate_numnodes(tree, 2, "yield_stmt")
1544 && validate_name(CHILD(tree, 0), "yield")
1545 && validate_testlist(CHILD(tree, 1)));
1546}
1547
1548
Fred Drakecff283c2000-08-21 22:24:43 +00001549static int
1550validate_import_as_name(node *tree)
1551{
1552 int nch = NCH(tree);
1553 int ok = validate_ntype(tree, import_as_name);
1554
1555 if (ok) {
1556 if (nch == 1)
1557 ok = validate_name(CHILD(tree, 0), NULL);
1558 else if (nch == 3)
1559 ok = (validate_name(CHILD(tree, 0), NULL)
1560 && validate_name(CHILD(tree, 1), "as")
1561 && validate_name(CHILD(tree, 2), NULL));
1562 else
1563 ok = validate_numnodes(tree, 3, "import_as_name");
1564 }
1565 return ok;
1566}
1567
1568
Fred Drake71137082001-01-07 05:59:59 +00001569/* dotted_name: NAME ("." NAME)*
1570 */
1571static int
1572validate_dotted_name(node *tree)
1573{
1574 int nch = NCH(tree);
1575 int res = (validate_ntype(tree, dotted_name)
1576 && is_odd(nch)
1577 && validate_name(CHILD(tree, 0), NULL));
1578 int i;
1579
1580 for (i = 1; res && (i < nch); i += 2) {
1581 res = (validate_dot(CHILD(tree, i))
1582 && validate_name(CHILD(tree, i+1), NULL));
1583 }
1584 return res;
1585}
1586
1587
Fred Drakecff283c2000-08-21 22:24:43 +00001588/* dotted_as_name: dotted_name [NAME NAME]
1589 */
1590static int
1591validate_dotted_as_name(node *tree)
1592{
1593 int nch = NCH(tree);
1594 int res = validate_ntype(tree, dotted_as_name);
1595
1596 if (res) {
1597 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001598 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001599 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001600 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001601 && validate_name(CHILD(tree, 1), "as")
1602 && validate_name(CHILD(tree, 2), NULL));
1603 else {
1604 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001605 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001606 }
1607 }
1608 return res;
1609}
1610
1611
Guido van Rossum3d602e31996-07-21 02:33:56 +00001612/* import_stmt:
1613 *
Fred Drakecff283c2000-08-21 22:24:43 +00001614 * 'import' dotted_as_name (',' dotted_as_name)*
1615 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001616 */
Guido van Rossum47478871996-08-21 14:32:37 +00001617static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001618validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001619{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001620 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001621 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001622 && (nch >= 2) && is_even(nch)
Fred Drakecff283c2000-08-21 22:24:43 +00001623 && validate_ntype(CHILD(tree, 0), NAME));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001624
1625 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001626 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001627
Fred Drakecff283c2000-08-21 22:24:43 +00001628 res = validate_dotted_as_name(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00001629 for (j = 2; res && (j < nch); j += 2)
1630 res = (validate_comma(CHILD(tree, j))
Fred Drake71137082001-01-07 05:59:59 +00001631 && validate_dotted_as_name(CHILD(tree, j + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001632 }
Fred Drakecff283c2000-08-21 22:24:43 +00001633 else if (res && (res = validate_name(CHILD(tree, 0), "from"))) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001634 res = ((nch >= 4) && is_even(nch)
Fred Drake71137082001-01-07 05:59:59 +00001635 && validate_dotted_name(CHILD(tree, 1))
1636 && validate_name(CHILD(tree, 2), "import"));
Fred Drakeff9ea482000-04-19 13:54:15 +00001637 if (nch == 4) {
Fred Drakecff283c2000-08-21 22:24:43 +00001638 if (TYPE(CHILD(tree, 3)) == import_as_name)
1639 res = validate_import_as_name(CHILD(tree, 3));
1640 else
1641 res = validate_star(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001642 }
1643 else {
Fred Drakecff283c2000-08-21 22:24:43 +00001644 /* 'from' dotted_name 'import' import_as_name
1645 * (',' import_as_name)+
1646 */
Fred Drakeff9ea482000-04-19 13:54:15 +00001647 int j;
Fred Drakecff283c2000-08-21 22:24:43 +00001648 res = validate_import_as_name(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001649 for (j = 4; res && (j < nch); j += 2)
1650 res = (validate_comma(CHILD(tree, j))
Fred Drakecff283c2000-08-21 22:24:43 +00001651 && validate_import_as_name(CHILD(tree, j + 1)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001652 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001653 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001654 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001655 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001656
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001657 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001658}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001659
1660
Guido van Rossum47478871996-08-21 14:32:37 +00001661static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001662validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001663{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001664 int j;
1665 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001666 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001667 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001668
Guido van Rossum3d602e31996-07-21 02:33:56 +00001669 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001670 res = (validate_name(CHILD(tree, 0), "global")
1671 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001672 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001673 res = (validate_comma(CHILD(tree, j))
1674 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001675
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001676 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001677}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001678
1679
Guido van Rossum3d602e31996-07-21 02:33:56 +00001680/* exec_stmt:
1681 *
1682 * 'exec' expr ['in' test [',' test]]
1683 */
Guido van Rossum47478871996-08-21 14:32:37 +00001684static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001685validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001686{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001687 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001688 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001689 && ((nch == 2) || (nch == 4) || (nch == 6))
1690 && validate_name(CHILD(tree, 0), "exec")
1691 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001692
Guido van Rossum3d602e31996-07-21 02:33:56 +00001693 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001694 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001695 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001696 res = (validate_name(CHILD(tree, 2), "in")
1697 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001698 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001699 res = (validate_comma(CHILD(tree, 4))
1700 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001701
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001702 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001703}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001704
1705
Guido van Rossum925e5471997-04-02 05:32:13 +00001706/* assert_stmt:
1707 *
1708 * 'assert' test [',' test]
1709 */
1710static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001711validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001712{
1713 int nch = NCH(tree);
1714 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001715 && ((nch == 2) || (nch == 4))
1716 && (validate_name(CHILD(tree, 0), "__assert__") ||
1717 validate_name(CHILD(tree, 0), "assert"))
1718 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001719
1720 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001721 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001722 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001723 res = (validate_comma(CHILD(tree, 2))
1724 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001725
1726 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001727}
Guido van Rossum925e5471997-04-02 05:32:13 +00001728
1729
Guido van Rossum47478871996-08-21 14:32:37 +00001730static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001731validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001732{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001733 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001734 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001735 && ((nch == 4) || (nch == 7))
1736 && validate_name(CHILD(tree, 0), "while")
1737 && validate_test(CHILD(tree, 1))
1738 && validate_colon(CHILD(tree, 2))
1739 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001740
Guido van Rossum3d602e31996-07-21 02:33:56 +00001741 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001742 res = (validate_name(CHILD(tree, 4), "else")
1743 && validate_colon(CHILD(tree, 5))
1744 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001745
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001746 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001747}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001748
1749
Guido van Rossum47478871996-08-21 14:32:37 +00001750static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001751validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001752{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001753 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001754 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001755 && ((nch == 6) || (nch == 9))
1756 && validate_name(CHILD(tree, 0), "for")
1757 && validate_exprlist(CHILD(tree, 1))
1758 && validate_name(CHILD(tree, 2), "in")
1759 && validate_testlist(CHILD(tree, 3))
1760 && validate_colon(CHILD(tree, 4))
1761 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001762
Guido van Rossum3d602e31996-07-21 02:33:56 +00001763 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001764 res = (validate_name(CHILD(tree, 6), "else")
1765 && validate_colon(CHILD(tree, 7))
1766 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001767
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001768 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001769}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001770
1771
Guido van Rossum3d602e31996-07-21 02:33:56 +00001772/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001773 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001774 * | 'try' ':' suite 'finally' ':' suite
1775 *
1776 */
Guido van Rossum47478871996-08-21 14:32:37 +00001777static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001778validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001779{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001780 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001781 int pos = 3;
1782 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001783 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001784
1785 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001786 res = (validate_name(CHILD(tree, 0), "try")
1787 && validate_colon(CHILD(tree, 1))
1788 && validate_suite(CHILD(tree, 2))
1789 && validate_colon(CHILD(tree, nch - 2))
1790 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001791 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001792 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001793 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1794 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001795
1796 PyErr_Format(parser_error,
1797 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001798 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001799 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001800 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001801 res = (validate_except_clause(CHILD(tree, pos))
1802 && validate_colon(CHILD(tree, pos + 1))
1803 && validate_suite(CHILD(tree, pos + 2)));
1804 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001805 }
1806 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001807 res = validate_ntype(CHILD(tree, pos), NAME);
1808 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1809 res = (validate_numnodes(tree, 6, "try/finally")
1810 && validate_colon(CHILD(tree, 4))
1811 && validate_suite(CHILD(tree, 5)));
1812 else if (res) {
1813 if (nch == (pos + 3)) {
1814 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1815 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1816 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001817 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001818 }
1819 else if (nch == (pos + 6)) {
1820 res = (validate_name(CHILD(tree, pos), "except")
1821 && validate_colon(CHILD(tree, pos + 1))
1822 && validate_suite(CHILD(tree, pos + 2))
1823 && validate_name(CHILD(tree, pos + 3), "else"));
1824 }
1825 else
1826 res = validate_numnodes(tree, pos + 3, "try/except");
1827 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001828 }
1829 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001830}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001831
1832
Guido van Rossum47478871996-08-21 14:32:37 +00001833static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001834validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001835{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001836 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001837 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001838 && ((nch == 1) || (nch == 2) || (nch == 4))
1839 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001840
Guido van Rossum3d602e31996-07-21 02:33:56 +00001841 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001842 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001843 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001844 res = (validate_comma(CHILD(tree, 2))
1845 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001846
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001847 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001848}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001849
1850
Guido van Rossum47478871996-08-21 14:32:37 +00001851static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001852validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001853{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001854 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001855 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001856
Guido van Rossum3d602e31996-07-21 02:33:56 +00001857 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001858 res = ((nch == 1)
1859 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001860 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001861 int pos;
1862 res = validate_and_test(CHILD(tree, 0));
1863 for (pos = 1; res && (pos < nch); pos += 2)
1864 res = (validate_name(CHILD(tree, pos), "or")
1865 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001866 }
1867 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001868}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001869
1870
Guido van Rossum47478871996-08-21 14:32:37 +00001871static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001872validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001873{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001874 int pos;
1875 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001876 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001877 && is_odd(nch)
1878 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001879
Guido van Rossum3d602e31996-07-21 02:33:56 +00001880 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001881 res = (validate_name(CHILD(tree, pos), "and")
1882 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001883
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001884 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001885}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001886
1887
Guido van Rossum47478871996-08-21 14:32:37 +00001888static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001889validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001890{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001891 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001892 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001893
Guido van Rossum3d602e31996-07-21 02:33:56 +00001894 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001895 if (nch == 2)
1896 res = (validate_name(CHILD(tree, 0), "not")
1897 && validate_not_test(CHILD(tree, 1)));
1898 else if (nch == 1)
1899 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001900 }
1901 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001902}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001903
1904
Guido van Rossum47478871996-08-21 14:32:37 +00001905static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001906validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001907{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001908 int pos;
1909 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001910 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001911 && is_odd(nch)
1912 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001913
Guido van Rossum3d602e31996-07-21 02:33:56 +00001914 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001915 res = (validate_comp_op(CHILD(tree, pos))
1916 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001917
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001918 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001919}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001920
1921
Guido van Rossum47478871996-08-21 14:32:37 +00001922static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001923validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001924{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001925 int res = 0;
1926 int nch = NCH(tree);
1927
Guido van Rossum3d602e31996-07-21 02:33:56 +00001928 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001929 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001930 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001931 /*
1932 * Only child will be a terminal with a well-defined symbolic name
1933 * or a NAME with a string of either 'is' or 'in'
1934 */
1935 tree = CHILD(tree, 0);
1936 switch (TYPE(tree)) {
1937 case LESS:
1938 case GREATER:
1939 case EQEQUAL:
1940 case EQUAL:
1941 case LESSEQUAL:
1942 case GREATEREQUAL:
1943 case NOTEQUAL:
1944 res = 1;
1945 break;
1946 case NAME:
1947 res = ((strcmp(STR(tree), "in") == 0)
1948 || (strcmp(STR(tree), "is") == 0));
1949 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001950 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00001951 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00001952 }
1953 break;
1954 default:
Fred Drake661ea262000-10-24 19:57:45 +00001955 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001956 break;
1957 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001958 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00001959 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001960 res = (validate_ntype(CHILD(tree, 0), NAME)
1961 && validate_ntype(CHILD(tree, 1), NAME)
1962 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
1963 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
1964 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
1965 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
1966 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001967 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001968 }
1969 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001970}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001971
1972
Guido van Rossum47478871996-08-21 14:32:37 +00001973static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001974validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001975{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001976 int j;
1977 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001978 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001979 && is_odd(nch)
1980 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001981
Guido van Rossum3d602e31996-07-21 02:33:56 +00001982 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001983 res = (validate_xor_expr(CHILD(tree, j))
1984 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001985
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001986 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001987}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001988
1989
Guido van Rossum47478871996-08-21 14:32:37 +00001990static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001991validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001992{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001993 int j;
1994 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001995 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001996 && is_odd(nch)
1997 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001998
Guido van Rossum3d602e31996-07-21 02:33:56 +00001999 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002000 res = (validate_circumflex(CHILD(tree, j - 1))
2001 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002002
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002003 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002004}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002005
2006
Guido van Rossum47478871996-08-21 14:32:37 +00002007static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002008validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002009{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002010 int pos;
2011 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002012 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002013 && is_odd(nch)
2014 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002015
Guido van Rossum3d602e31996-07-21 02:33:56 +00002016 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002017 res = (validate_ampersand(CHILD(tree, pos))
2018 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002019
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002020 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002021}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002022
2023
2024static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002025validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002026 {
2027 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002028 int nch = NCH(tree);
2029 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002030 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002031
Guido van Rossum3d602e31996-07-21 02:33:56 +00002032 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002033 if (TYPE(CHILD(tree, pos)) != op1)
2034 res = validate_ntype(CHILD(tree, pos), op2);
2035 if (res)
2036 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002037 }
2038 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002039}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002040
2041
Guido van Rossum47478871996-08-21 14:32:37 +00002042static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002043validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002044{
2045 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002046 && validate_chain_two_ops(tree, validate_arith_expr,
2047 LEFTSHIFT, RIGHTSHIFT));
2048}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002049
2050
Guido van Rossum47478871996-08-21 14:32:37 +00002051static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002052validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002053{
2054 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002055 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
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_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002061{
2062 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002063 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002064 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002065 && is_odd(nch)
2066 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002067
Guido van Rossum3d602e31996-07-21 02:33:56 +00002068 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002069 res = (((TYPE(CHILD(tree, pos)) == STAR)
2070 || (TYPE(CHILD(tree, pos)) == SLASH)
2071 || (TYPE(CHILD(tree, pos)) == PERCENT))
2072 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002073
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002074 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002075}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002076
2077
Guido van Rossum3d602e31996-07-21 02:33:56 +00002078/* factor:
2079 *
2080 * factor: ('+'|'-'|'~') factor | power
2081 */
Guido van Rossum47478871996-08-21 14:32:37 +00002082static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002083validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002084{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002085 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002086 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002087 && (((nch == 2)
2088 && ((TYPE(CHILD(tree, 0)) == PLUS)
2089 || (TYPE(CHILD(tree, 0)) == MINUS)
2090 || (TYPE(CHILD(tree, 0)) == TILDE))
2091 && validate_factor(CHILD(tree, 1)))
2092 || ((nch == 1)
2093 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002094 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002095}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002096
2097
Guido van Rossum3d602e31996-07-21 02:33:56 +00002098/* power:
2099 *
2100 * power: atom trailer* ('**' factor)*
2101 */
Guido van Rossum47478871996-08-21 14:32:37 +00002102static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002103validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002104{
2105 int pos = 1;
2106 int nch = NCH(tree);
2107 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002108 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002109
2110 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002111 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002112 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002113 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002114 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002115 return (0);
2116 }
2117 for ( ; res && (pos < (nch - 1)); pos += 2)
2118 res = (validate_doublestar(CHILD(tree, pos))
2119 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002120 }
2121 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002122}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002123
2124
Guido van Rossum47478871996-08-21 14:32:37 +00002125static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002126validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002127{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002128 int pos;
2129 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002130 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002131
Fred Drakecff283c2000-08-21 22:24:43 +00002132 if (res && nch < 1)
2133 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002134 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002135 switch (TYPE(CHILD(tree, 0))) {
2136 case LPAR:
2137 res = ((nch <= 3)
2138 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002139
Fred Drakeff9ea482000-04-19 13:54:15 +00002140 if (res && (nch == 3))
2141 res = validate_testlist(CHILD(tree, 1));
2142 break;
2143 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002144 if (nch == 2)
2145 res = validate_ntype(CHILD(tree, 1), RSQB);
2146 else if (nch == 3)
2147 res = (validate_listmaker(CHILD(tree, 1))
2148 && validate_ntype(CHILD(tree, 2), RSQB));
2149 else {
2150 res = 0;
2151 err_string("illegal list display atom");
2152 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002153 break;
2154 case LBRACE:
2155 res = ((nch <= 3)
2156 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002157
Fred Drakeff9ea482000-04-19 13:54:15 +00002158 if (res && (nch == 3))
2159 res = validate_dictmaker(CHILD(tree, 1));
2160 break;
2161 case BACKQUOTE:
2162 res = ((nch == 3)
2163 && validate_testlist(CHILD(tree, 1))
2164 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2165 break;
2166 case NAME:
2167 case NUMBER:
2168 res = (nch == 1);
2169 break;
2170 case STRING:
2171 for (pos = 1; res && (pos < nch); ++pos)
2172 res = validate_ntype(CHILD(tree, pos), STRING);
2173 break;
2174 default:
2175 res = 0;
2176 break;
2177 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002178 }
2179 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002180}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002181
2182
Fred Drake85bf3bb2000-08-23 15:35:26 +00002183/* listmaker:
2184 * test ( list_for | (',' test)* [','] )
2185 */
Fred Drakecff283c2000-08-21 22:24:43 +00002186static int
2187validate_listmaker(node *tree)
2188{
2189 int nch = NCH(tree);
2190 int ok = nch;
2191
2192 if (nch == 0)
2193 err_string("missing child nodes of listmaker");
2194 else
2195 ok = validate_test(CHILD(tree, 0));
2196
2197 /*
2198 * list_iter | (',' test)* [',']
2199 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002200 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2201 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002202 else {
2203 /* (',' test)* [','] */
2204 int i = 1;
2205 while (ok && nch - i >= 2) {
2206 ok = (validate_comma(CHILD(tree, i))
2207 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002208 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002209 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002210 if (ok && i == nch-1)
2211 ok = validate_comma(CHILD(tree, i));
2212 else if (i != nch) {
2213 ok = 0;
2214 err_string("illegal trailing nodes for listmaker");
2215 }
Fred Drakecff283c2000-08-21 22:24:43 +00002216 }
2217 return ok;
2218}
2219
2220
Guido van Rossum3d602e31996-07-21 02:33:56 +00002221/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002222 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002223 *
2224 */
Guido van Rossum47478871996-08-21 14:32:37 +00002225static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002226validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002227{
2228 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002229 && validate_numnodes(tree, 5, "funcdef")
2230 && validate_name(CHILD(tree, 0), "def")
2231 && validate_ntype(CHILD(tree, 1), NAME)
2232 && validate_colon(CHILD(tree, 3))
2233 && validate_parameters(CHILD(tree, 2))
2234 && validate_suite(CHILD(tree, 4)));
2235}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002236
2237
Guido van Rossum47478871996-08-21 14:32:37 +00002238static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002239validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002240{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002241 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002242 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002243 && ((nch == 3) || (nch == 4))
2244 && validate_name(CHILD(tree, 0), "lambda")
2245 && validate_colon(CHILD(tree, nch - 2))
2246 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002247
Guido van Rossum3d602e31996-07-21 02:33:56 +00002248 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002249 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002250 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002251 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002252
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002253 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002254}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002255
2256
Guido van Rossum3d602e31996-07-21 02:33:56 +00002257/* arglist:
2258 *
Fred Drakecff283c2000-08-21 22:24:43 +00002259 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002260 */
Guido van Rossum47478871996-08-21 14:32:37 +00002261static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002262validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002263{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002264 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002265 int i = 0;
2266 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002267
2268 if (nch <= 0)
2269 /* raise the right error from having an invalid number of children */
2270 return validate_numnodes(tree, nch + 1, "arglist");
2271
Fred Drakecff283c2000-08-21 22:24:43 +00002272 while (ok && nch-i >= 2) {
2273 /* skip leading (argument ',') */
2274 ok = (validate_argument(CHILD(tree, i))
2275 && validate_comma(CHILD(tree, i+1)));
2276 if (ok)
2277 i += 2;
2278 else
2279 PyErr_Clear();
2280 }
2281 ok = 1;
2282 if (nch-i > 0) {
2283 /*
2284 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002285 */
Fred Drakecff283c2000-08-21 22:24:43 +00002286 int sym = TYPE(CHILD(tree, i));
2287
2288 if (sym == argument) {
2289 ok = validate_argument(CHILD(tree, i));
2290 if (ok && i+1 != nch) {
2291 err_string("illegal arglist specification"
2292 " (extra stuff on end)");
2293 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002294 }
Fred Drakecff283c2000-08-21 22:24:43 +00002295 }
2296 else if (sym == STAR) {
2297 ok = validate_star(CHILD(tree, i));
2298 if (ok && (nch-i == 2))
2299 ok = validate_test(CHILD(tree, i+1));
2300 else if (ok && (nch-i == 5))
2301 ok = (validate_test(CHILD(tree, i+1))
2302 && validate_comma(CHILD(tree, i+2))
2303 && validate_doublestar(CHILD(tree, i+3))
2304 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002305 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002306 err_string("illegal use of '*' in arglist");
2307 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002308 }
2309 }
Fred Drakecff283c2000-08-21 22:24:43 +00002310 else if (sym == DOUBLESTAR) {
2311 if (nch-i == 2)
2312 ok = (validate_doublestar(CHILD(tree, i))
2313 && validate_test(CHILD(tree, i+1)));
2314 else {
2315 err_string("illegal use of '**' in arglist");
2316 ok = 0;
2317 }
2318 }
2319 else {
2320 err_string("illegal arglist specification");
2321 ok = 0;
2322 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002323 }
2324 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002325}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002326
2327
2328
2329/* argument:
2330 *
2331 * [test '='] test
2332 */
Guido van Rossum47478871996-08-21 14:32:37 +00002333static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002334validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002335{
2336 int nch = NCH(tree);
2337 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002338 && ((nch == 1) || (nch == 3))
2339 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002340
2341 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002342 res = (validate_equal(CHILD(tree, 1))
2343 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002344
2345 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002346}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002347
2348
2349
2350/* trailer:
2351 *
Guido van Rossum47478871996-08-21 14:32:37 +00002352 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002353 */
Guido van Rossum47478871996-08-21 14:32:37 +00002354static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002355validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002356{
2357 int nch = NCH(tree);
2358 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002359
2360 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002361 switch (TYPE(CHILD(tree, 0))) {
2362 case LPAR:
2363 res = validate_rparen(CHILD(tree, nch - 1));
2364 if (res && (nch == 3))
2365 res = validate_arglist(CHILD(tree, 1));
2366 break;
2367 case LSQB:
2368 res = (validate_numnodes(tree, 3, "trailer")
2369 && validate_subscriptlist(CHILD(tree, 1))
2370 && validate_ntype(CHILD(tree, 2), RSQB));
2371 break;
2372 case DOT:
2373 res = (validate_numnodes(tree, 2, "trailer")
2374 && validate_ntype(CHILD(tree, 1), NAME));
2375 break;
2376 default:
2377 res = 0;
2378 break;
2379 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002380 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002381 else {
2382 (void) validate_numnodes(tree, 2, "trailer");
2383 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002384 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002385}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002386
2387
Guido van Rossum47478871996-08-21 14:32:37 +00002388/* subscriptlist:
2389 *
2390 * subscript (',' subscript)* [',']
2391 */
2392static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002393validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002394{
2395 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002396 validate_subscript, "subscriptlist"));
2397}
Guido van Rossum47478871996-08-21 14:32:37 +00002398
2399
Guido van Rossum3d602e31996-07-21 02:33:56 +00002400/* subscript:
2401 *
Guido van Rossum47478871996-08-21 14:32:37 +00002402 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002403 */
Guido van Rossum47478871996-08-21 14:32:37 +00002404static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002405validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002406{
Guido van Rossum47478871996-08-21 14:32:37 +00002407 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002408 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002409 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002410
Guido van Rossum47478871996-08-21 14:32:37 +00002411 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002412 if (!PyErr_Occurred())
2413 err_string("invalid number of arguments for subscript node");
2414 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002415 }
Guido van Rossum47478871996-08-21 14:32:37 +00002416 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002417 /* take care of ('.' '.' '.') possibility */
2418 return (validate_numnodes(tree, 3, "subscript")
2419 && validate_dot(CHILD(tree, 0))
2420 && validate_dot(CHILD(tree, 1))
2421 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002422 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002423 if (TYPE(CHILD(tree, 0)) == test)
2424 res = validate_test(CHILD(tree, 0));
2425 else
2426 res = validate_colon(CHILD(tree, 0));
2427 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002428 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002429 /* Must be [test] ':' [test] [sliceop],
2430 * but at least one of the optional components will
2431 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002432 */
2433 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002434 res = validate_test(CHILD(tree, 0));
2435 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002436 }
2437 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002438 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002439 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002440 int rem = nch - ++offset;
2441 if (rem) {
2442 if (TYPE(CHILD(tree, offset)) == test) {
2443 res = validate_test(CHILD(tree, offset));
2444 ++offset;
2445 --rem;
2446 }
2447 if (res && rem)
2448 res = validate_sliceop(CHILD(tree, offset));
2449 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002450 }
2451 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002452}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002453
2454
Guido van Rossum47478871996-08-21 14:32:37 +00002455static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002456validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002457{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002458 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002459 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002460 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002461 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002462 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002463 }
Guido van Rossum47478871996-08-21 14:32:37 +00002464 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002465 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002466 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002467 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002468
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002469 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002470}
Guido van Rossum47478871996-08-21 14:32:37 +00002471
2472
2473static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002474validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002475{
2476 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002477 validate_expr, "exprlist"));
2478}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002479
2480
Guido van Rossum47478871996-08-21 14:32:37 +00002481static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002482validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002483{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002484 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002485 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002486 && (nch >= 3)
2487 && validate_test(CHILD(tree, 0))
2488 && validate_colon(CHILD(tree, 1))
2489 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002490
Guido van Rossum3d602e31996-07-21 02:33:56 +00002491 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002492 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002493 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002494 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002495
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002496 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002497 int pos = 3;
2498 /* ( ',' test ':' test )* */
2499 while (res && (pos < nch)) {
2500 res = (validate_comma(CHILD(tree, pos))
2501 && validate_test(CHILD(tree, pos + 1))
2502 && validate_colon(CHILD(tree, pos + 2))
2503 && validate_test(CHILD(tree, pos + 3)));
2504 pos += 4;
2505 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002506 }
2507 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002508}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002509
2510
Guido van Rossum47478871996-08-21 14:32:37 +00002511static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002512validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002513{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002514 int pos;
2515 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002516 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002517 && (nch >= 2)
2518 && validate_testlist(CHILD(tree, 0))
2519 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002520
Guido van Rossum3d602e31996-07-21 02:33:56 +00002521 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002522 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002523
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002524 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002525}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002526
2527
Guido van Rossum47478871996-08-21 14:32:37 +00002528static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002529validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002530{
Fred Drakeff9ea482000-04-19 13:54:15 +00002531 int nch = 0; /* num. children on current node */
2532 int res = 1; /* result value */
2533 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002534
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002535 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002536 nch = NCH(tree);
2537 next = 0;
2538 switch (TYPE(tree)) {
2539 /*
2540 * Definition nodes.
2541 */
2542 case funcdef:
2543 res = validate_funcdef(tree);
2544 break;
2545 case classdef:
2546 res = validate_class(tree);
2547 break;
2548 /*
2549 * "Trivial" parse tree nodes.
2550 * (Why did I call these trivial?)
2551 */
2552 case stmt:
2553 res = validate_stmt(tree);
2554 break;
2555 case small_stmt:
2556 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002557 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002558 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2559 */
2560 res = validate_small_stmt(tree);
2561 break;
2562 case flow_stmt:
2563 res = (validate_numnodes(tree, 1, "flow_stmt")
2564 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2565 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002566 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002567 || (TYPE(CHILD(tree, 0)) == return_stmt)
2568 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2569 if (res)
2570 next = CHILD(tree, 0);
2571 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002572 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002573 break;
Fred Drake02126f22001-07-17 02:59:15 +00002574 case yield_stmt:
2575 res = validate_yield_stmt(tree);
2576 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002577 /*
2578 * Compound statements.
2579 */
2580 case simple_stmt:
2581 res = validate_simple_stmt(tree);
2582 break;
2583 case compound_stmt:
2584 res = validate_compound_stmt(tree);
2585 break;
2586 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002587 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002588 */
2589 case expr_stmt:
2590 res = validate_expr_stmt(tree);
2591 break;
2592 case print_stmt:
2593 res = validate_print_stmt(tree);
2594 break;
2595 case del_stmt:
2596 res = validate_del_stmt(tree);
2597 break;
2598 case pass_stmt:
2599 res = (validate_numnodes(tree, 1, "pass")
2600 && validate_name(CHILD(tree, 0), "pass"));
2601 break;
2602 case break_stmt:
2603 res = (validate_numnodes(tree, 1, "break")
2604 && validate_name(CHILD(tree, 0), "break"));
2605 break;
2606 case continue_stmt:
2607 res = (validate_numnodes(tree, 1, "continue")
2608 && validate_name(CHILD(tree, 0), "continue"));
2609 break;
2610 case return_stmt:
2611 res = validate_return_stmt(tree);
2612 break;
2613 case raise_stmt:
2614 res = validate_raise_stmt(tree);
2615 break;
2616 case import_stmt:
2617 res = validate_import_stmt(tree);
2618 break;
2619 case global_stmt:
2620 res = validate_global_stmt(tree);
2621 break;
2622 case exec_stmt:
2623 res = validate_exec_stmt(tree);
2624 break;
2625 case assert_stmt:
2626 res = validate_assert_stmt(tree);
2627 break;
2628 case if_stmt:
2629 res = validate_if(tree);
2630 break;
2631 case while_stmt:
2632 res = validate_while(tree);
2633 break;
2634 case for_stmt:
2635 res = validate_for(tree);
2636 break;
2637 case try_stmt:
2638 res = validate_try(tree);
2639 break;
2640 case suite:
2641 res = validate_suite(tree);
2642 break;
2643 /*
2644 * Expression nodes.
2645 */
2646 case testlist:
2647 res = validate_testlist(tree);
2648 break;
2649 case test:
2650 res = validate_test(tree);
2651 break;
2652 case and_test:
2653 res = validate_and_test(tree);
2654 break;
2655 case not_test:
2656 res = validate_not_test(tree);
2657 break;
2658 case comparison:
2659 res = validate_comparison(tree);
2660 break;
2661 case exprlist:
2662 res = validate_exprlist(tree);
2663 break;
2664 case comp_op:
2665 res = validate_comp_op(tree);
2666 break;
2667 case expr:
2668 res = validate_expr(tree);
2669 break;
2670 case xor_expr:
2671 res = validate_xor_expr(tree);
2672 break;
2673 case and_expr:
2674 res = validate_and_expr(tree);
2675 break;
2676 case shift_expr:
2677 res = validate_shift_expr(tree);
2678 break;
2679 case arith_expr:
2680 res = validate_arith_expr(tree);
2681 break;
2682 case term:
2683 res = validate_term(tree);
2684 break;
2685 case factor:
2686 res = validate_factor(tree);
2687 break;
2688 case power:
2689 res = validate_power(tree);
2690 break;
2691 case atom:
2692 res = validate_atom(tree);
2693 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002694
Fred Drakeff9ea482000-04-19 13:54:15 +00002695 default:
2696 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002697 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002698 res = 0;
2699 break;
2700 }
2701 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002702 }
2703 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002704}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002705
2706
Guido van Rossum47478871996-08-21 14:32:37 +00002707static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002708validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002709{
2710 int res = validate_eval_input(tree);
2711
2712 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002713 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002714
2715 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002716}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002717
2718
Guido van Rossum3d602e31996-07-21 02:33:56 +00002719/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002720 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002721 */
Guido van Rossum47478871996-08-21 14:32:37 +00002722static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002723validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002724{
Fred Drakec2683dd2001-07-17 19:32:05 +00002725 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002726 int nch = NCH(tree) - 1;
2727 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002728 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002729
Fred Drakec2683dd2001-07-17 19:32:05 +00002730 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002731 if (TYPE(CHILD(tree, j)) == stmt)
2732 res = validate_stmt(CHILD(tree, j));
2733 else
2734 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002735 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002736 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002737 * user. Hopefully, this won't be needed. If a user reports getting
2738 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002739 */
2740 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002741 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002742
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002743 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002744}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002745
2746
Fred Drake43f8f9b1998-04-13 16:25:46 +00002747static PyObject*
2748pickle_constructor = NULL;
2749
2750
2751static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002752parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002753{
Fred Drake268397f1998-04-29 14:16:32 +00002754 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002755 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00002756 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002757 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002758
Fred Drakec2683dd2001-07-17 19:32:05 +00002759 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002760 PyObject *newargs;
2761 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002762
Fred Drake2a6875e1999-09-20 22:32:18 +00002763 if ((empty_dict = PyDict_New()) == NULL)
2764 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002765 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002766 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002767 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002768 if (tuple != NULL) {
2769 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2770 Py_DECREF(tuple);
2771 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002772 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002773 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002774 }
2775 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002776 Py_XDECREF(empty_dict);
2777
Fred Drake43f8f9b1998-04-13 16:25:46 +00002778 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002779}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002780
2781
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002782/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00002783 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002784 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002785 * We'd really have to write a wrapper around it all anyway to allow
2786 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002787 */
2788static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00002789 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
2790 "Creates a tuple-tree representation of an ST."},
2791 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
2792 "Creates a list-tree representation of an ST."},
2793 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
2794 "Compiles an ST object into a code object."},
2795 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
2796 "Compiles an ST object into a code object."},
2797 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
2798 "Creates an ST object from an expression."},
2799 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
2800 "Determines if an ST object was created from an expression."},
2801 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
2802 "Determines if an ST object was created from a suite."},
2803 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
2804 "Creates an ST object from a suite."},
2805 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2806 "Creates an ST object from a tree representation."},
2807 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2808 "Creates an ST object from a tree representation."},
2809 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
2810 "Creates a tuple-tree representation of an ST."},
2811 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
2812 "Creates a list-tree representation of an ST."},
2813 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2814 "Creates an ST object from a tree representation."},
2815 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2816 "Creates an ST object from a tree representation."},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002817
Fred Drake43f8f9b1998-04-13 16:25:46 +00002818 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00002819 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Fred Drakec2683dd2001-07-17 19:32:05 +00002820 "Returns the pickle magic to allow ST objects to be pickled."},
Fred Drake43f8f9b1998-04-13 16:25:46 +00002821
Fred Drake268397f1998-04-29 14:16:32 +00002822 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002823 };
2824
2825
Tim Peters6d7c4422000-08-26 07:38:06 +00002826DL_EXPORT(void) initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00002827
Guido van Rossum3886bb61998-12-04 18:50:17 +00002828DL_EXPORT(void)
Thomas Wouters5c669862000-07-24 15:49:08 +00002829initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00002830{
Fred Drake13130bc2001-08-15 16:44:56 +00002831 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00002832
2833 PyST_Type.ob_type = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002834 module = Py_InitModule("parser", parser_functions);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002835
Fred Drake7a15ba51999-09-09 14:21:52 +00002836 if (parser_error == 0)
2837 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002838
2839 if ((parser_error == 0)
Fred Drake13130bc2001-08-15 16:44:56 +00002840 || (PyModule_AddObject(module, "ParserError", parser_error) != 0)) {
Fred Drakec2683dd2001-07-17 19:32:05 +00002841 /* caller will check PyErr_Occurred() */
2842 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002843 }
Fred Drakec2683dd2001-07-17 19:32:05 +00002844 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002845 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00002846 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00002847 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002848
Fred Drake13130bc2001-08-15 16:44:56 +00002849 PyModule_AddStringConstant(module, "__copyright__",
2850 parser_copyright_string);
2851 PyModule_AddStringConstant(module, "__doc__",
2852 parser_doc_string);
2853 PyModule_AddStringConstant(module, "__version__",
2854 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002855
Fred Drake78bdb9b2001-07-19 20:17:15 +00002856 /* Register to support pickling.
2857 * If this fails, the import of this module will fail because an
2858 * exception will be raised here; should we clear the exception?
2859 */
Fred Drake13130bc2001-08-15 16:44:56 +00002860 copyreg = PyImport_ImportModule("copy_reg");
2861 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002862 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002863
Fred Drake13130bc2001-08-15 16:44:56 +00002864 func = PyObject_GetAttrString(copyreg, "pickle");
2865 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
2866 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00002867 Py_XINCREF(pickle_constructor);
2868 if ((func != NULL) && (pickle_constructor != NULL)
2869 && (pickler != NULL)) {
2870 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002871
Fred Drakec2683dd2001-07-17 19:32:05 +00002872 res = PyObject_CallFunction(func, "OOO", &PyST_Type, pickler,
2873 pickle_constructor);
Fred Drakeff9ea482000-04-19 13:54:15 +00002874 Py_XDECREF(res);
2875 }
2876 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00002877 Py_XDECREF(pickle_constructor);
2878 Py_XDECREF(pickler);
2879 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002880 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002881}