blob: d69d460ef5184291e907c91362667f3e17347736 [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000026 */
27
Fred Drakeff9ea482000-04-19 13:54:15 +000028#include "Python.h" /* general Python API */
29#include "graminit.h" /* symbols defined in the grammar */
30#include "node.h" /* internal parser structure */
31#include "token.h" /* token definitions */
32 /* ISTERMINAL() / ISNONTERMINAL() */
33#include "compile.h" /* PyNode_Compile() */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000034
Fred Drake268397f1998-04-29 14:16:32 +000035#ifdef lint
36#include <note.h>
37#else
38#define NOTE(x)
39#endif
40
Guido van Rossum19efc5f1998-04-28 16:10:19 +000041#ifdef macintosh
Fred Drakeff9ea482000-04-19 13:54:15 +000042char *strdup(char *);
Guido van Rossum19efc5f1998-04-28 16:10:19 +000043#endif
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000044
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000045/* String constants used to initialize module attributes.
46 *
47 */
48static char*
49parser_copyright_string
Guido van Rossum2a288461996-08-21 21:55:43 +000050= "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
51University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
52Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
53Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000054
55
56static char*
57parser_doc_string
58= "This is an interface to Python's internal parser.";
59
60static char*
Guido van Rossum47478871996-08-21 14:32:37 +000061parser_version_string = "0.4";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000062
63
Fred Drakeff9ea482000-04-19 13:54:15 +000064typedef PyObject* (*SeqMaker) (int length);
65typedef int (*SeqInserter) (PyObject* sequence,
66 int index,
67 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000068
Guido van Rossum3d602e31996-07-21 02:33:56 +000069/* The function below is copyrigthed by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000070 * original copyright statement is included below, and continues to apply
71 * in full to the function immediately following. All other material is
72 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
73 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000074 * new naming conventions. Added arguments to provide support for creating
75 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000076 */
77
Guido van Rossum52f2c051993-11-10 12:53:24 +000078/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +000079Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
80The Netherlands.
Guido van Rossum52f2c051993-11-10 12:53:24 +000081
82 All Rights Reserved
83
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000084Copyright (c) 2000, BeOpen.com.
85Copyright (c) 1995-2000, Corporation for National Research Initiatives.
86Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
87All rights reserved.
Guido van Rossum52f2c051993-11-10 12:53:24 +000088
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000089See the file "Misc/COPYRIGHT" for information on usage and
90redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum52f2c051993-11-10 12:53:24 +000091
92******************************************************************/
93
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000094static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000095node2tuple(node *n, /* node to convert */
96 SeqMaker mkseq, /* create sequence */
97 SeqInserter addelem, /* func. to add elem. in seq. */
98 int lineno) /* include line numbers? */
Guido van Rossum47478871996-08-21 14:32:37 +000099{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000100 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000101 Py_INCREF(Py_None);
102 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000103 }
104 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000105 int i;
106 PyObject *v;
107 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +0000108
Fred Drakeff9ea482000-04-19 13:54:15 +0000109 v = mkseq(1 + NCH(n));
110 if (v == NULL)
111 return (v);
112 w = PyInt_FromLong(TYPE(n));
113 if (w == NULL) {
114 Py_DECREF(v);
115 return ((PyObject*) NULL);
116 }
117 (void) addelem(v, 0, w);
118 for (i = 0; i < NCH(n); i++) {
119 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno);
120 if (w == NULL) {
121 Py_DECREF(v);
122 return ((PyObject*) NULL);
123 }
124 (void) addelem(v, i+1, w);
125 }
126 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000127 }
128 else if (ISTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000129 PyObject *result = mkseq(2 + lineno);
130 if (result != NULL) {
131 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
132 (void) addelem(result, 1, PyString_FromString(STR(n)));
133 if (lineno == 1)
134 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
135 }
136 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000137 }
138 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000139 PyErr_SetString(PyExc_SystemError,
140 "unrecognized parse tree node type");
141 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000142 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000143}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000144/*
145 * End of material copyrighted by Stichting Mathematisch Centrum.
146 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000147
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000148
149
150/* There are two types of intermediate objects we're interested in:
151 * 'eval' and 'exec' types. These constants can be used in the ast_type
152 * field of the object type to identify which any given object represents.
153 * These should probably go in an external header to allow other extensions
154 * to use them, but then, we really should be using C++ too. ;-)
155 *
Guido van Rossum3d602e31996-07-21 02:33:56 +0000156 * The PyAST_FRAGMENT type is not currently supported. Maybe not useful?
157 * Haven't decided yet.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000158 */
159
Fred Drakeff9ea482000-04-19 13:54:15 +0000160#define PyAST_EXPR 1
161#define PyAST_SUITE 2
162#define PyAST_FRAGMENT 3
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000163
164
165/* These are the internal objects and definitions required to implement the
166 * AST type. Most of the internal names are more reminiscent of the 'old'
167 * naming style, but the code uses the new naming convention.
168 */
169
170static PyObject*
171parser_error = 0;
172
173
174typedef struct _PyAST_Object {
Fred Drakeff9ea482000-04-19 13:54:15 +0000175 PyObject_HEAD /* standard object header */
176 node* ast_node; /* the node* returned by the parser */
177 int ast_type; /* EXPR or SUITE ? */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000178} PyAST_Object;
179
180
Fred Drake268397f1998-04-29 14:16:32 +0000181staticforward void
Fred Drakeff9ea482000-04-19 13:54:15 +0000182parser_free(PyAST_Object *ast);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000183
Fred Drake268397f1998-04-29 14:16:32 +0000184staticforward int
Fred Drakeff9ea482000-04-19 13:54:15 +0000185parser_compare(PyAST_Object *left, PyAST_Object *right);
Fred Drake268397f1998-04-29 14:16:32 +0000186
187staticforward PyObject *
Fred Drakeff9ea482000-04-19 13:54:15 +0000188parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000189
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000190
Fred Drake268397f1998-04-29 14:16:32 +0000191static
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000192PyTypeObject PyAST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000193 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000194 0,
Fred Drakeff9ea482000-04-19 13:54:15 +0000195 "ast", /* tp_name */
196 (int) sizeof(PyAST_Object), /* tp_basicsize */
197 0, /* tp_itemsize */
198 (destructor)parser_free, /* tp_dealloc */
199 0, /* tp_print */
200 parser_getattr, /* tp_getattr */
201 0, /* tp_setattr */
202 (cmpfunc)parser_compare, /* tp_compare */
203 0, /* tp_repr */
204 0, /* tp_as_number */
205 0, /* tp_as_sequence */
206 0, /* tp_as_mapping */
207 0, /* tp_hash */
208 0, /* tp_call */
209 0, /* tp_str */
210 0, /* tp_getattro */
211 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000212
213 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000214 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000215
Fred Drakeff9ea482000-04-19 13:54:15 +0000216 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000217
218 /* __doc__ */
219 "Intermediate representation of a Python parse tree."
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000220}; /* PyAST_Type */
221
222
223static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000224parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000225{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000226 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000227
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000228 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000229 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000230
231 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000232 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000233
234 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000235 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000236
237 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000238 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000239
240 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000241 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000242
243 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000244 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000245
Fred Drakeff9ea482000-04-19 13:54:15 +0000246 if (v != 0)
247 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000248 }
249 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000250}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000251
252
253/* int parser_compare(PyAST_Object* left, PyAST_Object* right)
254 *
255 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
256 * This really just wraps a call to parser_compare_nodes() with some easy
257 * checks and protection code.
258 *
259 */
260static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000261parser_compare(PyAST_Object *left, PyAST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000262{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000263 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000264 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000265
266 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000267 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000268
269 return (parser_compare_nodes(left->ast_node, right->ast_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000270}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000271
272
273/* parser_newastobject(node* ast)
274 *
275 * Allocates a new Python object representing an AST. This is simply the
276 * 'wrapper' object that holds a node* and allows it to be passed around in
277 * Python code.
278 *
279 */
280static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000281parser_newastobject(node *ast, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000282{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000283 PyAST_Object* o = PyObject_New(PyAST_Object, &PyAST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000284
285 if (o != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000286 o->ast_node = ast;
287 o->ast_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000288 }
Fred Drake268397f1998-04-29 14:16:32 +0000289 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000290 PyNode_Free(ast);
Fred Drake268397f1998-04-29 14:16:32 +0000291 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000292 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000293}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000294
295
296/* void parser_free(PyAST_Object* ast)
297 *
298 * This is called by a del statement that reduces the reference count to 0.
299 *
300 */
301static void
Fred Drakeff9ea482000-04-19 13:54:15 +0000302parser_free(PyAST_Object *ast)
Guido van Rossum47478871996-08-21 14:32:37 +0000303{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000304 PyNode_Free(ast->ast_node);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000305 PyObject_Del(ast);
Fred Drakeff9ea482000-04-19 13:54:15 +0000306}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000307
308
Fred Drake2a6875e1999-09-20 22:32:18 +0000309/* parser_ast2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000310 *
311 * This provides conversion from a node* to a tuple object that can be
312 * returned to the Python-level caller. The AST object is not modified.
313 *
314 */
315static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000316parser_ast2tuple(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000317{
Guido van Rossum47478871996-08-21 14:32:37 +0000318 PyObject *line_option = 0;
319 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000320 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000321
Fred Drake7a15ba51999-09-09 14:21:52 +0000322 static char *keywords[] = {"ast", "line_info", NULL};
323
Fred Drake268397f1998-04-29 14:16:32 +0000324 if (self == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000325 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:ast2tuple", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000326 &PyAST_Type, &self, &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000327 }
Fred Drake503d8d61998-04-13 18:45:18 +0000328 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000329 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:totuple", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000330 &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000331 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000332 int lineno = 0;
333 if (line_option != NULL) {
334 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
335 }
336 /*
337 * Convert AST into a tuple representation. Use Guido's function,
338 * since it's known to work already.
339 */
340 res = node2tuple(((PyAST_Object*)self)->ast_node,
341 PyTuple_New, PyTuple_SetItem, lineno);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000342 }
343 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000344}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000345
346
Fred Drake2a6875e1999-09-20 22:32:18 +0000347/* parser_ast2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000348 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000349 * This provides conversion from a node* to a list object that can be
Guido van Rossum47478871996-08-21 14:32:37 +0000350 * returned to the Python-level caller. The AST object is not modified.
351 *
352 */
353static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000354parser_ast2list(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000355{
Guido van Rossum47478871996-08-21 14:32:37 +0000356 PyObject *line_option = 0;
357 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000358 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000359
Fred Drake7a15ba51999-09-09 14:21:52 +0000360 static char *keywords[] = {"ast", "line_info", NULL};
361
Fred Drake503d8d61998-04-13 18:45:18 +0000362 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000363 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:ast2list", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000364 &PyAST_Type, &self, &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000365 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000366 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:tolist", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000367 &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000368 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000369 int lineno = 0;
370 if (line_option != 0) {
371 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
372 }
373 /*
374 * Convert AST into a tuple representation. Use Guido's function,
375 * since it's known to work already.
376 */
377 res = node2tuple(self->ast_node,
378 PyList_New, PyList_SetItem, lineno);
Guido van Rossum47478871996-08-21 14:32:37 +0000379 }
380 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000381}
Guido van Rossum47478871996-08-21 14:32:37 +0000382
383
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000384/* parser_compileast(PyObject* self, PyObject* args)
385 *
386 * This function creates code objects from the parse tree represented by
387 * the passed-in data object. An optional file name is passed in as well.
388 *
389 */
390static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000391parser_compileast(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000392{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000393 PyObject* res = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000394 char* str = "<ast>";
Fred Drake503d8d61998-04-13 18:45:18 +0000395 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000396
Fred Drake7a15ba51999-09-09 14:21:52 +0000397 static char *keywords[] = {"ast", "filename", NULL};
398
Fred Drake503d8d61998-04-13 18:45:18 +0000399 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000400 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compileast", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000401 &PyAST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000402 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000403 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000404 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000405
406 if (ok)
Fred Drakeff9ea482000-04-19 13:54:15 +0000407 res = (PyObject *)PyNode_Compile(self->ast_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000408
409 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000410}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000411
412
413/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
414 * PyObject* parser_issuite(PyObject* self, PyObject* args)
415 *
416 * Checks the passed-in AST object to determine if it is an expression or
417 * a statement suite, respectively. The return is a Python truth value.
418 *
419 */
420static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000421parser_isexpr(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000422{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000423 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000424 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000425
Fred Drake7a15ba51999-09-09 14:21:52 +0000426 static char *keywords[] = {"ast", NULL};
427
Fred Drake503d8d61998-04-13 18:45:18 +0000428 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000429 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000430 &PyAST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000431 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000432 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000433
434 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000435 /* Check to see if the AST represents an expression or not. */
436 res = (self->ast_type == PyAST_EXPR) ? Py_True : Py_False;
437 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000438 }
439 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000440}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000441
442
443static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000444parser_issuite(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000445{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000446 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000447 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000448
Fred Drake7a15ba51999-09-09 14:21:52 +0000449 static char *keywords[] = {"ast", NULL};
450
Fred Drake503d8d61998-04-13 18:45:18 +0000451 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000452 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000453 &PyAST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000454 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000455 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000456
457 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000458 /* Check to see if the AST represents an expression or not. */
459 res = (self->ast_type == PyAST_EXPR) ? Py_False : Py_True;
460 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000461 }
462 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000463}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000464
465
Fred Drake7a15ba51999-09-09 14:21:52 +0000466#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
467
Fred Drake503d8d61998-04-13 18:45:18 +0000468static PyMethodDef
469parser_methods[] = {
Fred Drakeff9ea482000-04-19 13:54:15 +0000470 {"compile", (PyCFunction)parser_compileast, PUBLIC_METHOD_TYPE,
471 "Compile this AST object into a code object."},
472 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
473 "Determines if this AST object was created from an expression."},
474 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
475 "Determines if this AST object was created from a suite."},
476 {"tolist", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
477 "Creates a list-tree representation of this AST."},
478 {"totuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
479 "Creates a tuple-tree representation of this AST."},
Fred Drake503d8d61998-04-13 18:45:18 +0000480
Fred Drake268397f1998-04-29 14:16:32 +0000481 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000482};
483
Fred Drake503d8d61998-04-13 18:45:18 +0000484
485static PyObject*
486parser_getattr(self, name)
487 PyObject *self;
488 char *name;
489{
Fred Drake503d8d61998-04-13 18:45:18 +0000490 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000491}
Fred Drake503d8d61998-04-13 18:45:18 +0000492
493
Guido van Rossum3d602e31996-07-21 02:33:56 +0000494/* err_string(char* message)
495 *
496 * Sets the error string for an exception of type ParserError.
497 *
498 */
499static void
500err_string(message)
501 char *message;
Guido van Rossum47478871996-08-21 14:32:37 +0000502{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000503 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000504}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000505
506
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000507/* PyObject* parser_do_parse(PyObject* args, int type)
508 *
509 * Internal function to actually execute the parse and return the result if
510 * successful, or set an exception if not.
511 *
512 */
513static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000514parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000515{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000516 char* string = 0;
517 PyObject* res = 0;
518
Fred Drake7a15ba51999-09-09 14:21:52 +0000519 static char *keywords[] = {"source", NULL};
520
521 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000522 node* n = PyParser_SimpleParseString(string,
523 (type == PyAST_EXPR)
524 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000525
Fred Drakeff9ea482000-04-19 13:54:15 +0000526 if (n != 0)
527 res = parser_newastobject(n, type);
528 else
529 err_string("Could not parse string.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000530 }
531 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000532}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000533
534
535/* PyObject* parser_expr(PyObject* self, PyObject* args)
536 * PyObject* parser_suite(PyObject* self, PyObject* args)
537 *
538 * External interfaces to the parser itself. Which is called determines if
539 * the parser attempts to recognize an expression ('eval' form) or statement
540 * suite ('exec' form). The real work is done by parser_do_parse() above.
541 *
542 */
543static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000544parser_expr(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000545{
Fred Drake268397f1998-04-29 14:16:32 +0000546 NOTE(ARGUNUSED(self))
Fred Drake7a15ba51999-09-09 14:21:52 +0000547 return (parser_do_parse(args, kw, "s:expr", PyAST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000548}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000549
550
551static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000552parser_suite(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000553{
Fred Drake268397f1998-04-29 14:16:32 +0000554 NOTE(ARGUNUSED(self))
Fred Drake7a15ba51999-09-09 14:21:52 +0000555 return (parser_do_parse(args, kw, "s:suite", PyAST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000556}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000557
558
559
560/* This is the messy part of the code. Conversion from a tuple to an AST
561 * object requires that the input tuple be valid without having to rely on
562 * catching an exception from the compiler. This is done to allow the
563 * compiler itself to remain fast, since most of its input will come from
564 * the parser directly, and therefore be known to be syntactically correct.
565 * This validation is done to ensure that we don't core dump the compile
566 * phase, returning an exception instead.
567 *
568 * Two aspects can be broken out in this code: creating a node tree from
569 * the tuple passed in, and verifying that it is indeed valid. It may be
570 * advantageous to expand the number of AST types to include funcdefs and
571 * lambdadefs to take advantage of the optimizer, recognizing those ASTs
572 * here. They are not necessary, and not quite as useful in a raw form.
573 * For now, let's get expressions and suites working reliably.
574 */
575
576
Fred Drakeff9ea482000-04-19 13:54:15 +0000577staticforward node* build_node_tree(PyObject *tuple);
578staticforward int validate_expr_tree(node *tree);
579staticforward int validate_file_input(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000580
581
582/* PyObject* parser_tuple2ast(PyObject* self, PyObject* args)
583 *
584 * This is the public function, called from the Python code. It receives a
585 * single tuple object from the caller, and creates an AST object if the
586 * tuple can be validated. It does this by checking the first code of the
587 * tuple, and, if acceptable, builds the internal representation. If this
588 * step succeeds, the internal representation is validated as fully as
589 * possible with the various validate_*() routines defined below.
590 *
591 * This function must be changed if support is to be added for PyAST_FRAGMENT
592 * AST objects.
593 *
594 */
595static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000596parser_tuple2ast(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000597{
Fred Drake268397f1998-04-29 14:16:32 +0000598 NOTE(ARGUNUSED(self))
Guido van Rossum47478871996-08-21 14:32:37 +0000599 PyObject *ast = 0;
600 PyObject *tuple = 0;
601 PyObject *temp = 0;
602 int ok;
Guido van Rossuma376cc51996-12-05 23:43:35 +0000603 int start_sym = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000604
Fred Drake7a15ba51999-09-09 14:21:52 +0000605 static char *keywords[] = {"sequence", NULL};
606
607 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:tuple2ast", keywords,
608 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000609 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000610 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000611 PyErr_SetString(PyExc_ValueError,
612 "tuple2ast() requires a single sequence argument");
613 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000614 }
615 /*
Fred Drakeff9ea482000-04-19 13:54:15 +0000616 * This mess of tests is written this way so we can use the abstract
617 * object interface (AOI). Unfortunately, the AOI increments reference
618 * counts, which requires that we store a pointer to retrieved object
619 * so we can DECREF it after the check. But we really should accept
620 * lists as well as tuples at the very least.
Guido van Rossum47478871996-08-21 14:32:37 +0000621 */
622 ok = PyObject_Length(tuple) >= 2;
623 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000624 temp = PySequence_GetItem(tuple, 0);
625 ok = (temp != NULL) && PyInt_Check(temp);
626 if (ok)
627 /* this is used after the initial checks: */
628 start_sym = PyInt_AS_LONG(temp);
629 Py_XDECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000630 }
631 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000632 temp = PySequence_GetItem(tuple, 1);
633 ok = (temp != NULL) && PySequence_Check(temp);
634 Py_XDECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000635 }
636 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000637 temp = PySequence_GetItem(tuple, 1);
638 ok = (temp != NULL) && PyObject_Length(temp) >= 2;
639 if (ok) {
640 PyObject *temp2 = PySequence_GetItem(temp, 0);
641 if (temp2 != NULL) {
642 ok = PyInt_Check(temp2);
643 Py_DECREF(temp2);
644 }
645 }
646 Py_XDECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000647 }
648 /* If we've failed at some point, get out of here. */
649 if (!ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000650 err_string("malformed sequence for tuple2ast()");
651 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000652 }
653 /*
654 * This might be a valid parse tree, but let's do a quick check
655 * before we jump the gun.
656 */
657 if (start_sym == eval_input) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000658 /* Might be an eval form. */
659 node* expression = build_node_tree(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000660
Fred Drakeff9ea482000-04-19 13:54:15 +0000661 if ((expression != 0) && validate_expr_tree(expression))
662 ast = parser_newastobject(expression, PyAST_EXPR);
Guido van Rossum47478871996-08-21 14:32:37 +0000663 }
664 else if (start_sym == file_input) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000665 /* This looks like an exec form so far. */
666 node* suite_tree = build_node_tree(tuple);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000667
Fred Drakeff9ea482000-04-19 13:54:15 +0000668 if ((suite_tree != 0) && validate_file_input(suite_tree))
669 ast = parser_newastobject(suite_tree, PyAST_SUITE);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000670 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000671 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000672 /* This is a fragment, and is not yet supported. Maybe they
673 * will be if I find a use for them.
674 */
675 err_string("Fragmentary parse trees not supported.");
Guido van Rossum47478871996-08-21 14:32:37 +0000676
677 /* Make sure we throw an exception on all errors. We should never
678 * get this, but we'd do well to be sure something is done.
679 */
680 if ((ast == 0) && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000681 err_string("Unspecified ast error occurred.");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000682
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000683 return (ast);
Fred Drakeff9ea482000-04-19 13:54:15 +0000684}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000685
686
687/* int check_terminal_tuple()
688 *
Guido van Rossum47478871996-08-21 14:32:37 +0000689 * Check a tuple to determine that it is indeed a valid terminal
690 * node. The node is known to be required as a terminal, so we throw
691 * an exception if there is a failure.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000692 *
Guido van Rossum47478871996-08-21 14:32:37 +0000693 * The format of an acceptable terminal tuple is "(is[i])": the fact
694 * that elem is a tuple and the integer is a valid terminal symbol
695 * has been established before this function is called. We must
696 * check the length of the tuple and the type of the second element
697 * and optional third element. We do *NOT* check the actual text of
698 * the string element, which we could do in many cases. This is done
699 * by the validate_*() functions which operate on the internal
700 * representation.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000701 */
702static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000703check_terminal_tuple(PyObject *elem)
Guido van Rossum47478871996-08-21 14:32:37 +0000704{
705 int len = PyObject_Length(elem);
706 int res = 1;
707 char* str = "Illegal terminal symbol; bad node length.";
Guido van Rossum3d602e31996-07-21 02:33:56 +0000708
Guido van Rossum47478871996-08-21 14:32:37 +0000709 if ((len == 2) || (len == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000710 PyObject *temp = PySequence_GetItem(elem, 1);
711 res = PyString_Check(temp);
712 str = "Illegal terminal symbol; expected a string.";
713 if (res && (len == 3)) {
714 PyObject* third = PySequence_GetItem(elem, 2);
715 res = PyInt_Check(third);
716 str = "Invalid third element of terminal node.";
717 Py_XDECREF(third);
718 }
719 Py_XDECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000720 }
721 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000722 res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000723 }
724 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000725 elem = Py_BuildValue("(os)", elem, str);
726 PyErr_SetObject(parser_error, elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000727 }
728 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000729}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000730
731
732/* node* build_node_children()
733 *
734 * Iterate across the children of the current non-terminal node and build
735 * their structures. If successful, return the root of this portion of
736 * the tree, otherwise, 0. Any required exception will be specified already,
737 * and no memory will have been deallocated.
738 *
739 */
740static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000741build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000742{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000743 int len = PyObject_Length(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000744 int i;
745
746 for (i = 1; i < len; ++i) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000747 /* elem must always be a tuple, however simple */
748 PyObject* elem = PySequence_GetItem(tuple, i);
749 int ok = elem != NULL;
750 long type = 0;
751 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000752
Fred Drakeff9ea482000-04-19 13:54:15 +0000753 if (ok)
754 ok = PySequence_Check(elem);
755 if (ok) {
756 PyObject *temp = PySequence_GetItem(elem, 0);
757 if (temp == NULL)
758 ok = 0;
759 else {
760 ok = PyInt_Check(temp);
761 if (ok)
762 type = PyInt_AS_LONG(temp);
763 Py_DECREF(temp);
764 }
765 }
766 if (!ok) {
767 PyErr_SetObject(parser_error,
768 Py_BuildValue("(os)", elem,
769 "Illegal node construct."));
770 Py_XDECREF(elem);
771 return (0);
772 }
773 if (ISTERMINAL(type)) {
774 if (check_terminal_tuple(elem)) {
775 PyObject *temp = PySequence_GetItem(elem, 1);
Guido van Rossum47478871996-08-21 14:32:37 +0000776
Fred Drakeff9ea482000-04-19 13:54:15 +0000777 /* check_terminal_tuple() already verified it's a string */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000778 strn = (char *)PyMem_MALLOC(PyString_GET_SIZE(temp) + 1);
Fred Drakeff9ea482000-04-19 13:54:15 +0000779 if (strn != NULL)
780 (void) strcpy(strn, PyString_AS_STRING(temp));
Guido van Rossumb18618d2000-05-03 23:44:39 +0000781 Py_DECREF(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000782
Fred Drakeff9ea482000-04-19 13:54:15 +0000783 if (PyObject_Length(elem) == 3) {
784 PyObject* temp = PySequence_GetItem(elem, 2);
785 *line_num = PyInt_AsLong(temp);
786 Py_DECREF(temp);
787 }
788 }
789 else {
790 Py_XDECREF(elem);
791 return (0);
792 }
793 }
794 else if (!ISNONTERMINAL(type)) {
795 /*
796 * It has to be one or the other; this is an error.
797 * Throw an exception.
798 */
799 PyErr_SetObject(parser_error,
800 Py_BuildValue("(os)", elem,
801 "Unknown node type."));
802 Py_XDECREF(elem);
803 return (0);
804 }
805 PyNode_AddChild(root, type, strn, *line_num);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000806
Fred Drakeff9ea482000-04-19 13:54:15 +0000807 if (ISNONTERMINAL(type)) {
808 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000809
Fred Drakeff9ea482000-04-19 13:54:15 +0000810 if (new_child != build_node_children(elem, new_child, line_num)) {
811 Py_XDECREF(elem);
812 return (0);
813 }
814 }
815 else if (type == NEWLINE) { /* It's true: we increment the */
816 ++(*line_num); /* line number *after* the newline! */
817 }
818 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000819 }
820 return (root);
Fred Drakeff9ea482000-04-19 13:54:15 +0000821}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000822
823
824static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000825build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000826{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000827 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000828 PyObject *temp = PySequence_GetItem(tuple, 0);
829 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000830
Guido van Rossum47478871996-08-21 14:32:37 +0000831 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000832 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000833 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000834 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000835 /*
836 * The tuple is simple, but it doesn't start with a start symbol.
837 * Throw an exception now and be done with it.
838 */
839 tuple = Py_BuildValue("(os)", tuple,
840 "Illegal ast tuple; cannot start with terminal symbol.");
841 PyErr_SetObject(parser_error, tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000842 }
843 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000844 /*
845 * Not efficient, but that can be handled later.
846 */
847 int line_num = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000848
Fred Drakeff9ea482000-04-19 13:54:15 +0000849 res = PyNode_New(num);
850 if (res != build_node_children(tuple, res, &line_num)) {
851 PyNode_Free(res);
852 res = 0;
853 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000854 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000855 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000856 /* The tuple is illegal -- if the number is neither TERMINAL nor
857 * NONTERMINAL, we can't use it.
858 */
859 PyErr_SetObject(parser_error,
860 Py_BuildValue("(os)", tuple,
861 "Illegal component tuple."));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000862
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000863 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000864}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000865
866
Guido van Rossum360a9341996-08-21 19:04:10 +0000867#ifdef HAVE_OLD_CPP
Fred Drakeff9ea482000-04-19 13:54:15 +0000868#define VALIDATER(n) static int validate_/**/n(node *tree)
Guido van Rossum360a9341996-08-21 19:04:10 +0000869#else
Fred Drakeff9ea482000-04-19 13:54:15 +0000870#define VALIDATER(n) static int validate_##n(node *tree)
Guido van Rossum360a9341996-08-21 19:04:10 +0000871#endif
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000872
873
874/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000875 * Validation routines used within the validation section:
876 */
Fred Drakeff9ea482000-04-19 13:54:15 +0000877staticforward int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000878
Fred Drakeff9ea482000-04-19 13:54:15 +0000879#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
880#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
881#define validate_colon(ch) validate_terminal(ch, COLON, ":")
882#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
883#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
884#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
885#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
886#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
887#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
888#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
889#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
890#define validate_star(ch) validate_terminal(ch, STAR, "*")
891#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
892#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
893#define validate_dot(ch) validate_terminal(ch, DOT, ".")
894#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000895
Fred Drakeff9ea482000-04-19 13:54:15 +0000896VALIDATER(node); VALIDATER(small_stmt);
897VALIDATER(class); VALIDATER(node);
898VALIDATER(parameters); VALIDATER(suite);
899VALIDATER(testlist); VALIDATER(varargslist);
900VALIDATER(fpdef); VALIDATER(fplist);
901VALIDATER(stmt); VALIDATER(simple_stmt);
902VALIDATER(expr_stmt); VALIDATER(power);
903VALIDATER(print_stmt); VALIDATER(del_stmt);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000904VALIDATER(return_stmt);
Fred Drakeff9ea482000-04-19 13:54:15 +0000905VALIDATER(raise_stmt); VALIDATER(import_stmt);
Guido van Rossum2a288461996-08-21 21:55:43 +0000906VALIDATER(global_stmt);
Guido van Rossum925e5471997-04-02 05:32:13 +0000907VALIDATER(assert_stmt);
Fred Drakeff9ea482000-04-19 13:54:15 +0000908VALIDATER(exec_stmt); VALIDATER(compound_stmt);
909VALIDATER(while); VALIDATER(for);
910VALIDATER(try); VALIDATER(except_clause);
911VALIDATER(test); VALIDATER(and_test);
912VALIDATER(not_test); VALIDATER(comparison);
913VALIDATER(comp_op); VALIDATER(expr);
914VALIDATER(xor_expr); VALIDATER(and_expr);
915VALIDATER(shift_expr); VALIDATER(arith_expr);
916VALIDATER(term); VALIDATER(factor);
917VALIDATER(atom); VALIDATER(lambdef);
918VALIDATER(trailer); VALIDATER(subscript);
919VALIDATER(subscriptlist); VALIDATER(sliceop);
920VALIDATER(exprlist); VALIDATER(dictmaker);
921VALIDATER(arglist); VALIDATER(argument);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000922
923
Fred Drakeff9ea482000-04-19 13:54:15 +0000924#define is_even(n) (((n) & 1) == 0)
925#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000926
927
928static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000929validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000930{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000931 int res = (TYPE(n) == t);
932
933 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000934 char buffer[128];
935 (void) sprintf(buffer, "Expected node type %d, got %d.", t, TYPE(n));
936 err_string(buffer);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000937 }
938 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000939}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000940
941
Fred Drakee7ab64e2000-04-25 04:14:46 +0000942/* Verifies that the number of child nodes is exactly 'num', raising
943 * an exception if it isn't. The exception message does not indicate
944 * the exact number of nodes, allowing this to be used to raise the
945 * "right" exception when the wrong number of nodes is present in a
946 * specific variant of a statement's syntax. This is commonly used
947 * in that fashion.
948 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000949static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000950validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000951{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000952 if (NCH(n) != num) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000953 char buff[60];
954 (void) sprintf(buff, "Illegal number of children for %s node.", name);
955 err_string(buff);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000956 }
957 return (NCH(n) == num);
Fred Drakeff9ea482000-04-19 13:54:15 +0000958}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000959
960
961static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000962validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000963{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000964 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000965 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000966
967 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000968 char buffer[60];
969 (void) sprintf(buffer, "Illegal terminal: expected \"%s\"", string);
970 err_string(buffer);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000971 }
972 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000973}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000974
975
Guido van Rossum47478871996-08-21 14:32:37 +0000976/* X (',' X) [',']
977 */
978static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000979validate_repeating_list(node *tree, int ntype, int (*vfunc)(),
980 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000981{
982 int nch = NCH(tree);
983 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000984 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000985
986 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000987 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000988 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000989 if (is_even(nch))
990 res = validate_comma(CHILD(tree, --nch));
991 if (res && nch > 1) {
992 int pos = 1;
993 for ( ; res && pos < nch; pos += 2)
994 res = (validate_comma(CHILD(tree, pos))
995 && vfunc(CHILD(tree, pos + 1)));
996 }
Guido van Rossum47478871996-08-21 14:32:37 +0000997 }
998 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000999}
Guido van Rossum47478871996-08-21 14:32:37 +00001000
1001
Guido van Rossum3d602e31996-07-21 02:33:56 +00001002/* VALIDATE(class)
1003 *
1004 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001005 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00001006 */
Guido van Rossum47478871996-08-21 14:32:37 +00001007static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001008validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001009{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001010 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001011 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001012
Guido van Rossum3d602e31996-07-21 02:33:56 +00001013 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001014 res = (validate_name(CHILD(tree, 0), "class")
1015 && validate_ntype(CHILD(tree, 1), NAME)
1016 && validate_colon(CHILD(tree, nch - 2))
1017 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001018 }
1019 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001020 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001021 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001022 res = (validate_lparen(CHILD(tree, 2))
1023 && validate_testlist(CHILD(tree, 3))
1024 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001025 }
1026 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001027}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001028
1029
Guido van Rossum3d602e31996-07-21 02:33:56 +00001030/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001031 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001032 */
Guido van Rossum47478871996-08-21 14:32:37 +00001033static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001034validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001035{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001036 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001037 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001038 && (nch >= 4)
1039 && validate_name(CHILD(tree, 0), "if")
1040 && validate_test(CHILD(tree, 1))
1041 && validate_colon(CHILD(tree, 2))
1042 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001043
1044 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001045 /* ... 'else' ':' suite */
1046 res = (validate_name(CHILD(tree, nch - 3), "else")
1047 && validate_colon(CHILD(tree, nch - 2))
1048 && validate_suite(CHILD(tree, nch - 1)));
1049 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001050 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001051 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001052 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001053 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001054 /* Will catch the case for nch < 4 */
1055 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001056 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001057 /* ... ('elif' test ':' suite)+ ... */
1058 int j = 4;
1059 while ((j < nch) && res) {
1060 res = (validate_name(CHILD(tree, j), "elif")
1061 && validate_colon(CHILD(tree, j + 2))
1062 && validate_test(CHILD(tree, j + 1))
1063 && validate_suite(CHILD(tree, j + 3)));
1064 j += 4;
1065 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001066 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001067 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001068}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001069
1070
Guido van Rossum3d602e31996-07-21 02:33:56 +00001071/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001072 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001073 *
1074 */
Guido van Rossum47478871996-08-21 14:32:37 +00001075static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001076validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001077{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001078 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001079 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001080
Guido van Rossum3d602e31996-07-21 02:33:56 +00001081 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001082 res = (validate_lparen(CHILD(tree, 0))
1083 && validate_rparen(CHILD(tree, nch - 1)));
1084 if (res && (nch == 3))
1085 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001086 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001087 else {
1088 (void) validate_numnodes(tree, 2, "parameters");
1089 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001090 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001091}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001092
1093
Guido van Rossum3d602e31996-07-21 02:33:56 +00001094/* VALIDATE(suite)
1095 *
1096 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001097 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001098 * | NEWLINE INDENT stmt+ DEDENT
1099 */
Guido van Rossum47478871996-08-21 14:32:37 +00001100static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001101validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001102{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001103 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001104 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001105
Guido van Rossum3d602e31996-07-21 02:33:56 +00001106 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001107 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001108 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001109 /* NEWLINE INDENT stmt+ DEDENT */
1110 res = (validate_newline(CHILD(tree, 0))
1111 && validate_indent(CHILD(tree, 1))
1112 && validate_stmt(CHILD(tree, 2))
1113 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001114
Fred Drakeff9ea482000-04-19 13:54:15 +00001115 if (res && (nch > 4)) {
1116 int i = 3;
1117 --nch; /* forget the DEDENT */
1118 for ( ; res && (i < nch); ++i)
1119 res = validate_stmt(CHILD(tree, i));
1120 }
1121 else if (nch < 4)
1122 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001123 }
1124 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001125}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001126
1127
Guido van Rossum47478871996-08-21 14:32:37 +00001128static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001129validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001130{
Guido van Rossum47478871996-08-21 14:32:37 +00001131 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001132 validate_test, "testlist"));
1133}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001134
1135
Guido van Rossum3d602e31996-07-21 02:33:56 +00001136/* VALIDATE(varargslist)
1137 *
1138 * varargslist:
Fred Drakeff9ea482000-04-19 13:54:15 +00001139 * (fpdef ['=' test] ',')* ('*' NAME [',' '*' '*' NAME] | '*' '*' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001140 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1141 *
1142 * (fpdef ['=' test] ',')*
1143 * ('*' NAME [',' ('**'|'*' '*') NAME]
1144 * | ('**'|'*' '*') NAME)
1145 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1146 *
1147 */
Guido van Rossum47478871996-08-21 14:32:37 +00001148static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001149validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001150{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001151 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001152 int res = validate_ntype(tree, varargslist) && (nch != 0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001153
Guido van Rossum3d602e31996-07-21 02:33:56 +00001154 if (res && (nch >= 2) && (TYPE(CHILD(tree, nch - 1)) == NAME)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001155 /* (fpdef ['=' test] ',')*
1156 * ('*' NAME [',' '*' '*' NAME] | '*' '*' NAME)
1157 */
1158 int pos = 0;
1159 int remaining = nch;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001160
Fred Drakeff9ea482000-04-19 13:54:15 +00001161 while (res && (TYPE(CHILD(tree, pos)) == fpdef)) {
1162 res = validate_fpdef(CHILD(tree, pos));
1163 if (res) {
1164 if (TYPE(CHILD(tree, pos + 1)) == EQUAL) {
1165 res = validate_test(CHILD(tree, pos + 2));
1166 pos += 2;
1167 }
1168 res = res && validate_comma(CHILD(tree, pos + 1));
1169 pos += 2;
1170 }
1171 }
1172 if (res) {
1173 remaining = nch - pos;
1174 res = ((remaining == 2) || (remaining == 3)
1175 || (remaining == 5) || (remaining == 6));
1176 if (!res)
1177 (void) validate_numnodes(tree, 2, "varargslist");
1178 else if (TYPE(CHILD(tree, pos)) == DOUBLESTAR)
1179 return ((remaining == 2)
1180 && validate_ntype(CHILD(tree, pos+1), NAME));
1181 else {
1182 res = validate_star(CHILD(tree, pos++));
1183 --remaining;
1184 }
1185 }
1186 if (res) {
1187 if (remaining == 2) {
1188 res = (validate_star(CHILD(tree, pos))
1189 && validate_ntype(CHILD(tree, pos + 1), NAME));
1190 }
1191 else {
1192 res = validate_ntype(CHILD(tree, pos++), NAME);
1193 if (res && (remaining >= 4)) {
1194 res = validate_comma(CHILD(tree, pos));
1195 if (--remaining == 3)
1196 res = (validate_star(CHILD(tree, pos + 1))
1197 && validate_star(CHILD(tree, pos + 2)));
1198 else
1199 res = validate_ntype(CHILD(tree, pos + 1), DOUBLESTAR);
1200 }
1201 }
1202 }
1203 if (!res && !PyErr_Occurred())
1204 err_string("Incorrect validation of variable arguments list.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001205 }
1206 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001207 /* fpdef ['=' test] (',' fpdef ['=' test])* [','] */
1208 if (TYPE(CHILD(tree, nch - 1)) == COMMA)
1209 --nch;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001210
Fred Drakeff9ea482000-04-19 13:54:15 +00001211 /* fpdef ['=' test] (',' fpdef ['=' test])* */
1212 res = (is_odd(nch)
1213 && validate_fpdef(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001214
Fred Drakeff9ea482000-04-19 13:54:15 +00001215 if (res && (nch > 1)) {
1216 int pos = 1;
1217 if (TYPE(CHILD(tree, 1)) == EQUAL) {
1218 res = validate_test(CHILD(tree, 2));
1219 pos += 2;
1220 }
1221 /* ... (',' fpdef ['=' test])* */
1222 for ( ; res && (pos < nch); pos += 2) {
1223 /* ',' fpdef */
1224 res = (validate_comma(CHILD(tree, pos))
1225 && validate_fpdef(CHILD(tree, pos + 1)));
1226 if (res
1227 && ((nch - pos) > 2)
1228 && (TYPE(CHILD(tree, pos + 2)) == EQUAL)) {
1229 /* ['=' test] */
1230 res = validate_test(CHILD(tree, pos + 3));
1231 pos += 2;
1232 }
1233 }
1234 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001235 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001236 else {
1237 err_string("Improperly formed argument list.");
1238 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001239 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001240}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001241
1242
Guido van Rossum3d602e31996-07-21 02:33:56 +00001243/* VALIDATE(fpdef)
1244 *
1245 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001246 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001247 * | '(' fplist ')'
1248 */
Guido van Rossum47478871996-08-21 14:32:37 +00001249static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001250validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001251{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001252 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001253 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001254
Guido van Rossum3d602e31996-07-21 02:33:56 +00001255 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001256 if (nch == 1)
1257 res = validate_ntype(CHILD(tree, 0), NAME);
1258 else if (nch == 3)
1259 res = (validate_lparen(CHILD(tree, 0))
1260 && validate_fplist(CHILD(tree, 1))
1261 && validate_rparen(CHILD(tree, 2)));
1262 else
1263 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001264 }
1265 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001266}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001267
1268
Guido van Rossum47478871996-08-21 14:32:37 +00001269static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001270validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001271{
Guido van Rossum47478871996-08-21 14:32:37 +00001272 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001273 validate_fpdef, "fplist"));
1274}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001275
1276
Guido van Rossum3d602e31996-07-21 02:33:56 +00001277/* simple_stmt | compound_stmt
1278 *
1279 */
Guido van Rossum47478871996-08-21 14:32:37 +00001280static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001281validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001282{
1283 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001284 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001285
Guido van Rossum3d602e31996-07-21 02:33:56 +00001286 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001287 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001288
Fred Drakeff9ea482000-04-19 13:54:15 +00001289 if (TYPE(tree) == simple_stmt)
1290 res = validate_simple_stmt(tree);
1291 else
1292 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001293 }
1294 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001295}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001296
1297
Guido van Rossum3d602e31996-07-21 02:33:56 +00001298/* small_stmt (';' small_stmt)* [';'] NEWLINE
1299 *
1300 */
Guido van Rossum47478871996-08-21 14:32:37 +00001301static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001302validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001303{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001304 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001305 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001306 && (nch >= 2)
1307 && validate_small_stmt(CHILD(tree, 0))
1308 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001309
Guido van Rossum3d602e31996-07-21 02:33:56 +00001310 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001311 res = validate_numnodes(tree, 2, "simple_stmt");
1312 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001313 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001314 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001315 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001316 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001317
Fred Drakeff9ea482000-04-19 13:54:15 +00001318 for (i = 1; res && (i < nch); i += 2)
1319 res = (validate_semi(CHILD(tree, i))
1320 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001321 }
1322 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001323}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001324
1325
Guido van Rossum47478871996-08-21 14:32:37 +00001326static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001327validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001328{
1329 int nch = NCH(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +00001330 int res = (validate_numnodes(tree, 1, "small_stmt")
1331 && ((TYPE(CHILD(tree, 0)) == expr_stmt)
1332 || (TYPE(CHILD(tree, 0)) == print_stmt)
1333 || (TYPE(CHILD(tree, 0)) == del_stmt)
1334 || (TYPE(CHILD(tree, 0)) == pass_stmt)
1335 || (TYPE(CHILD(tree, 0)) == flow_stmt)
1336 || (TYPE(CHILD(tree, 0)) == import_stmt)
1337 || (TYPE(CHILD(tree, 0)) == global_stmt)
1338 || (TYPE(CHILD(tree, 0)) == assert_stmt)
1339 || (TYPE(CHILD(tree, 0)) == exec_stmt)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001340
1341 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001342 res = validate_node(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001343 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001344 char buffer[60];
1345 (void) sprintf(buffer, "Unrecognized child node of small_stmt: %d.",
1346 TYPE(CHILD(tree, 0)));
1347 err_string(buffer);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001348 }
1349 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001350}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001351
1352
1353/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001354 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001355 */
Guido van Rossum47478871996-08-21 14:32:37 +00001356static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001357validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001358{
1359 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001360 && validate_numnodes(tree, 1, "compound_stmt"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001361
1362 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001363 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001364
1365 tree = CHILD(tree, 0);
1366 res = ((TYPE(tree) == if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001367 || (TYPE(tree) == while_stmt)
1368 || (TYPE(tree) == for_stmt)
1369 || (TYPE(tree) == try_stmt)
1370 || (TYPE(tree) == funcdef)
1371 || (TYPE(tree) == classdef));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001372 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001373 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001374 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001375 char buffer[60];
1376 (void) sprintf(buffer, "Illegal compound statement type: %d.",
1377 TYPE(tree));
1378 err_string(buffer);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001379 }
1380 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001381}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001382
1383
Guido van Rossum47478871996-08-21 14:32:37 +00001384static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001385validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001386{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001387 int j;
1388 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001389 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001390 && is_odd(nch)
1391 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001392
Guido van Rossum3d602e31996-07-21 02:33:56 +00001393 for (j = 1; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001394 res = (validate_equal(CHILD(tree, j))
1395 && validate_testlist(CHILD(tree, j + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001396
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001397 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001398}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001399
1400
Guido van Rossum3d602e31996-07-21 02:33:56 +00001401/* print_stmt:
1402 *
Fred Drakeff9ea482000-04-19 13:54:15 +00001403 * 'print' (test ',')* [test]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001404 *
1405 */
Guido van Rossum47478871996-08-21 14:32:37 +00001406static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001407validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001408{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001409 int j;
1410 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001411 int res = (validate_ntype(tree, print_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001412 && (nch != 0)
1413 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001414
1415 if (res && is_even(nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001416 res = validate_test(CHILD(tree, nch - 1));
1417 --nch;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001418 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001419 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001420 (void) validate_numnodes(tree, 1, "print_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001421 for (j = 1; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001422 res = (validate_test(CHILD(tree, j))
1423 && validate_ntype(CHILD(tree, j + 1), COMMA));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001424
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001425 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001426}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001427
1428
Guido van Rossum47478871996-08-21 14:32:37 +00001429static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001430validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001431{
1432 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001433 && validate_name(CHILD(tree, 0), "del")
1434 && validate_exprlist(CHILD(tree, 1)));
1435}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001436
1437
Guido van Rossum47478871996-08-21 14:32:37 +00001438static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001439validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001440{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001441 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001442 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001443 && ((nch == 1) || (nch == 2))
1444 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001445
Guido van Rossum3d602e31996-07-21 02:33:56 +00001446 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001447 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001448
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001449 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001450}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001451
1452
Guido van Rossum47478871996-08-21 14:32:37 +00001453static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001454validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001455{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001456 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001457 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001458 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001459
Guido van Rossum3d602e31996-07-21 02:33:56 +00001460 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001461 res = validate_name(CHILD(tree, 0), "raise");
1462 if (res && (nch >= 2))
1463 res = validate_test(CHILD(tree, 1));
1464 if (res && nch > 2) {
1465 res = (validate_comma(CHILD(tree, 2))
1466 && validate_test(CHILD(tree, 3)));
1467 if (res && (nch > 4))
1468 res = (validate_comma(CHILD(tree, 4))
1469 && validate_test(CHILD(tree, 5)));
1470 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001471 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001472 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001473 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001474 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001475 res = (validate_comma(CHILD(tree, 2))
1476 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001477
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001478 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001479}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001480
1481
Guido van Rossum3d602e31996-07-21 02:33:56 +00001482/* import_stmt:
1483 *
1484 * 'import' dotted_name (',' dotted_name)*
1485 * | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*)
1486 */
Guido van Rossum47478871996-08-21 14:32:37 +00001487static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001488validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001489{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001490 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001491 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001492 && (nch >= 2) && is_even(nch)
1493 && validate_ntype(CHILD(tree, 0), NAME)
1494 && validate_ntype(CHILD(tree, 1), dotted_name));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001495
1496 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001497 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001498
Fred Drakeff9ea482000-04-19 13:54:15 +00001499 for (j = 2; res && (j < nch); j += 2)
1500 res = (validate_comma(CHILD(tree, j))
1501 && validate_ntype(CHILD(tree, j + 1), dotted_name));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001502 }
1503 else if (res && validate_name(CHILD(tree, 0), "from")) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001504 res = ((nch >= 4) && is_even(nch)
1505 && validate_name(CHILD(tree, 2), "import"));
1506 if (nch == 4) {
1507 res = ((TYPE(CHILD(tree, 3)) == NAME)
1508 || (TYPE(CHILD(tree, 3)) == STAR));
1509 if (!res)
1510 err_string("Illegal import statement.");
1511 }
1512 else {
1513 /* 'from' NAME 'import' NAME (',' NAME)+ */
1514 int j;
1515 res = validate_ntype(CHILD(tree, 3), NAME);
1516 for (j = 4; res && (j < nch); j += 2)
1517 res = (validate_comma(CHILD(tree, j))
1518 && validate_ntype(CHILD(tree, j + 1), NAME));
1519 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001520 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001521 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001522 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001523
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001524 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001525}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001526
1527
Guido van Rossum47478871996-08-21 14:32:37 +00001528static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001529validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001530{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001531 int j;
1532 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001533 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001534 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001535
Guido van Rossum3d602e31996-07-21 02:33:56 +00001536 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001537 res = (validate_name(CHILD(tree, 0), "global")
1538 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001539 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001540 res = (validate_comma(CHILD(tree, j))
1541 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001542
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001543 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001544}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001545
1546
Guido van Rossum3d602e31996-07-21 02:33:56 +00001547/* exec_stmt:
1548 *
1549 * 'exec' expr ['in' test [',' test]]
1550 */
Guido van Rossum47478871996-08-21 14:32:37 +00001551static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001552validate_exec_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, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001556 && ((nch == 2) || (nch == 4) || (nch == 6))
1557 && validate_name(CHILD(tree, 0), "exec")
1558 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001559
Guido van Rossum3d602e31996-07-21 02:33:56 +00001560 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001561 err_string("Illegal exec statement.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001562 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001563 res = (validate_name(CHILD(tree, 2), "in")
1564 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001565 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001566 res = (validate_comma(CHILD(tree, 4))
1567 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001568
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001569 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001570}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001571
1572
Guido van Rossum925e5471997-04-02 05:32:13 +00001573/* assert_stmt:
1574 *
1575 * 'assert' test [',' test]
1576 */
1577static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001578validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001579{
1580 int nch = NCH(tree);
1581 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001582 && ((nch == 2) || (nch == 4))
1583 && (validate_name(CHILD(tree, 0), "__assert__") ||
1584 validate_name(CHILD(tree, 0), "assert"))
1585 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001586
1587 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001588 err_string("Illegal assert statement.");
Guido van Rossum925e5471997-04-02 05:32:13 +00001589 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001590 res = (validate_comma(CHILD(tree, 2))
1591 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001592
1593 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001594}
Guido van Rossum925e5471997-04-02 05:32:13 +00001595
1596
Guido van Rossum47478871996-08-21 14:32:37 +00001597static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001598validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001599{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001600 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001601 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001602 && ((nch == 4) || (nch == 7))
1603 && validate_name(CHILD(tree, 0), "while")
1604 && validate_test(CHILD(tree, 1))
1605 && validate_colon(CHILD(tree, 2))
1606 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001607
Guido van Rossum3d602e31996-07-21 02:33:56 +00001608 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001609 res = (validate_name(CHILD(tree, 4), "else")
1610 && validate_colon(CHILD(tree, 5))
1611 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001612
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001613 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001614}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001615
1616
Guido van Rossum47478871996-08-21 14:32:37 +00001617static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001618validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001619{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001620 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001621 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001622 && ((nch == 6) || (nch == 9))
1623 && validate_name(CHILD(tree, 0), "for")
1624 && validate_exprlist(CHILD(tree, 1))
1625 && validate_name(CHILD(tree, 2), "in")
1626 && validate_testlist(CHILD(tree, 3))
1627 && validate_colon(CHILD(tree, 4))
1628 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001629
Guido van Rossum3d602e31996-07-21 02:33:56 +00001630 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001631 res = (validate_name(CHILD(tree, 6), "else")
1632 && validate_colon(CHILD(tree, 7))
1633 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001634
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001635 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001636}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001637
1638
Guido van Rossum3d602e31996-07-21 02:33:56 +00001639/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001640 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001641 * | 'try' ':' suite 'finally' ':' suite
1642 *
1643 */
Guido van Rossum47478871996-08-21 14:32:37 +00001644static int
Guido van Rossum3d602e31996-07-21 02:33:56 +00001645validate_try(tree)
1646 node *tree;
1647{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001648 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001649 int pos = 3;
1650 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001651 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001652
1653 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001654 res = (validate_name(CHILD(tree, 0), "try")
1655 && validate_colon(CHILD(tree, 1))
1656 && validate_suite(CHILD(tree, 2))
1657 && validate_colon(CHILD(tree, nch - 2))
1658 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001659 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001660 const char* name = "execpt";
1661 char buffer[60];
1662 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1663 name = STR(CHILD(tree, nch - 3));
1664 (void) sprintf(buffer,
1665 "Illegal number of children for try/%s node.", name);
1666 err_string(buffer);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001667 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001668 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001669 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001670 res = (validate_except_clause(CHILD(tree, pos))
1671 && validate_colon(CHILD(tree, pos + 1))
1672 && validate_suite(CHILD(tree, pos + 2)));
1673 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001674 }
1675 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001676 res = validate_ntype(CHILD(tree, pos), NAME);
1677 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1678 res = (validate_numnodes(tree, 6, "try/finally")
1679 && validate_colon(CHILD(tree, 4))
1680 && validate_suite(CHILD(tree, 5)));
1681 else if (res) {
1682 if (nch == (pos + 3)) {
1683 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1684 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1685 if (!res)
1686 err_string("Illegal trailing triple in try statement.");
1687 }
1688 else if (nch == (pos + 6)) {
1689 res = (validate_name(CHILD(tree, pos), "except")
1690 && validate_colon(CHILD(tree, pos + 1))
1691 && validate_suite(CHILD(tree, pos + 2))
1692 && validate_name(CHILD(tree, pos + 3), "else"));
1693 }
1694 else
1695 res = validate_numnodes(tree, pos + 3, "try/except");
1696 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001697 }
1698 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001699}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001700
1701
Guido van Rossum47478871996-08-21 14:32:37 +00001702static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001703validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001704{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001705 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001706 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001707 && ((nch == 1) || (nch == 2) || (nch == 4))
1708 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001709
Guido van Rossum3d602e31996-07-21 02:33:56 +00001710 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001711 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001712 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001713 res = (validate_comma(CHILD(tree, 2))
1714 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001715
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001716 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001717}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001718
1719
Guido van Rossum47478871996-08-21 14:32:37 +00001720static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001721validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001722{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001723 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001724 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001725
Guido van Rossum3d602e31996-07-21 02:33:56 +00001726 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001727 res = ((nch == 1)
1728 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001729 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001730 int pos;
1731 res = validate_and_test(CHILD(tree, 0));
1732 for (pos = 1; res && (pos < nch); pos += 2)
1733 res = (validate_name(CHILD(tree, pos), "or")
1734 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001735 }
1736 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001737}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001738
1739
Guido van Rossum47478871996-08-21 14:32:37 +00001740static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001741validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001742{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001743 int pos;
1744 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001745 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001746 && is_odd(nch)
1747 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001748
Guido van Rossum3d602e31996-07-21 02:33:56 +00001749 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001750 res = (validate_name(CHILD(tree, pos), "and")
1751 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001752
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001753 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001754}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001755
1756
Guido van Rossum47478871996-08-21 14:32:37 +00001757static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001758validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001759{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001760 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001761 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001762
Guido van Rossum3d602e31996-07-21 02:33:56 +00001763 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001764 if (nch == 2)
1765 res = (validate_name(CHILD(tree, 0), "not")
1766 && validate_not_test(CHILD(tree, 1)));
1767 else if (nch == 1)
1768 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001769 }
1770 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001771}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001772
1773
Guido van Rossum47478871996-08-21 14:32:37 +00001774static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001775validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001776{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001777 int pos;
1778 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001779 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001780 && is_odd(nch)
1781 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001782
Guido van Rossum3d602e31996-07-21 02:33:56 +00001783 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001784 res = (validate_comp_op(CHILD(tree, pos))
1785 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001786
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001787 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001788}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001789
1790
Guido van Rossum47478871996-08-21 14:32:37 +00001791static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001792validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001793{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001794 int res = 0;
1795 int nch = NCH(tree);
1796
Guido van Rossum3d602e31996-07-21 02:33:56 +00001797 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001798 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001799 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001800 /*
1801 * Only child will be a terminal with a well-defined symbolic name
1802 * or a NAME with a string of either 'is' or 'in'
1803 */
1804 tree = CHILD(tree, 0);
1805 switch (TYPE(tree)) {
1806 case LESS:
1807 case GREATER:
1808 case EQEQUAL:
1809 case EQUAL:
1810 case LESSEQUAL:
1811 case GREATEREQUAL:
1812 case NOTEQUAL:
1813 res = 1;
1814 break;
1815 case NAME:
1816 res = ((strcmp(STR(tree), "in") == 0)
1817 || (strcmp(STR(tree), "is") == 0));
1818 if (!res) {
1819 char buff[128];
1820 (void) sprintf(buff, "Illegal operator: '%s'.", STR(tree));
1821 err_string(buff);
1822 }
1823 break;
1824 default:
1825 err_string("Illegal comparison operator type.");
1826 break;
1827 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001828 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00001829 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001830 res = (validate_ntype(CHILD(tree, 0), NAME)
1831 && validate_ntype(CHILD(tree, 1), NAME)
1832 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
1833 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
1834 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
1835 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
1836 if (!res && !PyErr_Occurred())
1837 err_string("Unknown comparison operator.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001838 }
1839 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001840}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001841
1842
Guido van Rossum47478871996-08-21 14:32:37 +00001843static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001844validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001845{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001846 int j;
1847 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001848 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001849 && is_odd(nch)
1850 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001851
Guido van Rossum3d602e31996-07-21 02:33:56 +00001852 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001853 res = (validate_xor_expr(CHILD(tree, j))
1854 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001855
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001856 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001857}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001858
1859
Guido van Rossum47478871996-08-21 14:32:37 +00001860static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001861validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001862{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001863 int j;
1864 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001865 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001866 && is_odd(nch)
1867 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001868
Guido van Rossum3d602e31996-07-21 02:33:56 +00001869 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001870 res = (validate_circumflex(CHILD(tree, j - 1))
1871 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001872
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001873 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001874}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001875
1876
Guido van Rossum47478871996-08-21 14:32:37 +00001877static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001878validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001879{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001880 int pos;
1881 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001882 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001883 && is_odd(nch)
1884 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001885
Guido van Rossum3d602e31996-07-21 02:33:56 +00001886 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001887 res = (validate_ampersand(CHILD(tree, pos))
1888 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001889
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001890 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001891}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001892
1893
1894static int
Guido van Rossum3d602e31996-07-21 02:33:56 +00001895validate_chain_two_ops(tree, termvalid, op1, op2)
1896 node *tree;
1897 int (*termvalid)();
1898 int op1;
1899 int op2;
1900 {
1901 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001902 int nch = NCH(tree);
1903 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00001904 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001905
Guido van Rossum3d602e31996-07-21 02:33:56 +00001906 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001907 if (TYPE(CHILD(tree, pos)) != op1)
1908 res = validate_ntype(CHILD(tree, pos), op2);
1909 if (res)
1910 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001911 }
1912 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001913}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001914
1915
Guido van Rossum47478871996-08-21 14:32:37 +00001916static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001917validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001918{
1919 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001920 && validate_chain_two_ops(tree, validate_arith_expr,
1921 LEFTSHIFT, RIGHTSHIFT));
1922}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001923
1924
Guido van Rossum47478871996-08-21 14:32:37 +00001925static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001926validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001927{
1928 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001929 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
1930}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001931
1932
Guido van Rossum47478871996-08-21 14:32:37 +00001933static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001934validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001935{
1936 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001937 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001938 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00001939 && is_odd(nch)
1940 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001941
Guido van Rossum3d602e31996-07-21 02:33:56 +00001942 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001943 res = (((TYPE(CHILD(tree, pos)) == STAR)
1944 || (TYPE(CHILD(tree, pos)) == SLASH)
1945 || (TYPE(CHILD(tree, pos)) == PERCENT))
1946 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001947
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001948 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001949}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001950
1951
Guido van Rossum3d602e31996-07-21 02:33:56 +00001952/* factor:
1953 *
1954 * factor: ('+'|'-'|'~') factor | power
1955 */
Guido van Rossum47478871996-08-21 14:32:37 +00001956static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001957validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001958{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001959 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001960 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00001961 && (((nch == 2)
1962 && ((TYPE(CHILD(tree, 0)) == PLUS)
1963 || (TYPE(CHILD(tree, 0)) == MINUS)
1964 || (TYPE(CHILD(tree, 0)) == TILDE))
1965 && validate_factor(CHILD(tree, 1)))
1966 || ((nch == 1)
1967 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001968 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001969}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001970
1971
Guido van Rossum3d602e31996-07-21 02:33:56 +00001972/* power:
1973 *
1974 * power: atom trailer* ('**' factor)*
1975 */
Guido van Rossum47478871996-08-21 14:32:37 +00001976static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001977validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001978{
1979 int pos = 1;
1980 int nch = NCH(tree);
1981 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00001982 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001983
1984 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00001985 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001986 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001987 if (!is_even(nch - pos)) {
1988 err_string("Illegal number of nodes for 'power'.");
1989 return (0);
1990 }
1991 for ( ; res && (pos < (nch - 1)); pos += 2)
1992 res = (validate_doublestar(CHILD(tree, pos))
1993 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001994 }
1995 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001996}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001997
1998
Guido van Rossum47478871996-08-21 14:32:37 +00001999static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002000validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002001{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002002 int pos;
2003 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002004 int res = validate_ntype(tree, atom) && (nch >= 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002005
2006 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002007 switch (TYPE(CHILD(tree, 0))) {
2008 case LPAR:
2009 res = ((nch <= 3)
2010 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002011
Fred Drakeff9ea482000-04-19 13:54:15 +00002012 if (res && (nch == 3))
2013 res = validate_testlist(CHILD(tree, 1));
2014 break;
2015 case LSQB:
2016 res = ((nch <= 3)
2017 && validate_ntype(CHILD(tree, nch - 1), RSQB));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002018
Fred Drakeff9ea482000-04-19 13:54:15 +00002019 if (res && (nch == 3))
2020 res = validate_testlist(CHILD(tree, 1));
2021 break;
2022 case LBRACE:
2023 res = ((nch <= 3)
2024 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002025
Fred Drakeff9ea482000-04-19 13:54:15 +00002026 if (res && (nch == 3))
2027 res = validate_dictmaker(CHILD(tree, 1));
2028 break;
2029 case BACKQUOTE:
2030 res = ((nch == 3)
2031 && validate_testlist(CHILD(tree, 1))
2032 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2033 break;
2034 case NAME:
2035 case NUMBER:
2036 res = (nch == 1);
2037 break;
2038 case STRING:
2039 for (pos = 1; res && (pos < nch); ++pos)
2040 res = validate_ntype(CHILD(tree, pos), STRING);
2041 break;
2042 default:
2043 res = 0;
2044 break;
2045 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002046 }
2047 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002048}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002049
2050
Guido van Rossum3d602e31996-07-21 02:33:56 +00002051/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002052 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002053 *
2054 */
Guido van Rossum47478871996-08-21 14:32:37 +00002055static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002056validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002057{
2058 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002059 && validate_numnodes(tree, 5, "funcdef")
2060 && validate_name(CHILD(tree, 0), "def")
2061 && validate_ntype(CHILD(tree, 1), NAME)
2062 && validate_colon(CHILD(tree, 3))
2063 && validate_parameters(CHILD(tree, 2))
2064 && validate_suite(CHILD(tree, 4)));
2065}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002066
2067
Guido van Rossum47478871996-08-21 14:32:37 +00002068static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002069validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002070{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002071 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002072 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002073 && ((nch == 3) || (nch == 4))
2074 && validate_name(CHILD(tree, 0), "lambda")
2075 && validate_colon(CHILD(tree, nch - 2))
2076 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002077
Guido van Rossum3d602e31996-07-21 02:33:56 +00002078 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002079 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002080 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002081 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002082
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002083 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002084}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002085
2086
Guido van Rossum3d602e31996-07-21 02:33:56 +00002087/* arglist:
2088 *
Fred Drakee7ab64e2000-04-25 04:14:46 +00002089 * (argument ',')* (argument* [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002090 */
Guido van Rossum47478871996-08-21 14:32:37 +00002091static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002092validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002093{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002094 int nch = NCH(tree);
2095 int i, ok;
2096 node *last;
2097
2098 if (nch <= 0)
2099 /* raise the right error from having an invalid number of children */
2100 return validate_numnodes(tree, nch + 1, "arglist");
2101
2102 last = CHILD(tree, nch - 1);
2103 if (TYPE(last) == test) {
2104 /* Extended call syntax introduced in Python 1.6 has been used;
2105 * validate and strip that off and continue;
2106 * adjust nch to perform the cut, and ensure resulting nch is even
2107 * (validation of the first part doesn't require that).
2108 */
2109 if (nch < 2) {
2110 validate_numnodes(tree, nch + 1, "arglist");
2111 return 0;
2112 }
2113 ok = validate_test(last);
2114 if (ok) {
2115 node *prev = CHILD(tree, nch - 2);
2116 /* next must be '*' or '**' */
2117 if (validate_doublestar(prev)) {
2118 nch -= 2;
2119 if (nch >= 3) {
2120 /* may include: '*' test ',' */
2121 last = CHILD(tree, nch - 1);
2122 prev = CHILD(tree, nch - 2);
2123 if (TYPE(prev) == test) {
2124 ok = validate_comma(last)
2125 && validate_test(prev)
2126 && validate_star(CHILD(tree, nch - 3));
2127 if (ok)
2128 nch -= 3;
2129 }
2130 /* otherwise, nothing special */
2131 }
2132 }
2133 else {
2134 /* must be only: '*' test */
2135 PyErr_Clear();
2136 ok = validate_star(prev);
2137 nch -= 2;
2138 }
2139 if (ok && is_odd(nch)) {
2140 /* Illegal number of nodes before extended call syntax;
2141 * validation of the "normal" arguments does not require
2142 * a trailing comma, but requiring an even number of
2143 * children will effect the same requirement.
2144 */
2145 return validate_numnodes(tree, nch + 1, "arglist");
2146 }
2147 }
2148 }
2149 /* what remains must be: (argument ",")* [argument [","]] */
2150 i = 0;
2151 while (ok && nch - i >= 2) {
2152 ok = validate_argument(CHILD(tree, i))
2153 && validate_comma(CHILD(tree, i + 1));
2154 i += 2;
2155 }
2156 if (ok && i < nch) {
2157 ok = validate_comma(CHILD(tree, i));
2158 ++i;
2159 }
2160 if (i != nch) {
2161 /* internal error! */
2162 ok = 0;
2163 err_string("arglist: internal error; nch != i");
2164 }
2165 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002166}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002167
2168
2169
2170/* argument:
2171 *
2172 * [test '='] test
2173 */
Guido van Rossum47478871996-08-21 14:32:37 +00002174static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002175validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002176{
2177 int nch = NCH(tree);
2178 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002179 && ((nch == 1) || (nch == 3))
2180 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002181
2182 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002183 res = (validate_equal(CHILD(tree, 1))
2184 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002185
2186 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002187}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002188
2189
2190
2191/* trailer:
2192 *
Guido van Rossum47478871996-08-21 14:32:37 +00002193 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002194 */
Guido van Rossum47478871996-08-21 14:32:37 +00002195static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002196validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002197{
2198 int nch = NCH(tree);
2199 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002200
2201 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002202 switch (TYPE(CHILD(tree, 0))) {
2203 case LPAR:
2204 res = validate_rparen(CHILD(tree, nch - 1));
2205 if (res && (nch == 3))
2206 res = validate_arglist(CHILD(tree, 1));
2207 break;
2208 case LSQB:
2209 res = (validate_numnodes(tree, 3, "trailer")
2210 && validate_subscriptlist(CHILD(tree, 1))
2211 && validate_ntype(CHILD(tree, 2), RSQB));
2212 break;
2213 case DOT:
2214 res = (validate_numnodes(tree, 2, "trailer")
2215 && validate_ntype(CHILD(tree, 1), NAME));
2216 break;
2217 default:
2218 res = 0;
2219 break;
2220 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002221 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002222 else {
2223 (void) validate_numnodes(tree, 2, "trailer");
2224 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002225 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002226}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002227
2228
Guido van Rossum47478871996-08-21 14:32:37 +00002229/* subscriptlist:
2230 *
2231 * subscript (',' subscript)* [',']
2232 */
2233static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002234validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002235{
2236 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002237 validate_subscript, "subscriptlist"));
2238}
Guido van Rossum47478871996-08-21 14:32:37 +00002239
2240
Guido van Rossum3d602e31996-07-21 02:33:56 +00002241/* subscript:
2242 *
Guido van Rossum47478871996-08-21 14:32:37 +00002243 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002244 */
Guido van Rossum47478871996-08-21 14:32:37 +00002245static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002246validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002247{
Guido van Rossum47478871996-08-21 14:32:37 +00002248 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002249 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002250 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002251
Guido van Rossum47478871996-08-21 14:32:37 +00002252 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002253 if (!PyErr_Occurred())
2254 err_string("invalid number of arguments for subscript node");
2255 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002256 }
Guido van Rossum47478871996-08-21 14:32:37 +00002257 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002258 /* take care of ('.' '.' '.') possibility */
2259 return (validate_numnodes(tree, 3, "subscript")
2260 && validate_dot(CHILD(tree, 0))
2261 && validate_dot(CHILD(tree, 1))
2262 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002263 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002264 if (TYPE(CHILD(tree, 0)) == test)
2265 res = validate_test(CHILD(tree, 0));
2266 else
2267 res = validate_colon(CHILD(tree, 0));
2268 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002269 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002270 /* Must be [test] ':' [test] [sliceop],
2271 * but at least one of the optional components will
2272 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002273 */
2274 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002275 res = validate_test(CHILD(tree, 0));
2276 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002277 }
2278 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002279 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002280 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002281 int rem = nch - ++offset;
2282 if (rem) {
2283 if (TYPE(CHILD(tree, offset)) == test) {
2284 res = validate_test(CHILD(tree, offset));
2285 ++offset;
2286 --rem;
2287 }
2288 if (res && rem)
2289 res = validate_sliceop(CHILD(tree, offset));
2290 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002291 }
2292 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002293}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002294
2295
Guido van Rossum47478871996-08-21 14:32:37 +00002296static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002297validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002298{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002299 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002300 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002301 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002302 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002303 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002304 }
Guido van Rossum47478871996-08-21 14:32:37 +00002305 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002306 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002307 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002308 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002309
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002310 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002311}
Guido van Rossum47478871996-08-21 14:32:37 +00002312
2313
2314static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002315validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002316{
2317 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002318 validate_expr, "exprlist"));
2319}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002320
2321
Guido van Rossum47478871996-08-21 14:32:37 +00002322static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002323validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002324{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002325 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002326 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002327 && (nch >= 3)
2328 && validate_test(CHILD(tree, 0))
2329 && validate_colon(CHILD(tree, 1))
2330 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002331
Guido van Rossum3d602e31996-07-21 02:33:56 +00002332 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002333 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002334 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002335 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002336
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002337 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002338 int pos = 3;
2339 /* ( ',' test ':' test )* */
2340 while (res && (pos < nch)) {
2341 res = (validate_comma(CHILD(tree, pos))
2342 && validate_test(CHILD(tree, pos + 1))
2343 && validate_colon(CHILD(tree, pos + 2))
2344 && validate_test(CHILD(tree, pos + 3)));
2345 pos += 4;
2346 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002347 }
2348 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002349}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002350
2351
Guido van Rossum47478871996-08-21 14:32:37 +00002352static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002353validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002354{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002355 int pos;
2356 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002357 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002358 && (nch >= 2)
2359 && validate_testlist(CHILD(tree, 0))
2360 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002361
Guido van Rossum3d602e31996-07-21 02:33:56 +00002362 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002363 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002364
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002365 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002366}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002367
2368
Guido van Rossum47478871996-08-21 14:32:37 +00002369static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002370validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002371{
Fred Drakeff9ea482000-04-19 13:54:15 +00002372 int nch = 0; /* num. children on current node */
2373 int res = 1; /* result value */
2374 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002375
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002376 while (res & (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002377 nch = NCH(tree);
2378 next = 0;
2379 switch (TYPE(tree)) {
2380 /*
2381 * Definition nodes.
2382 */
2383 case funcdef:
2384 res = validate_funcdef(tree);
2385 break;
2386 case classdef:
2387 res = validate_class(tree);
2388 break;
2389 /*
2390 * "Trivial" parse tree nodes.
2391 * (Why did I call these trivial?)
2392 */
2393 case stmt:
2394 res = validate_stmt(tree);
2395 break;
2396 case small_stmt:
2397 /*
2398 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
2399 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2400 */
2401 res = validate_small_stmt(tree);
2402 break;
2403 case flow_stmt:
2404 res = (validate_numnodes(tree, 1, "flow_stmt")
2405 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2406 || (TYPE(CHILD(tree, 0)) == continue_stmt)
2407 || (TYPE(CHILD(tree, 0)) == return_stmt)
2408 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2409 if (res)
2410 next = CHILD(tree, 0);
2411 else if (nch == 1)
2412 err_string("Illegal flow_stmt type.");
2413 break;
2414 /*
2415 * Compound statements.
2416 */
2417 case simple_stmt:
2418 res = validate_simple_stmt(tree);
2419 break;
2420 case compound_stmt:
2421 res = validate_compound_stmt(tree);
2422 break;
2423 /*
2424 * Fundemental statements.
2425 */
2426 case expr_stmt:
2427 res = validate_expr_stmt(tree);
2428 break;
2429 case print_stmt:
2430 res = validate_print_stmt(tree);
2431 break;
2432 case del_stmt:
2433 res = validate_del_stmt(tree);
2434 break;
2435 case pass_stmt:
2436 res = (validate_numnodes(tree, 1, "pass")
2437 && validate_name(CHILD(tree, 0), "pass"));
2438 break;
2439 case break_stmt:
2440 res = (validate_numnodes(tree, 1, "break")
2441 && validate_name(CHILD(tree, 0), "break"));
2442 break;
2443 case continue_stmt:
2444 res = (validate_numnodes(tree, 1, "continue")
2445 && validate_name(CHILD(tree, 0), "continue"));
2446 break;
2447 case return_stmt:
2448 res = validate_return_stmt(tree);
2449 break;
2450 case raise_stmt:
2451 res = validate_raise_stmt(tree);
2452 break;
2453 case import_stmt:
2454 res = validate_import_stmt(tree);
2455 break;
2456 case global_stmt:
2457 res = validate_global_stmt(tree);
2458 break;
2459 case exec_stmt:
2460 res = validate_exec_stmt(tree);
2461 break;
2462 case assert_stmt:
2463 res = validate_assert_stmt(tree);
2464 break;
2465 case if_stmt:
2466 res = validate_if(tree);
2467 break;
2468 case while_stmt:
2469 res = validate_while(tree);
2470 break;
2471 case for_stmt:
2472 res = validate_for(tree);
2473 break;
2474 case try_stmt:
2475 res = validate_try(tree);
2476 break;
2477 case suite:
2478 res = validate_suite(tree);
2479 break;
2480 /*
2481 * Expression nodes.
2482 */
2483 case testlist:
2484 res = validate_testlist(tree);
2485 break;
2486 case test:
2487 res = validate_test(tree);
2488 break;
2489 case and_test:
2490 res = validate_and_test(tree);
2491 break;
2492 case not_test:
2493 res = validate_not_test(tree);
2494 break;
2495 case comparison:
2496 res = validate_comparison(tree);
2497 break;
2498 case exprlist:
2499 res = validate_exprlist(tree);
2500 break;
2501 case comp_op:
2502 res = validate_comp_op(tree);
2503 break;
2504 case expr:
2505 res = validate_expr(tree);
2506 break;
2507 case xor_expr:
2508 res = validate_xor_expr(tree);
2509 break;
2510 case and_expr:
2511 res = validate_and_expr(tree);
2512 break;
2513 case shift_expr:
2514 res = validate_shift_expr(tree);
2515 break;
2516 case arith_expr:
2517 res = validate_arith_expr(tree);
2518 break;
2519 case term:
2520 res = validate_term(tree);
2521 break;
2522 case factor:
2523 res = validate_factor(tree);
2524 break;
2525 case power:
2526 res = validate_power(tree);
2527 break;
2528 case atom:
2529 res = validate_atom(tree);
2530 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002531
Fred Drakeff9ea482000-04-19 13:54:15 +00002532 default:
2533 /* Hopefully never reached! */
2534 err_string("Unrecogniged node type.");
2535 res = 0;
2536 break;
2537 }
2538 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002539 }
2540 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002541}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002542
2543
Guido van Rossum47478871996-08-21 14:32:37 +00002544static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002545validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002546{
2547 int res = validate_eval_input(tree);
2548
2549 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002550 err_string("Could not validate expression tuple.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002551
2552 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002553}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002554
2555
Guido van Rossum3d602e31996-07-21 02:33:56 +00002556/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002557 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002558 */
Guido van Rossum47478871996-08-21 14:32:37 +00002559static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002560validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002561{
2562 int j = 0;
2563 int nch = NCH(tree) - 1;
2564 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002565 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002566
Guido van Rossum3d602e31996-07-21 02:33:56 +00002567 for ( ; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002568 if (TYPE(CHILD(tree, j)) == stmt)
2569 res = validate_stmt(CHILD(tree, j));
2570 else
2571 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002572 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002573 /* This stays in to prevent any internal failues from getting to the
2574 * user. Hopefully, this won't be needed. If a user reports getting
2575 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002576 */
2577 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002578 err_string("VALIDATION FAILURE: report this to the maintainer!.");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002579
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002580 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002581}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002582
2583
Fred Drake43f8f9b1998-04-13 16:25:46 +00002584static PyObject*
2585pickle_constructor = NULL;
2586
2587
2588static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002589parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002590{
Fred Drake268397f1998-04-29 14:16:32 +00002591 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002592 PyObject *result = NULL;
2593 PyObject *ast = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002594 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002595
2596 if (PyArg_ParseTuple(args, "O!:_pickler", &PyAST_Type, &ast)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002597 PyObject *newargs;
2598 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002599
Fred Drake2a6875e1999-09-20 22:32:18 +00002600 if ((empty_dict = PyDict_New()) == NULL)
2601 goto finally;
2602 if ((newargs = Py_BuildValue("Oi", ast, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002603 goto finally;
2604 tuple = parser_ast2tuple((PyAST_Object*)NULL, newargs, empty_dict);
2605 if (tuple != NULL) {
2606 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2607 Py_DECREF(tuple);
2608 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002609 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002610 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002611 }
2612 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002613 Py_XDECREF(empty_dict);
2614
Fred Drake43f8f9b1998-04-13 16:25:46 +00002615 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002616}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002617
2618
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002619/* Functions exported by this module. Most of this should probably
2620 * be converted into an AST object with methods, but that is better
2621 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002622 * We'd really have to write a wrapper around it all anyway to allow
2623 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002624 */
2625static PyMethodDef parser_functions[] = {
Fred Drakeff9ea482000-04-19 13:54:15 +00002626 {"ast2tuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
2627 "Creates a tuple-tree representation of an AST."},
2628 {"ast2list", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
2629 "Creates a list-tree representation of an AST."},
2630 {"compileast", (PyCFunction)parser_compileast, PUBLIC_METHOD_TYPE,
2631 "Compiles an AST object into a code object."},
2632 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
2633 "Creates an AST object from an expression."},
2634 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
2635 "Determines if an AST object was created from an expression."},
2636 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
2637 "Determines if an AST object was created from a suite."},
2638 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
2639 "Creates an AST object from a suite."},
2640 {"sequence2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
2641 "Creates an AST object from a tree representation."},
2642 {"tuple2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
2643 "Creates an AST object from a tree representation."},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002644
Fred Drake43f8f9b1998-04-13 16:25:46 +00002645 /* private stuff: support pickle module */
Fred Drakeff9ea482000-04-19 13:54:15 +00002646 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Fred Drake43f8f9b1998-04-13 16:25:46 +00002647 "Returns the pickle magic to allow ast objects to be pickled."},
2648
Fred Drake268397f1998-04-29 14:16:32 +00002649 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002650 };
2651
2652
Guido van Rossum3886bb61998-12-04 18:50:17 +00002653DL_EXPORT(void)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002654initparser()
2655 {
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002656 PyObject* module;
2657 PyObject* dict;
Fred Drakeff9ea482000-04-19 13:54:15 +00002658
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002659 PyAST_Type.ob_type = &PyType_Type;
2660 module = Py_InitModule("parser", parser_functions);
2661 dict = PyModule_GetDict(module);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002662
Fred Drake7a15ba51999-09-09 14:21:52 +00002663 if (parser_error == 0)
2664 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
2665 else
2666 puts("parser module initialized more than once!");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002667
2668 if ((parser_error == 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002669 || (PyDict_SetItemString(dict, "ParserError", parser_error) != 0)) {
2670 /*
2671 * This is serious.
2672 */
2673 Py_FatalError("can't define parser.ParserError");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002674 }
2675 /*
2676 * Nice to have, but don't cry if we fail.
2677 */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002678 Py_INCREF(&PyAST_Type);
2679 PyDict_SetItemString(dict, "ASTType", (PyObject*)&PyAST_Type);
2680
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002681 PyDict_SetItemString(dict, "__copyright__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002682 PyString_FromString(parser_copyright_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002683 PyDict_SetItemString(dict, "__doc__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002684 PyString_FromString(parser_doc_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002685 PyDict_SetItemString(dict, "__version__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002686 PyString_FromString(parser_version_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002687
Fred Drake43f8f9b1998-04-13 16:25:46 +00002688 /* register to support pickling */
2689 module = PyImport_ImportModule("copy_reg");
2690 if (module != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002691 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002692
Fred Drakeff9ea482000-04-19 13:54:15 +00002693 func = PyObject_GetAttrString(module, "pickle");
2694 pickle_constructor = PyDict_GetItemString(dict, "sequence2ast");
2695 pickler = PyDict_GetItemString(dict, "_pickler");
2696 Py_XINCREF(pickle_constructor);
2697 if ((func != NULL) && (pickle_constructor != NULL)
2698 && (pickler != NULL)) {
2699 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002700
Fred Drakeff9ea482000-04-19 13:54:15 +00002701 res = PyObject_CallFunction(
2702 func, "OOO", &PyAST_Type, pickler, pickle_constructor);
2703 Py_XDECREF(res);
2704 }
2705 Py_XDECREF(func);
2706 Py_DECREF(module);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002707 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002708}