blob: 8baa693f9ccfdadfc40c3479e04334b14b5b45bc [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
Guido van Rossum3d602e31996-07-21 02:33:56 +000069/* The function below is copyrigthed 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*
480parser_getattr(self, name)
481 PyObject *self;
482 char *name;
483{
Fred Drake503d8d61998-04-13 18:45:18 +0000484 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000485}
Fred Drake503d8d61998-04-13 18:45:18 +0000486
487
Guido van Rossum3d602e31996-07-21 02:33:56 +0000488/* err_string(char* message)
489 *
490 * Sets the error string for an exception of type ParserError.
491 *
492 */
493static void
494err_string(message)
495 char *message;
Guido van Rossum47478871996-08-21 14:32:37 +0000496{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000497 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000498}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000499
500
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000501/* PyObject* parser_do_parse(PyObject* args, int type)
502 *
503 * Internal function to actually execute the parse and return the result if
504 * successful, or set an exception if not.
505 *
506 */
507static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000508parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000509{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000510 char* string = 0;
511 PyObject* res = 0;
512
Fred Drake7a15ba51999-09-09 14:21:52 +0000513 static char *keywords[] = {"source", NULL};
514
515 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000516 node* n = PyParser_SimpleParseString(string,
517 (type == PyAST_EXPR)
518 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000519
Fred Drakeff9ea482000-04-19 13:54:15 +0000520 if (n != 0)
521 res = parser_newastobject(n, type);
522 else
523 err_string("Could not parse string.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000524 }
525 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000526}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000527
528
529/* PyObject* parser_expr(PyObject* self, PyObject* args)
530 * PyObject* parser_suite(PyObject* self, PyObject* args)
531 *
532 * External interfaces to the parser itself. Which is called determines if
533 * the parser attempts to recognize an expression ('eval' form) or statement
534 * suite ('exec' form). The real work is done by parser_do_parse() above.
535 *
536 */
537static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000538parser_expr(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000539{
Fred Drake268397f1998-04-29 14:16:32 +0000540 NOTE(ARGUNUSED(self))
Fred Drake7a15ba51999-09-09 14:21:52 +0000541 return (parser_do_parse(args, kw, "s:expr", PyAST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000542}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000543
544
545static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000546parser_suite(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000547{
Fred Drake268397f1998-04-29 14:16:32 +0000548 NOTE(ARGUNUSED(self))
Fred Drake7a15ba51999-09-09 14:21:52 +0000549 return (parser_do_parse(args, kw, "s:suite", PyAST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000550}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000551
552
553
554/* This is the messy part of the code. Conversion from a tuple to an AST
555 * object requires that the input tuple be valid without having to rely on
556 * catching an exception from the compiler. This is done to allow the
557 * compiler itself to remain fast, since most of its input will come from
558 * the parser directly, and therefore be known to be syntactically correct.
559 * This validation is done to ensure that we don't core dump the compile
560 * phase, returning an exception instead.
561 *
562 * Two aspects can be broken out in this code: creating a node tree from
563 * the tuple passed in, and verifying that it is indeed valid. It may be
564 * advantageous to expand the number of AST types to include funcdefs and
565 * lambdadefs to take advantage of the optimizer, recognizing those ASTs
566 * here. They are not necessary, and not quite as useful in a raw form.
567 * For now, let's get expressions and suites working reliably.
568 */
569
570
Fred Drakeff9ea482000-04-19 13:54:15 +0000571staticforward node* build_node_tree(PyObject *tuple);
572staticforward int validate_expr_tree(node *tree);
573staticforward int validate_file_input(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000574
575
576/* PyObject* parser_tuple2ast(PyObject* self, PyObject* args)
577 *
578 * This is the public function, called from the Python code. It receives a
579 * single tuple object from the caller, and creates an AST object if the
580 * tuple can be validated. It does this by checking the first code of the
581 * tuple, and, if acceptable, builds the internal representation. If this
582 * step succeeds, the internal representation is validated as fully as
583 * possible with the various validate_*() routines defined below.
584 *
585 * This function must be changed if support is to be added for PyAST_FRAGMENT
586 * AST objects.
587 *
588 */
589static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000590parser_tuple2ast(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000591{
Fred Drake268397f1998-04-29 14:16:32 +0000592 NOTE(ARGUNUSED(self))
Guido van Rossum47478871996-08-21 14:32:37 +0000593 PyObject *ast = 0;
594 PyObject *tuple = 0;
595 PyObject *temp = 0;
596 int ok;
Guido van Rossuma376cc51996-12-05 23:43:35 +0000597 int start_sym = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000598
Fred Drake7a15ba51999-09-09 14:21:52 +0000599 static char *keywords[] = {"sequence", NULL};
600
601 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:tuple2ast", keywords,
602 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000603 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000604 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000605 PyErr_SetString(PyExc_ValueError,
606 "tuple2ast() requires a single sequence argument");
607 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000608 }
609 /*
Fred Drakeff9ea482000-04-19 13:54:15 +0000610 * This mess of tests is written this way so we can use the abstract
611 * object interface (AOI). Unfortunately, the AOI increments reference
612 * counts, which requires that we store a pointer to retrieved object
613 * so we can DECREF it after the check. But we really should accept
614 * lists as well as tuples at the very least.
Guido van Rossum47478871996-08-21 14:32:37 +0000615 */
616 ok = PyObject_Length(tuple) >= 2;
617 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000618 temp = PySequence_GetItem(tuple, 0);
619 ok = (temp != NULL) && PyInt_Check(temp);
620 if (ok)
621 /* this is used after the initial checks: */
622 start_sym = PyInt_AS_LONG(temp);
623 Py_XDECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000624 }
625 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000626 temp = PySequence_GetItem(tuple, 1);
627 ok = (temp != NULL) && PySequence_Check(temp);
628 Py_XDECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000629 }
630 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000631 temp = PySequence_GetItem(tuple, 1);
632 ok = (temp != NULL) && PyObject_Length(temp) >= 2;
633 if (ok) {
634 PyObject *temp2 = PySequence_GetItem(temp, 0);
635 if (temp2 != NULL) {
636 ok = PyInt_Check(temp2);
637 Py_DECREF(temp2);
638 }
639 }
640 Py_XDECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000641 }
642 /* If we've failed at some point, get out of here. */
643 if (!ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000644 err_string("malformed sequence for tuple2ast()");
645 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000646 }
647 /*
648 * This might be a valid parse tree, but let's do a quick check
649 * before we jump the gun.
650 */
651 if (start_sym == eval_input) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000652 /* Might be an eval form. */
653 node* expression = build_node_tree(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000654
Fred Drakeff9ea482000-04-19 13:54:15 +0000655 if ((expression != 0) && validate_expr_tree(expression))
656 ast = parser_newastobject(expression, PyAST_EXPR);
Guido van Rossum47478871996-08-21 14:32:37 +0000657 }
658 else if (start_sym == file_input) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000659 /* This looks like an exec form so far. */
660 node* suite_tree = build_node_tree(tuple);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000661
Fred Drakeff9ea482000-04-19 13:54:15 +0000662 if ((suite_tree != 0) && validate_file_input(suite_tree))
663 ast = parser_newastobject(suite_tree, PyAST_SUITE);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000664 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000665 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000666 /* This is a fragment, and is not yet supported. Maybe they
667 * will be if I find a use for them.
668 */
669 err_string("Fragmentary parse trees not supported.");
Guido van Rossum47478871996-08-21 14:32:37 +0000670
671 /* Make sure we throw an exception on all errors. We should never
672 * get this, but we'd do well to be sure something is done.
673 */
674 if ((ast == 0) && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000675 err_string("Unspecified ast error occurred.");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000676
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000677 return (ast);
Fred Drakeff9ea482000-04-19 13:54:15 +0000678}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000679
680
681/* int check_terminal_tuple()
682 *
Guido van Rossum47478871996-08-21 14:32:37 +0000683 * Check a tuple to determine that it is indeed a valid terminal
684 * node. The node is known to be required as a terminal, so we throw
685 * an exception if there is a failure.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000686 *
Guido van Rossum47478871996-08-21 14:32:37 +0000687 * The format of an acceptable terminal tuple is "(is[i])": the fact
688 * that elem is a tuple and the integer is a valid terminal symbol
689 * has been established before this function is called. We must
690 * check the length of the tuple and the type of the second element
691 * and optional third element. We do *NOT* check the actual text of
692 * the string element, which we could do in many cases. This is done
693 * by the validate_*() functions which operate on the internal
694 * representation.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000695 */
696static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000697check_terminal_tuple(PyObject *elem)
Guido van Rossum47478871996-08-21 14:32:37 +0000698{
699 int len = PyObject_Length(elem);
700 int res = 1;
701 char* str = "Illegal terminal symbol; bad node length.";
Guido van Rossum3d602e31996-07-21 02:33:56 +0000702
Guido van Rossum47478871996-08-21 14:32:37 +0000703 if ((len == 2) || (len == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000704 PyObject *temp = PySequence_GetItem(elem, 1);
705 res = PyString_Check(temp);
706 str = "Illegal terminal symbol; expected a string.";
707 if (res && (len == 3)) {
708 PyObject* third = PySequence_GetItem(elem, 2);
709 res = PyInt_Check(third);
710 str = "Invalid third element of terminal node.";
711 Py_XDECREF(third);
712 }
713 Py_XDECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000714 }
715 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000716 res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000717 }
718 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000719 elem = Py_BuildValue("(os)", elem, str);
720 PyErr_SetObject(parser_error, elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000721 }
722 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000723}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000724
725
726/* node* build_node_children()
727 *
728 * Iterate across the children of the current non-terminal node and build
729 * their structures. If successful, return the root of this portion of
730 * the tree, otherwise, 0. Any required exception will be specified already,
731 * and no memory will have been deallocated.
732 *
733 */
734static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000735build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000736{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000737 int len = PyObject_Length(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000738 int i;
739
740 for (i = 1; i < len; ++i) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000741 /* elem must always be a tuple, however simple */
742 PyObject* elem = PySequence_GetItem(tuple, i);
743 int ok = elem != NULL;
744 long type = 0;
745 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000746
Fred Drakeff9ea482000-04-19 13:54:15 +0000747 if (ok)
748 ok = PySequence_Check(elem);
749 if (ok) {
750 PyObject *temp = PySequence_GetItem(elem, 0);
751 if (temp == NULL)
752 ok = 0;
753 else {
754 ok = PyInt_Check(temp);
755 if (ok)
756 type = PyInt_AS_LONG(temp);
757 Py_DECREF(temp);
758 }
759 }
760 if (!ok) {
761 PyErr_SetObject(parser_error,
762 Py_BuildValue("(os)", elem,
763 "Illegal node construct."));
764 Py_XDECREF(elem);
765 return (0);
766 }
767 if (ISTERMINAL(type)) {
768 if (check_terminal_tuple(elem)) {
769 PyObject *temp = PySequence_GetItem(elem, 1);
Guido van Rossum47478871996-08-21 14:32:37 +0000770
Fred Drakeff9ea482000-04-19 13:54:15 +0000771 /* check_terminal_tuple() already verified it's a string */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000772 strn = (char *)PyMem_MALLOC(PyString_GET_SIZE(temp) + 1);
Fred Drakeff9ea482000-04-19 13:54:15 +0000773 if (strn != NULL)
774 (void) strcpy(strn, PyString_AS_STRING(temp));
Guido van Rossumb18618d2000-05-03 23:44:39 +0000775 Py_DECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000776
Fred Drakeff9ea482000-04-19 13:54:15 +0000777 if (PyObject_Length(elem) == 3) {
778 PyObject* temp = PySequence_GetItem(elem, 2);
779 *line_num = PyInt_AsLong(temp);
780 Py_DECREF(temp);
781 }
782 }
783 else {
784 Py_XDECREF(elem);
785 return (0);
786 }
787 }
788 else if (!ISNONTERMINAL(type)) {
789 /*
790 * It has to be one or the other; this is an error.
791 * Throw an exception.
792 */
793 PyErr_SetObject(parser_error,
794 Py_BuildValue("(os)", elem,
795 "Unknown node type."));
796 Py_XDECREF(elem);
797 return (0);
798 }
799 PyNode_AddChild(root, type, strn, *line_num);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000800
Fred Drakeff9ea482000-04-19 13:54:15 +0000801 if (ISNONTERMINAL(type)) {
802 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000803
Fred Drakeff9ea482000-04-19 13:54:15 +0000804 if (new_child != build_node_children(elem, new_child, line_num)) {
805 Py_XDECREF(elem);
806 return (0);
807 }
808 }
809 else if (type == NEWLINE) { /* It's true: we increment the */
810 ++(*line_num); /* line number *after* the newline! */
811 }
812 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000813 }
814 return (root);
Fred Drakeff9ea482000-04-19 13:54:15 +0000815}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000816
817
818static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000819build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000820{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000821 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000822 PyObject *temp = PySequence_GetItem(tuple, 0);
823 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000824
Guido van Rossum47478871996-08-21 14:32:37 +0000825 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000826 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000827 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000828 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000829 /*
830 * The tuple is simple, but it doesn't start with a start symbol.
831 * Throw an exception now and be done with it.
832 */
833 tuple = Py_BuildValue("(os)", tuple,
834 "Illegal ast tuple; cannot start with terminal symbol.");
835 PyErr_SetObject(parser_error, tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000836 }
837 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000838 /*
839 * Not efficient, but that can be handled later.
840 */
841 int line_num = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000842
Fred Drakeff9ea482000-04-19 13:54:15 +0000843 res = PyNode_New(num);
844 if (res != build_node_children(tuple, res, &line_num)) {
845 PyNode_Free(res);
846 res = 0;
847 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000848 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000849 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000850 /* The tuple is illegal -- if the number is neither TERMINAL nor
851 * NONTERMINAL, we can't use it.
852 */
853 PyErr_SetObject(parser_error,
854 Py_BuildValue("(os)", tuple,
855 "Illegal component tuple."));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000856
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000857 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000858}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000859
860
Guido van Rossum360a9341996-08-21 19:04:10 +0000861#ifdef HAVE_OLD_CPP
Fred Drakeff9ea482000-04-19 13:54:15 +0000862#define VALIDATER(n) static int validate_/**/n(node *tree)
Guido van Rossum360a9341996-08-21 19:04:10 +0000863#else
Fred Drakeff9ea482000-04-19 13:54:15 +0000864#define VALIDATER(n) static int validate_##n(node *tree)
Guido van Rossum360a9341996-08-21 19:04:10 +0000865#endif
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000866
867
868/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000869 * Validation routines used within the validation section:
870 */
Fred Drakeff9ea482000-04-19 13:54:15 +0000871staticforward int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000872
Fred Drakeff9ea482000-04-19 13:54:15 +0000873#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
874#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
875#define validate_colon(ch) validate_terminal(ch, COLON, ":")
876#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
877#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
878#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
879#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
880#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
881#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
882#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
883#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
884#define validate_star(ch) validate_terminal(ch, STAR, "*")
885#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
886#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
887#define validate_dot(ch) validate_terminal(ch, DOT, ".")
888#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000889
Fred Drakeff9ea482000-04-19 13:54:15 +0000890VALIDATER(node); VALIDATER(small_stmt);
891VALIDATER(class); VALIDATER(node);
892VALIDATER(parameters); VALIDATER(suite);
893VALIDATER(testlist); VALIDATER(varargslist);
894VALIDATER(fpdef); VALIDATER(fplist);
895VALIDATER(stmt); VALIDATER(simple_stmt);
896VALIDATER(expr_stmt); VALIDATER(power);
897VALIDATER(print_stmt); VALIDATER(del_stmt);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000898VALIDATER(return_stmt);
Fred Drakeff9ea482000-04-19 13:54:15 +0000899VALIDATER(raise_stmt); VALIDATER(import_stmt);
Guido van Rossum2a288461996-08-21 21:55:43 +0000900VALIDATER(global_stmt);
Guido van Rossum925e5471997-04-02 05:32:13 +0000901VALIDATER(assert_stmt);
Fred Drakeff9ea482000-04-19 13:54:15 +0000902VALIDATER(exec_stmt); VALIDATER(compound_stmt);
903VALIDATER(while); VALIDATER(for);
904VALIDATER(try); VALIDATER(except_clause);
905VALIDATER(test); VALIDATER(and_test);
906VALIDATER(not_test); VALIDATER(comparison);
907VALIDATER(comp_op); VALIDATER(expr);
908VALIDATER(xor_expr); VALIDATER(and_expr);
909VALIDATER(shift_expr); VALIDATER(arith_expr);
910VALIDATER(term); VALIDATER(factor);
911VALIDATER(atom); VALIDATER(lambdef);
912VALIDATER(trailer); VALIDATER(subscript);
913VALIDATER(subscriptlist); VALIDATER(sliceop);
914VALIDATER(exprlist); VALIDATER(dictmaker);
915VALIDATER(arglist); VALIDATER(argument);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000916
917
Fred Drakeff9ea482000-04-19 13:54:15 +0000918#define is_even(n) (((n) & 1) == 0)
919#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000920
921
922static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000923validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000924{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000925 int res = (TYPE(n) == t);
926
927 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000928 char buffer[128];
929 (void) sprintf(buffer, "Expected node type %d, got %d.", t, TYPE(n));
930 err_string(buffer);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000931 }
932 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000933}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000934
935
Fred Drakee7ab64e2000-04-25 04:14:46 +0000936/* Verifies that the number of child nodes is exactly 'num', raising
937 * an exception if it isn't. The exception message does not indicate
938 * the exact number of nodes, allowing this to be used to raise the
939 * "right" exception when the wrong number of nodes is present in a
940 * specific variant of a statement's syntax. This is commonly used
941 * in that fashion.
942 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000943static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000944validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000945{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000946 if (NCH(n) != num) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000947 char buff[60];
948 (void) sprintf(buff, "Illegal number of children for %s node.", name);
949 err_string(buff);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000950 }
951 return (NCH(n) == num);
Fred Drakeff9ea482000-04-19 13:54:15 +0000952}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000953
954
955static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000956validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000957{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000958 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000959 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000960
961 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000962 char buffer[60];
963 (void) sprintf(buffer, "Illegal terminal: expected \"%s\"", string);
964 err_string(buffer);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000965 }
966 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000967}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000968
969
Guido van Rossum47478871996-08-21 14:32:37 +0000970/* X (',' X) [',']
971 */
972static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000973validate_repeating_list(node *tree, int ntype, int (*vfunc)(),
974 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000975{
976 int nch = NCH(tree);
977 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000978 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000979
980 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000981 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000982 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000983 if (is_even(nch))
984 res = validate_comma(CHILD(tree, --nch));
985 if (res && nch > 1) {
986 int pos = 1;
987 for ( ; res && pos < nch; pos += 2)
988 res = (validate_comma(CHILD(tree, pos))
989 && vfunc(CHILD(tree, pos + 1)));
990 }
Guido van Rossum47478871996-08-21 14:32:37 +0000991 }
992 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000993}
Guido van Rossum47478871996-08-21 14:32:37 +0000994
995
Guido van Rossum3d602e31996-07-21 02:33:56 +0000996/* VALIDATE(class)
997 *
998 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000999 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00001000 */
Guido van Rossum47478871996-08-21 14:32:37 +00001001static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001002validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001003{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001004 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001005 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001006
Guido van Rossum3d602e31996-07-21 02:33:56 +00001007 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001008 res = (validate_name(CHILD(tree, 0), "class")
1009 && validate_ntype(CHILD(tree, 1), NAME)
1010 && validate_colon(CHILD(tree, nch - 2))
1011 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001012 }
1013 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001014 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001015 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001016 res = (validate_lparen(CHILD(tree, 2))
1017 && validate_testlist(CHILD(tree, 3))
1018 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001019 }
1020 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001021}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001022
1023
Guido van Rossum3d602e31996-07-21 02:33:56 +00001024/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001025 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001026 */
Guido van Rossum47478871996-08-21 14:32:37 +00001027static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001028validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001029{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001030 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001031 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001032 && (nch >= 4)
1033 && validate_name(CHILD(tree, 0), "if")
1034 && validate_test(CHILD(tree, 1))
1035 && validate_colon(CHILD(tree, 2))
1036 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001037
1038 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001039 /* ... 'else' ':' suite */
1040 res = (validate_name(CHILD(tree, nch - 3), "else")
1041 && validate_colon(CHILD(tree, nch - 2))
1042 && validate_suite(CHILD(tree, nch - 1)));
1043 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001044 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001045 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001046 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001047 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001048 /* Will catch the case for nch < 4 */
1049 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001050 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001051 /* ... ('elif' test ':' suite)+ ... */
1052 int j = 4;
1053 while ((j < nch) && res) {
1054 res = (validate_name(CHILD(tree, j), "elif")
1055 && validate_colon(CHILD(tree, j + 2))
1056 && validate_test(CHILD(tree, j + 1))
1057 && validate_suite(CHILD(tree, j + 3)));
1058 j += 4;
1059 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001060 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001061 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001062}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001063
1064
Guido van Rossum3d602e31996-07-21 02:33:56 +00001065/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001066 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001067 *
1068 */
Guido van Rossum47478871996-08-21 14:32:37 +00001069static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001070validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001071{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001072 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001073 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001074
Guido van Rossum3d602e31996-07-21 02:33:56 +00001075 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001076 res = (validate_lparen(CHILD(tree, 0))
1077 && validate_rparen(CHILD(tree, nch - 1)));
1078 if (res && (nch == 3))
1079 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001080 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001081 else {
1082 (void) validate_numnodes(tree, 2, "parameters");
1083 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001084 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001085}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001086
1087
Guido van Rossum3d602e31996-07-21 02:33:56 +00001088/* VALIDATE(suite)
1089 *
1090 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001091 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001092 * | NEWLINE INDENT stmt+ DEDENT
1093 */
Guido van Rossum47478871996-08-21 14:32:37 +00001094static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001095validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001096{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001097 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001098 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001099
Guido van Rossum3d602e31996-07-21 02:33:56 +00001100 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001101 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001102 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001103 /* NEWLINE INDENT stmt+ DEDENT */
1104 res = (validate_newline(CHILD(tree, 0))
1105 && validate_indent(CHILD(tree, 1))
1106 && validate_stmt(CHILD(tree, 2))
1107 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001108
Fred Drakeff9ea482000-04-19 13:54:15 +00001109 if (res && (nch > 4)) {
1110 int i = 3;
1111 --nch; /* forget the DEDENT */
1112 for ( ; res && (i < nch); ++i)
1113 res = validate_stmt(CHILD(tree, i));
1114 }
1115 else if (nch < 4)
1116 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001117 }
1118 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001119}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001120
1121
Guido van Rossum47478871996-08-21 14:32:37 +00001122static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001123validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001124{
Guido van Rossum47478871996-08-21 14:32:37 +00001125 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001126 validate_test, "testlist"));
1127}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001128
1129
Guido van Rossum3d602e31996-07-21 02:33:56 +00001130/* VALIDATE(varargslist)
1131 *
1132 * varargslist:
Fred Drakeff9ea482000-04-19 13:54:15 +00001133 * (fpdef ['=' test] ',')* ('*' NAME [',' '*' '*' NAME] | '*' '*' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001134 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1135 *
1136 * (fpdef ['=' test] ',')*
1137 * ('*' NAME [',' ('**'|'*' '*') NAME]
1138 * | ('**'|'*' '*') NAME)
1139 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1140 *
1141 */
Guido van Rossum47478871996-08-21 14:32:37 +00001142static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001143validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001144{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001145 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001146 int res = validate_ntype(tree, varargslist) && (nch != 0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001147
Guido van Rossum3d602e31996-07-21 02:33:56 +00001148 if (res && (nch >= 2) && (TYPE(CHILD(tree, nch - 1)) == NAME)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001149 /* (fpdef ['=' test] ',')*
1150 * ('*' NAME [',' '*' '*' NAME] | '*' '*' NAME)
1151 */
1152 int pos = 0;
1153 int remaining = nch;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001154
Fred Drakeff9ea482000-04-19 13:54:15 +00001155 while (res && (TYPE(CHILD(tree, pos)) == fpdef)) {
1156 res = validate_fpdef(CHILD(tree, pos));
1157 if (res) {
1158 if (TYPE(CHILD(tree, pos + 1)) == EQUAL) {
1159 res = validate_test(CHILD(tree, pos + 2));
1160 pos += 2;
1161 }
1162 res = res && validate_comma(CHILD(tree, pos + 1));
1163 pos += 2;
1164 }
1165 }
1166 if (res) {
1167 remaining = nch - pos;
1168 res = ((remaining == 2) || (remaining == 3)
1169 || (remaining == 5) || (remaining == 6));
1170 if (!res)
1171 (void) validate_numnodes(tree, 2, "varargslist");
1172 else if (TYPE(CHILD(tree, pos)) == DOUBLESTAR)
1173 return ((remaining == 2)
1174 && validate_ntype(CHILD(tree, pos+1), NAME));
1175 else {
1176 res = validate_star(CHILD(tree, pos++));
1177 --remaining;
1178 }
1179 }
1180 if (res) {
1181 if (remaining == 2) {
1182 res = (validate_star(CHILD(tree, pos))
1183 && validate_ntype(CHILD(tree, pos + 1), NAME));
1184 }
1185 else {
1186 res = validate_ntype(CHILD(tree, pos++), NAME);
1187 if (res && (remaining >= 4)) {
1188 res = validate_comma(CHILD(tree, pos));
1189 if (--remaining == 3)
1190 res = (validate_star(CHILD(tree, pos + 1))
1191 && validate_star(CHILD(tree, pos + 2)));
1192 else
1193 res = validate_ntype(CHILD(tree, pos + 1), DOUBLESTAR);
1194 }
1195 }
1196 }
1197 if (!res && !PyErr_Occurred())
1198 err_string("Incorrect validation of variable arguments list.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001199 }
1200 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001201 /* fpdef ['=' test] (',' fpdef ['=' test])* [','] */
1202 if (TYPE(CHILD(tree, nch - 1)) == COMMA)
1203 --nch;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001204
Fred Drakeff9ea482000-04-19 13:54:15 +00001205 /* fpdef ['=' test] (',' fpdef ['=' test])* */
1206 res = (is_odd(nch)
1207 && validate_fpdef(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001208
Fred Drakeff9ea482000-04-19 13:54:15 +00001209 if (res && (nch > 1)) {
1210 int pos = 1;
1211 if (TYPE(CHILD(tree, 1)) == EQUAL) {
1212 res = validate_test(CHILD(tree, 2));
1213 pos += 2;
1214 }
1215 /* ... (',' fpdef ['=' test])* */
1216 for ( ; res && (pos < nch); pos += 2) {
1217 /* ',' fpdef */
1218 res = (validate_comma(CHILD(tree, pos))
1219 && validate_fpdef(CHILD(tree, pos + 1)));
1220 if (res
1221 && ((nch - pos) > 2)
1222 && (TYPE(CHILD(tree, pos + 2)) == EQUAL)) {
1223 /* ['=' test] */
1224 res = validate_test(CHILD(tree, pos + 3));
1225 pos += 2;
1226 }
1227 }
1228 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001229 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001230 else {
1231 err_string("Improperly formed argument list.");
1232 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001233 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001234}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001235
1236
Guido van Rossum3d602e31996-07-21 02:33:56 +00001237/* VALIDATE(fpdef)
1238 *
1239 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001240 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001241 * | '(' fplist ')'
1242 */
Guido van Rossum47478871996-08-21 14:32:37 +00001243static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001244validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001245{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001246 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001247 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001248
Guido van Rossum3d602e31996-07-21 02:33:56 +00001249 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001250 if (nch == 1)
1251 res = validate_ntype(CHILD(tree, 0), NAME);
1252 else if (nch == 3)
1253 res = (validate_lparen(CHILD(tree, 0))
1254 && validate_fplist(CHILD(tree, 1))
1255 && validate_rparen(CHILD(tree, 2)));
1256 else
1257 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001258 }
1259 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001260}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001261
1262
Guido van Rossum47478871996-08-21 14:32:37 +00001263static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001264validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001265{
Guido van Rossum47478871996-08-21 14:32:37 +00001266 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001267 validate_fpdef, "fplist"));
1268}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001269
1270
Guido van Rossum3d602e31996-07-21 02:33:56 +00001271/* simple_stmt | compound_stmt
1272 *
1273 */
Guido van Rossum47478871996-08-21 14:32:37 +00001274static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001275validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001276{
1277 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001278 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001279
Guido van Rossum3d602e31996-07-21 02:33:56 +00001280 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001281 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001282
Fred Drakeff9ea482000-04-19 13:54:15 +00001283 if (TYPE(tree) == simple_stmt)
1284 res = validate_simple_stmt(tree);
1285 else
1286 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001287 }
1288 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001289}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001290
1291
Guido van Rossum3d602e31996-07-21 02:33:56 +00001292/* small_stmt (';' small_stmt)* [';'] NEWLINE
1293 *
1294 */
Guido van Rossum47478871996-08-21 14:32:37 +00001295static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001296validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001297{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001298 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001299 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001300 && (nch >= 2)
1301 && validate_small_stmt(CHILD(tree, 0))
1302 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001303
Guido van Rossum3d602e31996-07-21 02:33:56 +00001304 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001305 res = validate_numnodes(tree, 2, "simple_stmt");
1306 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001307 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001308 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001309 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001310 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001311
Fred Drakeff9ea482000-04-19 13:54:15 +00001312 for (i = 1; res && (i < nch); i += 2)
1313 res = (validate_semi(CHILD(tree, i))
1314 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001315 }
1316 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001317}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001318
1319
Guido van Rossum47478871996-08-21 14:32:37 +00001320static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001321validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001322{
1323 int nch = NCH(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +00001324 int res = (validate_numnodes(tree, 1, "small_stmt")
1325 && ((TYPE(CHILD(tree, 0)) == expr_stmt)
1326 || (TYPE(CHILD(tree, 0)) == print_stmt)
1327 || (TYPE(CHILD(tree, 0)) == del_stmt)
1328 || (TYPE(CHILD(tree, 0)) == pass_stmt)
1329 || (TYPE(CHILD(tree, 0)) == flow_stmt)
1330 || (TYPE(CHILD(tree, 0)) == import_stmt)
1331 || (TYPE(CHILD(tree, 0)) == global_stmt)
1332 || (TYPE(CHILD(tree, 0)) == assert_stmt)
1333 || (TYPE(CHILD(tree, 0)) == exec_stmt)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001334
1335 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001336 res = validate_node(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001337 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001338 char buffer[60];
1339 (void) sprintf(buffer, "Unrecognized child node of small_stmt: %d.",
1340 TYPE(CHILD(tree, 0)));
1341 err_string(buffer);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001342 }
1343 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001344}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001345
1346
1347/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001348 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001349 */
Guido van Rossum47478871996-08-21 14:32:37 +00001350static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001351validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001352{
1353 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001354 && validate_numnodes(tree, 1, "compound_stmt"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001355
1356 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001357 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001358
1359 tree = CHILD(tree, 0);
1360 res = ((TYPE(tree) == if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001361 || (TYPE(tree) == while_stmt)
1362 || (TYPE(tree) == for_stmt)
1363 || (TYPE(tree) == try_stmt)
1364 || (TYPE(tree) == funcdef)
1365 || (TYPE(tree) == classdef));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001366 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001367 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001368 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001369 char buffer[60];
1370 (void) sprintf(buffer, "Illegal compound statement type: %d.",
1371 TYPE(tree));
1372 err_string(buffer);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001373 }
1374 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001375}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001376
1377
Guido van Rossum47478871996-08-21 14:32:37 +00001378static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001379validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001380{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001381 int j;
1382 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001383 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001384 && is_odd(nch)
1385 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001386
Guido van Rossum3d602e31996-07-21 02:33:56 +00001387 for (j = 1; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001388 res = (validate_equal(CHILD(tree, j))
1389 && validate_testlist(CHILD(tree, j + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001390
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001391 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001392}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001393
1394
Guido van Rossum3d602e31996-07-21 02:33:56 +00001395/* print_stmt:
1396 *
Fred Drakeff9ea482000-04-19 13:54:15 +00001397 * 'print' (test ',')* [test]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001398 *
1399 */
Guido van Rossum47478871996-08-21 14:32:37 +00001400static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001401validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001402{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001403 int j;
1404 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001405 int res = (validate_ntype(tree, print_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001406 && (nch != 0)
1407 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001408
1409 if (res && is_even(nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001410 res = validate_test(CHILD(tree, nch - 1));
1411 --nch;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001412 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001413 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001414 (void) validate_numnodes(tree, 1, "print_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001415 for (j = 1; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001416 res = (validate_test(CHILD(tree, j))
1417 && validate_ntype(CHILD(tree, j + 1), COMMA));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001418
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001419 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001420}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001421
1422
Guido van Rossum47478871996-08-21 14:32:37 +00001423static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001424validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001425{
1426 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001427 && validate_name(CHILD(tree, 0), "del")
1428 && validate_exprlist(CHILD(tree, 1)));
1429}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001430
1431
Guido van Rossum47478871996-08-21 14:32:37 +00001432static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001433validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001434{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001435 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001436 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001437 && ((nch == 1) || (nch == 2))
1438 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001439
Guido van Rossum3d602e31996-07-21 02:33:56 +00001440 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001441 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001442
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001443 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001444}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001445
1446
Guido van Rossum47478871996-08-21 14:32:37 +00001447static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001448validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001449{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001450 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001451 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001452 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001453
Guido van Rossum3d602e31996-07-21 02:33:56 +00001454 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001455 res = validate_name(CHILD(tree, 0), "raise");
1456 if (res && (nch >= 2))
1457 res = validate_test(CHILD(tree, 1));
1458 if (res && nch > 2) {
1459 res = (validate_comma(CHILD(tree, 2))
1460 && validate_test(CHILD(tree, 3)));
1461 if (res && (nch > 4))
1462 res = (validate_comma(CHILD(tree, 4))
1463 && validate_test(CHILD(tree, 5)));
1464 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001465 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001466 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001467 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001468 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001469 res = (validate_comma(CHILD(tree, 2))
1470 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001471
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001472 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001473}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001474
1475
Guido van Rossum3d602e31996-07-21 02:33:56 +00001476/* import_stmt:
1477 *
1478 * 'import' dotted_name (',' dotted_name)*
1479 * | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*)
1480 */
Guido van Rossum47478871996-08-21 14:32:37 +00001481static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001482validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001483{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001484 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001485 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001486 && (nch >= 2) && is_even(nch)
1487 && validate_ntype(CHILD(tree, 0), NAME)
1488 && validate_ntype(CHILD(tree, 1), dotted_name));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001489
1490 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001491 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001492
Fred Drakeff9ea482000-04-19 13:54:15 +00001493 for (j = 2; res && (j < nch); j += 2)
1494 res = (validate_comma(CHILD(tree, j))
1495 && validate_ntype(CHILD(tree, j + 1), dotted_name));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001496 }
1497 else if (res && validate_name(CHILD(tree, 0), "from")) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001498 res = ((nch >= 4) && is_even(nch)
1499 && validate_name(CHILD(tree, 2), "import"));
1500 if (nch == 4) {
1501 res = ((TYPE(CHILD(tree, 3)) == NAME)
1502 || (TYPE(CHILD(tree, 3)) == STAR));
1503 if (!res)
1504 err_string("Illegal import statement.");
1505 }
1506 else {
1507 /* 'from' NAME 'import' NAME (',' NAME)+ */
1508 int j;
1509 res = validate_ntype(CHILD(tree, 3), NAME);
1510 for (j = 4; res && (j < nch); j += 2)
1511 res = (validate_comma(CHILD(tree, j))
1512 && validate_ntype(CHILD(tree, j + 1), NAME));
1513 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001514 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001515 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001516 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001517
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001518 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001519}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001520
1521
Guido van Rossum47478871996-08-21 14:32:37 +00001522static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001523validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001524{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001525 int j;
1526 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001527 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001528 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001529
Guido van Rossum3d602e31996-07-21 02:33:56 +00001530 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001531 res = (validate_name(CHILD(tree, 0), "global")
1532 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001533 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001534 res = (validate_comma(CHILD(tree, j))
1535 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001536
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001537 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001538}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001539
1540
Guido van Rossum3d602e31996-07-21 02:33:56 +00001541/* exec_stmt:
1542 *
1543 * 'exec' expr ['in' test [',' test]]
1544 */
Guido van Rossum47478871996-08-21 14:32:37 +00001545static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001546validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001547{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001548 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001549 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001550 && ((nch == 2) || (nch == 4) || (nch == 6))
1551 && validate_name(CHILD(tree, 0), "exec")
1552 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001553
Guido van Rossum3d602e31996-07-21 02:33:56 +00001554 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001555 err_string("Illegal exec statement.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001556 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001557 res = (validate_name(CHILD(tree, 2), "in")
1558 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001559 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001560 res = (validate_comma(CHILD(tree, 4))
1561 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001562
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001563 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001564}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001565
1566
Guido van Rossum925e5471997-04-02 05:32:13 +00001567/* assert_stmt:
1568 *
1569 * 'assert' test [',' test]
1570 */
1571static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001572validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001573{
1574 int nch = NCH(tree);
1575 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001576 && ((nch == 2) || (nch == 4))
1577 && (validate_name(CHILD(tree, 0), "__assert__") ||
1578 validate_name(CHILD(tree, 0), "assert"))
1579 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001580
1581 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001582 err_string("Illegal assert statement.");
Guido van Rossum925e5471997-04-02 05:32:13 +00001583 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001584 res = (validate_comma(CHILD(tree, 2))
1585 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001586
1587 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001588}
Guido van Rossum925e5471997-04-02 05:32:13 +00001589
1590
Guido van Rossum47478871996-08-21 14:32:37 +00001591static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001592validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001593{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001594 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001595 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001596 && ((nch == 4) || (nch == 7))
1597 && validate_name(CHILD(tree, 0), "while")
1598 && validate_test(CHILD(tree, 1))
1599 && validate_colon(CHILD(tree, 2))
1600 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001601
Guido van Rossum3d602e31996-07-21 02:33:56 +00001602 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001603 res = (validate_name(CHILD(tree, 4), "else")
1604 && validate_colon(CHILD(tree, 5))
1605 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001606
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001607 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001608}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001609
1610
Guido van Rossum47478871996-08-21 14:32:37 +00001611static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001612validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001613{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001614 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001615 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001616 && ((nch == 6) || (nch == 9))
1617 && validate_name(CHILD(tree, 0), "for")
1618 && validate_exprlist(CHILD(tree, 1))
1619 && validate_name(CHILD(tree, 2), "in")
1620 && validate_testlist(CHILD(tree, 3))
1621 && validate_colon(CHILD(tree, 4))
1622 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001623
Guido van Rossum3d602e31996-07-21 02:33:56 +00001624 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001625 res = (validate_name(CHILD(tree, 6), "else")
1626 && validate_colon(CHILD(tree, 7))
1627 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001628
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001629 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001630}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001631
1632
Guido van Rossum3d602e31996-07-21 02:33:56 +00001633/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001634 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001635 * | 'try' ':' suite 'finally' ':' suite
1636 *
1637 */
Guido van Rossum47478871996-08-21 14:32:37 +00001638static int
Guido van Rossum3d602e31996-07-21 02:33:56 +00001639validate_try(tree)
1640 node *tree;
1641{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001642 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001643 int pos = 3;
1644 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001645 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001646
1647 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001648 res = (validate_name(CHILD(tree, 0), "try")
1649 && validate_colon(CHILD(tree, 1))
1650 && validate_suite(CHILD(tree, 2))
1651 && validate_colon(CHILD(tree, nch - 2))
1652 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001653 else {
Fred Drake22269b52000-07-03 18:07:43 +00001654 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001655 char buffer[60];
1656 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1657 name = STR(CHILD(tree, nch - 3));
1658 (void) sprintf(buffer,
1659 "Illegal number of children for try/%s node.", name);
1660 err_string(buffer);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001661 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001662 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001663 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001664 res = (validate_except_clause(CHILD(tree, pos))
1665 && validate_colon(CHILD(tree, pos + 1))
1666 && validate_suite(CHILD(tree, pos + 2)));
1667 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001668 }
1669 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001670 res = validate_ntype(CHILD(tree, pos), NAME);
1671 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1672 res = (validate_numnodes(tree, 6, "try/finally")
1673 && validate_colon(CHILD(tree, 4))
1674 && validate_suite(CHILD(tree, 5)));
1675 else if (res) {
1676 if (nch == (pos + 3)) {
1677 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1678 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1679 if (!res)
1680 err_string("Illegal trailing triple in try statement.");
1681 }
1682 else if (nch == (pos + 6)) {
1683 res = (validate_name(CHILD(tree, pos), "except")
1684 && validate_colon(CHILD(tree, pos + 1))
1685 && validate_suite(CHILD(tree, pos + 2))
1686 && validate_name(CHILD(tree, pos + 3), "else"));
1687 }
1688 else
1689 res = validate_numnodes(tree, pos + 3, "try/except");
1690 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001691 }
1692 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001693}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001694
1695
Guido van Rossum47478871996-08-21 14:32:37 +00001696static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001697validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001698{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001699 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001700 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001701 && ((nch == 1) || (nch == 2) || (nch == 4))
1702 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001703
Guido van Rossum3d602e31996-07-21 02:33:56 +00001704 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001705 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001706 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001707 res = (validate_comma(CHILD(tree, 2))
1708 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001709
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001710 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001711}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001712
1713
Guido van Rossum47478871996-08-21 14:32:37 +00001714static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001715validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001716{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001717 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001718 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001719
Guido van Rossum3d602e31996-07-21 02:33:56 +00001720 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001721 res = ((nch == 1)
1722 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001723 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001724 int pos;
1725 res = validate_and_test(CHILD(tree, 0));
1726 for (pos = 1; res && (pos < nch); pos += 2)
1727 res = (validate_name(CHILD(tree, pos), "or")
1728 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001729 }
1730 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001731}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001732
1733
Guido van Rossum47478871996-08-21 14:32:37 +00001734static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001735validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001736{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001737 int pos;
1738 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001739 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001740 && is_odd(nch)
1741 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001742
Guido van Rossum3d602e31996-07-21 02:33:56 +00001743 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001744 res = (validate_name(CHILD(tree, pos), "and")
1745 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001746
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001747 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001748}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001749
1750
Guido van Rossum47478871996-08-21 14:32:37 +00001751static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001752validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001753{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001754 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001755 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001756
Guido van Rossum3d602e31996-07-21 02:33:56 +00001757 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001758 if (nch == 2)
1759 res = (validate_name(CHILD(tree, 0), "not")
1760 && validate_not_test(CHILD(tree, 1)));
1761 else if (nch == 1)
1762 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001763 }
1764 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001765}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001766
1767
Guido van Rossum47478871996-08-21 14:32:37 +00001768static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001769validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001770{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001771 int pos;
1772 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001773 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001774 && is_odd(nch)
1775 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001776
Guido van Rossum3d602e31996-07-21 02:33:56 +00001777 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001778 res = (validate_comp_op(CHILD(tree, pos))
1779 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001780
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001781 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001782}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001783
1784
Guido van Rossum47478871996-08-21 14:32:37 +00001785static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001786validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001787{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001788 int res = 0;
1789 int nch = NCH(tree);
1790
Guido van Rossum3d602e31996-07-21 02:33:56 +00001791 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001792 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001793 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001794 /*
1795 * Only child will be a terminal with a well-defined symbolic name
1796 * or a NAME with a string of either 'is' or 'in'
1797 */
1798 tree = CHILD(tree, 0);
1799 switch (TYPE(tree)) {
1800 case LESS:
1801 case GREATER:
1802 case EQEQUAL:
1803 case EQUAL:
1804 case LESSEQUAL:
1805 case GREATEREQUAL:
1806 case NOTEQUAL:
1807 res = 1;
1808 break;
1809 case NAME:
1810 res = ((strcmp(STR(tree), "in") == 0)
1811 || (strcmp(STR(tree), "is") == 0));
1812 if (!res) {
1813 char buff[128];
1814 (void) sprintf(buff, "Illegal operator: '%s'.", STR(tree));
1815 err_string(buff);
1816 }
1817 break;
1818 default:
1819 err_string("Illegal comparison operator type.");
1820 break;
1821 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001822 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00001823 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001824 res = (validate_ntype(CHILD(tree, 0), NAME)
1825 && validate_ntype(CHILD(tree, 1), NAME)
1826 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
1827 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
1828 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
1829 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
1830 if (!res && !PyErr_Occurred())
1831 err_string("Unknown comparison operator.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001832 }
1833 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001834}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001835
1836
Guido van Rossum47478871996-08-21 14:32:37 +00001837static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001838validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001839{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001840 int j;
1841 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001842 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001843 && is_odd(nch)
1844 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001845
Guido van Rossum3d602e31996-07-21 02:33:56 +00001846 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001847 res = (validate_xor_expr(CHILD(tree, j))
1848 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001849
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001850 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001851}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001852
1853
Guido van Rossum47478871996-08-21 14:32:37 +00001854static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001855validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001856{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001857 int j;
1858 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001859 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001860 && is_odd(nch)
1861 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001862
Guido van Rossum3d602e31996-07-21 02:33:56 +00001863 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001864 res = (validate_circumflex(CHILD(tree, j - 1))
1865 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001866
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001867 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001868}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001869
1870
Guido van Rossum47478871996-08-21 14:32:37 +00001871static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001872validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001873{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001874 int pos;
1875 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001876 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001877 && is_odd(nch)
1878 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001879
Guido van Rossum3d602e31996-07-21 02:33:56 +00001880 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001881 res = (validate_ampersand(CHILD(tree, pos))
1882 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001883
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001884 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001885}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001886
1887
1888static int
Guido van Rossum3d602e31996-07-21 02:33:56 +00001889validate_chain_two_ops(tree, termvalid, op1, op2)
1890 node *tree;
1891 int (*termvalid)();
1892 int op1;
1893 int op2;
1894 {
1895 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001896 int nch = NCH(tree);
1897 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00001898 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001899
Guido van Rossum3d602e31996-07-21 02:33:56 +00001900 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001901 if (TYPE(CHILD(tree, pos)) != op1)
1902 res = validate_ntype(CHILD(tree, pos), op2);
1903 if (res)
1904 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001905 }
1906 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001907}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001908
1909
Guido van Rossum47478871996-08-21 14:32:37 +00001910static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001911validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001912{
1913 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001914 && validate_chain_two_ops(tree, validate_arith_expr,
1915 LEFTSHIFT, RIGHTSHIFT));
1916}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001917
1918
Guido van Rossum47478871996-08-21 14:32:37 +00001919static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001920validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001921{
1922 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001923 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
1924}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001925
1926
Guido van Rossum47478871996-08-21 14:32:37 +00001927static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001928validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001929{
1930 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001931 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001932 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00001933 && is_odd(nch)
1934 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001935
Guido van Rossum3d602e31996-07-21 02:33:56 +00001936 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001937 res = (((TYPE(CHILD(tree, pos)) == STAR)
1938 || (TYPE(CHILD(tree, pos)) == SLASH)
1939 || (TYPE(CHILD(tree, pos)) == PERCENT))
1940 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001941
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001942 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001943}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001944
1945
Guido van Rossum3d602e31996-07-21 02:33:56 +00001946/* factor:
1947 *
1948 * factor: ('+'|'-'|'~') factor | power
1949 */
Guido van Rossum47478871996-08-21 14:32:37 +00001950static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001951validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001952{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001953 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001954 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00001955 && (((nch == 2)
1956 && ((TYPE(CHILD(tree, 0)) == PLUS)
1957 || (TYPE(CHILD(tree, 0)) == MINUS)
1958 || (TYPE(CHILD(tree, 0)) == TILDE))
1959 && validate_factor(CHILD(tree, 1)))
1960 || ((nch == 1)
1961 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001962 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001963}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001964
1965
Guido van Rossum3d602e31996-07-21 02:33:56 +00001966/* power:
1967 *
1968 * power: atom trailer* ('**' factor)*
1969 */
Guido van Rossum47478871996-08-21 14:32:37 +00001970static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001971validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001972{
1973 int pos = 1;
1974 int nch = NCH(tree);
1975 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00001976 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001977
1978 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00001979 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001980 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001981 if (!is_even(nch - pos)) {
1982 err_string("Illegal number of nodes for 'power'.");
1983 return (0);
1984 }
1985 for ( ; res && (pos < (nch - 1)); pos += 2)
1986 res = (validate_doublestar(CHILD(tree, pos))
1987 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001988 }
1989 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001990}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001991
1992
Guido van Rossum47478871996-08-21 14:32:37 +00001993static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001994validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001995{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001996 int pos;
1997 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001998 int res = validate_ntype(tree, atom) && (nch >= 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001999
2000 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002001 switch (TYPE(CHILD(tree, 0))) {
2002 case LPAR:
2003 res = ((nch <= 3)
2004 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002005
Fred Drakeff9ea482000-04-19 13:54:15 +00002006 if (res && (nch == 3))
2007 res = validate_testlist(CHILD(tree, 1));
2008 break;
2009 case LSQB:
2010 res = ((nch <= 3)
2011 && validate_ntype(CHILD(tree, nch - 1), RSQB));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002012
Fred Drakeff9ea482000-04-19 13:54:15 +00002013 if (res && (nch == 3))
2014 res = validate_testlist(CHILD(tree, 1));
2015 break;
2016 case LBRACE:
2017 res = ((nch <= 3)
2018 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002019
Fred Drakeff9ea482000-04-19 13:54:15 +00002020 if (res && (nch == 3))
2021 res = validate_dictmaker(CHILD(tree, 1));
2022 break;
2023 case BACKQUOTE:
2024 res = ((nch == 3)
2025 && validate_testlist(CHILD(tree, 1))
2026 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2027 break;
2028 case NAME:
2029 case NUMBER:
2030 res = (nch == 1);
2031 break;
2032 case STRING:
2033 for (pos = 1; res && (pos < nch); ++pos)
2034 res = validate_ntype(CHILD(tree, pos), STRING);
2035 break;
2036 default:
2037 res = 0;
2038 break;
2039 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002040 }
2041 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002042}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002043
2044
Guido van Rossum3d602e31996-07-21 02:33:56 +00002045/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002046 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002047 *
2048 */
Guido van Rossum47478871996-08-21 14:32:37 +00002049static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002050validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002051{
2052 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002053 && validate_numnodes(tree, 5, "funcdef")
2054 && validate_name(CHILD(tree, 0), "def")
2055 && validate_ntype(CHILD(tree, 1), NAME)
2056 && validate_colon(CHILD(tree, 3))
2057 && validate_parameters(CHILD(tree, 2))
2058 && validate_suite(CHILD(tree, 4)));
2059}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002060
2061
Guido van Rossum47478871996-08-21 14:32:37 +00002062static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002063validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002064{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002065 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002066 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002067 && ((nch == 3) || (nch == 4))
2068 && validate_name(CHILD(tree, 0), "lambda")
2069 && validate_colon(CHILD(tree, nch - 2))
2070 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002071
Guido van Rossum3d602e31996-07-21 02:33:56 +00002072 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002073 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002074 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002075 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002076
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002077 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002078}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002079
2080
Guido van Rossum3d602e31996-07-21 02:33:56 +00002081/* arglist:
2082 *
Fred Drakee7ab64e2000-04-25 04:14:46 +00002083 * (argument ',')* (argument* [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002084 */
Guido van Rossum47478871996-08-21 14:32:37 +00002085static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002086validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002087{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002088 int nch = NCH(tree);
Fred Drake7797d362000-07-04 18:48:46 +00002089 int i, ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002090 node *last;
2091
2092 if (nch <= 0)
2093 /* raise the right error from having an invalid number of children */
2094 return validate_numnodes(tree, nch + 1, "arglist");
2095
2096 last = CHILD(tree, nch - 1);
2097 if (TYPE(last) == test) {
2098 /* Extended call syntax introduced in Python 1.6 has been used;
2099 * validate and strip that off and continue;
2100 * adjust nch to perform the cut, and ensure resulting nch is even
2101 * (validation of the first part doesn't require that).
2102 */
2103 if (nch < 2) {
2104 validate_numnodes(tree, nch + 1, "arglist");
2105 return 0;
2106 }
2107 ok = validate_test(last);
2108 if (ok) {
2109 node *prev = CHILD(tree, nch - 2);
2110 /* next must be '*' or '**' */
2111 if (validate_doublestar(prev)) {
2112 nch -= 2;
2113 if (nch >= 3) {
2114 /* may include: '*' test ',' */
2115 last = CHILD(tree, nch - 1);
2116 prev = CHILD(tree, nch - 2);
2117 if (TYPE(prev) == test) {
2118 ok = validate_comma(last)
2119 && validate_test(prev)
2120 && validate_star(CHILD(tree, nch - 3));
2121 if (ok)
2122 nch -= 3;
2123 }
2124 /* otherwise, nothing special */
2125 }
2126 }
2127 else {
2128 /* must be only: '*' test */
2129 PyErr_Clear();
2130 ok = validate_star(prev);
2131 nch -= 2;
2132 }
2133 if (ok && is_odd(nch)) {
2134 /* Illegal number of nodes before extended call syntax;
2135 * validation of the "normal" arguments does not require
2136 * a trailing comma, but requiring an even number of
2137 * children will effect the same requirement.
2138 */
2139 return validate_numnodes(tree, nch + 1, "arglist");
2140 }
2141 }
2142 }
2143 /* what remains must be: (argument ",")* [argument [","]] */
2144 i = 0;
2145 while (ok && nch - i >= 2) {
2146 ok = validate_argument(CHILD(tree, i))
2147 && validate_comma(CHILD(tree, i + 1));
2148 i += 2;
2149 }
2150 if (ok && i < nch) {
2151 ok = validate_comma(CHILD(tree, i));
2152 ++i;
2153 }
2154 if (i != nch) {
2155 /* internal error! */
2156 ok = 0;
2157 err_string("arglist: internal error; nch != i");
2158 }
2159 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002160}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002161
2162
2163
2164/* argument:
2165 *
2166 * [test '='] test
2167 */
Guido van Rossum47478871996-08-21 14:32:37 +00002168static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002169validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002170{
2171 int nch = NCH(tree);
2172 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002173 && ((nch == 1) || (nch == 3))
2174 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002175
2176 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002177 res = (validate_equal(CHILD(tree, 1))
2178 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002179
2180 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002181}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002182
2183
2184
2185/* trailer:
2186 *
Guido van Rossum47478871996-08-21 14:32:37 +00002187 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002188 */
Guido van Rossum47478871996-08-21 14:32:37 +00002189static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002190validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002191{
2192 int nch = NCH(tree);
2193 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002194
2195 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002196 switch (TYPE(CHILD(tree, 0))) {
2197 case LPAR:
2198 res = validate_rparen(CHILD(tree, nch - 1));
2199 if (res && (nch == 3))
2200 res = validate_arglist(CHILD(tree, 1));
2201 break;
2202 case LSQB:
2203 res = (validate_numnodes(tree, 3, "trailer")
2204 && validate_subscriptlist(CHILD(tree, 1))
2205 && validate_ntype(CHILD(tree, 2), RSQB));
2206 break;
2207 case DOT:
2208 res = (validate_numnodes(tree, 2, "trailer")
2209 && validate_ntype(CHILD(tree, 1), NAME));
2210 break;
2211 default:
2212 res = 0;
2213 break;
2214 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002215 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002216 else {
2217 (void) validate_numnodes(tree, 2, "trailer");
2218 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002219 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002220}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002221
2222
Guido van Rossum47478871996-08-21 14:32:37 +00002223/* subscriptlist:
2224 *
2225 * subscript (',' subscript)* [',']
2226 */
2227static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002228validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002229{
2230 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002231 validate_subscript, "subscriptlist"));
2232}
Guido van Rossum47478871996-08-21 14:32:37 +00002233
2234
Guido van Rossum3d602e31996-07-21 02:33:56 +00002235/* subscript:
2236 *
Guido van Rossum47478871996-08-21 14:32:37 +00002237 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002238 */
Guido van Rossum47478871996-08-21 14:32:37 +00002239static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002240validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002241{
Guido van Rossum47478871996-08-21 14:32:37 +00002242 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002243 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002244 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002245
Guido van Rossum47478871996-08-21 14:32:37 +00002246 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002247 if (!PyErr_Occurred())
2248 err_string("invalid number of arguments for subscript node");
2249 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002250 }
Guido van Rossum47478871996-08-21 14:32:37 +00002251 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002252 /* take care of ('.' '.' '.') possibility */
2253 return (validate_numnodes(tree, 3, "subscript")
2254 && validate_dot(CHILD(tree, 0))
2255 && validate_dot(CHILD(tree, 1))
2256 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002257 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002258 if (TYPE(CHILD(tree, 0)) == test)
2259 res = validate_test(CHILD(tree, 0));
2260 else
2261 res = validate_colon(CHILD(tree, 0));
2262 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002263 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002264 /* Must be [test] ':' [test] [sliceop],
2265 * but at least one of the optional components will
2266 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002267 */
2268 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002269 res = validate_test(CHILD(tree, 0));
2270 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002271 }
2272 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002273 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002274 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002275 int rem = nch - ++offset;
2276 if (rem) {
2277 if (TYPE(CHILD(tree, offset)) == test) {
2278 res = validate_test(CHILD(tree, offset));
2279 ++offset;
2280 --rem;
2281 }
2282 if (res && rem)
2283 res = validate_sliceop(CHILD(tree, offset));
2284 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002285 }
2286 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002287}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002288
2289
Guido van Rossum47478871996-08-21 14:32:37 +00002290static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002291validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002292{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002293 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002294 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002295 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002296 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002297 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002298 }
Guido van Rossum47478871996-08-21 14:32:37 +00002299 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002300 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002301 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002302 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002303
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002304 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002305}
Guido van Rossum47478871996-08-21 14:32:37 +00002306
2307
2308static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002309validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002310{
2311 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002312 validate_expr, "exprlist"));
2313}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002314
2315
Guido van Rossum47478871996-08-21 14:32:37 +00002316static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002317validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002318{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002319 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002320 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002321 && (nch >= 3)
2322 && validate_test(CHILD(tree, 0))
2323 && validate_colon(CHILD(tree, 1))
2324 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002325
Guido van Rossum3d602e31996-07-21 02:33:56 +00002326 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002327 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002328 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002329 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002330
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002331 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002332 int pos = 3;
2333 /* ( ',' test ':' test )* */
2334 while (res && (pos < nch)) {
2335 res = (validate_comma(CHILD(tree, pos))
2336 && validate_test(CHILD(tree, pos + 1))
2337 && validate_colon(CHILD(tree, pos + 2))
2338 && validate_test(CHILD(tree, pos + 3)));
2339 pos += 4;
2340 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002341 }
2342 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002343}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002344
2345
Guido van Rossum47478871996-08-21 14:32:37 +00002346static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002347validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002348{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002349 int pos;
2350 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002351 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002352 && (nch >= 2)
2353 && validate_testlist(CHILD(tree, 0))
2354 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002355
Guido van Rossum3d602e31996-07-21 02:33:56 +00002356 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002357 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002358
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002359 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002360}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002361
2362
Guido van Rossum47478871996-08-21 14:32:37 +00002363static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002364validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002365{
Fred Drakeff9ea482000-04-19 13:54:15 +00002366 int nch = 0; /* num. children on current node */
2367 int res = 1; /* result value */
2368 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002369
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002370 while (res & (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002371 nch = NCH(tree);
2372 next = 0;
2373 switch (TYPE(tree)) {
2374 /*
2375 * Definition nodes.
2376 */
2377 case funcdef:
2378 res = validate_funcdef(tree);
2379 break;
2380 case classdef:
2381 res = validate_class(tree);
2382 break;
2383 /*
2384 * "Trivial" parse tree nodes.
2385 * (Why did I call these trivial?)
2386 */
2387 case stmt:
2388 res = validate_stmt(tree);
2389 break;
2390 case small_stmt:
2391 /*
2392 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
2393 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2394 */
2395 res = validate_small_stmt(tree);
2396 break;
2397 case flow_stmt:
2398 res = (validate_numnodes(tree, 1, "flow_stmt")
2399 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2400 || (TYPE(CHILD(tree, 0)) == continue_stmt)
2401 || (TYPE(CHILD(tree, 0)) == return_stmt)
2402 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2403 if (res)
2404 next = CHILD(tree, 0);
2405 else if (nch == 1)
2406 err_string("Illegal flow_stmt type.");
2407 break;
2408 /*
2409 * Compound statements.
2410 */
2411 case simple_stmt:
2412 res = validate_simple_stmt(tree);
2413 break;
2414 case compound_stmt:
2415 res = validate_compound_stmt(tree);
2416 break;
2417 /*
2418 * Fundemental statements.
2419 */
2420 case expr_stmt:
2421 res = validate_expr_stmt(tree);
2422 break;
2423 case print_stmt:
2424 res = validate_print_stmt(tree);
2425 break;
2426 case del_stmt:
2427 res = validate_del_stmt(tree);
2428 break;
2429 case pass_stmt:
2430 res = (validate_numnodes(tree, 1, "pass")
2431 && validate_name(CHILD(tree, 0), "pass"));
2432 break;
2433 case break_stmt:
2434 res = (validate_numnodes(tree, 1, "break")
2435 && validate_name(CHILD(tree, 0), "break"));
2436 break;
2437 case continue_stmt:
2438 res = (validate_numnodes(tree, 1, "continue")
2439 && validate_name(CHILD(tree, 0), "continue"));
2440 break;
2441 case return_stmt:
2442 res = validate_return_stmt(tree);
2443 break;
2444 case raise_stmt:
2445 res = validate_raise_stmt(tree);
2446 break;
2447 case import_stmt:
2448 res = validate_import_stmt(tree);
2449 break;
2450 case global_stmt:
2451 res = validate_global_stmt(tree);
2452 break;
2453 case exec_stmt:
2454 res = validate_exec_stmt(tree);
2455 break;
2456 case assert_stmt:
2457 res = validate_assert_stmt(tree);
2458 break;
2459 case if_stmt:
2460 res = validate_if(tree);
2461 break;
2462 case while_stmt:
2463 res = validate_while(tree);
2464 break;
2465 case for_stmt:
2466 res = validate_for(tree);
2467 break;
2468 case try_stmt:
2469 res = validate_try(tree);
2470 break;
2471 case suite:
2472 res = validate_suite(tree);
2473 break;
2474 /*
2475 * Expression nodes.
2476 */
2477 case testlist:
2478 res = validate_testlist(tree);
2479 break;
2480 case test:
2481 res = validate_test(tree);
2482 break;
2483 case and_test:
2484 res = validate_and_test(tree);
2485 break;
2486 case not_test:
2487 res = validate_not_test(tree);
2488 break;
2489 case comparison:
2490 res = validate_comparison(tree);
2491 break;
2492 case exprlist:
2493 res = validate_exprlist(tree);
2494 break;
2495 case comp_op:
2496 res = validate_comp_op(tree);
2497 break;
2498 case expr:
2499 res = validate_expr(tree);
2500 break;
2501 case xor_expr:
2502 res = validate_xor_expr(tree);
2503 break;
2504 case and_expr:
2505 res = validate_and_expr(tree);
2506 break;
2507 case shift_expr:
2508 res = validate_shift_expr(tree);
2509 break;
2510 case arith_expr:
2511 res = validate_arith_expr(tree);
2512 break;
2513 case term:
2514 res = validate_term(tree);
2515 break;
2516 case factor:
2517 res = validate_factor(tree);
2518 break;
2519 case power:
2520 res = validate_power(tree);
2521 break;
2522 case atom:
2523 res = validate_atom(tree);
2524 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002525
Fred Drakeff9ea482000-04-19 13:54:15 +00002526 default:
2527 /* Hopefully never reached! */
2528 err_string("Unrecogniged node type.");
2529 res = 0;
2530 break;
2531 }
2532 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002533 }
2534 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002535}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002536
2537
Guido van Rossum47478871996-08-21 14:32:37 +00002538static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002539validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002540{
2541 int res = validate_eval_input(tree);
2542
2543 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002544 err_string("Could not validate expression tuple.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002545
2546 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002547}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002548
2549
Guido van Rossum3d602e31996-07-21 02:33:56 +00002550/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002551 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002552 */
Guido van Rossum47478871996-08-21 14:32:37 +00002553static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002554validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002555{
2556 int j = 0;
2557 int nch = NCH(tree) - 1;
2558 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002559 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002560
Guido van Rossum3d602e31996-07-21 02:33:56 +00002561 for ( ; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002562 if (TYPE(CHILD(tree, j)) == stmt)
2563 res = validate_stmt(CHILD(tree, j));
2564 else
2565 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002566 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002567 /* This stays in to prevent any internal failues from getting to the
2568 * user. Hopefully, this won't be needed. If a user reports getting
2569 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002570 */
2571 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002572 err_string("VALIDATION FAILURE: report this to the maintainer!.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002573
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002574 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002575}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002576
2577
Fred Drake43f8f9b1998-04-13 16:25:46 +00002578static PyObject*
2579pickle_constructor = NULL;
2580
2581
2582static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002583parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002584{
Fred Drake268397f1998-04-29 14:16:32 +00002585 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002586 PyObject *result = NULL;
2587 PyObject *ast = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002588 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002589
2590 if (PyArg_ParseTuple(args, "O!:_pickler", &PyAST_Type, &ast)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002591 PyObject *newargs;
2592 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002593
Fred Drake2a6875e1999-09-20 22:32:18 +00002594 if ((empty_dict = PyDict_New()) == NULL)
2595 goto finally;
2596 if ((newargs = Py_BuildValue("Oi", ast, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002597 goto finally;
2598 tuple = parser_ast2tuple((PyAST_Object*)NULL, newargs, empty_dict);
2599 if (tuple != NULL) {
2600 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2601 Py_DECREF(tuple);
2602 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002603 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002604 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002605 }
2606 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002607 Py_XDECREF(empty_dict);
2608
Fred Drake43f8f9b1998-04-13 16:25:46 +00002609 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002610}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002611
2612
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002613/* Functions exported by this module. Most of this should probably
2614 * be converted into an AST object with methods, but that is better
2615 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002616 * We'd really have to write a wrapper around it all anyway to allow
2617 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002618 */
2619static PyMethodDef parser_functions[] = {
Fred Drakeff9ea482000-04-19 13:54:15 +00002620 {"ast2tuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
2621 "Creates a tuple-tree representation of an AST."},
2622 {"ast2list", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
2623 "Creates a list-tree representation of an AST."},
2624 {"compileast", (PyCFunction)parser_compileast, PUBLIC_METHOD_TYPE,
2625 "Compiles an AST object into a code object."},
2626 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
2627 "Creates an AST object from an expression."},
2628 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
2629 "Determines if an AST object was created from an expression."},
2630 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
2631 "Determines if an AST object was created from a suite."},
2632 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
2633 "Creates an AST object from a suite."},
2634 {"sequence2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
2635 "Creates an AST object from a tree representation."},
2636 {"tuple2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
2637 "Creates an AST object from a tree representation."},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002638
Fred Drake43f8f9b1998-04-13 16:25:46 +00002639 /* private stuff: support pickle module */
Fred Drakeff9ea482000-04-19 13:54:15 +00002640 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Fred Drake43f8f9b1998-04-13 16:25:46 +00002641 "Returns the pickle magic to allow ast objects to be pickled."},
2642
Fred Drake268397f1998-04-29 14:16:32 +00002643 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002644 };
2645
2646
Guido van Rossum3886bb61998-12-04 18:50:17 +00002647DL_EXPORT(void)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002648initparser()
2649 {
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002650 PyObject* module;
2651 PyObject* dict;
Fred Drakeff9ea482000-04-19 13:54:15 +00002652
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002653 PyAST_Type.ob_type = &PyType_Type;
2654 module = Py_InitModule("parser", parser_functions);
2655 dict = PyModule_GetDict(module);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002656
Fred Drake7a15ba51999-09-09 14:21:52 +00002657 if (parser_error == 0)
2658 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
2659 else
2660 puts("parser module initialized more than once!");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002661
2662 if ((parser_error == 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002663 || (PyDict_SetItemString(dict, "ParserError", parser_error) != 0)) {
2664 /*
2665 * This is serious.
2666 */
2667 Py_FatalError("can't define parser.ParserError");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002668 }
2669 /*
2670 * Nice to have, but don't cry if we fail.
2671 */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002672 Py_INCREF(&PyAST_Type);
2673 PyDict_SetItemString(dict, "ASTType", (PyObject*)&PyAST_Type);
2674
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002675 PyDict_SetItemString(dict, "__copyright__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002676 PyString_FromString(parser_copyright_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002677 PyDict_SetItemString(dict, "__doc__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002678 PyString_FromString(parser_doc_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002679 PyDict_SetItemString(dict, "__version__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002680 PyString_FromString(parser_version_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002681
Fred Drake43f8f9b1998-04-13 16:25:46 +00002682 /* register to support pickling */
2683 module = PyImport_ImportModule("copy_reg");
2684 if (module != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002685 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002686
Fred Drakeff9ea482000-04-19 13:54:15 +00002687 func = PyObject_GetAttrString(module, "pickle");
2688 pickle_constructor = PyDict_GetItemString(dict, "sequence2ast");
2689 pickler = PyDict_GetItemString(dict, "_pickler");
2690 Py_XINCREF(pickle_constructor);
2691 if ((func != NULL) && (pickle_constructor != NULL)
2692 && (pickler != NULL)) {
2693 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002694
Fred Drakeff9ea482000-04-19 13:54:15 +00002695 res = PyObject_CallFunction(
2696 func, "OOO", &PyAST_Type, pickler, pickle_constructor);
2697 Py_XDECREF(res);
2698 }
2699 Py_XDECREF(func);
2700 Py_DECREF(module);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002701 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002702}