blob: 57a304602045fe0287d9f3cb9eccf90d09c19660 [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
Guido van Rossum3d602e31996-07-21 02:33:56 +00001470 for (j = 1; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001471 res = (validate_equal(CHILD(tree, j))
1472 && validate_testlist(CHILD(tree, j + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001473
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001474 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001475}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001476
1477
Guido van Rossum3d602e31996-07-21 02:33:56 +00001478/* print_stmt:
1479 *
Fred Drakecff283c2000-08-21 22:24:43 +00001480 * 'print' ( [ test (',' test)* [','] ]
1481 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001482 */
Guido van Rossum47478871996-08-21 14:32:37 +00001483static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001484validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001485{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001486 int j;
1487 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001488 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001489 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001490 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001491
Fred Drakecff283c2000-08-21 22:24:43 +00001492 if (res && nch > 1) {
1493 int sym = TYPE(CHILD(tree, 1));
1494 int i = 1;
1495 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001496
Fred Drakecff283c2000-08-21 22:24:43 +00001497 if (sym == test)
1498 res = validate_test(CHILD(tree, i++));
1499 else {
1500 if (nch < 3)
1501 res = validate_numnodes(tree, 3, "print_stmt");
1502 else {
1503 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1504 && validate_test(CHILD(tree, i+1)));
1505 i += 2;
1506 allow_trailing_comma = 0;
1507 }
1508 }
1509 if (res) {
1510 /* ... (',' test)* [','] */
1511 while (res && i+2 <= nch) {
1512 res = (validate_comma(CHILD(tree, i))
1513 && validate_test(CHILD(tree, i+1)));
1514 allow_trailing_comma = 1;
1515 i += 2;
1516 }
1517 if (res && !allow_trailing_comma)
1518 res = validate_numnodes(tree, i, "print_stmt");
1519 else if (res && i < nch)
1520 res = validate_comma(CHILD(tree, i));
1521 }
1522 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001523 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001524}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001525
1526
Guido van Rossum47478871996-08-21 14:32:37 +00001527static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001528validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001529{
1530 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001531 && validate_name(CHILD(tree, 0), "del")
1532 && validate_exprlist(CHILD(tree, 1)));
1533}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001534
1535
Guido van Rossum47478871996-08-21 14:32:37 +00001536static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001537validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001538{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001539 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001540 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001541 && ((nch == 1) || (nch == 2))
1542 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001543
Guido van Rossum3d602e31996-07-21 02:33:56 +00001544 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001545 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001546
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001547 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001548}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001549
1550
Guido van Rossum47478871996-08-21 14:32:37 +00001551static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001552validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001553{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001554 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001555 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001556 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001557
Guido van Rossum3d602e31996-07-21 02:33:56 +00001558 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001559 res = validate_name(CHILD(tree, 0), "raise");
1560 if (res && (nch >= 2))
1561 res = validate_test(CHILD(tree, 1));
1562 if (res && nch > 2) {
1563 res = (validate_comma(CHILD(tree, 2))
1564 && validate_test(CHILD(tree, 3)));
1565 if (res && (nch > 4))
1566 res = (validate_comma(CHILD(tree, 4))
1567 && validate_test(CHILD(tree, 5)));
1568 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001569 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001570 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001571 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001572 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001573 res = (validate_comma(CHILD(tree, 2))
1574 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001575
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001576 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001577}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001578
1579
Fred Drakecff283c2000-08-21 22:24:43 +00001580static int
1581validate_import_as_name(node *tree)
1582{
1583 int nch = NCH(tree);
1584 int ok = validate_ntype(tree, import_as_name);
1585
1586 if (ok) {
1587 if (nch == 1)
1588 ok = validate_name(CHILD(tree, 0), NULL);
1589 else if (nch == 3)
1590 ok = (validate_name(CHILD(tree, 0), NULL)
1591 && validate_name(CHILD(tree, 1), "as")
1592 && validate_name(CHILD(tree, 2), NULL));
1593 else
1594 ok = validate_numnodes(tree, 3, "import_as_name");
1595 }
1596 return ok;
1597}
1598
1599
1600/* dotted_as_name: dotted_name [NAME NAME]
1601 */
1602static int
1603validate_dotted_as_name(node *tree)
1604{
1605 int nch = NCH(tree);
1606 int res = validate_ntype(tree, dotted_as_name);
1607
1608 if (res) {
1609 if (nch == 1)
1610 res = validate_ntype(CHILD(tree, 0), dotted_name);
1611 else if (nch == 3)
1612 res = (validate_ntype(CHILD(tree, 0), dotted_name)
1613 && validate_name(CHILD(tree, 1), "as")
1614 && validate_name(CHILD(tree, 2), NULL));
1615 else {
1616 res = 0;
1617 err_string("Illegal number of children for dotted_as_name.");
1618 }
1619 }
1620 return res;
1621}
1622
1623
Guido van Rossum3d602e31996-07-21 02:33:56 +00001624/* import_stmt:
1625 *
Fred Drakecff283c2000-08-21 22:24:43 +00001626 * 'import' dotted_as_name (',' dotted_as_name)*
1627 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001628 */
Guido van Rossum47478871996-08-21 14:32:37 +00001629static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001630validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001631{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001632 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001633 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001634 && (nch >= 2) && is_even(nch)
Fred Drakecff283c2000-08-21 22:24:43 +00001635 && validate_ntype(CHILD(tree, 0), NAME));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001636
1637 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001638 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001639
Fred Drakecff283c2000-08-21 22:24:43 +00001640 res = validate_dotted_as_name(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00001641 for (j = 2; res && (j < nch); j += 2)
1642 res = (validate_comma(CHILD(tree, j))
1643 && validate_ntype(CHILD(tree, j + 1), dotted_name));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001644 }
Fred Drakecff283c2000-08-21 22:24:43 +00001645 else if (res && (res = validate_name(CHILD(tree, 0), "from"))) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001646 res = ((nch >= 4) && is_even(nch)
Fred Drakecff283c2000-08-21 22:24:43 +00001647 && validate_name(CHILD(tree, 2), "import")
1648 && validate_dotted_as_name(CHILD(tree, 1)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001649 if (nch == 4) {
Fred Drakecff283c2000-08-21 22:24:43 +00001650 if (TYPE(CHILD(tree, 3)) == import_as_name)
1651 res = validate_import_as_name(CHILD(tree, 3));
1652 else
1653 res = validate_star(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001654 }
1655 else {
Fred Drakecff283c2000-08-21 22:24:43 +00001656 /* 'from' dotted_name 'import' import_as_name
1657 * (',' import_as_name)+
1658 */
Fred Drakeff9ea482000-04-19 13:54:15 +00001659 int j;
Fred Drakecff283c2000-08-21 22:24:43 +00001660 res = validate_import_as_name(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001661 for (j = 4; res && (j < nch); j += 2)
1662 res = (validate_comma(CHILD(tree, j))
Fred Drakecff283c2000-08-21 22:24:43 +00001663 && validate_import_as_name(CHILD(tree, j + 1)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001664 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001665 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001666 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001667 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001668
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001669 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001670}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001671
1672
Guido van Rossum47478871996-08-21 14:32:37 +00001673static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001674validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001675{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001676 int j;
1677 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001678 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001679 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001680
Guido van Rossum3d602e31996-07-21 02:33:56 +00001681 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001682 res = (validate_name(CHILD(tree, 0), "global")
1683 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001684 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001685 res = (validate_comma(CHILD(tree, j))
1686 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001687
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001688 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001689}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001690
1691
Guido van Rossum3d602e31996-07-21 02:33:56 +00001692/* exec_stmt:
1693 *
1694 * 'exec' expr ['in' test [',' test]]
1695 */
Guido van Rossum47478871996-08-21 14:32:37 +00001696static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001697validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001698{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001699 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001700 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001701 && ((nch == 2) || (nch == 4) || (nch == 6))
1702 && validate_name(CHILD(tree, 0), "exec")
1703 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001704
Guido van Rossum3d602e31996-07-21 02:33:56 +00001705 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001706 err_string("Illegal exec statement.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001707 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001708 res = (validate_name(CHILD(tree, 2), "in")
1709 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001710 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001711 res = (validate_comma(CHILD(tree, 4))
1712 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001713
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001714 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001715}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001716
1717
Guido van Rossum925e5471997-04-02 05:32:13 +00001718/* assert_stmt:
1719 *
1720 * 'assert' test [',' test]
1721 */
1722static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001723validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001724{
1725 int nch = NCH(tree);
1726 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001727 && ((nch == 2) || (nch == 4))
1728 && (validate_name(CHILD(tree, 0), "__assert__") ||
1729 validate_name(CHILD(tree, 0), "assert"))
1730 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001731
1732 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001733 err_string("Illegal assert statement.");
Guido van Rossum925e5471997-04-02 05:32:13 +00001734 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001735 res = (validate_comma(CHILD(tree, 2))
1736 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001737
1738 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001739}
Guido van Rossum925e5471997-04-02 05:32:13 +00001740
1741
Guido van Rossum47478871996-08-21 14:32:37 +00001742static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001743validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001744{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001745 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001746 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001747 && ((nch == 4) || (nch == 7))
1748 && validate_name(CHILD(tree, 0), "while")
1749 && validate_test(CHILD(tree, 1))
1750 && validate_colon(CHILD(tree, 2))
1751 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001752
Guido van Rossum3d602e31996-07-21 02:33:56 +00001753 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001754 res = (validate_name(CHILD(tree, 4), "else")
1755 && validate_colon(CHILD(tree, 5))
1756 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001757
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001758 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001759}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001760
1761
Guido van Rossum47478871996-08-21 14:32:37 +00001762static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001763validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001764{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001765 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001766 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001767 && ((nch == 6) || (nch == 9))
1768 && validate_name(CHILD(tree, 0), "for")
1769 && validate_exprlist(CHILD(tree, 1))
1770 && validate_name(CHILD(tree, 2), "in")
1771 && validate_testlist(CHILD(tree, 3))
1772 && validate_colon(CHILD(tree, 4))
1773 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001774
Guido van Rossum3d602e31996-07-21 02:33:56 +00001775 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001776 res = (validate_name(CHILD(tree, 6), "else")
1777 && validate_colon(CHILD(tree, 7))
1778 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001779
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001780 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001781}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001782
1783
Guido van Rossum3d602e31996-07-21 02:33:56 +00001784/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001785 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001786 * | 'try' ':' suite 'finally' ':' suite
1787 *
1788 */
Guido van Rossum47478871996-08-21 14:32:37 +00001789static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001790validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001791{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001792 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001793 int pos = 3;
1794 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001795 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001796
1797 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001798 res = (validate_name(CHILD(tree, 0), "try")
1799 && validate_colon(CHILD(tree, 1))
1800 && validate_suite(CHILD(tree, 2))
1801 && validate_colon(CHILD(tree, nch - 2))
1802 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001803 else {
Fred Drake22269b52000-07-03 18:07:43 +00001804 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001805 char buffer[60];
1806 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1807 name = STR(CHILD(tree, nch - 3));
1808 (void) sprintf(buffer,
1809 "Illegal number of children for try/%s node.", name);
1810 err_string(buffer);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001811 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001812 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001813 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001814 res = (validate_except_clause(CHILD(tree, pos))
1815 && validate_colon(CHILD(tree, pos + 1))
1816 && validate_suite(CHILD(tree, pos + 2)));
1817 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001818 }
1819 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001820 res = validate_ntype(CHILD(tree, pos), NAME);
1821 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1822 res = (validate_numnodes(tree, 6, "try/finally")
1823 && validate_colon(CHILD(tree, 4))
1824 && validate_suite(CHILD(tree, 5)));
1825 else if (res) {
1826 if (nch == (pos + 3)) {
1827 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1828 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1829 if (!res)
1830 err_string("Illegal trailing triple in try statement.");
1831 }
1832 else if (nch == (pos + 6)) {
1833 res = (validate_name(CHILD(tree, pos), "except")
1834 && validate_colon(CHILD(tree, pos + 1))
1835 && validate_suite(CHILD(tree, pos + 2))
1836 && validate_name(CHILD(tree, pos + 3), "else"));
1837 }
1838 else
1839 res = validate_numnodes(tree, pos + 3, "try/except");
1840 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001841 }
1842 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001843}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001844
1845
Guido van Rossum47478871996-08-21 14:32:37 +00001846static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001847validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001848{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001849 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001850 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001851 && ((nch == 1) || (nch == 2) || (nch == 4))
1852 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001853
Guido van Rossum3d602e31996-07-21 02:33:56 +00001854 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001855 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001856 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001857 res = (validate_comma(CHILD(tree, 2))
1858 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001859
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001860 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001861}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001862
1863
Guido van Rossum47478871996-08-21 14:32:37 +00001864static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001865validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001866{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001867 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001868 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001869
Guido van Rossum3d602e31996-07-21 02:33:56 +00001870 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001871 res = ((nch == 1)
1872 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001873 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001874 int pos;
1875 res = validate_and_test(CHILD(tree, 0));
1876 for (pos = 1; res && (pos < nch); pos += 2)
1877 res = (validate_name(CHILD(tree, pos), "or")
1878 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001879 }
1880 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001881}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001882
1883
Guido van Rossum47478871996-08-21 14:32:37 +00001884static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001885validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001886{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001887 int pos;
1888 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001889 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001890 && is_odd(nch)
1891 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001892
Guido van Rossum3d602e31996-07-21 02:33:56 +00001893 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001894 res = (validate_name(CHILD(tree, pos), "and")
1895 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001896
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001897 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001898}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001899
1900
Guido van Rossum47478871996-08-21 14:32:37 +00001901static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001902validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001903{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001904 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001905 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001906
Guido van Rossum3d602e31996-07-21 02:33:56 +00001907 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001908 if (nch == 2)
1909 res = (validate_name(CHILD(tree, 0), "not")
1910 && validate_not_test(CHILD(tree, 1)));
1911 else if (nch == 1)
1912 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001913 }
1914 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001915}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001916
1917
Guido van Rossum47478871996-08-21 14:32:37 +00001918static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001919validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001920{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001921 int pos;
1922 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001923 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001924 && is_odd(nch)
1925 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001926
Guido van Rossum3d602e31996-07-21 02:33:56 +00001927 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001928 res = (validate_comp_op(CHILD(tree, pos))
1929 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001930
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001931 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001932}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001933
1934
Guido van Rossum47478871996-08-21 14:32:37 +00001935static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001936validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001937{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001938 int res = 0;
1939 int nch = NCH(tree);
1940
Guido van Rossum3d602e31996-07-21 02:33:56 +00001941 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001942 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001943 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001944 /*
1945 * Only child will be a terminal with a well-defined symbolic name
1946 * or a NAME with a string of either 'is' or 'in'
1947 */
1948 tree = CHILD(tree, 0);
1949 switch (TYPE(tree)) {
1950 case LESS:
1951 case GREATER:
1952 case EQEQUAL:
1953 case EQUAL:
1954 case LESSEQUAL:
1955 case GREATEREQUAL:
1956 case NOTEQUAL:
1957 res = 1;
1958 break;
1959 case NAME:
1960 res = ((strcmp(STR(tree), "in") == 0)
1961 || (strcmp(STR(tree), "is") == 0));
1962 if (!res) {
1963 char buff[128];
1964 (void) sprintf(buff, "Illegal operator: '%s'.", STR(tree));
1965 err_string(buff);
1966 }
1967 break;
1968 default:
1969 err_string("Illegal comparison operator type.");
1970 break;
1971 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001972 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00001973 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001974 res = (validate_ntype(CHILD(tree, 0), NAME)
1975 && validate_ntype(CHILD(tree, 1), NAME)
1976 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
1977 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
1978 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
1979 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
1980 if (!res && !PyErr_Occurred())
1981 err_string("Unknown comparison operator.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001982 }
1983 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001984}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001985
1986
Guido van Rossum47478871996-08-21 14:32:37 +00001987static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001988validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001989{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001990 int j;
1991 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001992 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001993 && is_odd(nch)
1994 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001995
Guido van Rossum3d602e31996-07-21 02:33:56 +00001996 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001997 res = (validate_xor_expr(CHILD(tree, j))
1998 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001999
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002000 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002001}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002002
2003
Guido van Rossum47478871996-08-21 14:32:37 +00002004static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002005validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002006{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002007 int j;
2008 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002009 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002010 && is_odd(nch)
2011 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002012
Guido van Rossum3d602e31996-07-21 02:33:56 +00002013 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002014 res = (validate_circumflex(CHILD(tree, j - 1))
2015 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002016
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002017 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002018}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002019
2020
Guido van Rossum47478871996-08-21 14:32:37 +00002021static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002022validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002023{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002024 int pos;
2025 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002026 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002027 && is_odd(nch)
2028 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002029
Guido van Rossum3d602e31996-07-21 02:33:56 +00002030 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002031 res = (validate_ampersand(CHILD(tree, pos))
2032 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002033
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002034 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002035}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002036
2037
2038static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002039validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002040 {
2041 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002042 int nch = NCH(tree);
2043 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002044 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002045
Guido van Rossum3d602e31996-07-21 02:33:56 +00002046 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002047 if (TYPE(CHILD(tree, pos)) != op1)
2048 res = validate_ntype(CHILD(tree, pos), op2);
2049 if (res)
2050 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002051 }
2052 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002053}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002054
2055
Guido van Rossum47478871996-08-21 14:32:37 +00002056static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002057validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002058{
2059 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002060 && validate_chain_two_ops(tree, validate_arith_expr,
2061 LEFTSHIFT, RIGHTSHIFT));
2062}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002063
2064
Guido van Rossum47478871996-08-21 14:32:37 +00002065static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002066validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002067{
2068 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002069 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2070}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002071
2072
Guido van Rossum47478871996-08-21 14:32:37 +00002073static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002074validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002075{
2076 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002077 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002078 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002079 && is_odd(nch)
2080 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002081
Guido van Rossum3d602e31996-07-21 02:33:56 +00002082 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002083 res = (((TYPE(CHILD(tree, pos)) == STAR)
2084 || (TYPE(CHILD(tree, pos)) == SLASH)
2085 || (TYPE(CHILD(tree, pos)) == PERCENT))
2086 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002087
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002088 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002089}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002090
2091
Guido van Rossum3d602e31996-07-21 02:33:56 +00002092/* factor:
2093 *
2094 * factor: ('+'|'-'|'~') factor | power
2095 */
Guido van Rossum47478871996-08-21 14:32:37 +00002096static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002097validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002098{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002099 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002100 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002101 && (((nch == 2)
2102 && ((TYPE(CHILD(tree, 0)) == PLUS)
2103 || (TYPE(CHILD(tree, 0)) == MINUS)
2104 || (TYPE(CHILD(tree, 0)) == TILDE))
2105 && validate_factor(CHILD(tree, 1)))
2106 || ((nch == 1)
2107 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002108 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002109}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002110
2111
Guido van Rossum3d602e31996-07-21 02:33:56 +00002112/* power:
2113 *
2114 * power: atom trailer* ('**' factor)*
2115 */
Guido van Rossum47478871996-08-21 14:32:37 +00002116static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002117validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002118{
2119 int pos = 1;
2120 int nch = NCH(tree);
2121 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002122 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002123
2124 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002125 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002126 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002127 if (!is_even(nch - pos)) {
2128 err_string("Illegal number of nodes for 'power'.");
2129 return (0);
2130 }
2131 for ( ; res && (pos < (nch - 1)); pos += 2)
2132 res = (validate_doublestar(CHILD(tree, pos))
2133 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002134 }
2135 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002136}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002137
2138
Guido van Rossum47478871996-08-21 14:32:37 +00002139static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002140validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002141{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002142 int pos;
2143 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002144 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002145
Fred Drakecff283c2000-08-21 22:24:43 +00002146 if (res && nch < 1)
2147 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002148 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002149 switch (TYPE(CHILD(tree, 0))) {
2150 case LPAR:
2151 res = ((nch <= 3)
2152 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002153
Fred Drakeff9ea482000-04-19 13:54:15 +00002154 if (res && (nch == 3))
2155 res = validate_testlist(CHILD(tree, 1));
2156 break;
2157 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002158 if (nch == 2)
2159 res = validate_ntype(CHILD(tree, 1), RSQB);
2160 else if (nch == 3)
2161 res = (validate_listmaker(CHILD(tree, 1))
2162 && validate_ntype(CHILD(tree, 2), RSQB));
2163 else {
2164 res = 0;
2165 err_string("illegal list display atom");
2166 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002167 break;
2168 case LBRACE:
2169 res = ((nch <= 3)
2170 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002171
Fred Drakeff9ea482000-04-19 13:54:15 +00002172 if (res && (nch == 3))
2173 res = validate_dictmaker(CHILD(tree, 1));
2174 break;
2175 case BACKQUOTE:
2176 res = ((nch == 3)
2177 && validate_testlist(CHILD(tree, 1))
2178 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2179 break;
2180 case NAME:
2181 case NUMBER:
2182 res = (nch == 1);
2183 break;
2184 case STRING:
2185 for (pos = 1; res && (pos < nch); ++pos)
2186 res = validate_ntype(CHILD(tree, pos), STRING);
2187 break;
2188 default:
2189 res = 0;
2190 break;
2191 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002192 }
2193 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002194}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002195
2196
Fred Drakecff283c2000-08-21 22:24:43 +00002197static int
2198validate_listmaker(node *tree)
2199{
2200 int nch = NCH(tree);
2201 int ok = nch;
2202
2203 if (nch == 0)
2204 err_string("missing child nodes of listmaker");
2205 else
2206 ok = validate_test(CHILD(tree, 0));
2207
2208 /*
2209 * list_iter | (',' test)* [',']
2210 */
2211 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_iter)
2212 ok = validate_list_iter(CHILD(tree, 1));
2213 else {
2214 /* (',' test)* [','] */
2215 int i = 1;
2216 while (ok && nch - i >= 2) {
2217 ok = (validate_comma(CHILD(tree, i))
2218 && validate_test(CHILD(tree, i+1)));
2219 if (ok)
2220 i += 2;
2221 }
2222 if (ok && nch-i)
2223 ok = validate_comma(CHILD(tree, nch-1));
2224 }
2225 return ok;
2226}
2227
2228
Guido van Rossum3d602e31996-07-21 02:33:56 +00002229/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002230 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002231 *
2232 */
Guido van Rossum47478871996-08-21 14:32:37 +00002233static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002234validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002235{
2236 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002237 && validate_numnodes(tree, 5, "funcdef")
2238 && validate_name(CHILD(tree, 0), "def")
2239 && validate_ntype(CHILD(tree, 1), NAME)
2240 && validate_colon(CHILD(tree, 3))
2241 && validate_parameters(CHILD(tree, 2))
2242 && validate_suite(CHILD(tree, 4)));
2243}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002244
2245
Guido van Rossum47478871996-08-21 14:32:37 +00002246static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002247validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002248{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002249 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002250 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002251 && ((nch == 3) || (nch == 4))
2252 && validate_name(CHILD(tree, 0), "lambda")
2253 && validate_colon(CHILD(tree, nch - 2))
2254 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002255
Guido van Rossum3d602e31996-07-21 02:33:56 +00002256 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002257 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002258 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002259 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002260
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002261 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002262}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002263
2264
Guido van Rossum3d602e31996-07-21 02:33:56 +00002265/* arglist:
2266 *
Fred Drakecff283c2000-08-21 22:24:43 +00002267 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002268 */
Guido van Rossum47478871996-08-21 14:32:37 +00002269static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002270validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002271{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002272 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002273 int i = 0;
2274 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002275
2276 if (nch <= 0)
2277 /* raise the right error from having an invalid number of children */
2278 return validate_numnodes(tree, nch + 1, "arglist");
2279
Fred Drakecff283c2000-08-21 22:24:43 +00002280 while (ok && nch-i >= 2) {
2281 /* skip leading (argument ',') */
2282 ok = (validate_argument(CHILD(tree, i))
2283 && validate_comma(CHILD(tree, i+1)));
2284 if (ok)
2285 i += 2;
2286 else
2287 PyErr_Clear();
2288 }
2289 ok = 1;
2290 if (nch-i > 0) {
2291 /*
2292 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002293 */
Fred Drakecff283c2000-08-21 22:24:43 +00002294 int sym = TYPE(CHILD(tree, i));
2295
2296 if (sym == argument) {
2297 ok = validate_argument(CHILD(tree, i));
2298 if (ok && i+1 != nch) {
2299 err_string("illegal arglist specification"
2300 " (extra stuff on end)");
2301 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002302 }
Fred Drakecff283c2000-08-21 22:24:43 +00002303 }
2304 else if (sym == STAR) {
2305 ok = validate_star(CHILD(tree, i));
2306 if (ok && (nch-i == 2))
2307 ok = validate_test(CHILD(tree, i+1));
2308 else if (ok && (nch-i == 5))
2309 ok = (validate_test(CHILD(tree, i+1))
2310 && validate_comma(CHILD(tree, i+2))
2311 && validate_doublestar(CHILD(tree, i+3))
2312 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002313 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002314 err_string("illegal use of '*' in arglist");
2315 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002316 }
2317 }
Fred Drakecff283c2000-08-21 22:24:43 +00002318 else if (sym == DOUBLESTAR) {
2319 if (nch-i == 2)
2320 ok = (validate_doublestar(CHILD(tree, i))
2321 && validate_test(CHILD(tree, i+1)));
2322 else {
2323 err_string("illegal use of '**' in arglist");
2324 ok = 0;
2325 }
2326 }
2327 else {
2328 err_string("illegal arglist specification");
2329 ok = 0;
2330 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002331 }
2332 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002333}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002334
2335
2336
2337/* argument:
2338 *
2339 * [test '='] test
2340 */
Guido van Rossum47478871996-08-21 14:32:37 +00002341static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002342validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002343{
2344 int nch = NCH(tree);
2345 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002346 && ((nch == 1) || (nch == 3))
2347 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002348
2349 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002350 res = (validate_equal(CHILD(tree, 1))
2351 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002352
2353 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002354}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002355
2356
2357
2358/* trailer:
2359 *
Guido van Rossum47478871996-08-21 14:32:37 +00002360 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002361 */
Guido van Rossum47478871996-08-21 14:32:37 +00002362static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002363validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002364{
2365 int nch = NCH(tree);
2366 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002367
2368 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002369 switch (TYPE(CHILD(tree, 0))) {
2370 case LPAR:
2371 res = validate_rparen(CHILD(tree, nch - 1));
2372 if (res && (nch == 3))
2373 res = validate_arglist(CHILD(tree, 1));
2374 break;
2375 case LSQB:
2376 res = (validate_numnodes(tree, 3, "trailer")
2377 && validate_subscriptlist(CHILD(tree, 1))
2378 && validate_ntype(CHILD(tree, 2), RSQB));
2379 break;
2380 case DOT:
2381 res = (validate_numnodes(tree, 2, "trailer")
2382 && validate_ntype(CHILD(tree, 1), NAME));
2383 break;
2384 default:
2385 res = 0;
2386 break;
2387 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002388 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002389 else {
2390 (void) validate_numnodes(tree, 2, "trailer");
2391 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002392 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002393}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002394
2395
Guido van Rossum47478871996-08-21 14:32:37 +00002396/* subscriptlist:
2397 *
2398 * subscript (',' subscript)* [',']
2399 */
2400static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002401validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002402{
2403 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002404 validate_subscript, "subscriptlist"));
2405}
Guido van Rossum47478871996-08-21 14:32:37 +00002406
2407
Guido van Rossum3d602e31996-07-21 02:33:56 +00002408/* subscript:
2409 *
Guido van Rossum47478871996-08-21 14:32:37 +00002410 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002411 */
Guido van Rossum47478871996-08-21 14:32:37 +00002412static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002413validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002414{
Guido van Rossum47478871996-08-21 14:32:37 +00002415 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002416 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002417 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002418
Guido van Rossum47478871996-08-21 14:32:37 +00002419 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002420 if (!PyErr_Occurred())
2421 err_string("invalid number of arguments for subscript node");
2422 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002423 }
Guido van Rossum47478871996-08-21 14:32:37 +00002424 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002425 /* take care of ('.' '.' '.') possibility */
2426 return (validate_numnodes(tree, 3, "subscript")
2427 && validate_dot(CHILD(tree, 0))
2428 && validate_dot(CHILD(tree, 1))
2429 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002430 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002431 if (TYPE(CHILD(tree, 0)) == test)
2432 res = validate_test(CHILD(tree, 0));
2433 else
2434 res = validate_colon(CHILD(tree, 0));
2435 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002436 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002437 /* Must be [test] ':' [test] [sliceop],
2438 * but at least one of the optional components will
2439 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002440 */
2441 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002442 res = validate_test(CHILD(tree, 0));
2443 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002444 }
2445 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002446 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002447 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002448 int rem = nch - ++offset;
2449 if (rem) {
2450 if (TYPE(CHILD(tree, offset)) == test) {
2451 res = validate_test(CHILD(tree, offset));
2452 ++offset;
2453 --rem;
2454 }
2455 if (res && rem)
2456 res = validate_sliceop(CHILD(tree, offset));
2457 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002458 }
2459 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002460}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002461
2462
Guido van Rossum47478871996-08-21 14:32:37 +00002463static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002464validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002465{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002466 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002467 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002468 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002469 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002470 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002471 }
Guido van Rossum47478871996-08-21 14:32:37 +00002472 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002473 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002474 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002475 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002476
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002477 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002478}
Guido van Rossum47478871996-08-21 14:32:37 +00002479
2480
2481static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002482validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002483{
2484 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002485 validate_expr, "exprlist"));
2486}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002487
2488
Guido van Rossum47478871996-08-21 14:32:37 +00002489static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002490validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002491{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002492 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002493 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002494 && (nch >= 3)
2495 && validate_test(CHILD(tree, 0))
2496 && validate_colon(CHILD(tree, 1))
2497 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002498
Guido van Rossum3d602e31996-07-21 02:33:56 +00002499 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002500 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002501 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002502 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002503
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002504 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002505 int pos = 3;
2506 /* ( ',' test ':' test )* */
2507 while (res && (pos < nch)) {
2508 res = (validate_comma(CHILD(tree, pos))
2509 && validate_test(CHILD(tree, pos + 1))
2510 && validate_colon(CHILD(tree, pos + 2))
2511 && validate_test(CHILD(tree, pos + 3)));
2512 pos += 4;
2513 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002514 }
2515 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002516}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002517
2518
Guido van Rossum47478871996-08-21 14:32:37 +00002519static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002520validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002521{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002522 int pos;
2523 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002524 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002525 && (nch >= 2)
2526 && validate_testlist(CHILD(tree, 0))
2527 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002528
Guido van Rossum3d602e31996-07-21 02:33:56 +00002529 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002530 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002531
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002532 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002533}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002534
2535
Guido van Rossum47478871996-08-21 14:32:37 +00002536static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002537validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002538{
Fred Drakeff9ea482000-04-19 13:54:15 +00002539 int nch = 0; /* num. children on current node */
2540 int res = 1; /* result value */
2541 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002542
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002543 while (res & (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002544 nch = NCH(tree);
2545 next = 0;
2546 switch (TYPE(tree)) {
2547 /*
2548 * Definition nodes.
2549 */
2550 case funcdef:
2551 res = validate_funcdef(tree);
2552 break;
2553 case classdef:
2554 res = validate_class(tree);
2555 break;
2556 /*
2557 * "Trivial" parse tree nodes.
2558 * (Why did I call these trivial?)
2559 */
2560 case stmt:
2561 res = validate_stmt(tree);
2562 break;
2563 case small_stmt:
2564 /*
2565 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
2566 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2567 */
2568 res = validate_small_stmt(tree);
2569 break;
2570 case flow_stmt:
2571 res = (validate_numnodes(tree, 1, "flow_stmt")
2572 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2573 || (TYPE(CHILD(tree, 0)) == continue_stmt)
2574 || (TYPE(CHILD(tree, 0)) == return_stmt)
2575 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2576 if (res)
2577 next = CHILD(tree, 0);
2578 else if (nch == 1)
2579 err_string("Illegal flow_stmt type.");
2580 break;
2581 /*
2582 * Compound statements.
2583 */
2584 case simple_stmt:
2585 res = validate_simple_stmt(tree);
2586 break;
2587 case compound_stmt:
2588 res = validate_compound_stmt(tree);
2589 break;
2590 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002591 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002592 */
2593 case expr_stmt:
2594 res = validate_expr_stmt(tree);
2595 break;
2596 case print_stmt:
2597 res = validate_print_stmt(tree);
2598 break;
2599 case del_stmt:
2600 res = validate_del_stmt(tree);
2601 break;
2602 case pass_stmt:
2603 res = (validate_numnodes(tree, 1, "pass")
2604 && validate_name(CHILD(tree, 0), "pass"));
2605 break;
2606 case break_stmt:
2607 res = (validate_numnodes(tree, 1, "break")
2608 && validate_name(CHILD(tree, 0), "break"));
2609 break;
2610 case continue_stmt:
2611 res = (validate_numnodes(tree, 1, "continue")
2612 && validate_name(CHILD(tree, 0), "continue"));
2613 break;
2614 case return_stmt:
2615 res = validate_return_stmt(tree);
2616 break;
2617 case raise_stmt:
2618 res = validate_raise_stmt(tree);
2619 break;
2620 case import_stmt:
2621 res = validate_import_stmt(tree);
2622 break;
2623 case global_stmt:
2624 res = validate_global_stmt(tree);
2625 break;
2626 case exec_stmt:
2627 res = validate_exec_stmt(tree);
2628 break;
2629 case assert_stmt:
2630 res = validate_assert_stmt(tree);
2631 break;
2632 case if_stmt:
2633 res = validate_if(tree);
2634 break;
2635 case while_stmt:
2636 res = validate_while(tree);
2637 break;
2638 case for_stmt:
2639 res = validate_for(tree);
2640 break;
2641 case try_stmt:
2642 res = validate_try(tree);
2643 break;
2644 case suite:
2645 res = validate_suite(tree);
2646 break;
2647 /*
2648 * Expression nodes.
2649 */
2650 case testlist:
2651 res = validate_testlist(tree);
2652 break;
2653 case test:
2654 res = validate_test(tree);
2655 break;
2656 case and_test:
2657 res = validate_and_test(tree);
2658 break;
2659 case not_test:
2660 res = validate_not_test(tree);
2661 break;
2662 case comparison:
2663 res = validate_comparison(tree);
2664 break;
2665 case exprlist:
2666 res = validate_exprlist(tree);
2667 break;
2668 case comp_op:
2669 res = validate_comp_op(tree);
2670 break;
2671 case expr:
2672 res = validate_expr(tree);
2673 break;
2674 case xor_expr:
2675 res = validate_xor_expr(tree);
2676 break;
2677 case and_expr:
2678 res = validate_and_expr(tree);
2679 break;
2680 case shift_expr:
2681 res = validate_shift_expr(tree);
2682 break;
2683 case arith_expr:
2684 res = validate_arith_expr(tree);
2685 break;
2686 case term:
2687 res = validate_term(tree);
2688 break;
2689 case factor:
2690 res = validate_factor(tree);
2691 break;
2692 case power:
2693 res = validate_power(tree);
2694 break;
2695 case atom:
2696 res = validate_atom(tree);
2697 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002698
Fred Drakeff9ea482000-04-19 13:54:15 +00002699 default:
2700 /* Hopefully never reached! */
2701 err_string("Unrecogniged node type.");
2702 res = 0;
2703 break;
2704 }
2705 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002706 }
2707 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002708}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002709
2710
Guido van Rossum47478871996-08-21 14:32:37 +00002711static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002712validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002713{
2714 int res = validate_eval_input(tree);
2715
2716 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002717 err_string("Could not validate expression tuple.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002718
2719 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002720}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002721
2722
Guido van Rossum3d602e31996-07-21 02:33:56 +00002723/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002724 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002725 */
Guido van Rossum47478871996-08-21 14:32:37 +00002726static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002727validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002728{
2729 int j = 0;
2730 int nch = NCH(tree) - 1;
2731 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002732 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002733
Guido van Rossum3d602e31996-07-21 02:33:56 +00002734 for ( ; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002735 if (TYPE(CHILD(tree, j)) == stmt)
2736 res = validate_stmt(CHILD(tree, j));
2737 else
2738 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002739 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002740 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002741 * user. Hopefully, this won't be needed. If a user reports getting
2742 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002743 */
2744 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002745 err_string("VALIDATION FAILURE: report this to the maintainer!.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002746
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002747 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002748}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002749
2750
Fred Drake43f8f9b1998-04-13 16:25:46 +00002751static PyObject*
2752pickle_constructor = NULL;
2753
2754
2755static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002756parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002757{
Fred Drake268397f1998-04-29 14:16:32 +00002758 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002759 PyObject *result = NULL;
2760 PyObject *ast = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002761 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002762
2763 if (PyArg_ParseTuple(args, "O!:_pickler", &PyAST_Type, &ast)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002764 PyObject *newargs;
2765 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002766
Fred Drake2a6875e1999-09-20 22:32:18 +00002767 if ((empty_dict = PyDict_New()) == NULL)
2768 goto finally;
2769 if ((newargs = Py_BuildValue("Oi", ast, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002770 goto finally;
2771 tuple = parser_ast2tuple((PyAST_Object*)NULL, newargs, empty_dict);
2772 if (tuple != NULL) {
2773 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2774 Py_DECREF(tuple);
2775 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002776 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002777 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002778 }
2779 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002780 Py_XDECREF(empty_dict);
2781
Fred Drake43f8f9b1998-04-13 16:25:46 +00002782 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002783}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002784
2785
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002786/* Functions exported by this module. Most of this should probably
2787 * be converted into an AST object with methods, but that is better
2788 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002789 * We'd really have to write a wrapper around it all anyway to allow
2790 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002791 */
2792static PyMethodDef parser_functions[] = {
Fred Drakeff9ea482000-04-19 13:54:15 +00002793 {"ast2tuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
2794 "Creates a tuple-tree representation of an AST."},
2795 {"ast2list", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
2796 "Creates a list-tree representation of an AST."},
2797 {"compileast", (PyCFunction)parser_compileast, PUBLIC_METHOD_TYPE,
2798 "Compiles an AST object into a code object."},
2799 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
2800 "Creates an AST object from an expression."},
2801 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
2802 "Determines if an AST object was created from an expression."},
2803 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
2804 "Determines if an AST object was created from a suite."},
2805 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
2806 "Creates an AST object from a suite."},
2807 {"sequence2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
2808 "Creates an AST object from a tree representation."},
2809 {"tuple2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
2810 "Creates an AST object from a tree representation."},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002811
Fred Drake43f8f9b1998-04-13 16:25:46 +00002812 /* private stuff: support pickle module */
Fred Drakeff9ea482000-04-19 13:54:15 +00002813 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Fred Drake43f8f9b1998-04-13 16:25:46 +00002814 "Returns the pickle magic to allow ast objects to be pickled."},
2815
Fred Drake268397f1998-04-29 14:16:32 +00002816 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002817 };
2818
2819
Guido van Rossum3886bb61998-12-04 18:50:17 +00002820DL_EXPORT(void)
Thomas Wouters5c669862000-07-24 15:49:08 +00002821initparser(void)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002822 {
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002823 PyObject* module;
2824 PyObject* dict;
Fred Drakeff9ea482000-04-19 13:54:15 +00002825
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002826 PyAST_Type.ob_type = &PyType_Type;
2827 module = Py_InitModule("parser", parser_functions);
2828 dict = PyModule_GetDict(module);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002829
Fred Drake7a15ba51999-09-09 14:21:52 +00002830 if (parser_error == 0)
2831 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
2832 else
2833 puts("parser module initialized more than once!");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002834
2835 if ((parser_error == 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002836 || (PyDict_SetItemString(dict, "ParserError", parser_error) != 0)) {
2837 /*
2838 * This is serious.
2839 */
2840 Py_FatalError("can't define parser.ParserError");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002841 }
2842 /*
2843 * Nice to have, but don't cry if we fail.
2844 */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002845 Py_INCREF(&PyAST_Type);
2846 PyDict_SetItemString(dict, "ASTType", (PyObject*)&PyAST_Type);
2847
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002848 PyDict_SetItemString(dict, "__copyright__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002849 PyString_FromString(parser_copyright_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002850 PyDict_SetItemString(dict, "__doc__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002851 PyString_FromString(parser_doc_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002852 PyDict_SetItemString(dict, "__version__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002853 PyString_FromString(parser_version_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002854
Fred Drake43f8f9b1998-04-13 16:25:46 +00002855 /* register to support pickling */
2856 module = PyImport_ImportModule("copy_reg");
2857 if (module != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002858 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002859
Fred Drakeff9ea482000-04-19 13:54:15 +00002860 func = PyObject_GetAttrString(module, "pickle");
2861 pickle_constructor = PyDict_GetItemString(dict, "sequence2ast");
2862 pickler = PyDict_GetItemString(dict, "_pickler");
2863 Py_XINCREF(pickle_constructor);
2864 if ((func != NULL) && (pickle_constructor != NULL)
2865 && (pickler != NULL)) {
2866 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002867
Fred Drakeff9ea482000-04-19 13:54:15 +00002868 res = PyObject_CallFunction(
2869 func, "OOO", &PyAST_Type, pickler, pickle_constructor);
2870 Py_XDECREF(res);
2871 }
2872 Py_XDECREF(func);
2873 Py_DECREF(module);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002874 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002875}