blob: 056d2bbf27227bd37fc8ee24a033d8b07ba4ee02 [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000026 */
27
Fred Drakeff9ea482000-04-19 13:54:15 +000028#include "Python.h" /* general Python API */
29#include "graminit.h" /* symbols defined in the grammar */
30#include "node.h" /* internal parser structure */
31#include "token.h" /* token definitions */
32 /* ISTERMINAL() / ISNONTERMINAL() */
33#include "compile.h" /* PyNode_Compile() */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000034
Fred Drake268397f1998-04-29 14:16:32 +000035#ifdef lint
36#include <note.h>
37#else
38#define NOTE(x)
39#endif
40
Guido van Rossum19efc5f1998-04-28 16:10:19 +000041#ifdef macintosh
Fred Drakeff9ea482000-04-19 13:54:15 +000042char *strdup(char *);
Guido van Rossum19efc5f1998-04-28 16:10:19 +000043#endif
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000044
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000045/* String constants used to initialize module attributes.
46 *
47 */
48static char*
49parser_copyright_string
Guido van Rossum2a288461996-08-21 21:55:43 +000050= "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
51University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
52Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
53Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000054
55
56static char*
57parser_doc_string
58= "This is an interface to Python's internal parser.";
59
60static char*
Fred Drakecff283c2000-08-21 22:24:43 +000061parser_version_string = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000062
63
Fred Drakeff9ea482000-04-19 13:54:15 +000064typedef PyObject* (*SeqMaker) (int length);
65typedef int (*SeqInserter) (PyObject* sequence,
66 int index,
67 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000068
Thomas Wouters7e474022000-07-16 12:04:32 +000069/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000070 * original copyright statement is included below, and continues to apply
71 * in full to the function immediately following. All other material is
72 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
73 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000074 * new naming conventions. Added arguments to provide support for creating
75 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000076 */
77
Guido van Rossum52f2c051993-11-10 12:53:24 +000078/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000079Copyright (c) 2000, BeOpen.com.
80Copyright (c) 1995-2000, Corporation for National Research Initiatives.
81Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
82All rights reserved.
Guido van Rossum52f2c051993-11-10 12:53:24 +000083
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000084See the file "Misc/COPYRIGHT" for information on usage and
85redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum52f2c051993-11-10 12:53:24 +000086******************************************************************/
87
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000088static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000089node2tuple(node *n, /* node to convert */
90 SeqMaker mkseq, /* create sequence */
91 SeqInserter addelem, /* func. to add elem. in seq. */
92 int lineno) /* include line numbers? */
Guido van Rossum47478871996-08-21 14:32:37 +000093{
Guido van Rossum3d602e31996-07-21 02:33:56 +000094 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000095 Py_INCREF(Py_None);
96 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000097 }
98 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000099 int i;
100 PyObject *v;
101 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +0000102
Fred Drakeff9ea482000-04-19 13:54:15 +0000103 v = mkseq(1 + NCH(n));
104 if (v == NULL)
105 return (v);
106 w = PyInt_FromLong(TYPE(n));
107 if (w == NULL) {
108 Py_DECREF(v);
109 return ((PyObject*) NULL);
110 }
111 (void) addelem(v, 0, w);
112 for (i = 0; i < NCH(n); i++) {
113 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno);
114 if (w == NULL) {
115 Py_DECREF(v);
116 return ((PyObject*) NULL);
117 }
118 (void) addelem(v, i+1, w);
119 }
120 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000121 }
122 else if (ISTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000123 PyObject *result = mkseq(2 + lineno);
124 if (result != NULL) {
125 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
126 (void) addelem(result, 1, PyString_FromString(STR(n)));
127 if (lineno == 1)
128 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
129 }
130 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000131 }
132 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000133 PyErr_SetString(PyExc_SystemError,
134 "unrecognized parse tree node type");
135 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000136 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000137}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000138/*
139 * End of material copyrighted by Stichting Mathematisch Centrum.
140 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000141
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000142
143
144/* There are two types of intermediate objects we're interested in:
145 * 'eval' and 'exec' types. These constants can be used in the ast_type
146 * field of the object type to identify which any given object represents.
147 * These should probably go in an external header to allow other extensions
148 * to use them, but then, we really should be using C++ too. ;-)
149 *
Guido van Rossum3d602e31996-07-21 02:33:56 +0000150 * The PyAST_FRAGMENT type is not currently supported. Maybe not useful?
151 * Haven't decided yet.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000152 */
153
Fred Drakeff9ea482000-04-19 13:54:15 +0000154#define PyAST_EXPR 1
155#define PyAST_SUITE 2
156#define PyAST_FRAGMENT 3
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000157
158
159/* These are the internal objects and definitions required to implement the
160 * AST type. Most of the internal names are more reminiscent of the 'old'
161 * naming style, but the code uses the new naming convention.
162 */
163
164static PyObject*
165parser_error = 0;
166
167
168typedef struct _PyAST_Object {
Fred Drakeff9ea482000-04-19 13:54:15 +0000169 PyObject_HEAD /* standard object header */
170 node* ast_node; /* the node* returned by the parser */
171 int ast_type; /* EXPR or SUITE ? */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000172} PyAST_Object;
173
174
Fred Drake268397f1998-04-29 14:16:32 +0000175staticforward void
Fred Drakeff9ea482000-04-19 13:54:15 +0000176parser_free(PyAST_Object *ast);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000177
Fred Drake268397f1998-04-29 14:16:32 +0000178staticforward int
Fred Drakeff9ea482000-04-19 13:54:15 +0000179parser_compare(PyAST_Object *left, PyAST_Object *right);
Fred Drake268397f1998-04-29 14:16:32 +0000180
181staticforward PyObject *
Fred Drakeff9ea482000-04-19 13:54:15 +0000182parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000183
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000184
Fred Drake268397f1998-04-29 14:16:32 +0000185static
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000186PyTypeObject PyAST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000187 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000188 0,
Fred Drakeff9ea482000-04-19 13:54:15 +0000189 "ast", /* tp_name */
190 (int) sizeof(PyAST_Object), /* tp_basicsize */
191 0, /* tp_itemsize */
192 (destructor)parser_free, /* tp_dealloc */
193 0, /* tp_print */
194 parser_getattr, /* tp_getattr */
195 0, /* tp_setattr */
196 (cmpfunc)parser_compare, /* tp_compare */
197 0, /* tp_repr */
198 0, /* tp_as_number */
199 0, /* tp_as_sequence */
200 0, /* tp_as_mapping */
201 0, /* tp_hash */
202 0, /* tp_call */
203 0, /* tp_str */
204 0, /* tp_getattro */
205 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000206
207 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000208 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000209
Fred Drakeff9ea482000-04-19 13:54:15 +0000210 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000211
212 /* __doc__ */
213 "Intermediate representation of a Python parse tree."
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000214}; /* PyAST_Type */
215
216
217static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000218parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000219{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000220 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000221
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000222 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000223 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000224
225 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000226 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000227
228 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000229 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000230
231 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000232 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000233
234 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000235 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000236
237 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000238 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000239
Fred Drakeff9ea482000-04-19 13:54:15 +0000240 if (v != 0)
241 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000242 }
243 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000244}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000245
246
247/* int parser_compare(PyAST_Object* left, PyAST_Object* right)
248 *
249 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
250 * This really just wraps a call to parser_compare_nodes() with some easy
251 * checks and protection code.
252 *
253 */
254static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000255parser_compare(PyAST_Object *left, PyAST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000256{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000257 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000258 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000259
260 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000261 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000262
263 return (parser_compare_nodes(left->ast_node, right->ast_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000264}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265
266
267/* parser_newastobject(node* ast)
268 *
269 * Allocates a new Python object representing an AST. This is simply the
270 * 'wrapper' object that holds a node* and allows it to be passed around in
271 * Python code.
272 *
273 */
274static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000275parser_newastobject(node *ast, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000276{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000277 PyAST_Object* o = PyObject_New(PyAST_Object, &PyAST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000278
279 if (o != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000280 o->ast_node = ast;
281 o->ast_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000282 }
Fred Drake268397f1998-04-29 14:16:32 +0000283 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000284 PyNode_Free(ast);
Fred Drake268397f1998-04-29 14:16:32 +0000285 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000286 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000287}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000288
289
290/* void parser_free(PyAST_Object* ast)
291 *
292 * This is called by a del statement that reduces the reference count to 0.
293 *
294 */
295static void
Fred Drakeff9ea482000-04-19 13:54:15 +0000296parser_free(PyAST_Object *ast)
Guido van Rossum47478871996-08-21 14:32:37 +0000297{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000298 PyNode_Free(ast->ast_node);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000299 PyObject_Del(ast);
Fred Drakeff9ea482000-04-19 13:54:15 +0000300}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000301
302
Fred Drake2a6875e1999-09-20 22:32:18 +0000303/* parser_ast2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000304 *
305 * This provides conversion from a node* to a tuple object that can be
306 * returned to the Python-level caller. The AST object is not modified.
307 *
308 */
309static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000310parser_ast2tuple(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000311{
Guido van Rossum47478871996-08-21 14:32:37 +0000312 PyObject *line_option = 0;
313 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000314 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000315
Fred Drake7a15ba51999-09-09 14:21:52 +0000316 static char *keywords[] = {"ast", "line_info", NULL};
317
Fred Drake268397f1998-04-29 14:16:32 +0000318 if (self == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000319 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:ast2tuple", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000320 &PyAST_Type, &self, &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000321 }
Fred Drake503d8d61998-04-13 18:45:18 +0000322 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000323 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:totuple", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000324 &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000325 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000326 int lineno = 0;
327 if (line_option != NULL) {
328 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
329 }
330 /*
331 * Convert AST into a tuple representation. Use Guido's function,
332 * since it's known to work already.
333 */
334 res = node2tuple(((PyAST_Object*)self)->ast_node,
335 PyTuple_New, PyTuple_SetItem, lineno);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000336 }
337 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000338}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000339
340
Fred Drake2a6875e1999-09-20 22:32:18 +0000341/* parser_ast2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000342 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000343 * This provides conversion from a node* to a list object that can be
Guido van Rossum47478871996-08-21 14:32:37 +0000344 * returned to the Python-level caller. The AST object is not modified.
345 *
346 */
347static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000348parser_ast2list(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000349{
Guido van Rossum47478871996-08-21 14:32:37 +0000350 PyObject *line_option = 0;
351 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000352 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000353
Fred Drake7a15ba51999-09-09 14:21:52 +0000354 static char *keywords[] = {"ast", "line_info", NULL};
355
Fred Drake503d8d61998-04-13 18:45:18 +0000356 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000357 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:ast2list", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000358 &PyAST_Type, &self, &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000359 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000360 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:tolist", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000361 &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000362 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000363 int lineno = 0;
364 if (line_option != 0) {
365 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
366 }
367 /*
368 * Convert AST into a tuple representation. Use Guido's function,
369 * since it's known to work already.
370 */
371 res = node2tuple(self->ast_node,
372 PyList_New, PyList_SetItem, lineno);
Guido van Rossum47478871996-08-21 14:32:37 +0000373 }
374 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000375}
Guido van Rossum47478871996-08-21 14:32:37 +0000376
377
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000378/* parser_compileast(PyObject* self, PyObject* args)
379 *
380 * This function creates code objects from the parse tree represented by
381 * the passed-in data object. An optional file name is passed in as well.
382 *
383 */
384static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000385parser_compileast(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000386{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000387 PyObject* res = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000388 char* str = "<ast>";
Fred Drake503d8d61998-04-13 18:45:18 +0000389 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000390
Fred Drake7a15ba51999-09-09 14:21:52 +0000391 static char *keywords[] = {"ast", "filename", NULL};
392
Fred Drake503d8d61998-04-13 18:45:18 +0000393 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000394 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compileast", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000395 &PyAST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000396 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000397 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000398 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000399
400 if (ok)
Fred Drakeff9ea482000-04-19 13:54:15 +0000401 res = (PyObject *)PyNode_Compile(self->ast_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000402
403 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000404}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000405
406
407/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
408 * PyObject* parser_issuite(PyObject* self, PyObject* args)
409 *
410 * Checks the passed-in AST object to determine if it is an expression or
411 * a statement suite, respectively. The return is a Python truth value.
412 *
413 */
414static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000415parser_isexpr(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000416{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000417 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000418 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000419
Fred Drake7a15ba51999-09-09 14:21:52 +0000420 static char *keywords[] = {"ast", NULL};
421
Fred Drake503d8d61998-04-13 18:45:18 +0000422 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000423 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000424 &PyAST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000425 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000426 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000427
428 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000429 /* Check to see if the AST represents an expression or not. */
430 res = (self->ast_type == PyAST_EXPR) ? Py_True : Py_False;
431 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000432 }
433 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000434}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000435
436
437static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000438parser_issuite(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000439{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000440 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000441 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000442
Fred Drake7a15ba51999-09-09 14:21:52 +0000443 static char *keywords[] = {"ast", NULL};
444
Fred Drake503d8d61998-04-13 18:45:18 +0000445 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000446 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000447 &PyAST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000448 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000449 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000450
451 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000452 /* Check to see if the AST represents an expression or not. */
453 res = (self->ast_type == PyAST_EXPR) ? Py_False : Py_True;
454 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000455 }
456 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000457}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000458
459
Fred Drake7a15ba51999-09-09 14:21:52 +0000460#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
461
Fred Drake503d8d61998-04-13 18:45:18 +0000462static PyMethodDef
463parser_methods[] = {
Fred Drakeff9ea482000-04-19 13:54:15 +0000464 {"compile", (PyCFunction)parser_compileast, PUBLIC_METHOD_TYPE,
465 "Compile this AST object into a code object."},
466 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
467 "Determines if this AST object was created from an expression."},
468 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
469 "Determines if this AST object was created from a suite."},
470 {"tolist", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
471 "Creates a list-tree representation of this AST."},
472 {"totuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
473 "Creates a tuple-tree representation of this AST."},
Fred Drake503d8d61998-04-13 18:45:18 +0000474
Fred Drake268397f1998-04-29 14:16:32 +0000475 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000476};
477
Fred Drake503d8d61998-04-13 18:45:18 +0000478
479static PyObject*
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000480parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000481{
Fred Drake503d8d61998-04-13 18:45:18 +0000482 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000483}
Fred Drake503d8d61998-04-13 18:45:18 +0000484
485
Guido van Rossum3d602e31996-07-21 02:33:56 +0000486/* err_string(char* message)
487 *
488 * Sets the error string for an exception of type ParserError.
489 *
490 */
491static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000492err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000493{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000494 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000495}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000496
497
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000498/* PyObject* parser_do_parse(PyObject* args, int type)
499 *
500 * Internal function to actually execute the parse and return the result if
501 * successful, or set an exception if not.
502 *
503 */
504static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000505parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000506{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000507 char* string = 0;
508 PyObject* res = 0;
509
Fred Drake7a15ba51999-09-09 14:21:52 +0000510 static char *keywords[] = {"source", NULL};
511
512 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000513 node* n = PyParser_SimpleParseString(string,
514 (type == PyAST_EXPR)
515 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000516
Fred Drakeff9ea482000-04-19 13:54:15 +0000517 if (n != 0)
518 res = parser_newastobject(n, type);
519 else
520 err_string("Could not parse string.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000521 }
522 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000523}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000524
525
526/* PyObject* parser_expr(PyObject* self, PyObject* args)
527 * PyObject* parser_suite(PyObject* self, PyObject* args)
528 *
529 * External interfaces to the parser itself. Which is called determines if
530 * the parser attempts to recognize an expression ('eval' form) or statement
531 * suite ('exec' form). The real work is done by parser_do_parse() above.
532 *
533 */
534static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000535parser_expr(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000536{
Fred Drake268397f1998-04-29 14:16:32 +0000537 NOTE(ARGUNUSED(self))
Fred Drake7a15ba51999-09-09 14:21:52 +0000538 return (parser_do_parse(args, kw, "s:expr", PyAST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000539}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000540
541
542static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000543parser_suite(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000544{
Fred Drake268397f1998-04-29 14:16:32 +0000545 NOTE(ARGUNUSED(self))
Fred Drake7a15ba51999-09-09 14:21:52 +0000546 return (parser_do_parse(args, kw, "s:suite", PyAST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000547}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000548
549
550
551/* This is the messy part of the code. Conversion from a tuple to an AST
552 * object requires that the input tuple be valid without having to rely on
553 * catching an exception from the compiler. This is done to allow the
554 * compiler itself to remain fast, since most of its input will come from
555 * the parser directly, and therefore be known to be syntactically correct.
556 * This validation is done to ensure that we don't core dump the compile
557 * phase, returning an exception instead.
558 *
559 * Two aspects can be broken out in this code: creating a node tree from
560 * the tuple passed in, and verifying that it is indeed valid. It may be
561 * advantageous to expand the number of AST types to include funcdefs and
562 * lambdadefs to take advantage of the optimizer, recognizing those ASTs
563 * here. They are not necessary, and not quite as useful in a raw form.
564 * For now, let's get expressions and suites working reliably.
565 */
566
567
Fred Drakeff9ea482000-04-19 13:54:15 +0000568staticforward node* build_node_tree(PyObject *tuple);
569staticforward int validate_expr_tree(node *tree);
570staticforward int validate_file_input(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000571
572
573/* PyObject* parser_tuple2ast(PyObject* self, PyObject* args)
574 *
575 * This is the public function, called from the Python code. It receives a
576 * single tuple object from the caller, and creates an AST object if the
577 * tuple can be validated. It does this by checking the first code of the
578 * tuple, and, if acceptable, builds the internal representation. If this
579 * step succeeds, the internal representation is validated as fully as
580 * possible with the various validate_*() routines defined below.
581 *
582 * This function must be changed if support is to be added for PyAST_FRAGMENT
583 * AST objects.
584 *
585 */
586static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000587parser_tuple2ast(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000588{
Fred Drake268397f1998-04-29 14:16:32 +0000589 NOTE(ARGUNUSED(self))
Guido van Rossum47478871996-08-21 14:32:37 +0000590 PyObject *ast = 0;
591 PyObject *tuple = 0;
592 PyObject *temp = 0;
593 int ok;
Guido van Rossuma376cc51996-12-05 23:43:35 +0000594 int start_sym = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000595
Fred Drake7a15ba51999-09-09 14:21:52 +0000596 static char *keywords[] = {"sequence", NULL};
597
598 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:tuple2ast", keywords,
599 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000600 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000601 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000602 PyErr_SetString(PyExc_ValueError,
603 "tuple2ast() requires a single sequence argument");
604 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000605 }
606 /*
Fred Drakeff9ea482000-04-19 13:54:15 +0000607 * This mess of tests is written this way so we can use the abstract
608 * object interface (AOI). Unfortunately, the AOI increments reference
609 * counts, which requires that we store a pointer to retrieved object
610 * so we can DECREF it after the check. But we really should accept
611 * lists as well as tuples at the very least.
Guido van Rossum47478871996-08-21 14:32:37 +0000612 */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000613 ok = PyObject_Size(tuple) >= 2;
Guido van Rossum47478871996-08-21 14:32:37 +0000614 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000615 temp = PySequence_GetItem(tuple, 0);
616 ok = (temp != NULL) && PyInt_Check(temp);
617 if (ok)
618 /* this is used after the initial checks: */
619 start_sym = PyInt_AS_LONG(temp);
620 Py_XDECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000621 }
622 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000623 temp = PySequence_GetItem(tuple, 1);
624 ok = (temp != NULL) && PySequence_Check(temp);
625 Py_XDECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000626 }
627 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000628 temp = PySequence_GetItem(tuple, 1);
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000629 ok = (temp != NULL) && PyObject_Size(temp) >= 2;
Fred Drakeff9ea482000-04-19 13:54:15 +0000630 if (ok) {
631 PyObject *temp2 = PySequence_GetItem(temp, 0);
632 if (temp2 != NULL) {
633 ok = PyInt_Check(temp2);
634 Py_DECREF(temp2);
635 }
636 }
637 Py_XDECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000638 }
639 /* If we've failed at some point, get out of here. */
640 if (!ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000641 err_string("malformed sequence for tuple2ast()");
642 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000643 }
644 /*
645 * This might be a valid parse tree, but let's do a quick check
646 * before we jump the gun.
647 */
648 if (start_sym == eval_input) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000649 /* Might be an eval form. */
650 node* expression = build_node_tree(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000651
Fred Drakeff9ea482000-04-19 13:54:15 +0000652 if ((expression != 0) && validate_expr_tree(expression))
653 ast = parser_newastobject(expression, PyAST_EXPR);
Guido van Rossum47478871996-08-21 14:32:37 +0000654 }
655 else if (start_sym == file_input) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000656 /* This looks like an exec form so far. */
657 node* suite_tree = build_node_tree(tuple);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000658
Fred Drakeff9ea482000-04-19 13:54:15 +0000659 if ((suite_tree != 0) && validate_file_input(suite_tree))
660 ast = parser_newastobject(suite_tree, PyAST_SUITE);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000661 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000662 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000663 /* This is a fragment, and is not yet supported. Maybe they
664 * will be if I find a use for them.
665 */
666 err_string("Fragmentary parse trees not supported.");
Guido van Rossum47478871996-08-21 14:32:37 +0000667
668 /* Make sure we throw an exception on all errors. We should never
669 * get this, but we'd do well to be sure something is done.
670 */
671 if ((ast == 0) && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000672 err_string("Unspecified ast error occurred.");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000673
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000674 return (ast);
Fred Drakeff9ea482000-04-19 13:54:15 +0000675}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000676
677
678/* int check_terminal_tuple()
679 *
Guido van Rossum47478871996-08-21 14:32:37 +0000680 * Check a tuple to determine that it is indeed a valid terminal
681 * node. The node is known to be required as a terminal, so we throw
682 * an exception if there is a failure.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000683 *
Guido van Rossum47478871996-08-21 14:32:37 +0000684 * The format of an acceptable terminal tuple is "(is[i])": the fact
685 * that elem is a tuple and the integer is a valid terminal symbol
686 * has been established before this function is called. We must
687 * check the length of the tuple and the type of the second element
688 * and optional third element. We do *NOT* check the actual text of
689 * the string element, which we could do in many cases. This is done
690 * by the validate_*() functions which operate on the internal
691 * representation.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000692 */
693static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000694check_terminal_tuple(PyObject *elem)
Guido van Rossum47478871996-08-21 14:32:37 +0000695{
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000696 int len = PyObject_Size(elem);
Guido van Rossum47478871996-08-21 14:32:37 +0000697 int res = 1;
698 char* str = "Illegal terminal symbol; bad node length.";
Guido van Rossum3d602e31996-07-21 02:33:56 +0000699
Guido van Rossum47478871996-08-21 14:32:37 +0000700 if ((len == 2) || (len == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000701 PyObject *temp = PySequence_GetItem(elem, 1);
702 res = PyString_Check(temp);
703 str = "Illegal terminal symbol; expected a string.";
704 if (res && (len == 3)) {
705 PyObject* third = PySequence_GetItem(elem, 2);
706 res = PyInt_Check(third);
707 str = "Invalid third element of terminal node.";
708 Py_XDECREF(third);
709 }
710 Py_XDECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000711 }
712 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000713 res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000714 }
715 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000716 elem = Py_BuildValue("(os)", elem, str);
717 PyErr_SetObject(parser_error, elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000718 }
719 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000720}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000721
722
723/* node* build_node_children()
724 *
725 * Iterate across the children of the current non-terminal node and build
726 * their structures. If successful, return the root of this portion of
727 * the tree, otherwise, 0. Any required exception will be specified already,
728 * and no memory will have been deallocated.
729 *
730 */
731static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000732build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000733{
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000734 int len = PyObject_Size(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000735 int i;
736
737 for (i = 1; i < len; ++i) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000738 /* elem must always be a tuple, however simple */
739 PyObject* elem = PySequence_GetItem(tuple, i);
740 int ok = elem != NULL;
741 long type = 0;
742 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000743
Fred Drakeff9ea482000-04-19 13:54:15 +0000744 if (ok)
745 ok = PySequence_Check(elem);
746 if (ok) {
747 PyObject *temp = PySequence_GetItem(elem, 0);
748 if (temp == NULL)
749 ok = 0;
750 else {
751 ok = PyInt_Check(temp);
752 if (ok)
753 type = PyInt_AS_LONG(temp);
754 Py_DECREF(temp);
755 }
756 }
757 if (!ok) {
758 PyErr_SetObject(parser_error,
759 Py_BuildValue("(os)", elem,
760 "Illegal node construct."));
761 Py_XDECREF(elem);
762 return (0);
763 }
764 if (ISTERMINAL(type)) {
765 if (check_terminal_tuple(elem)) {
766 PyObject *temp = PySequence_GetItem(elem, 1);
Guido van Rossum47478871996-08-21 14:32:37 +0000767
Fred Drakeff9ea482000-04-19 13:54:15 +0000768 /* check_terminal_tuple() already verified it's a string */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000769 strn = (char *)PyMem_MALLOC(PyString_GET_SIZE(temp) + 1);
Fred Drakeff9ea482000-04-19 13:54:15 +0000770 if (strn != NULL)
771 (void) strcpy(strn, PyString_AS_STRING(temp));
Guido van Rossumb18618d2000-05-03 23:44:39 +0000772 Py_DECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000773
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000774 if (PyObject_Size(elem) == 3) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000775 PyObject* temp = PySequence_GetItem(elem, 2);
776 *line_num = PyInt_AsLong(temp);
777 Py_DECREF(temp);
778 }
779 }
780 else {
781 Py_XDECREF(elem);
782 return (0);
783 }
784 }
785 else if (!ISNONTERMINAL(type)) {
786 /*
787 * It has to be one or the other; this is an error.
788 * Throw an exception.
789 */
790 PyErr_SetObject(parser_error,
791 Py_BuildValue("(os)", elem,
792 "Unknown node type."));
793 Py_XDECREF(elem);
794 return (0);
795 }
796 PyNode_AddChild(root, type, strn, *line_num);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000797
Fred Drakeff9ea482000-04-19 13:54:15 +0000798 if (ISNONTERMINAL(type)) {
799 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000800
Fred Drakeff9ea482000-04-19 13:54:15 +0000801 if (new_child != build_node_children(elem, new_child, line_num)) {
802 Py_XDECREF(elem);
803 return (0);
804 }
805 }
806 else if (type == NEWLINE) { /* It's true: we increment the */
807 ++(*line_num); /* line number *after* the newline! */
808 }
809 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000810 }
811 return (root);
Fred Drakeff9ea482000-04-19 13:54:15 +0000812}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000813
814
815static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000816build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000817{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000818 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000819 PyObject *temp = PySequence_GetItem(tuple, 0);
820 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000821
Guido van Rossum47478871996-08-21 14:32:37 +0000822 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000823 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000824 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000825 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000826 /*
827 * The tuple is simple, but it doesn't start with a start symbol.
828 * Throw an exception now and be done with it.
829 */
830 tuple = Py_BuildValue("(os)", tuple,
831 "Illegal ast tuple; cannot start with terminal symbol.");
832 PyErr_SetObject(parser_error, tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000833 }
834 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000835 /*
836 * Not efficient, but that can be handled later.
837 */
838 int line_num = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000839
Fred Drakeff9ea482000-04-19 13:54:15 +0000840 res = PyNode_New(num);
841 if (res != build_node_children(tuple, res, &line_num)) {
842 PyNode_Free(res);
843 res = 0;
844 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000845 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000846 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000847 /* The tuple is illegal -- if the number is neither TERMINAL nor
848 * NONTERMINAL, we can't use it.
849 */
850 PyErr_SetObject(parser_error,
851 Py_BuildValue("(os)", tuple,
852 "Illegal component tuple."));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000853
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000854 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000855}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000856
857
Fred Drakeff9ea482000-04-19 13:54:15 +0000858#define VALIDATER(n) static int validate_##n(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000859
860
861/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000862 * Validation routines used within the validation section:
863 */
Fred Drakeff9ea482000-04-19 13:54:15 +0000864staticforward int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000865
Fred Drakeff9ea482000-04-19 13:54:15 +0000866#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
867#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
868#define validate_colon(ch) validate_terminal(ch, COLON, ":")
869#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
870#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
871#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
872#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
873#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
874#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
875#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
876#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
877#define validate_star(ch) validate_terminal(ch, STAR, "*")
878#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
879#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
880#define validate_dot(ch) validate_terminal(ch, DOT, ".")
881#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000882
Fred Drakeff9ea482000-04-19 13:54:15 +0000883VALIDATER(node); VALIDATER(small_stmt);
884VALIDATER(class); VALIDATER(node);
885VALIDATER(parameters); VALIDATER(suite);
886VALIDATER(testlist); VALIDATER(varargslist);
887VALIDATER(fpdef); VALIDATER(fplist);
888VALIDATER(stmt); VALIDATER(simple_stmt);
889VALIDATER(expr_stmt); VALIDATER(power);
890VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000891VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000892VALIDATER(raise_stmt); VALIDATER(import_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000893VALIDATER(global_stmt); VALIDATER(list_if);
894VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000895VALIDATER(exec_stmt); VALIDATER(compound_stmt);
896VALIDATER(while); VALIDATER(for);
897VALIDATER(try); VALIDATER(except_clause);
898VALIDATER(test); VALIDATER(and_test);
899VALIDATER(not_test); VALIDATER(comparison);
900VALIDATER(comp_op); VALIDATER(expr);
901VALIDATER(xor_expr); VALIDATER(and_expr);
902VALIDATER(shift_expr); VALIDATER(arith_expr);
903VALIDATER(term); VALIDATER(factor);
904VALIDATER(atom); VALIDATER(lambdef);
905VALIDATER(trailer); VALIDATER(subscript);
906VALIDATER(subscriptlist); VALIDATER(sliceop);
907VALIDATER(exprlist); VALIDATER(dictmaker);
908VALIDATER(arglist); VALIDATER(argument);
Fred Drakecff283c2000-08-21 22:24:43 +0000909VALIDATER(listmaker);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000910
911
Fred Drakeff9ea482000-04-19 13:54:15 +0000912#define is_even(n) (((n) & 1) == 0)
913#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000914
915
916static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000917validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000918{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000919 int res = (TYPE(n) == t);
920
921 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000922 char buffer[128];
923 (void) sprintf(buffer, "Expected node type %d, got %d.", t, TYPE(n));
924 err_string(buffer);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000925 }
926 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000927}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000928
929
Fred Drakee7ab64e2000-04-25 04:14:46 +0000930/* Verifies that the number of child nodes is exactly 'num', raising
931 * an exception if it isn't. The exception message does not indicate
932 * the exact number of nodes, allowing this to be used to raise the
933 * "right" exception when the wrong number of nodes is present in a
934 * specific variant of a statement's syntax. This is commonly used
935 * in that fashion.
936 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000937static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000938validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000939{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000940 if (NCH(n) != num) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000941 char buff[60];
942 (void) sprintf(buff, "Illegal number of children for %s node.", name);
943 err_string(buff);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000944 }
945 return (NCH(n) == num);
Fred Drakeff9ea482000-04-19 13:54:15 +0000946}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000947
948
949static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000950validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000951{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000952 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000953 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000954
955 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000956 char buffer[60];
957 (void) sprintf(buffer, "Illegal terminal: expected \"%s\"", string);
958 err_string(buffer);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000959 }
960 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000961}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000962
963
Guido van Rossum47478871996-08-21 14:32:37 +0000964/* X (',' X) [',']
965 */
966static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000967validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000968 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000969{
970 int nch = NCH(tree);
971 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000972 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000973
974 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000975 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000976 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000977 if (is_even(nch))
978 res = validate_comma(CHILD(tree, --nch));
979 if (res && nch > 1) {
980 int pos = 1;
981 for ( ; res && pos < nch; pos += 2)
982 res = (validate_comma(CHILD(tree, pos))
983 && vfunc(CHILD(tree, pos + 1)));
984 }
Guido van Rossum47478871996-08-21 14:32:37 +0000985 }
986 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000987}
Guido van Rossum47478871996-08-21 14:32:37 +0000988
989
Fred Drakecff283c2000-08-21 22:24:43 +0000990/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000991 *
992 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000993 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000994 */
Guido van Rossum47478871996-08-21 14:32:37 +0000995static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000996validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000997{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000998 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000999 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001000
Guido van Rossum3d602e31996-07-21 02:33:56 +00001001 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001002 res = (validate_name(CHILD(tree, 0), "class")
1003 && validate_ntype(CHILD(tree, 1), NAME)
1004 && validate_colon(CHILD(tree, nch - 2))
1005 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001006 }
1007 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001008 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001009 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001010 res = (validate_lparen(CHILD(tree, 2))
1011 && validate_testlist(CHILD(tree, 3))
1012 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001013 }
1014 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001015}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001016
1017
Guido van Rossum3d602e31996-07-21 02:33:56 +00001018/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001019 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001020 */
Guido van Rossum47478871996-08-21 14:32:37 +00001021static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001022validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001023{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001024 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001025 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001026 && (nch >= 4)
1027 && validate_name(CHILD(tree, 0), "if")
1028 && validate_test(CHILD(tree, 1))
1029 && validate_colon(CHILD(tree, 2))
1030 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001031
1032 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001033 /* ... 'else' ':' suite */
1034 res = (validate_name(CHILD(tree, nch - 3), "else")
1035 && validate_colon(CHILD(tree, nch - 2))
1036 && validate_suite(CHILD(tree, nch - 1)));
1037 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001038 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001039 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001040 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001041 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001042 /* Will catch the case for nch < 4 */
1043 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001044 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001045 /* ... ('elif' test ':' suite)+ ... */
1046 int j = 4;
1047 while ((j < nch) && res) {
1048 res = (validate_name(CHILD(tree, j), "elif")
1049 && validate_colon(CHILD(tree, j + 2))
1050 && validate_test(CHILD(tree, j + 1))
1051 && validate_suite(CHILD(tree, j + 3)));
1052 j += 4;
1053 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001054 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001055 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001056}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001057
1058
Guido van Rossum3d602e31996-07-21 02:33:56 +00001059/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001060 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001061 *
1062 */
Guido van Rossum47478871996-08-21 14:32:37 +00001063static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001064validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001065{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001066 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001067 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001068
Guido van Rossum3d602e31996-07-21 02:33:56 +00001069 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001070 res = (validate_lparen(CHILD(tree, 0))
1071 && validate_rparen(CHILD(tree, nch - 1)));
1072 if (res && (nch == 3))
1073 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001074 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001075 else {
1076 (void) validate_numnodes(tree, 2, "parameters");
1077 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001078 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001079}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001080
1081
Fred Drakecff283c2000-08-21 22:24:43 +00001082/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001083 *
1084 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001085 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001086 * | NEWLINE INDENT stmt+ DEDENT
1087 */
Guido van Rossum47478871996-08-21 14:32:37 +00001088static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001089validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001090{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001091 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001092 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001093
Guido van Rossum3d602e31996-07-21 02:33:56 +00001094 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001095 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001096 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001097 /* NEWLINE INDENT stmt+ DEDENT */
1098 res = (validate_newline(CHILD(tree, 0))
1099 && validate_indent(CHILD(tree, 1))
1100 && validate_stmt(CHILD(tree, 2))
1101 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001102
Fred Drakeff9ea482000-04-19 13:54:15 +00001103 if (res && (nch > 4)) {
1104 int i = 3;
1105 --nch; /* forget the DEDENT */
1106 for ( ; res && (i < nch); ++i)
1107 res = validate_stmt(CHILD(tree, i));
1108 }
1109 else if (nch < 4)
1110 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001111 }
1112 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001113}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001114
1115
Guido van Rossum47478871996-08-21 14:32:37 +00001116static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001117validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001118{
Guido van Rossum47478871996-08-21 14:32:37 +00001119 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001120 validate_test, "testlist"));
1121}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001122
1123
Fred Drakecff283c2000-08-21 22:24:43 +00001124/* '*' NAME [',' '**' NAME] | '**' NAME
1125 */
1126static int
1127validate_varargslist_trailer(node *tree, int start)
1128{
1129 int nch = NCH(tree);
1130 int res = 0;
1131 int sym;
1132
1133 if (nch <= start) {
1134 err_string("expected variable argument trailer for varargslist");
1135 return 0;
1136 }
1137 sym = TYPE(CHILD(tree, start));
1138 if (sym == STAR) {
1139 /*
1140 * ('*' NAME [',' '**' NAME]
1141 */
1142 if (nch-start == 2)
1143 res = validate_name(CHILD(tree, start+1), NULL);
1144 else if (nch-start == 5)
1145 res = (validate_name(CHILD(tree, start+1), NULL)
1146 && validate_comma(CHILD(tree, start+2))
1147 && validate_doublestar(CHILD(tree, start+3))
1148 && validate_name(CHILD(tree, start+4), NULL));
1149 }
1150 else if (sym == DOUBLESTAR) {
1151 /*
1152 * '**' NAME
1153 */
1154 if (nch-start == 2)
1155 res = validate_name(CHILD(tree, start+1), NULL);
1156 }
1157 if (!res)
1158 err_string("illegal variable argument trailer for varargslist");
1159 return res;
1160}
1161
1162
1163/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001164 *
1165 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001166 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001167 * ('*' NAME [',' '**' NAME]
1168 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001169 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1170 *
1171 */
Guido van Rossum47478871996-08-21 14:32:37 +00001172static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001173validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001174{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001175 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001176 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001177 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001178
Fred Drakecff283c2000-08-21 22:24:43 +00001179 if (nch < 1) {
1180 err_string("varargslist missing child nodes");
1181 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001182 }
Fred Drakecff283c2000-08-21 22:24:43 +00001183 sym = TYPE(CHILD(tree, 0));
1184 if (sym == STAR || sym == DOUBLESTAR)
1185 res = validate_varargslist_trailer(tree, 0);
1186 else if (sym == fpdef) {
1187 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001188
Fred Drakecff283c2000-08-21 22:24:43 +00001189 sym = TYPE(CHILD(tree, nch-1));
1190 if (sym == NAME) {
1191 /*
1192 * (fpdef ['=' test] ',')+
1193 * ('*' NAME [',' '**' NAME]
1194 * | '**' NAME)
1195 */
1196 /* skip over (fpdef ['=' test] ',')+ */
1197 while (res && (i+2 <= nch)) {
1198 res = validate_fpdef(CHILD(tree, i));
1199 ++i;
1200 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1201 res = (validate_equal(CHILD(tree, i))
1202 && validate_test(CHILD(tree, i+1)));
1203 if (res)
1204 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001205 }
Fred Drakecff283c2000-08-21 22:24:43 +00001206 if (res && i < nch) {
1207 res = validate_comma(CHILD(tree, i));
1208 if (res)
1209 ++i;
1210 }
1211 }
1212 /* handle '*' NAME [',' '**' NAME] | '**' NAME */
1213 if (res)
1214 res = validate_varargslist_trailer(tree, i);
1215 }
1216 else {
1217 /*
1218 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1219 */
1220 if (sym == COMMA) {
1221 res = validate_comma(CHILD(tree, nch-1));
1222 if (!res)
1223 return 0;
1224 --nch;
1225 }
1226 /*
1227 * fpdef ['=' test] (',' fpdef ['=' test])*
1228 */
1229 res = validate_fpdef(CHILD(tree, 0));
1230 ++i;
1231 if (res && (i+2 < nch) && TYPE(CHILD(tree, 1)) == EQUAL) {
1232 res = (validate_equal(CHILD(tree, 1))
1233 && validate_test(CHILD(tree, 2)));
1234 i += 2;
1235 }
1236 /*
1237 * ... (',' fpdef ['=' test])*
1238 * i ---^^^
1239 */
1240 while (res && (nch - i) >= 2) {
1241 res = (validate_comma(CHILD(tree, i))
1242 && validate_fpdef(CHILD(tree, i+1)));
1243 i += 2;
1244 if (res && (nch - i) >= 2
1245 && TYPE(CHILD(tree, i)) == COMMA) {
1246 res = (validate_comma(CHILD(tree, i))
1247 && validate_test(CHILD(tree, i+1)));
1248 if (res)
1249 i += 2;
1250 }
1251 }
1252 if (res && nch - i != 0) {
1253 res = 0;
1254 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001255 }
1256 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001257 }
Fred Drakecff283c2000-08-21 22:24:43 +00001258 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001259}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001260
1261
Fred Drakecff283c2000-08-21 22:24:43 +00001262/* list_iter: list_for | list_if
1263 */
1264static int
1265validate_list_iter(node *tree)
1266{
1267 int res = (validate_ntype(tree, list_iter)
1268 && validate_numnodes(tree, 1, "list_iter"));
1269 if (res && TYPE(CHILD(tree, 0)) == list_for)
1270 res = validate_list_for(CHILD(tree, 0));
1271 else
1272 res = validate_list_if(CHILD(tree, 0));
1273
1274 return res;
1275}
1276
1277/* list_for: 'for' exprlist 'in' testlist [list_iter]
1278 */
1279static int
1280validate_list_for(node *tree)
1281{
1282 int nch = NCH(tree);
1283 int res;
1284
1285 if (nch == 5)
1286 res = validate_list_iter(CHILD(tree, 4));
1287 else
1288 res = validate_numnodes(tree, 4, "list_for");
1289
1290 if (res)
1291 res = (validate_name(CHILD(tree, 0), "for")
1292 && validate_exprlist(CHILD(tree, 1))
1293 && validate_name(CHILD(tree, 2), "in")
1294 && validate_testlist(CHILD(tree, 3)));
1295
1296 return res;
1297}
1298
1299/* list_if: 'if' test [list_iter]
1300 */
1301static int
1302validate_list_if(node *tree)
1303{
1304 int nch = NCH(tree);
1305 int res;
1306
1307 if (nch == 3)
1308 res = validate_list_iter(CHILD(tree, 2));
1309 else
1310 res = validate_numnodes(tree, 2, "list_if");
1311
1312 if (res)
1313 res = (validate_name(CHILD(tree, 0), "if")
1314 && validate_test(CHILD(tree, 1)));
1315
1316 return res;
1317}
1318
1319
1320/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001321 *
1322 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001323 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001324 * | '(' fplist ')'
1325 */
Guido van Rossum47478871996-08-21 14:32:37 +00001326static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001327validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001328{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001329 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001330 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001331
Guido van Rossum3d602e31996-07-21 02:33:56 +00001332 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001333 if (nch == 1)
1334 res = validate_ntype(CHILD(tree, 0), NAME);
1335 else if (nch == 3)
1336 res = (validate_lparen(CHILD(tree, 0))
1337 && validate_fplist(CHILD(tree, 1))
1338 && validate_rparen(CHILD(tree, 2)));
1339 else
1340 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001341 }
1342 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001343}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001344
1345
Guido van Rossum47478871996-08-21 14:32:37 +00001346static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001347validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001348{
Guido van Rossum47478871996-08-21 14:32:37 +00001349 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001350 validate_fpdef, "fplist"));
1351}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001352
1353
Guido van Rossum3d602e31996-07-21 02:33:56 +00001354/* simple_stmt | compound_stmt
1355 *
1356 */
Guido van Rossum47478871996-08-21 14:32:37 +00001357static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001358validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001359{
1360 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001361 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001362
Guido van Rossum3d602e31996-07-21 02:33:56 +00001363 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001364 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001365
Fred Drakeff9ea482000-04-19 13:54:15 +00001366 if (TYPE(tree) == simple_stmt)
1367 res = validate_simple_stmt(tree);
1368 else
1369 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001370 }
1371 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001372}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001373
1374
Guido van Rossum3d602e31996-07-21 02:33:56 +00001375/* small_stmt (';' small_stmt)* [';'] NEWLINE
1376 *
1377 */
Guido van Rossum47478871996-08-21 14:32:37 +00001378static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001379validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001380{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001381 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001382 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001383 && (nch >= 2)
1384 && validate_small_stmt(CHILD(tree, 0))
1385 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001386
Guido van Rossum3d602e31996-07-21 02:33:56 +00001387 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001388 res = validate_numnodes(tree, 2, "simple_stmt");
1389 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001390 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001391 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001392 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001393 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001394
Fred Drakeff9ea482000-04-19 13:54:15 +00001395 for (i = 1; res && (i < nch); i += 2)
1396 res = (validate_semi(CHILD(tree, i))
1397 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001398 }
1399 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001400}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001401
1402
Guido van Rossum47478871996-08-21 14:32:37 +00001403static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001404validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001405{
1406 int nch = NCH(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +00001407 int res = (validate_numnodes(tree, 1, "small_stmt")
1408 && ((TYPE(CHILD(tree, 0)) == expr_stmt)
1409 || (TYPE(CHILD(tree, 0)) == print_stmt)
1410 || (TYPE(CHILD(tree, 0)) == del_stmt)
1411 || (TYPE(CHILD(tree, 0)) == pass_stmt)
1412 || (TYPE(CHILD(tree, 0)) == flow_stmt)
1413 || (TYPE(CHILD(tree, 0)) == import_stmt)
1414 || (TYPE(CHILD(tree, 0)) == global_stmt)
1415 || (TYPE(CHILD(tree, 0)) == assert_stmt)
1416 || (TYPE(CHILD(tree, 0)) == exec_stmt)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001417
1418 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001419 res = validate_node(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001420 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001421 char buffer[60];
1422 (void) sprintf(buffer, "Unrecognized child node of small_stmt: %d.",
1423 TYPE(CHILD(tree, 0)));
1424 err_string(buffer);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001425 }
1426 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001427}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001428
1429
1430/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001431 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001432 */
Guido van Rossum47478871996-08-21 14:32:37 +00001433static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001434validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001435{
1436 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001437 && validate_numnodes(tree, 1, "compound_stmt"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001438
1439 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001440 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001441
1442 tree = CHILD(tree, 0);
1443 res = ((TYPE(tree) == if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001444 || (TYPE(tree) == while_stmt)
1445 || (TYPE(tree) == for_stmt)
1446 || (TYPE(tree) == try_stmt)
1447 || (TYPE(tree) == funcdef)
1448 || (TYPE(tree) == classdef));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001449 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001450 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001451 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001452 char buffer[60];
1453 (void) sprintf(buffer, "Illegal compound statement type: %d.",
1454 TYPE(tree));
1455 err_string(buffer);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001456 }
1457 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001458}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001459
1460
Guido van Rossum47478871996-08-21 14:32:37 +00001461static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001462validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001463{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001464 int j;
1465 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001466 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001467 && is_odd(nch)
1468 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001469
Fred Drake28f739a2000-08-25 22:42:40 +00001470 if (res && nch == 3
1471 && TYPE(CHILD(tree, 1)) == augassign) {
1472 res = (validate_numnodes(CHILD(tree, 1), 1, "augassign")
1473 && validate_testlist(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001474
Fred Drake28f739a2000-08-25 22:42:40 +00001475 if (res) {
1476 char *s = STR(CHILD(CHILD(tree, 1), 0));
1477
1478 res = (strcmp(s, "+=") == 0
1479 || strcmp(s, "-=") == 0
1480 || strcmp(s, "*=") == 0
1481 || strcmp(s, "/=") == 0
1482 || strcmp(s, "%=") == 0
1483 || strcmp(s, "&=") == 0
1484 || strcmp(s, "|=") == 0
1485 || strcmp(s, "^=") == 0
1486 || strcmp(s, "<<=") == 0
1487 || strcmp(s, ">>=") == 0
1488 || strcmp(s, "**=") == 0);
1489 if (!res)
1490 err_string("illegal augmmented assignment operator");
1491 }
1492 }
1493 else {
1494 for (j = 1; res && (j < nch); j += 2)
1495 res = (validate_equal(CHILD(tree, j))
1496 && validate_testlist(CHILD(tree, j + 1)));
1497 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001498 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001499}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001500
1501
Guido van Rossum3d602e31996-07-21 02:33:56 +00001502/* print_stmt:
1503 *
Fred Drakecff283c2000-08-21 22:24:43 +00001504 * 'print' ( [ test (',' test)* [','] ]
1505 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001506 */
Guido van Rossum47478871996-08-21 14:32:37 +00001507static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001508validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001509{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001510 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001511 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001512 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001513 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001514
Fred Drakecff283c2000-08-21 22:24:43 +00001515 if (res && nch > 1) {
1516 int sym = TYPE(CHILD(tree, 1));
1517 int i = 1;
1518 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001519
Fred Drakecff283c2000-08-21 22:24:43 +00001520 if (sym == test)
1521 res = validate_test(CHILD(tree, i++));
1522 else {
1523 if (nch < 3)
1524 res = validate_numnodes(tree, 3, "print_stmt");
1525 else {
1526 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1527 && validate_test(CHILD(tree, i+1)));
1528 i += 2;
1529 allow_trailing_comma = 0;
1530 }
1531 }
1532 if (res) {
1533 /* ... (',' test)* [','] */
1534 while (res && i+2 <= nch) {
1535 res = (validate_comma(CHILD(tree, i))
1536 && validate_test(CHILD(tree, i+1)));
1537 allow_trailing_comma = 1;
1538 i += 2;
1539 }
1540 if (res && !allow_trailing_comma)
1541 res = validate_numnodes(tree, i, "print_stmt");
1542 else if (res && i < nch)
1543 res = validate_comma(CHILD(tree, i));
1544 }
1545 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001546 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001547}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001548
1549
Guido van Rossum47478871996-08-21 14:32:37 +00001550static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001551validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001552{
1553 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001554 && validate_name(CHILD(tree, 0), "del")
1555 && validate_exprlist(CHILD(tree, 1)));
1556}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001557
1558
Guido van Rossum47478871996-08-21 14:32:37 +00001559static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001560validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001561{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001562 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001563 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001564 && ((nch == 1) || (nch == 2))
1565 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001566
Guido van Rossum3d602e31996-07-21 02:33:56 +00001567 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001568 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001569
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001570 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001571}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001572
1573
Guido van Rossum47478871996-08-21 14:32:37 +00001574static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001575validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001576{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001577 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001578 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001579 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001580
Guido van Rossum3d602e31996-07-21 02:33:56 +00001581 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001582 res = validate_name(CHILD(tree, 0), "raise");
1583 if (res && (nch >= 2))
1584 res = validate_test(CHILD(tree, 1));
1585 if (res && nch > 2) {
1586 res = (validate_comma(CHILD(tree, 2))
1587 && validate_test(CHILD(tree, 3)));
1588 if (res && (nch > 4))
1589 res = (validate_comma(CHILD(tree, 4))
1590 && validate_test(CHILD(tree, 5)));
1591 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001592 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001593 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001594 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001595 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001596 res = (validate_comma(CHILD(tree, 2))
1597 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001598
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001599 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001600}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001601
1602
Fred Drakecff283c2000-08-21 22:24:43 +00001603static int
1604validate_import_as_name(node *tree)
1605{
1606 int nch = NCH(tree);
1607 int ok = validate_ntype(tree, import_as_name);
1608
1609 if (ok) {
1610 if (nch == 1)
1611 ok = validate_name(CHILD(tree, 0), NULL);
1612 else if (nch == 3)
1613 ok = (validate_name(CHILD(tree, 0), NULL)
1614 && validate_name(CHILD(tree, 1), "as")
1615 && validate_name(CHILD(tree, 2), NULL));
1616 else
1617 ok = validate_numnodes(tree, 3, "import_as_name");
1618 }
1619 return ok;
1620}
1621
1622
1623/* dotted_as_name: dotted_name [NAME NAME]
1624 */
1625static int
1626validate_dotted_as_name(node *tree)
1627{
1628 int nch = NCH(tree);
1629 int res = validate_ntype(tree, dotted_as_name);
1630
1631 if (res) {
1632 if (nch == 1)
1633 res = validate_ntype(CHILD(tree, 0), dotted_name);
1634 else if (nch == 3)
1635 res = (validate_ntype(CHILD(tree, 0), dotted_name)
1636 && validate_name(CHILD(tree, 1), "as")
1637 && validate_name(CHILD(tree, 2), NULL));
1638 else {
1639 res = 0;
1640 err_string("Illegal number of children for dotted_as_name.");
1641 }
1642 }
1643 return res;
1644}
1645
1646
Guido van Rossum3d602e31996-07-21 02:33:56 +00001647/* import_stmt:
1648 *
Fred Drakecff283c2000-08-21 22:24:43 +00001649 * 'import' dotted_as_name (',' dotted_as_name)*
1650 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001651 */
Guido van Rossum47478871996-08-21 14:32:37 +00001652static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001653validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001654{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001655 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001656 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001657 && (nch >= 2) && is_even(nch)
Fred Drakecff283c2000-08-21 22:24:43 +00001658 && validate_ntype(CHILD(tree, 0), NAME));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001659
1660 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001661 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001662
Fred Drakecff283c2000-08-21 22:24:43 +00001663 res = validate_dotted_as_name(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00001664 for (j = 2; res && (j < nch); j += 2)
1665 res = (validate_comma(CHILD(tree, j))
1666 && validate_ntype(CHILD(tree, j + 1), dotted_name));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001667 }
Fred Drakecff283c2000-08-21 22:24:43 +00001668 else if (res && (res = validate_name(CHILD(tree, 0), "from"))) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001669 res = ((nch >= 4) && is_even(nch)
Fred Drakecff283c2000-08-21 22:24:43 +00001670 && validate_name(CHILD(tree, 2), "import")
1671 && validate_dotted_as_name(CHILD(tree, 1)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001672 if (nch == 4) {
Fred Drakecff283c2000-08-21 22:24:43 +00001673 if (TYPE(CHILD(tree, 3)) == import_as_name)
1674 res = validate_import_as_name(CHILD(tree, 3));
1675 else
1676 res = validate_star(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001677 }
1678 else {
Fred Drakecff283c2000-08-21 22:24:43 +00001679 /* 'from' dotted_name 'import' import_as_name
1680 * (',' import_as_name)+
1681 */
Fred Drakeff9ea482000-04-19 13:54:15 +00001682 int j;
Fred Drakecff283c2000-08-21 22:24:43 +00001683 res = validate_import_as_name(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001684 for (j = 4; res && (j < nch); j += 2)
1685 res = (validate_comma(CHILD(tree, j))
Fred Drakecff283c2000-08-21 22:24:43 +00001686 && validate_import_as_name(CHILD(tree, j + 1)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001687 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001688 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001689 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001690 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001691
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001692 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001693}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001694
1695
Guido van Rossum47478871996-08-21 14:32:37 +00001696static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001697validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001698{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001699 int j;
1700 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001701 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001702 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001703
Guido van Rossum3d602e31996-07-21 02:33:56 +00001704 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001705 res = (validate_name(CHILD(tree, 0), "global")
1706 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001707 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001708 res = (validate_comma(CHILD(tree, j))
1709 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001710
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001711 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001712}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001713
1714
Guido van Rossum3d602e31996-07-21 02:33:56 +00001715/* exec_stmt:
1716 *
1717 * 'exec' expr ['in' test [',' test]]
1718 */
Guido van Rossum47478871996-08-21 14:32:37 +00001719static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001720validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001721{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001722 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001723 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001724 && ((nch == 2) || (nch == 4) || (nch == 6))
1725 && validate_name(CHILD(tree, 0), "exec")
1726 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001727
Guido van Rossum3d602e31996-07-21 02:33:56 +00001728 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001729 err_string("Illegal exec statement.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001730 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001731 res = (validate_name(CHILD(tree, 2), "in")
1732 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001733 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001734 res = (validate_comma(CHILD(tree, 4))
1735 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001736
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001737 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001738}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001739
1740
Guido van Rossum925e5471997-04-02 05:32:13 +00001741/* assert_stmt:
1742 *
1743 * 'assert' test [',' test]
1744 */
1745static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001746validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001747{
1748 int nch = NCH(tree);
1749 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001750 && ((nch == 2) || (nch == 4))
1751 && (validate_name(CHILD(tree, 0), "__assert__") ||
1752 validate_name(CHILD(tree, 0), "assert"))
1753 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001754
1755 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001756 err_string("Illegal assert statement.");
Guido van Rossum925e5471997-04-02 05:32:13 +00001757 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001758 res = (validate_comma(CHILD(tree, 2))
1759 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001760
1761 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001762}
Guido van Rossum925e5471997-04-02 05:32:13 +00001763
1764
Guido van Rossum47478871996-08-21 14:32:37 +00001765static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001766validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001767{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001768 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001769 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001770 && ((nch == 4) || (nch == 7))
1771 && validate_name(CHILD(tree, 0), "while")
1772 && validate_test(CHILD(tree, 1))
1773 && validate_colon(CHILD(tree, 2))
1774 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001775
Guido van Rossum3d602e31996-07-21 02:33:56 +00001776 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001777 res = (validate_name(CHILD(tree, 4), "else")
1778 && validate_colon(CHILD(tree, 5))
1779 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001780
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001781 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001782}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001783
1784
Guido van Rossum47478871996-08-21 14:32:37 +00001785static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001786validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001787{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001788 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001789 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001790 && ((nch == 6) || (nch == 9))
1791 && validate_name(CHILD(tree, 0), "for")
1792 && validate_exprlist(CHILD(tree, 1))
1793 && validate_name(CHILD(tree, 2), "in")
1794 && validate_testlist(CHILD(tree, 3))
1795 && validate_colon(CHILD(tree, 4))
1796 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001797
Guido van Rossum3d602e31996-07-21 02:33:56 +00001798 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001799 res = (validate_name(CHILD(tree, 6), "else")
1800 && validate_colon(CHILD(tree, 7))
1801 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001802
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001803 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001804}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001805
1806
Guido van Rossum3d602e31996-07-21 02:33:56 +00001807/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001808 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001809 * | 'try' ':' suite 'finally' ':' suite
1810 *
1811 */
Guido van Rossum47478871996-08-21 14:32:37 +00001812static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001813validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001814{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001815 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001816 int pos = 3;
1817 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001818 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001819
1820 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001821 res = (validate_name(CHILD(tree, 0), "try")
1822 && validate_colon(CHILD(tree, 1))
1823 && validate_suite(CHILD(tree, 2))
1824 && validate_colon(CHILD(tree, nch - 2))
1825 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001826 else {
Fred Drake22269b52000-07-03 18:07:43 +00001827 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001828 char buffer[60];
1829 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1830 name = STR(CHILD(tree, nch - 3));
1831 (void) sprintf(buffer,
1832 "Illegal number of children for try/%s node.", name);
1833 err_string(buffer);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001834 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001835 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001836 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001837 res = (validate_except_clause(CHILD(tree, pos))
1838 && validate_colon(CHILD(tree, pos + 1))
1839 && validate_suite(CHILD(tree, pos + 2)));
1840 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001841 }
1842 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001843 res = validate_ntype(CHILD(tree, pos), NAME);
1844 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1845 res = (validate_numnodes(tree, 6, "try/finally")
1846 && validate_colon(CHILD(tree, 4))
1847 && validate_suite(CHILD(tree, 5)));
1848 else if (res) {
1849 if (nch == (pos + 3)) {
1850 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1851 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1852 if (!res)
1853 err_string("Illegal trailing triple in try statement.");
1854 }
1855 else if (nch == (pos + 6)) {
1856 res = (validate_name(CHILD(tree, pos), "except")
1857 && validate_colon(CHILD(tree, pos + 1))
1858 && validate_suite(CHILD(tree, pos + 2))
1859 && validate_name(CHILD(tree, pos + 3), "else"));
1860 }
1861 else
1862 res = validate_numnodes(tree, pos + 3, "try/except");
1863 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001864 }
1865 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001866}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001867
1868
Guido van Rossum47478871996-08-21 14:32:37 +00001869static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001870validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001871{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001872 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001873 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001874 && ((nch == 1) || (nch == 2) || (nch == 4))
1875 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001876
Guido van Rossum3d602e31996-07-21 02:33:56 +00001877 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001878 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001879 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001880 res = (validate_comma(CHILD(tree, 2))
1881 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001882
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001883 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001884}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001885
1886
Guido van Rossum47478871996-08-21 14:32:37 +00001887static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001888validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001889{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001890 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001891 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001892
Guido van Rossum3d602e31996-07-21 02:33:56 +00001893 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001894 res = ((nch == 1)
1895 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001896 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001897 int pos;
1898 res = validate_and_test(CHILD(tree, 0));
1899 for (pos = 1; res && (pos < nch); pos += 2)
1900 res = (validate_name(CHILD(tree, pos), "or")
1901 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001902 }
1903 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001904}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001905
1906
Guido van Rossum47478871996-08-21 14:32:37 +00001907static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001908validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001909{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001910 int pos;
1911 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001912 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001913 && is_odd(nch)
1914 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001915
Guido van Rossum3d602e31996-07-21 02:33:56 +00001916 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001917 res = (validate_name(CHILD(tree, pos), "and")
1918 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001919
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001920 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001921}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001922
1923
Guido van Rossum47478871996-08-21 14:32:37 +00001924static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001925validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001926{
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, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001929
Guido van Rossum3d602e31996-07-21 02:33:56 +00001930 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001931 if (nch == 2)
1932 res = (validate_name(CHILD(tree, 0), "not")
1933 && validate_not_test(CHILD(tree, 1)));
1934 else if (nch == 1)
1935 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001936 }
1937 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001938}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001939
1940
Guido van Rossum47478871996-08-21 14:32:37 +00001941static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001942validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001943{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001944 int pos;
1945 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001946 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001947 && is_odd(nch)
1948 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001949
Guido van Rossum3d602e31996-07-21 02:33:56 +00001950 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001951 res = (validate_comp_op(CHILD(tree, pos))
1952 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001953
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001954 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001955}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001956
1957
Guido van Rossum47478871996-08-21 14:32:37 +00001958static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001959validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001960{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001961 int res = 0;
1962 int nch = NCH(tree);
1963
Guido van Rossum3d602e31996-07-21 02:33:56 +00001964 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001965 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001966 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001967 /*
1968 * Only child will be a terminal with a well-defined symbolic name
1969 * or a NAME with a string of either 'is' or 'in'
1970 */
1971 tree = CHILD(tree, 0);
1972 switch (TYPE(tree)) {
1973 case LESS:
1974 case GREATER:
1975 case EQEQUAL:
1976 case EQUAL:
1977 case LESSEQUAL:
1978 case GREATEREQUAL:
1979 case NOTEQUAL:
1980 res = 1;
1981 break;
1982 case NAME:
1983 res = ((strcmp(STR(tree), "in") == 0)
1984 || (strcmp(STR(tree), "is") == 0));
1985 if (!res) {
1986 char buff[128];
1987 (void) sprintf(buff, "Illegal operator: '%s'.", STR(tree));
1988 err_string(buff);
1989 }
1990 break;
1991 default:
1992 err_string("Illegal comparison operator type.");
1993 break;
1994 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001995 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00001996 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001997 res = (validate_ntype(CHILD(tree, 0), NAME)
1998 && validate_ntype(CHILD(tree, 1), NAME)
1999 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2000 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2001 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2002 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2003 if (!res && !PyErr_Occurred())
2004 err_string("Unknown comparison operator.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002005 }
2006 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002007}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002008
2009
Guido van Rossum47478871996-08-21 14:32:37 +00002010static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002011validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002012{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002013 int j;
2014 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002015 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002016 && is_odd(nch)
2017 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002018
Guido van Rossum3d602e31996-07-21 02:33:56 +00002019 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002020 res = (validate_xor_expr(CHILD(tree, j))
2021 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002022
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002023 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002024}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002025
2026
Guido van Rossum47478871996-08-21 14:32:37 +00002027static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002028validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002029{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002030 int j;
2031 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002032 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002033 && is_odd(nch)
2034 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002035
Guido van Rossum3d602e31996-07-21 02:33:56 +00002036 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002037 res = (validate_circumflex(CHILD(tree, j - 1))
2038 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002039
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002040 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002041}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002042
2043
Guido van Rossum47478871996-08-21 14:32:37 +00002044static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002045validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002046{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002047 int pos;
2048 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002049 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002050 && is_odd(nch)
2051 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002052
Guido van Rossum3d602e31996-07-21 02:33:56 +00002053 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002054 res = (validate_ampersand(CHILD(tree, pos))
2055 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002056
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002057 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002058}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002059
2060
2061static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002062validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002063 {
2064 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002065 int nch = NCH(tree);
2066 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002067 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002068
Guido van Rossum3d602e31996-07-21 02:33:56 +00002069 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002070 if (TYPE(CHILD(tree, pos)) != op1)
2071 res = validate_ntype(CHILD(tree, pos), op2);
2072 if (res)
2073 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002074 }
2075 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002076}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002077
2078
Guido van Rossum47478871996-08-21 14:32:37 +00002079static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002080validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002081{
2082 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002083 && validate_chain_two_ops(tree, validate_arith_expr,
2084 LEFTSHIFT, RIGHTSHIFT));
2085}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002086
2087
Guido van Rossum47478871996-08-21 14:32:37 +00002088static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002089validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002090{
2091 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002092 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2093}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002094
2095
Guido van Rossum47478871996-08-21 14:32:37 +00002096static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002097validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002098{
2099 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002100 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002101 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002102 && is_odd(nch)
2103 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002104
Guido van Rossum3d602e31996-07-21 02:33:56 +00002105 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002106 res = (((TYPE(CHILD(tree, pos)) == STAR)
2107 || (TYPE(CHILD(tree, pos)) == SLASH)
2108 || (TYPE(CHILD(tree, pos)) == PERCENT))
2109 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002110
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002111 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002112}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002113
2114
Guido van Rossum3d602e31996-07-21 02:33:56 +00002115/* factor:
2116 *
2117 * factor: ('+'|'-'|'~') factor | power
2118 */
Guido van Rossum47478871996-08-21 14:32:37 +00002119static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002120validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002121{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002122 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002123 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002124 && (((nch == 2)
2125 && ((TYPE(CHILD(tree, 0)) == PLUS)
2126 || (TYPE(CHILD(tree, 0)) == MINUS)
2127 || (TYPE(CHILD(tree, 0)) == TILDE))
2128 && validate_factor(CHILD(tree, 1)))
2129 || ((nch == 1)
2130 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002131 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002132}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002133
2134
Guido van Rossum3d602e31996-07-21 02:33:56 +00002135/* power:
2136 *
2137 * power: atom trailer* ('**' factor)*
2138 */
Guido van Rossum47478871996-08-21 14:32:37 +00002139static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002140validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002141{
2142 int pos = 1;
2143 int nch = NCH(tree);
2144 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002145 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002146
2147 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002148 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002149 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002150 if (!is_even(nch - pos)) {
2151 err_string("Illegal number of nodes for 'power'.");
2152 return (0);
2153 }
2154 for ( ; res && (pos < (nch - 1)); pos += 2)
2155 res = (validate_doublestar(CHILD(tree, pos))
2156 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002157 }
2158 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002159}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002160
2161
Guido van Rossum47478871996-08-21 14:32:37 +00002162static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002163validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002164{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002165 int pos;
2166 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002167 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002168
Fred Drakecff283c2000-08-21 22:24:43 +00002169 if (res && nch < 1)
2170 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002171 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002172 switch (TYPE(CHILD(tree, 0))) {
2173 case LPAR:
2174 res = ((nch <= 3)
2175 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002176
Fred Drakeff9ea482000-04-19 13:54:15 +00002177 if (res && (nch == 3))
2178 res = validate_testlist(CHILD(tree, 1));
2179 break;
2180 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002181 if (nch == 2)
2182 res = validate_ntype(CHILD(tree, 1), RSQB);
2183 else if (nch == 3)
2184 res = (validate_listmaker(CHILD(tree, 1))
2185 && validate_ntype(CHILD(tree, 2), RSQB));
2186 else {
2187 res = 0;
2188 err_string("illegal list display atom");
2189 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002190 break;
2191 case LBRACE:
2192 res = ((nch <= 3)
2193 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002194
Fred Drakeff9ea482000-04-19 13:54:15 +00002195 if (res && (nch == 3))
2196 res = validate_dictmaker(CHILD(tree, 1));
2197 break;
2198 case BACKQUOTE:
2199 res = ((nch == 3)
2200 && validate_testlist(CHILD(tree, 1))
2201 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2202 break;
2203 case NAME:
2204 case NUMBER:
2205 res = (nch == 1);
2206 break;
2207 case STRING:
2208 for (pos = 1; res && (pos < nch); ++pos)
2209 res = validate_ntype(CHILD(tree, pos), STRING);
2210 break;
2211 default:
2212 res = 0;
2213 break;
2214 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002215 }
2216 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002217}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002218
2219
Fred Drake85bf3bb2000-08-23 15:35:26 +00002220/* listmaker:
2221 * test ( list_for | (',' test)* [','] )
2222 */
Fred Drakecff283c2000-08-21 22:24:43 +00002223static int
2224validate_listmaker(node *tree)
2225{
2226 int nch = NCH(tree);
2227 int ok = nch;
2228
2229 if (nch == 0)
2230 err_string("missing child nodes of listmaker");
2231 else
2232 ok = validate_test(CHILD(tree, 0));
2233
2234 /*
2235 * list_iter | (',' test)* [',']
2236 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002237 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2238 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002239 else {
2240 /* (',' test)* [','] */
2241 int i = 1;
2242 while (ok && nch - i >= 2) {
2243 ok = (validate_comma(CHILD(tree, i))
2244 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002245 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002246 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002247 if (ok && i == nch-1)
2248 ok = validate_comma(CHILD(tree, i));
2249 else if (i != nch) {
2250 ok = 0;
2251 err_string("illegal trailing nodes for listmaker");
2252 }
Fred Drakecff283c2000-08-21 22:24:43 +00002253 }
2254 return ok;
2255}
2256
2257
Guido van Rossum3d602e31996-07-21 02:33:56 +00002258/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002259 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002260 *
2261 */
Guido van Rossum47478871996-08-21 14:32:37 +00002262static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002263validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002264{
2265 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002266 && validate_numnodes(tree, 5, "funcdef")
2267 && validate_name(CHILD(tree, 0), "def")
2268 && validate_ntype(CHILD(tree, 1), NAME)
2269 && validate_colon(CHILD(tree, 3))
2270 && validate_parameters(CHILD(tree, 2))
2271 && validate_suite(CHILD(tree, 4)));
2272}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002273
2274
Guido van Rossum47478871996-08-21 14:32:37 +00002275static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002276validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002277{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002278 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002279 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002280 && ((nch == 3) || (nch == 4))
2281 && validate_name(CHILD(tree, 0), "lambda")
2282 && validate_colon(CHILD(tree, nch - 2))
2283 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002284
Guido van Rossum3d602e31996-07-21 02:33:56 +00002285 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002286 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002287 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002288 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002289
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002290 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002291}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002292
2293
Guido van Rossum3d602e31996-07-21 02:33:56 +00002294/* arglist:
2295 *
Fred Drakecff283c2000-08-21 22:24:43 +00002296 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002297 */
Guido van Rossum47478871996-08-21 14:32:37 +00002298static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002299validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002300{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002301 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002302 int i = 0;
2303 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002304
2305 if (nch <= 0)
2306 /* raise the right error from having an invalid number of children */
2307 return validate_numnodes(tree, nch + 1, "arglist");
2308
Fred Drakecff283c2000-08-21 22:24:43 +00002309 while (ok && nch-i >= 2) {
2310 /* skip leading (argument ',') */
2311 ok = (validate_argument(CHILD(tree, i))
2312 && validate_comma(CHILD(tree, i+1)));
2313 if (ok)
2314 i += 2;
2315 else
2316 PyErr_Clear();
2317 }
2318 ok = 1;
2319 if (nch-i > 0) {
2320 /*
2321 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002322 */
Fred Drakecff283c2000-08-21 22:24:43 +00002323 int sym = TYPE(CHILD(tree, i));
2324
2325 if (sym == argument) {
2326 ok = validate_argument(CHILD(tree, i));
2327 if (ok && i+1 != nch) {
2328 err_string("illegal arglist specification"
2329 " (extra stuff on end)");
2330 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002331 }
Fred Drakecff283c2000-08-21 22:24:43 +00002332 }
2333 else if (sym == STAR) {
2334 ok = validate_star(CHILD(tree, i));
2335 if (ok && (nch-i == 2))
2336 ok = validate_test(CHILD(tree, i+1));
2337 else if (ok && (nch-i == 5))
2338 ok = (validate_test(CHILD(tree, i+1))
2339 && validate_comma(CHILD(tree, i+2))
2340 && validate_doublestar(CHILD(tree, i+3))
2341 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002342 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002343 err_string("illegal use of '*' in arglist");
2344 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002345 }
2346 }
Fred Drakecff283c2000-08-21 22:24:43 +00002347 else if (sym == DOUBLESTAR) {
2348 if (nch-i == 2)
2349 ok = (validate_doublestar(CHILD(tree, i))
2350 && validate_test(CHILD(tree, i+1)));
2351 else {
2352 err_string("illegal use of '**' in arglist");
2353 ok = 0;
2354 }
2355 }
2356 else {
2357 err_string("illegal arglist specification");
2358 ok = 0;
2359 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002360 }
2361 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002362}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002363
2364
2365
2366/* argument:
2367 *
2368 * [test '='] test
2369 */
Guido van Rossum47478871996-08-21 14:32:37 +00002370static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002371validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002372{
2373 int nch = NCH(tree);
2374 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002375 && ((nch == 1) || (nch == 3))
2376 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002377
2378 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002379 res = (validate_equal(CHILD(tree, 1))
2380 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002381
2382 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002383}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002384
2385
2386
2387/* trailer:
2388 *
Guido van Rossum47478871996-08-21 14:32:37 +00002389 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002390 */
Guido van Rossum47478871996-08-21 14:32:37 +00002391static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002392validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002393{
2394 int nch = NCH(tree);
2395 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002396
2397 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002398 switch (TYPE(CHILD(tree, 0))) {
2399 case LPAR:
2400 res = validate_rparen(CHILD(tree, nch - 1));
2401 if (res && (nch == 3))
2402 res = validate_arglist(CHILD(tree, 1));
2403 break;
2404 case LSQB:
2405 res = (validate_numnodes(tree, 3, "trailer")
2406 && validate_subscriptlist(CHILD(tree, 1))
2407 && validate_ntype(CHILD(tree, 2), RSQB));
2408 break;
2409 case DOT:
2410 res = (validate_numnodes(tree, 2, "trailer")
2411 && validate_ntype(CHILD(tree, 1), NAME));
2412 break;
2413 default:
2414 res = 0;
2415 break;
2416 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002417 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002418 else {
2419 (void) validate_numnodes(tree, 2, "trailer");
2420 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002421 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002422}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002423
2424
Guido van Rossum47478871996-08-21 14:32:37 +00002425/* subscriptlist:
2426 *
2427 * subscript (',' subscript)* [',']
2428 */
2429static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002430validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002431{
2432 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002433 validate_subscript, "subscriptlist"));
2434}
Guido van Rossum47478871996-08-21 14:32:37 +00002435
2436
Guido van Rossum3d602e31996-07-21 02:33:56 +00002437/* subscript:
2438 *
Guido van Rossum47478871996-08-21 14:32:37 +00002439 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002440 */
Guido van Rossum47478871996-08-21 14:32:37 +00002441static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002442validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002443{
Guido van Rossum47478871996-08-21 14:32:37 +00002444 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002445 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002446 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002447
Guido van Rossum47478871996-08-21 14:32:37 +00002448 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002449 if (!PyErr_Occurred())
2450 err_string("invalid number of arguments for subscript node");
2451 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002452 }
Guido van Rossum47478871996-08-21 14:32:37 +00002453 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002454 /* take care of ('.' '.' '.') possibility */
2455 return (validate_numnodes(tree, 3, "subscript")
2456 && validate_dot(CHILD(tree, 0))
2457 && validate_dot(CHILD(tree, 1))
2458 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002459 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002460 if (TYPE(CHILD(tree, 0)) == test)
2461 res = validate_test(CHILD(tree, 0));
2462 else
2463 res = validate_colon(CHILD(tree, 0));
2464 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002465 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002466 /* Must be [test] ':' [test] [sliceop],
2467 * but at least one of the optional components will
2468 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002469 */
2470 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002471 res = validate_test(CHILD(tree, 0));
2472 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002473 }
2474 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002475 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002476 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002477 int rem = nch - ++offset;
2478 if (rem) {
2479 if (TYPE(CHILD(tree, offset)) == test) {
2480 res = validate_test(CHILD(tree, offset));
2481 ++offset;
2482 --rem;
2483 }
2484 if (res && rem)
2485 res = validate_sliceop(CHILD(tree, offset));
2486 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002487 }
2488 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002489}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002490
2491
Guido van Rossum47478871996-08-21 14:32:37 +00002492static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002493validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002494{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002495 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002496 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002497 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002498 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002499 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002500 }
Guido van Rossum47478871996-08-21 14:32:37 +00002501 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002502 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002503 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002504 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002505
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002506 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002507}
Guido van Rossum47478871996-08-21 14:32:37 +00002508
2509
2510static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002511validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002512{
2513 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002514 validate_expr, "exprlist"));
2515}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002516
2517
Guido van Rossum47478871996-08-21 14:32:37 +00002518static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002519validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002520{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002521 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002522 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002523 && (nch >= 3)
2524 && validate_test(CHILD(tree, 0))
2525 && validate_colon(CHILD(tree, 1))
2526 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002527
Guido van Rossum3d602e31996-07-21 02:33:56 +00002528 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002529 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002530 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002531 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002532
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002533 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002534 int pos = 3;
2535 /* ( ',' test ':' test )* */
2536 while (res && (pos < nch)) {
2537 res = (validate_comma(CHILD(tree, pos))
2538 && validate_test(CHILD(tree, pos + 1))
2539 && validate_colon(CHILD(tree, pos + 2))
2540 && validate_test(CHILD(tree, pos + 3)));
2541 pos += 4;
2542 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002543 }
2544 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002545}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002546
2547
Guido van Rossum47478871996-08-21 14:32:37 +00002548static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002549validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002550{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002551 int pos;
2552 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002553 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002554 && (nch >= 2)
2555 && validate_testlist(CHILD(tree, 0))
2556 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002557
Guido van Rossum3d602e31996-07-21 02:33:56 +00002558 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002559 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002560
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002561 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002562}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002563
2564
Guido van Rossum47478871996-08-21 14:32:37 +00002565static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002566validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002567{
Fred Drakeff9ea482000-04-19 13:54:15 +00002568 int nch = 0; /* num. children on current node */
2569 int res = 1; /* result value */
2570 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002571
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002572 while (res & (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002573 nch = NCH(tree);
2574 next = 0;
2575 switch (TYPE(tree)) {
2576 /*
2577 * Definition nodes.
2578 */
2579 case funcdef:
2580 res = validate_funcdef(tree);
2581 break;
2582 case classdef:
2583 res = validate_class(tree);
2584 break;
2585 /*
2586 * "Trivial" parse tree nodes.
2587 * (Why did I call these trivial?)
2588 */
2589 case stmt:
2590 res = validate_stmt(tree);
2591 break;
2592 case small_stmt:
2593 /*
2594 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
2595 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2596 */
2597 res = validate_small_stmt(tree);
2598 break;
2599 case flow_stmt:
2600 res = (validate_numnodes(tree, 1, "flow_stmt")
2601 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2602 || (TYPE(CHILD(tree, 0)) == continue_stmt)
2603 || (TYPE(CHILD(tree, 0)) == return_stmt)
2604 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2605 if (res)
2606 next = CHILD(tree, 0);
2607 else if (nch == 1)
2608 err_string("Illegal flow_stmt type.");
2609 break;
2610 /*
2611 * Compound statements.
2612 */
2613 case simple_stmt:
2614 res = validate_simple_stmt(tree);
2615 break;
2616 case compound_stmt:
2617 res = validate_compound_stmt(tree);
2618 break;
2619 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002620 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002621 */
2622 case expr_stmt:
2623 res = validate_expr_stmt(tree);
2624 break;
2625 case print_stmt:
2626 res = validate_print_stmt(tree);
2627 break;
2628 case del_stmt:
2629 res = validate_del_stmt(tree);
2630 break;
2631 case pass_stmt:
2632 res = (validate_numnodes(tree, 1, "pass")
2633 && validate_name(CHILD(tree, 0), "pass"));
2634 break;
2635 case break_stmt:
2636 res = (validate_numnodes(tree, 1, "break")
2637 && validate_name(CHILD(tree, 0), "break"));
2638 break;
2639 case continue_stmt:
2640 res = (validate_numnodes(tree, 1, "continue")
2641 && validate_name(CHILD(tree, 0), "continue"));
2642 break;
2643 case return_stmt:
2644 res = validate_return_stmt(tree);
2645 break;
2646 case raise_stmt:
2647 res = validate_raise_stmt(tree);
2648 break;
2649 case import_stmt:
2650 res = validate_import_stmt(tree);
2651 break;
2652 case global_stmt:
2653 res = validate_global_stmt(tree);
2654 break;
2655 case exec_stmt:
2656 res = validate_exec_stmt(tree);
2657 break;
2658 case assert_stmt:
2659 res = validate_assert_stmt(tree);
2660 break;
2661 case if_stmt:
2662 res = validate_if(tree);
2663 break;
2664 case while_stmt:
2665 res = validate_while(tree);
2666 break;
2667 case for_stmt:
2668 res = validate_for(tree);
2669 break;
2670 case try_stmt:
2671 res = validate_try(tree);
2672 break;
2673 case suite:
2674 res = validate_suite(tree);
2675 break;
2676 /*
2677 * Expression nodes.
2678 */
2679 case testlist:
2680 res = validate_testlist(tree);
2681 break;
2682 case test:
2683 res = validate_test(tree);
2684 break;
2685 case and_test:
2686 res = validate_and_test(tree);
2687 break;
2688 case not_test:
2689 res = validate_not_test(tree);
2690 break;
2691 case comparison:
2692 res = validate_comparison(tree);
2693 break;
2694 case exprlist:
2695 res = validate_exprlist(tree);
2696 break;
2697 case comp_op:
2698 res = validate_comp_op(tree);
2699 break;
2700 case expr:
2701 res = validate_expr(tree);
2702 break;
2703 case xor_expr:
2704 res = validate_xor_expr(tree);
2705 break;
2706 case and_expr:
2707 res = validate_and_expr(tree);
2708 break;
2709 case shift_expr:
2710 res = validate_shift_expr(tree);
2711 break;
2712 case arith_expr:
2713 res = validate_arith_expr(tree);
2714 break;
2715 case term:
2716 res = validate_term(tree);
2717 break;
2718 case factor:
2719 res = validate_factor(tree);
2720 break;
2721 case power:
2722 res = validate_power(tree);
2723 break;
2724 case atom:
2725 res = validate_atom(tree);
2726 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002727
Fred Drakeff9ea482000-04-19 13:54:15 +00002728 default:
2729 /* Hopefully never reached! */
2730 err_string("Unrecogniged node type.");
2731 res = 0;
2732 break;
2733 }
2734 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002735 }
2736 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002737}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002738
2739
Guido van Rossum47478871996-08-21 14:32:37 +00002740static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002741validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002742{
2743 int res = validate_eval_input(tree);
2744
2745 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002746 err_string("Could not validate expression tuple.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002747
2748 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002749}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002750
2751
Guido van Rossum3d602e31996-07-21 02:33:56 +00002752/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002753 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002754 */
Guido van Rossum47478871996-08-21 14:32:37 +00002755static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002756validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002757{
2758 int j = 0;
2759 int nch = NCH(tree) - 1;
2760 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002761 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002762
Guido van Rossum3d602e31996-07-21 02:33:56 +00002763 for ( ; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002764 if (TYPE(CHILD(tree, j)) == stmt)
2765 res = validate_stmt(CHILD(tree, j));
2766 else
2767 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002768 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002769 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002770 * user. Hopefully, this won't be needed. If a user reports getting
2771 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002772 */
2773 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002774 err_string("VALIDATION FAILURE: report this to the maintainer!.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002775
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002776 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002777}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002778
2779
Fred Drake43f8f9b1998-04-13 16:25:46 +00002780static PyObject*
2781pickle_constructor = NULL;
2782
2783
2784static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002785parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002786{
Fred Drake268397f1998-04-29 14:16:32 +00002787 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002788 PyObject *result = NULL;
2789 PyObject *ast = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002790 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002791
2792 if (PyArg_ParseTuple(args, "O!:_pickler", &PyAST_Type, &ast)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002793 PyObject *newargs;
2794 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002795
Fred Drake2a6875e1999-09-20 22:32:18 +00002796 if ((empty_dict = PyDict_New()) == NULL)
2797 goto finally;
2798 if ((newargs = Py_BuildValue("Oi", ast, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002799 goto finally;
2800 tuple = parser_ast2tuple((PyAST_Object*)NULL, newargs, empty_dict);
2801 if (tuple != NULL) {
2802 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2803 Py_DECREF(tuple);
2804 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002805 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002806 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002807 }
2808 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002809 Py_XDECREF(empty_dict);
2810
Fred Drake43f8f9b1998-04-13 16:25:46 +00002811 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002812}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002813
2814
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002815/* Functions exported by this module. Most of this should probably
2816 * be converted into an AST object with methods, but that is better
2817 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002818 * We'd really have to write a wrapper around it all anyway to allow
2819 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002820 */
2821static PyMethodDef parser_functions[] = {
Fred Drakeff9ea482000-04-19 13:54:15 +00002822 {"ast2tuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
2823 "Creates a tuple-tree representation of an AST."},
2824 {"ast2list", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
2825 "Creates a list-tree representation of an AST."},
2826 {"compileast", (PyCFunction)parser_compileast, PUBLIC_METHOD_TYPE,
2827 "Compiles an AST object into a code object."},
2828 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
2829 "Creates an AST object from an expression."},
2830 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
2831 "Determines if an AST object was created from an expression."},
2832 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
2833 "Determines if an AST object was created from a suite."},
2834 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
2835 "Creates an AST object from a suite."},
2836 {"sequence2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
2837 "Creates an AST object from a tree representation."},
2838 {"tuple2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
2839 "Creates an AST object from a tree representation."},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002840
Fred Drake43f8f9b1998-04-13 16:25:46 +00002841 /* private stuff: support pickle module */
Fred Drakeff9ea482000-04-19 13:54:15 +00002842 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Fred Drake43f8f9b1998-04-13 16:25:46 +00002843 "Returns the pickle magic to allow ast objects to be pickled."},
2844
Fred Drake268397f1998-04-29 14:16:32 +00002845 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002846 };
2847
2848
Tim Peters6d7c4422000-08-26 07:38:06 +00002849DL_EXPORT(void) initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00002850
Guido van Rossum3886bb61998-12-04 18:50:17 +00002851DL_EXPORT(void)
Thomas Wouters5c669862000-07-24 15:49:08 +00002852initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00002853{
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002854 PyObject* module;
2855 PyObject* dict;
Fred Drakeff9ea482000-04-19 13:54:15 +00002856
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002857 PyAST_Type.ob_type = &PyType_Type;
2858 module = Py_InitModule("parser", parser_functions);
2859 dict = PyModule_GetDict(module);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002860
Fred Drake7a15ba51999-09-09 14:21:52 +00002861 if (parser_error == 0)
2862 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002863
2864 if ((parser_error == 0)
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +00002865 || (PyDict_SetItemString(dict, "ParserError", parser_error) != 0))
2866 {
2867 /* caller will check PyErr_Occurred() */
2868 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002869 }
2870 /*
2871 * Nice to have, but don't cry if we fail.
2872 */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002873 Py_INCREF(&PyAST_Type);
2874 PyDict_SetItemString(dict, "ASTType", (PyObject*)&PyAST_Type);
2875
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002876 PyDict_SetItemString(dict, "__copyright__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002877 PyString_FromString(parser_copyright_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002878 PyDict_SetItemString(dict, "__doc__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002879 PyString_FromString(parser_doc_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002880 PyDict_SetItemString(dict, "__version__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002881 PyString_FromString(parser_version_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002882
Fred Drake43f8f9b1998-04-13 16:25:46 +00002883 /* register to support pickling */
2884 module = PyImport_ImportModule("copy_reg");
2885 if (module != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002886 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002887
Fred Drakeff9ea482000-04-19 13:54:15 +00002888 func = PyObject_GetAttrString(module, "pickle");
2889 pickle_constructor = PyDict_GetItemString(dict, "sequence2ast");
2890 pickler = PyDict_GetItemString(dict, "_pickler");
2891 Py_XINCREF(pickle_constructor);
2892 if ((func != NULL) && (pickle_constructor != NULL)
2893 && (pickler != NULL)) {
2894 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002895
Fred Drakeff9ea482000-04-19 13:54:15 +00002896 res = PyObject_CallFunction(
2897 func, "OOO", &PyAST_Type, pickler, pickle_constructor);
2898 Py_XDECREF(res);
2899 }
2900 Py_XDECREF(func);
2901 Py_DECREF(module);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002902 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002903}