blob: 91ffef11ee4f36afb668094032a21c5048cf42ab [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*
Guido van Rossum47478871996-08-21 14:32:37 +000061parser_version_string = "0.4";
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 Rossumfd71b9e2000-06-30 23:50:40 +000079Copyright (c) 2000, BeOpen.com.
80Copyright (c) 1995-2000, Corporation for National Research Initiatives.
81Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
82All rights reserved.
Guido van Rossum52f2c051993-11-10 12:53:24 +000083
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000084See the file "Misc/COPYRIGHT" for information on usage and
85redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum52f2c051993-11-10 12:53:24 +000086******************************************************************/
87
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000088static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000089node2tuple(node *n, /* node to convert */
90 SeqMaker mkseq, /* create sequence */
91 SeqInserter addelem, /* func. to add elem. in seq. */
92 int lineno) /* include line numbers? */
Guido van Rossum47478871996-08-21 14:32:37 +000093{
Guido van Rossum3d602e31996-07-21 02:33:56 +000094 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000095 Py_INCREF(Py_None);
96 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000097 }
98 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000099 int i;
100 PyObject *v;
101 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +0000102
Fred Drakeff9ea482000-04-19 13:54:15 +0000103 v = mkseq(1 + NCH(n));
104 if (v == NULL)
105 return (v);
106 w = PyInt_FromLong(TYPE(n));
107 if (w == NULL) {
108 Py_DECREF(v);
109 return ((PyObject*) NULL);
110 }
111 (void) addelem(v, 0, w);
112 for (i = 0; i < NCH(n); i++) {
113 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno);
114 if (w == NULL) {
115 Py_DECREF(v);
116 return ((PyObject*) NULL);
117 }
118 (void) addelem(v, i+1, w);
119 }
120 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000121 }
122 else if (ISTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000123 PyObject *result = mkseq(2 + lineno);
124 if (result != NULL) {
125 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
126 (void) addelem(result, 1, PyString_FromString(STR(n)));
127 if (lineno == 1)
128 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
129 }
130 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000131 }
132 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000133 PyErr_SetString(PyExc_SystemError,
134 "unrecognized parse tree node type");
135 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000136 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000137}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000138/*
139 * End of material copyrighted by Stichting Mathematisch Centrum.
140 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000141
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000142
143
144/* There are two types of intermediate objects we're interested in:
145 * 'eval' and 'exec' types. These constants can be used in the ast_type
146 * field of the object type to identify which any given object represents.
147 * These should probably go in an external header to allow other extensions
148 * to use them, but then, we really should be using C++ too. ;-)
149 *
Guido van Rossum3d602e31996-07-21 02:33:56 +0000150 * The PyAST_FRAGMENT type is not currently supported. Maybe not useful?
151 * Haven't decided yet.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000152 */
153
Fred Drakeff9ea482000-04-19 13:54:15 +0000154#define PyAST_EXPR 1
155#define PyAST_SUITE 2
156#define PyAST_FRAGMENT 3
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000157
158
159/* These are the internal objects and definitions required to implement the
160 * AST type. Most of the internal names are more reminiscent of the 'old'
161 * naming style, but the code uses the new naming convention.
162 */
163
164static PyObject*
165parser_error = 0;
166
167
168typedef struct _PyAST_Object {
Fred Drakeff9ea482000-04-19 13:54:15 +0000169 PyObject_HEAD /* standard object header */
170 node* ast_node; /* the node* returned by the parser */
171 int ast_type; /* EXPR or SUITE ? */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000172} PyAST_Object;
173
174
Fred Drake268397f1998-04-29 14:16:32 +0000175staticforward void
Fred Drakeff9ea482000-04-19 13:54:15 +0000176parser_free(PyAST_Object *ast);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000177
Fred Drake268397f1998-04-29 14:16:32 +0000178staticforward int
Fred Drakeff9ea482000-04-19 13:54:15 +0000179parser_compare(PyAST_Object *left, PyAST_Object *right);
Fred Drake268397f1998-04-29 14:16:32 +0000180
181staticforward PyObject *
Fred Drakeff9ea482000-04-19 13:54:15 +0000182parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000183
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000184
Fred Drake268397f1998-04-29 14:16:32 +0000185static
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000186PyTypeObject PyAST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000187 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000188 0,
Fred Drakeff9ea482000-04-19 13:54:15 +0000189 "ast", /* tp_name */
190 (int) sizeof(PyAST_Object), /* tp_basicsize */
191 0, /* tp_itemsize */
192 (destructor)parser_free, /* tp_dealloc */
193 0, /* tp_print */
194 parser_getattr, /* tp_getattr */
195 0, /* tp_setattr */
196 (cmpfunc)parser_compare, /* tp_compare */
197 0, /* tp_repr */
198 0, /* tp_as_number */
199 0, /* tp_as_sequence */
200 0, /* tp_as_mapping */
201 0, /* tp_hash */
202 0, /* tp_call */
203 0, /* tp_str */
204 0, /* tp_getattro */
205 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000206
207 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000208 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000209
Fred Drakeff9ea482000-04-19 13:54:15 +0000210 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000211
212 /* __doc__ */
213 "Intermediate representation of a Python parse tree."
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000214}; /* PyAST_Type */
215
216
217static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000218parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000219{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000220 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000221
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000222 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000223 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000224
225 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000226 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000227
228 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000229 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000230
231 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000232 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000233
234 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000235 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000236
237 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000238 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000239
Fred Drakeff9ea482000-04-19 13:54:15 +0000240 if (v != 0)
241 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000242 }
243 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000244}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000245
246
247/* int parser_compare(PyAST_Object* left, PyAST_Object* right)
248 *
249 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
250 * This really just wraps a call to parser_compare_nodes() with some easy
251 * checks and protection code.
252 *
253 */
254static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000255parser_compare(PyAST_Object *left, PyAST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000256{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000257 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000258 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000259
260 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000261 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000262
263 return (parser_compare_nodes(left->ast_node, right->ast_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000264}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265
266
267/* parser_newastobject(node* ast)
268 *
269 * Allocates a new Python object representing an AST. This is simply the
270 * 'wrapper' object that holds a node* and allows it to be passed around in
271 * Python code.
272 *
273 */
274static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000275parser_newastobject(node *ast, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000276{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000277 PyAST_Object* o = PyObject_New(PyAST_Object, &PyAST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000278
279 if (o != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000280 o->ast_node = ast;
281 o->ast_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000282 }
Fred Drake268397f1998-04-29 14:16:32 +0000283 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000284 PyNode_Free(ast);
Fred Drake268397f1998-04-29 14:16:32 +0000285 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000286 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000287}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000288
289
290/* void parser_free(PyAST_Object* ast)
291 *
292 * This is called by a del statement that reduces the reference count to 0.
293 *
294 */
295static void
Fred Drakeff9ea482000-04-19 13:54:15 +0000296parser_free(PyAST_Object *ast)
Guido van Rossum47478871996-08-21 14:32:37 +0000297{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000298 PyNode_Free(ast->ast_node);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000299 PyObject_Del(ast);
Fred Drakeff9ea482000-04-19 13:54:15 +0000300}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000301
302
Fred Drake2a6875e1999-09-20 22:32:18 +0000303/* parser_ast2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000304 *
305 * This provides conversion from a node* to a tuple object that can be
306 * returned to the Python-level caller. The AST object is not modified.
307 *
308 */
309static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000310parser_ast2tuple(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000311{
Guido van Rossum47478871996-08-21 14:32:37 +0000312 PyObject *line_option = 0;
313 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000314 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000315
Fred Drake7a15ba51999-09-09 14:21:52 +0000316 static char *keywords[] = {"ast", "line_info", NULL};
317
Fred Drake268397f1998-04-29 14:16:32 +0000318 if (self == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000319 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:ast2tuple", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000320 &PyAST_Type, &self, &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000321 }
Fred Drake503d8d61998-04-13 18:45:18 +0000322 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000323 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:totuple", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000324 &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000325 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000326 int lineno = 0;
327 if (line_option != NULL) {
328 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
329 }
330 /*
331 * Convert AST into a tuple representation. Use Guido's function,
332 * since it's known to work already.
333 */
334 res = node2tuple(((PyAST_Object*)self)->ast_node,
335 PyTuple_New, PyTuple_SetItem, lineno);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000336 }
337 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000338}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000339
340
Fred Drake2a6875e1999-09-20 22:32:18 +0000341/* parser_ast2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000342 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000343 * This provides conversion from a node* to a list object that can be
Guido van Rossum47478871996-08-21 14:32:37 +0000344 * returned to the Python-level caller. The AST object is not modified.
345 *
346 */
347static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000348parser_ast2list(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000349{
Guido van Rossum47478871996-08-21 14:32:37 +0000350 PyObject *line_option = 0;
351 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000352 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000353
Fred Drake7a15ba51999-09-09 14:21:52 +0000354 static char *keywords[] = {"ast", "line_info", NULL};
355
Fred Drake503d8d61998-04-13 18:45:18 +0000356 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000357 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:ast2list", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000358 &PyAST_Type, &self, &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000359 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000360 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:tolist", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000361 &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000362 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000363 int lineno = 0;
364 if (line_option != 0) {
365 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
366 }
367 /*
368 * Convert AST into a tuple representation. Use Guido's function,
369 * since it's known to work already.
370 */
371 res = node2tuple(self->ast_node,
372 PyList_New, PyList_SetItem, lineno);
Guido van Rossum47478871996-08-21 14:32:37 +0000373 }
374 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000375}
Guido van Rossum47478871996-08-21 14:32:37 +0000376
377
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000378/* parser_compileast(PyObject* self, PyObject* args)
379 *
380 * This function creates code objects from the parse tree represented by
381 * the passed-in data object. An optional file name is passed in as well.
382 *
383 */
384static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000385parser_compileast(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000386{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000387 PyObject* res = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000388 char* str = "<ast>";
Fred Drake503d8d61998-04-13 18:45:18 +0000389 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000390
Fred Drake7a15ba51999-09-09 14:21:52 +0000391 static char *keywords[] = {"ast", "filename", NULL};
392
Fred Drake503d8d61998-04-13 18:45:18 +0000393 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000394 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compileast", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000395 &PyAST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000396 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000397 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000398 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000399
400 if (ok)
Fred Drakeff9ea482000-04-19 13:54:15 +0000401 res = (PyObject *)PyNode_Compile(self->ast_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000402
403 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000404}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000405
406
407/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
408 * PyObject* parser_issuite(PyObject* self, PyObject* args)
409 *
410 * Checks the passed-in AST object to determine if it is an expression or
411 * a statement suite, respectively. The return is a Python truth value.
412 *
413 */
414static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000415parser_isexpr(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000416{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000417 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000418 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000419
Fred Drake7a15ba51999-09-09 14:21:52 +0000420 static char *keywords[] = {"ast", NULL};
421
Fred Drake503d8d61998-04-13 18:45:18 +0000422 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000423 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000424 &PyAST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000425 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000426 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000427
428 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000429 /* Check to see if the AST represents an expression or not. */
430 res = (self->ast_type == PyAST_EXPR) ? Py_True : Py_False;
431 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000432 }
433 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000434}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000435
436
437static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000438parser_issuite(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000439{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000440 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000441 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000442
Fred Drake7a15ba51999-09-09 14:21:52 +0000443 static char *keywords[] = {"ast", NULL};
444
Fred Drake503d8d61998-04-13 18:45:18 +0000445 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000446 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000447 &PyAST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000448 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000449 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000450
451 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000452 /* Check to see if the AST represents an expression or not. */
453 res = (self->ast_type == PyAST_EXPR) ? Py_False : Py_True;
454 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000455 }
456 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000457}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000458
459
Fred Drake7a15ba51999-09-09 14:21:52 +0000460#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
461
Fred Drake503d8d61998-04-13 18:45:18 +0000462static PyMethodDef
463parser_methods[] = {
Fred Drakeff9ea482000-04-19 13:54:15 +0000464 {"compile", (PyCFunction)parser_compileast, PUBLIC_METHOD_TYPE,
465 "Compile this AST object into a code object."},
466 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
467 "Determines if this AST object was created from an expression."},
468 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
469 "Determines if this AST object was created from a suite."},
470 {"tolist", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
471 "Creates a list-tree representation of this AST."},
472 {"totuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
473 "Creates a tuple-tree representation of this AST."},
Fred Drake503d8d61998-04-13 18:45:18 +0000474
Fred Drake268397f1998-04-29 14:16:32 +0000475 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000476};
477
Fred Drake503d8d61998-04-13 18:45:18 +0000478
479static PyObject*
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000480parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000481{
Fred Drake503d8d61998-04-13 18:45:18 +0000482 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000483}
Fred Drake503d8d61998-04-13 18:45:18 +0000484
485
Guido van Rossum3d602e31996-07-21 02:33:56 +0000486/* err_string(char* message)
487 *
488 * Sets the error string for an exception of type ParserError.
489 *
490 */
491static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000492err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000493{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000494 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000495}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000496
497
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000498/* PyObject* parser_do_parse(PyObject* args, int type)
499 *
500 * Internal function to actually execute the parse and return the result if
501 * successful, or set an exception if not.
502 *
503 */
504static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000505parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000506{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000507 char* string = 0;
508 PyObject* res = 0;
509
Fred Drake7a15ba51999-09-09 14:21:52 +0000510 static char *keywords[] = {"source", NULL};
511
512 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000513 node* n = PyParser_SimpleParseString(string,
514 (type == PyAST_EXPR)
515 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000516
Fred Drakeff9ea482000-04-19 13:54:15 +0000517 if (n != 0)
518 res = parser_newastobject(n, type);
519 else
520 err_string("Could not parse string.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000521 }
522 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000523}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000524
525
526/* PyObject* parser_expr(PyObject* self, PyObject* args)
527 * PyObject* parser_suite(PyObject* self, PyObject* args)
528 *
529 * External interfaces to the parser itself. Which is called determines if
530 * the parser attempts to recognize an expression ('eval' form) or statement
531 * suite ('exec' form). The real work is done by parser_do_parse() above.
532 *
533 */
534static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000535parser_expr(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000536{
Fred Drake268397f1998-04-29 14:16:32 +0000537 NOTE(ARGUNUSED(self))
Fred Drake7a15ba51999-09-09 14:21:52 +0000538 return (parser_do_parse(args, kw, "s:expr", PyAST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000539}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000540
541
542static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000543parser_suite(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000544{
Fred Drake268397f1998-04-29 14:16:32 +0000545 NOTE(ARGUNUSED(self))
Fred Drake7a15ba51999-09-09 14:21:52 +0000546 return (parser_do_parse(args, kw, "s:suite", PyAST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000547}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000548
549
550
551/* This is the messy part of the code. Conversion from a tuple to an AST
552 * object requires that the input tuple be valid without having to rely on
553 * catching an exception from the compiler. This is done to allow the
554 * compiler itself to remain fast, since most of its input will come from
555 * the parser directly, and therefore be known to be syntactically correct.
556 * This validation is done to ensure that we don't core dump the compile
557 * phase, returning an exception instead.
558 *
559 * Two aspects can be broken out in this code: creating a node tree from
560 * the tuple passed in, and verifying that it is indeed valid. It may be
561 * advantageous to expand the number of AST types to include funcdefs and
562 * lambdadefs to take advantage of the optimizer, recognizing those ASTs
563 * here. They are not necessary, and not quite as useful in a raw form.
564 * For now, let's get expressions and suites working reliably.
565 */
566
567
Fred Drakeff9ea482000-04-19 13:54:15 +0000568staticforward node* build_node_tree(PyObject *tuple);
569staticforward int validate_expr_tree(node *tree);
570staticforward int validate_file_input(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000571
572
573/* PyObject* parser_tuple2ast(PyObject* self, PyObject* args)
574 *
575 * This is the public function, called from the Python code. It receives a
576 * single tuple object from the caller, and creates an AST object if the
577 * tuple can be validated. It does this by checking the first code of the
578 * tuple, and, if acceptable, builds the internal representation. If this
579 * step succeeds, the internal representation is validated as fully as
580 * possible with the various validate_*() routines defined below.
581 *
582 * This function must be changed if support is to be added for PyAST_FRAGMENT
583 * AST objects.
584 *
585 */
586static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000587parser_tuple2ast(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000588{
Fred Drake268397f1998-04-29 14:16:32 +0000589 NOTE(ARGUNUSED(self))
Guido van Rossum47478871996-08-21 14:32:37 +0000590 PyObject *ast = 0;
591 PyObject *tuple = 0;
592 PyObject *temp = 0;
593 int ok;
Guido van Rossuma376cc51996-12-05 23:43:35 +0000594 int start_sym = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000595
Fred Drake7a15ba51999-09-09 14:21:52 +0000596 static char *keywords[] = {"sequence", NULL};
597
598 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:tuple2ast", keywords,
599 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000600 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000601 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000602 PyErr_SetString(PyExc_ValueError,
603 "tuple2ast() requires a single sequence argument");
604 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000605 }
606 /*
Fred Drakeff9ea482000-04-19 13:54:15 +0000607 * This mess of tests is written this way so we can use the abstract
608 * object interface (AOI). Unfortunately, the AOI increments reference
609 * counts, which requires that we store a pointer to retrieved object
610 * so we can DECREF it after the check. But we really should accept
611 * lists as well as tuples at the very least.
Guido van Rossum47478871996-08-21 14:32:37 +0000612 */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000613 ok = PyObject_Size(tuple) >= 2;
Guido van Rossum47478871996-08-21 14:32:37 +0000614 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000615 temp = PySequence_GetItem(tuple, 0);
616 ok = (temp != NULL) && PyInt_Check(temp);
617 if (ok)
618 /* this is used after the initial checks: */
619 start_sym = PyInt_AS_LONG(temp);
620 Py_XDECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000621 }
622 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000623 temp = PySequence_GetItem(tuple, 1);
624 ok = (temp != NULL) && PySequence_Check(temp);
625 Py_XDECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000626 }
627 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000628 temp = PySequence_GetItem(tuple, 1);
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000629 ok = (temp != NULL) && PyObject_Size(temp) >= 2;
Fred Drakeff9ea482000-04-19 13:54:15 +0000630 if (ok) {
631 PyObject *temp2 = PySequence_GetItem(temp, 0);
632 if (temp2 != NULL) {
633 ok = PyInt_Check(temp2);
634 Py_DECREF(temp2);
635 }
636 }
637 Py_XDECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000638 }
639 /* If we've failed at some point, get out of here. */
640 if (!ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000641 err_string("malformed sequence for tuple2ast()");
642 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000643 }
644 /*
645 * This might be a valid parse tree, but let's do a quick check
646 * before we jump the gun.
647 */
648 if (start_sym == eval_input) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000649 /* Might be an eval form. */
650 node* expression = build_node_tree(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000651
Fred Drakeff9ea482000-04-19 13:54:15 +0000652 if ((expression != 0) && validate_expr_tree(expression))
653 ast = parser_newastobject(expression, PyAST_EXPR);
Guido van Rossum47478871996-08-21 14:32:37 +0000654 }
655 else if (start_sym == file_input) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000656 /* This looks like an exec form so far. */
657 node* suite_tree = build_node_tree(tuple);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000658
Fred Drakeff9ea482000-04-19 13:54:15 +0000659 if ((suite_tree != 0) && validate_file_input(suite_tree))
660 ast = parser_newastobject(suite_tree, PyAST_SUITE);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000661 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000662 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000663 /* This is a fragment, and is not yet supported. Maybe they
664 * will be if I find a use for them.
665 */
666 err_string("Fragmentary parse trees not supported.");
Guido van Rossum47478871996-08-21 14:32:37 +0000667
668 /* Make sure we throw an exception on all errors. We should never
669 * get this, but we'd do well to be sure something is done.
670 */
671 if ((ast == 0) && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000672 err_string("Unspecified ast error occurred.");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000673
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000674 return (ast);
Fred Drakeff9ea482000-04-19 13:54:15 +0000675}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000676
677
678/* int check_terminal_tuple()
679 *
Guido van Rossum47478871996-08-21 14:32:37 +0000680 * Check a tuple to determine that it is indeed a valid terminal
681 * node. The node is known to be required as a terminal, so we throw
682 * an exception if there is a failure.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000683 *
Guido van Rossum47478871996-08-21 14:32:37 +0000684 * The format of an acceptable terminal tuple is "(is[i])": the fact
685 * that elem is a tuple and the integer is a valid terminal symbol
686 * has been established before this function is called. We must
687 * check the length of the tuple and the type of the second element
688 * and optional third element. We do *NOT* check the actual text of
689 * the string element, which we could do in many cases. This is done
690 * by the validate_*() functions which operate on the internal
691 * representation.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000692 */
693static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000694check_terminal_tuple(PyObject *elem)
Guido van Rossum47478871996-08-21 14:32:37 +0000695{
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000696 int len = PyObject_Size(elem);
Guido van Rossum47478871996-08-21 14:32:37 +0000697 int res = 1;
698 char* str = "Illegal terminal symbol; bad node length.";
Guido van Rossum3d602e31996-07-21 02:33:56 +0000699
Guido van Rossum47478871996-08-21 14:32:37 +0000700 if ((len == 2) || (len == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000701 PyObject *temp = PySequence_GetItem(elem, 1);
702 res = PyString_Check(temp);
703 str = "Illegal terminal symbol; expected a string.";
704 if (res && (len == 3)) {
705 PyObject* third = PySequence_GetItem(elem, 2);
706 res = PyInt_Check(third);
707 str = "Invalid third element of terminal node.";
708 Py_XDECREF(third);
709 }
710 Py_XDECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000711 }
712 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000713 res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000714 }
715 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000716 elem = Py_BuildValue("(os)", elem, str);
717 PyErr_SetObject(parser_error, elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000718 }
719 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000720}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000721
722
723/* node* build_node_children()
724 *
725 * Iterate across the children of the current non-terminal node and build
726 * their structures. If successful, return the root of this portion of
727 * the tree, otherwise, 0. Any required exception will be specified already,
728 * and no memory will have been deallocated.
729 *
730 */
731static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000732build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000733{
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000734 int len = PyObject_Size(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000735 int i;
736
737 for (i = 1; i < len; ++i) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000738 /* elem must always be a tuple, however simple */
739 PyObject* elem = PySequence_GetItem(tuple, i);
740 int ok = elem != NULL;
741 long type = 0;
742 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000743
Fred Drakeff9ea482000-04-19 13:54:15 +0000744 if (ok)
745 ok = PySequence_Check(elem);
746 if (ok) {
747 PyObject *temp = PySequence_GetItem(elem, 0);
748 if (temp == NULL)
749 ok = 0;
750 else {
751 ok = PyInt_Check(temp);
752 if (ok)
753 type = PyInt_AS_LONG(temp);
754 Py_DECREF(temp);
755 }
756 }
757 if (!ok) {
758 PyErr_SetObject(parser_error,
759 Py_BuildValue("(os)", elem,
760 "Illegal node construct."));
761 Py_XDECREF(elem);
762 return (0);
763 }
764 if (ISTERMINAL(type)) {
765 if (check_terminal_tuple(elem)) {
766 PyObject *temp = PySequence_GetItem(elem, 1);
Guido van Rossum47478871996-08-21 14:32:37 +0000767
Fred Drakeff9ea482000-04-19 13:54:15 +0000768 /* check_terminal_tuple() already verified it's a string */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000769 strn = (char *)PyMem_MALLOC(PyString_GET_SIZE(temp) + 1);
Fred Drakeff9ea482000-04-19 13:54:15 +0000770 if (strn != NULL)
771 (void) strcpy(strn, PyString_AS_STRING(temp));
Guido van Rossumb18618d2000-05-03 23:44:39 +0000772 Py_DECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000773
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000774 if (PyObject_Size(elem) == 3) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000775 PyObject* temp = PySequence_GetItem(elem, 2);
776 *line_num = PyInt_AsLong(temp);
777 Py_DECREF(temp);
778 }
779 }
780 else {
781 Py_XDECREF(elem);
782 return (0);
783 }
784 }
785 else if (!ISNONTERMINAL(type)) {
786 /*
787 * It has to be one or the other; this is an error.
788 * Throw an exception.
789 */
790 PyErr_SetObject(parser_error,
791 Py_BuildValue("(os)", elem,
792 "Unknown node type."));
793 Py_XDECREF(elem);
794 return (0);
795 }
796 PyNode_AddChild(root, type, strn, *line_num);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000797
Fred Drakeff9ea482000-04-19 13:54:15 +0000798 if (ISNONTERMINAL(type)) {
799 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000800
Fred Drakeff9ea482000-04-19 13:54:15 +0000801 if (new_child != build_node_children(elem, new_child, line_num)) {
802 Py_XDECREF(elem);
803 return (0);
804 }
805 }
806 else if (type == NEWLINE) { /* It's true: we increment the */
807 ++(*line_num); /* line number *after* the newline! */
808 }
809 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000810 }
811 return (root);
Fred Drakeff9ea482000-04-19 13:54:15 +0000812}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000813
814
815static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000816build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000817{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000818 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000819 PyObject *temp = PySequence_GetItem(tuple, 0);
820 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000821
Guido van Rossum47478871996-08-21 14:32:37 +0000822 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000823 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000824 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000825 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000826 /*
827 * The tuple is simple, but it doesn't start with a start symbol.
828 * Throw an exception now and be done with it.
829 */
830 tuple = Py_BuildValue("(os)", tuple,
831 "Illegal ast tuple; cannot start with terminal symbol.");
832 PyErr_SetObject(parser_error, tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000833 }
834 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000835 /*
836 * Not efficient, but that can be handled later.
837 */
838 int line_num = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000839
Fred Drakeff9ea482000-04-19 13:54:15 +0000840 res = PyNode_New(num);
841 if (res != build_node_children(tuple, res, &line_num)) {
842 PyNode_Free(res);
843 res = 0;
844 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000845 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000846 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000847 /* The tuple is illegal -- if the number is neither TERMINAL nor
848 * NONTERMINAL, we can't use it.
849 */
850 PyErr_SetObject(parser_error,
851 Py_BuildValue("(os)", tuple,
852 "Illegal component tuple."));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000853
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000854 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000855}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000856
857
Fred Drakeff9ea482000-04-19 13:54:15 +0000858#define VALIDATER(n) static int validate_##n(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000859
860
861/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000862 * Validation routines used within the validation section:
863 */
Fred Drakeff9ea482000-04-19 13:54:15 +0000864staticforward int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000865
Fred Drakeff9ea482000-04-19 13:54:15 +0000866#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
867#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
868#define validate_colon(ch) validate_terminal(ch, COLON, ":")
869#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
870#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
871#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
872#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
873#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
874#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
875#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
876#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
877#define validate_star(ch) validate_terminal(ch, STAR, "*")
878#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
879#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
880#define validate_dot(ch) validate_terminal(ch, DOT, ".")
881#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000882
Fred Drakeff9ea482000-04-19 13:54:15 +0000883VALIDATER(node); VALIDATER(small_stmt);
884VALIDATER(class); VALIDATER(node);
885VALIDATER(parameters); VALIDATER(suite);
886VALIDATER(testlist); VALIDATER(varargslist);
887VALIDATER(fpdef); VALIDATER(fplist);
888VALIDATER(stmt); VALIDATER(simple_stmt);
889VALIDATER(expr_stmt); VALIDATER(power);
890VALIDATER(print_stmt); VALIDATER(del_stmt);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000891VALIDATER(return_stmt);
Fred Drakeff9ea482000-04-19 13:54:15 +0000892VALIDATER(raise_stmt); VALIDATER(import_stmt);
Guido van Rossum2a288461996-08-21 21:55:43 +0000893VALIDATER(global_stmt);
Guido van Rossum925e5471997-04-02 05:32:13 +0000894VALIDATER(assert_stmt);
Fred Drakeff9ea482000-04-19 13:54:15 +0000895VALIDATER(exec_stmt); VALIDATER(compound_stmt);
896VALIDATER(while); VALIDATER(for);
897VALIDATER(try); VALIDATER(except_clause);
898VALIDATER(test); VALIDATER(and_test);
899VALIDATER(not_test); VALIDATER(comparison);
900VALIDATER(comp_op); VALIDATER(expr);
901VALIDATER(xor_expr); VALIDATER(and_expr);
902VALIDATER(shift_expr); VALIDATER(arith_expr);
903VALIDATER(term); VALIDATER(factor);
904VALIDATER(atom); VALIDATER(lambdef);
905VALIDATER(trailer); VALIDATER(subscript);
906VALIDATER(subscriptlist); VALIDATER(sliceop);
907VALIDATER(exprlist); VALIDATER(dictmaker);
908VALIDATER(arglist); VALIDATER(argument);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000909
910
Fred Drakeff9ea482000-04-19 13:54:15 +0000911#define is_even(n) (((n) & 1) == 0)
912#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000913
914
915static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000916validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000917{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000918 int res = (TYPE(n) == t);
919
920 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000921 char buffer[128];
922 (void) sprintf(buffer, "Expected node type %d, got %d.", t, TYPE(n));
923 err_string(buffer);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000924 }
925 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000926}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000927
928
Fred Drakee7ab64e2000-04-25 04:14:46 +0000929/* Verifies that the number of child nodes is exactly 'num', raising
930 * an exception if it isn't. The exception message does not indicate
931 * the exact number of nodes, allowing this to be used to raise the
932 * "right" exception when the wrong number of nodes is present in a
933 * specific variant of a statement's syntax. This is commonly used
934 * in that fashion.
935 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000936static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000937validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000938{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000939 if (NCH(n) != num) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000940 char buff[60];
941 (void) sprintf(buff, "Illegal number of children for %s node.", name);
942 err_string(buff);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000943 }
944 return (NCH(n) == num);
Fred Drakeff9ea482000-04-19 13:54:15 +0000945}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000946
947
948static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000949validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000950{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000951 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000952 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000953
954 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000955 char buffer[60];
956 (void) sprintf(buffer, "Illegal terminal: expected \"%s\"", string);
957 err_string(buffer);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000958 }
959 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000960}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000961
962
Guido van Rossum47478871996-08-21 14:32:37 +0000963/* X (',' X) [',']
964 */
965static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000966validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000967 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000968{
969 int nch = NCH(tree);
970 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000971 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000972
973 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000974 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000975 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000976 if (is_even(nch))
977 res = validate_comma(CHILD(tree, --nch));
978 if (res && nch > 1) {
979 int pos = 1;
980 for ( ; res && pos < nch; pos += 2)
981 res = (validate_comma(CHILD(tree, pos))
982 && vfunc(CHILD(tree, pos + 1)));
983 }
Guido van Rossum47478871996-08-21 14:32:37 +0000984 }
985 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000986}
Guido van Rossum47478871996-08-21 14:32:37 +0000987
988
Guido van Rossum3d602e31996-07-21 02:33:56 +0000989/* VALIDATE(class)
990 *
991 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000992 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000993 */
Guido van Rossum47478871996-08-21 14:32:37 +0000994static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000995validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000996{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000997 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000998 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000999
Guido van Rossum3d602e31996-07-21 02:33:56 +00001000 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001001 res = (validate_name(CHILD(tree, 0), "class")
1002 && validate_ntype(CHILD(tree, 1), NAME)
1003 && validate_colon(CHILD(tree, nch - 2))
1004 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001005 }
1006 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001007 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001008 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001009 res = (validate_lparen(CHILD(tree, 2))
1010 && validate_testlist(CHILD(tree, 3))
1011 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001012 }
1013 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001014}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001015
1016
Guido van Rossum3d602e31996-07-21 02:33:56 +00001017/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001018 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001019 */
Guido van Rossum47478871996-08-21 14:32:37 +00001020static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001021validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001022{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001023 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001024 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001025 && (nch >= 4)
1026 && validate_name(CHILD(tree, 0), "if")
1027 && validate_test(CHILD(tree, 1))
1028 && validate_colon(CHILD(tree, 2))
1029 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001030
1031 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001032 /* ... 'else' ':' suite */
1033 res = (validate_name(CHILD(tree, nch - 3), "else")
1034 && validate_colon(CHILD(tree, nch - 2))
1035 && validate_suite(CHILD(tree, nch - 1)));
1036 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001037 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001038 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001039 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001040 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001041 /* Will catch the case for nch < 4 */
1042 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001043 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001044 /* ... ('elif' test ':' suite)+ ... */
1045 int j = 4;
1046 while ((j < nch) && res) {
1047 res = (validate_name(CHILD(tree, j), "elif")
1048 && validate_colon(CHILD(tree, j + 2))
1049 && validate_test(CHILD(tree, j + 1))
1050 && validate_suite(CHILD(tree, j + 3)));
1051 j += 4;
1052 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001053 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001054 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001055}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001056
1057
Guido van Rossum3d602e31996-07-21 02:33:56 +00001058/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001059 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001060 *
1061 */
Guido van Rossum47478871996-08-21 14:32:37 +00001062static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001063validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001064{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001065 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001066 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001067
Guido van Rossum3d602e31996-07-21 02:33:56 +00001068 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001069 res = (validate_lparen(CHILD(tree, 0))
1070 && validate_rparen(CHILD(tree, nch - 1)));
1071 if (res && (nch == 3))
1072 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001073 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001074 else {
1075 (void) validate_numnodes(tree, 2, "parameters");
1076 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001077 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001078}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001079
1080
Guido van Rossum3d602e31996-07-21 02:33:56 +00001081/* VALIDATE(suite)
1082 *
1083 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001084 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001085 * | NEWLINE INDENT stmt+ DEDENT
1086 */
Guido van Rossum47478871996-08-21 14:32:37 +00001087static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001088validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001089{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001090 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001091 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001092
Guido van Rossum3d602e31996-07-21 02:33:56 +00001093 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001094 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001095 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001096 /* NEWLINE INDENT stmt+ DEDENT */
1097 res = (validate_newline(CHILD(tree, 0))
1098 && validate_indent(CHILD(tree, 1))
1099 && validate_stmt(CHILD(tree, 2))
1100 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001101
Fred Drakeff9ea482000-04-19 13:54:15 +00001102 if (res && (nch > 4)) {
1103 int i = 3;
1104 --nch; /* forget the DEDENT */
1105 for ( ; res && (i < nch); ++i)
1106 res = validate_stmt(CHILD(tree, i));
1107 }
1108 else if (nch < 4)
1109 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001110 }
1111 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001112}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001113
1114
Guido van Rossum47478871996-08-21 14:32:37 +00001115static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001116validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001117{
Guido van Rossum47478871996-08-21 14:32:37 +00001118 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001119 validate_test, "testlist"));
1120}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001121
1122
Guido van Rossum3d602e31996-07-21 02:33:56 +00001123/* VALIDATE(varargslist)
1124 *
1125 * varargslist:
Fred Drakeff9ea482000-04-19 13:54:15 +00001126 * (fpdef ['=' test] ',')* ('*' NAME [',' '*' '*' NAME] | '*' '*' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001127 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1128 *
1129 * (fpdef ['=' test] ',')*
1130 * ('*' NAME [',' ('**'|'*' '*') NAME]
1131 * | ('**'|'*' '*') NAME)
1132 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1133 *
1134 */
Guido van Rossum47478871996-08-21 14:32:37 +00001135static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001136validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001137{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001138 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001139 int res = validate_ntype(tree, varargslist) && (nch != 0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001140
Guido van Rossum3d602e31996-07-21 02:33:56 +00001141 if (res && (nch >= 2) && (TYPE(CHILD(tree, nch - 1)) == NAME)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001142 /* (fpdef ['=' test] ',')*
1143 * ('*' NAME [',' '*' '*' NAME] | '*' '*' NAME)
1144 */
1145 int pos = 0;
1146 int remaining = nch;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001147
Fred Drakeff9ea482000-04-19 13:54:15 +00001148 while (res && (TYPE(CHILD(tree, pos)) == fpdef)) {
1149 res = validate_fpdef(CHILD(tree, pos));
1150 if (res) {
1151 if (TYPE(CHILD(tree, pos + 1)) == EQUAL) {
1152 res = validate_test(CHILD(tree, pos + 2));
1153 pos += 2;
1154 }
1155 res = res && validate_comma(CHILD(tree, pos + 1));
1156 pos += 2;
1157 }
1158 }
1159 if (res) {
1160 remaining = nch - pos;
1161 res = ((remaining == 2) || (remaining == 3)
1162 || (remaining == 5) || (remaining == 6));
1163 if (!res)
1164 (void) validate_numnodes(tree, 2, "varargslist");
1165 else if (TYPE(CHILD(tree, pos)) == DOUBLESTAR)
1166 return ((remaining == 2)
1167 && validate_ntype(CHILD(tree, pos+1), NAME));
1168 else {
1169 res = validate_star(CHILD(tree, pos++));
1170 --remaining;
1171 }
1172 }
1173 if (res) {
1174 if (remaining == 2) {
1175 res = (validate_star(CHILD(tree, pos))
1176 && validate_ntype(CHILD(tree, pos + 1), NAME));
1177 }
1178 else {
1179 res = validate_ntype(CHILD(tree, pos++), NAME);
1180 if (res && (remaining >= 4)) {
1181 res = validate_comma(CHILD(tree, pos));
1182 if (--remaining == 3)
1183 res = (validate_star(CHILD(tree, pos + 1))
1184 && validate_star(CHILD(tree, pos + 2)));
1185 else
1186 res = validate_ntype(CHILD(tree, pos + 1), DOUBLESTAR);
1187 }
1188 }
1189 }
1190 if (!res && !PyErr_Occurred())
1191 err_string("Incorrect validation of variable arguments list.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001192 }
1193 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001194 /* fpdef ['=' test] (',' fpdef ['=' test])* [','] */
1195 if (TYPE(CHILD(tree, nch - 1)) == COMMA)
1196 --nch;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001197
Fred Drakeff9ea482000-04-19 13:54:15 +00001198 /* fpdef ['=' test] (',' fpdef ['=' test])* */
1199 res = (is_odd(nch)
1200 && validate_fpdef(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001201
Fred Drakeff9ea482000-04-19 13:54:15 +00001202 if (res && (nch > 1)) {
1203 int pos = 1;
1204 if (TYPE(CHILD(tree, 1)) == EQUAL) {
1205 res = validate_test(CHILD(tree, 2));
1206 pos += 2;
1207 }
1208 /* ... (',' fpdef ['=' test])* */
1209 for ( ; res && (pos < nch); pos += 2) {
1210 /* ',' fpdef */
1211 res = (validate_comma(CHILD(tree, pos))
1212 && validate_fpdef(CHILD(tree, pos + 1)));
1213 if (res
1214 && ((nch - pos) > 2)
1215 && (TYPE(CHILD(tree, pos + 2)) == EQUAL)) {
1216 /* ['=' test] */
1217 res = validate_test(CHILD(tree, pos + 3));
1218 pos += 2;
1219 }
1220 }
1221 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001222 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001223 else {
1224 err_string("Improperly formed argument list.");
1225 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001226 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001227}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001228
1229
Guido van Rossum3d602e31996-07-21 02:33:56 +00001230/* VALIDATE(fpdef)
1231 *
1232 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001233 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001234 * | '(' fplist ')'
1235 */
Guido van Rossum47478871996-08-21 14:32:37 +00001236static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001237validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001238{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001239 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001240 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001241
Guido van Rossum3d602e31996-07-21 02:33:56 +00001242 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001243 if (nch == 1)
1244 res = validate_ntype(CHILD(tree, 0), NAME);
1245 else if (nch == 3)
1246 res = (validate_lparen(CHILD(tree, 0))
1247 && validate_fplist(CHILD(tree, 1))
1248 && validate_rparen(CHILD(tree, 2)));
1249 else
1250 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001251 }
1252 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001253}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001254
1255
Guido van Rossum47478871996-08-21 14:32:37 +00001256static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001257validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001258{
Guido van Rossum47478871996-08-21 14:32:37 +00001259 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001260 validate_fpdef, "fplist"));
1261}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001262
1263
Guido van Rossum3d602e31996-07-21 02:33:56 +00001264/* simple_stmt | compound_stmt
1265 *
1266 */
Guido van Rossum47478871996-08-21 14:32:37 +00001267static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001268validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001269{
1270 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001271 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001272
Guido van Rossum3d602e31996-07-21 02:33:56 +00001273 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001274 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001275
Fred Drakeff9ea482000-04-19 13:54:15 +00001276 if (TYPE(tree) == simple_stmt)
1277 res = validate_simple_stmt(tree);
1278 else
1279 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001280 }
1281 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001282}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001283
1284
Guido van Rossum3d602e31996-07-21 02:33:56 +00001285/* small_stmt (';' small_stmt)* [';'] NEWLINE
1286 *
1287 */
Guido van Rossum47478871996-08-21 14:32:37 +00001288static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001289validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001290{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001291 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001292 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001293 && (nch >= 2)
1294 && validate_small_stmt(CHILD(tree, 0))
1295 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001296
Guido van Rossum3d602e31996-07-21 02:33:56 +00001297 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001298 res = validate_numnodes(tree, 2, "simple_stmt");
1299 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001300 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001301 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001302 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001303 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001304
Fred Drakeff9ea482000-04-19 13:54:15 +00001305 for (i = 1; res && (i < nch); i += 2)
1306 res = (validate_semi(CHILD(tree, i))
1307 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001308 }
1309 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001310}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001311
1312
Guido van Rossum47478871996-08-21 14:32:37 +00001313static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001314validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001315{
1316 int nch = NCH(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +00001317 int res = (validate_numnodes(tree, 1, "small_stmt")
1318 && ((TYPE(CHILD(tree, 0)) == expr_stmt)
1319 || (TYPE(CHILD(tree, 0)) == print_stmt)
1320 || (TYPE(CHILD(tree, 0)) == del_stmt)
1321 || (TYPE(CHILD(tree, 0)) == pass_stmt)
1322 || (TYPE(CHILD(tree, 0)) == flow_stmt)
1323 || (TYPE(CHILD(tree, 0)) == import_stmt)
1324 || (TYPE(CHILD(tree, 0)) == global_stmt)
1325 || (TYPE(CHILD(tree, 0)) == assert_stmt)
1326 || (TYPE(CHILD(tree, 0)) == exec_stmt)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001327
1328 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001329 res = validate_node(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001330 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001331 char buffer[60];
1332 (void) sprintf(buffer, "Unrecognized child node of small_stmt: %d.",
1333 TYPE(CHILD(tree, 0)));
1334 err_string(buffer);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001335 }
1336 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001337}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001338
1339
1340/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001341 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001342 */
Guido van Rossum47478871996-08-21 14:32:37 +00001343static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001344validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001345{
1346 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001347 && validate_numnodes(tree, 1, "compound_stmt"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001348
1349 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001350 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001351
1352 tree = CHILD(tree, 0);
1353 res = ((TYPE(tree) == if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001354 || (TYPE(tree) == while_stmt)
1355 || (TYPE(tree) == for_stmt)
1356 || (TYPE(tree) == try_stmt)
1357 || (TYPE(tree) == funcdef)
1358 || (TYPE(tree) == classdef));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001359 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001360 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001361 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001362 char buffer[60];
1363 (void) sprintf(buffer, "Illegal compound statement type: %d.",
1364 TYPE(tree));
1365 err_string(buffer);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001366 }
1367 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001368}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001369
1370
Guido van Rossum47478871996-08-21 14:32:37 +00001371static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001372validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001373{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001374 int j;
1375 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001376 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001377 && is_odd(nch)
1378 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001379
Guido van Rossum3d602e31996-07-21 02:33:56 +00001380 for (j = 1; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001381 res = (validate_equal(CHILD(tree, j))
1382 && validate_testlist(CHILD(tree, j + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001383
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001384 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001385}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001386
1387
Guido van Rossum3d602e31996-07-21 02:33:56 +00001388/* print_stmt:
1389 *
Fred Drakeff9ea482000-04-19 13:54:15 +00001390 * 'print' (test ',')* [test]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001391 *
1392 */
Guido van Rossum47478871996-08-21 14:32:37 +00001393static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001394validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001395{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001396 int j;
1397 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001398 int res = (validate_ntype(tree, print_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001399 && (nch != 0)
1400 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001401
1402 if (res && is_even(nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001403 res = validate_test(CHILD(tree, nch - 1));
1404 --nch;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001405 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001406 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001407 (void) validate_numnodes(tree, 1, "print_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001408 for (j = 1; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001409 res = (validate_test(CHILD(tree, j))
1410 && validate_ntype(CHILD(tree, j + 1), COMMA));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001411
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001412 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001413}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001414
1415
Guido van Rossum47478871996-08-21 14:32:37 +00001416static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001417validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001418{
1419 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001420 && validate_name(CHILD(tree, 0), "del")
1421 && validate_exprlist(CHILD(tree, 1)));
1422}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001423
1424
Guido van Rossum47478871996-08-21 14:32:37 +00001425static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001426validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001427{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001428 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001429 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001430 && ((nch == 1) || (nch == 2))
1431 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001432
Guido van Rossum3d602e31996-07-21 02:33:56 +00001433 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001434 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001435
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001436 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001437}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001438
1439
Guido van Rossum47478871996-08-21 14:32:37 +00001440static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001441validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001442{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001443 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001444 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001445 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001446
Guido van Rossum3d602e31996-07-21 02:33:56 +00001447 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001448 res = validate_name(CHILD(tree, 0), "raise");
1449 if (res && (nch >= 2))
1450 res = validate_test(CHILD(tree, 1));
1451 if (res && nch > 2) {
1452 res = (validate_comma(CHILD(tree, 2))
1453 && validate_test(CHILD(tree, 3)));
1454 if (res && (nch > 4))
1455 res = (validate_comma(CHILD(tree, 4))
1456 && validate_test(CHILD(tree, 5)));
1457 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001458 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001459 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001460 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001461 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001462 res = (validate_comma(CHILD(tree, 2))
1463 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001464
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001465 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001466}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001467
1468
Guido van Rossum3d602e31996-07-21 02:33:56 +00001469/* import_stmt:
1470 *
1471 * 'import' dotted_name (',' dotted_name)*
1472 * | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*)
1473 */
Guido van Rossum47478871996-08-21 14:32:37 +00001474static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001475validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001476{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001477 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001478 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001479 && (nch >= 2) && is_even(nch)
1480 && validate_ntype(CHILD(tree, 0), NAME)
1481 && validate_ntype(CHILD(tree, 1), dotted_name));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001482
1483 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001484 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001485
Fred Drakeff9ea482000-04-19 13:54:15 +00001486 for (j = 2; res && (j < nch); j += 2)
1487 res = (validate_comma(CHILD(tree, j))
1488 && validate_ntype(CHILD(tree, j + 1), dotted_name));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001489 }
1490 else if (res && validate_name(CHILD(tree, 0), "from")) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001491 res = ((nch >= 4) && is_even(nch)
1492 && validate_name(CHILD(tree, 2), "import"));
1493 if (nch == 4) {
1494 res = ((TYPE(CHILD(tree, 3)) == NAME)
1495 || (TYPE(CHILD(tree, 3)) == STAR));
1496 if (!res)
1497 err_string("Illegal import statement.");
1498 }
1499 else {
1500 /* 'from' NAME 'import' NAME (',' NAME)+ */
1501 int j;
1502 res = validate_ntype(CHILD(tree, 3), NAME);
1503 for (j = 4; res && (j < nch); j += 2)
1504 res = (validate_comma(CHILD(tree, j))
1505 && validate_ntype(CHILD(tree, j + 1), NAME));
1506 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001507 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001508 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001509 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001510
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001511 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001512}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001513
1514
Guido van Rossum47478871996-08-21 14:32:37 +00001515static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001516validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001517{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001518 int j;
1519 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001520 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001521 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001522
Guido van Rossum3d602e31996-07-21 02:33:56 +00001523 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001524 res = (validate_name(CHILD(tree, 0), "global")
1525 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001526 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001527 res = (validate_comma(CHILD(tree, j))
1528 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001529
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001530 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001531}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001532
1533
Guido van Rossum3d602e31996-07-21 02:33:56 +00001534/* exec_stmt:
1535 *
1536 * 'exec' expr ['in' test [',' test]]
1537 */
Guido van Rossum47478871996-08-21 14:32:37 +00001538static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001539validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001540{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001541 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001542 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001543 && ((nch == 2) || (nch == 4) || (nch == 6))
1544 && validate_name(CHILD(tree, 0), "exec")
1545 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001546
Guido van Rossum3d602e31996-07-21 02:33:56 +00001547 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001548 err_string("Illegal exec statement.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001549 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001550 res = (validate_name(CHILD(tree, 2), "in")
1551 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001552 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001553 res = (validate_comma(CHILD(tree, 4))
1554 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001555
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001556 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001557}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001558
1559
Guido van Rossum925e5471997-04-02 05:32:13 +00001560/* assert_stmt:
1561 *
1562 * 'assert' test [',' test]
1563 */
1564static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001565validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001566{
1567 int nch = NCH(tree);
1568 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001569 && ((nch == 2) || (nch == 4))
1570 && (validate_name(CHILD(tree, 0), "__assert__") ||
1571 validate_name(CHILD(tree, 0), "assert"))
1572 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001573
1574 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001575 err_string("Illegal assert statement.");
Guido van Rossum925e5471997-04-02 05:32:13 +00001576 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001577 res = (validate_comma(CHILD(tree, 2))
1578 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001579
1580 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001581}
Guido van Rossum925e5471997-04-02 05:32:13 +00001582
1583
Guido van Rossum47478871996-08-21 14:32:37 +00001584static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001585validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001586{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001587 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001588 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001589 && ((nch == 4) || (nch == 7))
1590 && validate_name(CHILD(tree, 0), "while")
1591 && validate_test(CHILD(tree, 1))
1592 && validate_colon(CHILD(tree, 2))
1593 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001594
Guido van Rossum3d602e31996-07-21 02:33:56 +00001595 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001596 res = (validate_name(CHILD(tree, 4), "else")
1597 && validate_colon(CHILD(tree, 5))
1598 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001599
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001600 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001601}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001602
1603
Guido van Rossum47478871996-08-21 14:32:37 +00001604static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001605validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001606{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001607 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001608 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001609 && ((nch == 6) || (nch == 9))
1610 && validate_name(CHILD(tree, 0), "for")
1611 && validate_exprlist(CHILD(tree, 1))
1612 && validate_name(CHILD(tree, 2), "in")
1613 && validate_testlist(CHILD(tree, 3))
1614 && validate_colon(CHILD(tree, 4))
1615 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001616
Guido van Rossum3d602e31996-07-21 02:33:56 +00001617 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001618 res = (validate_name(CHILD(tree, 6), "else")
1619 && validate_colon(CHILD(tree, 7))
1620 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001621
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001622 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001623}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001624
1625
Guido van Rossum3d602e31996-07-21 02:33:56 +00001626/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001627 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001628 * | 'try' ':' suite 'finally' ':' suite
1629 *
1630 */
Guido van Rossum47478871996-08-21 14:32:37 +00001631static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001632validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001633{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001634 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001635 int pos = 3;
1636 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001637 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001638
1639 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001640 res = (validate_name(CHILD(tree, 0), "try")
1641 && validate_colon(CHILD(tree, 1))
1642 && validate_suite(CHILD(tree, 2))
1643 && validate_colon(CHILD(tree, nch - 2))
1644 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001645 else {
Fred Drake22269b52000-07-03 18:07:43 +00001646 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001647 char buffer[60];
1648 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1649 name = STR(CHILD(tree, nch - 3));
1650 (void) sprintf(buffer,
1651 "Illegal number of children for try/%s node.", name);
1652 err_string(buffer);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001653 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001654 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001655 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001656 res = (validate_except_clause(CHILD(tree, pos))
1657 && validate_colon(CHILD(tree, pos + 1))
1658 && validate_suite(CHILD(tree, pos + 2)));
1659 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001660 }
1661 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001662 res = validate_ntype(CHILD(tree, pos), NAME);
1663 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1664 res = (validate_numnodes(tree, 6, "try/finally")
1665 && validate_colon(CHILD(tree, 4))
1666 && validate_suite(CHILD(tree, 5)));
1667 else if (res) {
1668 if (nch == (pos + 3)) {
1669 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1670 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1671 if (!res)
1672 err_string("Illegal trailing triple in try statement.");
1673 }
1674 else if (nch == (pos + 6)) {
1675 res = (validate_name(CHILD(tree, pos), "except")
1676 && validate_colon(CHILD(tree, pos + 1))
1677 && validate_suite(CHILD(tree, pos + 2))
1678 && validate_name(CHILD(tree, pos + 3), "else"));
1679 }
1680 else
1681 res = validate_numnodes(tree, pos + 3, "try/except");
1682 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001683 }
1684 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001685}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001686
1687
Guido van Rossum47478871996-08-21 14:32:37 +00001688static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001689validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001690{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001691 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001692 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001693 && ((nch == 1) || (nch == 2) || (nch == 4))
1694 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001695
Guido van Rossum3d602e31996-07-21 02:33:56 +00001696 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001697 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001698 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001699 res = (validate_comma(CHILD(tree, 2))
1700 && validate_test(CHILD(tree, 3)));
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 Rossum47478871996-08-21 14:32:37 +00001706static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001707validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001708{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001709 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001710 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001711
Guido van Rossum3d602e31996-07-21 02:33:56 +00001712 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001713 res = ((nch == 1)
1714 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001715 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001716 int pos;
1717 res = validate_and_test(CHILD(tree, 0));
1718 for (pos = 1; res && (pos < nch); pos += 2)
1719 res = (validate_name(CHILD(tree, pos), "or")
1720 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001721 }
1722 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001723}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001724
1725
Guido van Rossum47478871996-08-21 14:32:37 +00001726static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001727validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001728{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001729 int pos;
1730 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001731 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001732 && is_odd(nch)
1733 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001734
Guido van Rossum3d602e31996-07-21 02:33:56 +00001735 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001736 res = (validate_name(CHILD(tree, pos), "and")
1737 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001738
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001739 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001740}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001741
1742
Guido van Rossum47478871996-08-21 14:32:37 +00001743static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001744validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001745{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001746 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001747 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001748
Guido van Rossum3d602e31996-07-21 02:33:56 +00001749 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001750 if (nch == 2)
1751 res = (validate_name(CHILD(tree, 0), "not")
1752 && validate_not_test(CHILD(tree, 1)));
1753 else if (nch == 1)
1754 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001755 }
1756 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001757}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001758
1759
Guido van Rossum47478871996-08-21 14:32:37 +00001760static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001761validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001762{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001763 int pos;
1764 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001765 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001766 && is_odd(nch)
1767 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001768
Guido van Rossum3d602e31996-07-21 02:33:56 +00001769 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001770 res = (validate_comp_op(CHILD(tree, pos))
1771 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001772
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001773 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001774}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001775
1776
Guido van Rossum47478871996-08-21 14:32:37 +00001777static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001778validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001779{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001780 int res = 0;
1781 int nch = NCH(tree);
1782
Guido van Rossum3d602e31996-07-21 02:33:56 +00001783 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001784 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001785 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001786 /*
1787 * Only child will be a terminal with a well-defined symbolic name
1788 * or a NAME with a string of either 'is' or 'in'
1789 */
1790 tree = CHILD(tree, 0);
1791 switch (TYPE(tree)) {
1792 case LESS:
1793 case GREATER:
1794 case EQEQUAL:
1795 case EQUAL:
1796 case LESSEQUAL:
1797 case GREATEREQUAL:
1798 case NOTEQUAL:
1799 res = 1;
1800 break;
1801 case NAME:
1802 res = ((strcmp(STR(tree), "in") == 0)
1803 || (strcmp(STR(tree), "is") == 0));
1804 if (!res) {
1805 char buff[128];
1806 (void) sprintf(buff, "Illegal operator: '%s'.", STR(tree));
1807 err_string(buff);
1808 }
1809 break;
1810 default:
1811 err_string("Illegal comparison operator type.");
1812 break;
1813 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001814 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00001815 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001816 res = (validate_ntype(CHILD(tree, 0), NAME)
1817 && validate_ntype(CHILD(tree, 1), NAME)
1818 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
1819 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
1820 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
1821 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
1822 if (!res && !PyErr_Occurred())
1823 err_string("Unknown comparison operator.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001824 }
1825 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001826}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001827
1828
Guido van Rossum47478871996-08-21 14:32:37 +00001829static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001830validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001831{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001832 int j;
1833 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001834 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001835 && is_odd(nch)
1836 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001837
Guido van Rossum3d602e31996-07-21 02:33:56 +00001838 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001839 res = (validate_xor_expr(CHILD(tree, j))
1840 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001841
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001842 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001843}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001844
1845
Guido van Rossum47478871996-08-21 14:32:37 +00001846static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001847validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001848{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001849 int j;
1850 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001851 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001852 && is_odd(nch)
1853 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001854
Guido van Rossum3d602e31996-07-21 02:33:56 +00001855 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001856 res = (validate_circumflex(CHILD(tree, j - 1))
1857 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001858
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001859 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001860}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001861
1862
Guido van Rossum47478871996-08-21 14:32:37 +00001863static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001864validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001865{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001866 int pos;
1867 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001868 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001869 && is_odd(nch)
1870 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001871
Guido van Rossum3d602e31996-07-21 02:33:56 +00001872 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001873 res = (validate_ampersand(CHILD(tree, pos))
1874 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001875
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001876 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001877}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001878
1879
1880static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001881validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001882 {
1883 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001884 int nch = NCH(tree);
1885 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00001886 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001887
Guido van Rossum3d602e31996-07-21 02:33:56 +00001888 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001889 if (TYPE(CHILD(tree, pos)) != op1)
1890 res = validate_ntype(CHILD(tree, pos), op2);
1891 if (res)
1892 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001893 }
1894 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001895}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001896
1897
Guido van Rossum47478871996-08-21 14:32:37 +00001898static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001899validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001900{
1901 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001902 && validate_chain_two_ops(tree, validate_arith_expr,
1903 LEFTSHIFT, RIGHTSHIFT));
1904}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001905
1906
Guido van Rossum47478871996-08-21 14:32:37 +00001907static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001908validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001909{
1910 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001911 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
1912}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001913
1914
Guido van Rossum47478871996-08-21 14:32:37 +00001915static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001916validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001917{
1918 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001919 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001920 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00001921 && is_odd(nch)
1922 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001923
Guido van Rossum3d602e31996-07-21 02:33:56 +00001924 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001925 res = (((TYPE(CHILD(tree, pos)) == STAR)
1926 || (TYPE(CHILD(tree, pos)) == SLASH)
1927 || (TYPE(CHILD(tree, pos)) == PERCENT))
1928 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001929
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001930 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001931}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001932
1933
Guido van Rossum3d602e31996-07-21 02:33:56 +00001934/* factor:
1935 *
1936 * factor: ('+'|'-'|'~') factor | power
1937 */
Guido van Rossum47478871996-08-21 14:32:37 +00001938static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001939validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001940{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001941 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001942 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00001943 && (((nch == 2)
1944 && ((TYPE(CHILD(tree, 0)) == PLUS)
1945 || (TYPE(CHILD(tree, 0)) == MINUS)
1946 || (TYPE(CHILD(tree, 0)) == TILDE))
1947 && validate_factor(CHILD(tree, 1)))
1948 || ((nch == 1)
1949 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001950 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001951}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001952
1953
Guido van Rossum3d602e31996-07-21 02:33:56 +00001954/* power:
1955 *
1956 * power: atom trailer* ('**' factor)*
1957 */
Guido van Rossum47478871996-08-21 14:32:37 +00001958static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001959validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001960{
1961 int pos = 1;
1962 int nch = NCH(tree);
1963 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00001964 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001965
1966 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00001967 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001968 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001969 if (!is_even(nch - pos)) {
1970 err_string("Illegal number of nodes for 'power'.");
1971 return (0);
1972 }
1973 for ( ; res && (pos < (nch - 1)); pos += 2)
1974 res = (validate_doublestar(CHILD(tree, pos))
1975 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001976 }
1977 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001978}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001979
1980
Guido van Rossum47478871996-08-21 14:32:37 +00001981static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001982validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001983{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001984 int pos;
1985 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001986 int res = validate_ntype(tree, atom) && (nch >= 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001987
1988 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001989 switch (TYPE(CHILD(tree, 0))) {
1990 case LPAR:
1991 res = ((nch <= 3)
1992 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001993
Fred Drakeff9ea482000-04-19 13:54:15 +00001994 if (res && (nch == 3))
1995 res = validate_testlist(CHILD(tree, 1));
1996 break;
1997 case LSQB:
1998 res = ((nch <= 3)
1999 && validate_ntype(CHILD(tree, nch - 1), RSQB));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002000
Fred Drakeff9ea482000-04-19 13:54:15 +00002001 if (res && (nch == 3))
2002 res = validate_testlist(CHILD(tree, 1));
2003 break;
2004 case LBRACE:
2005 res = ((nch <= 3)
2006 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002007
Fred Drakeff9ea482000-04-19 13:54:15 +00002008 if (res && (nch == 3))
2009 res = validate_dictmaker(CHILD(tree, 1));
2010 break;
2011 case BACKQUOTE:
2012 res = ((nch == 3)
2013 && validate_testlist(CHILD(tree, 1))
2014 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2015 break;
2016 case NAME:
2017 case NUMBER:
2018 res = (nch == 1);
2019 break;
2020 case STRING:
2021 for (pos = 1; res && (pos < nch); ++pos)
2022 res = validate_ntype(CHILD(tree, pos), STRING);
2023 break;
2024 default:
2025 res = 0;
2026 break;
2027 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002028 }
2029 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002030}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002031
2032
Guido van Rossum3d602e31996-07-21 02:33:56 +00002033/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002034 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002035 *
2036 */
Guido van Rossum47478871996-08-21 14:32:37 +00002037static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002038validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002039{
2040 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002041 && validate_numnodes(tree, 5, "funcdef")
2042 && validate_name(CHILD(tree, 0), "def")
2043 && validate_ntype(CHILD(tree, 1), NAME)
2044 && validate_colon(CHILD(tree, 3))
2045 && validate_parameters(CHILD(tree, 2))
2046 && validate_suite(CHILD(tree, 4)));
2047}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002048
2049
Guido van Rossum47478871996-08-21 14:32:37 +00002050static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002051validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002052{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002053 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002054 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002055 && ((nch == 3) || (nch == 4))
2056 && validate_name(CHILD(tree, 0), "lambda")
2057 && validate_colon(CHILD(tree, nch - 2))
2058 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002059
Guido van Rossum3d602e31996-07-21 02:33:56 +00002060 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002061 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002062 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002063 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002064
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002065 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002066}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002067
2068
Guido van Rossum3d602e31996-07-21 02:33:56 +00002069/* arglist:
2070 *
Fred Drakee7ab64e2000-04-25 04:14:46 +00002071 * (argument ',')* (argument* [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002072 */
Guido van Rossum47478871996-08-21 14:32:37 +00002073static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002074validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002075{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002076 int nch = NCH(tree);
Fred Drake7797d362000-07-04 18:48:46 +00002077 int i, ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002078 node *last;
2079
2080 if (nch <= 0)
2081 /* raise the right error from having an invalid number of children */
2082 return validate_numnodes(tree, nch + 1, "arglist");
2083
2084 last = CHILD(tree, nch - 1);
2085 if (TYPE(last) == test) {
2086 /* Extended call syntax introduced in Python 1.6 has been used;
2087 * validate and strip that off and continue;
2088 * adjust nch to perform the cut, and ensure resulting nch is even
2089 * (validation of the first part doesn't require that).
2090 */
2091 if (nch < 2) {
2092 validate_numnodes(tree, nch + 1, "arglist");
2093 return 0;
2094 }
2095 ok = validate_test(last);
2096 if (ok) {
2097 node *prev = CHILD(tree, nch - 2);
2098 /* next must be '*' or '**' */
2099 if (validate_doublestar(prev)) {
2100 nch -= 2;
2101 if (nch >= 3) {
2102 /* may include: '*' test ',' */
2103 last = CHILD(tree, nch - 1);
2104 prev = CHILD(tree, nch - 2);
2105 if (TYPE(prev) == test) {
2106 ok = validate_comma(last)
2107 && validate_test(prev)
2108 && validate_star(CHILD(tree, nch - 3));
2109 if (ok)
2110 nch -= 3;
2111 }
2112 /* otherwise, nothing special */
2113 }
2114 }
2115 else {
2116 /* must be only: '*' test */
2117 PyErr_Clear();
2118 ok = validate_star(prev);
2119 nch -= 2;
2120 }
2121 if (ok && is_odd(nch)) {
2122 /* Illegal number of nodes before extended call syntax;
2123 * validation of the "normal" arguments does not require
2124 * a trailing comma, but requiring an even number of
2125 * children will effect the same requirement.
2126 */
2127 return validate_numnodes(tree, nch + 1, "arglist");
2128 }
2129 }
2130 }
2131 /* what remains must be: (argument ",")* [argument [","]] */
2132 i = 0;
2133 while (ok && nch - i >= 2) {
2134 ok = validate_argument(CHILD(tree, i))
2135 && validate_comma(CHILD(tree, i + 1));
2136 i += 2;
2137 }
2138 if (ok && i < nch) {
2139 ok = validate_comma(CHILD(tree, i));
2140 ++i;
2141 }
2142 if (i != nch) {
2143 /* internal error! */
2144 ok = 0;
2145 err_string("arglist: internal error; nch != i");
2146 }
2147 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002148}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002149
2150
2151
2152/* argument:
2153 *
2154 * [test '='] test
2155 */
Guido van Rossum47478871996-08-21 14:32:37 +00002156static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002157validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002158{
2159 int nch = NCH(tree);
2160 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002161 && ((nch == 1) || (nch == 3))
2162 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002163
2164 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002165 res = (validate_equal(CHILD(tree, 1))
2166 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002167
2168 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002169}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002170
2171
2172
2173/* trailer:
2174 *
Guido van Rossum47478871996-08-21 14:32:37 +00002175 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002176 */
Guido van Rossum47478871996-08-21 14:32:37 +00002177static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002178validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002179{
2180 int nch = NCH(tree);
2181 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002182
2183 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002184 switch (TYPE(CHILD(tree, 0))) {
2185 case LPAR:
2186 res = validate_rparen(CHILD(tree, nch - 1));
2187 if (res && (nch == 3))
2188 res = validate_arglist(CHILD(tree, 1));
2189 break;
2190 case LSQB:
2191 res = (validate_numnodes(tree, 3, "trailer")
2192 && validate_subscriptlist(CHILD(tree, 1))
2193 && validate_ntype(CHILD(tree, 2), RSQB));
2194 break;
2195 case DOT:
2196 res = (validate_numnodes(tree, 2, "trailer")
2197 && validate_ntype(CHILD(tree, 1), NAME));
2198 break;
2199 default:
2200 res = 0;
2201 break;
2202 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002203 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002204 else {
2205 (void) validate_numnodes(tree, 2, "trailer");
2206 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002207 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002208}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002209
2210
Guido van Rossum47478871996-08-21 14:32:37 +00002211/* subscriptlist:
2212 *
2213 * subscript (',' subscript)* [',']
2214 */
2215static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002216validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002217{
2218 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002219 validate_subscript, "subscriptlist"));
2220}
Guido van Rossum47478871996-08-21 14:32:37 +00002221
2222
Guido van Rossum3d602e31996-07-21 02:33:56 +00002223/* subscript:
2224 *
Guido van Rossum47478871996-08-21 14:32:37 +00002225 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002226 */
Guido van Rossum47478871996-08-21 14:32:37 +00002227static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002228validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002229{
Guido van Rossum47478871996-08-21 14:32:37 +00002230 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002231 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002232 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002233
Guido van Rossum47478871996-08-21 14:32:37 +00002234 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002235 if (!PyErr_Occurred())
2236 err_string("invalid number of arguments for subscript node");
2237 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002238 }
Guido van Rossum47478871996-08-21 14:32:37 +00002239 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002240 /* take care of ('.' '.' '.') possibility */
2241 return (validate_numnodes(tree, 3, "subscript")
2242 && validate_dot(CHILD(tree, 0))
2243 && validate_dot(CHILD(tree, 1))
2244 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002245 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002246 if (TYPE(CHILD(tree, 0)) == test)
2247 res = validate_test(CHILD(tree, 0));
2248 else
2249 res = validate_colon(CHILD(tree, 0));
2250 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002251 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002252 /* Must be [test] ':' [test] [sliceop],
2253 * but at least one of the optional components will
2254 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002255 */
2256 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002257 res = validate_test(CHILD(tree, 0));
2258 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002259 }
2260 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002261 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002262 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002263 int rem = nch - ++offset;
2264 if (rem) {
2265 if (TYPE(CHILD(tree, offset)) == test) {
2266 res = validate_test(CHILD(tree, offset));
2267 ++offset;
2268 --rem;
2269 }
2270 if (res && rem)
2271 res = validate_sliceop(CHILD(tree, offset));
2272 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002273 }
2274 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002275}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002276
2277
Guido van Rossum47478871996-08-21 14:32:37 +00002278static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002279validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002280{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002281 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002282 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002283 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002284 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002285 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002286 }
Guido van Rossum47478871996-08-21 14:32:37 +00002287 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002288 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002289 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002290 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002291
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002292 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002293}
Guido van Rossum47478871996-08-21 14:32:37 +00002294
2295
2296static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002297validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002298{
2299 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002300 validate_expr, "exprlist"));
2301}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002302
2303
Guido van Rossum47478871996-08-21 14:32:37 +00002304static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002305validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002306{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002307 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002308 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002309 && (nch >= 3)
2310 && validate_test(CHILD(tree, 0))
2311 && validate_colon(CHILD(tree, 1))
2312 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002313
Guido van Rossum3d602e31996-07-21 02:33:56 +00002314 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002315 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002316 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002317 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002318
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002319 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002320 int pos = 3;
2321 /* ( ',' test ':' test )* */
2322 while (res && (pos < nch)) {
2323 res = (validate_comma(CHILD(tree, pos))
2324 && validate_test(CHILD(tree, pos + 1))
2325 && validate_colon(CHILD(tree, pos + 2))
2326 && validate_test(CHILD(tree, pos + 3)));
2327 pos += 4;
2328 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002329 }
2330 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002331}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002332
2333
Guido van Rossum47478871996-08-21 14:32:37 +00002334static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002335validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002336{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002337 int pos;
2338 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002339 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002340 && (nch >= 2)
2341 && validate_testlist(CHILD(tree, 0))
2342 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002343
Guido van Rossum3d602e31996-07-21 02:33:56 +00002344 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002345 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002346
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002347 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002348}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002349
2350
Guido van Rossum47478871996-08-21 14:32:37 +00002351static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002352validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002353{
Fred Drakeff9ea482000-04-19 13:54:15 +00002354 int nch = 0; /* num. children on current node */
2355 int res = 1; /* result value */
2356 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002357
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002358 while (res & (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002359 nch = NCH(tree);
2360 next = 0;
2361 switch (TYPE(tree)) {
2362 /*
2363 * Definition nodes.
2364 */
2365 case funcdef:
2366 res = validate_funcdef(tree);
2367 break;
2368 case classdef:
2369 res = validate_class(tree);
2370 break;
2371 /*
2372 * "Trivial" parse tree nodes.
2373 * (Why did I call these trivial?)
2374 */
2375 case stmt:
2376 res = validate_stmt(tree);
2377 break;
2378 case small_stmt:
2379 /*
2380 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
2381 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2382 */
2383 res = validate_small_stmt(tree);
2384 break;
2385 case flow_stmt:
2386 res = (validate_numnodes(tree, 1, "flow_stmt")
2387 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2388 || (TYPE(CHILD(tree, 0)) == continue_stmt)
2389 || (TYPE(CHILD(tree, 0)) == return_stmt)
2390 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2391 if (res)
2392 next = CHILD(tree, 0);
2393 else if (nch == 1)
2394 err_string("Illegal flow_stmt type.");
2395 break;
2396 /*
2397 * Compound statements.
2398 */
2399 case simple_stmt:
2400 res = validate_simple_stmt(tree);
2401 break;
2402 case compound_stmt:
2403 res = validate_compound_stmt(tree);
2404 break;
2405 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002406 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002407 */
2408 case expr_stmt:
2409 res = validate_expr_stmt(tree);
2410 break;
2411 case print_stmt:
2412 res = validate_print_stmt(tree);
2413 break;
2414 case del_stmt:
2415 res = validate_del_stmt(tree);
2416 break;
2417 case pass_stmt:
2418 res = (validate_numnodes(tree, 1, "pass")
2419 && validate_name(CHILD(tree, 0), "pass"));
2420 break;
2421 case break_stmt:
2422 res = (validate_numnodes(tree, 1, "break")
2423 && validate_name(CHILD(tree, 0), "break"));
2424 break;
2425 case continue_stmt:
2426 res = (validate_numnodes(tree, 1, "continue")
2427 && validate_name(CHILD(tree, 0), "continue"));
2428 break;
2429 case return_stmt:
2430 res = validate_return_stmt(tree);
2431 break;
2432 case raise_stmt:
2433 res = validate_raise_stmt(tree);
2434 break;
2435 case import_stmt:
2436 res = validate_import_stmt(tree);
2437 break;
2438 case global_stmt:
2439 res = validate_global_stmt(tree);
2440 break;
2441 case exec_stmt:
2442 res = validate_exec_stmt(tree);
2443 break;
2444 case assert_stmt:
2445 res = validate_assert_stmt(tree);
2446 break;
2447 case if_stmt:
2448 res = validate_if(tree);
2449 break;
2450 case while_stmt:
2451 res = validate_while(tree);
2452 break;
2453 case for_stmt:
2454 res = validate_for(tree);
2455 break;
2456 case try_stmt:
2457 res = validate_try(tree);
2458 break;
2459 case suite:
2460 res = validate_suite(tree);
2461 break;
2462 /*
2463 * Expression nodes.
2464 */
2465 case testlist:
2466 res = validate_testlist(tree);
2467 break;
2468 case test:
2469 res = validate_test(tree);
2470 break;
2471 case and_test:
2472 res = validate_and_test(tree);
2473 break;
2474 case not_test:
2475 res = validate_not_test(tree);
2476 break;
2477 case comparison:
2478 res = validate_comparison(tree);
2479 break;
2480 case exprlist:
2481 res = validate_exprlist(tree);
2482 break;
2483 case comp_op:
2484 res = validate_comp_op(tree);
2485 break;
2486 case expr:
2487 res = validate_expr(tree);
2488 break;
2489 case xor_expr:
2490 res = validate_xor_expr(tree);
2491 break;
2492 case and_expr:
2493 res = validate_and_expr(tree);
2494 break;
2495 case shift_expr:
2496 res = validate_shift_expr(tree);
2497 break;
2498 case arith_expr:
2499 res = validate_arith_expr(tree);
2500 break;
2501 case term:
2502 res = validate_term(tree);
2503 break;
2504 case factor:
2505 res = validate_factor(tree);
2506 break;
2507 case power:
2508 res = validate_power(tree);
2509 break;
2510 case atom:
2511 res = validate_atom(tree);
2512 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002513
Fred Drakeff9ea482000-04-19 13:54:15 +00002514 default:
2515 /* Hopefully never reached! */
2516 err_string("Unrecogniged node type.");
2517 res = 0;
2518 break;
2519 }
2520 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002521 }
2522 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002523}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002524
2525
Guido van Rossum47478871996-08-21 14:32:37 +00002526static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002527validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002528{
2529 int res = validate_eval_input(tree);
2530
2531 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002532 err_string("Could not validate expression tuple.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002533
2534 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002535}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002536
2537
Guido van Rossum3d602e31996-07-21 02:33:56 +00002538/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002539 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002540 */
Guido van Rossum47478871996-08-21 14:32:37 +00002541static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002542validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002543{
2544 int j = 0;
2545 int nch = NCH(tree) - 1;
2546 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002547 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002548
Guido van Rossum3d602e31996-07-21 02:33:56 +00002549 for ( ; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002550 if (TYPE(CHILD(tree, j)) == stmt)
2551 res = validate_stmt(CHILD(tree, j));
2552 else
2553 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002554 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002555 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002556 * user. Hopefully, this won't be needed. If a user reports getting
2557 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002558 */
2559 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002560 err_string("VALIDATION FAILURE: report this to the maintainer!.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002561
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002562 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002563}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002564
2565
Fred Drake43f8f9b1998-04-13 16:25:46 +00002566static PyObject*
2567pickle_constructor = NULL;
2568
2569
2570static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002571parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002572{
Fred Drake268397f1998-04-29 14:16:32 +00002573 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002574 PyObject *result = NULL;
2575 PyObject *ast = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002576 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002577
2578 if (PyArg_ParseTuple(args, "O!:_pickler", &PyAST_Type, &ast)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002579 PyObject *newargs;
2580 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002581
Fred Drake2a6875e1999-09-20 22:32:18 +00002582 if ((empty_dict = PyDict_New()) == NULL)
2583 goto finally;
2584 if ((newargs = Py_BuildValue("Oi", ast, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002585 goto finally;
2586 tuple = parser_ast2tuple((PyAST_Object*)NULL, newargs, empty_dict);
2587 if (tuple != NULL) {
2588 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2589 Py_DECREF(tuple);
2590 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002591 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002592 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002593 }
2594 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002595 Py_XDECREF(empty_dict);
2596
Fred Drake43f8f9b1998-04-13 16:25:46 +00002597 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002598}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002599
2600
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002601/* Functions exported by this module. Most of this should probably
2602 * be converted into an AST object with methods, but that is better
2603 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002604 * We'd really have to write a wrapper around it all anyway to allow
2605 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002606 */
2607static PyMethodDef parser_functions[] = {
Fred Drakeff9ea482000-04-19 13:54:15 +00002608 {"ast2tuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
2609 "Creates a tuple-tree representation of an AST."},
2610 {"ast2list", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
2611 "Creates a list-tree representation of an AST."},
2612 {"compileast", (PyCFunction)parser_compileast, PUBLIC_METHOD_TYPE,
2613 "Compiles an AST object into a code object."},
2614 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
2615 "Creates an AST object from an expression."},
2616 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
2617 "Determines if an AST object was created from an expression."},
2618 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
2619 "Determines if an AST object was created from a suite."},
2620 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
2621 "Creates an AST object from a suite."},
2622 {"sequence2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
2623 "Creates an AST object from a tree representation."},
2624 {"tuple2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
2625 "Creates an AST object from a tree representation."},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002626
Fred Drake43f8f9b1998-04-13 16:25:46 +00002627 /* private stuff: support pickle module */
Fred Drakeff9ea482000-04-19 13:54:15 +00002628 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Fred Drake43f8f9b1998-04-13 16:25:46 +00002629 "Returns the pickle magic to allow ast objects to be pickled."},
2630
Fred Drake268397f1998-04-29 14:16:32 +00002631 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002632 };
2633
2634
Guido van Rossum3886bb61998-12-04 18:50:17 +00002635DL_EXPORT(void)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002636initparser()
2637 {
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002638 PyObject* module;
2639 PyObject* dict;
Fred Drakeff9ea482000-04-19 13:54:15 +00002640
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002641 PyAST_Type.ob_type = &PyType_Type;
2642 module = Py_InitModule("parser", parser_functions);
2643 dict = PyModule_GetDict(module);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002644
Fred Drake7a15ba51999-09-09 14:21:52 +00002645 if (parser_error == 0)
2646 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
2647 else
2648 puts("parser module initialized more than once!");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002649
2650 if ((parser_error == 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002651 || (PyDict_SetItemString(dict, "ParserError", parser_error) != 0)) {
2652 /*
2653 * This is serious.
2654 */
2655 Py_FatalError("can't define parser.ParserError");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002656 }
2657 /*
2658 * Nice to have, but don't cry if we fail.
2659 */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002660 Py_INCREF(&PyAST_Type);
2661 PyDict_SetItemString(dict, "ASTType", (PyObject*)&PyAST_Type);
2662
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002663 PyDict_SetItemString(dict, "__copyright__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002664 PyString_FromString(parser_copyright_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002665 PyDict_SetItemString(dict, "__doc__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002666 PyString_FromString(parser_doc_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002667 PyDict_SetItemString(dict, "__version__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002668 PyString_FromString(parser_version_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002669
Fred Drake43f8f9b1998-04-13 16:25:46 +00002670 /* register to support pickling */
2671 module = PyImport_ImportModule("copy_reg");
2672 if (module != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002673 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002674
Fred Drakeff9ea482000-04-19 13:54:15 +00002675 func = PyObject_GetAttrString(module, "pickle");
2676 pickle_constructor = PyDict_GetItemString(dict, "sequence2ast");
2677 pickler = PyDict_GetItemString(dict, "_pickler");
2678 Py_XINCREF(pickle_constructor);
2679 if ((func != NULL) && (pickle_constructor != NULL)
2680 && (pickler != NULL)) {
2681 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002682
Fred Drakeff9ea482000-04-19 13:54:15 +00002683 res = PyObject_CallFunction(
2684 func, "OOO", &PyAST_Type, pickler, pickle_constructor);
2685 Py_XDECREF(res);
2686 }
2687 Py_XDECREF(func);
2688 Py_DECREF(module);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002689 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002690}