blob: 6ff98ded901fc58301ec50ac43cd95dced724078 [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000026 */
27
Fred Drakeff9ea482000-04-19 13:54:15 +000028#include "Python.h" /* general Python API */
29#include "graminit.h" /* symbols defined in the grammar */
30#include "node.h" /* internal parser structure */
31#include "token.h" /* token definitions */
32 /* ISTERMINAL() / ISNONTERMINAL() */
33#include "compile.h" /* PyNode_Compile() */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000034
Fred Drake268397f1998-04-29 14:16:32 +000035#ifdef lint
36#include <note.h>
37#else
38#define NOTE(x)
39#endif
40
Guido van Rossum19efc5f1998-04-28 16:10:19 +000041#ifdef macintosh
Fred Drakeff9ea482000-04-19 13:54:15 +000042char *strdup(char *);
Guido van Rossum19efc5f1998-04-28 16:10:19 +000043#endif
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000044
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000045/* String constants used to initialize module attributes.
46 *
47 */
48static char*
49parser_copyright_string
Guido van Rossum2a288461996-08-21 21:55:43 +000050= "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
51University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
52Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
53Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000054
55
56static char*
57parser_doc_string
58= "This is an interface to Python's internal parser.";
59
60static char*
Fred Drakecff283c2000-08-21 22:24:43 +000061parser_version_string = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000062
63
Fred Drakeff9ea482000-04-19 13:54:15 +000064typedef PyObject* (*SeqMaker) (int length);
65typedef int (*SeqInserter) (PyObject* sequence,
66 int index,
67 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000068
Thomas Wouters7e474022000-07-16 12:04:32 +000069/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000070 * original copyright statement is included below, and continues to apply
71 * in full to the function immediately following. All other material is
72 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
73 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000074 * new naming conventions. Added arguments to provide support for creating
75 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000076 */
77
Guido van Rossum52f2c051993-11-10 12:53:24 +000078
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000079static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000080node2tuple(node *n, /* node to convert */
81 SeqMaker mkseq, /* create sequence */
82 SeqInserter addelem, /* func. to add elem. in seq. */
83 int lineno) /* include line numbers? */
Guido van Rossum47478871996-08-21 14:32:37 +000084{
Guido van Rossum3d602e31996-07-21 02:33:56 +000085 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000086 Py_INCREF(Py_None);
87 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000088 }
89 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000090 int i;
91 PyObject *v;
92 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000093
Fred Drakeff9ea482000-04-19 13:54:15 +000094 v = mkseq(1 + NCH(n));
95 if (v == NULL)
96 return (v);
97 w = PyInt_FromLong(TYPE(n));
98 if (w == NULL) {
99 Py_DECREF(v);
100 return ((PyObject*) NULL);
101 }
102 (void) addelem(v, 0, w);
103 for (i = 0; i < NCH(n); i++) {
104 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno);
105 if (w == NULL) {
106 Py_DECREF(v);
107 return ((PyObject*) NULL);
108 }
109 (void) addelem(v, i+1, w);
110 }
111 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000112 }
113 else if (ISTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000114 PyObject *result = mkseq(2 + lineno);
115 if (result != NULL) {
116 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
117 (void) addelem(result, 1, PyString_FromString(STR(n)));
118 if (lineno == 1)
119 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
120 }
121 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000122 }
123 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000124 PyErr_SetString(PyExc_SystemError,
125 "unrecognized parse tree node type");
126 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000127 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000128}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000129/*
130 * End of material copyrighted by Stichting Mathematisch Centrum.
131 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000132
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000133
134
135/* There are two types of intermediate objects we're interested in:
136 * 'eval' and 'exec' types. These constants can be used in the ast_type
137 * field of the object type to identify which any given object represents.
138 * These should probably go in an external header to allow other extensions
139 * to use them, but then, we really should be using C++ too. ;-)
140 *
Guido van Rossum3d602e31996-07-21 02:33:56 +0000141 * The PyAST_FRAGMENT type is not currently supported. Maybe not useful?
142 * Haven't decided yet.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000143 */
144
Fred Drakeff9ea482000-04-19 13:54:15 +0000145#define PyAST_EXPR 1
146#define PyAST_SUITE 2
147#define PyAST_FRAGMENT 3
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000148
149
150/* These are the internal objects and definitions required to implement the
151 * AST type. Most of the internal names are more reminiscent of the 'old'
152 * naming style, but the code uses the new naming convention.
153 */
154
155static PyObject*
156parser_error = 0;
157
158
159typedef struct _PyAST_Object {
Fred Drakeff9ea482000-04-19 13:54:15 +0000160 PyObject_HEAD /* standard object header */
161 node* ast_node; /* the node* returned by the parser */
162 int ast_type; /* EXPR or SUITE ? */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000163} PyAST_Object;
164
165
Fred Drake268397f1998-04-29 14:16:32 +0000166staticforward void
Fred Drakeff9ea482000-04-19 13:54:15 +0000167parser_free(PyAST_Object *ast);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000168
Fred Drake268397f1998-04-29 14:16:32 +0000169staticforward int
Fred Drakeff9ea482000-04-19 13:54:15 +0000170parser_compare(PyAST_Object *left, PyAST_Object *right);
Fred Drake268397f1998-04-29 14:16:32 +0000171
172staticforward PyObject *
Fred Drakeff9ea482000-04-19 13:54:15 +0000173parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000174
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000175
Fred Drake268397f1998-04-29 14:16:32 +0000176static
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000177PyTypeObject PyAST_Type = {
Guido van Rossum3c8c5981998-05-29 02:58:20 +0000178 PyObject_HEAD_INIT(NULL)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000179 0,
Fred Drakeff9ea482000-04-19 13:54:15 +0000180 "ast", /* tp_name */
181 (int) sizeof(PyAST_Object), /* tp_basicsize */
182 0, /* tp_itemsize */
183 (destructor)parser_free, /* tp_dealloc */
184 0, /* tp_print */
185 parser_getattr, /* tp_getattr */
186 0, /* tp_setattr */
187 (cmpfunc)parser_compare, /* tp_compare */
188 0, /* tp_repr */
189 0, /* tp_as_number */
190 0, /* tp_as_sequence */
191 0, /* tp_as_mapping */
192 0, /* tp_hash */
193 0, /* tp_call */
194 0, /* tp_str */
195 0, /* tp_getattro */
196 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000197
198 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000199 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000200
Fred Drakeff9ea482000-04-19 13:54:15 +0000201 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000202
203 /* __doc__ */
204 "Intermediate representation of a Python parse tree."
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000205}; /* PyAST_Type */
206
207
208static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000209parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000210{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000211 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000212
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000213 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000214 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000215
216 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000217 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000218
219 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000220 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000221
222 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000223 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000224
225 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000226 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000227
228 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000229 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000230
Fred Drakeff9ea482000-04-19 13:54:15 +0000231 if (v != 0)
232 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000233 }
234 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000235}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000236
237
238/* int parser_compare(PyAST_Object* left, PyAST_Object* right)
239 *
240 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
241 * This really just wraps a call to parser_compare_nodes() with some easy
242 * checks and protection code.
243 *
244 */
245static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000246parser_compare(PyAST_Object *left, PyAST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000247{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000248 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000249 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000250
251 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000252 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000253
254 return (parser_compare_nodes(left->ast_node, right->ast_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000255}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000256
257
258/* parser_newastobject(node* ast)
259 *
260 * Allocates a new Python object representing an AST. This is simply the
261 * 'wrapper' object that holds a node* and allows it to be passed around in
262 * Python code.
263 *
264 */
265static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000266parser_newastobject(node *ast, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000267{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000268 PyAST_Object* o = PyObject_New(PyAST_Object, &PyAST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000269
270 if (o != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000271 o->ast_node = ast;
272 o->ast_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000273 }
Fred Drake268397f1998-04-29 14:16:32 +0000274 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000275 PyNode_Free(ast);
Fred Drake268397f1998-04-29 14:16:32 +0000276 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000277 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000278}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000279
280
281/* void parser_free(PyAST_Object* ast)
282 *
283 * This is called by a del statement that reduces the reference count to 0.
284 *
285 */
286static void
Fred Drakeff9ea482000-04-19 13:54:15 +0000287parser_free(PyAST_Object *ast)
Guido van Rossum47478871996-08-21 14:32:37 +0000288{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000289 PyNode_Free(ast->ast_node);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000290 PyObject_Del(ast);
Fred Drakeff9ea482000-04-19 13:54:15 +0000291}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000292
293
Fred Drake2a6875e1999-09-20 22:32:18 +0000294/* parser_ast2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000295 *
296 * This provides conversion from a node* to a tuple object that can be
297 * returned to the Python-level caller. The AST object is not modified.
298 *
299 */
300static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000301parser_ast2tuple(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000302{
Guido van Rossum47478871996-08-21 14:32:37 +0000303 PyObject *line_option = 0;
304 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000305 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000306
Fred Drake7a15ba51999-09-09 14:21:52 +0000307 static char *keywords[] = {"ast", "line_info", NULL};
308
Fred Drake268397f1998-04-29 14:16:32 +0000309 if (self == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000310 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:ast2tuple", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000311 &PyAST_Type, &self, &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000312 }
Fred Drake503d8d61998-04-13 18:45:18 +0000313 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000314 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:totuple", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000315 &line_option);
Fred Drake268397f1998-04-29 14:16:32 +0000316 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000317 int lineno = 0;
318 if (line_option != NULL) {
319 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
320 }
321 /*
322 * Convert AST into a tuple representation. Use Guido's function,
323 * since it's known to work already.
324 */
325 res = node2tuple(((PyAST_Object*)self)->ast_node,
326 PyTuple_New, PyTuple_SetItem, lineno);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000327 }
328 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000329}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000330
331
Fred Drake2a6875e1999-09-20 22:32:18 +0000332/* parser_ast2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000333 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000334 * This provides conversion from a node* to a list object that can be
Guido van Rossum47478871996-08-21 14:32:37 +0000335 * returned to the Python-level caller. The AST object is not modified.
336 *
337 */
338static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000339parser_ast2list(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000340{
Guido van Rossum47478871996-08-21 14:32:37 +0000341 PyObject *line_option = 0;
342 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000343 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000344
Fred Drake7a15ba51999-09-09 14:21:52 +0000345 static char *keywords[] = {"ast", "line_info", NULL};
346
Fred Drake503d8d61998-04-13 18:45:18 +0000347 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000348 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:ast2list", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000349 &PyAST_Type, &self, &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000350 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000351 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:tolist", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000352 &line_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000353 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000354 int lineno = 0;
355 if (line_option != 0) {
356 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
357 }
358 /*
359 * Convert AST into a tuple representation. Use Guido's function,
360 * since it's known to work already.
361 */
362 res = node2tuple(self->ast_node,
363 PyList_New, PyList_SetItem, lineno);
Guido van Rossum47478871996-08-21 14:32:37 +0000364 }
365 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000366}
Guido van Rossum47478871996-08-21 14:32:37 +0000367
368
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000369/* parser_compileast(PyObject* self, PyObject* args)
370 *
371 * This function creates code objects from the parse tree represented by
372 * the passed-in data object. An optional file name is passed in as well.
373 *
374 */
375static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000376parser_compileast(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000377{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000378 PyObject* res = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000379 char* str = "<ast>";
Fred Drake503d8d61998-04-13 18:45:18 +0000380 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000381
Fred Drake7a15ba51999-09-09 14:21:52 +0000382 static char *keywords[] = {"ast", "filename", NULL};
383
Fred Drake503d8d61998-04-13 18:45:18 +0000384 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000385 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compileast", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000386 &PyAST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000387 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000388 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000389 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000390
391 if (ok)
Fred Drakeff9ea482000-04-19 13:54:15 +0000392 res = (PyObject *)PyNode_Compile(self->ast_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000393
394 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000395}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000396
397
398/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
399 * PyObject* parser_issuite(PyObject* self, PyObject* args)
400 *
401 * Checks the passed-in AST object to determine if it is an expression or
402 * a statement suite, respectively. The return is a Python truth value.
403 *
404 */
405static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000406parser_isexpr(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000407{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000408 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000409 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000410
Fred Drake7a15ba51999-09-09 14:21:52 +0000411 static char *keywords[] = {"ast", NULL};
412
Fred Drake503d8d61998-04-13 18:45:18 +0000413 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000414 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000415 &PyAST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000416 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000417 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000418
419 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000420 /* Check to see if the AST represents an expression or not. */
421 res = (self->ast_type == PyAST_EXPR) ? Py_True : Py_False;
422 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000423 }
424 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000425}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000426
427
428static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000429parser_issuite(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000430{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000431 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000432 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000433
Fred Drake7a15ba51999-09-09 14:21:52 +0000434 static char *keywords[] = {"ast", NULL};
435
Fred Drake503d8d61998-04-13 18:45:18 +0000436 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000437 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000438 &PyAST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000439 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000440 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000441
442 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000443 /* Check to see if the AST represents an expression or not. */
444 res = (self->ast_type == PyAST_EXPR) ? Py_False : Py_True;
445 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000446 }
447 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000448}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000449
450
Fred Drake7a15ba51999-09-09 14:21:52 +0000451#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
452
Fred Drake503d8d61998-04-13 18:45:18 +0000453static PyMethodDef
454parser_methods[] = {
Fred Drakeff9ea482000-04-19 13:54:15 +0000455 {"compile", (PyCFunction)parser_compileast, PUBLIC_METHOD_TYPE,
456 "Compile this AST object into a code object."},
457 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
458 "Determines if this AST object was created from an expression."},
459 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
460 "Determines if this AST object was created from a suite."},
461 {"tolist", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
462 "Creates a list-tree representation of this AST."},
463 {"totuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
464 "Creates a tuple-tree representation of this AST."},
Fred Drake503d8d61998-04-13 18:45:18 +0000465
Fred Drake268397f1998-04-29 14:16:32 +0000466 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000467};
468
Fred Drake503d8d61998-04-13 18:45:18 +0000469
470static PyObject*
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000471parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000472{
Fred Drake503d8d61998-04-13 18:45:18 +0000473 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000474}
Fred Drake503d8d61998-04-13 18:45:18 +0000475
476
Guido van Rossum3d602e31996-07-21 02:33:56 +0000477/* err_string(char* message)
478 *
479 * Sets the error string for an exception of type ParserError.
480 *
481 */
482static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000483err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000484{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000485 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000486}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000487
488
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000489/* PyObject* parser_do_parse(PyObject* args, int type)
490 *
491 * Internal function to actually execute the parse and return the result if
492 * successful, or set an exception if not.
493 *
494 */
495static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000496parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000497{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000498 char* string = 0;
499 PyObject* res = 0;
500
Fred Drake7a15ba51999-09-09 14:21:52 +0000501 static char *keywords[] = {"source", NULL};
502
503 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000504 node* n = PyParser_SimpleParseString(string,
505 (type == PyAST_EXPR)
506 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000507
Fred Drakeff9ea482000-04-19 13:54:15 +0000508 if (n != 0)
509 res = parser_newastobject(n, type);
510 else
Fred Drake661ea262000-10-24 19:57:45 +0000511 err_string("could not parse string");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000512 }
513 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000514}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000515
516
517/* PyObject* parser_expr(PyObject* self, PyObject* args)
518 * PyObject* parser_suite(PyObject* self, PyObject* args)
519 *
520 * External interfaces to the parser itself. Which is called determines if
521 * the parser attempts to recognize an expression ('eval' form) or statement
522 * suite ('exec' form). The real work is done by parser_do_parse() above.
523 *
524 */
525static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000526parser_expr(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000527{
Fred Drake268397f1998-04-29 14:16:32 +0000528 NOTE(ARGUNUSED(self))
Fred Drake7a15ba51999-09-09 14:21:52 +0000529 return (parser_do_parse(args, kw, "s:expr", PyAST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000530}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000531
532
533static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000534parser_suite(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000535{
Fred Drake268397f1998-04-29 14:16:32 +0000536 NOTE(ARGUNUSED(self))
Fred Drake7a15ba51999-09-09 14:21:52 +0000537 return (parser_do_parse(args, kw, "s:suite", PyAST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000538}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000539
540
541
542/* This is the messy part of the code. Conversion from a tuple to an AST
543 * object requires that the input tuple be valid without having to rely on
544 * catching an exception from the compiler. This is done to allow the
545 * compiler itself to remain fast, since most of its input will come from
546 * the parser directly, and therefore be known to be syntactically correct.
547 * This validation is done to ensure that we don't core dump the compile
548 * phase, returning an exception instead.
549 *
550 * Two aspects can be broken out in this code: creating a node tree from
551 * the tuple passed in, and verifying that it is indeed valid. It may be
552 * advantageous to expand the number of AST types to include funcdefs and
553 * lambdadefs to take advantage of the optimizer, recognizing those ASTs
554 * here. They are not necessary, and not quite as useful in a raw form.
555 * For now, let's get expressions and suites working reliably.
556 */
557
558
Fred Drakeff9ea482000-04-19 13:54:15 +0000559staticforward node* build_node_tree(PyObject *tuple);
560staticforward int validate_expr_tree(node *tree);
561staticforward int validate_file_input(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000562
563
564/* PyObject* parser_tuple2ast(PyObject* self, PyObject* args)
565 *
566 * This is the public function, called from the Python code. It receives a
567 * single tuple object from the caller, and creates an AST object if the
568 * tuple can be validated. It does this by checking the first code of the
569 * tuple, and, if acceptable, builds the internal representation. If this
570 * step succeeds, the internal representation is validated as fully as
571 * possible with the various validate_*() routines defined below.
572 *
573 * This function must be changed if support is to be added for PyAST_FRAGMENT
574 * AST objects.
575 *
576 */
577static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000578parser_tuple2ast(PyAST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000579{
Fred Drake268397f1998-04-29 14:16:32 +0000580 NOTE(ARGUNUSED(self))
Guido van Rossum47478871996-08-21 14:32:37 +0000581 PyObject *ast = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000582 PyObject *tuple;
583 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000584
Fred Drake7a15ba51999-09-09 14:21:52 +0000585 static char *keywords[] = {"sequence", NULL};
586
Fred Drake0ac9b072000-09-12 21:58:06 +0000587 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2ast", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000588 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000589 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000590 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000591 PyErr_SetString(PyExc_ValueError,
Fred Drake0ac9b072000-09-12 21:58:06 +0000592 "sequence2ast() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000593 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000594 }
595 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000596 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000597 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000598 tree = build_node_tree(tuple);
599 if (tree != 0) {
600 int start_sym = TYPE(tree);
601 if (start_sym == eval_input) {
602 /* Might be an eval form. */
603 if (validate_expr_tree(tree))
604 ast = parser_newastobject(tree, PyAST_EXPR);
Fred Drakeff9ea482000-04-19 13:54:15 +0000605 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000606 else if (start_sym == file_input) {
607 /* This looks like an exec form so far. */
608 if (validate_file_input(tree))
609 ast = parser_newastobject(tree, PyAST_SUITE);
610 }
611 else {
612 /* This is a fragment, at best. */
613 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000614 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000615 }
Guido van Rossum47478871996-08-21 14:32:37 +0000616 }
Guido van Rossum47478871996-08-21 14:32:37 +0000617 /* Make sure we throw an exception on all errors. We should never
618 * get this, but we'd do well to be sure something is done.
619 */
620 if ((ast == 0) && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +0000621 err_string("unspecified AST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000622
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000623 return (ast);
Fred Drakeff9ea482000-04-19 13:54:15 +0000624}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000625
626
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000627/* node* build_node_children()
628 *
629 * Iterate across the children of the current non-terminal node and build
630 * their structures. If successful, return the root of this portion of
631 * the tree, otherwise, 0. Any required exception will be specified already,
632 * and no memory will have been deallocated.
633 *
634 */
635static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000636build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000637{
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000638 int len = PyObject_Size(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000639 int i;
640
641 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000642 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000643 PyObject* elem = PySequence_GetItem(tuple, i);
644 int ok = elem != NULL;
645 long type = 0;
646 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000647
Fred Drakeff9ea482000-04-19 13:54:15 +0000648 if (ok)
649 ok = PySequence_Check(elem);
650 if (ok) {
651 PyObject *temp = PySequence_GetItem(elem, 0);
652 if (temp == NULL)
653 ok = 0;
654 else {
655 ok = PyInt_Check(temp);
656 if (ok)
657 type = PyInt_AS_LONG(temp);
658 Py_DECREF(temp);
659 }
660 }
661 if (!ok) {
662 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000663 Py_BuildValue("os", elem,
Fred Drakeff9ea482000-04-19 13:54:15 +0000664 "Illegal node construct."));
665 Py_XDECREF(elem);
666 return (0);
667 }
668 if (ISTERMINAL(type)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000669 int len = PyObject_Size(elem);
670 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000671
Fred Drake0ac9b072000-09-12 21:58:06 +0000672 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000673 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000674 return 0;
675 }
676 temp = PySequence_GetItem(elem, 1);
677 if (temp == NULL)
678 return 0;
679 if (!PyString_Check(temp)) {
680 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000681 "second item in terminal node must be a string,"
682 " found %s",
Fred Drake0ac9b072000-09-12 21:58:06 +0000683 ((PyTypeObject*)PyObject_Type(temp))->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000684 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000685 return 0;
686 }
687 if (len == 3) {
688 PyObject *o = PySequence_GetItem(elem, 2);
689 if (o != NULL) {
690 if (PyInt_Check(o))
691 *line_num = PyInt_AS_LONG(o);
692 else {
693 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000694 "third item in terminal node must be an"
695 " integer, found %s",
Fred Drake0ac9b072000-09-12 21:58:06 +0000696 ((PyTypeObject*)PyObject_Type(temp))->tp_name);
697 Py_DECREF(o);
698 Py_DECREF(temp);
699 return 0;
700 }
701 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000702 }
703 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000704 len = PyString_GET_SIZE(temp) + 1;
705 strn = (char *)PyMem_MALLOC(len);
706 if (strn != NULL)
707 (void) memcpy(strn, PyString_AS_STRING(temp), len);
708 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000709 }
710 else if (!ISNONTERMINAL(type)) {
711 /*
712 * It has to be one or the other; this is an error.
713 * Throw an exception.
714 */
715 PyErr_SetObject(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000716 Py_BuildValue("os", elem, "unknown node type."));
Fred Drakeff9ea482000-04-19 13:54:15 +0000717 Py_XDECREF(elem);
718 return (0);
719 }
720 PyNode_AddChild(root, type, strn, *line_num);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000721
Fred Drakeff9ea482000-04-19 13:54:15 +0000722 if (ISNONTERMINAL(type)) {
723 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000724
Fred Drakeff9ea482000-04-19 13:54:15 +0000725 if (new_child != build_node_children(elem, new_child, line_num)) {
726 Py_XDECREF(elem);
727 return (0);
728 }
729 }
730 else if (type == NEWLINE) { /* It's true: we increment the */
731 ++(*line_num); /* line number *after* the newline! */
732 }
733 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000734 }
735 return (root);
Fred Drakeff9ea482000-04-19 13:54:15 +0000736}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000737
738
739static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000740build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000741{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000742 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000743 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000744 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000745
Guido van Rossum47478871996-08-21 14:32:37 +0000746 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000747 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000748 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000749 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000750 /*
751 * The tuple is simple, but it doesn't start with a start symbol.
752 * Throw an exception now and be done with it.
753 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000754 tuple = Py_BuildValue("os", tuple,
755 "Illegal ast tuple; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000756 PyErr_SetObject(parser_error, tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000757 }
758 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000759 /*
760 * Not efficient, but that can be handled later.
761 */
762 int line_num = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000763
Fred Drakeff9ea482000-04-19 13:54:15 +0000764 res = PyNode_New(num);
765 if (res != build_node_children(tuple, res, &line_num)) {
766 PyNode_Free(res);
767 res = 0;
768 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000769 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000770 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000771 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000772 * NONTERMINAL, we can't use it. Not sure the implementation
773 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000774 */
775 PyErr_SetObject(parser_error,
Fred Drake0ac9b072000-09-12 21:58:06 +0000776 Py_BuildValue("os", tuple,
Fred Drakeff9ea482000-04-19 13:54:15 +0000777 "Illegal component tuple."));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000778
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000779 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000780}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000781
782
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000783/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000784 * Validation routines used within the validation section:
785 */
Fred Drakeff9ea482000-04-19 13:54:15 +0000786staticforward int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000787
Fred Drakeff9ea482000-04-19 13:54:15 +0000788#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
789#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
790#define validate_colon(ch) validate_terminal(ch, COLON, ":")
791#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
792#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
793#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
794#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
795#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
796#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
797#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
798#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
799#define validate_star(ch) validate_terminal(ch, STAR, "*")
800#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
801#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
802#define validate_dot(ch) validate_terminal(ch, DOT, ".")
803#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000804
Fred Drake0ac9b072000-09-12 21:58:06 +0000805#define VALIDATER(n) static int validate_##n(node *tree)
806
Fred Drakeff9ea482000-04-19 13:54:15 +0000807VALIDATER(node); VALIDATER(small_stmt);
808VALIDATER(class); VALIDATER(node);
809VALIDATER(parameters); VALIDATER(suite);
810VALIDATER(testlist); VALIDATER(varargslist);
811VALIDATER(fpdef); VALIDATER(fplist);
812VALIDATER(stmt); VALIDATER(simple_stmt);
813VALIDATER(expr_stmt); VALIDATER(power);
814VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000815VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000816VALIDATER(raise_stmt); VALIDATER(import_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000817VALIDATER(global_stmt); VALIDATER(list_if);
818VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000819VALIDATER(exec_stmt); VALIDATER(compound_stmt);
820VALIDATER(while); VALIDATER(for);
821VALIDATER(try); VALIDATER(except_clause);
822VALIDATER(test); VALIDATER(and_test);
823VALIDATER(not_test); VALIDATER(comparison);
824VALIDATER(comp_op); VALIDATER(expr);
825VALIDATER(xor_expr); VALIDATER(and_expr);
826VALIDATER(shift_expr); VALIDATER(arith_expr);
827VALIDATER(term); VALIDATER(factor);
828VALIDATER(atom); VALIDATER(lambdef);
829VALIDATER(trailer); VALIDATER(subscript);
830VALIDATER(subscriptlist); VALIDATER(sliceop);
831VALIDATER(exprlist); VALIDATER(dictmaker);
832VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000833VALIDATER(listmaker); VALIDATER(yield_stmt);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000834
Fred Drake0ac9b072000-09-12 21:58:06 +0000835#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000836
Fred Drakeff9ea482000-04-19 13:54:15 +0000837#define is_even(n) (((n) & 1) == 0)
838#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000839
840
841static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000842validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000843{
Fred Drake0ac9b072000-09-12 21:58:06 +0000844 if (TYPE(n) != t) {
845 PyErr_Format(parser_error, "Expected node type %d, got %d.",
846 t, TYPE(n));
847 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000848 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000849 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000850}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000851
852
Fred Drakee7ab64e2000-04-25 04:14:46 +0000853/* Verifies that the number of child nodes is exactly 'num', raising
854 * an exception if it isn't. The exception message does not indicate
855 * the exact number of nodes, allowing this to be used to raise the
856 * "right" exception when the wrong number of nodes is present in a
857 * specific variant of a statement's syntax. This is commonly used
858 * in that fashion.
859 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000860static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000861validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000862{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000863 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000864 PyErr_Format(parser_error,
865 "Illegal number of children for %s node.", name);
866 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000867 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000868 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000869}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000870
871
872static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000873validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000874{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000875 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000876 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000877
878 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000879 PyErr_Format(parser_error,
880 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000881 }
882 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000883}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000884
885
Guido van Rossum47478871996-08-21 14:32:37 +0000886/* X (',' X) [',']
887 */
888static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000889validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000890 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000891{
892 int nch = NCH(tree);
893 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000894 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000895
896 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000897 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000898 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000899 if (is_even(nch))
900 res = validate_comma(CHILD(tree, --nch));
901 if (res && nch > 1) {
902 int pos = 1;
903 for ( ; res && pos < nch; pos += 2)
904 res = (validate_comma(CHILD(tree, pos))
905 && vfunc(CHILD(tree, pos + 1)));
906 }
Guido van Rossum47478871996-08-21 14:32:37 +0000907 }
908 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000909}
Guido van Rossum47478871996-08-21 14:32:37 +0000910
911
Fred Drakecff283c2000-08-21 22:24:43 +0000912/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000913 *
914 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000915 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000916 */
Guido van Rossum47478871996-08-21 14:32:37 +0000917static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000918validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000919{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000920 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000921 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000922
Guido van Rossum3d602e31996-07-21 02:33:56 +0000923 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000924 res = (validate_name(CHILD(tree, 0), "class")
925 && validate_ntype(CHILD(tree, 1), NAME)
926 && validate_colon(CHILD(tree, nch - 2))
927 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000928 }
929 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000930 (void) validate_numnodes(tree, 4, "class");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000931 if (res && (nch == 7)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000932 res = (validate_lparen(CHILD(tree, 2))
933 && validate_testlist(CHILD(tree, 3))
934 && validate_rparen(CHILD(tree, 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000935 }
936 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000937}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000938
939
Guido van Rossum3d602e31996-07-21 02:33:56 +0000940/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +0000941 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +0000942 */
Guido van Rossum47478871996-08-21 14:32:37 +0000943static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000944validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000945{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000946 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000947 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +0000948 && (nch >= 4)
949 && validate_name(CHILD(tree, 0), "if")
950 && validate_test(CHILD(tree, 1))
951 && validate_colon(CHILD(tree, 2))
952 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000953
954 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000955 /* ... 'else' ':' suite */
956 res = (validate_name(CHILD(tree, nch - 3), "else")
957 && validate_colon(CHILD(tree, nch - 2))
958 && validate_suite(CHILD(tree, nch - 1)));
959 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000960 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000961 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000962 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000963 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +0000964 /* Will catch the case for nch < 4 */
965 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000966 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000967 /* ... ('elif' test ':' suite)+ ... */
968 int j = 4;
969 while ((j < nch) && res) {
970 res = (validate_name(CHILD(tree, j), "elif")
971 && validate_colon(CHILD(tree, j + 2))
972 && validate_test(CHILD(tree, j + 1))
973 && validate_suite(CHILD(tree, j + 3)));
974 j += 4;
975 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000976 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000977 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000978}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000979
980
Guido van Rossum3d602e31996-07-21 02:33:56 +0000981/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +0000982 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +0000983 *
984 */
Guido van Rossum47478871996-08-21 14:32:37 +0000985static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000986validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000987{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000988 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000989 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000990
Guido van Rossum3d602e31996-07-21 02:33:56 +0000991 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000992 res = (validate_lparen(CHILD(tree, 0))
993 && validate_rparen(CHILD(tree, nch - 1)));
994 if (res && (nch == 3))
995 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000996 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000997 else {
998 (void) validate_numnodes(tree, 2, "parameters");
999 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001000 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001001}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001002
1003
Fred Drakecff283c2000-08-21 22:24:43 +00001004/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001005 *
1006 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001007 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001008 * | NEWLINE INDENT stmt+ DEDENT
1009 */
Guido van Rossum47478871996-08-21 14:32:37 +00001010static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001011validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001012{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001013 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001014 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001015
Guido van Rossum3d602e31996-07-21 02:33:56 +00001016 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001017 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001018 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001019 /* NEWLINE INDENT stmt+ DEDENT */
1020 res = (validate_newline(CHILD(tree, 0))
1021 && validate_indent(CHILD(tree, 1))
1022 && validate_stmt(CHILD(tree, 2))
1023 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001024
Fred Drakeff9ea482000-04-19 13:54:15 +00001025 if (res && (nch > 4)) {
1026 int i = 3;
1027 --nch; /* forget the DEDENT */
1028 for ( ; res && (i < nch); ++i)
1029 res = validate_stmt(CHILD(tree, i));
1030 }
1031 else if (nch < 4)
1032 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001033 }
1034 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001035}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001036
1037
Guido van Rossum47478871996-08-21 14:32:37 +00001038static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001039validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001040{
Guido van Rossum47478871996-08-21 14:32:37 +00001041 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001042 validate_test, "testlist"));
1043}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001044
1045
Fred Drakecff283c2000-08-21 22:24:43 +00001046/* '*' NAME [',' '**' NAME] | '**' NAME
1047 */
1048static int
1049validate_varargslist_trailer(node *tree, int start)
1050{
1051 int nch = NCH(tree);
1052 int res = 0;
1053 int sym;
1054
1055 if (nch <= start) {
1056 err_string("expected variable argument trailer for varargslist");
1057 return 0;
1058 }
1059 sym = TYPE(CHILD(tree, start));
1060 if (sym == STAR) {
1061 /*
1062 * ('*' NAME [',' '**' NAME]
1063 */
1064 if (nch-start == 2)
1065 res = validate_name(CHILD(tree, start+1), NULL);
1066 else if (nch-start == 5)
1067 res = (validate_name(CHILD(tree, start+1), NULL)
1068 && validate_comma(CHILD(tree, start+2))
1069 && validate_doublestar(CHILD(tree, start+3))
1070 && validate_name(CHILD(tree, start+4), NULL));
1071 }
1072 else if (sym == DOUBLESTAR) {
1073 /*
1074 * '**' NAME
1075 */
1076 if (nch-start == 2)
1077 res = validate_name(CHILD(tree, start+1), NULL);
1078 }
1079 if (!res)
1080 err_string("illegal variable argument trailer for varargslist");
1081 return res;
1082}
1083
1084
1085/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001086 *
1087 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001088 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001089 * ('*' NAME [',' '**' NAME]
1090 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001091 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1092 *
1093 */
Guido van Rossum47478871996-08-21 14:32:37 +00001094static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001095validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001096{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001097 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001098 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001099 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001100
Fred Drakeb6429a22000-12-11 22:08:27 +00001101 if (!res)
1102 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001103 if (nch < 1) {
1104 err_string("varargslist missing child nodes");
1105 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001106 }
Fred Drakecff283c2000-08-21 22:24:43 +00001107 sym = TYPE(CHILD(tree, 0));
1108 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001109 /* whole thing matches:
1110 * '*' NAME [',' '**' NAME] | '**' NAME
1111 */
Fred Drakecff283c2000-08-21 22:24:43 +00001112 res = validate_varargslist_trailer(tree, 0);
1113 else if (sym == fpdef) {
1114 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001115
Fred Drakecff283c2000-08-21 22:24:43 +00001116 sym = TYPE(CHILD(tree, nch-1));
1117 if (sym == NAME) {
1118 /*
1119 * (fpdef ['=' test] ',')+
1120 * ('*' NAME [',' '**' NAME]
1121 * | '**' NAME)
1122 */
1123 /* skip over (fpdef ['=' test] ',')+ */
1124 while (res && (i+2 <= nch)) {
1125 res = validate_fpdef(CHILD(tree, i));
1126 ++i;
1127 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1128 res = (validate_equal(CHILD(tree, i))
1129 && validate_test(CHILD(tree, i+1)));
1130 if (res)
1131 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001132 }
Fred Drakecff283c2000-08-21 22:24:43 +00001133 if (res && i < nch) {
1134 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001135 ++i;
1136 if (res && i < nch
1137 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1138 || TYPE(CHILD(tree, i)) == STAR))
1139 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001140 }
1141 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001142 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1143 * i --^^^
1144 */
Fred Drakecff283c2000-08-21 22:24:43 +00001145 if (res)
1146 res = validate_varargslist_trailer(tree, i);
1147 }
1148 else {
1149 /*
1150 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1151 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001152 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001153 if (sym == COMMA) {
1154 res = validate_comma(CHILD(tree, nch-1));
1155 if (!res)
1156 return 0;
1157 --nch;
1158 }
1159 /*
1160 * fpdef ['=' test] (',' fpdef ['=' test])*
1161 */
1162 res = validate_fpdef(CHILD(tree, 0));
1163 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001164 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1165 res = (validate_equal(CHILD(tree, i))
1166 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001167 i += 2;
1168 }
1169 /*
1170 * ... (',' fpdef ['=' test])*
1171 * i ---^^^
1172 */
1173 while (res && (nch - i) >= 2) {
1174 res = (validate_comma(CHILD(tree, i))
1175 && validate_fpdef(CHILD(tree, i+1)));
1176 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001177 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1178 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001179 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001180 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001181 }
1182 }
1183 if (res && nch - i != 0) {
1184 res = 0;
1185 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001186 }
1187 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001188 }
Fred Drakecff283c2000-08-21 22:24:43 +00001189 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001190}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001191
1192
Fred Drakecff283c2000-08-21 22:24:43 +00001193/* list_iter: list_for | list_if
1194 */
1195static int
1196validate_list_iter(node *tree)
1197{
1198 int res = (validate_ntype(tree, list_iter)
1199 && validate_numnodes(tree, 1, "list_iter"));
1200 if (res && TYPE(CHILD(tree, 0)) == list_for)
1201 res = validate_list_for(CHILD(tree, 0));
1202 else
1203 res = validate_list_if(CHILD(tree, 0));
1204
1205 return res;
1206}
1207
1208/* list_for: 'for' exprlist 'in' testlist [list_iter]
1209 */
1210static int
1211validate_list_for(node *tree)
1212{
1213 int nch = NCH(tree);
1214 int res;
1215
1216 if (nch == 5)
1217 res = validate_list_iter(CHILD(tree, 4));
1218 else
1219 res = validate_numnodes(tree, 4, "list_for");
1220
1221 if (res)
1222 res = (validate_name(CHILD(tree, 0), "for")
1223 && validate_exprlist(CHILD(tree, 1))
1224 && validate_name(CHILD(tree, 2), "in")
1225 && validate_testlist(CHILD(tree, 3)));
1226
1227 return res;
1228}
1229
1230/* list_if: 'if' test [list_iter]
1231 */
1232static int
1233validate_list_if(node *tree)
1234{
1235 int nch = NCH(tree);
1236 int res;
1237
1238 if (nch == 3)
1239 res = validate_list_iter(CHILD(tree, 2));
1240 else
1241 res = validate_numnodes(tree, 2, "list_if");
1242
1243 if (res)
1244 res = (validate_name(CHILD(tree, 0), "if")
1245 && validate_test(CHILD(tree, 1)));
1246
1247 return res;
1248}
1249
1250
1251/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001252 *
1253 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001254 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001255 * | '(' fplist ')'
1256 */
Guido van Rossum47478871996-08-21 14:32:37 +00001257static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001258validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001259{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001260 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001261 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001262
Guido van Rossum3d602e31996-07-21 02:33:56 +00001263 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001264 if (nch == 1)
1265 res = validate_ntype(CHILD(tree, 0), NAME);
1266 else if (nch == 3)
1267 res = (validate_lparen(CHILD(tree, 0))
1268 && validate_fplist(CHILD(tree, 1))
1269 && validate_rparen(CHILD(tree, 2)));
1270 else
1271 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001272 }
1273 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001274}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001275
1276
Guido van Rossum47478871996-08-21 14:32:37 +00001277static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001278validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001279{
Guido van Rossum47478871996-08-21 14:32:37 +00001280 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001281 validate_fpdef, "fplist"));
1282}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001283
1284
Guido van Rossum3d602e31996-07-21 02:33:56 +00001285/* simple_stmt | compound_stmt
1286 *
1287 */
Guido van Rossum47478871996-08-21 14:32:37 +00001288static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001289validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001290{
1291 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001292 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001293
Guido van Rossum3d602e31996-07-21 02:33:56 +00001294 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001295 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001296
Fred Drakeff9ea482000-04-19 13:54:15 +00001297 if (TYPE(tree) == simple_stmt)
1298 res = validate_simple_stmt(tree);
1299 else
1300 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001301 }
1302 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001303}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001304
1305
Guido van Rossum3d602e31996-07-21 02:33:56 +00001306/* small_stmt (';' small_stmt)* [';'] NEWLINE
1307 *
1308 */
Guido van Rossum47478871996-08-21 14:32:37 +00001309static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001310validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001311{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001312 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001313 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001314 && (nch >= 2)
1315 && validate_small_stmt(CHILD(tree, 0))
1316 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001317
Guido van Rossum3d602e31996-07-21 02:33:56 +00001318 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001319 res = validate_numnodes(tree, 2, "simple_stmt");
1320 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001321 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001322 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001323 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001324 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001325
Fred Drakeff9ea482000-04-19 13:54:15 +00001326 for (i = 1; res && (i < nch); i += 2)
1327 res = (validate_semi(CHILD(tree, i))
1328 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001329 }
1330 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001331}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001332
1333
Guido van Rossum47478871996-08-21 14:32:37 +00001334static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001335validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001336{
1337 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001338 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001339
Fred Drake0ac9b072000-09-12 21:58:06 +00001340 if (res) {
1341 int ntype = TYPE(CHILD(tree, 0));
1342
1343 if ( (ntype == expr_stmt)
1344 || (ntype == print_stmt)
1345 || (ntype == del_stmt)
1346 || (ntype == pass_stmt)
1347 || (ntype == flow_stmt)
1348 || (ntype == import_stmt)
1349 || (ntype == global_stmt)
1350 || (ntype == assert_stmt)
1351 || (ntype == exec_stmt))
1352 res = validate_node(CHILD(tree, 0));
1353 else {
1354 res = 0;
1355 err_string("illegal small_stmt child type");
1356 }
1357 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001358 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001359 res = 0;
1360 PyErr_Format(parser_error,
1361 "Unrecognized child node of small_stmt: %d.",
1362 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001363 }
1364 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001365}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001366
1367
1368/* compound_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001369 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Guido van Rossum3d602e31996-07-21 02:33:56 +00001370 */
Guido van Rossum47478871996-08-21 14:32:37 +00001371static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001372validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001373{
1374 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001375 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001376 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001377
1378 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001379 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001380
1381 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001382 ntype = TYPE(tree);
1383 if ( (ntype == if_stmt)
1384 || (ntype == while_stmt)
1385 || (ntype == for_stmt)
1386 || (ntype == try_stmt)
1387 || (ntype == funcdef)
1388 || (ntype == classdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001389 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001390 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001391 res = 0;
1392 PyErr_Format(parser_error,
1393 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001394 }
1395 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001396}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001397
1398
Guido van Rossum47478871996-08-21 14:32:37 +00001399static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001400validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001401{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001402 int j;
1403 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001404 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001405 && is_odd(nch)
1406 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001407
Fred Drake28f739a2000-08-25 22:42:40 +00001408 if (res && nch == 3
1409 && TYPE(CHILD(tree, 1)) == augassign) {
1410 res = (validate_numnodes(CHILD(tree, 1), 1, "augassign")
1411 && validate_testlist(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001412
Fred Drake28f739a2000-08-25 22:42:40 +00001413 if (res) {
1414 char *s = STR(CHILD(CHILD(tree, 1), 0));
1415
1416 res = (strcmp(s, "+=") == 0
1417 || strcmp(s, "-=") == 0
1418 || strcmp(s, "*=") == 0
1419 || strcmp(s, "/=") == 0
1420 || strcmp(s, "%=") == 0
1421 || strcmp(s, "&=") == 0
1422 || strcmp(s, "|=") == 0
1423 || strcmp(s, "^=") == 0
1424 || strcmp(s, "<<=") == 0
1425 || strcmp(s, ">>=") == 0
1426 || strcmp(s, "**=") == 0);
1427 if (!res)
1428 err_string("illegal augmmented assignment operator");
1429 }
1430 }
1431 else {
1432 for (j = 1; res && (j < nch); j += 2)
1433 res = (validate_equal(CHILD(tree, j))
1434 && validate_testlist(CHILD(tree, j + 1)));
1435 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001436 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001437}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001438
1439
Guido van Rossum3d602e31996-07-21 02:33:56 +00001440/* print_stmt:
1441 *
Fred Drakecff283c2000-08-21 22:24:43 +00001442 * 'print' ( [ test (',' test)* [','] ]
1443 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001444 */
Guido van Rossum47478871996-08-21 14:32:37 +00001445static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001446validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001447{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001448 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001449 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001450 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001451 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001452
Fred Drakecff283c2000-08-21 22:24:43 +00001453 if (res && nch > 1) {
1454 int sym = TYPE(CHILD(tree, 1));
1455 int i = 1;
1456 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001457
Fred Drakecff283c2000-08-21 22:24:43 +00001458 if (sym == test)
1459 res = validate_test(CHILD(tree, i++));
1460 else {
1461 if (nch < 3)
1462 res = validate_numnodes(tree, 3, "print_stmt");
1463 else {
1464 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1465 && validate_test(CHILD(tree, i+1)));
1466 i += 2;
1467 allow_trailing_comma = 0;
1468 }
1469 }
1470 if (res) {
1471 /* ... (',' test)* [','] */
1472 while (res && i+2 <= nch) {
1473 res = (validate_comma(CHILD(tree, i))
1474 && validate_test(CHILD(tree, i+1)));
1475 allow_trailing_comma = 1;
1476 i += 2;
1477 }
1478 if (res && !allow_trailing_comma)
1479 res = validate_numnodes(tree, i, "print_stmt");
1480 else if (res && i < nch)
1481 res = validate_comma(CHILD(tree, i));
1482 }
1483 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001484 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001485}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001486
1487
Guido van Rossum47478871996-08-21 14:32:37 +00001488static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001489validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001490{
1491 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001492 && validate_name(CHILD(tree, 0), "del")
1493 && validate_exprlist(CHILD(tree, 1)));
1494}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001495
1496
Guido van Rossum47478871996-08-21 14:32:37 +00001497static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001498validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001499{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001500 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001501 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001502 && ((nch == 1) || (nch == 2))
1503 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001504
Guido van Rossum3d602e31996-07-21 02:33:56 +00001505 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001506 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001507
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001508 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001509}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001510
1511
Guido van Rossum47478871996-08-21 14:32:37 +00001512static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001513validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001514{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001515 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001516 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001517 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001518
Guido van Rossum3d602e31996-07-21 02:33:56 +00001519 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001520 res = validate_name(CHILD(tree, 0), "raise");
1521 if (res && (nch >= 2))
1522 res = validate_test(CHILD(tree, 1));
1523 if (res && nch > 2) {
1524 res = (validate_comma(CHILD(tree, 2))
1525 && validate_test(CHILD(tree, 3)));
1526 if (res && (nch > 4))
1527 res = (validate_comma(CHILD(tree, 4))
1528 && validate_test(CHILD(tree, 5)));
1529 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001530 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001531 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001532 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001533 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001534 res = (validate_comma(CHILD(tree, 2))
1535 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001536
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001537 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001538}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001539
1540
Fred Drake02126f22001-07-17 02:59:15 +00001541/* yield_stmt: 'yield' testlist
1542 */
1543static int
1544validate_yield_stmt(node *tree)
1545{
1546 return (validate_ntype(tree, yield_stmt)
1547 && validate_numnodes(tree, 2, "yield_stmt")
1548 && validate_name(CHILD(tree, 0), "yield")
1549 && validate_testlist(CHILD(tree, 1)));
1550}
1551
1552
Fred Drakecff283c2000-08-21 22:24:43 +00001553static int
1554validate_import_as_name(node *tree)
1555{
1556 int nch = NCH(tree);
1557 int ok = validate_ntype(tree, import_as_name);
1558
1559 if (ok) {
1560 if (nch == 1)
1561 ok = validate_name(CHILD(tree, 0), NULL);
1562 else if (nch == 3)
1563 ok = (validate_name(CHILD(tree, 0), NULL)
1564 && validate_name(CHILD(tree, 1), "as")
1565 && validate_name(CHILD(tree, 2), NULL));
1566 else
1567 ok = validate_numnodes(tree, 3, "import_as_name");
1568 }
1569 return ok;
1570}
1571
1572
Fred Drake71137082001-01-07 05:59:59 +00001573/* dotted_name: NAME ("." NAME)*
1574 */
1575static int
1576validate_dotted_name(node *tree)
1577{
1578 int nch = NCH(tree);
1579 int res = (validate_ntype(tree, dotted_name)
1580 && is_odd(nch)
1581 && validate_name(CHILD(tree, 0), NULL));
1582 int i;
1583
1584 for (i = 1; res && (i < nch); i += 2) {
1585 res = (validate_dot(CHILD(tree, i))
1586 && validate_name(CHILD(tree, i+1), NULL));
1587 }
1588 return res;
1589}
1590
1591
Fred Drakecff283c2000-08-21 22:24:43 +00001592/* dotted_as_name: dotted_name [NAME NAME]
1593 */
1594static int
1595validate_dotted_as_name(node *tree)
1596{
1597 int nch = NCH(tree);
1598 int res = validate_ntype(tree, dotted_as_name);
1599
1600 if (res) {
1601 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001602 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001603 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001604 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001605 && validate_name(CHILD(tree, 1), "as")
1606 && validate_name(CHILD(tree, 2), NULL));
1607 else {
1608 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001609 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001610 }
1611 }
1612 return res;
1613}
1614
1615
Guido van Rossum3d602e31996-07-21 02:33:56 +00001616/* import_stmt:
1617 *
Fred Drakecff283c2000-08-21 22:24:43 +00001618 * 'import' dotted_as_name (',' dotted_as_name)*
1619 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001620 */
Guido van Rossum47478871996-08-21 14:32:37 +00001621static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001622validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001623{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001624 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001625 int res = (validate_ntype(tree, import_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001626 && (nch >= 2) && is_even(nch)
Fred Drakecff283c2000-08-21 22:24:43 +00001627 && validate_ntype(CHILD(tree, 0), NAME));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001628
1629 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001630 int j;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001631
Fred Drakecff283c2000-08-21 22:24:43 +00001632 res = validate_dotted_as_name(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00001633 for (j = 2; res && (j < nch); j += 2)
1634 res = (validate_comma(CHILD(tree, j))
Fred Drake71137082001-01-07 05:59:59 +00001635 && validate_dotted_as_name(CHILD(tree, j + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001636 }
Fred Drakecff283c2000-08-21 22:24:43 +00001637 else if (res && (res = validate_name(CHILD(tree, 0), "from"))) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001638 res = ((nch >= 4) && is_even(nch)
Fred Drake71137082001-01-07 05:59:59 +00001639 && validate_dotted_name(CHILD(tree, 1))
1640 && validate_name(CHILD(tree, 2), "import"));
Fred Drakeff9ea482000-04-19 13:54:15 +00001641 if (nch == 4) {
Fred Drakecff283c2000-08-21 22:24:43 +00001642 if (TYPE(CHILD(tree, 3)) == import_as_name)
1643 res = validate_import_as_name(CHILD(tree, 3));
1644 else
1645 res = validate_star(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001646 }
1647 else {
Fred Drakecff283c2000-08-21 22:24:43 +00001648 /* 'from' dotted_name 'import' import_as_name
1649 * (',' import_as_name)+
1650 */
Fred Drakeff9ea482000-04-19 13:54:15 +00001651 int j;
Fred Drakecff283c2000-08-21 22:24:43 +00001652 res = validate_import_as_name(CHILD(tree, 3));
Fred Drakeff9ea482000-04-19 13:54:15 +00001653 for (j = 4; res && (j < nch); j += 2)
1654 res = (validate_comma(CHILD(tree, j))
Fred Drakecff283c2000-08-21 22:24:43 +00001655 && validate_import_as_name(CHILD(tree, j + 1)));
Fred Drakeff9ea482000-04-19 13:54:15 +00001656 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001657 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001658 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001659 res = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001660
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001661 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001662}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001663
1664
Guido van Rossum47478871996-08-21 14:32:37 +00001665static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001666validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001667{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001668 int j;
1669 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001670 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001671 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001672
Guido van Rossum3d602e31996-07-21 02:33:56 +00001673 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001674 res = (validate_name(CHILD(tree, 0), "global")
1675 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001676 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001677 res = (validate_comma(CHILD(tree, j))
1678 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001679
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001680 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001681}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001682
1683
Guido van Rossum3d602e31996-07-21 02:33:56 +00001684/* exec_stmt:
1685 *
1686 * 'exec' expr ['in' test [',' test]]
1687 */
Guido van Rossum47478871996-08-21 14:32:37 +00001688static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001689validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001690{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001691 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001692 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001693 && ((nch == 2) || (nch == 4) || (nch == 6))
1694 && validate_name(CHILD(tree, 0), "exec")
1695 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001696
Guido van Rossum3d602e31996-07-21 02:33:56 +00001697 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001698 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001699 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001700 res = (validate_name(CHILD(tree, 2), "in")
1701 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001702 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001703 res = (validate_comma(CHILD(tree, 4))
1704 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001705
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001706 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001707}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001708
1709
Guido van Rossum925e5471997-04-02 05:32:13 +00001710/* assert_stmt:
1711 *
1712 * 'assert' test [',' test]
1713 */
1714static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001715validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001716{
1717 int nch = NCH(tree);
1718 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001719 && ((nch == 2) || (nch == 4))
1720 && (validate_name(CHILD(tree, 0), "__assert__") ||
1721 validate_name(CHILD(tree, 0), "assert"))
1722 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001723
1724 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001725 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001726 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001727 res = (validate_comma(CHILD(tree, 2))
1728 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001729
1730 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001731}
Guido van Rossum925e5471997-04-02 05:32:13 +00001732
1733
Guido van Rossum47478871996-08-21 14:32:37 +00001734static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001735validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001736{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001737 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001738 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001739 && ((nch == 4) || (nch == 7))
1740 && validate_name(CHILD(tree, 0), "while")
1741 && validate_test(CHILD(tree, 1))
1742 && validate_colon(CHILD(tree, 2))
1743 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001744
Guido van Rossum3d602e31996-07-21 02:33:56 +00001745 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001746 res = (validate_name(CHILD(tree, 4), "else")
1747 && validate_colon(CHILD(tree, 5))
1748 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001749
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001750 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001751}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001752
1753
Guido van Rossum47478871996-08-21 14:32:37 +00001754static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001755validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001756{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001757 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001758 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001759 && ((nch == 6) || (nch == 9))
1760 && validate_name(CHILD(tree, 0), "for")
1761 && validate_exprlist(CHILD(tree, 1))
1762 && validate_name(CHILD(tree, 2), "in")
1763 && validate_testlist(CHILD(tree, 3))
1764 && validate_colon(CHILD(tree, 4))
1765 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001766
Guido van Rossum3d602e31996-07-21 02:33:56 +00001767 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001768 res = (validate_name(CHILD(tree, 6), "else")
1769 && validate_colon(CHILD(tree, 7))
1770 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001771
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001772 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001773}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001774
1775
Guido van Rossum3d602e31996-07-21 02:33:56 +00001776/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001777 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001778 * | 'try' ':' suite 'finally' ':' suite
1779 *
1780 */
Guido van Rossum47478871996-08-21 14:32:37 +00001781static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001782validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001783{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001784 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001785 int pos = 3;
1786 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001787 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001788
1789 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001790 res = (validate_name(CHILD(tree, 0), "try")
1791 && validate_colon(CHILD(tree, 1))
1792 && validate_suite(CHILD(tree, 2))
1793 && validate_colon(CHILD(tree, nch - 2))
1794 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001795 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001796 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001797 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1798 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001799
1800 PyErr_Format(parser_error,
1801 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001802 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001803 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001804 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001805 res = (validate_except_clause(CHILD(tree, pos))
1806 && validate_colon(CHILD(tree, pos + 1))
1807 && validate_suite(CHILD(tree, pos + 2)));
1808 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001809 }
1810 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001811 res = validate_ntype(CHILD(tree, pos), NAME);
1812 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1813 res = (validate_numnodes(tree, 6, "try/finally")
1814 && validate_colon(CHILD(tree, 4))
1815 && validate_suite(CHILD(tree, 5)));
1816 else if (res) {
1817 if (nch == (pos + 3)) {
1818 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1819 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1820 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001821 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001822 }
1823 else if (nch == (pos + 6)) {
1824 res = (validate_name(CHILD(tree, pos), "except")
1825 && validate_colon(CHILD(tree, pos + 1))
1826 && validate_suite(CHILD(tree, pos + 2))
1827 && validate_name(CHILD(tree, pos + 3), "else"));
1828 }
1829 else
1830 res = validate_numnodes(tree, pos + 3, "try/except");
1831 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001832 }
1833 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001834}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001835
1836
Guido van Rossum47478871996-08-21 14:32:37 +00001837static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001838validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001839{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001840 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001841 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001842 && ((nch == 1) || (nch == 2) || (nch == 4))
1843 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001844
Guido van Rossum3d602e31996-07-21 02:33:56 +00001845 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001846 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001847 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001848 res = (validate_comma(CHILD(tree, 2))
1849 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001850
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001851 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001852}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001853
1854
Guido van Rossum47478871996-08-21 14:32:37 +00001855static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001856validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001857{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001858 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001859 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001860
Guido van Rossum3d602e31996-07-21 02:33:56 +00001861 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001862 res = ((nch == 1)
1863 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001864 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001865 int pos;
1866 res = validate_and_test(CHILD(tree, 0));
1867 for (pos = 1; res && (pos < nch); pos += 2)
1868 res = (validate_name(CHILD(tree, pos), "or")
1869 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001870 }
1871 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001872}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001873
1874
Guido van Rossum47478871996-08-21 14:32:37 +00001875static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001876validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001877{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001878 int pos;
1879 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001880 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001881 && is_odd(nch)
1882 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001883
Guido van Rossum3d602e31996-07-21 02:33:56 +00001884 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001885 res = (validate_name(CHILD(tree, pos), "and")
1886 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001887
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001888 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001889}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001890
1891
Guido van Rossum47478871996-08-21 14:32:37 +00001892static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001893validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001894{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001895 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001896 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001897
Guido van Rossum3d602e31996-07-21 02:33:56 +00001898 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001899 if (nch == 2)
1900 res = (validate_name(CHILD(tree, 0), "not")
1901 && validate_not_test(CHILD(tree, 1)));
1902 else if (nch == 1)
1903 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001904 }
1905 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001906}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001907
1908
Guido van Rossum47478871996-08-21 14:32:37 +00001909static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001910validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001911{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001912 int pos;
1913 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001914 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00001915 && is_odd(nch)
1916 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001917
Guido van Rossum3d602e31996-07-21 02:33:56 +00001918 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001919 res = (validate_comp_op(CHILD(tree, pos))
1920 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001921
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001922 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001923}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001924
1925
Guido van Rossum47478871996-08-21 14:32:37 +00001926static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001927validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001928{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001929 int res = 0;
1930 int nch = NCH(tree);
1931
Guido van Rossum3d602e31996-07-21 02:33:56 +00001932 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00001933 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001934 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001935 /*
1936 * Only child will be a terminal with a well-defined symbolic name
1937 * or a NAME with a string of either 'is' or 'in'
1938 */
1939 tree = CHILD(tree, 0);
1940 switch (TYPE(tree)) {
1941 case LESS:
1942 case GREATER:
1943 case EQEQUAL:
1944 case EQUAL:
1945 case LESSEQUAL:
1946 case GREATEREQUAL:
1947 case NOTEQUAL:
1948 res = 1;
1949 break;
1950 case NAME:
1951 res = ((strcmp(STR(tree), "in") == 0)
1952 || (strcmp(STR(tree), "is") == 0));
1953 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001954 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00001955 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00001956 }
1957 break;
1958 default:
Fred Drake661ea262000-10-24 19:57:45 +00001959 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001960 break;
1961 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001962 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00001963 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001964 res = (validate_ntype(CHILD(tree, 0), NAME)
1965 && validate_ntype(CHILD(tree, 1), NAME)
1966 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
1967 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
1968 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
1969 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
1970 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001971 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001972 }
1973 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001974}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001975
1976
Guido van Rossum47478871996-08-21 14:32:37 +00001977static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001978validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001979{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001980 int j;
1981 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001982 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00001983 && is_odd(nch)
1984 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001985
Guido van Rossum3d602e31996-07-21 02:33:56 +00001986 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001987 res = (validate_xor_expr(CHILD(tree, j))
1988 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001989
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001990 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001991}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001992
1993
Guido van Rossum47478871996-08-21 14:32:37 +00001994static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001995validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001996{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001997 int j;
1998 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001999 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002000 && is_odd(nch)
2001 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002002
Guido van Rossum3d602e31996-07-21 02:33:56 +00002003 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002004 res = (validate_circumflex(CHILD(tree, j - 1))
2005 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002006
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002007 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002008}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002009
2010
Guido van Rossum47478871996-08-21 14:32:37 +00002011static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002012validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002013{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002014 int pos;
2015 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002016 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002017 && is_odd(nch)
2018 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002019
Guido van Rossum3d602e31996-07-21 02:33:56 +00002020 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002021 res = (validate_ampersand(CHILD(tree, pos))
2022 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002023
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002024 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002025}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002026
2027
2028static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002029validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002030 {
2031 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002032 int nch = NCH(tree);
2033 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002034 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002035
Guido van Rossum3d602e31996-07-21 02:33:56 +00002036 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002037 if (TYPE(CHILD(tree, pos)) != op1)
2038 res = validate_ntype(CHILD(tree, pos), op2);
2039 if (res)
2040 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002041 }
2042 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002043}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002044
2045
Guido van Rossum47478871996-08-21 14:32:37 +00002046static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002047validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002048{
2049 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002050 && validate_chain_two_ops(tree, validate_arith_expr,
2051 LEFTSHIFT, RIGHTSHIFT));
2052}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002053
2054
Guido van Rossum47478871996-08-21 14:32:37 +00002055static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002056validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002057{
2058 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002059 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2060}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002061
2062
Guido van Rossum47478871996-08-21 14:32:37 +00002063static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002064validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002065{
2066 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002067 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002068 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002069 && is_odd(nch)
2070 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002071
Guido van Rossum3d602e31996-07-21 02:33:56 +00002072 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002073 res = (((TYPE(CHILD(tree, pos)) == STAR)
2074 || (TYPE(CHILD(tree, pos)) == SLASH)
2075 || (TYPE(CHILD(tree, pos)) == PERCENT))
2076 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002077
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002078 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002079}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002080
2081
Guido van Rossum3d602e31996-07-21 02:33:56 +00002082/* factor:
2083 *
2084 * factor: ('+'|'-'|'~') factor | power
2085 */
Guido van Rossum47478871996-08-21 14:32:37 +00002086static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002087validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002088{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002089 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002090 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002091 && (((nch == 2)
2092 && ((TYPE(CHILD(tree, 0)) == PLUS)
2093 || (TYPE(CHILD(tree, 0)) == MINUS)
2094 || (TYPE(CHILD(tree, 0)) == TILDE))
2095 && validate_factor(CHILD(tree, 1)))
2096 || ((nch == 1)
2097 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002098 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002099}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002100
2101
Guido van Rossum3d602e31996-07-21 02:33:56 +00002102/* power:
2103 *
2104 * power: atom trailer* ('**' factor)*
2105 */
Guido van Rossum47478871996-08-21 14:32:37 +00002106static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002107validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002108{
2109 int pos = 1;
2110 int nch = NCH(tree);
2111 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002112 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002113
2114 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002115 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002116 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002117 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002118 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002119 return (0);
2120 }
2121 for ( ; res && (pos < (nch - 1)); pos += 2)
2122 res = (validate_doublestar(CHILD(tree, pos))
2123 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002124 }
2125 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002126}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002127
2128
Guido van Rossum47478871996-08-21 14:32:37 +00002129static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002130validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002131{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002132 int pos;
2133 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002134 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002135
Fred Drakecff283c2000-08-21 22:24:43 +00002136 if (res && nch < 1)
2137 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002138 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002139 switch (TYPE(CHILD(tree, 0))) {
2140 case LPAR:
2141 res = ((nch <= 3)
2142 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002143
Fred Drakeff9ea482000-04-19 13:54:15 +00002144 if (res && (nch == 3))
2145 res = validate_testlist(CHILD(tree, 1));
2146 break;
2147 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002148 if (nch == 2)
2149 res = validate_ntype(CHILD(tree, 1), RSQB);
2150 else if (nch == 3)
2151 res = (validate_listmaker(CHILD(tree, 1))
2152 && validate_ntype(CHILD(tree, 2), RSQB));
2153 else {
2154 res = 0;
2155 err_string("illegal list display atom");
2156 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002157 break;
2158 case LBRACE:
2159 res = ((nch <= 3)
2160 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002161
Fred Drakeff9ea482000-04-19 13:54:15 +00002162 if (res && (nch == 3))
2163 res = validate_dictmaker(CHILD(tree, 1));
2164 break;
2165 case BACKQUOTE:
2166 res = ((nch == 3)
2167 && validate_testlist(CHILD(tree, 1))
2168 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2169 break;
2170 case NAME:
2171 case NUMBER:
2172 res = (nch == 1);
2173 break;
2174 case STRING:
2175 for (pos = 1; res && (pos < nch); ++pos)
2176 res = validate_ntype(CHILD(tree, pos), STRING);
2177 break;
2178 default:
2179 res = 0;
2180 break;
2181 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002182 }
2183 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002184}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002185
2186
Fred Drake85bf3bb2000-08-23 15:35:26 +00002187/* listmaker:
2188 * test ( list_for | (',' test)* [','] )
2189 */
Fred Drakecff283c2000-08-21 22:24:43 +00002190static int
2191validate_listmaker(node *tree)
2192{
2193 int nch = NCH(tree);
2194 int ok = nch;
2195
2196 if (nch == 0)
2197 err_string("missing child nodes of listmaker");
2198 else
2199 ok = validate_test(CHILD(tree, 0));
2200
2201 /*
2202 * list_iter | (',' test)* [',']
2203 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002204 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2205 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002206 else {
2207 /* (',' test)* [','] */
2208 int i = 1;
2209 while (ok && nch - i >= 2) {
2210 ok = (validate_comma(CHILD(tree, i))
2211 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002212 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002213 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002214 if (ok && i == nch-1)
2215 ok = validate_comma(CHILD(tree, i));
2216 else if (i != nch) {
2217 ok = 0;
2218 err_string("illegal trailing nodes for listmaker");
2219 }
Fred Drakecff283c2000-08-21 22:24:43 +00002220 }
2221 return ok;
2222}
2223
2224
Guido van Rossum3d602e31996-07-21 02:33:56 +00002225/* funcdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00002226 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002227 *
2228 */
Guido van Rossum47478871996-08-21 14:32:37 +00002229static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002230validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002231{
2232 return (validate_ntype(tree, funcdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002233 && validate_numnodes(tree, 5, "funcdef")
2234 && validate_name(CHILD(tree, 0), "def")
2235 && validate_ntype(CHILD(tree, 1), NAME)
2236 && validate_colon(CHILD(tree, 3))
2237 && validate_parameters(CHILD(tree, 2))
2238 && validate_suite(CHILD(tree, 4)));
2239}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002240
2241
Guido van Rossum47478871996-08-21 14:32:37 +00002242static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002243validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002244{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002245 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002246 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002247 && ((nch == 3) || (nch == 4))
2248 && validate_name(CHILD(tree, 0), "lambda")
2249 && validate_colon(CHILD(tree, nch - 2))
2250 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002251
Guido van Rossum3d602e31996-07-21 02:33:56 +00002252 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002253 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002254 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002255 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002256
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002257 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002258}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002259
2260
Guido van Rossum3d602e31996-07-21 02:33:56 +00002261/* arglist:
2262 *
Fred Drakecff283c2000-08-21 22:24:43 +00002263 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002264 */
Guido van Rossum47478871996-08-21 14:32:37 +00002265static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002266validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002267{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002268 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002269 int i = 0;
2270 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002271
2272 if (nch <= 0)
2273 /* raise the right error from having an invalid number of children */
2274 return validate_numnodes(tree, nch + 1, "arglist");
2275
Fred Drakecff283c2000-08-21 22:24:43 +00002276 while (ok && nch-i >= 2) {
2277 /* skip leading (argument ',') */
2278 ok = (validate_argument(CHILD(tree, i))
2279 && validate_comma(CHILD(tree, i+1)));
2280 if (ok)
2281 i += 2;
2282 else
2283 PyErr_Clear();
2284 }
2285 ok = 1;
2286 if (nch-i > 0) {
2287 /*
2288 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002289 */
Fred Drakecff283c2000-08-21 22:24:43 +00002290 int sym = TYPE(CHILD(tree, i));
2291
2292 if (sym == argument) {
2293 ok = validate_argument(CHILD(tree, i));
2294 if (ok && i+1 != nch) {
2295 err_string("illegal arglist specification"
2296 " (extra stuff on end)");
2297 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002298 }
Fred Drakecff283c2000-08-21 22:24:43 +00002299 }
2300 else if (sym == STAR) {
2301 ok = validate_star(CHILD(tree, i));
2302 if (ok && (nch-i == 2))
2303 ok = validate_test(CHILD(tree, i+1));
2304 else if (ok && (nch-i == 5))
2305 ok = (validate_test(CHILD(tree, i+1))
2306 && validate_comma(CHILD(tree, i+2))
2307 && validate_doublestar(CHILD(tree, i+3))
2308 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002309 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002310 err_string("illegal use of '*' in arglist");
2311 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002312 }
2313 }
Fred Drakecff283c2000-08-21 22:24:43 +00002314 else if (sym == DOUBLESTAR) {
2315 if (nch-i == 2)
2316 ok = (validate_doublestar(CHILD(tree, i))
2317 && validate_test(CHILD(tree, i+1)));
2318 else {
2319 err_string("illegal use of '**' in arglist");
2320 ok = 0;
2321 }
2322 }
2323 else {
2324 err_string("illegal arglist specification");
2325 ok = 0;
2326 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002327 }
2328 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002329}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002330
2331
2332
2333/* argument:
2334 *
2335 * [test '='] test
2336 */
Guido van Rossum47478871996-08-21 14:32:37 +00002337static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002338validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002339{
2340 int nch = NCH(tree);
2341 int res = (validate_ntype(tree, argument)
Fred Drakeff9ea482000-04-19 13:54:15 +00002342 && ((nch == 1) || (nch == 3))
2343 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002344
2345 if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002346 res = (validate_equal(CHILD(tree, 1))
2347 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002348
2349 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002350}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002351
2352
2353
2354/* trailer:
2355 *
Guido van Rossum47478871996-08-21 14:32:37 +00002356 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002357 */
Guido van Rossum47478871996-08-21 14:32:37 +00002358static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002359validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002360{
2361 int nch = NCH(tree);
2362 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002363
2364 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002365 switch (TYPE(CHILD(tree, 0))) {
2366 case LPAR:
2367 res = validate_rparen(CHILD(tree, nch - 1));
2368 if (res && (nch == 3))
2369 res = validate_arglist(CHILD(tree, 1));
2370 break;
2371 case LSQB:
2372 res = (validate_numnodes(tree, 3, "trailer")
2373 && validate_subscriptlist(CHILD(tree, 1))
2374 && validate_ntype(CHILD(tree, 2), RSQB));
2375 break;
2376 case DOT:
2377 res = (validate_numnodes(tree, 2, "trailer")
2378 && validate_ntype(CHILD(tree, 1), NAME));
2379 break;
2380 default:
2381 res = 0;
2382 break;
2383 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002384 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002385 else {
2386 (void) validate_numnodes(tree, 2, "trailer");
2387 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002388 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002389}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002390
2391
Guido van Rossum47478871996-08-21 14:32:37 +00002392/* subscriptlist:
2393 *
2394 * subscript (',' subscript)* [',']
2395 */
2396static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002397validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002398{
2399 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002400 validate_subscript, "subscriptlist"));
2401}
Guido van Rossum47478871996-08-21 14:32:37 +00002402
2403
Guido van Rossum3d602e31996-07-21 02:33:56 +00002404/* subscript:
2405 *
Guido van Rossum47478871996-08-21 14:32:37 +00002406 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002407 */
Guido van Rossum47478871996-08-21 14:32:37 +00002408static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002409validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002410{
Guido van Rossum47478871996-08-21 14:32:37 +00002411 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002412 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002413 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002414
Guido van Rossum47478871996-08-21 14:32:37 +00002415 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002416 if (!PyErr_Occurred())
2417 err_string("invalid number of arguments for subscript node");
2418 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002419 }
Guido van Rossum47478871996-08-21 14:32:37 +00002420 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002421 /* take care of ('.' '.' '.') possibility */
2422 return (validate_numnodes(tree, 3, "subscript")
2423 && validate_dot(CHILD(tree, 0))
2424 && validate_dot(CHILD(tree, 1))
2425 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002426 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002427 if (TYPE(CHILD(tree, 0)) == test)
2428 res = validate_test(CHILD(tree, 0));
2429 else
2430 res = validate_colon(CHILD(tree, 0));
2431 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002432 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002433 /* Must be [test] ':' [test] [sliceop],
2434 * but at least one of the optional components will
2435 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002436 */
2437 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002438 res = validate_test(CHILD(tree, 0));
2439 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002440 }
2441 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002442 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002443 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002444 int rem = nch - ++offset;
2445 if (rem) {
2446 if (TYPE(CHILD(tree, offset)) == test) {
2447 res = validate_test(CHILD(tree, offset));
2448 ++offset;
2449 --rem;
2450 }
2451 if (res && rem)
2452 res = validate_sliceop(CHILD(tree, offset));
2453 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002454 }
2455 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002456}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002457
2458
Guido van Rossum47478871996-08-21 14:32:37 +00002459static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002460validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002461{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002462 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002463 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002464 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002465 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002466 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002467 }
Guido van Rossum47478871996-08-21 14:32:37 +00002468 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002469 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002470 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002471 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002472
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002473 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002474}
Guido van Rossum47478871996-08-21 14:32:37 +00002475
2476
2477static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002478validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002479{
2480 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002481 validate_expr, "exprlist"));
2482}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002483
2484
Guido van Rossum47478871996-08-21 14:32:37 +00002485static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002486validate_dictmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002487{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002488 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002489 int res = (validate_ntype(tree, dictmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002490 && (nch >= 3)
2491 && validate_test(CHILD(tree, 0))
2492 && validate_colon(CHILD(tree, 1))
2493 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002494
Guido van Rossum3d602e31996-07-21 02:33:56 +00002495 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002496 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002497 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002498 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002499
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002500 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002501 int pos = 3;
2502 /* ( ',' test ':' test )* */
2503 while (res && (pos < nch)) {
2504 res = (validate_comma(CHILD(tree, pos))
2505 && validate_test(CHILD(tree, pos + 1))
2506 && validate_colon(CHILD(tree, pos + 2))
2507 && validate_test(CHILD(tree, pos + 3)));
2508 pos += 4;
2509 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002510 }
2511 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002512}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002513
2514
Guido van Rossum47478871996-08-21 14:32:37 +00002515static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002516validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002517{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002518 int pos;
2519 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002520 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002521 && (nch >= 2)
2522 && validate_testlist(CHILD(tree, 0))
2523 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002524
Guido van Rossum3d602e31996-07-21 02:33:56 +00002525 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002526 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002527
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002528 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002529}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002530
2531
Guido van Rossum47478871996-08-21 14:32:37 +00002532static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002533validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002534{
Fred Drakeff9ea482000-04-19 13:54:15 +00002535 int nch = 0; /* num. children on current node */
2536 int res = 1; /* result value */
2537 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002538
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002539 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002540 nch = NCH(tree);
2541 next = 0;
2542 switch (TYPE(tree)) {
2543 /*
2544 * Definition nodes.
2545 */
2546 case funcdef:
2547 res = validate_funcdef(tree);
2548 break;
2549 case classdef:
2550 res = validate_class(tree);
2551 break;
2552 /*
2553 * "Trivial" parse tree nodes.
2554 * (Why did I call these trivial?)
2555 */
2556 case stmt:
2557 res = validate_stmt(tree);
2558 break;
2559 case small_stmt:
2560 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00002561 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002562 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2563 */
2564 res = validate_small_stmt(tree);
2565 break;
2566 case flow_stmt:
2567 res = (validate_numnodes(tree, 1, "flow_stmt")
2568 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2569 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002570 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002571 || (TYPE(CHILD(tree, 0)) == return_stmt)
2572 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2573 if (res)
2574 next = CHILD(tree, 0);
2575 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002576 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002577 break;
Fred Drake02126f22001-07-17 02:59:15 +00002578 case yield_stmt:
2579 res = validate_yield_stmt(tree);
2580 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002581 /*
2582 * Compound statements.
2583 */
2584 case simple_stmt:
2585 res = validate_simple_stmt(tree);
2586 break;
2587 case compound_stmt:
2588 res = validate_compound_stmt(tree);
2589 break;
2590 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002591 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002592 */
2593 case expr_stmt:
2594 res = validate_expr_stmt(tree);
2595 break;
2596 case print_stmt:
2597 res = validate_print_stmt(tree);
2598 break;
2599 case del_stmt:
2600 res = validate_del_stmt(tree);
2601 break;
2602 case pass_stmt:
2603 res = (validate_numnodes(tree, 1, "pass")
2604 && validate_name(CHILD(tree, 0), "pass"));
2605 break;
2606 case break_stmt:
2607 res = (validate_numnodes(tree, 1, "break")
2608 && validate_name(CHILD(tree, 0), "break"));
2609 break;
2610 case continue_stmt:
2611 res = (validate_numnodes(tree, 1, "continue")
2612 && validate_name(CHILD(tree, 0), "continue"));
2613 break;
2614 case return_stmt:
2615 res = validate_return_stmt(tree);
2616 break;
2617 case raise_stmt:
2618 res = validate_raise_stmt(tree);
2619 break;
2620 case import_stmt:
2621 res = validate_import_stmt(tree);
2622 break;
2623 case global_stmt:
2624 res = validate_global_stmt(tree);
2625 break;
2626 case exec_stmt:
2627 res = validate_exec_stmt(tree);
2628 break;
2629 case assert_stmt:
2630 res = validate_assert_stmt(tree);
2631 break;
2632 case if_stmt:
2633 res = validate_if(tree);
2634 break;
2635 case while_stmt:
2636 res = validate_while(tree);
2637 break;
2638 case for_stmt:
2639 res = validate_for(tree);
2640 break;
2641 case try_stmt:
2642 res = validate_try(tree);
2643 break;
2644 case suite:
2645 res = validate_suite(tree);
2646 break;
2647 /*
2648 * Expression nodes.
2649 */
2650 case testlist:
2651 res = validate_testlist(tree);
2652 break;
2653 case test:
2654 res = validate_test(tree);
2655 break;
2656 case and_test:
2657 res = validate_and_test(tree);
2658 break;
2659 case not_test:
2660 res = validate_not_test(tree);
2661 break;
2662 case comparison:
2663 res = validate_comparison(tree);
2664 break;
2665 case exprlist:
2666 res = validate_exprlist(tree);
2667 break;
2668 case comp_op:
2669 res = validate_comp_op(tree);
2670 break;
2671 case expr:
2672 res = validate_expr(tree);
2673 break;
2674 case xor_expr:
2675 res = validate_xor_expr(tree);
2676 break;
2677 case and_expr:
2678 res = validate_and_expr(tree);
2679 break;
2680 case shift_expr:
2681 res = validate_shift_expr(tree);
2682 break;
2683 case arith_expr:
2684 res = validate_arith_expr(tree);
2685 break;
2686 case term:
2687 res = validate_term(tree);
2688 break;
2689 case factor:
2690 res = validate_factor(tree);
2691 break;
2692 case power:
2693 res = validate_power(tree);
2694 break;
2695 case atom:
2696 res = validate_atom(tree);
2697 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002698
Fred Drakeff9ea482000-04-19 13:54:15 +00002699 default:
2700 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002701 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002702 res = 0;
2703 break;
2704 }
2705 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002706 }
2707 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002708}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002709
2710
Guido van Rossum47478871996-08-21 14:32:37 +00002711static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002712validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002713{
2714 int res = validate_eval_input(tree);
2715
2716 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002717 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002718
2719 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002720}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002721
2722
Guido van Rossum3d602e31996-07-21 02:33:56 +00002723/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002724 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002725 */
Guido van Rossum47478871996-08-21 14:32:37 +00002726static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002727validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002728{
2729 int j = 0;
2730 int nch = NCH(tree) - 1;
2731 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002732 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002733
Guido van Rossum3d602e31996-07-21 02:33:56 +00002734 for ( ; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002735 if (TYPE(CHILD(tree, j)) == stmt)
2736 res = validate_stmt(CHILD(tree, j));
2737 else
2738 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002739 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002740 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002741 * user. Hopefully, this won't be needed. If a user reports getting
2742 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002743 */
2744 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002745 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002746
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002747 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002748}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002749
2750
Fred Drake43f8f9b1998-04-13 16:25:46 +00002751static PyObject*
2752pickle_constructor = NULL;
2753
2754
2755static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002756parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002757{
Fred Drake268397f1998-04-29 14:16:32 +00002758 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002759 PyObject *result = NULL;
2760 PyObject *ast = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002761 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002762
2763 if (PyArg_ParseTuple(args, "O!:_pickler", &PyAST_Type, &ast)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002764 PyObject *newargs;
2765 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002766
Fred Drake2a6875e1999-09-20 22:32:18 +00002767 if ((empty_dict = PyDict_New()) == NULL)
2768 goto finally;
2769 if ((newargs = Py_BuildValue("Oi", ast, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002770 goto finally;
2771 tuple = parser_ast2tuple((PyAST_Object*)NULL, newargs, empty_dict);
2772 if (tuple != NULL) {
2773 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2774 Py_DECREF(tuple);
2775 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002776 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002777 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002778 }
2779 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002780 Py_XDECREF(empty_dict);
2781
Fred Drake43f8f9b1998-04-13 16:25:46 +00002782 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00002783}
Fred Drake43f8f9b1998-04-13 16:25:46 +00002784
2785
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002786/* Functions exported by this module. Most of this should probably
2787 * be converted into an AST object with methods, but that is better
2788 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002789 * We'd really have to write a wrapper around it all anyway to allow
2790 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002791 */
2792static PyMethodDef parser_functions[] = {
Fred Drakeff9ea482000-04-19 13:54:15 +00002793 {"ast2tuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
2794 "Creates a tuple-tree representation of an AST."},
2795 {"ast2list", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
2796 "Creates a list-tree representation of an AST."},
2797 {"compileast", (PyCFunction)parser_compileast, PUBLIC_METHOD_TYPE,
2798 "Compiles an AST object into a code object."},
2799 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
2800 "Creates an AST object from an expression."},
2801 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
2802 "Determines if an AST object was created from an expression."},
2803 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
2804 "Determines if an AST object was created from a suite."},
2805 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
2806 "Creates an AST object from a suite."},
2807 {"sequence2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
2808 "Creates an AST object from a tree representation."},
2809 {"tuple2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
2810 "Creates an AST object from a tree representation."},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002811
Fred Drake43f8f9b1998-04-13 16:25:46 +00002812 /* private stuff: support pickle module */
Fred Drakeff9ea482000-04-19 13:54:15 +00002813 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Fred Drake43f8f9b1998-04-13 16:25:46 +00002814 "Returns the pickle magic to allow ast objects to be pickled."},
2815
Fred Drake268397f1998-04-29 14:16:32 +00002816 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002817 };
2818
2819
Tim Peters6d7c4422000-08-26 07:38:06 +00002820DL_EXPORT(void) initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00002821
Guido van Rossum3886bb61998-12-04 18:50:17 +00002822DL_EXPORT(void)
Thomas Wouters5c669862000-07-24 15:49:08 +00002823initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00002824{
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002825 PyObject* module;
2826 PyObject* dict;
Fred Drakeff9ea482000-04-19 13:54:15 +00002827
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00002828 PyAST_Type.ob_type = &PyType_Type;
2829 module = Py_InitModule("parser", parser_functions);
2830 dict = PyModule_GetDict(module);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002831
Fred Drake7a15ba51999-09-09 14:21:52 +00002832 if (parser_error == 0)
2833 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002834
2835 if ((parser_error == 0)
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +00002836 || (PyDict_SetItemString(dict, "ParserError", parser_error) != 0))
2837 {
2838 /* caller will check PyErr_Occurred() */
2839 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002840 }
2841 /*
2842 * Nice to have, but don't cry if we fail.
2843 */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002844 Py_INCREF(&PyAST_Type);
2845 PyDict_SetItemString(dict, "ASTType", (PyObject*)&PyAST_Type);
2846
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002847 PyDict_SetItemString(dict, "__copyright__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002848 PyString_FromString(parser_copyright_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002849 PyDict_SetItemString(dict, "__doc__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002850 PyString_FromString(parser_doc_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002851 PyDict_SetItemString(dict, "__version__",
Fred Drakeff9ea482000-04-19 13:54:15 +00002852 PyString_FromString(parser_version_string));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002853
Fred Drake43f8f9b1998-04-13 16:25:46 +00002854 /* register to support pickling */
2855 module = PyImport_ImportModule("copy_reg");
2856 if (module != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002857 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002858
Fred Drakeff9ea482000-04-19 13:54:15 +00002859 func = PyObject_GetAttrString(module, "pickle");
2860 pickle_constructor = PyDict_GetItemString(dict, "sequence2ast");
2861 pickler = PyDict_GetItemString(dict, "_pickler");
2862 Py_XINCREF(pickle_constructor);
2863 if ((func != NULL) && (pickle_constructor != NULL)
2864 && (pickler != NULL)) {
2865 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002866
Fred Drakeff9ea482000-04-19 13:54:15 +00002867 res = PyObject_CallFunction(
2868 func, "OOO", &PyAST_Type, pickler, pickle_constructor);
2869 Py_XDECREF(res);
2870 }
2871 Py_XDECREF(func);
2872 Py_DECREF(module);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002873 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002874}