blob: daa2f80126ac4a394d86f04637e846081bf182f0 [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
Fred Drakeff9ea482000-04-19 13:54:15 +0000861#define VALIDATER(n) static int validate_##n(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000862
863
864/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000865 * Validation routines used within the validation section:
866 */
Fred Drakeff9ea482000-04-19 13:54:15 +0000867staticforward int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000868
Fred Drakeff9ea482000-04-19 13:54:15 +0000869#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
870#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
871#define validate_colon(ch) validate_terminal(ch, COLON, ":")
872#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
873#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
874#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
875#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
876#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
877#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
878#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
879#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
880#define validate_star(ch) validate_terminal(ch, STAR, "*")
881#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
882#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
883#define validate_dot(ch) validate_terminal(ch, DOT, ".")
884#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000885
Fred Drakeff9ea482000-04-19 13:54:15 +0000886VALIDATER(node); VALIDATER(small_stmt);
887VALIDATER(class); VALIDATER(node);
888VALIDATER(parameters); VALIDATER(suite);
889VALIDATER(testlist); VALIDATER(varargslist);
890VALIDATER(fpdef); VALIDATER(fplist);
891VALIDATER(stmt); VALIDATER(simple_stmt);
892VALIDATER(expr_stmt); VALIDATER(power);
893VALIDATER(print_stmt); VALIDATER(del_stmt);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000894VALIDATER(return_stmt);
Fred Drakeff9ea482000-04-19 13:54:15 +0000895VALIDATER(raise_stmt); VALIDATER(import_stmt);
Guido van Rossum2a288461996-08-21 21:55:43 +0000896VALIDATER(global_stmt);
Guido van Rossum925e5471997-04-02 05:32:13 +0000897VALIDATER(assert_stmt);
Fred Drakeff9ea482000-04-19 13:54:15 +0000898VALIDATER(exec_stmt); VALIDATER(compound_stmt);
899VALIDATER(while); VALIDATER(for);
900VALIDATER(try); VALIDATER(except_clause);
901VALIDATER(test); VALIDATER(and_test);
902VALIDATER(not_test); VALIDATER(comparison);
903VALIDATER(comp_op); VALIDATER(expr);
904VALIDATER(xor_expr); VALIDATER(and_expr);
905VALIDATER(shift_expr); VALIDATER(arith_expr);
906VALIDATER(term); VALIDATER(factor);
907VALIDATER(atom); VALIDATER(lambdef);
908VALIDATER(trailer); VALIDATER(subscript);
909VALIDATER(subscriptlist); VALIDATER(sliceop);
910VALIDATER(exprlist); VALIDATER(dictmaker);
911VALIDATER(arglist); VALIDATER(argument);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000912
913
Fred Drakeff9ea482000-04-19 13:54:15 +0000914#define is_even(n) (((n) & 1) == 0)
915#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000916
917
918static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000919validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000920{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000921 int res = (TYPE(n) == t);
922
923 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000924 char buffer[128];
925 (void) sprintf(buffer, "Expected node type %d, got %d.", t, TYPE(n));
926 err_string(buffer);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000927 }
928 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000929}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000930
931
Fred Drakee7ab64e2000-04-25 04:14:46 +0000932/* Verifies that the number of child nodes is exactly 'num', raising
933 * an exception if it isn't. The exception message does not indicate
934 * the exact number of nodes, allowing this to be used to raise the
935 * "right" exception when the wrong number of nodes is present in a
936 * specific variant of a statement's syntax. This is commonly used
937 * in that fashion.
938 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000939static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000940validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000941{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000942 if (NCH(n) != num) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000943 char buff[60];
944 (void) sprintf(buff, "Illegal number of children for %s node.", name);
945 err_string(buff);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000946 }
947 return (NCH(n) == num);
Fred Drakeff9ea482000-04-19 13:54:15 +0000948}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000949
950
951static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000952validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000953{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000954 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000955 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000956
957 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000958 char buffer[60];
959 (void) sprintf(buffer, "Illegal terminal: expected \"%s\"", string);
960 err_string(buffer);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000961 }
962 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000963}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000964
965
Guido van Rossum47478871996-08-21 14:32:37 +0000966/* X (',' X) [',']
967 */
968static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000969validate_repeating_list(node *tree, int ntype, int (*vfunc)(),
970 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000971{
972 int nch = NCH(tree);
973 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000974 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000975
976 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000977 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000978 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000979 if (is_even(nch))
980 res = validate_comma(CHILD(tree, --nch));
981 if (res && nch > 1) {
982 int pos = 1;
983 for ( ; res && pos < nch; pos += 2)
984 res = (validate_comma(CHILD(tree, pos))
985 && vfunc(CHILD(tree, pos + 1)));
986 }
Guido van Rossum47478871996-08-21 14:32:37 +0000987 }
988 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000989}
Guido van Rossum47478871996-08-21 14:32:37 +0000990
991
Guido van Rossum3d602e31996-07-21 02:33:56 +0000992/* VALIDATE(class)
993 *
994 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000995 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000996 */
Guido van Rossum47478871996-08-21 14:32:37 +0000997static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000998validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000999{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001000 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001001 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001002
Guido van Rossum3d602e31996-07-21 02:33:56 +00001003 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001004 res = (validate_name(CHILD(tree, 0), "class")
1005 && validate_ntype(CHILD(tree, 1), NAME)
1006 && validate_colon(CHILD(tree, nch - 2))
1007 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001008 }
1009 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001010 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001011 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001012 res = (validate_lparen(CHILD(tree, 2))
1013 && validate_testlist(CHILD(tree, 3))
1014 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001015 }
1016 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001017}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001018
1019
Guido van Rossum3d602e31996-07-21 02:33:56 +00001020/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001021 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001022 */
Guido van Rossum47478871996-08-21 14:32:37 +00001023static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001024validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001025{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001026 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001027 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001028 && (nch >= 4)
1029 && validate_name(CHILD(tree, 0), "if")
1030 && validate_test(CHILD(tree, 1))
1031 && validate_colon(CHILD(tree, 2))
1032 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001033
1034 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001035 /* ... 'else' ':' suite */
1036 res = (validate_name(CHILD(tree, nch - 3), "else")
1037 && validate_colon(CHILD(tree, nch - 2))
1038 && validate_suite(CHILD(tree, nch - 1)));
1039 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001040 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001041 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001042 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001043 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001044 /* Will catch the case for nch < 4 */
1045 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001046 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001047 /* ... ('elif' test ':' suite)+ ... */
1048 int j = 4;
1049 while ((j < nch) && res) {
1050 res = (validate_name(CHILD(tree, j), "elif")
1051 && validate_colon(CHILD(tree, j + 2))
1052 && validate_test(CHILD(tree, j + 1))
1053 && validate_suite(CHILD(tree, j + 3)));
1054 j += 4;
1055 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001056 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001057 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001058}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001059
1060
Guido van Rossum3d602e31996-07-21 02:33:56 +00001061/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001062 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001063 *
1064 */
Guido van Rossum47478871996-08-21 14:32:37 +00001065static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001066validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001067{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001068 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001069 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001070
Guido van Rossum3d602e31996-07-21 02:33:56 +00001071 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001072 res = (validate_lparen(CHILD(tree, 0))
1073 && validate_rparen(CHILD(tree, nch - 1)));
1074 if (res && (nch == 3))
1075 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001076 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001077 else {
1078 (void) validate_numnodes(tree, 2, "parameters");
1079 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001080 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001081}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001082
1083
Guido van Rossum3d602e31996-07-21 02:33:56 +00001084/* VALIDATE(suite)
1085 *
1086 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001087 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001088 * | NEWLINE INDENT stmt+ DEDENT
1089 */
Guido van Rossum47478871996-08-21 14:32:37 +00001090static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001091validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001092{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001093 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001094 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001095
Guido van Rossum3d602e31996-07-21 02:33:56 +00001096 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001097 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001098 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001099 /* NEWLINE INDENT stmt+ DEDENT */
1100 res = (validate_newline(CHILD(tree, 0))
1101 && validate_indent(CHILD(tree, 1))
1102 && validate_stmt(CHILD(tree, 2))
1103 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001104
Fred Drakeff9ea482000-04-19 13:54:15 +00001105 if (res && (nch > 4)) {
1106 int i = 3;
1107 --nch; /* forget the DEDENT */
1108 for ( ; res && (i < nch); ++i)
1109 res = validate_stmt(CHILD(tree, i));
1110 }
1111 else if (nch < 4)
1112 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001113 }
1114 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001115}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001116
1117
Guido van Rossum47478871996-08-21 14:32:37 +00001118static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001119validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001120{
Guido van Rossum47478871996-08-21 14:32:37 +00001121 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001122 validate_test, "testlist"));
1123}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001124
1125
Guido van Rossum3d602e31996-07-21 02:33:56 +00001126/* VALIDATE(varargslist)
1127 *
1128 * varargslist:
Fred Drakeff9ea482000-04-19 13:54:15 +00001129 * (fpdef ['=' test] ',')* ('*' NAME [',' '*' '*' NAME] | '*' '*' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001130 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1131 *
1132 * (fpdef ['=' test] ',')*
1133 * ('*' NAME [',' ('**'|'*' '*') NAME]
1134 * | ('**'|'*' '*') NAME)
1135 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1136 *
1137 */
Guido van Rossum47478871996-08-21 14:32:37 +00001138static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001139validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001140{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001141 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001142 int res = validate_ntype(tree, varargslist) && (nch != 0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001143
Guido van Rossum3d602e31996-07-21 02:33:56 +00001144 if (res && (nch >= 2) && (TYPE(CHILD(tree, nch - 1)) == NAME)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001145 /* (fpdef ['=' test] ',')*
1146 * ('*' NAME [',' '*' '*' NAME] | '*' '*' NAME)
1147 */
1148 int pos = 0;
1149 int remaining = nch;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001150
Fred Drakeff9ea482000-04-19 13:54:15 +00001151 while (res && (TYPE(CHILD(tree, pos)) == fpdef)) {
1152 res = validate_fpdef(CHILD(tree, pos));
1153 if (res) {
1154 if (TYPE(CHILD(tree, pos + 1)) == EQUAL) {
1155 res = validate_test(CHILD(tree, pos + 2));
1156 pos += 2;
1157 }
1158 res = res && validate_comma(CHILD(tree, pos + 1));
1159 pos += 2;
1160 }
1161 }
1162 if (res) {
1163 remaining = nch - pos;
1164 res = ((remaining == 2) || (remaining == 3)
1165 || (remaining == 5) || (remaining == 6));
1166 if (!res)
1167 (void) validate_numnodes(tree, 2, "varargslist");
1168 else if (TYPE(CHILD(tree, pos)) == DOUBLESTAR)
1169 return ((remaining == 2)
1170 && validate_ntype(CHILD(tree, pos+1), NAME));
1171 else {
1172 res = validate_star(CHILD(tree, pos++));
1173 --remaining;
1174 }
1175 }
1176 if (res) {
1177 if (remaining == 2) {
1178 res = (validate_star(CHILD(tree, pos))
1179 && validate_ntype(CHILD(tree, pos + 1), NAME));
1180 }
1181 else {
1182 res = validate_ntype(CHILD(tree, pos++), NAME);
1183 if (res && (remaining >= 4)) {
1184 res = validate_comma(CHILD(tree, pos));
1185 if (--remaining == 3)
1186 res = (validate_star(CHILD(tree, pos + 1))
1187 && validate_star(CHILD(tree, pos + 2)));
1188 else
1189 res = validate_ntype(CHILD(tree, pos + 1), DOUBLESTAR);
1190 }
1191 }
1192 }
1193 if (!res && !PyErr_Occurred())
1194 err_string("Incorrect validation of variable arguments list.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001195 }
1196 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001197 /* fpdef ['=' test] (',' fpdef ['=' test])* [','] */
1198 if (TYPE(CHILD(tree, nch - 1)) == COMMA)
1199 --nch;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001200
Fred Drakeff9ea482000-04-19 13:54:15 +00001201 /* fpdef ['=' test] (',' fpdef ['=' test])* */
1202 res = (is_odd(nch)
1203 && validate_fpdef(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001204
Fred Drakeff9ea482000-04-19 13:54:15 +00001205 if (res && (nch > 1)) {
1206 int pos = 1;
1207 if (TYPE(CHILD(tree, 1)) == EQUAL) {
1208 res = validate_test(CHILD(tree, 2));
1209 pos += 2;
1210 }
1211 /* ... (',' fpdef ['=' test])* */
1212 for ( ; res && (pos < nch); pos += 2) {
1213 /* ',' fpdef */
1214 res = (validate_comma(CHILD(tree, pos))
1215 && validate_fpdef(CHILD(tree, pos + 1)));
1216 if (res
1217 && ((nch - pos) > 2)
1218 && (TYPE(CHILD(tree, pos + 2)) == EQUAL)) {
1219 /* ['=' test] */
1220 res = validate_test(CHILD(tree, pos + 3));
1221 pos += 2;
1222 }
1223 }
1224 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001225 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001226 else {
1227 err_string("Improperly formed argument list.");
1228 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001229 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001230}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001231
1232
Guido van Rossum3d602e31996-07-21 02:33:56 +00001233/* VALIDATE(fpdef)
1234 *
1235 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001236 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001237 * | '(' fplist ')'
1238 */
Guido van Rossum47478871996-08-21 14:32:37 +00001239static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001240validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001241{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001242 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001243 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001244
Guido van Rossum3d602e31996-07-21 02:33:56 +00001245 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001246 if (nch == 1)
1247 res = validate_ntype(CHILD(tree, 0), NAME);
1248 else if (nch == 3)
1249 res = (validate_lparen(CHILD(tree, 0))
1250 && validate_fplist(CHILD(tree, 1))
1251 && validate_rparen(CHILD(tree, 2)));
1252 else
1253 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001254 }
1255 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001256}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001257
1258
Guido van Rossum47478871996-08-21 14:32:37 +00001259static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001260validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001261{
Guido van Rossum47478871996-08-21 14:32:37 +00001262 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001263 validate_fpdef, "fplist"));
1264}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001265
1266
Guido van Rossum3d602e31996-07-21 02:33:56 +00001267/* simple_stmt | compound_stmt
1268 *
1269 */
Guido van Rossum47478871996-08-21 14:32:37 +00001270static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001271validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001272{
1273 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001274 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001275
Guido van Rossum3d602e31996-07-21 02:33:56 +00001276 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001277 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001278
Fred Drakeff9ea482000-04-19 13:54:15 +00001279 if (TYPE(tree) == simple_stmt)
1280 res = validate_simple_stmt(tree);
1281 else
1282 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001283 }
1284 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001285}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001286
1287
Guido van Rossum3d602e31996-07-21 02:33:56 +00001288/* small_stmt (';' small_stmt)* [';'] NEWLINE
1289 *
1290 */
Guido van Rossum47478871996-08-21 14:32:37 +00001291static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001292validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001293{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001294 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001295 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001296 && (nch >= 2)
1297 && validate_small_stmt(CHILD(tree, 0))
1298 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001299
Guido van Rossum3d602e31996-07-21 02:33:56 +00001300 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001301 res = validate_numnodes(tree, 2, "simple_stmt");
1302 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001303 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001304 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001305 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001306 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001307
Fred Drakeff9ea482000-04-19 13:54:15 +00001308 for (i = 1; res && (i < nch); i += 2)
1309 res = (validate_semi(CHILD(tree, i))
1310 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001311 }
1312 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001313}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001314
1315
Guido van Rossum47478871996-08-21 14:32:37 +00001316static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001317validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001318{
1319 int nch = NCH(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +00001320 int res = (validate_numnodes(tree, 1, "small_stmt")
1321 && ((TYPE(CHILD(tree, 0)) == expr_stmt)
1322 || (TYPE(CHILD(tree, 0)) == print_stmt)
1323 || (TYPE(CHILD(tree, 0)) == del_stmt)
1324 || (TYPE(CHILD(tree, 0)) == pass_stmt)
1325 || (TYPE(CHILD(tree, 0)) == flow_stmt)
1326 || (TYPE(CHILD(tree, 0)) == import_stmt)
1327 || (TYPE(CHILD(tree, 0)) == global_stmt)
1328 || (TYPE(CHILD(tree, 0)) == assert_stmt)
1329 || (TYPE(CHILD(tree, 0)) == exec_stmt)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001330
1331 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001332 res = validate_node(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001333 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001334 char buffer[60];
1335 (void) sprintf(buffer, "Unrecognized child node of small_stmt: %d.",
1336 TYPE(CHILD(tree, 0)));
1337 err_string(buffer);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001338 }
1339 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001340}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001341
1342
1343/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001344 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001345 */
Guido van Rossum47478871996-08-21 14:32:37 +00001346static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001347validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001348{
1349 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001350 && validate_numnodes(tree, 1, "compound_stmt"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001351
1352 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001353 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001354
1355 tree = CHILD(tree, 0);
1356 res = ((TYPE(tree) == if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001357 || (TYPE(tree) == while_stmt)
1358 || (TYPE(tree) == for_stmt)
1359 || (TYPE(tree) == try_stmt)
1360 || (TYPE(tree) == funcdef)
1361 || (TYPE(tree) == classdef));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001362 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001363 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001364 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001365 char buffer[60];
1366 (void) sprintf(buffer, "Illegal compound statement type: %d.",
1367 TYPE(tree));
1368 err_string(buffer);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001369 }
1370 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001371}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001372
1373
Guido van Rossum47478871996-08-21 14:32:37 +00001374static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001375validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001376{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001377 int j;
1378 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001379 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001380 && is_odd(nch)
1381 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001382
Guido van Rossum3d602e31996-07-21 02:33:56 +00001383 for (j = 1; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001384 res = (validate_equal(CHILD(tree, j))
1385 && validate_testlist(CHILD(tree, j + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001386
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001387 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001388}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001389
1390
Guido van Rossum3d602e31996-07-21 02:33:56 +00001391/* print_stmt:
1392 *
Fred Drakeff9ea482000-04-19 13:54:15 +00001393 * 'print' (test ',')* [test]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001394 *
1395 */
Guido van Rossum47478871996-08-21 14:32:37 +00001396static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001397validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001398{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001399 int j;
1400 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001401 int res = (validate_ntype(tree, print_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001402 && (nch != 0)
1403 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001404
1405 if (res && is_even(nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001406 res = validate_test(CHILD(tree, nch - 1));
1407 --nch;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001408 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001409 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001410 (void) validate_numnodes(tree, 1, "print_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001411 for (j = 1; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001412 res = (validate_test(CHILD(tree, j))
1413 && validate_ntype(CHILD(tree, j + 1), COMMA));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001414
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001415 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001416}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001417
1418
Guido van Rossum47478871996-08-21 14:32:37 +00001419static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001420validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001421{
1422 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001423 && validate_name(CHILD(tree, 0), "del")
1424 && validate_exprlist(CHILD(tree, 1)));
1425}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001426
1427
Guido van Rossum47478871996-08-21 14:32:37 +00001428static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001429validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001430{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001431 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001432 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001433 && ((nch == 1) || (nch == 2))
1434 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001435
Guido van Rossum3d602e31996-07-21 02:33:56 +00001436 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001437 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001438
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001439 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001440}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001441
1442
Guido van Rossum47478871996-08-21 14:32:37 +00001443static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001444validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001445{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001446 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001447 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001448 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001449
Guido van Rossum3d602e31996-07-21 02:33:56 +00001450 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001451 res = validate_name(CHILD(tree, 0), "raise");
1452 if (res && (nch >= 2))
1453 res = validate_test(CHILD(tree, 1));
1454 if (res && nch > 2) {
1455 res = (validate_comma(CHILD(tree, 2))
1456 && validate_test(CHILD(tree, 3)));
1457 if (res && (nch > 4))
1458 res = (validate_comma(CHILD(tree, 4))
1459 && validate_test(CHILD(tree, 5)));
1460 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001461 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001462 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001463 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001464 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001465 res = (validate_comma(CHILD(tree, 2))
1466 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001467
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001468 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001469}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001470
1471
Guido van Rossum3d602e31996-07-21 02:33:56 +00001472/* import_stmt:
1473 *
1474 * 'import' dotted_name (',' dotted_name)*
1475 * | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*)
1476 */
Guido van Rossum47478871996-08-21 14:32:37 +00001477static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001478validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001479{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001480 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001481 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001482 && (nch >= 2) && is_even(nch)
1483 && validate_ntype(CHILD(tree, 0), NAME)
1484 && validate_ntype(CHILD(tree, 1), dotted_name));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001485
1486 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001487 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001488
Fred Drakeff9ea482000-04-19 13:54:15 +00001489 for (j = 2; res && (j < nch); j += 2)
1490 res = (validate_comma(CHILD(tree, j))
1491 && validate_ntype(CHILD(tree, j + 1), dotted_name));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001492 }
1493 else if (res && validate_name(CHILD(tree, 0), "from")) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001494 res = ((nch >= 4) && is_even(nch)
1495 && validate_name(CHILD(tree, 2), "import"));
1496 if (nch == 4) {
1497 res = ((TYPE(CHILD(tree, 3)) == NAME)
1498 || (TYPE(CHILD(tree, 3)) == STAR));
1499 if (!res)
1500 err_string("Illegal import statement.");
1501 }
1502 else {
1503 /* 'from' NAME 'import' NAME (',' NAME)+ */
1504 int j;
1505 res = validate_ntype(CHILD(tree, 3), NAME);
1506 for (j = 4; res && (j < nch); j += 2)
1507 res = (validate_comma(CHILD(tree, j))
1508 && validate_ntype(CHILD(tree, j + 1), NAME));
1509 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001510 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001511 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001512 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001513
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001514 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001515}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001516
1517
Guido van Rossum47478871996-08-21 14:32:37 +00001518static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001519validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001520{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001521 int j;
1522 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001523 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001524 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001525
Guido van Rossum3d602e31996-07-21 02:33:56 +00001526 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001527 res = (validate_name(CHILD(tree, 0), "global")
1528 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001529 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001530 res = (validate_comma(CHILD(tree, j))
1531 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001532
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001533 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001534}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001535
1536
Guido van Rossum3d602e31996-07-21 02:33:56 +00001537/* exec_stmt:
1538 *
1539 * 'exec' expr ['in' test [',' test]]
1540 */
Guido van Rossum47478871996-08-21 14:32:37 +00001541static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001542validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001543{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001544 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001545 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001546 && ((nch == 2) || (nch == 4) || (nch == 6))
1547 && validate_name(CHILD(tree, 0), "exec")
1548 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001549
Guido van Rossum3d602e31996-07-21 02:33:56 +00001550 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001551 err_string("Illegal exec statement.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001552 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001553 res = (validate_name(CHILD(tree, 2), "in")
1554 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001555 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001556 res = (validate_comma(CHILD(tree, 4))
1557 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001558
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001559 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001560}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001561
1562
Guido van Rossum925e5471997-04-02 05:32:13 +00001563/* assert_stmt:
1564 *
1565 * 'assert' test [',' test]
1566 */
1567static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001568validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001569{
1570 int nch = NCH(tree);
1571 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001572 && ((nch == 2) || (nch == 4))
1573 && (validate_name(CHILD(tree, 0), "__assert__") ||
1574 validate_name(CHILD(tree, 0), "assert"))
1575 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001576
1577 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001578 err_string("Illegal assert statement.");
Guido van Rossum925e5471997-04-02 05:32:13 +00001579 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001580 res = (validate_comma(CHILD(tree, 2))
1581 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001582
1583 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001584}
Guido van Rossum925e5471997-04-02 05:32:13 +00001585
1586
Guido van Rossum47478871996-08-21 14:32:37 +00001587static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001588validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001589{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001590 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001591 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001592 && ((nch == 4) || (nch == 7))
1593 && validate_name(CHILD(tree, 0), "while")
1594 && validate_test(CHILD(tree, 1))
1595 && validate_colon(CHILD(tree, 2))
1596 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001597
Guido van Rossum3d602e31996-07-21 02:33:56 +00001598 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001599 res = (validate_name(CHILD(tree, 4), "else")
1600 && validate_colon(CHILD(tree, 5))
1601 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001602
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001603 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001604}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001605
1606
Guido van Rossum47478871996-08-21 14:32:37 +00001607static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001608validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001609{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001610 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001611 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001612 && ((nch == 6) || (nch == 9))
1613 && validate_name(CHILD(tree, 0), "for")
1614 && validate_exprlist(CHILD(tree, 1))
1615 && validate_name(CHILD(tree, 2), "in")
1616 && validate_testlist(CHILD(tree, 3))
1617 && validate_colon(CHILD(tree, 4))
1618 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001619
Guido van Rossum3d602e31996-07-21 02:33:56 +00001620 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001621 res = (validate_name(CHILD(tree, 6), "else")
1622 && validate_colon(CHILD(tree, 7))
1623 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001624
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001625 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001626}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001627
1628
Guido van Rossum3d602e31996-07-21 02:33:56 +00001629/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001630 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001631 * | 'try' ':' suite 'finally' ':' suite
1632 *
1633 */
Guido van Rossum47478871996-08-21 14:32:37 +00001634static int
Guido van Rossum3d602e31996-07-21 02:33:56 +00001635validate_try(tree)
1636 node *tree;
1637{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001638 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001639 int pos = 3;
1640 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001641 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001642
1643 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001644 res = (validate_name(CHILD(tree, 0), "try")
1645 && validate_colon(CHILD(tree, 1))
1646 && validate_suite(CHILD(tree, 2))
1647 && validate_colon(CHILD(tree, nch - 2))
1648 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001649 else {
Fred Drake22269b52000-07-03 18:07:43 +00001650 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001651 char buffer[60];
1652 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1653 name = STR(CHILD(tree, nch - 3));
1654 (void) sprintf(buffer,
1655 "Illegal number of children for try/%s node.", name);
1656 err_string(buffer);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001657 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001658 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001659 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001660 res = (validate_except_clause(CHILD(tree, pos))
1661 && validate_colon(CHILD(tree, pos + 1))
1662 && validate_suite(CHILD(tree, pos + 2)));
1663 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001664 }
1665 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001666 res = validate_ntype(CHILD(tree, pos), NAME);
1667 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1668 res = (validate_numnodes(tree, 6, "try/finally")
1669 && validate_colon(CHILD(tree, 4))
1670 && validate_suite(CHILD(tree, 5)));
1671 else if (res) {
1672 if (nch == (pos + 3)) {
1673 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1674 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1675 if (!res)
1676 err_string("Illegal trailing triple in try statement.");
1677 }
1678 else if (nch == (pos + 6)) {
1679 res = (validate_name(CHILD(tree, pos), "except")
1680 && validate_colon(CHILD(tree, pos + 1))
1681 && validate_suite(CHILD(tree, pos + 2))
1682 && validate_name(CHILD(tree, pos + 3), "else"));
1683 }
1684 else
1685 res = validate_numnodes(tree, pos + 3, "try/except");
1686 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001687 }
1688 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001689}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001690
1691
Guido van Rossum47478871996-08-21 14:32:37 +00001692static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001693validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001694{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001695 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001696 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001697 && ((nch == 1) || (nch == 2) || (nch == 4))
1698 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001699
Guido van Rossum3d602e31996-07-21 02:33:56 +00001700 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001701 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001702 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001703 res = (validate_comma(CHILD(tree, 2))
1704 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001705
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001706 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001707}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001708
1709
Guido van Rossum47478871996-08-21 14:32:37 +00001710static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001711validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001712{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001713 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001714 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001715
Guido van Rossum3d602e31996-07-21 02:33:56 +00001716 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001717 res = ((nch == 1)
1718 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001719 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001720 int pos;
1721 res = validate_and_test(CHILD(tree, 0));
1722 for (pos = 1; res && (pos < nch); pos += 2)
1723 res = (validate_name(CHILD(tree, pos), "or")
1724 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001725 }
1726 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001727}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001728
1729
Guido van Rossum47478871996-08-21 14:32:37 +00001730static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001731validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001732{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001733 int pos;
1734 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001735 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001736 && is_odd(nch)
1737 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001738
Guido van Rossum3d602e31996-07-21 02:33:56 +00001739 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001740 res = (validate_name(CHILD(tree, pos), "and")
1741 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001742
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001743 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001744}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001745
1746
Guido van Rossum47478871996-08-21 14:32:37 +00001747static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001748validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001749{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001750 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001751 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001752
Guido van Rossum3d602e31996-07-21 02:33:56 +00001753 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001754 if (nch == 2)
1755 res = (validate_name(CHILD(tree, 0), "not")
1756 && validate_not_test(CHILD(tree, 1)));
1757 else if (nch == 1)
1758 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001759 }
1760 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001761}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001762
1763
Guido van Rossum47478871996-08-21 14:32:37 +00001764static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001765validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001766{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001767 int pos;
1768 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001769 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001770 && is_odd(nch)
1771 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001772
Guido van Rossum3d602e31996-07-21 02:33:56 +00001773 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001774 res = (validate_comp_op(CHILD(tree, pos))
1775 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001776
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001777 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001778}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001779
1780
Guido van Rossum47478871996-08-21 14:32:37 +00001781static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001782validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001783{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001784 int res = 0;
1785 int nch = NCH(tree);
1786
Guido van Rossum3d602e31996-07-21 02:33:56 +00001787 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001788 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001789 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001790 /*
1791 * Only child will be a terminal with a well-defined symbolic name
1792 * or a NAME with a string of either 'is' or 'in'
1793 */
1794 tree = CHILD(tree, 0);
1795 switch (TYPE(tree)) {
1796 case LESS:
1797 case GREATER:
1798 case EQEQUAL:
1799 case EQUAL:
1800 case LESSEQUAL:
1801 case GREATEREQUAL:
1802 case NOTEQUAL:
1803 res = 1;
1804 break;
1805 case NAME:
1806 res = ((strcmp(STR(tree), "in") == 0)
1807 || (strcmp(STR(tree), "is") == 0));
1808 if (!res) {
1809 char buff[128];
1810 (void) sprintf(buff, "Illegal operator: '%s'.", STR(tree));
1811 err_string(buff);
1812 }
1813 break;
1814 default:
1815 err_string("Illegal comparison operator type.");
1816 break;
1817 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001818 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00001819 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001820 res = (validate_ntype(CHILD(tree, 0), NAME)
1821 && validate_ntype(CHILD(tree, 1), NAME)
1822 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
1823 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
1824 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
1825 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
1826 if (!res && !PyErr_Occurred())
1827 err_string("Unknown comparison operator.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001828 }
1829 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001830}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001831
1832
Guido van Rossum47478871996-08-21 14:32:37 +00001833static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001834validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001835{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001836 int j;
1837 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001838 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001839 && is_odd(nch)
1840 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001841
Guido van Rossum3d602e31996-07-21 02:33:56 +00001842 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001843 res = (validate_xor_expr(CHILD(tree, j))
1844 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001845
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001846 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001847}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001848
1849
Guido van Rossum47478871996-08-21 14:32:37 +00001850static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001851validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001852{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001853 int j;
1854 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001855 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001856 && is_odd(nch)
1857 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001858
Guido van Rossum3d602e31996-07-21 02:33:56 +00001859 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001860 res = (validate_circumflex(CHILD(tree, j - 1))
1861 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001862
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001863 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001864}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001865
1866
Guido van Rossum47478871996-08-21 14:32:37 +00001867static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001868validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001869{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001870 int pos;
1871 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001872 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001873 && is_odd(nch)
1874 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001875
Guido van Rossum3d602e31996-07-21 02:33:56 +00001876 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001877 res = (validate_ampersand(CHILD(tree, pos))
1878 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001879
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001880 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001881}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001882
1883
1884static int
Guido van Rossum3d602e31996-07-21 02:33:56 +00001885validate_chain_two_ops(tree, termvalid, op1, op2)
1886 node *tree;
1887 int (*termvalid)();
1888 int op1;
1889 int op2;
1890 {
1891 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001892 int nch = NCH(tree);
1893 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00001894 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001895
Guido van Rossum3d602e31996-07-21 02:33:56 +00001896 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001897 if (TYPE(CHILD(tree, pos)) != op1)
1898 res = validate_ntype(CHILD(tree, pos), op2);
1899 if (res)
1900 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001901 }
1902 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001903}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001904
1905
Guido van Rossum47478871996-08-21 14:32:37 +00001906static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001907validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001908{
1909 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001910 && validate_chain_two_ops(tree, validate_arith_expr,
1911 LEFTSHIFT, RIGHTSHIFT));
1912}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001913
1914
Guido van Rossum47478871996-08-21 14:32:37 +00001915static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001916validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001917{
1918 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001919 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
1920}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001921
1922
Guido van Rossum47478871996-08-21 14:32:37 +00001923static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001924validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001925{
1926 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001927 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001928 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00001929 && is_odd(nch)
1930 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001931
Guido van Rossum3d602e31996-07-21 02:33:56 +00001932 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001933 res = (((TYPE(CHILD(tree, pos)) == STAR)
1934 || (TYPE(CHILD(tree, pos)) == SLASH)
1935 || (TYPE(CHILD(tree, pos)) == PERCENT))
1936 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001937
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001938 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001939}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001940
1941
Guido van Rossum3d602e31996-07-21 02:33:56 +00001942/* factor:
1943 *
1944 * factor: ('+'|'-'|'~') factor | power
1945 */
Guido van Rossum47478871996-08-21 14:32:37 +00001946static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001947validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001948{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001949 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001950 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00001951 && (((nch == 2)
1952 && ((TYPE(CHILD(tree, 0)) == PLUS)
1953 || (TYPE(CHILD(tree, 0)) == MINUS)
1954 || (TYPE(CHILD(tree, 0)) == TILDE))
1955 && validate_factor(CHILD(tree, 1)))
1956 || ((nch == 1)
1957 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001958 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001959}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001960
1961
Guido van Rossum3d602e31996-07-21 02:33:56 +00001962/* power:
1963 *
1964 * power: atom trailer* ('**' factor)*
1965 */
Guido van Rossum47478871996-08-21 14:32:37 +00001966static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001967validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001968{
1969 int pos = 1;
1970 int nch = NCH(tree);
1971 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00001972 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001973
1974 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00001975 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001976 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001977 if (!is_even(nch - pos)) {
1978 err_string("Illegal number of nodes for 'power'.");
1979 return (0);
1980 }
1981 for ( ; res && (pos < (nch - 1)); pos += 2)
1982 res = (validate_doublestar(CHILD(tree, pos))
1983 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001984 }
1985 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001986}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001987
1988
Guido van Rossum47478871996-08-21 14:32:37 +00001989static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001990validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001991{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001992 int pos;
1993 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001994 int res = validate_ntype(tree, atom) && (nch >= 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001995
1996 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001997 switch (TYPE(CHILD(tree, 0))) {
1998 case LPAR:
1999 res = ((nch <= 3)
2000 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002001
Fred Drakeff9ea482000-04-19 13:54:15 +00002002 if (res && (nch == 3))
2003 res = validate_testlist(CHILD(tree, 1));
2004 break;
2005 case LSQB:
2006 res = ((nch <= 3)
2007 && validate_ntype(CHILD(tree, nch - 1), RSQB));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002008
Fred Drakeff9ea482000-04-19 13:54:15 +00002009 if (res && (nch == 3))
2010 res = validate_testlist(CHILD(tree, 1));
2011 break;
2012 case LBRACE:
2013 res = ((nch <= 3)
2014 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002015
Fred Drakeff9ea482000-04-19 13:54:15 +00002016 if (res && (nch == 3))
2017 res = validate_dictmaker(CHILD(tree, 1));
2018 break;
2019 case BACKQUOTE:
2020 res = ((nch == 3)
2021 && validate_testlist(CHILD(tree, 1))
2022 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2023 break;
2024 case NAME:
2025 case NUMBER:
2026 res = (nch == 1);
2027 break;
2028 case STRING:
2029 for (pos = 1; res && (pos < nch); ++pos)
2030 res = validate_ntype(CHILD(tree, pos), STRING);
2031 break;
2032 default:
2033 res = 0;
2034 break;
2035 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002036 }
2037 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002038}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002039
2040
Guido van Rossum3d602e31996-07-21 02:33:56 +00002041/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002042 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002043 *
2044 */
Guido van Rossum47478871996-08-21 14:32:37 +00002045static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002046validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002047{
2048 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002049 && validate_numnodes(tree, 5, "funcdef")
2050 && validate_name(CHILD(tree, 0), "def")
2051 && validate_ntype(CHILD(tree, 1), NAME)
2052 && validate_colon(CHILD(tree, 3))
2053 && validate_parameters(CHILD(tree, 2))
2054 && validate_suite(CHILD(tree, 4)));
2055}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002056
2057
Guido van Rossum47478871996-08-21 14:32:37 +00002058static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002059validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002060{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002061 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002062 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002063 && ((nch == 3) || (nch == 4))
2064 && validate_name(CHILD(tree, 0), "lambda")
2065 && validate_colon(CHILD(tree, nch - 2))
2066 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002067
Guido van Rossum3d602e31996-07-21 02:33:56 +00002068 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002069 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002070 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002071 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002072
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002073 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002074}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002075
2076
Guido van Rossum3d602e31996-07-21 02:33:56 +00002077/* arglist:
2078 *
Fred Drakee7ab64e2000-04-25 04:14:46 +00002079 * (argument ',')* (argument* [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002080 */
Guido van Rossum47478871996-08-21 14:32:37 +00002081static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002082validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002083{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002084 int nch = NCH(tree);
Fred Drake7797d362000-07-04 18:48:46 +00002085 int i, ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002086 node *last;
2087
2088 if (nch <= 0)
2089 /* raise the right error from having an invalid number of children */
2090 return validate_numnodes(tree, nch + 1, "arglist");
2091
2092 last = CHILD(tree, nch - 1);
2093 if (TYPE(last) == test) {
2094 /* Extended call syntax introduced in Python 1.6 has been used;
2095 * validate and strip that off and continue;
2096 * adjust nch to perform the cut, and ensure resulting nch is even
2097 * (validation of the first part doesn't require that).
2098 */
2099 if (nch < 2) {
2100 validate_numnodes(tree, nch + 1, "arglist");
2101 return 0;
2102 }
2103 ok = validate_test(last);
2104 if (ok) {
2105 node *prev = CHILD(tree, nch - 2);
2106 /* next must be '*' or '**' */
2107 if (validate_doublestar(prev)) {
2108 nch -= 2;
2109 if (nch >= 3) {
2110 /* may include: '*' test ',' */
2111 last = CHILD(tree, nch - 1);
2112 prev = CHILD(tree, nch - 2);
2113 if (TYPE(prev) == test) {
2114 ok = validate_comma(last)
2115 && validate_test(prev)
2116 && validate_star(CHILD(tree, nch - 3));
2117 if (ok)
2118 nch -= 3;
2119 }
2120 /* otherwise, nothing special */
2121 }
2122 }
2123 else {
2124 /* must be only: '*' test */
2125 PyErr_Clear();
2126 ok = validate_star(prev);
2127 nch -= 2;
2128 }
2129 if (ok && is_odd(nch)) {
2130 /* Illegal number of nodes before extended call syntax;
2131 * validation of the "normal" arguments does not require
2132 * a trailing comma, but requiring an even number of
2133 * children will effect the same requirement.
2134 */
2135 return validate_numnodes(tree, nch + 1, "arglist");
2136 }
2137 }
2138 }
2139 /* what remains must be: (argument ",")* [argument [","]] */
2140 i = 0;
2141 while (ok && nch - i >= 2) {
2142 ok = validate_argument(CHILD(tree, i))
2143 && validate_comma(CHILD(tree, i + 1));
2144 i += 2;
2145 }
2146 if (ok && i < nch) {
2147 ok = validate_comma(CHILD(tree, i));
2148 ++i;
2149 }
2150 if (i != nch) {
2151 /* internal error! */
2152 ok = 0;
2153 err_string("arglist: internal error; nch != i");
2154 }
2155 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002156}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002157
2158
2159
2160/* argument:
2161 *
2162 * [test '='] test
2163 */
Guido van Rossum47478871996-08-21 14:32:37 +00002164static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002165validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002166{
2167 int nch = NCH(tree);
2168 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002169 && ((nch == 1) || (nch == 3))
2170 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002171
2172 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002173 res = (validate_equal(CHILD(tree, 1))
2174 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002175
2176 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002177}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002178
2179
2180
2181/* trailer:
2182 *
Guido van Rossum47478871996-08-21 14:32:37 +00002183 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002184 */
Guido van Rossum47478871996-08-21 14:32:37 +00002185static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002186validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002187{
2188 int nch = NCH(tree);
2189 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002190
2191 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002192 switch (TYPE(CHILD(tree, 0))) {
2193 case LPAR:
2194 res = validate_rparen(CHILD(tree, nch - 1));
2195 if (res && (nch == 3))
2196 res = validate_arglist(CHILD(tree, 1));
2197 break;
2198 case LSQB:
2199 res = (validate_numnodes(tree, 3, "trailer")
2200 && validate_subscriptlist(CHILD(tree, 1))
2201 && validate_ntype(CHILD(tree, 2), RSQB));
2202 break;
2203 case DOT:
2204 res = (validate_numnodes(tree, 2, "trailer")
2205 && validate_ntype(CHILD(tree, 1), NAME));
2206 break;
2207 default:
2208 res = 0;
2209 break;
2210 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002211 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002212 else {
2213 (void) validate_numnodes(tree, 2, "trailer");
2214 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002215 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002216}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002217
2218
Guido van Rossum47478871996-08-21 14:32:37 +00002219/* subscriptlist:
2220 *
2221 * subscript (',' subscript)* [',']
2222 */
2223static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002224validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002225{
2226 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002227 validate_subscript, "subscriptlist"));
2228}
Guido van Rossum47478871996-08-21 14:32:37 +00002229
2230
Guido van Rossum3d602e31996-07-21 02:33:56 +00002231/* subscript:
2232 *
Guido van Rossum47478871996-08-21 14:32:37 +00002233 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002234 */
Guido van Rossum47478871996-08-21 14:32:37 +00002235static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002236validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002237{
Guido van Rossum47478871996-08-21 14:32:37 +00002238 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002239 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002240 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002241
Guido van Rossum47478871996-08-21 14:32:37 +00002242 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002243 if (!PyErr_Occurred())
2244 err_string("invalid number of arguments for subscript node");
2245 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002246 }
Guido van Rossum47478871996-08-21 14:32:37 +00002247 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002248 /* take care of ('.' '.' '.') possibility */
2249 return (validate_numnodes(tree, 3, "subscript")
2250 && validate_dot(CHILD(tree, 0))
2251 && validate_dot(CHILD(tree, 1))
2252 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002253 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002254 if (TYPE(CHILD(tree, 0)) == test)
2255 res = validate_test(CHILD(tree, 0));
2256 else
2257 res = validate_colon(CHILD(tree, 0));
2258 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002259 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002260 /* Must be [test] ':' [test] [sliceop],
2261 * but at least one of the optional components will
2262 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002263 */
2264 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002265 res = validate_test(CHILD(tree, 0));
2266 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002267 }
2268 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002269 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002270 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002271 int rem = nch - ++offset;
2272 if (rem) {
2273 if (TYPE(CHILD(tree, offset)) == test) {
2274 res = validate_test(CHILD(tree, offset));
2275 ++offset;
2276 --rem;
2277 }
2278 if (res && rem)
2279 res = validate_sliceop(CHILD(tree, offset));
2280 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002281 }
2282 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002283}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002284
2285
Guido van Rossum47478871996-08-21 14:32:37 +00002286static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002287validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002288{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002289 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002290 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002291 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002292 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002293 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002294 }
Guido van Rossum47478871996-08-21 14:32:37 +00002295 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002296 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002297 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002298 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002299
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002300 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002301}
Guido van Rossum47478871996-08-21 14:32:37 +00002302
2303
2304static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002305validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002306{
2307 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002308 validate_expr, "exprlist"));
2309}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002310
2311
Guido van Rossum47478871996-08-21 14:32:37 +00002312static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002313validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002314{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002315 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002316 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002317 && (nch >= 3)
2318 && validate_test(CHILD(tree, 0))
2319 && validate_colon(CHILD(tree, 1))
2320 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002321
Guido van Rossum3d602e31996-07-21 02:33:56 +00002322 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002323 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002324 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002325 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002326
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002327 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002328 int pos = 3;
2329 /* ( ',' test ':' test )* */
2330 while (res && (pos < nch)) {
2331 res = (validate_comma(CHILD(tree, pos))
2332 && validate_test(CHILD(tree, pos + 1))
2333 && validate_colon(CHILD(tree, pos + 2))
2334 && validate_test(CHILD(tree, pos + 3)));
2335 pos += 4;
2336 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002337 }
2338 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002339}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002340
2341
Guido van Rossum47478871996-08-21 14:32:37 +00002342static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002343validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002344{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002345 int pos;
2346 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002347 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002348 && (nch >= 2)
2349 && validate_testlist(CHILD(tree, 0))
2350 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002351
Guido van Rossum3d602e31996-07-21 02:33:56 +00002352 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002353 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002354
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002355 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002356}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002357
2358
Guido van Rossum47478871996-08-21 14:32:37 +00002359static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002360validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002361{
Fred Drakeff9ea482000-04-19 13:54:15 +00002362 int nch = 0; /* num. children on current node */
2363 int res = 1; /* result value */
2364 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002365
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002366 while (res & (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002367 nch = NCH(tree);
2368 next = 0;
2369 switch (TYPE(tree)) {
2370 /*
2371 * Definition nodes.
2372 */
2373 case funcdef:
2374 res = validate_funcdef(tree);
2375 break;
2376 case classdef:
2377 res = validate_class(tree);
2378 break;
2379 /*
2380 * "Trivial" parse tree nodes.
2381 * (Why did I call these trivial?)
2382 */
2383 case stmt:
2384 res = validate_stmt(tree);
2385 break;
2386 case small_stmt:
2387 /*
2388 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
2389 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2390 */
2391 res = validate_small_stmt(tree);
2392 break;
2393 case flow_stmt:
2394 res = (validate_numnodes(tree, 1, "flow_stmt")
2395 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2396 || (TYPE(CHILD(tree, 0)) == continue_stmt)
2397 || (TYPE(CHILD(tree, 0)) == return_stmt)
2398 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2399 if (res)
2400 next = CHILD(tree, 0);
2401 else if (nch == 1)
2402 err_string("Illegal flow_stmt type.");
2403 break;
2404 /*
2405 * Compound statements.
2406 */
2407 case simple_stmt:
2408 res = validate_simple_stmt(tree);
2409 break;
2410 case compound_stmt:
2411 res = validate_compound_stmt(tree);
2412 break;
2413 /*
2414 * Fundemental statements.
2415 */
2416 case expr_stmt:
2417 res = validate_expr_stmt(tree);
2418 break;
2419 case print_stmt:
2420 res = validate_print_stmt(tree);
2421 break;
2422 case del_stmt:
2423 res = validate_del_stmt(tree);
2424 break;
2425 case pass_stmt:
2426 res = (validate_numnodes(tree, 1, "pass")
2427 && validate_name(CHILD(tree, 0), "pass"));
2428 break;
2429 case break_stmt:
2430 res = (validate_numnodes(tree, 1, "break")
2431 && validate_name(CHILD(tree, 0), "break"));
2432 break;
2433 case continue_stmt:
2434 res = (validate_numnodes(tree, 1, "continue")
2435 && validate_name(CHILD(tree, 0), "continue"));
2436 break;
2437 case return_stmt:
2438 res = validate_return_stmt(tree);
2439 break;
2440 case raise_stmt:
2441 res = validate_raise_stmt(tree);
2442 break;
2443 case import_stmt:
2444 res = validate_import_stmt(tree);
2445 break;
2446 case global_stmt:
2447 res = validate_global_stmt(tree);
2448 break;
2449 case exec_stmt:
2450 res = validate_exec_stmt(tree);
2451 break;
2452 case assert_stmt:
2453 res = validate_assert_stmt(tree);
2454 break;
2455 case if_stmt:
2456 res = validate_if(tree);
2457 break;
2458 case while_stmt:
2459 res = validate_while(tree);
2460 break;
2461 case for_stmt:
2462 res = validate_for(tree);
2463 break;
2464 case try_stmt:
2465 res = validate_try(tree);
2466 break;
2467 case suite:
2468 res = validate_suite(tree);
2469 break;
2470 /*
2471 * Expression nodes.
2472 */
2473 case testlist:
2474 res = validate_testlist(tree);
2475 break;
2476 case test:
2477 res = validate_test(tree);
2478 break;
2479 case and_test:
2480 res = validate_and_test(tree);
2481 break;
2482 case not_test:
2483 res = validate_not_test(tree);
2484 break;
2485 case comparison:
2486 res = validate_comparison(tree);
2487 break;
2488 case exprlist:
2489 res = validate_exprlist(tree);
2490 break;
2491 case comp_op:
2492 res = validate_comp_op(tree);
2493 break;
2494 case expr:
2495 res = validate_expr(tree);
2496 break;
2497 case xor_expr:
2498 res = validate_xor_expr(tree);
2499 break;
2500 case and_expr:
2501 res = validate_and_expr(tree);
2502 break;
2503 case shift_expr:
2504 res = validate_shift_expr(tree);
2505 break;
2506 case arith_expr:
2507 res = validate_arith_expr(tree);
2508 break;
2509 case term:
2510 res = validate_term(tree);
2511 break;
2512 case factor:
2513 res = validate_factor(tree);
2514 break;
2515 case power:
2516 res = validate_power(tree);
2517 break;
2518 case atom:
2519 res = validate_atom(tree);
2520 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002521
Fred Drakeff9ea482000-04-19 13:54:15 +00002522 default:
2523 /* Hopefully never reached! */
2524 err_string("Unrecogniged node type.");
2525 res = 0;
2526 break;
2527 }
2528 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002529 }
2530 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002531}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002532
2533
Guido van Rossum47478871996-08-21 14:32:37 +00002534static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002535validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002536{
2537 int res = validate_eval_input(tree);
2538
2539 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002540 err_string("Could not validate expression tuple.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002541
2542 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002543}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002544
2545
Guido van Rossum3d602e31996-07-21 02:33:56 +00002546/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002547 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002548 */
Guido van Rossum47478871996-08-21 14:32:37 +00002549static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002550validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002551{
2552 int j = 0;
2553 int nch = NCH(tree) - 1;
2554 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002555 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002556
Guido van Rossum3d602e31996-07-21 02:33:56 +00002557 for ( ; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002558 if (TYPE(CHILD(tree, j)) == stmt)
2559 res = validate_stmt(CHILD(tree, j));
2560 else
2561 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002562 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002563 /* This stays in to prevent any internal failues from getting to the
2564 * user. Hopefully, this won't be needed. If a user reports getting
2565 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002566 */
2567 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002568 err_string("VALIDATION FAILURE: report this to the maintainer!.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002569
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002570 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002571}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002572
2573
Fred Drake43f8f9b1998-04-13 16:25:46 +00002574static PyObject*
2575pickle_constructor = NULL;
2576
2577
2578static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002579parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002580{
Fred Drake268397f1998-04-29 14:16:32 +00002581 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002582 PyObject *result = NULL;
2583 PyObject *ast = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002584 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002585
2586 if (PyArg_ParseTuple(args, "O!:_pickler", &PyAST_Type, &ast)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002587 PyObject *newargs;
2588 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002589
Fred Drake2a6875e1999-09-20 22:32:18 +00002590 if ((empty_dict = PyDict_New()) == NULL)
2591 goto finally;
2592 if ((newargs = Py_BuildValue("Oi", ast, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002593 goto finally;
2594 tuple = parser_ast2tuple((PyAST_Object*)NULL, newargs, empty_dict);
2595 if (tuple != NULL) {
2596 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2597 Py_DECREF(tuple);
2598 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002599 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002600 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002601 }
2602 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002603 Py_XDECREF(empty_dict);
2604
Fred Drake43f8f9b1998-04-13 16:25:46 +00002605 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002606}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002607
2608
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002609/* Functions exported by this module. Most of this should probably
2610 * be converted into an AST object with methods, but that is better
2611 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002612 * We'd really have to write a wrapper around it all anyway to allow
2613 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002614 */
2615static PyMethodDef parser_functions[] = {
Fred Drakeff9ea482000-04-19 13:54:15 +00002616 {"ast2tuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
2617 "Creates a tuple-tree representation of an AST."},
2618 {"ast2list", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
2619 "Creates a list-tree representation of an AST."},
2620 {"compileast", (PyCFunction)parser_compileast, PUBLIC_METHOD_TYPE,
2621 "Compiles an AST object into a code object."},
2622 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
2623 "Creates an AST object from an expression."},
2624 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
2625 "Determines if an AST object was created from an expression."},
2626 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
2627 "Determines if an AST object was created from a suite."},
2628 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
2629 "Creates an AST object from a suite."},
2630 {"sequence2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
2631 "Creates an AST object from a tree representation."},
2632 {"tuple2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
2633 "Creates an AST object from a tree representation."},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002634
Fred Drake43f8f9b1998-04-13 16:25:46 +00002635 /* private stuff: support pickle module */
Fred Drakeff9ea482000-04-19 13:54:15 +00002636 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Fred Drake43f8f9b1998-04-13 16:25:46 +00002637 "Returns the pickle magic to allow ast objects to be pickled."},
2638
Fred Drake268397f1998-04-29 14:16:32 +00002639 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002640 };
2641
2642
Guido van Rossum3886bb61998-12-04 18:50:17 +00002643DL_EXPORT(void)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002644initparser()
2645 {
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002646 PyObject* module;
2647 PyObject* dict;
Fred Drakeff9ea482000-04-19 13:54:15 +00002648
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002649 PyAST_Type.ob_type = &PyType_Type;
2650 module = Py_InitModule("parser", parser_functions);
2651 dict = PyModule_GetDict(module);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002652
Fred Drake7a15ba51999-09-09 14:21:52 +00002653 if (parser_error == 0)
2654 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
2655 else
2656 puts("parser module initialized more than once!");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002657
2658 if ((parser_error == 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002659 || (PyDict_SetItemString(dict, "ParserError", parser_error) != 0)) {
2660 /*
2661 * This is serious.
2662 */
2663 Py_FatalError("can't define parser.ParserError");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002664 }
2665 /*
2666 * Nice to have, but don't cry if we fail.
2667 */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002668 Py_INCREF(&PyAST_Type);
2669 PyDict_SetItemString(dict, "ASTType", (PyObject*)&PyAST_Type);
2670
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002671 PyDict_SetItemString(dict, "__copyright__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002672 PyString_FromString(parser_copyright_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002673 PyDict_SetItemString(dict, "__doc__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002674 PyString_FromString(parser_doc_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002675 PyDict_SetItemString(dict, "__version__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002676 PyString_FromString(parser_version_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002677
Fred Drake43f8f9b1998-04-13 16:25:46 +00002678 /* register to support pickling */
2679 module = PyImport_ImportModule("copy_reg");
2680 if (module != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002681 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002682
Fred Drakeff9ea482000-04-19 13:54:15 +00002683 func = PyObject_GetAttrString(module, "pickle");
2684 pickle_constructor = PyDict_GetItemString(dict, "sequence2ast");
2685 pickler = PyDict_GetItemString(dict, "_pickler");
2686 Py_XINCREF(pickle_constructor);
2687 if ((func != NULL) && (pickle_constructor != NULL)
2688 && (pickler != NULL)) {
2689 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002690
Fred Drakeff9ea482000-04-19 13:54:15 +00002691 res = PyObject_CallFunction(
2692 func, "OOO", &PyAST_Type, pickler, pickle_constructor);
2693 Py_XDECREF(res);
2694 }
2695 Py_XDECREF(func);
2696 Py_DECREF(module);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002697 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002698}