blob: 823d754fd8182ee211926b3c906abdea79fba2b1 [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 */
Benjamin Petersondcee09d2008-10-31 02:16:05 +000029#include "Python-ast.h" /* mod_ty */
Fred Drakeff9ea482000-04-19 13:54:15 +000030#include "graminit.h" /* symbols defined in the grammar */
31#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000032#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000033#include "token.h" /* token definitions */
Benjamin Petersondcee09d2008-10-31 02:16:05 +000034#include "grammar.h"
35#include "parsetok.h"
Fred Drakeff9ea482000-04-19 13:54:15 +000036 /* ISTERMINAL() / ISNONTERMINAL() */
Benjamin Petersondcee09d2008-10-31 02:16:05 +000037#include "compile.h"
38#undef Yield
39#include "ast.h"
40#include "pyarena.h"
41
42extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000043
Fred Drake268397f1998-04-29 14:16:32 +000044#ifdef lint
45#include <note.h>
46#else
47#define NOTE(x)
48#endif
49
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000050/* String constants used to initialize module attributes.
51 *
52 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000053static char parser_copyright_string[] =
54"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000055University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
56Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
57Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000058
59
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000060PyDoc_STRVAR(parser_doc_string,
61"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000062
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000063static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000064
65
Martin v. Löwis18e16552006-02-15 17:27:45 +000066typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000067typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000068 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000069 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000070
Thomas Wouters7e474022000-07-16 12:04:32 +000071/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000072 * original copyright statement is included below, and continues to apply
73 * in full to the function immediately following. All other material is
74 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
75 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000076 * new naming conventions. Added arguments to provide support for creating
77 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000078 */
79
Guido van Rossum52f2c051993-11-10 12:53:24 +000080
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000081static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000082node2tuple(node *n, /* node to convert */
83 SeqMaker mkseq, /* create sequence */
84 SeqInserter addelem, /* func. to add elem. in seq. */
Jeremy Hylton60e96f62006-08-22 20:46:00 +000085 int lineno, /* include line numbers? */
86 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000087{
Guido van Rossum3d602e31996-07-21 02:33:56 +000088 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000089 Py_INCREF(Py_None);
90 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000091 }
92 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000093 int i;
94 PyObject *v;
95 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000096
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000097 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000098 if (v == NULL)
99 return (v);
100 w = PyInt_FromLong(TYPE(n));
101 if (w == NULL) {
102 Py_DECREF(v);
103 return ((PyObject*) NULL);
104 }
105 (void) addelem(v, 0, w);
106 for (i = 0; i < NCH(n); i++) {
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000107 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000108 if (w == NULL) {
109 Py_DECREF(v);
110 return ((PyObject*) NULL);
111 }
112 (void) addelem(v, i+1, w);
113 }
Tim Peters6a627252003-07-21 14:25:23 +0000114
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000115 if (TYPE(n) == encoding_decl)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000116 (void) addelem(v, i+1, PyString_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000117 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000118 }
119 else if (ISTERMINAL(TYPE(n))) {
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000120 PyObject *result = mkseq(2 + lineno + col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000121 if (result != NULL) {
122 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000123 (void) addelem(result, 1, PyString_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000124 if (lineno == 1)
125 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000126 if (col_offset == 1)
127 (void) addelem(result, 3, PyInt_FromLong(n->n_col_offset));
Fred Drakeff9ea482000-04-19 13:54:15 +0000128 }
129 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000130 }
131 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000132 PyErr_SetString(PyExc_SystemError,
133 "unrecognized parse tree node type");
134 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000135 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000136}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000137/*
138 * End of material copyrighted by Stichting Mathematisch Centrum.
139 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000140
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000141
142
143/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000144 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000145 * field of the object type to identify which any given object represents.
146 * These should probably go in an external header to allow other extensions
147 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000148 */
149
Fred Drakec2683dd2001-07-17 19:32:05 +0000150#define PyST_EXPR 1
151#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000152
153
154/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000155 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000156 * naming style, but the code uses the new naming convention.
157 */
158
159static PyObject*
160parser_error = 0;
161
162
Fred Drakec2683dd2001-07-17 19:32:05 +0000163typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000164 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000165 node* st_node; /* the node* returned by the parser */
166 int st_type; /* EXPR or SUITE ? */
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000167 PyCompilerFlags st_flags; /* Parser and compiler flags */
Fred Drakec2683dd2001-07-17 19:32:05 +0000168} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000169
170
Jeremy Hylton938ace62002-07-17 16:30:39 +0000171static void parser_free(PyST_Object *st);
172static int parser_compare(PyST_Object *left, PyST_Object *right);
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000173static PyObject *parser_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
Fred Drakec2683dd2001-07-17 19:32:05 +0000177PyTypeObject PyST_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000178 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000179 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000180 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000181 0, /* tp_itemsize */
182 (destructor)parser_free, /* tp_dealloc */
183 0, /* tp_print */
184 parser_getattr, /* tp_getattr */
185 0, /* tp_setattr */
186 (cmpfunc)parser_compare, /* tp_compare */
187 0, /* tp_repr */
188 0, /* tp_as_number */
189 0, /* tp_as_sequence */
190 0, /* tp_as_mapping */
191 0, /* tp_hash */
192 0, /* tp_call */
193 0, /* tp_str */
194 0, /* tp_getattro */
195 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000196
197 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000198 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000199
Fred Drakeff9ea482000-04-19 13:54:15 +0000200 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000201
202 /* __doc__ */
203 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000204}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000205
206
207static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000208parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000209{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000210 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000211
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000212 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000213 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000214
215 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000216 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000217
218 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000219 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000220
221 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000222 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000223
224 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000225 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000226
227 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000228 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000229
Fred Drakeff9ea482000-04-19 13:54:15 +0000230 if (v != 0)
231 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000232 }
233 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000234}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000235
236
Fred Drakec2683dd2001-07-17 19:32:05 +0000237/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000238 *
239 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
240 * This really just wraps a call to parser_compare_nodes() with some easy
241 * checks and protection code.
242 *
243 */
244static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000245parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000246{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000247 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000248 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000249
250 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000251 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000252
Fred Drakec2683dd2001-07-17 19:32:05 +0000253 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000254}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000255
256
Fred Drakec2683dd2001-07-17 19:32:05 +0000257/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000258 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000259 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000260 * 'wrapper' object that holds a node* and allows it to be passed around in
261 * Python code.
262 *
263 */
264static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000265parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000266{
Fred Drakec2683dd2001-07-17 19:32:05 +0000267 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000268
269 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000270 o->st_node = st;
271 o->st_type = type;
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000272 o->st_flags.cf_flags = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000273 }
Fred Drake268397f1998-04-29 14:16:32 +0000274 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000275 PyNode_Free(st);
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
Fred Drakec2683dd2001-07-17 19:32:05 +0000281/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000282 *
283 * This is called by a del statement that reduces the reference count to 0.
284 *
285 */
286static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000287parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000288{
Fred Drakec2683dd2001-07-17 19:32:05 +0000289 PyNode_Free(st->st_node);
290 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000291}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000292
293
Fred Drakec2683dd2001-07-17 19:32:05 +0000294/* parser_st2tuple(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
Fred Drakec2683dd2001-07-17 19:32:05 +0000297 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000298 *
299 */
300static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000301parser_st2tuple(PyST_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;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000304 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000305 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000306 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000307
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000308 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000309
Fred Drake268397f1998-04-29 14:16:32 +0000310 if (self == NULL) {
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000311 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
312 &PyST_Type, &self, &line_option,
313 &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000314 }
Fred Drake503d8d61998-04-13 18:45:18 +0000315 else
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000316 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
317 &line_option, &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000318 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000319 int lineno = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000320 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000321 if (line_option != NULL) {
322 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
323 }
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000324 if (col_option != NULL) {
325 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
326 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000327 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000328 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000329 * since it's known to work already.
330 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000331 res = node2tuple(((PyST_Object*)self)->st_node,
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000332 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000333 }
334 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000335}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000336
Georg Brandlf9efabb2008-07-23 15:16:45 +0000337static PyObject*
338parser_ast2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
339{
340 if (PyErr_WarnPy3k("ast2tuple is removed in 3.x; use st2tuple", 1) < 0)
341 return NULL;
342 return parser_st2tuple(self, args, kw);
343}
344
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000345
Fred Drakec2683dd2001-07-17 19:32:05 +0000346/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000347 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000348 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000349 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000350 *
351 */
352static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000353parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000354{
Guido van Rossum47478871996-08-21 14:32:37 +0000355 PyObject *line_option = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000356 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000357 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000358 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000359
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000360 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000361
Fred Drake503d8d61998-04-13 18:45:18 +0000362 if (self == NULL)
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000363 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
364 &PyST_Type, &self, &line_option,
365 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000366 else
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000367 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
368 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000369 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000370 int lineno = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000371 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000372 if (line_option != 0) {
373 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
374 }
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000375 if (col_option != NULL) {
376 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
377 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000378 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000379 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000380 * since it's known to work already.
381 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000382 res = node2tuple(self->st_node,
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000383 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000384 }
385 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000386}
Guido van Rossum47478871996-08-21 14:32:37 +0000387
Georg Brandlf9efabb2008-07-23 15:16:45 +0000388static PyObject*
389parser_ast2list(PyST_Object *self, PyObject *args, PyObject *kw)
390{
391 if (PyErr_WarnPy3k("ast2list is removed in 3.x; use st2list", 1) < 0)
392 return NULL;
393 return parser_st2list(self, args, kw);
394}
395
Guido van Rossum47478871996-08-21 14:32:37 +0000396
Fred Drakec2683dd2001-07-17 19:32:05 +0000397/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000398 *
399 * This function creates code objects from the parse tree represented by
400 * the passed-in data object. An optional file name is passed in as well.
401 *
402 */
403static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000404parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000405{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000406 PyObject* res = 0;
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000407 PyArena* arena;
408 mod_ty mod;
Fred Drakec2683dd2001-07-17 19:32:05 +0000409 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000410 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000411
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000412 static char *keywords[] = {"ast", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000413
Fred Drake503d8d61998-04-13 18:45:18 +0000414 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000415 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
416 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000417 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000418 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000419 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000420
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000421 if (ok) {
422 arena = PyArena_New();
423 if (arena) {
424 mod = PyAST_FromNode(self->st_node, &(self->st_flags), str, arena);
425 if (mod) {
426 res = (PyObject *)PyAST_Compile(mod, str, &(self->st_flags), arena);
427 }
428 PyArena_Free(arena);
429 }
430 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000431
432 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000433}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000434
Georg Brandlf9efabb2008-07-23 15:16:45 +0000435static PyObject*
436parser_compileast(PyST_Object *self, PyObject *args, PyObject *kw)
437{
438 if (PyErr_WarnPy3k("compileast is removed in 3.x; use compilest", 1) < 0)
439 return NULL;
440 return parser_compilest(self, args, kw);
441}
442
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000443
444/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
445 * PyObject* parser_issuite(PyObject* self, PyObject* args)
446 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000447 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000448 * a statement suite, respectively. The return is a Python truth value.
449 *
450 */
451static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000452parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000453{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000454 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000455 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000456
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000457 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000458
Fred Drake503d8d61998-04-13 18:45:18 +0000459 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000460 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000461 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000462 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000463 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000464
465 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000466 /* Check to see if the ST represents an expression or not. */
467 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000468 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000469 }
470 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000471}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000472
473
474static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000475parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000476{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000477 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000478 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000479
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000480 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000481
Fred Drake503d8d61998-04-13 18:45:18 +0000482 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000483 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000484 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000485 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000486 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000487
488 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000489 /* Check to see if the ST represents an expression or not. */
490 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000491 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000492 }
493 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000494}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000495
496
Fred Drake7a15ba51999-09-09 14:21:52 +0000497#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
498
Fred Drake503d8d61998-04-13 18:45:18 +0000499static PyMethodDef
500parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000501 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000502 PyDoc_STR("Compile this ST object into a code object.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000503 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000504 PyDoc_STR("Determines if this ST object was created from an expression.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000505 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000506 PyDoc_STR("Determines if this ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000507 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000508 PyDoc_STR("Creates a list-tree representation of this ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000509 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000510 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Fred Drake503d8d61998-04-13 18:45:18 +0000511
Fred Drake268397f1998-04-29 14:16:32 +0000512 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000513};
514
Fred Drake503d8d61998-04-13 18:45:18 +0000515
516static PyObject*
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000517parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000518{
Fred Drake503d8d61998-04-13 18:45:18 +0000519 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000520}
Fred Drake503d8d61998-04-13 18:45:18 +0000521
522
Guido van Rossum3d602e31996-07-21 02:33:56 +0000523/* err_string(char* message)
524 *
525 * Sets the error string for an exception of type ParserError.
526 *
527 */
528static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000529err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000530{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000531 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000532}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000533
534
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000535/* PyObject* parser_do_parse(PyObject* args, int type)
536 *
537 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000538 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000539 *
540 */
541static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000542parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000543{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000544 char* string = 0;
545 PyObject* res = 0;
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000546 int flags = 0;
547 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000548
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000549 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000550
551 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000552 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
553 &_PyParser_Grammar,
554 (type == PyST_EXPR)
555 ? eval_input : file_input,
556 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000557
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000558 if (n) {
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000559 res = parser_newstobject(n, type);
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000560 if (res)
561 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
562 }
563 else
564 PyParser_SetError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000565 }
566 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000567}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000568
569
570/* PyObject* parser_expr(PyObject* self, PyObject* args)
571 * PyObject* parser_suite(PyObject* self, PyObject* args)
572 *
573 * External interfaces to the parser itself. Which is called determines if
574 * the parser attempts to recognize an expression ('eval' form) or statement
575 * suite ('exec' form). The real work is done by parser_do_parse() above.
576 *
577 */
578static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000579parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000580{
Fred Drake268397f1998-04-29 14:16:32 +0000581 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000582 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000583}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000584
585
586static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000587parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000588{
Fred Drake268397f1998-04-29 14:16:32 +0000589 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000590 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000591}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000592
593
594
Fred Drakec2683dd2001-07-17 19:32:05 +0000595/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000596 * object requires that the input tuple be valid without having to rely on
597 * catching an exception from the compiler. This is done to allow the
598 * compiler itself to remain fast, since most of its input will come from
599 * the parser directly, and therefore be known to be syntactically correct.
600 * This validation is done to ensure that we don't core dump the compile
601 * phase, returning an exception instead.
602 *
603 * Two aspects can be broken out in this code: creating a node tree from
604 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000605 * advantageous to expand the number of ST types to include funcdefs and
606 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000607 * here. They are not necessary, and not quite as useful in a raw form.
608 * For now, let's get expressions and suites working reliably.
609 */
610
611
Jeremy Hylton938ace62002-07-17 16:30:39 +0000612static node* build_node_tree(PyObject *tuple);
613static int validate_expr_tree(node *tree);
614static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000615static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000616
Fred Drakec2683dd2001-07-17 19:32:05 +0000617/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000618 *
619 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000620 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000621 * tuple can be validated. It does this by checking the first code of the
622 * tuple, and, if acceptable, builds the internal representation. If this
623 * step succeeds, the internal representation is validated as fully as
624 * possible with the various validate_*() routines defined below.
625 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000626 * This function must be changed if support is to be added for PyST_FRAGMENT
627 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000628 *
629 */
630static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000631parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000632{
Fred Drake268397f1998-04-29 14:16:32 +0000633 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000634 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000635 PyObject *tuple;
636 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000637
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000638 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000639
Fred Drakec2683dd2001-07-17 19:32:05 +0000640 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000641 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000642 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000643 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000644 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000645 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000646 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000647 }
648 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000649 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000650 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000651 tree = build_node_tree(tuple);
652 if (tree != 0) {
653 int start_sym = TYPE(tree);
654 if (start_sym == eval_input) {
655 /* Might be an eval form. */
656 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000657 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000658 else
659 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000660 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000661 else if (start_sym == file_input) {
662 /* This looks like an exec form so far. */
663 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000664 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000665 else
666 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000667 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000668 else if (start_sym == encoding_decl) {
669 /* This looks like an encoding_decl so far. */
670 if (validate_encoding_decl(tree))
671 st = parser_newstobject(tree, PyST_SUITE);
672 else
673 PyNode_Free(tree);
674 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000675 else {
676 /* This is a fragment, at best. */
677 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000678 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000679 }
Guido van Rossum47478871996-08-21 14:32:37 +0000680 }
Guido van Rossum47478871996-08-21 14:32:37 +0000681 /* Make sure we throw an exception on all errors. We should never
682 * get this, but we'd do well to be sure something is done.
683 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000684 if (st == NULL && !PyErr_Occurred())
685 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000686
Fred Drakec2683dd2001-07-17 19:32:05 +0000687 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000688}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000689
Georg Brandlf9efabb2008-07-23 15:16:45 +0000690static PyObject*
691parser_tuple2ast(PyST_Object *self, PyObject *args, PyObject *kw)
692{
693 if (PyErr_WarnPy3k("tuple2ast is removed in 3.x; use tuple2st", 1) < 0)
694 return NULL;
695 return parser_tuple2st(self, args, kw);
696}
697
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000698
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000699/* node* build_node_children()
700 *
701 * Iterate across the children of the current non-terminal node and build
702 * their structures. If successful, return the root of this portion of
703 * the tree, otherwise, 0. Any required exception will be specified already,
704 * and no memory will have been deallocated.
705 *
706 */
707static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000708build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000709{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000710 Py_ssize_t len = PyObject_Size(tuple);
711 Py_ssize_t i;
712 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000713
714 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000715 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000716 PyObject* elem = PySequence_GetItem(tuple, i);
717 int ok = elem != NULL;
718 long type = 0;
719 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000720
Fred Drakeff9ea482000-04-19 13:54:15 +0000721 if (ok)
722 ok = PySequence_Check(elem);
723 if (ok) {
724 PyObject *temp = PySequence_GetItem(elem, 0);
725 if (temp == NULL)
726 ok = 0;
727 else {
728 ok = PyInt_Check(temp);
729 if (ok)
730 type = PyInt_AS_LONG(temp);
731 Py_DECREF(temp);
732 }
733 }
734 if (!ok) {
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000735 PyObject *err = Py_BuildValue("os", elem,
736 "Illegal node construct.");
737 PyErr_SetObject(parser_error, err);
738 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000739 Py_XDECREF(elem);
740 return (0);
741 }
742 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000743 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000744 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000745
Fred Drake0ac9b072000-09-12 21:58:06 +0000746 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000747 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000748 return 0;
749 }
750 temp = PySequence_GetItem(elem, 1);
751 if (temp == NULL)
752 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000753 if (!PyString_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000754 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000755 "second item in terminal node must be a string,"
756 " found %s",
Christian Heimese93237d2007-12-19 02:37:44 +0000757 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000758 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000759 return 0;
760 }
761 if (len == 3) {
762 PyObject *o = PySequence_GetItem(elem, 2);
763 if (o != NULL) {
764 if (PyInt_Check(o))
765 *line_num = PyInt_AS_LONG(o);
766 else {
767 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000768 "third item in terminal node must be an"
769 " integer, found %s",
Christian Heimese93237d2007-12-19 02:37:44 +0000770 Py_TYPE(temp)->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000771 Py_DECREF(o);
772 Py_DECREF(temp);
773 return 0;
774 }
775 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000776 }
777 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000778 len = PyString_GET_SIZE(temp) + 1;
Tim Petersc9d78aa2006-03-26 23:27:58 +0000779 strn = (char *)PyObject_MALLOC(len);
Fred Drake0ac9b072000-09-12 21:58:06 +0000780 if (strn != NULL)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000781 (void) memcpy(strn, PyString_AS_STRING(temp), len);
Fred Drake0ac9b072000-09-12 21:58:06 +0000782 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000783 }
784 else if (!ISNONTERMINAL(type)) {
785 /*
786 * It has to be one or the other; this is an error.
787 * Throw an exception.
788 */
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000789 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
790 PyErr_SetObject(parser_error, err);
791 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000792 Py_XDECREF(elem);
793 return (0);
794 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000795 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000796 if (err == E_NOMEM) {
Tim Petersc9d78aa2006-03-26 23:27:58 +0000797 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000798 return (node *) PyErr_NoMemory();
799 }
800 if (err == E_OVERFLOW) {
Tim Petersc9d78aa2006-03-26 23:27:58 +0000801 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000802 PyErr_SetString(PyExc_ValueError,
803 "unsupported number of child nodes");
804 return NULL;
805 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000806
Fred Drakeff9ea482000-04-19 13:54:15 +0000807 if (ISNONTERMINAL(type)) {
808 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000809
Fred Drakeff9ea482000-04-19 13:54:15 +0000810 if (new_child != build_node_children(elem, new_child, line_num)) {
811 Py_XDECREF(elem);
812 return (0);
813 }
814 }
815 else if (type == NEWLINE) { /* It's true: we increment the */
816 ++(*line_num); /* line number *after* the newline! */
817 }
818 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000819 }
Tim Petersc9d78aa2006-03-26 23:27:58 +0000820 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000821}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000822
823
824static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000825build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000826{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000827 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000828 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000829 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000830
Guido van Rossum47478871996-08-21 14:32:37 +0000831 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000832 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000833 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000834 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000835 /*
836 * The tuple is simple, but it doesn't start with a start symbol.
837 * Throw an exception now and be done with it.
838 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000839 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000840 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000841 PyErr_SetObject(parser_error, tuple);
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000842 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000843 }
844 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000845 /*
846 * Not efficient, but that can be handled later.
847 */
848 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000849 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000850
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000851 if (num == encoding_decl) {
852 encoding = PySequence_GetItem(tuple, 2);
853 /* tuple isn't borrowed anymore here, need to DECREF */
854 tuple = PySequence_GetSlice(tuple, 0, 2);
855 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000856 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000857 if (res != NULL) {
858 if (res != build_node_children(tuple, res, &line_num)) {
859 PyNode_Free(res);
860 res = NULL;
861 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000862 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000863 Py_ssize_t len;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000864 len = PyString_GET_SIZE(encoding) + 1;
Tim Petersc9d78aa2006-03-26 23:27:58 +0000865 res->n_str = (char *)PyObject_MALLOC(len);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000866 if (res->n_str != NULL)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000867 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000868 Py_DECREF(encoding);
869 Py_DECREF(tuple);
870 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000871 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000872 }
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000873 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000874 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000875 * NONTERMINAL, we can't use it. Not sure the implementation
876 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000877 */
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000878 PyObject *err = Py_BuildValue("os", tuple,
879 "Illegal component tuple.");
880 PyErr_SetObject(parser_error, err);
881 Py_XDECREF(err);
882 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000883
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000884 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000885}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000886
887
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000888/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000889 * Validation routines used within the validation section:
890 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000891static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000892
Fred Drakeff9ea482000-04-19 13:54:15 +0000893#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
894#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
895#define validate_colon(ch) validate_terminal(ch, COLON, ":")
896#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
897#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
898#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
899#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
900#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
901#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
902#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
903#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
904#define validate_star(ch) validate_terminal(ch, STAR, "*")
905#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
906#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
907#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000908#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000909#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000910
Fred Drake0ac9b072000-09-12 21:58:06 +0000911#define VALIDATER(n) static int validate_##n(node *tree)
912
Fred Drakeff9ea482000-04-19 13:54:15 +0000913VALIDATER(node); VALIDATER(small_stmt);
914VALIDATER(class); VALIDATER(node);
915VALIDATER(parameters); VALIDATER(suite);
916VALIDATER(testlist); VALIDATER(varargslist);
917VALIDATER(fpdef); VALIDATER(fplist);
918VALIDATER(stmt); VALIDATER(simple_stmt);
919VALIDATER(expr_stmt); VALIDATER(power);
920VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000921VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000922VALIDATER(raise_stmt); VALIDATER(import_stmt);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000923VALIDATER(import_name); VALIDATER(import_from);
Fred Drakecff283c2000-08-21 22:24:43 +0000924VALIDATER(global_stmt); VALIDATER(list_if);
925VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000926VALIDATER(exec_stmt); VALIDATER(compound_stmt);
927VALIDATER(while); VALIDATER(for);
928VALIDATER(try); VALIDATER(except_clause);
929VALIDATER(test); VALIDATER(and_test);
930VALIDATER(not_test); VALIDATER(comparison);
931VALIDATER(comp_op); VALIDATER(expr);
932VALIDATER(xor_expr); VALIDATER(and_expr);
933VALIDATER(shift_expr); VALIDATER(arith_expr);
934VALIDATER(term); VALIDATER(factor);
935VALIDATER(atom); VALIDATER(lambdef);
936VALIDATER(trailer); VALIDATER(subscript);
937VALIDATER(subscriptlist); VALIDATER(sliceop);
Alexandre Vassalottiee936a22010-01-09 23:35:54 +0000938VALIDATER(exprlist); VALIDATER(dictorsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000939VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000940VALIDATER(listmaker); VALIDATER(yield_stmt);
Raymond Hettinger354433a2004-05-19 08:20:33 +0000941VALIDATER(testlist1); VALIDATER(gen_for);
942VALIDATER(gen_iter); VALIDATER(gen_if);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000943VALIDATER(testlist_gexp); VALIDATER(yield_expr);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +0000944VALIDATER(yield_or_testlist); VALIDATER(or_test);
945VALIDATER(old_test); VALIDATER(old_lambdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000946
Fred Drake0ac9b072000-09-12 21:58:06 +0000947#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000948
Fred Drakeff9ea482000-04-19 13:54:15 +0000949#define is_even(n) (((n) & 1) == 0)
950#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000951
952
953static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000954validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000955{
Fred Drake0ac9b072000-09-12 21:58:06 +0000956 if (TYPE(n) != t) {
957 PyErr_Format(parser_error, "Expected node type %d, got %d.",
958 t, TYPE(n));
959 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000960 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000961 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000962}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000963
964
Fred Drakee7ab64e2000-04-25 04:14:46 +0000965/* Verifies that the number of child nodes is exactly 'num', raising
966 * an exception if it isn't. The exception message does not indicate
967 * the exact number of nodes, allowing this to be used to raise the
968 * "right" exception when the wrong number of nodes is present in a
969 * specific variant of a statement's syntax. This is commonly used
970 * in that fashion.
971 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000972static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000973validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000974{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000975 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000976 PyErr_Format(parser_error,
977 "Illegal number of children for %s node.", name);
978 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000979 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000980 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000981}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000982
983
984static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000985validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000986{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000987 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000988 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000989
990 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000991 PyErr_Format(parser_error,
992 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000993 }
994 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000995}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000996
997
Guido van Rossum47478871996-08-21 14:32:37 +0000998/* X (',' X) [',']
999 */
1000static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +00001001validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +00001002 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001003{
1004 int nch = NCH(tree);
1005 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +00001006 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +00001007
1008 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001009 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +00001010 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001011 if (is_even(nch))
1012 res = validate_comma(CHILD(tree, --nch));
1013 if (res && nch > 1) {
1014 int pos = 1;
1015 for ( ; res && pos < nch; pos += 2)
1016 res = (validate_comma(CHILD(tree, pos))
1017 && vfunc(CHILD(tree, pos + 1)));
1018 }
Guido van Rossum47478871996-08-21 14:32:37 +00001019 }
1020 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001021}
Guido van Rossum47478871996-08-21 14:32:37 +00001022
1023
Fred Drakecff283c2000-08-21 22:24:43 +00001024/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001025 *
1026 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001027 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00001028 */
Guido van Rossum47478871996-08-21 14:32:37 +00001029static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001030validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001031{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001032 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +00001033 int res = (validate_ntype(tree, classdef) &&
1034 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001035
Guido van Rossum3d602e31996-07-21 02:33:56 +00001036 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001037 res = (validate_name(CHILD(tree, 0), "class")
1038 && validate_ntype(CHILD(tree, 1), NAME)
1039 && validate_colon(CHILD(tree, nch - 2))
1040 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001041 }
Brett Cannonf4189912005-04-09 02:30:16 +00001042 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001043 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +00001044 }
1045
1046 if (res) {
1047 if (nch == 7) {
1048 res = ((validate_lparen(CHILD(tree, 2)) &&
1049 validate_testlist(CHILD(tree, 3)) &&
1050 validate_rparen(CHILD(tree, 4))));
1051 }
1052 else if (nch == 6) {
1053 res = (validate_lparen(CHILD(tree,2)) &&
1054 validate_rparen(CHILD(tree,3)));
1055 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001056 }
1057 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001058}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001059
1060
Guido van Rossum3d602e31996-07-21 02:33:56 +00001061/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001062 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001063 */
Guido van Rossum47478871996-08-21 14:32:37 +00001064static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001065validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001066{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001067 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001068 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001069 && (nch >= 4)
1070 && validate_name(CHILD(tree, 0), "if")
1071 && validate_test(CHILD(tree, 1))
1072 && validate_colon(CHILD(tree, 2))
1073 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001074
1075 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001076 /* ... 'else' ':' suite */
1077 res = (validate_name(CHILD(tree, nch - 3), "else")
1078 && validate_colon(CHILD(tree, nch - 2))
1079 && validate_suite(CHILD(tree, nch - 1)));
1080 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001081 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001082 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001083 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001084 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001085 /* Will catch the case for nch < 4 */
1086 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001087 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001088 /* ... ('elif' test ':' suite)+ ... */
1089 int j = 4;
1090 while ((j < nch) && res) {
1091 res = (validate_name(CHILD(tree, j), "elif")
1092 && validate_colon(CHILD(tree, j + 2))
1093 && validate_test(CHILD(tree, j + 1))
1094 && validate_suite(CHILD(tree, j + 3)));
1095 j += 4;
1096 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001097 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001098 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001099}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001100
1101
Guido van Rossum3d602e31996-07-21 02:33:56 +00001102/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001103 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001104 *
1105 */
Guido van Rossum47478871996-08-21 14:32:37 +00001106static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001107validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001108{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001109 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001110 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001111
Guido van Rossum3d602e31996-07-21 02:33:56 +00001112 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001113 res = (validate_lparen(CHILD(tree, 0))
1114 && validate_rparen(CHILD(tree, nch - 1)));
1115 if (res && (nch == 3))
1116 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001117 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001118 else {
1119 (void) validate_numnodes(tree, 2, "parameters");
1120 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001121 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001122}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001123
1124
Fred Drakecff283c2000-08-21 22:24:43 +00001125/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001126 *
1127 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001128 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001129 * | NEWLINE INDENT stmt+ DEDENT
1130 */
Guido van Rossum47478871996-08-21 14:32:37 +00001131static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001132validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001133{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001134 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001135 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001136
Guido van Rossum3d602e31996-07-21 02:33:56 +00001137 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001138 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001139 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001140 /* NEWLINE INDENT stmt+ DEDENT */
1141 res = (validate_newline(CHILD(tree, 0))
1142 && validate_indent(CHILD(tree, 1))
1143 && validate_stmt(CHILD(tree, 2))
1144 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001145
Fred Drakeff9ea482000-04-19 13:54:15 +00001146 if (res && (nch > 4)) {
1147 int i = 3;
1148 --nch; /* forget the DEDENT */
1149 for ( ; res && (i < nch); ++i)
1150 res = validate_stmt(CHILD(tree, i));
1151 }
1152 else if (nch < 4)
1153 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001154 }
1155 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001156}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001157
1158
Guido van Rossum47478871996-08-21 14:32:37 +00001159static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001160validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001161{
Guido van Rossum47478871996-08-21 14:32:37 +00001162 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001163 validate_test, "testlist"));
1164}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001165
1166
Guido van Rossum1c917072001-10-15 15:44:05 +00001167static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001168validate_testlist1(node *tree)
1169{
1170 return (validate_repeating_list(tree, testlist1,
1171 validate_test, "testlist1"));
1172}
1173
1174
1175static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001176validate_testlist_safe(node *tree)
1177{
1178 return (validate_repeating_list(tree, testlist_safe,
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001179 validate_old_test, "testlist_safe"));
Guido van Rossum1c917072001-10-15 15:44:05 +00001180}
1181
1182
Fred Drakecff283c2000-08-21 22:24:43 +00001183/* '*' NAME [',' '**' NAME] | '**' NAME
1184 */
1185static int
1186validate_varargslist_trailer(node *tree, int start)
1187{
1188 int nch = NCH(tree);
1189 int res = 0;
1190 int sym;
1191
1192 if (nch <= start) {
1193 err_string("expected variable argument trailer for varargslist");
1194 return 0;
1195 }
1196 sym = TYPE(CHILD(tree, start));
1197 if (sym == STAR) {
1198 /*
1199 * ('*' NAME [',' '**' NAME]
1200 */
1201 if (nch-start == 2)
1202 res = validate_name(CHILD(tree, start+1), NULL);
1203 else if (nch-start == 5)
1204 res = (validate_name(CHILD(tree, start+1), NULL)
1205 && validate_comma(CHILD(tree, start+2))
1206 && validate_doublestar(CHILD(tree, start+3))
1207 && validate_name(CHILD(tree, start+4), NULL));
1208 }
1209 else if (sym == DOUBLESTAR) {
1210 /*
1211 * '**' NAME
1212 */
1213 if (nch-start == 2)
1214 res = validate_name(CHILD(tree, start+1), NULL);
1215 }
1216 if (!res)
1217 err_string("illegal variable argument trailer for varargslist");
1218 return res;
1219}
1220
1221
1222/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001223 *
1224 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001225 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001226 * ('*' NAME [',' '**' NAME]
1227 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001228 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1229 *
1230 */
Guido van Rossum47478871996-08-21 14:32:37 +00001231static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001232validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001233{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001234 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001235 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001236 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001237
Fred Drakeb6429a22000-12-11 22:08:27 +00001238 if (!res)
1239 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001240 if (nch < 1) {
1241 err_string("varargslist missing child nodes");
1242 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001243 }
Fred Drakecff283c2000-08-21 22:24:43 +00001244 sym = TYPE(CHILD(tree, 0));
1245 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001246 /* whole thing matches:
1247 * '*' NAME [',' '**' NAME] | '**' NAME
1248 */
Fred Drakecff283c2000-08-21 22:24:43 +00001249 res = validate_varargslist_trailer(tree, 0);
1250 else if (sym == fpdef) {
1251 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001252
Fred Drakecff283c2000-08-21 22:24:43 +00001253 sym = TYPE(CHILD(tree, nch-1));
1254 if (sym == NAME) {
1255 /*
1256 * (fpdef ['=' test] ',')+
1257 * ('*' NAME [',' '**' NAME]
1258 * | '**' NAME)
1259 */
1260 /* skip over (fpdef ['=' test] ',')+ */
1261 while (res && (i+2 <= nch)) {
1262 res = validate_fpdef(CHILD(tree, i));
1263 ++i;
1264 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1265 res = (validate_equal(CHILD(tree, i))
1266 && validate_test(CHILD(tree, i+1)));
1267 if (res)
1268 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001269 }
Fred Drakecff283c2000-08-21 22:24:43 +00001270 if (res && i < nch) {
1271 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001272 ++i;
1273 if (res && i < nch
1274 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1275 || TYPE(CHILD(tree, i)) == STAR))
1276 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001277 }
1278 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001279 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1280 * i --^^^
1281 */
Fred Drakecff283c2000-08-21 22:24:43 +00001282 if (res)
1283 res = validate_varargslist_trailer(tree, i);
1284 }
1285 else {
1286 /*
1287 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1288 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001289 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001290 if (sym == COMMA) {
1291 res = validate_comma(CHILD(tree, nch-1));
1292 if (!res)
1293 return 0;
1294 --nch;
1295 }
1296 /*
1297 * fpdef ['=' test] (',' fpdef ['=' test])*
1298 */
1299 res = validate_fpdef(CHILD(tree, 0));
1300 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001301 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1302 res = (validate_equal(CHILD(tree, i))
1303 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001304 i += 2;
1305 }
1306 /*
1307 * ... (',' fpdef ['=' test])*
1308 * i ---^^^
1309 */
1310 while (res && (nch - i) >= 2) {
1311 res = (validate_comma(CHILD(tree, i))
1312 && validate_fpdef(CHILD(tree, i+1)));
1313 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001314 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1315 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001316 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001317 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001318 }
1319 }
1320 if (res && nch - i != 0) {
1321 res = 0;
1322 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001323 }
1324 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001325 }
Fred Drakecff283c2000-08-21 22:24:43 +00001326 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001327}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001328
1329
Fred Drakecff283c2000-08-21 22:24:43 +00001330/* list_iter: list_for | list_if
1331 */
1332static int
1333validate_list_iter(node *tree)
1334{
1335 int res = (validate_ntype(tree, list_iter)
1336 && validate_numnodes(tree, 1, "list_iter"));
1337 if (res && TYPE(CHILD(tree, 0)) == list_for)
1338 res = validate_list_for(CHILD(tree, 0));
1339 else
1340 res = validate_list_if(CHILD(tree, 0));
1341
1342 return res;
1343}
1344
Raymond Hettinger354433a2004-05-19 08:20:33 +00001345/* gen_iter: gen_for | gen_if
1346 */
1347static int
1348validate_gen_iter(node *tree)
1349{
1350 int res = (validate_ntype(tree, gen_iter)
1351 && validate_numnodes(tree, 1, "gen_iter"));
1352 if (res && TYPE(CHILD(tree, 0)) == gen_for)
1353 res = validate_gen_for(CHILD(tree, 0));
1354 else
1355 res = validate_gen_if(CHILD(tree, 0));
1356
1357 return res;
1358}
1359
Fred Drakecff283c2000-08-21 22:24:43 +00001360/* list_for: 'for' exprlist 'in' testlist [list_iter]
1361 */
1362static int
1363validate_list_for(node *tree)
1364{
1365 int nch = NCH(tree);
1366 int res;
1367
1368 if (nch == 5)
1369 res = validate_list_iter(CHILD(tree, 4));
1370 else
1371 res = validate_numnodes(tree, 4, "list_for");
1372
1373 if (res)
1374 res = (validate_name(CHILD(tree, 0), "for")
1375 && validate_exprlist(CHILD(tree, 1))
1376 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001377 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001378
1379 return res;
1380}
1381
Raymond Hettinger354433a2004-05-19 08:20:33 +00001382/* gen_for: 'for' exprlist 'in' test [gen_iter]
1383 */
1384static int
1385validate_gen_for(node *tree)
1386{
1387 int nch = NCH(tree);
1388 int res;
1389
1390 if (nch == 5)
1391 res = validate_gen_iter(CHILD(tree, 4));
1392 else
1393 res = validate_numnodes(tree, 4, "gen_for");
1394
1395 if (res)
1396 res = (validate_name(CHILD(tree, 0), "for")
1397 && validate_exprlist(CHILD(tree, 1))
1398 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001399 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001400
1401 return res;
1402}
1403
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001404/* list_if: 'if' old_test [list_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001405 */
1406static int
1407validate_list_if(node *tree)
1408{
1409 int nch = NCH(tree);
1410 int res;
1411
1412 if (nch == 3)
1413 res = validate_list_iter(CHILD(tree, 2));
1414 else
1415 res = validate_numnodes(tree, 2, "list_if");
1416
1417 if (res)
1418 res = (validate_name(CHILD(tree, 0), "if")
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001419 && validate_old_test(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001420
1421 return res;
1422}
1423
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001424/* gen_if: 'if' old_test [gen_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001425 */
1426static int
1427validate_gen_if(node *tree)
1428{
1429 int nch = NCH(tree);
1430 int res;
1431
1432 if (nch == 3)
1433 res = validate_gen_iter(CHILD(tree, 2));
1434 else
1435 res = validate_numnodes(tree, 2, "gen_if");
1436
1437 if (res)
1438 res = (validate_name(CHILD(tree, 0), "if")
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001439 && validate_old_test(CHILD(tree, 1)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001440
1441 return res;
1442}
Fred Drakecff283c2000-08-21 22:24:43 +00001443
1444/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001445 *
1446 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001447 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001448 * | '(' fplist ')'
1449 */
Guido van Rossum47478871996-08-21 14:32:37 +00001450static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001451validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001452{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001453 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001454 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001455
Guido van Rossum3d602e31996-07-21 02:33:56 +00001456 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001457 if (nch == 1)
1458 res = validate_ntype(CHILD(tree, 0), NAME);
1459 else if (nch == 3)
1460 res = (validate_lparen(CHILD(tree, 0))
1461 && validate_fplist(CHILD(tree, 1))
1462 && validate_rparen(CHILD(tree, 2)));
1463 else
1464 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001465 }
1466 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001467}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001468
1469
Guido van Rossum47478871996-08-21 14:32:37 +00001470static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001471validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001472{
Guido van Rossum47478871996-08-21 14:32:37 +00001473 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001474 validate_fpdef, "fplist"));
1475}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001476
1477
Guido van Rossum3d602e31996-07-21 02:33:56 +00001478/* simple_stmt | compound_stmt
1479 *
1480 */
Guido van Rossum47478871996-08-21 14:32:37 +00001481static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001482validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001483{
1484 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001485 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001486
Guido van Rossum3d602e31996-07-21 02:33:56 +00001487 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001488 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001489
Fred Drakeff9ea482000-04-19 13:54:15 +00001490 if (TYPE(tree) == simple_stmt)
1491 res = validate_simple_stmt(tree);
1492 else
1493 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001494 }
1495 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001496}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001497
1498
Guido van Rossum3d602e31996-07-21 02:33:56 +00001499/* small_stmt (';' small_stmt)* [';'] NEWLINE
1500 *
1501 */
Guido van Rossum47478871996-08-21 14:32:37 +00001502static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001503validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001504{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001505 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001506 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001507 && (nch >= 2)
1508 && validate_small_stmt(CHILD(tree, 0))
1509 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001510
Guido van Rossum3d602e31996-07-21 02:33:56 +00001511 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001512 res = validate_numnodes(tree, 2, "simple_stmt");
1513 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001514 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001515 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001516 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001517 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001518
Fred Drakeff9ea482000-04-19 13:54:15 +00001519 for (i = 1; res && (i < nch); i += 2)
1520 res = (validate_semi(CHILD(tree, i))
1521 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001522 }
1523 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001524}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001525
1526
Guido van Rossum47478871996-08-21 14:32:37 +00001527static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001528validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001529{
1530 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001531 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001532
Fred Drake0ac9b072000-09-12 21:58:06 +00001533 if (res) {
1534 int ntype = TYPE(CHILD(tree, 0));
1535
1536 if ( (ntype == expr_stmt)
1537 || (ntype == print_stmt)
1538 || (ntype == del_stmt)
1539 || (ntype == pass_stmt)
1540 || (ntype == flow_stmt)
1541 || (ntype == import_stmt)
1542 || (ntype == global_stmt)
1543 || (ntype == assert_stmt)
1544 || (ntype == exec_stmt))
1545 res = validate_node(CHILD(tree, 0));
1546 else {
1547 res = 0;
1548 err_string("illegal small_stmt child type");
1549 }
1550 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001551 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001552 res = 0;
1553 PyErr_Format(parser_error,
1554 "Unrecognized child node of small_stmt: %d.",
1555 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001556 }
1557 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001558}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001559
1560
1561/* compound_stmt:
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00001562 * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
Guido van Rossum3d602e31996-07-21 02:33:56 +00001563 */
Guido van Rossum47478871996-08-21 14:32:37 +00001564static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001565validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001566{
1567 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001568 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001569 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001570
1571 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001572 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001573
1574 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001575 ntype = TYPE(tree);
1576 if ( (ntype == if_stmt)
1577 || (ntype == while_stmt)
1578 || (ntype == for_stmt)
1579 || (ntype == try_stmt)
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00001580 || (ntype == with_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001581 || (ntype == funcdef)
Christian Heimes5224d282008-02-23 15:01:05 +00001582 || (ntype == classdef)
1583 || (ntype == decorated))
Fred Drakeff9ea482000-04-19 13:54:15 +00001584 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001585 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001586 res = 0;
1587 PyErr_Format(parser_error,
1588 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001589 }
1590 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001591}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001592
Guido van Rossum47478871996-08-21 14:32:37 +00001593static int
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001594validate_yield_or_testlist(node *tree)
1595{
1596 if (TYPE(tree) == yield_expr)
1597 return validate_yield_expr(tree);
1598 else
1599 return validate_testlist(tree);
1600}
1601
1602static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001603validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001604{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001605 int j;
1606 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001607 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001608 && is_odd(nch)
1609 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001610
Fred Drake28f739a2000-08-25 22:42:40 +00001611 if (res && nch == 3
1612 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001613 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1614 && validate_yield_or_testlist(CHILD(tree, 2));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001615
Fred Drake28f739a2000-08-25 22:42:40 +00001616 if (res) {
1617 char *s = STR(CHILD(CHILD(tree, 1), 0));
1618
1619 res = (strcmp(s, "+=") == 0
1620 || strcmp(s, "-=") == 0
1621 || strcmp(s, "*=") == 0
1622 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001623 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001624 || strcmp(s, "%=") == 0
1625 || strcmp(s, "&=") == 0
1626 || strcmp(s, "|=") == 0
1627 || strcmp(s, "^=") == 0
1628 || strcmp(s, "<<=") == 0
1629 || strcmp(s, ">>=") == 0
1630 || strcmp(s, "**=") == 0);
1631 if (!res)
1632 err_string("illegal augmmented assignment operator");
1633 }
1634 }
1635 else {
1636 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001637 res = validate_equal(CHILD(tree, j))
1638 && validate_yield_or_testlist(CHILD(tree, j + 1));
Fred Drake28f739a2000-08-25 22:42:40 +00001639 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001640 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001641}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001642
1643
Guido van Rossum3d602e31996-07-21 02:33:56 +00001644/* print_stmt:
1645 *
Fred Drakecff283c2000-08-21 22:24:43 +00001646 * 'print' ( [ test (',' test)* [','] ]
1647 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001648 */
Guido van Rossum47478871996-08-21 14:32:37 +00001649static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001650validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001651{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001652 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001653 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001654 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001655 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001656
Fred Drakecff283c2000-08-21 22:24:43 +00001657 if (res && nch > 1) {
1658 int sym = TYPE(CHILD(tree, 1));
1659 int i = 1;
1660 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001661
Fred Drakecff283c2000-08-21 22:24:43 +00001662 if (sym == test)
1663 res = validate_test(CHILD(tree, i++));
1664 else {
1665 if (nch < 3)
1666 res = validate_numnodes(tree, 3, "print_stmt");
1667 else {
1668 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1669 && validate_test(CHILD(tree, i+1)));
1670 i += 2;
1671 allow_trailing_comma = 0;
1672 }
1673 }
1674 if (res) {
1675 /* ... (',' test)* [','] */
1676 while (res && i+2 <= nch) {
1677 res = (validate_comma(CHILD(tree, i))
1678 && validate_test(CHILD(tree, i+1)));
1679 allow_trailing_comma = 1;
1680 i += 2;
1681 }
1682 if (res && !allow_trailing_comma)
1683 res = validate_numnodes(tree, i, "print_stmt");
1684 else if (res && i < nch)
1685 res = validate_comma(CHILD(tree, i));
1686 }
1687 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001688 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001689}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001690
1691
Guido van Rossum47478871996-08-21 14:32:37 +00001692static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001693validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001694{
1695 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001696 && validate_name(CHILD(tree, 0), "del")
1697 && validate_exprlist(CHILD(tree, 1)));
1698}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001699
1700
Guido van Rossum47478871996-08-21 14:32:37 +00001701static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001702validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001703{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001704 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001705 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001706 && ((nch == 1) || (nch == 2))
1707 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001708
Guido van Rossum3d602e31996-07-21 02:33:56 +00001709 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001710 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001711
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001712 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001713}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001714
1715
Guido van Rossum47478871996-08-21 14:32:37 +00001716static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001717validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001718{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001719 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001720 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001721 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001722
Guido van Rossum3d602e31996-07-21 02:33:56 +00001723 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001724 res = validate_name(CHILD(tree, 0), "raise");
1725 if (res && (nch >= 2))
1726 res = validate_test(CHILD(tree, 1));
1727 if (res && nch > 2) {
1728 res = (validate_comma(CHILD(tree, 2))
1729 && validate_test(CHILD(tree, 3)));
1730 if (res && (nch > 4))
1731 res = (validate_comma(CHILD(tree, 4))
1732 && validate_test(CHILD(tree, 5)));
1733 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001734 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001735 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001736 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001737 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001738 res = (validate_comma(CHILD(tree, 2))
1739 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001740
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001741 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001742}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001743
1744
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001745/* yield_expr: 'yield' [testlist]
1746 */
1747static int
1748validate_yield_expr(node *tree)
1749{
1750 int nch = NCH(tree);
1751 int res = (validate_ntype(tree, yield_expr)
1752 && ((nch == 1) || (nch == 2))
1753 && validate_name(CHILD(tree, 0), "yield"));
1754
1755 if (res && (nch == 2))
1756 res = validate_testlist(CHILD(tree, 1));
1757
1758 return (res);
1759}
1760
1761
1762/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001763 */
1764static int
1765validate_yield_stmt(node *tree)
1766{
1767 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001768 && validate_numnodes(tree, 1, "yield_stmt")
1769 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001770}
1771
1772
Fred Drakecff283c2000-08-21 22:24:43 +00001773static int
1774validate_import_as_name(node *tree)
1775{
1776 int nch = NCH(tree);
1777 int ok = validate_ntype(tree, import_as_name);
1778
1779 if (ok) {
1780 if (nch == 1)
1781 ok = validate_name(CHILD(tree, 0), NULL);
1782 else if (nch == 3)
1783 ok = (validate_name(CHILD(tree, 0), NULL)
1784 && validate_name(CHILD(tree, 1), "as")
1785 && validate_name(CHILD(tree, 2), NULL));
1786 else
1787 ok = validate_numnodes(tree, 3, "import_as_name");
1788 }
1789 return ok;
1790}
1791
1792
Fred Drake71137082001-01-07 05:59:59 +00001793/* dotted_name: NAME ("." NAME)*
1794 */
1795static int
1796validate_dotted_name(node *tree)
1797{
1798 int nch = NCH(tree);
1799 int res = (validate_ntype(tree, dotted_name)
1800 && is_odd(nch)
1801 && validate_name(CHILD(tree, 0), NULL));
1802 int i;
1803
1804 for (i = 1; res && (i < nch); i += 2) {
1805 res = (validate_dot(CHILD(tree, i))
1806 && validate_name(CHILD(tree, i+1), NULL));
1807 }
1808 return res;
1809}
1810
1811
Fred Drakecff283c2000-08-21 22:24:43 +00001812/* dotted_as_name: dotted_name [NAME NAME]
1813 */
1814static int
1815validate_dotted_as_name(node *tree)
1816{
1817 int nch = NCH(tree);
1818 int res = validate_ntype(tree, dotted_as_name);
1819
1820 if (res) {
1821 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001822 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001823 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001824 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001825 && validate_name(CHILD(tree, 1), "as")
1826 && validate_name(CHILD(tree, 2), NULL));
1827 else {
1828 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001829 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001830 }
1831 }
1832 return res;
1833}
1834
1835
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001836/* dotted_as_name (',' dotted_as_name)* */
1837static int
1838validate_dotted_as_names(node *tree)
1839{
1840 int nch = NCH(tree);
1841 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1842 int i;
1843
1844 for (i = 1; res && (i < nch); i += 2)
1845 res = (validate_comma(CHILD(tree, i))
1846 && validate_dotted_as_name(CHILD(tree, i + 1)));
1847 return (res);
1848}
1849
1850
1851/* import_as_name (',' import_as_name)* [','] */
1852static int
1853validate_import_as_names(node *tree)
1854{
1855 int nch = NCH(tree);
1856 int res = validate_import_as_name(CHILD(tree, 0));
1857 int i;
1858
1859 for (i = 1; res && (i + 1 < nch); i += 2)
1860 res = (validate_comma(CHILD(tree, i))
1861 && validate_import_as_name(CHILD(tree, i + 1)));
1862 return (res);
1863}
1864
1865
1866/* 'import' dotted_as_names */
1867static int
1868validate_import_name(node *tree)
1869{
1870 return (validate_ntype(tree, import_name)
1871 && validate_numnodes(tree, 2, "import_name")
1872 && validate_name(CHILD(tree, 0), "import")
1873 && validate_dotted_as_names(CHILD(tree, 1)));
1874}
1875
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001876/* Helper function to count the number of leading dots in
1877 * 'from ...module import name'
1878 */
1879static int
1880count_from_dots(node *tree)
1881{
1882 int i;
Benjamin Peterson6624a9f2008-11-03 15:14:51 +00001883 for (i = 1; i < NCH(tree); i++)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001884 if (TYPE(CHILD(tree, i)) != DOT)
1885 break;
Benjamin Peterson6624a9f2008-11-03 15:14:51 +00001886 return i-1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001887}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001888
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001889/* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001890 * import_as_names
Guido van Rossum3d602e31996-07-21 02:33:56 +00001891 */
Guido van Rossum47478871996-08-21 14:32:37 +00001892static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001893validate_import_from(node *tree)
1894{
1895 int nch = NCH(tree);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001896 int ndots = count_from_dots(tree);
1897 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1898 int offset = ndots + havename;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001899 int res = validate_ntype(tree, import_from)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001900 && (nch >= 4 + ndots)
1901 && validate_name(CHILD(tree, 0), "from")
1902 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1903 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001904
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001905 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1906 res = ((nch == offset + 5)
1907 && validate_lparen(CHILD(tree, offset + 2))
1908 && validate_import_as_names(CHILD(tree, offset + 3))
1909 && validate_rparen(CHILD(tree, offset + 4)));
1910 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1911 res = validate_import_as_names(CHILD(tree, offset + 2));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001912 return (res);
1913}
1914
1915
1916/* import_stmt: import_name | import_from */
1917static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001918validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001919{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001920 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001921 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001922
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001923 if (res) {
1924 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001925
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001926 if (ntype == import_name || ntype == import_from)
1927 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001928 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001929 res = 0;
1930 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001931 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001932 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001933 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001934 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001935 PyErr_Format(parser_error,
1936 "Unrecognized child node of import_stmt: %d.",
1937 TYPE(CHILD(tree, 0)));
1938 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001939 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001940}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001941
1942
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001943
1944
Guido van Rossum47478871996-08-21 14:32:37 +00001945static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001946validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001947{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001948 int j;
1949 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001950 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001951 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001952
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001953 if (!res && !PyErr_Occurred())
1954 err_string("illegal global statement");
1955
Guido van Rossum3d602e31996-07-21 02:33:56 +00001956 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001957 res = (validate_name(CHILD(tree, 0), "global")
1958 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001959 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001960 res = (validate_comma(CHILD(tree, j))
1961 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001962
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001963 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001964}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001965
1966
Guido van Rossum3d602e31996-07-21 02:33:56 +00001967/* exec_stmt:
1968 *
1969 * 'exec' expr ['in' test [',' test]]
1970 */
Guido van Rossum47478871996-08-21 14:32:37 +00001971static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001972validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001973{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001974 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001975 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001976 && ((nch == 2) || (nch == 4) || (nch == 6))
1977 && validate_name(CHILD(tree, 0), "exec")
1978 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001979
Guido van Rossum3d602e31996-07-21 02:33:56 +00001980 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001981 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001982 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001983 res = (validate_name(CHILD(tree, 2), "in")
1984 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001985 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00001986 res = (validate_comma(CHILD(tree, 4))
1987 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001988
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001989 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001990}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001991
1992
Guido van Rossum925e5471997-04-02 05:32:13 +00001993/* assert_stmt:
1994 *
1995 * 'assert' test [',' test]
1996 */
1997static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001998validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001999{
2000 int nch = NCH(tree);
2001 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002002 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00002003 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002004 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00002005
2006 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002007 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00002008 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002009 res = (validate_comma(CHILD(tree, 2))
2010 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00002011
2012 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002013}
Guido van Rossum925e5471997-04-02 05:32:13 +00002014
2015
Guido van Rossum47478871996-08-21 14:32:37 +00002016static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002017validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002018{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002019 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002020 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002021 && ((nch == 4) || (nch == 7))
2022 && validate_name(CHILD(tree, 0), "while")
2023 && validate_test(CHILD(tree, 1))
2024 && validate_colon(CHILD(tree, 2))
2025 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002026
Guido van Rossum3d602e31996-07-21 02:33:56 +00002027 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00002028 res = (validate_name(CHILD(tree, 4), "else")
2029 && validate_colon(CHILD(tree, 5))
2030 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002031
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002032 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002033}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002034
2035
Guido van Rossum47478871996-08-21 14:32:37 +00002036static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002037validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002038{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002039 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002040 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002041 && ((nch == 6) || (nch == 9))
2042 && validate_name(CHILD(tree, 0), "for")
2043 && validate_exprlist(CHILD(tree, 1))
2044 && validate_name(CHILD(tree, 2), "in")
2045 && validate_testlist(CHILD(tree, 3))
2046 && validate_colon(CHILD(tree, 4))
2047 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002048
Guido van Rossum3d602e31996-07-21 02:33:56 +00002049 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00002050 res = (validate_name(CHILD(tree, 6), "else")
2051 && validate_colon(CHILD(tree, 7))
2052 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002053
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002054 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002055}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002056
2057
Guido van Rossum3d602e31996-07-21 02:33:56 +00002058/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00002059 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Georg Brandlfe879e82008-12-05 12:09:41 +00002060 ['finally' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002061 * | 'try' ':' suite 'finally' ':' suite
2062 *
2063 */
Guido van Rossum47478871996-08-21 14:32:37 +00002064static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002065validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002066{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002067 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002068 int pos = 3;
2069 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002070 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002071
2072 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002073 res = (validate_name(CHILD(tree, 0), "try")
2074 && validate_colon(CHILD(tree, 1))
2075 && validate_suite(CHILD(tree, 2))
2076 && validate_colon(CHILD(tree, nch - 2))
2077 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00002078 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00002079 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00002080 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
2081 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00002082
2083 PyErr_Format(parser_error,
2084 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002085 }
Georg Brandlfe879e82008-12-05 12:09:41 +00002086 /* Handle try/finally statement */
2087 if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
2088 (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
2089 res = (validate_numnodes(tree, 6, "try/finally")
2090 && validate_colon(CHILD(tree, 4))
2091 && validate_suite(CHILD(tree, 5)));
2092 return (res);
2093 }
2094 /* try/except statement: skip past except_clause sections */
Antoine Pitrou42b5bcf2009-05-14 21:48:09 +00002095 while (res && pos < nch && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002096 res = (validate_except_clause(CHILD(tree, pos))
2097 && validate_colon(CHILD(tree, pos + 1))
2098 && validate_suite(CHILD(tree, pos + 2)));
2099 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002100 }
Georg Brandlfe879e82008-12-05 12:09:41 +00002101 /* skip else clause */
Antoine Pitrou42b5bcf2009-05-14 21:48:09 +00002102 if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) &&
Georg Brandlfe879e82008-12-05 12:09:41 +00002103 (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
2104 res = (validate_colon(CHILD(tree, pos + 1))
2105 && validate_suite(CHILD(tree, pos + 2)));
2106 pos += 3;
2107 }
2108 if (res && pos < nch) {
2109 /* last clause must be a finally */
2110 res = (validate_name(CHILD(tree, pos), "finally")
2111 && validate_numnodes(tree, pos + 3, "try/except/finally")
2112 && validate_colon(CHILD(tree, pos + 1))
2113 && validate_suite(CHILD(tree, pos + 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002114 }
2115 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002116}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002117
2118
Guido van Rossum47478871996-08-21 14:32:37 +00002119static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002120validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002121{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002122 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002123 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00002124 && ((nch == 1) || (nch == 2) || (nch == 4))
2125 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002126
Guido van Rossum3d602e31996-07-21 02:33:56 +00002127 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002128 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002129 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002130 res = (validate_comma(CHILD(tree, 2))
2131 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002132
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002133 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002134}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002135
2136
Guido van Rossum47478871996-08-21 14:32:37 +00002137static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002138validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002139{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002140 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002141 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002142
Guido van Rossum3d602e31996-07-21 02:33:56 +00002143 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002144 res = ((nch == 1)
2145 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002146 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002147 res = validate_or_test(CHILD(tree, 0));
2148 res = (res && (nch == 1 || (nch == 5 &&
2149 validate_name(CHILD(tree, 1), "if") &&
2150 validate_or_test(CHILD(tree, 2)) &&
2151 validate_name(CHILD(tree, 3), "else") &&
2152 validate_test(CHILD(tree, 4)))));
2153 }
2154 return (res);
2155}
2156
2157static int
2158validate_old_test(node *tree)
2159{
2160 int nch = NCH(tree);
2161 int res = validate_ntype(tree, old_test) && (nch == 1);
2162
2163 if (res && (TYPE(CHILD(tree, 0)) == old_lambdef))
2164 res = (validate_old_lambdef(CHILD(tree, 0)));
2165 else if (res) {
2166 res = (validate_or_test(CHILD(tree, 0)));
2167 }
2168 return (res);
2169}
2170
2171static int
2172validate_or_test(node *tree)
2173{
2174 int nch = NCH(tree);
2175 int res = validate_ntype(tree, or_test) && is_odd(nch);
2176
2177 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002178 int pos;
2179 res = validate_and_test(CHILD(tree, 0));
2180 for (pos = 1; res && (pos < nch); pos += 2)
2181 res = (validate_name(CHILD(tree, pos), "or")
2182 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002183 }
2184 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002185}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002186
2187
Guido van Rossum47478871996-08-21 14:32:37 +00002188static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002189validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002190{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002191 int pos;
2192 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002193 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002194 && is_odd(nch)
2195 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002196
Guido van Rossum3d602e31996-07-21 02:33:56 +00002197 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002198 res = (validate_name(CHILD(tree, pos), "and")
2199 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002200
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002201 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002202}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002203
2204
Guido van Rossum47478871996-08-21 14:32:37 +00002205static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002206validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002207{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002208 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002209 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002210
Guido van Rossum3d602e31996-07-21 02:33:56 +00002211 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002212 if (nch == 2)
2213 res = (validate_name(CHILD(tree, 0), "not")
2214 && validate_not_test(CHILD(tree, 1)));
2215 else if (nch == 1)
2216 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002217 }
2218 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002219}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002220
2221
Guido van Rossum47478871996-08-21 14:32:37 +00002222static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002223validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002224{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002225 int pos;
2226 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002227 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002228 && is_odd(nch)
2229 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002230
Guido van Rossum3d602e31996-07-21 02:33:56 +00002231 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002232 res = (validate_comp_op(CHILD(tree, pos))
2233 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002234
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002235 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002236}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002237
2238
Guido van Rossum47478871996-08-21 14:32:37 +00002239static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002240validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002241{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002242 int res = 0;
2243 int nch = NCH(tree);
2244
Guido van Rossum3d602e31996-07-21 02:33:56 +00002245 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002246 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002247 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002248 /*
2249 * Only child will be a terminal with a well-defined symbolic name
2250 * or a NAME with a string of either 'is' or 'in'
2251 */
2252 tree = CHILD(tree, 0);
2253 switch (TYPE(tree)) {
2254 case LESS:
2255 case GREATER:
2256 case EQEQUAL:
2257 case EQUAL:
2258 case LESSEQUAL:
2259 case GREATEREQUAL:
2260 case NOTEQUAL:
2261 res = 1;
2262 break;
2263 case NAME:
2264 res = ((strcmp(STR(tree), "in") == 0)
2265 || (strcmp(STR(tree), "is") == 0));
2266 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002267 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002268 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002269 }
2270 break;
2271 default:
Fred Drake661ea262000-10-24 19:57:45 +00002272 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002273 break;
2274 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002275 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002276 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002277 res = (validate_ntype(CHILD(tree, 0), NAME)
2278 && validate_ntype(CHILD(tree, 1), NAME)
2279 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2280 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2281 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2282 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2283 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002284 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002285 }
2286 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002287}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002288
2289
Guido van Rossum47478871996-08-21 14:32:37 +00002290static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002291validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002292{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002293 int j;
2294 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002295 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002296 && is_odd(nch)
2297 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002298
Guido van Rossum3d602e31996-07-21 02:33:56 +00002299 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002300 res = (validate_xor_expr(CHILD(tree, j))
2301 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002302
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002303 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002304}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002305
2306
Guido van Rossum47478871996-08-21 14:32:37 +00002307static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002308validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002309{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002310 int j;
2311 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002312 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002313 && is_odd(nch)
2314 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002315
Guido van Rossum3d602e31996-07-21 02:33:56 +00002316 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002317 res = (validate_circumflex(CHILD(tree, j - 1))
2318 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002319
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002320 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002321}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002322
2323
Guido van Rossum47478871996-08-21 14:32:37 +00002324static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002325validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002326{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002327 int pos;
2328 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002329 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002330 && is_odd(nch)
2331 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002332
Guido van Rossum3d602e31996-07-21 02:33:56 +00002333 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002334 res = (validate_ampersand(CHILD(tree, pos))
2335 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002336
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002337 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002338}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002339
2340
2341static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002342validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002343 {
2344 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002345 int nch = NCH(tree);
2346 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002347 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002348
Guido van Rossum3d602e31996-07-21 02:33:56 +00002349 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002350 if (TYPE(CHILD(tree, pos)) != op1)
2351 res = validate_ntype(CHILD(tree, pos), op2);
2352 if (res)
2353 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002354 }
2355 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002356}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002357
2358
Guido van Rossum47478871996-08-21 14:32:37 +00002359static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002360validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002361{
2362 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002363 && validate_chain_two_ops(tree, validate_arith_expr,
2364 LEFTSHIFT, RIGHTSHIFT));
2365}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002366
2367
Guido van Rossum47478871996-08-21 14:32:37 +00002368static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002369validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002370{
2371 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002372 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2373}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002374
2375
Guido van Rossum47478871996-08-21 14:32:37 +00002376static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002377validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002378{
2379 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002380 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002381 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002382 && is_odd(nch)
2383 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002384
Guido van Rossum3d602e31996-07-21 02:33:56 +00002385 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002386 res = (((TYPE(CHILD(tree, pos)) == STAR)
2387 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002388 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002389 || (TYPE(CHILD(tree, pos)) == PERCENT))
2390 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002391
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002392 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002393}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002394
2395
Guido van Rossum3d602e31996-07-21 02:33:56 +00002396/* factor:
2397 *
2398 * factor: ('+'|'-'|'~') factor | power
2399 */
Guido van Rossum47478871996-08-21 14:32:37 +00002400static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002401validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002402{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002403 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002404 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002405 && (((nch == 2)
2406 && ((TYPE(CHILD(tree, 0)) == PLUS)
2407 || (TYPE(CHILD(tree, 0)) == MINUS)
2408 || (TYPE(CHILD(tree, 0)) == TILDE))
2409 && validate_factor(CHILD(tree, 1)))
2410 || ((nch == 1)
2411 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002412 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002413}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002414
2415
Guido van Rossum3d602e31996-07-21 02:33:56 +00002416/* power:
2417 *
2418 * power: atom trailer* ('**' factor)*
2419 */
Guido van Rossum47478871996-08-21 14:32:37 +00002420static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002421validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002422{
2423 int pos = 1;
2424 int nch = NCH(tree);
2425 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002426 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002427
2428 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002429 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002430 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002431 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002432 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002433 return (0);
2434 }
2435 for ( ; res && (pos < (nch - 1)); pos += 2)
2436 res = (validate_doublestar(CHILD(tree, pos))
2437 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002438 }
2439 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002440}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002441
2442
Guido van Rossum47478871996-08-21 14:32:37 +00002443static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002444validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002445{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002446 int pos;
2447 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002448 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002449
Fred Drakecff283c2000-08-21 22:24:43 +00002450 if (res && nch < 1)
2451 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002452 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002453 switch (TYPE(CHILD(tree, 0))) {
2454 case LPAR:
2455 res = ((nch <= 3)
2456 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002457
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002458 if (res && (nch == 3)) {
2459 if (TYPE(CHILD(tree, 1))==yield_expr)
2460 res = validate_yield_expr(CHILD(tree, 1));
2461 else
2462 res = validate_testlist_gexp(CHILD(tree, 1));
2463 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002464 break;
2465 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002466 if (nch == 2)
2467 res = validate_ntype(CHILD(tree, 1), RSQB);
2468 else if (nch == 3)
2469 res = (validate_listmaker(CHILD(tree, 1))
2470 && validate_ntype(CHILD(tree, 2), RSQB));
2471 else {
2472 res = 0;
2473 err_string("illegal list display atom");
2474 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002475 break;
2476 case LBRACE:
2477 res = ((nch <= 3)
2478 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002479
Fred Drakeff9ea482000-04-19 13:54:15 +00002480 if (res && (nch == 3))
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002481 res = validate_dictorsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002482 break;
2483 case BACKQUOTE:
2484 res = ((nch == 3)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002485 && validate_testlist1(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002486 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2487 break;
2488 case NAME:
2489 case NUMBER:
2490 res = (nch == 1);
2491 break;
2492 case STRING:
2493 for (pos = 1; res && (pos < nch); ++pos)
2494 res = validate_ntype(CHILD(tree, pos), STRING);
2495 break;
2496 default:
2497 res = 0;
2498 break;
2499 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002500 }
2501 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002502}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002503
2504
Fred Drake85bf3bb2000-08-23 15:35:26 +00002505/* listmaker:
2506 * test ( list_for | (',' test)* [','] )
2507 */
Fred Drakecff283c2000-08-21 22:24:43 +00002508static int
2509validate_listmaker(node *tree)
2510{
2511 int nch = NCH(tree);
2512 int ok = nch;
2513
2514 if (nch == 0)
2515 err_string("missing child nodes of listmaker");
2516 else
2517 ok = validate_test(CHILD(tree, 0));
2518
2519 /*
Raymond Hettinger354433a2004-05-19 08:20:33 +00002520 * list_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002521 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002522 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2523 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002524 else {
2525 /* (',' test)* [','] */
2526 int i = 1;
2527 while (ok && nch - i >= 2) {
2528 ok = (validate_comma(CHILD(tree, i))
2529 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002530 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002531 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002532 if (ok && i == nch-1)
2533 ok = validate_comma(CHILD(tree, i));
2534 else if (i != nch) {
2535 ok = 0;
2536 err_string("illegal trailing nodes for listmaker");
2537 }
Fred Drakecff283c2000-08-21 22:24:43 +00002538 }
2539 return ok;
2540}
2541
Raymond Hettinger354433a2004-05-19 08:20:33 +00002542/* testlist_gexp:
2543 * test ( gen_for | (',' test)* [','] )
2544 */
2545static int
2546validate_testlist_gexp(node *tree)
2547{
2548 int nch = NCH(tree);
2549 int ok = nch;
2550
2551 if (nch == 0)
2552 err_string("missing child nodes of testlist_gexp");
2553 else {
2554 ok = validate_test(CHILD(tree, 0));
2555 }
2556
2557 /*
2558 * gen_for | (',' test)* [',']
2559 */
2560 if (nch == 2 && TYPE(CHILD(tree, 1)) == gen_for)
2561 ok = validate_gen_for(CHILD(tree, 1));
2562 else {
2563 /* (',' test)* [','] */
2564 int i = 1;
2565 while (ok && nch - i >= 2) {
2566 ok = (validate_comma(CHILD(tree, i))
2567 && validate_test(CHILD(tree, i+1)));
2568 i += 2;
2569 }
2570 if (ok && i == nch-1)
2571 ok = validate_comma(CHILD(tree, i));
2572 else if (i != nch) {
2573 ok = 0;
2574 err_string("illegal trailing nodes for testlist_gexp");
2575 }
2576 }
2577 return ok;
2578}
Fred Drakecff283c2000-08-21 22:24:43 +00002579
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002580/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002581 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002582 */
2583static int
2584validate_decorator(node *tree)
2585{
2586 int ok;
2587 int nch = NCH(tree);
2588 ok = (validate_ntype(tree, decorator) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002589 (nch == 3 || nch == 5 || nch == 6) &&
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002590 validate_at(CHILD(tree, 0)) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002591 validate_dotted_name(CHILD(tree, 1)) &&
2592 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002593
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002594 if (ok && nch != 3) {
2595 ok = (validate_lparen(CHILD(tree, 2)) &&
2596 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002597
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002598 if (ok && nch == 6)
2599 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002600 }
2601
2602 return ok;
2603}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002604
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002605/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002606 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002607 */
2608static int
2609validate_decorators(node *tree)
2610{
2611 int i, nch, ok;
2612 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002613 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002614
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002615 for (i = 0; ok && i < nch; ++i)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002616 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002617
2618 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002619}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002620
Georg Brandl944f6842009-05-25 21:02:56 +00002621/* with_item:
2622 * test ['as' expr]
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002623 */
2624static int
Georg Brandl944f6842009-05-25 21:02:56 +00002625validate_with_item(node *tree)
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002626{
2627 int nch = NCH(tree);
Georg Brandl944f6842009-05-25 21:02:56 +00002628 int ok = (validate_ntype(tree, with_item)
2629 && (nch == 1 || nch == 3)
2630 && validate_test(CHILD(tree, 0)));
2631 if (ok && nch == 3)
2632 ok = (validate_name(CHILD(tree, 1), "as")
2633 && validate_expr(CHILD(tree, 2)));
2634 return ok;
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002635}
2636
Georg Brandl944f6842009-05-25 21:02:56 +00002637/* with_stmt:
2638 * 0 1 ... -2 -1
2639 * 'with' with_item (',' with_item)* ':' suite
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002640 */
2641static int
2642validate_with_stmt(node *tree)
2643{
Georg Brandl944f6842009-05-25 21:02:56 +00002644 int i;
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002645 int nch = NCH(tree);
2646 int ok = (validate_ntype(tree, with_stmt)
Georg Brandl944f6842009-05-25 21:02:56 +00002647 && (nch % 2 == 0)
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002648 && validate_name(CHILD(tree, 0), "with")
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002649 && validate_colon(RCHILD(tree, -2))
2650 && validate_suite(RCHILD(tree, -1)));
Georg Brandl944f6842009-05-25 21:02:56 +00002651 for (i = 1; ok && i < nch - 2; i += 2)
2652 ok = validate_with_item(CHILD(tree, i));
2653 return ok;
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002654}
2655
Guido van Rossum3d602e31996-07-21 02:33:56 +00002656/* funcdef:
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002657 *
Christian Heimes5224d282008-02-23 15:01:05 +00002658 * -5 -4 -3 -2 -1
2659 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002660 */
Guido van Rossum47478871996-08-21 14:32:37 +00002661static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002662validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002663{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002664 int nch = NCH(tree);
2665 int ok = (validate_ntype(tree, funcdef)
Christian Heimes5224d282008-02-23 15:01:05 +00002666 && (nch == 5)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002667 && validate_name(RCHILD(tree, -5), "def")
2668 && validate_ntype(RCHILD(tree, -4), NAME)
2669 && validate_colon(RCHILD(tree, -2))
2670 && validate_parameters(RCHILD(tree, -3))
2671 && validate_suite(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002672 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002673}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002674
2675
Christian Heimes5224d282008-02-23 15:01:05 +00002676/* decorated
2677 * decorators (classdef | funcdef)
2678 */
2679static int
2680validate_decorated(node *tree)
2681{
2682 int nch = NCH(tree);
2683 int ok = (validate_ntype(tree, decorated)
2684 && (nch == 2)
2685 && validate_decorators(RCHILD(tree, -2))
2686 && (validate_funcdef(RCHILD(tree, -1))
2687 || validate_class(RCHILD(tree, -1)))
2688 );
2689 return ok;
2690}
2691
Guido van Rossum47478871996-08-21 14:32:37 +00002692static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002693validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002694{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002695 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002696 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002697 && ((nch == 3) || (nch == 4))
2698 && validate_name(CHILD(tree, 0), "lambda")
2699 && validate_colon(CHILD(tree, nch - 2))
2700 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002701
Guido van Rossum3d602e31996-07-21 02:33:56 +00002702 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002703 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002704 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002705 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002706
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002707 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002708}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002709
2710
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002711static int
2712validate_old_lambdef(node *tree)
2713{
2714 int nch = NCH(tree);
2715 int res = (validate_ntype(tree, old_lambdef)
2716 && ((nch == 3) || (nch == 4))
2717 && validate_name(CHILD(tree, 0), "lambda")
2718 && validate_colon(CHILD(tree, nch - 2))
2719 && validate_test(CHILD(tree, nch - 1)));
2720
2721 if (res && (nch == 4))
2722 res = validate_varargslist(CHILD(tree, 1));
2723 else if (!res && !PyErr_Occurred())
2724 (void) validate_numnodes(tree, 3, "old_lambdef");
2725
2726 return (res);
2727}
2728
2729
Guido van Rossum3d602e31996-07-21 02:33:56 +00002730/* arglist:
2731 *
Fred Drakecff283c2000-08-21 22:24:43 +00002732 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002733 */
Guido van Rossum47478871996-08-21 14:32:37 +00002734static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002735validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002736{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002737 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002738 int i = 0;
2739 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002740
2741 if (nch <= 0)
2742 /* raise the right error from having an invalid number of children */
2743 return validate_numnodes(tree, nch + 1, "arglist");
2744
Raymond Hettinger354433a2004-05-19 08:20:33 +00002745 if (nch > 1) {
2746 for (i=0; i<nch; i++) {
2747 if (TYPE(CHILD(tree, i)) == argument) {
2748 node *ch = CHILD(tree, i);
2749 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == gen_for) {
2750 err_string("need '(', ')' for generator expression");
2751 return 0;
2752 }
2753 }
2754 }
2755 }
2756
Fred Drakecff283c2000-08-21 22:24:43 +00002757 while (ok && nch-i >= 2) {
2758 /* skip leading (argument ',') */
2759 ok = (validate_argument(CHILD(tree, i))
2760 && validate_comma(CHILD(tree, i+1)));
2761 if (ok)
2762 i += 2;
2763 else
2764 PyErr_Clear();
2765 }
2766 ok = 1;
2767 if (nch-i > 0) {
2768 /*
2769 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002770 */
Fred Drakecff283c2000-08-21 22:24:43 +00002771 int sym = TYPE(CHILD(tree, i));
2772
2773 if (sym == argument) {
2774 ok = validate_argument(CHILD(tree, i));
2775 if (ok && i+1 != nch) {
2776 err_string("illegal arglist specification"
2777 " (extra stuff on end)");
2778 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002779 }
Fred Drakecff283c2000-08-21 22:24:43 +00002780 }
2781 else if (sym == STAR) {
2782 ok = validate_star(CHILD(tree, i));
2783 if (ok && (nch-i == 2))
2784 ok = validate_test(CHILD(tree, i+1));
2785 else if (ok && (nch-i == 5))
2786 ok = (validate_test(CHILD(tree, i+1))
2787 && validate_comma(CHILD(tree, i+2))
2788 && validate_doublestar(CHILD(tree, i+3))
2789 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002790 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002791 err_string("illegal use of '*' in arglist");
2792 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002793 }
2794 }
Fred Drakecff283c2000-08-21 22:24:43 +00002795 else if (sym == DOUBLESTAR) {
2796 if (nch-i == 2)
2797 ok = (validate_doublestar(CHILD(tree, i))
2798 && validate_test(CHILD(tree, i+1)));
2799 else {
2800 err_string("illegal use of '**' in arglist");
2801 ok = 0;
2802 }
2803 }
2804 else {
2805 err_string("illegal arglist specification");
2806 ok = 0;
2807 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002808 }
2809 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002810}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002811
2812
2813
2814/* argument:
2815 *
Raymond Hettinger354433a2004-05-19 08:20:33 +00002816 * [test '='] test [gen_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002817 */
Guido van Rossum47478871996-08-21 14:32:37 +00002818static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002819validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002820{
2821 int nch = NCH(tree);
2822 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002823 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002824 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002825
Raymond Hettinger354433a2004-05-19 08:20:33 +00002826 if (res && (nch == 2))
2827 res = validate_gen_for(CHILD(tree, 1));
2828 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002829 res = (validate_equal(CHILD(tree, 1))
2830 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002831
2832 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002833}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002834
2835
2836
2837/* trailer:
2838 *
Guido van Rossum47478871996-08-21 14:32:37 +00002839 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002840 */
Guido van Rossum47478871996-08-21 14:32:37 +00002841static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002842validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002843{
2844 int nch = NCH(tree);
2845 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002846
2847 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002848 switch (TYPE(CHILD(tree, 0))) {
2849 case LPAR:
2850 res = validate_rparen(CHILD(tree, nch - 1));
2851 if (res && (nch == 3))
2852 res = validate_arglist(CHILD(tree, 1));
2853 break;
2854 case LSQB:
2855 res = (validate_numnodes(tree, 3, "trailer")
2856 && validate_subscriptlist(CHILD(tree, 1))
2857 && validate_ntype(CHILD(tree, 2), RSQB));
2858 break;
2859 case DOT:
2860 res = (validate_numnodes(tree, 2, "trailer")
2861 && validate_ntype(CHILD(tree, 1), NAME));
2862 break;
2863 default:
2864 res = 0;
2865 break;
2866 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002867 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002868 else {
2869 (void) validate_numnodes(tree, 2, "trailer");
2870 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002871 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002872}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002873
2874
Guido van Rossum47478871996-08-21 14:32:37 +00002875/* subscriptlist:
2876 *
2877 * subscript (',' subscript)* [',']
2878 */
2879static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002880validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002881{
2882 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002883 validate_subscript, "subscriptlist"));
2884}
Guido van Rossum47478871996-08-21 14:32:37 +00002885
2886
Guido van Rossum3d602e31996-07-21 02:33:56 +00002887/* subscript:
2888 *
Guido van Rossum47478871996-08-21 14:32:37 +00002889 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002890 */
Guido van Rossum47478871996-08-21 14:32:37 +00002891static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002892validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002893{
Guido van Rossum47478871996-08-21 14:32:37 +00002894 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002895 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002896 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002897
Guido van Rossum47478871996-08-21 14:32:37 +00002898 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002899 if (!PyErr_Occurred())
2900 err_string("invalid number of arguments for subscript node");
2901 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002902 }
Guido van Rossum47478871996-08-21 14:32:37 +00002903 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002904 /* take care of ('.' '.' '.') possibility */
2905 return (validate_numnodes(tree, 3, "subscript")
2906 && validate_dot(CHILD(tree, 0))
2907 && validate_dot(CHILD(tree, 1))
2908 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002909 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002910 if (TYPE(CHILD(tree, 0)) == test)
2911 res = validate_test(CHILD(tree, 0));
2912 else
2913 res = validate_colon(CHILD(tree, 0));
2914 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002915 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002916 /* Must be [test] ':' [test] [sliceop],
2917 * but at least one of the optional components will
2918 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002919 */
2920 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002921 res = validate_test(CHILD(tree, 0));
2922 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002923 }
2924 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002925 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002926 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002927 int rem = nch - ++offset;
2928 if (rem) {
2929 if (TYPE(CHILD(tree, offset)) == test) {
2930 res = validate_test(CHILD(tree, offset));
2931 ++offset;
2932 --rem;
2933 }
2934 if (res && rem)
2935 res = validate_sliceop(CHILD(tree, offset));
2936 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002937 }
2938 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002939}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002940
2941
Guido van Rossum47478871996-08-21 14:32:37 +00002942static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002943validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002944{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002945 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002946 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002947 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002948 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002949 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002950 }
Guido van Rossum47478871996-08-21 14:32:37 +00002951 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002952 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002953 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002954 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002955
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002956 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002957}
Guido van Rossum47478871996-08-21 14:32:37 +00002958
2959
2960static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002961validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002962{
2963 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002964 validate_expr, "exprlist"));
2965}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002966
2967
Guido van Rossum47478871996-08-21 14:32:37 +00002968static int
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002969validate_dictorsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002970{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002971 int nch = NCH(tree);
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002972 int ok = validate_ntype(tree, dictorsetmaker);
2973 int i = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002974
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002975 assert(nch > 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002976
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002977 if (ok && (nch == 1 || TYPE(CHILD(tree, 1)) == COMMA)) {
2978 /* We got a set:
2979 * test (',' test)* [',']
2980 */
2981 ok = validate_test(CHILD(tree, i++));
2982 while (ok && nch - i >= 2) {
2983 ok = (validate_comma(CHILD(tree, i))
2984 && validate_test(CHILD(tree, i+1)));
2985 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00002986 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002987 }
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002988 else if (ok) {
2989 /* We got a dict:
2990 * test ':' test (',' test ':' test)* [',']
2991 */
2992 if (nch >= 3) {
2993 ok = (validate_test(CHILD(tree, i))
2994 && validate_colon(CHILD(tree, i+1))
2995 && validate_test(CHILD(tree, i+2)));
2996 i += 3;
2997 }
2998 else {
2999 ok = 0;
3000 err_string("illegal number of nodes for dictorsetmaker");
3001 }
3002
3003 while (ok && nch - i >= 4) {
3004 ok = (validate_comma(CHILD(tree, i))
3005 && validate_test(CHILD(tree, i+1))
3006 && validate_colon(CHILD(tree, i+2))
3007 && validate_test(CHILD(tree, i+3)));
3008 i += 4;
3009 }
3010 }
3011 /* Check for a trailing comma. */
3012 if (ok) {
3013 if (i == nch-1)
3014 ok = validate_comma(CHILD(tree, i));
3015 else if (i != nch) {
3016 ok = 0;
3017 err_string("illegal trailing nodes for dictorsetmaker");
3018 }
3019 }
3020
3021 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00003022}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003023
3024
Guido van Rossum47478871996-08-21 14:32:37 +00003025static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003026validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003027{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003028 int pos;
3029 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003030 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00003031 && (nch >= 2)
3032 && validate_testlist(CHILD(tree, 0))
3033 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003034
Guido van Rossum3d602e31996-07-21 02:33:56 +00003035 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00003036 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003037
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003038 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003039}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003040
3041
Guido van Rossum47478871996-08-21 14:32:37 +00003042static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003043validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003044{
Fred Drakeff9ea482000-04-19 13:54:15 +00003045 int nch = 0; /* num. children on current node */
3046 int res = 1; /* result value */
3047 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00003048
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00003049 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003050 nch = NCH(tree);
3051 next = 0;
3052 switch (TYPE(tree)) {
3053 /*
3054 * Definition nodes.
3055 */
3056 case funcdef:
3057 res = validate_funcdef(tree);
3058 break;
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00003059 case with_stmt:
3060 res = validate_with_stmt(tree);
3061 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003062 case classdef:
3063 res = validate_class(tree);
3064 break;
Christian Heimes5224d282008-02-23 15:01:05 +00003065 case decorated:
3066 res = validate_decorated(tree);
3067 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003068 /*
3069 * "Trivial" parse tree nodes.
3070 * (Why did I call these trivial?)
3071 */
3072 case stmt:
3073 res = validate_stmt(tree);
3074 break;
3075 case small_stmt:
3076 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00003077 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00003078 * | import_stmt | global_stmt | exec_stmt | assert_stmt
3079 */
3080 res = validate_small_stmt(tree);
3081 break;
3082 case flow_stmt:
3083 res = (validate_numnodes(tree, 1, "flow_stmt")
3084 && ((TYPE(CHILD(tree, 0)) == break_stmt)
3085 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00003086 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00003087 || (TYPE(CHILD(tree, 0)) == return_stmt)
3088 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
3089 if (res)
3090 next = CHILD(tree, 0);
3091 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00003092 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003093 break;
Fred Drake02126f22001-07-17 02:59:15 +00003094 case yield_stmt:
3095 res = validate_yield_stmt(tree);
3096 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003097 /*
3098 * Compound statements.
3099 */
3100 case simple_stmt:
3101 res = validate_simple_stmt(tree);
3102 break;
3103 case compound_stmt:
3104 res = validate_compound_stmt(tree);
3105 break;
3106 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00003107 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00003108 */
3109 case expr_stmt:
3110 res = validate_expr_stmt(tree);
3111 break;
3112 case print_stmt:
3113 res = validate_print_stmt(tree);
3114 break;
3115 case del_stmt:
3116 res = validate_del_stmt(tree);
3117 break;
3118 case pass_stmt:
3119 res = (validate_numnodes(tree, 1, "pass")
3120 && validate_name(CHILD(tree, 0), "pass"));
3121 break;
3122 case break_stmt:
3123 res = (validate_numnodes(tree, 1, "break")
3124 && validate_name(CHILD(tree, 0), "break"));
3125 break;
3126 case continue_stmt:
3127 res = (validate_numnodes(tree, 1, "continue")
3128 && validate_name(CHILD(tree, 0), "continue"));
3129 break;
3130 case return_stmt:
3131 res = validate_return_stmt(tree);
3132 break;
3133 case raise_stmt:
3134 res = validate_raise_stmt(tree);
3135 break;
3136 case import_stmt:
3137 res = validate_import_stmt(tree);
3138 break;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00003139 case import_name:
3140 res = validate_import_name(tree);
3141 break;
3142 case import_from:
3143 res = validate_import_from(tree);
3144 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003145 case global_stmt:
3146 res = validate_global_stmt(tree);
3147 break;
3148 case exec_stmt:
3149 res = validate_exec_stmt(tree);
3150 break;
3151 case assert_stmt:
3152 res = validate_assert_stmt(tree);
3153 break;
3154 case if_stmt:
3155 res = validate_if(tree);
3156 break;
3157 case while_stmt:
3158 res = validate_while(tree);
3159 break;
3160 case for_stmt:
3161 res = validate_for(tree);
3162 break;
3163 case try_stmt:
3164 res = validate_try(tree);
3165 break;
3166 case suite:
3167 res = validate_suite(tree);
3168 break;
3169 /*
3170 * Expression nodes.
3171 */
3172 case testlist:
3173 res = validate_testlist(tree);
3174 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003175 case yield_expr:
3176 res = validate_yield_expr(tree);
3177 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00003178 case testlist1:
3179 res = validate_testlist1(tree);
3180 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003181 case test:
3182 res = validate_test(tree);
3183 break;
3184 case and_test:
3185 res = validate_and_test(tree);
3186 break;
3187 case not_test:
3188 res = validate_not_test(tree);
3189 break;
3190 case comparison:
3191 res = validate_comparison(tree);
3192 break;
3193 case exprlist:
3194 res = validate_exprlist(tree);
3195 break;
3196 case comp_op:
3197 res = validate_comp_op(tree);
3198 break;
3199 case expr:
3200 res = validate_expr(tree);
3201 break;
3202 case xor_expr:
3203 res = validate_xor_expr(tree);
3204 break;
3205 case and_expr:
3206 res = validate_and_expr(tree);
3207 break;
3208 case shift_expr:
3209 res = validate_shift_expr(tree);
3210 break;
3211 case arith_expr:
3212 res = validate_arith_expr(tree);
3213 break;
3214 case term:
3215 res = validate_term(tree);
3216 break;
3217 case factor:
3218 res = validate_factor(tree);
3219 break;
3220 case power:
3221 res = validate_power(tree);
3222 break;
3223 case atom:
3224 res = validate_atom(tree);
3225 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003226
Fred Drakeff9ea482000-04-19 13:54:15 +00003227 default:
3228 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00003229 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003230 res = 0;
3231 break;
3232 }
3233 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003234 }
3235 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003236}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003237
3238
Guido van Rossum47478871996-08-21 14:32:37 +00003239static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003240validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003241{
3242 int res = validate_eval_input(tree);
3243
3244 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003245 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003246
3247 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003248}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003249
3250
Guido van Rossum3d602e31996-07-21 02:33:56 +00003251/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003252 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003253 */
Guido van Rossum47478871996-08-21 14:32:37 +00003254static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003255validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003256{
Fred Drakec2683dd2001-07-17 19:32:05 +00003257 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003258 int nch = NCH(tree) - 1;
3259 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003260 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003261
Fred Drakec2683dd2001-07-17 19:32:05 +00003262 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003263 if (TYPE(CHILD(tree, j)) == stmt)
3264 res = validate_stmt(CHILD(tree, j));
3265 else
3266 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003267 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003268 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003269 * user. Hopefully, this won't be needed. If a user reports getting
3270 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003271 */
3272 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003273 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003274
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003275 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003276}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003277
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003278static int
3279validate_encoding_decl(node *tree)
3280{
3281 int nch = NCH(tree);
3282 int res = ((nch == 1)
3283 && validate_file_input(CHILD(tree, 0)));
3284
3285 if (!res && !PyErr_Occurred())
3286 err_string("Error Parsing encoding_decl");
3287
3288 return res;
3289}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003290
Fred Drake43f8f9b1998-04-13 16:25:46 +00003291static PyObject*
3292pickle_constructor = NULL;
3293
3294
3295static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003296parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003297{
Fred Drake268397f1998-04-29 14:16:32 +00003298 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003299 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003300 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003301 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003302
Fred Drakec2683dd2001-07-17 19:32:05 +00003303 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003304 PyObject *newargs;
3305 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003306
Fred Drake2a6875e1999-09-20 22:32:18 +00003307 if ((empty_dict = PyDict_New()) == NULL)
3308 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003309 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003310 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003311 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003312 if (tuple != NULL) {
3313 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3314 Py_DECREF(tuple);
3315 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003316 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003317 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003318 }
3319 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003320 Py_XDECREF(empty_dict);
3321
Fred Drake43f8f9b1998-04-13 16:25:46 +00003322 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003323}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003324
3325
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003326/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003327 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003328 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003329 * We'd really have to write a wrapper around it all anyway to allow
3330 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003331 */
3332static PyMethodDef parser_functions[] = {
Georg Brandlf9efabb2008-07-23 15:16:45 +00003333 {"ast2tuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003334 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Georg Brandlf9efabb2008-07-23 15:16:45 +00003335 {"ast2list", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003336 PyDoc_STR("Creates a list-tree representation of an ST.")},
Georg Brandlf9efabb2008-07-23 15:16:45 +00003337 {"compileast", (PyCFunction)parser_compileast,PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003338 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003339 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003340 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003341 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003342 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003343 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003344 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003345 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003346 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003347 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003348 PyDoc_STR("Creates an ST object from a suite.")},
Georg Brandlf9efabb2008-07-23 15:16:45 +00003349 {"sequence2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003350 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003351 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003352 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003353 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003354 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003355 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003356 PyDoc_STR("Creates a list-tree representation of an ST.")},
Georg Brandlf9efabb2008-07-23 15:16:45 +00003357 {"tuple2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003358 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003359 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003360 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003361
Fred Drake43f8f9b1998-04-13 16:25:46 +00003362 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003363 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003364 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003365
Fred Drake268397f1998-04-29 14:16:32 +00003366 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003367 };
3368
3369
Mark Hammond62b1ab12002-07-23 06:31:15 +00003370PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003371
Mark Hammond62b1ab12002-07-23 06:31:15 +00003372PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00003373initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003374{
Fred Drake13130bc2001-08-15 16:44:56 +00003375 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003376
Christian Heimese93237d2007-12-19 02:37:44 +00003377 Py_TYPE(&PyST_Type) = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00003378 module = Py_InitModule("parser", parser_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003379 if (module == NULL)
3380 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003381
Fred Drake7a15ba51999-09-09 14:21:52 +00003382 if (parser_error == 0)
3383 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003384
Tim Peters6a627252003-07-21 14:25:23 +00003385 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00003386 /* caller will check PyErr_Occurred() */
3387 return;
Tim Peters6a627252003-07-21 14:25:23 +00003388 /* CAUTION: The code next used to skip bumping the refcount on
3389 * parser_error. That's a disaster if initparser() gets called more
3390 * than once. By incref'ing, we ensure that each module dict that
3391 * gets created owns its reference to the shared parser_error object,
3392 * and the file static parser_error vrbl owns a reference too.
3393 */
3394 Py_INCREF(parser_error);
3395 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3396 return;
3397
Fred Drakec2683dd2001-07-17 19:32:05 +00003398 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003399 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00003400 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003401 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003402
Fred Drake13130bc2001-08-15 16:44:56 +00003403 PyModule_AddStringConstant(module, "__copyright__",
3404 parser_copyright_string);
3405 PyModule_AddStringConstant(module, "__doc__",
3406 parser_doc_string);
3407 PyModule_AddStringConstant(module, "__version__",
3408 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003409
Fred Drake78bdb9b2001-07-19 20:17:15 +00003410 /* Register to support pickling.
3411 * If this fails, the import of this module will fail because an
3412 * exception will be raised here; should we clear the exception?
3413 */
Georg Brandldffbf5f2008-05-20 07:49:57 +00003414 copyreg = PyImport_ImportModuleNoBlock("copy_reg");
Fred Drake13130bc2001-08-15 16:44:56 +00003415 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003416 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003417
Fred Drake13130bc2001-08-15 16:44:56 +00003418 func = PyObject_GetAttrString(copyreg, "pickle");
3419 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3420 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003421 Py_XINCREF(pickle_constructor);
3422 if ((func != NULL) && (pickle_constructor != NULL)
3423 && (pickler != NULL)) {
3424 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003425
Georg Brandl684fd0c2006-05-25 19:15:31 +00003426 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3427 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003428 Py_XDECREF(res);
3429 }
3430 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003431 Py_XDECREF(pickle_constructor);
3432 Py_XDECREF(pickler);
3433 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003434 }
Fred Drakeff9ea482000-04-19 13:54:15 +00003435}