blob: 8e581c234f5c10e62b611e6d573cb547886fa634 [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);
Jesus Cea3e3192d2012-08-03 14:25:53 +0200172static PyObject* parser_sizeof(PyST_Object *, void *);
Jeremy Hylton938ace62002-07-17 16:30:39 +0000173static int parser_compare(PyST_Object *left, PyST_Object *right);
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000174static PyObject *parser_getattr(PyObject *self, char *name);
Jesus Cea4fa7a5f2012-08-03 15:51:11 +0200175static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
176static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
177static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
178static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
179static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
Fred Drake503d8d61998-04-13 18:45:18 +0000180
Jesus Cea4fa7a5f2012-08-03 15:51:11 +0200181#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
182
183static PyMethodDef
184parser_methods[] = {
185 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
186 PyDoc_STR("Compile this ST object into a code object.")},
187 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
188 PyDoc_STR("Determines if this ST object was created from an expression.")},
189 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
190 PyDoc_STR("Determines if this ST object was created from a suite.")},
191 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
192 PyDoc_STR("Creates a list-tree representation of this ST.")},
193 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
194 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
195 {"__sizeof__", (PyCFunction)parser_sizeof, METH_NOARGS,
196 PyDoc_STR("Returns size in memory, in bytes.")},
197 {NULL, NULL, 0, NULL}
198};
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000199
Fred Drake268397f1998-04-29 14:16:32 +0000200static
Fred Drakec2683dd2001-07-17 19:32:05 +0000201PyTypeObject PyST_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000202 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000203 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000204 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000205 0, /* tp_itemsize */
206 (destructor)parser_free, /* tp_dealloc */
207 0, /* tp_print */
208 parser_getattr, /* tp_getattr */
209 0, /* tp_setattr */
210 (cmpfunc)parser_compare, /* tp_compare */
211 0, /* tp_repr */
212 0, /* tp_as_number */
213 0, /* tp_as_sequence */
214 0, /* tp_as_mapping */
215 0, /* tp_hash */
216 0, /* tp_call */
217 0, /* tp_str */
218 0, /* tp_getattro */
219 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000220
221 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000222 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000223
Fred Drakeff9ea482000-04-19 13:54:15 +0000224 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000225
226 /* __doc__ */
Jesus Cea3e3192d2012-08-03 14:25:53 +0200227 "Intermediate representation of a Python parse tree.",
228 0, /* tp_traverse */
229 0, /* tp_clear */
230 0, /* tp_richcompare */
231 0, /* tp_weaklistoffset */
232 0, /* tp_iter */
233 0, /* tp_iternext */
234 parser_methods, /* tp_methods */
Fred Drakec2683dd2001-07-17 19:32:05 +0000235}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000236
237
238static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000239parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000240{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000241 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000242
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000243 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000244 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000245
246 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000247 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000248
249 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000250 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000251
252 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000253 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000254
255 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000256 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000257
258 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000259 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000260
Fred Drakeff9ea482000-04-19 13:54:15 +0000261 if (v != 0)
262 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000263 }
264 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000265}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000266
267
Fred Drakec2683dd2001-07-17 19:32:05 +0000268/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000269 *
270 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
271 * This really just wraps a call to parser_compare_nodes() with some easy
272 * checks and protection code.
273 *
274 */
275static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000276parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000277{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000278 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000279 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000280
281 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000282 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000283
Fred Drakec2683dd2001-07-17 19:32:05 +0000284 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000285}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000286
287
Fred Drakec2683dd2001-07-17 19:32:05 +0000288/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000289 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000290 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000291 * 'wrapper' object that holds a node* and allows it to be passed around in
292 * Python code.
293 *
294 */
295static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000296parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000297{
Fred Drakec2683dd2001-07-17 19:32:05 +0000298 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000299
300 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000301 o->st_node = st;
302 o->st_type = type;
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000303 o->st_flags.cf_flags = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000304 }
Fred Drake268397f1998-04-29 14:16:32 +0000305 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000306 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000307 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000308 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000309}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000310
311
Fred Drakec2683dd2001-07-17 19:32:05 +0000312/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000313 *
314 * This is called by a del statement that reduces the reference count to 0.
315 *
316 */
317static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000318parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000319{
Fred Drakec2683dd2001-07-17 19:32:05 +0000320 PyNode_Free(st->st_node);
321 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000322}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000323
324
Fred Drakec2683dd2001-07-17 19:32:05 +0000325/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000326 *
327 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000328 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000329 *
330 */
331static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000332parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000333{
Guido van Rossum47478871996-08-21 14:32:37 +0000334 PyObject *line_option = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000335 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000336 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000337 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000338
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000339 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000340
Fred Drake268397f1998-04-29 14:16:32 +0000341 if (self == NULL) {
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000342 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
343 &PyST_Type, &self, &line_option,
344 &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000345 }
Fred Drake503d8d61998-04-13 18:45:18 +0000346 else
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000347 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
348 &line_option, &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000349 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000350 int lineno = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000351 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000352 if (line_option != NULL) {
Antoine Pitrouc5bef752012-08-15 23:16:51 +0200353 lineno = PyObject_IsTrue(line_option);
354 if (lineno < 0)
355 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000356 }
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000357 if (col_option != NULL) {
Antoine Pitrouc5bef752012-08-15 23:16:51 +0200358 col_offset = PyObject_IsTrue(col_option);
359 if (col_offset < 0)
360 return NULL;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000361 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000362 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000363 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000364 * since it's known to work already.
365 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000366 res = node2tuple(((PyST_Object*)self)->st_node,
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000367 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000368 }
369 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000370}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000371
Georg Brandlf9efabb2008-07-23 15:16:45 +0000372static PyObject*
373parser_ast2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
374{
375 if (PyErr_WarnPy3k("ast2tuple is removed in 3.x; use st2tuple", 1) < 0)
376 return NULL;
377 return parser_st2tuple(self, args, kw);
378}
379
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000380
Fred Drakec2683dd2001-07-17 19:32:05 +0000381/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000382 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000383 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000384 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000385 *
386 */
387static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000388parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000389{
Guido van Rossum47478871996-08-21 14:32:37 +0000390 PyObject *line_option = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000391 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000392 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000393 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000394
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000395 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000396
Fred Drake503d8d61998-04-13 18:45:18 +0000397 if (self == NULL)
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000398 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
399 &PyST_Type, &self, &line_option,
400 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000401 else
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000402 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
403 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000404 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000405 int lineno = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000406 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000407 if (line_option != 0) {
Antoine Pitrouc5bef752012-08-15 23:16:51 +0200408 lineno = PyObject_IsTrue(line_option);
409 if (lineno < 0)
410 return NULL;
Fred Drakeff9ea482000-04-19 13:54:15 +0000411 }
Antoine Pitrouc5bef752012-08-15 23:16:51 +0200412 if (col_option != 0) {
413 col_offset = PyObject_IsTrue(col_option);
414 if (col_offset < 0)
415 return NULL;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000416 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000417 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000418 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000419 * since it's known to work already.
420 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000421 res = node2tuple(self->st_node,
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000422 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000423 }
424 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000425}
Guido van Rossum47478871996-08-21 14:32:37 +0000426
Georg Brandlf9efabb2008-07-23 15:16:45 +0000427static PyObject*
428parser_ast2list(PyST_Object *self, PyObject *args, PyObject *kw)
429{
430 if (PyErr_WarnPy3k("ast2list is removed in 3.x; use st2list", 1) < 0)
431 return NULL;
432 return parser_st2list(self, args, kw);
433}
434
Guido van Rossum47478871996-08-21 14:32:37 +0000435
Fred Drakec2683dd2001-07-17 19:32:05 +0000436/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000437 *
438 * This function creates code objects from the parse tree represented by
439 * the passed-in data object. An optional file name is passed in as well.
440 *
441 */
442static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000443parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000444{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000445 PyObject* res = 0;
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000446 PyArena* arena;
447 mod_ty mod;
Fred Drakec2683dd2001-07-17 19:32:05 +0000448 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000449 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000450
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000451 static char *keywords[] = {"ast", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000452
Fred Drake503d8d61998-04-13 18:45:18 +0000453 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000454 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
455 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000456 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000457 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000458 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000459
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000460 if (ok) {
461 arena = PyArena_New();
462 if (arena) {
463 mod = PyAST_FromNode(self->st_node, &(self->st_flags), str, arena);
464 if (mod) {
465 res = (PyObject *)PyAST_Compile(mod, str, &(self->st_flags), arena);
466 }
467 PyArena_Free(arena);
468 }
469 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000470
471 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000472}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000473
Georg Brandlf9efabb2008-07-23 15:16:45 +0000474static PyObject*
475parser_compileast(PyST_Object *self, PyObject *args, PyObject *kw)
476{
477 if (PyErr_WarnPy3k("compileast is removed in 3.x; use compilest", 1) < 0)
478 return NULL;
479 return parser_compilest(self, args, kw);
480}
481
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000482
483/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
484 * PyObject* parser_issuite(PyObject* self, PyObject* args)
485 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000486 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000487 * a statement suite, respectively. The return is a Python truth value.
488 *
489 */
490static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000491parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000492{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000493 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000494 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000495
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000496 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000497
Fred Drake503d8d61998-04-13 18:45:18 +0000498 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000499 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000500 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000501 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000502 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000503
504 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000505 /* Check to see if the ST represents an expression or not. */
506 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000507 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000508 }
509 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000510}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000511
512
513static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000514parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000515{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000516 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000517 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000518
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000519 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000520
Fred Drake503d8d61998-04-13 18:45:18 +0000521 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000522 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000523 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000524 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000525 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000526
527 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000528 /* Check to see if the ST represents an expression or not. */
529 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000530 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000531 }
532 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000533}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000534
535
Fred Drake503d8d61998-04-13 18:45:18 +0000536static PyObject*
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000537parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000538{
Fred Drake503d8d61998-04-13 18:45:18 +0000539 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000540}
Fred Drake503d8d61998-04-13 18:45:18 +0000541
542
Guido van Rossum3d602e31996-07-21 02:33:56 +0000543/* err_string(char* message)
544 *
545 * Sets the error string for an exception of type ParserError.
546 *
547 */
548static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000549err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000550{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000551 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000552}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000553
554
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000555/* PyObject* parser_do_parse(PyObject* args, int type)
556 *
557 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000558 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000559 *
560 */
561static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000562parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000563{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000564 char* string = 0;
565 PyObject* res = 0;
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000566 int flags = 0;
567 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000568
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000569 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000570
571 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000572 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
573 &_PyParser_Grammar,
574 (type == PyST_EXPR)
575 ? eval_input : file_input,
576 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000577
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000578 if (n) {
579 res = parser_newstobject(n, type);
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000580 if (res)
581 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
582 }
583 else
584 PyParser_SetError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000585 }
586 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000587}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000588
589
590/* PyObject* parser_expr(PyObject* self, PyObject* args)
591 * PyObject* parser_suite(PyObject* self, PyObject* args)
592 *
593 * External interfaces to the parser itself. Which is called determines if
594 * the parser attempts to recognize an expression ('eval' form) or statement
595 * suite ('exec' form). The real work is done by parser_do_parse() above.
596 *
597 */
598static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000599parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000600{
Fred Drake268397f1998-04-29 14:16:32 +0000601 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000602 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000603}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000604
605
606static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000607parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000608{
Fred Drake268397f1998-04-29 14:16:32 +0000609 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000610 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000611}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000612
613
614
Fred Drakec2683dd2001-07-17 19:32:05 +0000615/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000616 * object requires that the input tuple be valid without having to rely on
617 * catching an exception from the compiler. This is done to allow the
618 * compiler itself to remain fast, since most of its input will come from
619 * the parser directly, and therefore be known to be syntactically correct.
620 * This validation is done to ensure that we don't core dump the compile
621 * phase, returning an exception instead.
622 *
623 * Two aspects can be broken out in this code: creating a node tree from
624 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000625 * advantageous to expand the number of ST types to include funcdefs and
626 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000627 * here. They are not necessary, and not quite as useful in a raw form.
628 * For now, let's get expressions and suites working reliably.
629 */
630
631
Jeremy Hylton938ace62002-07-17 16:30:39 +0000632static node* build_node_tree(PyObject *tuple);
633static int validate_expr_tree(node *tree);
634static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000635static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000636
Fred Drakec2683dd2001-07-17 19:32:05 +0000637/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000638 *
639 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000640 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000641 * tuple can be validated. It does this by checking the first code of the
642 * tuple, and, if acceptable, builds the internal representation. If this
643 * step succeeds, the internal representation is validated as fully as
644 * possible with the various validate_*() routines defined below.
645 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000646 * This function must be changed if support is to be added for PyST_FRAGMENT
647 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000648 *
649 */
650static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000651parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000652{
Fred Drake268397f1998-04-29 14:16:32 +0000653 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000654 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000655 PyObject *tuple;
656 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000657
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000658 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000659
Fred Drakec2683dd2001-07-17 19:32:05 +0000660 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000661 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000662 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000663 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000664 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000665 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000666 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000667 }
668 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000669 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000670 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000671 tree = build_node_tree(tuple);
672 if (tree != 0) {
673 int start_sym = TYPE(tree);
674 if (start_sym == eval_input) {
675 /* Might be an eval form. */
676 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000677 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000678 else
679 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000680 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000681 else if (start_sym == file_input) {
682 /* This looks like an exec form so far. */
683 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000684 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000685 else
686 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000687 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000688 else if (start_sym == encoding_decl) {
689 /* This looks like an encoding_decl so far. */
690 if (validate_encoding_decl(tree))
691 st = parser_newstobject(tree, PyST_SUITE);
692 else
693 PyNode_Free(tree);
694 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000695 else {
696 /* This is a fragment, at best. */
697 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000698 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000699 }
Guido van Rossum47478871996-08-21 14:32:37 +0000700 }
Guido van Rossum47478871996-08-21 14:32:37 +0000701 /* Make sure we throw an exception on all errors. We should never
702 * get this, but we'd do well to be sure something is done.
703 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000704 if (st == NULL && !PyErr_Occurred())
705 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000706
Fred Drakec2683dd2001-07-17 19:32:05 +0000707 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000708}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000709
Georg Brandlf9efabb2008-07-23 15:16:45 +0000710static PyObject*
711parser_tuple2ast(PyST_Object *self, PyObject *args, PyObject *kw)
712{
713 if (PyErr_WarnPy3k("tuple2ast is removed in 3.x; use tuple2st", 1) < 0)
714 return NULL;
715 return parser_tuple2st(self, args, kw);
716}
717
Jesus Cea3e3192d2012-08-03 14:25:53 +0200718static PyObject *
719parser_sizeof(PyST_Object *st, void *unused)
720{
721 Py_ssize_t res;
722
723 res = sizeof(PyST_Object) + _PyNode_SizeOf(st->st_node);
724 return PyLong_FromSsize_t(res);
725}
726
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000727
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000728/* node* build_node_children()
729 *
730 * Iterate across the children of the current non-terminal node and build
731 * their structures. If successful, return the root of this portion of
732 * the tree, otherwise, 0. Any required exception will be specified already,
733 * and no memory will have been deallocated.
734 *
735 */
736static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000737build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000738{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000739 Py_ssize_t len = PyObject_Size(tuple);
740 Py_ssize_t i;
741 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000742
743 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000744 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000745 PyObject* elem = PySequence_GetItem(tuple, i);
746 int ok = elem != NULL;
747 long type = 0;
748 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000749
Fred Drakeff9ea482000-04-19 13:54:15 +0000750 if (ok)
751 ok = PySequence_Check(elem);
752 if (ok) {
753 PyObject *temp = PySequence_GetItem(elem, 0);
754 if (temp == NULL)
755 ok = 0;
756 else {
757 ok = PyInt_Check(temp);
758 if (ok)
759 type = PyInt_AS_LONG(temp);
760 Py_DECREF(temp);
761 }
762 }
763 if (!ok) {
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000764 PyObject *err = Py_BuildValue("os", elem,
765 "Illegal node construct.");
766 PyErr_SetObject(parser_error, err);
767 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000768 Py_XDECREF(elem);
769 return (0);
770 }
771 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000772 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000773 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000774
Fred Drake0ac9b072000-09-12 21:58:06 +0000775 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000776 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000777 return 0;
778 }
779 temp = PySequence_GetItem(elem, 1);
780 if (temp == NULL)
781 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000782 if (!PyString_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000783 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000784 "second item in terminal node must be a string,"
785 " found %s",
Christian Heimese93237d2007-12-19 02:37:44 +0000786 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000787 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000788 return 0;
789 }
790 if (len == 3) {
791 PyObject *o = PySequence_GetItem(elem, 2);
792 if (o != NULL) {
793 if (PyInt_Check(o))
794 *line_num = PyInt_AS_LONG(o);
795 else {
796 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000797 "third item in terminal node must be an"
798 " integer, found %s",
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000799 Py_TYPE(temp)->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000800 Py_DECREF(o);
801 Py_DECREF(temp);
802 return 0;
803 }
804 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000805 }
806 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000807 len = PyString_GET_SIZE(temp) + 1;
Tim Petersc9d78aa2006-03-26 23:27:58 +0000808 strn = (char *)PyObject_MALLOC(len);
Fred Drake0ac9b072000-09-12 21:58:06 +0000809 if (strn != NULL)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000810 (void) memcpy(strn, PyString_AS_STRING(temp), len);
Fred Drake0ac9b072000-09-12 21:58:06 +0000811 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000812 }
813 else if (!ISNONTERMINAL(type)) {
814 /*
815 * It has to be one or the other; this is an error.
816 * Throw an exception.
817 */
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000818 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
819 PyErr_SetObject(parser_error, err);
820 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000821 Py_XDECREF(elem);
822 return (0);
823 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000824 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000825 if (err == E_NOMEM) {
Tim Petersc9d78aa2006-03-26 23:27:58 +0000826 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000827 return (node *) PyErr_NoMemory();
828 }
829 if (err == E_OVERFLOW) {
Tim Petersc9d78aa2006-03-26 23:27:58 +0000830 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000831 PyErr_SetString(PyExc_ValueError,
832 "unsupported number of child nodes");
833 return NULL;
834 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000835
Fred Drakeff9ea482000-04-19 13:54:15 +0000836 if (ISNONTERMINAL(type)) {
837 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000838
Fred Drakeff9ea482000-04-19 13:54:15 +0000839 if (new_child != build_node_children(elem, new_child, line_num)) {
840 Py_XDECREF(elem);
841 return (0);
842 }
843 }
844 else if (type == NEWLINE) { /* It's true: we increment the */
845 ++(*line_num); /* line number *after* the newline! */
846 }
847 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000848 }
Tim Petersc9d78aa2006-03-26 23:27:58 +0000849 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000850}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000851
852
853static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000854build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000855{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000856 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000857 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000858 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000859
Guido van Rossum47478871996-08-21 14:32:37 +0000860 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000861 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000862 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000863 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000864 /*
865 * The tuple is simple, but it doesn't start with a start symbol.
866 * Throw an exception now and be done with it.
867 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000868 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000869 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000870 PyErr_SetObject(parser_error, tuple);
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000871 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000872 }
873 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000874 /*
875 * Not efficient, but that can be handled later.
876 */
877 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000878 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000879
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000880 if (num == encoding_decl) {
881 encoding = PySequence_GetItem(tuple, 2);
882 /* tuple isn't borrowed anymore here, need to DECREF */
883 tuple = PySequence_GetSlice(tuple, 0, 2);
884 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000885 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000886 if (res != NULL) {
887 if (res != build_node_children(tuple, res, &line_num)) {
888 PyNode_Free(res);
889 res = NULL;
890 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000891 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000892 Py_ssize_t len;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000893 len = PyString_GET_SIZE(encoding) + 1;
Tim Petersc9d78aa2006-03-26 23:27:58 +0000894 res->n_str = (char *)PyObject_MALLOC(len);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000895 if (res->n_str != NULL)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000896 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000897 Py_DECREF(encoding);
898 Py_DECREF(tuple);
899 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000900 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000901 }
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000902 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000903 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000904 * NONTERMINAL, we can't use it. Not sure the implementation
905 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000906 */
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000907 PyObject *err = Py_BuildValue("os", tuple,
908 "Illegal component tuple.");
909 PyErr_SetObject(parser_error, err);
910 Py_XDECREF(err);
911 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000912
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000913 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000914}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000915
916
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000917/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000918 * Validation routines used within the validation section:
919 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000920static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000921
Fred Drakeff9ea482000-04-19 13:54:15 +0000922#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
923#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
924#define validate_colon(ch) validate_terminal(ch, COLON, ":")
925#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
926#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
927#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
928#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
929#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
930#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
931#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
932#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
933#define validate_star(ch) validate_terminal(ch, STAR, "*")
934#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
935#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
936#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000937#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000938#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000939
Fred Drake0ac9b072000-09-12 21:58:06 +0000940#define VALIDATER(n) static int validate_##n(node *tree)
941
Fred Drakeff9ea482000-04-19 13:54:15 +0000942VALIDATER(node); VALIDATER(small_stmt);
943VALIDATER(class); VALIDATER(node);
944VALIDATER(parameters); VALIDATER(suite);
945VALIDATER(testlist); VALIDATER(varargslist);
946VALIDATER(fpdef); VALIDATER(fplist);
947VALIDATER(stmt); VALIDATER(simple_stmt);
948VALIDATER(expr_stmt); VALIDATER(power);
949VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000950VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000951VALIDATER(raise_stmt); VALIDATER(import_stmt);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000952VALIDATER(import_name); VALIDATER(import_from);
Fred Drakecff283c2000-08-21 22:24:43 +0000953VALIDATER(global_stmt); VALIDATER(list_if);
954VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000955VALIDATER(exec_stmt); VALIDATER(compound_stmt);
956VALIDATER(while); VALIDATER(for);
957VALIDATER(try); VALIDATER(except_clause);
958VALIDATER(test); VALIDATER(and_test);
959VALIDATER(not_test); VALIDATER(comparison);
960VALIDATER(comp_op); VALIDATER(expr);
961VALIDATER(xor_expr); VALIDATER(and_expr);
962VALIDATER(shift_expr); VALIDATER(arith_expr);
963VALIDATER(term); VALIDATER(factor);
964VALIDATER(atom); VALIDATER(lambdef);
965VALIDATER(trailer); VALIDATER(subscript);
966VALIDATER(subscriptlist); VALIDATER(sliceop);
Alexandre Vassalottiee936a22010-01-09 23:35:54 +0000967VALIDATER(exprlist); VALIDATER(dictorsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000968VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000969VALIDATER(listmaker); VALIDATER(yield_stmt);
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000970VALIDATER(testlist1); VALIDATER(comp_for);
971VALIDATER(comp_iter); VALIDATER(comp_if);
972VALIDATER(testlist_comp); VALIDATER(yield_expr);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000973VALIDATER(yield_or_testlist); VALIDATER(or_test);
974VALIDATER(old_test); VALIDATER(old_lambdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000975
Fred Drake0ac9b072000-09-12 21:58:06 +0000976#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000977
Fred Drakeff9ea482000-04-19 13:54:15 +0000978#define is_even(n) (((n) & 1) == 0)
979#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000980
981
982static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000983validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000984{
Fred Drake0ac9b072000-09-12 21:58:06 +0000985 if (TYPE(n) != t) {
986 PyErr_Format(parser_error, "Expected node type %d, got %d.",
987 t, TYPE(n));
988 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000989 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000990 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000991}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000992
993
Fred Drakee7ab64e2000-04-25 04:14:46 +0000994/* Verifies that the number of child nodes is exactly 'num', raising
995 * an exception if it isn't. The exception message does not indicate
996 * the exact number of nodes, allowing this to be used to raise the
997 * "right" exception when the wrong number of nodes is present in a
998 * specific variant of a statement's syntax. This is commonly used
999 * in that fashion.
1000 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001001static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001002validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001003{
Guido van Rossum3d602e31996-07-21 02:33:56 +00001004 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001005 PyErr_Format(parser_error,
1006 "Illegal number of children for %s node.", name);
1007 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001008 }
Fred Drake0ac9b072000-09-12 21:58:06 +00001009 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00001010}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001011
1012
1013static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001014validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +00001015{
Guido van Rossum3d602e31996-07-21 02:33:56 +00001016 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +00001017 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001018
1019 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001020 PyErr_Format(parser_error,
1021 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001022 }
1023 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001024}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001025
1026
Guido van Rossum47478871996-08-21 14:32:37 +00001027/* X (',' X) [',']
1028 */
1029static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +00001030validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +00001031 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001032{
1033 int nch = NCH(tree);
1034 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +00001035 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +00001036
1037 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001038 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +00001039 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001040 if (is_even(nch))
1041 res = validate_comma(CHILD(tree, --nch));
1042 if (res && nch > 1) {
1043 int pos = 1;
1044 for ( ; res && pos < nch; pos += 2)
1045 res = (validate_comma(CHILD(tree, pos))
1046 && vfunc(CHILD(tree, pos + 1)));
1047 }
Guido van Rossum47478871996-08-21 14:32:37 +00001048 }
1049 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001050}
Guido van Rossum47478871996-08-21 14:32:37 +00001051
1052
Fred Drakecff283c2000-08-21 22:24:43 +00001053/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001054 *
1055 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001056 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00001057 */
Guido van Rossum47478871996-08-21 14:32:37 +00001058static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001059validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001060{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001061 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +00001062 int res = (validate_ntype(tree, classdef) &&
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001063 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001064
Guido van Rossum3d602e31996-07-21 02:33:56 +00001065 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001066 res = (validate_name(CHILD(tree, 0), "class")
1067 && validate_ntype(CHILD(tree, 1), NAME)
1068 && validate_colon(CHILD(tree, nch - 2))
1069 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001070 }
Brett Cannonf4189912005-04-09 02:30:16 +00001071 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001072 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +00001073 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001074
Brett Cannonf4189912005-04-09 02:30:16 +00001075 if (res) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001076 if (nch == 7) {
1077 res = ((validate_lparen(CHILD(tree, 2)) &&
1078 validate_testlist(CHILD(tree, 3)) &&
1079 validate_rparen(CHILD(tree, 4))));
1080 }
1081 else if (nch == 6) {
1082 res = (validate_lparen(CHILD(tree,2)) &&
1083 validate_rparen(CHILD(tree,3)));
1084 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001085 }
1086 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001087}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001088
1089
Guido van Rossum3d602e31996-07-21 02:33:56 +00001090/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001091 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001092 */
Guido van Rossum47478871996-08-21 14:32:37 +00001093static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001094validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001095{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001096 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001097 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001098 && (nch >= 4)
1099 && validate_name(CHILD(tree, 0), "if")
1100 && validate_test(CHILD(tree, 1))
1101 && validate_colon(CHILD(tree, 2))
1102 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001103
1104 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001105 /* ... 'else' ':' suite */
1106 res = (validate_name(CHILD(tree, nch - 3), "else")
1107 && validate_colon(CHILD(tree, nch - 2))
1108 && validate_suite(CHILD(tree, nch - 1)));
1109 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001110 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001111 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001112 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001113 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001114 /* Will catch the case for nch < 4 */
1115 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001116 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001117 /* ... ('elif' test ':' suite)+ ... */
1118 int j = 4;
1119 while ((j < nch) && res) {
1120 res = (validate_name(CHILD(tree, j), "elif")
1121 && validate_colon(CHILD(tree, j + 2))
1122 && validate_test(CHILD(tree, j + 1))
1123 && validate_suite(CHILD(tree, j + 3)));
1124 j += 4;
1125 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001126 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001127 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001128}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001129
1130
Guido van Rossum3d602e31996-07-21 02:33:56 +00001131/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001132 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001133 *
1134 */
Guido van Rossum47478871996-08-21 14:32:37 +00001135static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001136validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001137{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001138 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001139 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001140
Guido van Rossum3d602e31996-07-21 02:33:56 +00001141 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001142 res = (validate_lparen(CHILD(tree, 0))
1143 && validate_rparen(CHILD(tree, nch - 1)));
1144 if (res && (nch == 3))
1145 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001146 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001147 else {
1148 (void) validate_numnodes(tree, 2, "parameters");
1149 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001150 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001151}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001152
1153
Fred Drakecff283c2000-08-21 22:24:43 +00001154/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001155 *
1156 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001157 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001158 * | NEWLINE INDENT stmt+ DEDENT
1159 */
Guido van Rossum47478871996-08-21 14:32:37 +00001160static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001161validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001162{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001163 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001164 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001165
Guido van Rossum3d602e31996-07-21 02:33:56 +00001166 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001167 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001168 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001169 /* NEWLINE INDENT stmt+ DEDENT */
1170 res = (validate_newline(CHILD(tree, 0))
1171 && validate_indent(CHILD(tree, 1))
1172 && validate_stmt(CHILD(tree, 2))
1173 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001174
Fred Drakeff9ea482000-04-19 13:54:15 +00001175 if (res && (nch > 4)) {
1176 int i = 3;
1177 --nch; /* forget the DEDENT */
1178 for ( ; res && (i < nch); ++i)
1179 res = validate_stmt(CHILD(tree, i));
1180 }
1181 else if (nch < 4)
1182 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001183 }
1184 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001185}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001186
1187
Guido van Rossum47478871996-08-21 14:32:37 +00001188static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001189validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001190{
Guido van Rossum47478871996-08-21 14:32:37 +00001191 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001192 validate_test, "testlist"));
1193}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001194
1195
Guido van Rossum1c917072001-10-15 15:44:05 +00001196static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001197validate_testlist1(node *tree)
1198{
1199 return (validate_repeating_list(tree, testlist1,
1200 validate_test, "testlist1"));
1201}
1202
1203
1204static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001205validate_testlist_safe(node *tree)
1206{
1207 return (validate_repeating_list(tree, testlist_safe,
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001208 validate_old_test, "testlist_safe"));
Guido van Rossum1c917072001-10-15 15:44:05 +00001209}
1210
1211
Fred Drakecff283c2000-08-21 22:24:43 +00001212/* '*' NAME [',' '**' NAME] | '**' NAME
1213 */
1214static int
1215validate_varargslist_trailer(node *tree, int start)
1216{
1217 int nch = NCH(tree);
1218 int res = 0;
1219 int sym;
1220
1221 if (nch <= start) {
1222 err_string("expected variable argument trailer for varargslist");
1223 return 0;
1224 }
1225 sym = TYPE(CHILD(tree, start));
1226 if (sym == STAR) {
1227 /*
1228 * ('*' NAME [',' '**' NAME]
1229 */
1230 if (nch-start == 2)
1231 res = validate_name(CHILD(tree, start+1), NULL);
1232 else if (nch-start == 5)
1233 res = (validate_name(CHILD(tree, start+1), NULL)
1234 && validate_comma(CHILD(tree, start+2))
1235 && validate_doublestar(CHILD(tree, start+3))
1236 && validate_name(CHILD(tree, start+4), NULL));
1237 }
1238 else if (sym == DOUBLESTAR) {
1239 /*
1240 * '**' NAME
1241 */
1242 if (nch-start == 2)
1243 res = validate_name(CHILD(tree, start+1), NULL);
1244 }
1245 if (!res)
1246 err_string("illegal variable argument trailer for varargslist");
1247 return res;
1248}
1249
1250
1251/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001252 *
1253 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001254 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001255 * ('*' NAME [',' '**' NAME]
1256 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001257 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1258 *
1259 */
Guido van Rossum47478871996-08-21 14:32:37 +00001260static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001261validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001262{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001263 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001264 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001265 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001266
Fred Drakeb6429a22000-12-11 22:08:27 +00001267 if (!res)
1268 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001269 if (nch < 1) {
1270 err_string("varargslist missing child nodes");
1271 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001272 }
Fred Drakecff283c2000-08-21 22:24:43 +00001273 sym = TYPE(CHILD(tree, 0));
1274 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001275 /* whole thing matches:
1276 * '*' NAME [',' '**' NAME] | '**' NAME
1277 */
Fred Drakecff283c2000-08-21 22:24:43 +00001278 res = validate_varargslist_trailer(tree, 0);
1279 else if (sym == fpdef) {
1280 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001281
Fred Drakecff283c2000-08-21 22:24:43 +00001282 sym = TYPE(CHILD(tree, nch-1));
1283 if (sym == NAME) {
1284 /*
1285 * (fpdef ['=' test] ',')+
1286 * ('*' NAME [',' '**' NAME]
1287 * | '**' NAME)
1288 */
1289 /* skip over (fpdef ['=' test] ',')+ */
1290 while (res && (i+2 <= nch)) {
1291 res = validate_fpdef(CHILD(tree, i));
1292 ++i;
1293 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1294 res = (validate_equal(CHILD(tree, i))
1295 && validate_test(CHILD(tree, i+1)));
1296 if (res)
1297 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001298 }
Fred Drakecff283c2000-08-21 22:24:43 +00001299 if (res && i < nch) {
1300 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001301 ++i;
1302 if (res && i < nch
1303 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1304 || TYPE(CHILD(tree, i)) == STAR))
1305 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001306 }
1307 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001308 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1309 * i --^^^
1310 */
Fred Drakecff283c2000-08-21 22:24:43 +00001311 if (res)
1312 res = validate_varargslist_trailer(tree, i);
1313 }
1314 else {
1315 /*
1316 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1317 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001318 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001319 if (sym == COMMA) {
1320 res = validate_comma(CHILD(tree, nch-1));
1321 if (!res)
1322 return 0;
1323 --nch;
1324 }
1325 /*
1326 * fpdef ['=' test] (',' fpdef ['=' test])*
1327 */
1328 res = validate_fpdef(CHILD(tree, 0));
1329 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001330 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1331 res = (validate_equal(CHILD(tree, i))
1332 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001333 i += 2;
1334 }
1335 /*
1336 * ... (',' fpdef ['=' test])*
1337 * i ---^^^
1338 */
1339 while (res && (nch - i) >= 2) {
1340 res = (validate_comma(CHILD(tree, i))
1341 && validate_fpdef(CHILD(tree, i+1)));
1342 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001343 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1344 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001345 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001346 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001347 }
1348 }
1349 if (res && nch - i != 0) {
1350 res = 0;
1351 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001352 }
1353 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001354 }
Fred Drakecff283c2000-08-21 22:24:43 +00001355 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001356}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001357
1358
Fred Drakecff283c2000-08-21 22:24:43 +00001359/* list_iter: list_for | list_if
1360 */
1361static int
1362validate_list_iter(node *tree)
1363{
1364 int res = (validate_ntype(tree, list_iter)
1365 && validate_numnodes(tree, 1, "list_iter"));
1366 if (res && TYPE(CHILD(tree, 0)) == list_for)
1367 res = validate_list_for(CHILD(tree, 0));
1368 else
1369 res = validate_list_if(CHILD(tree, 0));
1370
1371 return res;
1372}
1373
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001374/* comp_iter: comp_for | comp_if
Raymond Hettinger354433a2004-05-19 08:20:33 +00001375 */
1376static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001377validate_comp_iter(node *tree)
Raymond Hettinger354433a2004-05-19 08:20:33 +00001378{
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001379 int res = (validate_ntype(tree, comp_iter)
1380 && validate_numnodes(tree, 1, "comp_iter"));
1381 if (res && TYPE(CHILD(tree, 0)) == comp_for)
1382 res = validate_comp_for(CHILD(tree, 0));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001383 else
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001384 res = validate_comp_if(CHILD(tree, 0));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001385
1386 return res;
1387}
1388
Fred Drakecff283c2000-08-21 22:24:43 +00001389/* list_for: 'for' exprlist 'in' testlist [list_iter]
1390 */
1391static int
1392validate_list_for(node *tree)
1393{
1394 int nch = NCH(tree);
1395 int res;
1396
1397 if (nch == 5)
1398 res = validate_list_iter(CHILD(tree, 4));
1399 else
1400 res = validate_numnodes(tree, 4, "list_for");
1401
1402 if (res)
1403 res = (validate_name(CHILD(tree, 0), "for")
1404 && validate_exprlist(CHILD(tree, 1))
1405 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001406 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001407
1408 return res;
1409}
1410
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001411/* comp_for: 'for' exprlist 'in' test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001412 */
1413static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001414validate_comp_for(node *tree)
Raymond Hettinger354433a2004-05-19 08:20:33 +00001415{
1416 int nch = NCH(tree);
1417 int res;
1418
1419 if (nch == 5)
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001420 res = validate_comp_iter(CHILD(tree, 4));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001421 else
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001422 res = validate_numnodes(tree, 4, "comp_for");
Raymond Hettinger354433a2004-05-19 08:20:33 +00001423
1424 if (res)
1425 res = (validate_name(CHILD(tree, 0), "for")
1426 && validate_exprlist(CHILD(tree, 1))
1427 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001428 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001429
1430 return res;
1431}
1432
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001433/* list_if: 'if' old_test [list_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001434 */
1435static int
1436validate_list_if(node *tree)
1437{
1438 int nch = NCH(tree);
1439 int res;
1440
1441 if (nch == 3)
1442 res = validate_list_iter(CHILD(tree, 2));
1443 else
1444 res = validate_numnodes(tree, 2, "list_if");
1445
1446 if (res)
1447 res = (validate_name(CHILD(tree, 0), "if")
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001448 && validate_old_test(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001449
1450 return res;
1451}
1452
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001453/* comp_if: 'if' old_test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001454 */
1455static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001456validate_comp_if(node *tree)
Raymond Hettinger354433a2004-05-19 08:20:33 +00001457{
1458 int nch = NCH(tree);
1459 int res;
1460
1461 if (nch == 3)
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001462 res = validate_comp_iter(CHILD(tree, 2));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001463 else
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001464 res = validate_numnodes(tree, 2, "comp_if");
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001465
Raymond Hettinger354433a2004-05-19 08:20:33 +00001466 if (res)
1467 res = (validate_name(CHILD(tree, 0), "if")
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001468 && validate_old_test(CHILD(tree, 1)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001469
1470 return res;
1471}
Fred Drakecff283c2000-08-21 22:24:43 +00001472
1473/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001474 *
1475 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001476 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001477 * | '(' fplist ')'
1478 */
Guido van Rossum47478871996-08-21 14:32:37 +00001479static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001480validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001481{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001482 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001483 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001484
Guido van Rossum3d602e31996-07-21 02:33:56 +00001485 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001486 if (nch == 1)
1487 res = validate_ntype(CHILD(tree, 0), NAME);
1488 else if (nch == 3)
1489 res = (validate_lparen(CHILD(tree, 0))
1490 && validate_fplist(CHILD(tree, 1))
1491 && validate_rparen(CHILD(tree, 2)));
1492 else
1493 res = validate_numnodes(tree, 1, "fpdef");
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 Rossum47478871996-08-21 14:32:37 +00001499static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001500validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001501{
Guido van Rossum47478871996-08-21 14:32:37 +00001502 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001503 validate_fpdef, "fplist"));
1504}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001505
1506
Guido van Rossum3d602e31996-07-21 02:33:56 +00001507/* simple_stmt | compound_stmt
1508 *
1509 */
Guido van Rossum47478871996-08-21 14:32:37 +00001510static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001511validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001512{
1513 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001514 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001515
Guido van Rossum3d602e31996-07-21 02:33:56 +00001516 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001517 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001518
Fred Drakeff9ea482000-04-19 13:54:15 +00001519 if (TYPE(tree) == simple_stmt)
1520 res = validate_simple_stmt(tree);
1521 else
1522 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001523 }
1524 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001525}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001526
1527
Guido van Rossum3d602e31996-07-21 02:33:56 +00001528/* small_stmt (';' small_stmt)* [';'] NEWLINE
1529 *
1530 */
Guido van Rossum47478871996-08-21 14:32:37 +00001531static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001532validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001533{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001534 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001535 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001536 && (nch >= 2)
1537 && validate_small_stmt(CHILD(tree, 0))
1538 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001539
Guido van Rossum3d602e31996-07-21 02:33:56 +00001540 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001541 res = validate_numnodes(tree, 2, "simple_stmt");
1542 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001543 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001544 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001545 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001546 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001547
Fred Drakeff9ea482000-04-19 13:54:15 +00001548 for (i = 1; res && (i < nch); i += 2)
1549 res = (validate_semi(CHILD(tree, i))
1550 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001551 }
1552 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001553}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001554
1555
Guido van Rossum47478871996-08-21 14:32:37 +00001556static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001557validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001558{
1559 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001560 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001561
Fred Drake0ac9b072000-09-12 21:58:06 +00001562 if (res) {
1563 int ntype = TYPE(CHILD(tree, 0));
1564
1565 if ( (ntype == expr_stmt)
1566 || (ntype == print_stmt)
1567 || (ntype == del_stmt)
1568 || (ntype == pass_stmt)
1569 || (ntype == flow_stmt)
1570 || (ntype == import_stmt)
1571 || (ntype == global_stmt)
1572 || (ntype == assert_stmt)
1573 || (ntype == exec_stmt))
1574 res = validate_node(CHILD(tree, 0));
1575 else {
1576 res = 0;
1577 err_string("illegal small_stmt child type");
1578 }
1579 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001580 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001581 res = 0;
1582 PyErr_Format(parser_error,
1583 "Unrecognized child node of small_stmt: %d.",
1584 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001585 }
1586 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001587}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001588
1589
1590/* compound_stmt:
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00001591 * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
Guido van Rossum3d602e31996-07-21 02:33:56 +00001592 */
Guido van Rossum47478871996-08-21 14:32:37 +00001593static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001594validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001595{
1596 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001597 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001598 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001599
1600 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001601 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001602
1603 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001604 ntype = TYPE(tree);
1605 if ( (ntype == if_stmt)
1606 || (ntype == while_stmt)
1607 || (ntype == for_stmt)
1608 || (ntype == try_stmt)
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00001609 || (ntype == with_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001610 || (ntype == funcdef)
Christian Heimes5224d282008-02-23 15:01:05 +00001611 || (ntype == classdef)
1612 || (ntype == decorated))
Fred Drakeff9ea482000-04-19 13:54:15 +00001613 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001614 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001615 res = 0;
1616 PyErr_Format(parser_error,
1617 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001618 }
1619 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001620}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001621
Guido van Rossum47478871996-08-21 14:32:37 +00001622static int
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001623validate_yield_or_testlist(node *tree)
1624{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001625 if (TYPE(tree) == yield_expr)
1626 return validate_yield_expr(tree);
1627 else
1628 return validate_testlist(tree);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001629}
1630
1631static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001632validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001633{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001634 int j;
1635 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001636 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001637 && is_odd(nch)
1638 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001639
Fred Drake28f739a2000-08-25 22:42:40 +00001640 if (res && nch == 3
1641 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001642 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001643 && validate_yield_or_testlist(CHILD(tree, 2));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001644
Fred Drake28f739a2000-08-25 22:42:40 +00001645 if (res) {
1646 char *s = STR(CHILD(CHILD(tree, 1), 0));
1647
1648 res = (strcmp(s, "+=") == 0
1649 || strcmp(s, "-=") == 0
1650 || strcmp(s, "*=") == 0
1651 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001652 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001653 || strcmp(s, "%=") == 0
1654 || strcmp(s, "&=") == 0
1655 || strcmp(s, "|=") == 0
1656 || strcmp(s, "^=") == 0
1657 || strcmp(s, "<<=") == 0
1658 || strcmp(s, ">>=") == 0
1659 || strcmp(s, "**=") == 0);
1660 if (!res)
Ezio Melotti24b07bc2011-03-15 18:55:01 +02001661 err_string("illegal augmented assignment operator");
Fred Drake28f739a2000-08-25 22:42:40 +00001662 }
1663 }
1664 else {
1665 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001666 res = validate_equal(CHILD(tree, j))
1667 && validate_yield_or_testlist(CHILD(tree, j + 1));
Fred Drake28f739a2000-08-25 22:42:40 +00001668 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001669 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001670}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001671
1672
Guido van Rossum3d602e31996-07-21 02:33:56 +00001673/* print_stmt:
1674 *
Fred Drakecff283c2000-08-21 22:24:43 +00001675 * 'print' ( [ test (',' test)* [','] ]
1676 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001677 */
Guido van Rossum47478871996-08-21 14:32:37 +00001678static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001679validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001680{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001681 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001682 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001683 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001684 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001685
Fred Drakecff283c2000-08-21 22:24:43 +00001686 if (res && nch > 1) {
1687 int sym = TYPE(CHILD(tree, 1));
1688 int i = 1;
1689 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001690
Fred Drakecff283c2000-08-21 22:24:43 +00001691 if (sym == test)
1692 res = validate_test(CHILD(tree, i++));
1693 else {
1694 if (nch < 3)
1695 res = validate_numnodes(tree, 3, "print_stmt");
1696 else {
1697 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1698 && validate_test(CHILD(tree, i+1)));
1699 i += 2;
1700 allow_trailing_comma = 0;
1701 }
1702 }
1703 if (res) {
1704 /* ... (',' test)* [','] */
1705 while (res && i+2 <= nch) {
1706 res = (validate_comma(CHILD(tree, i))
1707 && validate_test(CHILD(tree, i+1)));
1708 allow_trailing_comma = 1;
1709 i += 2;
1710 }
1711 if (res && !allow_trailing_comma)
1712 res = validate_numnodes(tree, i, "print_stmt");
1713 else if (res && i < nch)
1714 res = validate_comma(CHILD(tree, i));
1715 }
1716 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001717 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001718}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001719
1720
Guido van Rossum47478871996-08-21 14:32:37 +00001721static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001722validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001723{
1724 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001725 && validate_name(CHILD(tree, 0), "del")
1726 && validate_exprlist(CHILD(tree, 1)));
1727}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001728
1729
Guido van Rossum47478871996-08-21 14:32:37 +00001730static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001731validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001732{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001733 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001734 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001735 && ((nch == 1) || (nch == 2))
1736 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001737
Guido van Rossum3d602e31996-07-21 02:33:56 +00001738 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001739 res = validate_testlist(CHILD(tree, 1));
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
Guido van Rossum47478871996-08-21 14:32:37 +00001745static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001746validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001747{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001748 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001749 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001750 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001751
Guido van Rossum3d602e31996-07-21 02:33:56 +00001752 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001753 res = validate_name(CHILD(tree, 0), "raise");
1754 if (res && (nch >= 2))
1755 res = validate_test(CHILD(tree, 1));
1756 if (res && nch > 2) {
1757 res = (validate_comma(CHILD(tree, 2))
1758 && validate_test(CHILD(tree, 3)));
1759 if (res && (nch > 4))
1760 res = (validate_comma(CHILD(tree, 4))
1761 && validate_test(CHILD(tree, 5)));
1762 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001763 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001764 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001765 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001766 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001767 res = (validate_comma(CHILD(tree, 2))
1768 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001769
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001770 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001771}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001772
1773
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001774/* yield_expr: 'yield' [testlist]
1775 */
1776static int
1777validate_yield_expr(node *tree)
1778{
1779 int nch = NCH(tree);
1780 int res = (validate_ntype(tree, yield_expr)
1781 && ((nch == 1) || (nch == 2))
1782 && validate_name(CHILD(tree, 0), "yield"));
1783
1784 if (res && (nch == 2))
1785 res = validate_testlist(CHILD(tree, 1));
1786
1787 return (res);
1788}
1789
1790
1791/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001792 */
1793static int
1794validate_yield_stmt(node *tree)
1795{
1796 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001797 && validate_numnodes(tree, 1, "yield_stmt")
1798 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001799}
1800
1801
Fred Drakecff283c2000-08-21 22:24:43 +00001802static int
1803validate_import_as_name(node *tree)
1804{
1805 int nch = NCH(tree);
1806 int ok = validate_ntype(tree, import_as_name);
1807
1808 if (ok) {
1809 if (nch == 1)
1810 ok = validate_name(CHILD(tree, 0), NULL);
1811 else if (nch == 3)
1812 ok = (validate_name(CHILD(tree, 0), NULL)
1813 && validate_name(CHILD(tree, 1), "as")
1814 && validate_name(CHILD(tree, 2), NULL));
1815 else
1816 ok = validate_numnodes(tree, 3, "import_as_name");
1817 }
1818 return ok;
1819}
1820
1821
Fred Drake71137082001-01-07 05:59:59 +00001822/* dotted_name: NAME ("." NAME)*
1823 */
1824static int
1825validate_dotted_name(node *tree)
1826{
1827 int nch = NCH(tree);
1828 int res = (validate_ntype(tree, dotted_name)
1829 && is_odd(nch)
1830 && validate_name(CHILD(tree, 0), NULL));
1831 int i;
1832
1833 for (i = 1; res && (i < nch); i += 2) {
1834 res = (validate_dot(CHILD(tree, i))
1835 && validate_name(CHILD(tree, i+1), NULL));
1836 }
1837 return res;
1838}
1839
1840
Fred Drakecff283c2000-08-21 22:24:43 +00001841/* dotted_as_name: dotted_name [NAME NAME]
1842 */
1843static int
1844validate_dotted_as_name(node *tree)
1845{
1846 int nch = NCH(tree);
1847 int res = validate_ntype(tree, dotted_as_name);
1848
1849 if (res) {
1850 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001851 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001852 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001853 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001854 && validate_name(CHILD(tree, 1), "as")
1855 && validate_name(CHILD(tree, 2), NULL));
1856 else {
1857 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001858 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001859 }
1860 }
1861 return res;
1862}
1863
1864
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001865/* dotted_as_name (',' dotted_as_name)* */
1866static int
1867validate_dotted_as_names(node *tree)
1868{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001869 int nch = NCH(tree);
1870 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1871 int i;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001872
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001873 for (i = 1; res && (i < nch); i += 2)
1874 res = (validate_comma(CHILD(tree, i))
1875 && validate_dotted_as_name(CHILD(tree, i + 1)));
1876 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001877}
1878
1879
1880/* import_as_name (',' import_as_name)* [','] */
1881static int
1882validate_import_as_names(node *tree)
1883{
1884 int nch = NCH(tree);
1885 int res = validate_import_as_name(CHILD(tree, 0));
1886 int i;
1887
1888 for (i = 1; res && (i + 1 < nch); i += 2)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001889 res = (validate_comma(CHILD(tree, i))
1890 && validate_import_as_name(CHILD(tree, i + 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001891 return (res);
1892}
1893
1894
1895/* 'import' dotted_as_names */
1896static int
1897validate_import_name(node *tree)
1898{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001899 return (validate_ntype(tree, import_name)
1900 && validate_numnodes(tree, 2, "import_name")
1901 && validate_name(CHILD(tree, 0), "import")
1902 && validate_dotted_as_names(CHILD(tree, 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001903}
1904
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001905/* Helper function to count the number of leading dots in
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001906 * 'from ...module import name'
1907 */
1908static int
1909count_from_dots(node *tree)
1910{
1911 int i;
Benjamin Peterson6624a9f2008-11-03 15:14:51 +00001912 for (i = 1; i < NCH(tree); i++)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001913 if (TYPE(CHILD(tree, i)) != DOT)
1914 break;
Benjamin Peterson6624a9f2008-11-03 15:14:51 +00001915 return i-1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001916}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001917
Mark Dickinson75b44b32010-07-04 16:47:56 +00001918/* import_from: ('from' ('.'* dotted_name | '.'+)
1919 * 'import' ('*' | '(' import_as_names ')' | import_as_names))
Guido van Rossum3d602e31996-07-21 02:33:56 +00001920 */
Guido van Rossum47478871996-08-21 14:32:37 +00001921static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001922validate_import_from(node *tree)
1923{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001924 int nch = NCH(tree);
1925 int ndots = count_from_dots(tree);
1926 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1927 int offset = ndots + havename;
1928 int res = validate_ntype(tree, import_from)
Mark Dickinson75b44b32010-07-04 16:47:56 +00001929 && (offset >= 1)
1930 && (nch >= 3 + offset)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001931 && validate_name(CHILD(tree, 0), "from")
1932 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1933 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001934
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001935 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1936 res = ((nch == offset + 5)
1937 && validate_lparen(CHILD(tree, offset + 2))
1938 && validate_import_as_names(CHILD(tree, offset + 3))
1939 && validate_rparen(CHILD(tree, offset + 4)));
1940 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1941 res = validate_import_as_names(CHILD(tree, offset + 2));
1942 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001943}
1944
1945
1946/* import_stmt: import_name | import_from */
1947static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001948validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001949{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001950 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001951 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001952
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001953 if (res) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001954 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001955
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001956 if (ntype == import_name || ntype == import_from)
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001957 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001958 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001959 res = 0;
1960 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001961 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001962 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001963 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001964 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001965 PyErr_Format(parser_error,
1966 "Unrecognized child node of import_stmt: %d.",
1967 TYPE(CHILD(tree, 0)));
1968 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001969 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001970}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001971
1972
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001973
1974
Guido van Rossum47478871996-08-21 14:32:37 +00001975static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001976validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001977{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001978 int j;
1979 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001980 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001981 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001982
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001983 if (!res && !PyErr_Occurred())
1984 err_string("illegal global statement");
1985
Guido van Rossum3d602e31996-07-21 02:33:56 +00001986 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001987 res = (validate_name(CHILD(tree, 0), "global")
1988 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001989 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001990 res = (validate_comma(CHILD(tree, j))
1991 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001992
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001993 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001994}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001995
1996
Guido van Rossum3d602e31996-07-21 02:33:56 +00001997/* exec_stmt:
1998 *
1999 * 'exec' expr ['in' test [',' test]]
2000 */
Guido van Rossum47478871996-08-21 14:32:37 +00002001static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002002validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002003{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002004 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002005 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002006 && ((nch == 2) || (nch == 4) || (nch == 6))
2007 && validate_name(CHILD(tree, 0), "exec")
2008 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002009
Guido van Rossum3d602e31996-07-21 02:33:56 +00002010 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002011 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002012 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002013 res = (validate_name(CHILD(tree, 2), "in")
2014 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002015 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00002016 res = (validate_comma(CHILD(tree, 4))
2017 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002018
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002019 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002020}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002021
2022
Guido van Rossum925e5471997-04-02 05:32:13 +00002023/* assert_stmt:
2024 *
2025 * 'assert' test [',' test]
2026 */
2027static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002028validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00002029{
2030 int nch = NCH(tree);
2031 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002032 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00002033 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002034 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00002035
2036 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002037 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00002038 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002039 res = (validate_comma(CHILD(tree, 2))
2040 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00002041
2042 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002043}
Guido van Rossum925e5471997-04-02 05:32:13 +00002044
2045
Guido van Rossum47478871996-08-21 14:32:37 +00002046static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002047validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002048{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002049 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002050 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002051 && ((nch == 4) || (nch == 7))
2052 && validate_name(CHILD(tree, 0), "while")
2053 && validate_test(CHILD(tree, 1))
2054 && validate_colon(CHILD(tree, 2))
2055 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002056
Guido van Rossum3d602e31996-07-21 02:33:56 +00002057 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00002058 res = (validate_name(CHILD(tree, 4), "else")
2059 && validate_colon(CHILD(tree, 5))
2060 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002061
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002062 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002063}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002064
2065
Guido van Rossum47478871996-08-21 14:32:37 +00002066static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002067validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002068{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002069 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002070 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002071 && ((nch == 6) || (nch == 9))
2072 && validate_name(CHILD(tree, 0), "for")
2073 && validate_exprlist(CHILD(tree, 1))
2074 && validate_name(CHILD(tree, 2), "in")
2075 && validate_testlist(CHILD(tree, 3))
2076 && validate_colon(CHILD(tree, 4))
2077 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002078
Guido van Rossum3d602e31996-07-21 02:33:56 +00002079 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00002080 res = (validate_name(CHILD(tree, 6), "else")
2081 && validate_colon(CHILD(tree, 7))
2082 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002083
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002084 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002085}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002086
2087
Guido van Rossum3d602e31996-07-21 02:33:56 +00002088/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00002089 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Georg Brandlfe879e82008-12-05 12:09:41 +00002090 ['finally' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002091 * | 'try' ':' suite 'finally' ':' suite
2092 *
2093 */
Guido van Rossum47478871996-08-21 14:32:37 +00002094static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002095validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002096{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002097 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002098 int pos = 3;
2099 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002100 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002101
2102 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002103 res = (validate_name(CHILD(tree, 0), "try")
2104 && validate_colon(CHILD(tree, 1))
2105 && validate_suite(CHILD(tree, 2))
2106 && validate_colon(CHILD(tree, nch - 2))
2107 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00002108 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00002109 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00002110 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
2111 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00002112
2113 PyErr_Format(parser_error,
2114 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002115 }
Georg Brandlfe879e82008-12-05 12:09:41 +00002116 /* Handle try/finally statement */
2117 if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
2118 (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
2119 res = (validate_numnodes(tree, 6, "try/finally")
2120 && validate_colon(CHILD(tree, 4))
2121 && validate_suite(CHILD(tree, 5)));
2122 return (res);
2123 }
2124 /* try/except statement: skip past except_clause sections */
Antoine Pitrou42b5bcf2009-05-14 21:48:09 +00002125 while (res && pos < nch && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002126 res = (validate_except_clause(CHILD(tree, pos))
2127 && validate_colon(CHILD(tree, pos + 1))
2128 && validate_suite(CHILD(tree, pos + 2)));
2129 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002130 }
Georg Brandlfe879e82008-12-05 12:09:41 +00002131 /* skip else clause */
Antoine Pitrou42b5bcf2009-05-14 21:48:09 +00002132 if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) &&
Georg Brandlfe879e82008-12-05 12:09:41 +00002133 (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
2134 res = (validate_colon(CHILD(tree, pos + 1))
2135 && validate_suite(CHILD(tree, pos + 2)));
2136 pos += 3;
2137 }
2138 if (res && pos < nch) {
2139 /* last clause must be a finally */
2140 res = (validate_name(CHILD(tree, pos), "finally")
2141 && validate_numnodes(tree, pos + 3, "try/except/finally")
2142 && validate_colon(CHILD(tree, pos + 1))
2143 && validate_suite(CHILD(tree, pos + 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002144 }
2145 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002146}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002147
2148
Guido van Rossum47478871996-08-21 14:32:37 +00002149static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002150validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002151{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002152 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002153 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00002154 && ((nch == 1) || (nch == 2) || (nch == 4))
2155 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002156
Guido van Rossum3d602e31996-07-21 02:33:56 +00002157 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002158 res = validate_test(CHILD(tree, 1));
Mark Dickinson070f0ab2010-06-30 16:27:57 +00002159 if (res && (nch == 4)) {
2160 if (TYPE(CHILD(tree, 2)) == NAME)
2161 res = validate_name(CHILD(tree, 2), "as");
2162 else
2163 res = validate_comma(CHILD(tree, 2));
2164 res = res && validate_test(CHILD(tree, 3));
2165 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002166 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002167}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002168
2169
Guido van Rossum47478871996-08-21 14:32:37 +00002170static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002171validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002172{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002173 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002174 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002175
Guido van Rossum3d602e31996-07-21 02:33:56 +00002176 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002177 res = ((nch == 1)
2178 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002179 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002180 res = validate_or_test(CHILD(tree, 0));
2181 res = (res && (nch == 1 || (nch == 5 &&
2182 validate_name(CHILD(tree, 1), "if") &&
2183 validate_or_test(CHILD(tree, 2)) &&
2184 validate_name(CHILD(tree, 3), "else") &&
2185 validate_test(CHILD(tree, 4)))));
2186 }
2187 return (res);
2188}
2189
2190static int
2191validate_old_test(node *tree)
2192{
2193 int nch = NCH(tree);
2194 int res = validate_ntype(tree, old_test) && (nch == 1);
2195
2196 if (res && (TYPE(CHILD(tree, 0)) == old_lambdef))
2197 res = (validate_old_lambdef(CHILD(tree, 0)));
2198 else if (res) {
2199 res = (validate_or_test(CHILD(tree, 0)));
2200 }
2201 return (res);
2202}
2203
2204static int
2205validate_or_test(node *tree)
2206{
2207 int nch = NCH(tree);
2208 int res = validate_ntype(tree, or_test) && is_odd(nch);
2209
2210 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002211 int pos;
2212 res = validate_and_test(CHILD(tree, 0));
2213 for (pos = 1; res && (pos < nch); pos += 2)
2214 res = (validate_name(CHILD(tree, pos), "or")
2215 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002216 }
2217 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002218}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002219
2220
Guido van Rossum47478871996-08-21 14:32:37 +00002221static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002222validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002223{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002224 int pos;
2225 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002226 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002227 && is_odd(nch)
2228 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002229
Guido van Rossum3d602e31996-07-21 02:33:56 +00002230 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002231 res = (validate_name(CHILD(tree, pos), "and")
2232 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002233
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002234 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002235}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002236
2237
Guido van Rossum47478871996-08-21 14:32:37 +00002238static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002239validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002240{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002241 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002242 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002243
Guido van Rossum3d602e31996-07-21 02:33:56 +00002244 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002245 if (nch == 2)
2246 res = (validate_name(CHILD(tree, 0), "not")
2247 && validate_not_test(CHILD(tree, 1)));
2248 else if (nch == 1)
2249 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002250 }
2251 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002252}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002253
2254
Guido van Rossum47478871996-08-21 14:32:37 +00002255static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002256validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002257{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002258 int pos;
2259 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002260 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002261 && is_odd(nch)
2262 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002263
Guido van Rossum3d602e31996-07-21 02:33:56 +00002264 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002265 res = (validate_comp_op(CHILD(tree, pos))
2266 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002267
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002268 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002269}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002270
2271
Guido van Rossum47478871996-08-21 14:32:37 +00002272static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002273validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002274{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002275 int res = 0;
2276 int nch = NCH(tree);
2277
Guido van Rossum3d602e31996-07-21 02:33:56 +00002278 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002279 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002280 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002281 /*
2282 * Only child will be a terminal with a well-defined symbolic name
2283 * or a NAME with a string of either 'is' or 'in'
2284 */
2285 tree = CHILD(tree, 0);
2286 switch (TYPE(tree)) {
2287 case LESS:
2288 case GREATER:
2289 case EQEQUAL:
2290 case EQUAL:
2291 case LESSEQUAL:
2292 case GREATEREQUAL:
2293 case NOTEQUAL:
2294 res = 1;
2295 break;
2296 case NAME:
2297 res = ((strcmp(STR(tree), "in") == 0)
2298 || (strcmp(STR(tree), "is") == 0));
2299 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002300 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002301 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002302 }
2303 break;
2304 default:
Fred Drake661ea262000-10-24 19:57:45 +00002305 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002306 break;
2307 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002308 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002309 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002310 res = (validate_ntype(CHILD(tree, 0), NAME)
2311 && validate_ntype(CHILD(tree, 1), NAME)
2312 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2313 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2314 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2315 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2316 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002317 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002318 }
2319 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002320}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002321
2322
Guido van Rossum47478871996-08-21 14:32:37 +00002323static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002324validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002325{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002326 int j;
2327 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002328 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002329 && is_odd(nch)
2330 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002331
Guido van Rossum3d602e31996-07-21 02:33:56 +00002332 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002333 res = (validate_xor_expr(CHILD(tree, j))
2334 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002335
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002336 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002337}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002338
2339
Guido van Rossum47478871996-08-21 14:32:37 +00002340static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002341validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002342{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002343 int j;
2344 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002345 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002346 && is_odd(nch)
2347 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002348
Guido van Rossum3d602e31996-07-21 02:33:56 +00002349 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002350 res = (validate_circumflex(CHILD(tree, j - 1))
2351 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002352
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002353 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002354}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002355
2356
Guido van Rossum47478871996-08-21 14:32:37 +00002357static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002358validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002359{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002360 int pos;
2361 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002362 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002363 && is_odd(nch)
2364 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002365
Guido van Rossum3d602e31996-07-21 02:33:56 +00002366 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002367 res = (validate_ampersand(CHILD(tree, pos))
2368 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002369
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002370 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002371}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002372
2373
2374static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002375validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002376 {
2377 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002378 int nch = NCH(tree);
2379 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002380 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002381
Guido van Rossum3d602e31996-07-21 02:33:56 +00002382 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002383 if (TYPE(CHILD(tree, pos)) != op1)
2384 res = validate_ntype(CHILD(tree, pos), op2);
2385 if (res)
2386 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002387 }
2388 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002389}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002390
2391
Guido van Rossum47478871996-08-21 14:32:37 +00002392static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002393validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002394{
2395 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002396 && validate_chain_two_ops(tree, validate_arith_expr,
2397 LEFTSHIFT, RIGHTSHIFT));
2398}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002399
2400
Guido van Rossum47478871996-08-21 14:32:37 +00002401static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002402validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002403{
2404 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002405 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2406}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002407
2408
Guido van Rossum47478871996-08-21 14:32:37 +00002409static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002410validate_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002411{
2412 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002413 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002414 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002415 && is_odd(nch)
2416 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002417
Guido van Rossum3d602e31996-07-21 02:33:56 +00002418 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002419 res = (((TYPE(CHILD(tree, pos)) == STAR)
2420 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002421 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002422 || (TYPE(CHILD(tree, pos)) == PERCENT))
2423 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002424
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002425 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002426}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002427
2428
Guido van Rossum3d602e31996-07-21 02:33:56 +00002429/* factor:
2430 *
2431 * factor: ('+'|'-'|'~') factor | power
2432 */
Guido van Rossum47478871996-08-21 14:32:37 +00002433static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002434validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002435{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002436 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002437 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002438 && (((nch == 2)
2439 && ((TYPE(CHILD(tree, 0)) == PLUS)
2440 || (TYPE(CHILD(tree, 0)) == MINUS)
2441 || (TYPE(CHILD(tree, 0)) == TILDE))
2442 && validate_factor(CHILD(tree, 1)))
2443 || ((nch == 1)
2444 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002445 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002446}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002447
2448
Guido van Rossum3d602e31996-07-21 02:33:56 +00002449/* power:
2450 *
2451 * power: atom trailer* ('**' factor)*
2452 */
Guido van Rossum47478871996-08-21 14:32:37 +00002453static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002454validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002455{
2456 int pos = 1;
2457 int nch = NCH(tree);
2458 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002459 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002460
2461 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002462 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002463 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002464 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002465 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002466 return (0);
2467 }
2468 for ( ; res && (pos < (nch - 1)); pos += 2)
2469 res = (validate_doublestar(CHILD(tree, pos))
2470 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002471 }
2472 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002473}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002474
2475
Guido van Rossum47478871996-08-21 14:32:37 +00002476static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002477validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002478{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002479 int pos;
2480 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002481 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002482
Fred Drakecff283c2000-08-21 22:24:43 +00002483 if (res && nch < 1)
2484 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002485 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002486 switch (TYPE(CHILD(tree, 0))) {
2487 case LPAR:
2488 res = ((nch <= 3)
2489 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002490
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002491 if (res && (nch == 3)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002492 if (TYPE(CHILD(tree, 1))==yield_expr)
2493 res = validate_yield_expr(CHILD(tree, 1));
2494 else
2495 res = validate_testlist_comp(CHILD(tree, 1));
2496 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002497 break;
2498 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002499 if (nch == 2)
2500 res = validate_ntype(CHILD(tree, 1), RSQB);
2501 else if (nch == 3)
2502 res = (validate_listmaker(CHILD(tree, 1))
2503 && validate_ntype(CHILD(tree, 2), RSQB));
2504 else {
2505 res = 0;
2506 err_string("illegal list display atom");
2507 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002508 break;
2509 case LBRACE:
2510 res = ((nch <= 3)
2511 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002512
Fred Drakeff9ea482000-04-19 13:54:15 +00002513 if (res && (nch == 3))
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002514 res = validate_dictorsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002515 break;
2516 case BACKQUOTE:
2517 res = ((nch == 3)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002518 && validate_testlist1(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002519 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2520 break;
2521 case NAME:
2522 case NUMBER:
2523 res = (nch == 1);
2524 break;
2525 case STRING:
2526 for (pos = 1; res && (pos < nch); ++pos)
2527 res = validate_ntype(CHILD(tree, pos), STRING);
2528 break;
2529 default:
2530 res = 0;
2531 break;
2532 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002533 }
2534 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002535}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002536
2537
Fred Drake85bf3bb2000-08-23 15:35:26 +00002538/* listmaker:
2539 * test ( list_for | (',' test)* [','] )
2540 */
Fred Drakecff283c2000-08-21 22:24:43 +00002541static int
2542validate_listmaker(node *tree)
2543{
2544 int nch = NCH(tree);
2545 int ok = nch;
2546
2547 if (nch == 0)
2548 err_string("missing child nodes of listmaker");
2549 else
2550 ok = validate_test(CHILD(tree, 0));
2551
2552 /*
Raymond Hettinger354433a2004-05-19 08:20:33 +00002553 * list_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002554 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002555 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2556 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002557 else {
2558 /* (',' test)* [','] */
2559 int i = 1;
2560 while (ok && nch - i >= 2) {
2561 ok = (validate_comma(CHILD(tree, i))
2562 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002563 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002564 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002565 if (ok && i == nch-1)
2566 ok = validate_comma(CHILD(tree, i));
2567 else if (i != nch) {
2568 ok = 0;
2569 err_string("illegal trailing nodes for listmaker");
2570 }
Fred Drakecff283c2000-08-21 22:24:43 +00002571 }
2572 return ok;
2573}
2574
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002575/* testlist_comp:
2576 * test ( comp_for | (',' test)* [','] )
Raymond Hettinger354433a2004-05-19 08:20:33 +00002577 */
2578static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002579validate_testlist_comp(node *tree)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002580{
2581 int nch = NCH(tree);
2582 int ok = nch;
2583
2584 if (nch == 0)
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002585 err_string("missing child nodes of testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002586 else {
2587 ok = validate_test(CHILD(tree, 0));
2588 }
2589
2590 /*
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002591 * comp_for | (',' test)* [',']
Raymond Hettinger354433a2004-05-19 08:20:33 +00002592 */
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002593 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2594 ok = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002595 else {
2596 /* (',' test)* [','] */
2597 int i = 1;
2598 while (ok && nch - i >= 2) {
2599 ok = (validate_comma(CHILD(tree, i))
2600 && validate_test(CHILD(tree, i+1)));
2601 i += 2;
2602 }
2603 if (ok && i == nch-1)
2604 ok = validate_comma(CHILD(tree, i));
2605 else if (i != nch) {
2606 ok = 0;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002607 err_string("illegal trailing nodes for testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002608 }
2609 }
2610 return ok;
2611}
Fred Drakecff283c2000-08-21 22:24:43 +00002612
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002613/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002614 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002615 */
2616static int
2617validate_decorator(node *tree)
2618{
2619 int ok;
2620 int nch = NCH(tree);
2621 ok = (validate_ntype(tree, decorator) &&
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002622 (nch == 3 || nch == 5 || nch == 6) &&
2623 validate_at(CHILD(tree, 0)) &&
2624 validate_dotted_name(CHILD(tree, 1)) &&
2625 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002626
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002627 if (ok && nch != 3) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002628 ok = (validate_lparen(CHILD(tree, 2)) &&
2629 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002630
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002631 if (ok && nch == 6)
2632 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002633 }
2634
2635 return ok;
2636}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002637
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002638/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002639 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002640 */
2641static int
2642validate_decorators(node *tree)
2643{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002644 int i, nch, ok;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002645 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002646 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002647
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002648 for (i = 0; ok && i < nch; ++i)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002649 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002650
2651 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002652}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002653
Georg Brandl944f6842009-05-25 21:02:56 +00002654/* with_item:
2655 * test ['as' expr]
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002656 */
2657static int
Georg Brandl944f6842009-05-25 21:02:56 +00002658validate_with_item(node *tree)
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002659{
2660 int nch = NCH(tree);
Georg Brandl944f6842009-05-25 21:02:56 +00002661 int ok = (validate_ntype(tree, with_item)
2662 && (nch == 1 || nch == 3)
2663 && validate_test(CHILD(tree, 0)));
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002664 if (ok && nch == 3)
Georg Brandl944f6842009-05-25 21:02:56 +00002665 ok = (validate_name(CHILD(tree, 1), "as")
2666 && validate_expr(CHILD(tree, 2)));
2667 return ok;
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002668}
2669
Georg Brandl944f6842009-05-25 21:02:56 +00002670/* with_stmt:
2671 * 0 1 ... -2 -1
2672 * 'with' with_item (',' with_item)* ':' suite
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002673 */
2674static int
2675validate_with_stmt(node *tree)
2676{
Georg Brandl944f6842009-05-25 21:02:56 +00002677 int i;
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002678 int nch = NCH(tree);
2679 int ok = (validate_ntype(tree, with_stmt)
Georg Brandl944f6842009-05-25 21:02:56 +00002680 && (nch % 2 == 0)
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002681 && validate_name(CHILD(tree, 0), "with")
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002682 && validate_colon(RCHILD(tree, -2))
2683 && validate_suite(RCHILD(tree, -1)));
Georg Brandl944f6842009-05-25 21:02:56 +00002684 for (i = 1; ok && i < nch - 2; i += 2)
2685 ok = validate_with_item(CHILD(tree, i));
2686 return ok;
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002687}
2688
Guido van Rossum3d602e31996-07-21 02:33:56 +00002689/* funcdef:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002690 *
Christian Heimes5224d282008-02-23 15:01:05 +00002691 * -5 -4 -3 -2 -1
2692 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002693 */
Guido van Rossum47478871996-08-21 14:32:37 +00002694static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002695validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002696{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002697 int nch = NCH(tree);
2698 int ok = (validate_ntype(tree, funcdef)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002699 && (nch == 5)
2700 && validate_name(RCHILD(tree, -5), "def")
2701 && validate_ntype(RCHILD(tree, -4), NAME)
2702 && validate_colon(RCHILD(tree, -2))
2703 && validate_parameters(RCHILD(tree, -3))
2704 && validate_suite(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002705 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002706}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002707
2708
Christian Heimes5224d282008-02-23 15:01:05 +00002709/* decorated
2710 * decorators (classdef | funcdef)
2711 */
2712static int
2713validate_decorated(node *tree)
2714{
Mark Dickinsona7ee59b2010-07-04 16:23:54 +00002715 int nch = NCH(tree);
2716 int ok = (validate_ntype(tree, decorated)
2717 && (nch == 2)
2718 && validate_decorators(RCHILD(tree, -2)));
2719 if (TYPE(RCHILD(tree, -1)) == funcdef)
2720 ok = ok && validate_funcdef(RCHILD(tree, -1));
2721 else
2722 ok = ok && validate_class(RCHILD(tree, -1));
2723 return ok;
Christian Heimes5224d282008-02-23 15:01:05 +00002724}
2725
Guido van Rossum47478871996-08-21 14:32:37 +00002726static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002727validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002728{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002729 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002730 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002731 && ((nch == 3) || (nch == 4))
2732 && validate_name(CHILD(tree, 0), "lambda")
2733 && validate_colon(CHILD(tree, nch - 2))
2734 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002735
Guido van Rossum3d602e31996-07-21 02:33:56 +00002736 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002737 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002738 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002739 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002740
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002741 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002742}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002743
2744
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002745static int
2746validate_old_lambdef(node *tree)
2747{
2748 int nch = NCH(tree);
2749 int res = (validate_ntype(tree, old_lambdef)
2750 && ((nch == 3) || (nch == 4))
2751 && validate_name(CHILD(tree, 0), "lambda")
2752 && validate_colon(CHILD(tree, nch - 2))
2753 && validate_test(CHILD(tree, nch - 1)));
2754
2755 if (res && (nch == 4))
2756 res = validate_varargslist(CHILD(tree, 1));
2757 else if (!res && !PyErr_Occurred())
2758 (void) validate_numnodes(tree, 3, "old_lambdef");
2759
2760 return (res);
2761}
2762
2763
Guido van Rossum3d602e31996-07-21 02:33:56 +00002764/* arglist:
2765 *
Fred Drakecff283c2000-08-21 22:24:43 +00002766 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002767 */
Guido van Rossum47478871996-08-21 14:32:37 +00002768static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002769validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002770{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002771 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002772 int i = 0;
2773 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002774
2775 if (nch <= 0)
2776 /* raise the right error from having an invalid number of children */
2777 return validate_numnodes(tree, nch + 1, "arglist");
2778
Raymond Hettinger354433a2004-05-19 08:20:33 +00002779 if (nch > 1) {
2780 for (i=0; i<nch; i++) {
2781 if (TYPE(CHILD(tree, i)) == argument) {
2782 node *ch = CHILD(tree, i);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002783 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
Raymond Hettinger354433a2004-05-19 08:20:33 +00002784 err_string("need '(', ')' for generator expression");
2785 return 0;
2786 }
2787 }
2788 }
2789 }
2790
Fred Drakecff283c2000-08-21 22:24:43 +00002791 while (ok && nch-i >= 2) {
2792 /* skip leading (argument ',') */
2793 ok = (validate_argument(CHILD(tree, i))
2794 && validate_comma(CHILD(tree, i+1)));
2795 if (ok)
2796 i += 2;
2797 else
2798 PyErr_Clear();
2799 }
2800 ok = 1;
2801 if (nch-i > 0) {
2802 /*
2803 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002804 */
Fred Drakecff283c2000-08-21 22:24:43 +00002805 int sym = TYPE(CHILD(tree, i));
2806
2807 if (sym == argument) {
2808 ok = validate_argument(CHILD(tree, i));
2809 if (ok && i+1 != nch) {
2810 err_string("illegal arglist specification"
2811 " (extra stuff on end)");
2812 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002813 }
Fred Drakecff283c2000-08-21 22:24:43 +00002814 }
2815 else if (sym == STAR) {
2816 ok = validate_star(CHILD(tree, i));
2817 if (ok && (nch-i == 2))
2818 ok = validate_test(CHILD(tree, i+1));
2819 else if (ok && (nch-i == 5))
2820 ok = (validate_test(CHILD(tree, i+1))
2821 && validate_comma(CHILD(tree, i+2))
2822 && validate_doublestar(CHILD(tree, i+3))
2823 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002824 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002825 err_string("illegal use of '*' in arglist");
2826 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002827 }
2828 }
Fred Drakecff283c2000-08-21 22:24:43 +00002829 else if (sym == DOUBLESTAR) {
2830 if (nch-i == 2)
2831 ok = (validate_doublestar(CHILD(tree, i))
2832 && validate_test(CHILD(tree, i+1)));
2833 else {
2834 err_string("illegal use of '**' in arglist");
2835 ok = 0;
2836 }
2837 }
2838 else {
2839 err_string("illegal arglist specification");
2840 ok = 0;
2841 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002842 }
2843 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002844}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002845
2846
2847
2848/* argument:
2849 *
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002850 * [test '='] test [comp_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002851 */
Guido van Rossum47478871996-08-21 14:32:37 +00002852static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002853validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002854{
2855 int nch = NCH(tree);
2856 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002857 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002858 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002859
Raymond Hettinger354433a2004-05-19 08:20:33 +00002860 if (res && (nch == 2))
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002861 res = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002862 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002863 res = (validate_equal(CHILD(tree, 1))
2864 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002865
2866 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002867}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002868
2869
2870
2871/* trailer:
2872 *
Guido van Rossum47478871996-08-21 14:32:37 +00002873 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002874 */
Guido van Rossum47478871996-08-21 14:32:37 +00002875static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002876validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002877{
2878 int nch = NCH(tree);
2879 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002880
2881 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002882 switch (TYPE(CHILD(tree, 0))) {
2883 case LPAR:
2884 res = validate_rparen(CHILD(tree, nch - 1));
2885 if (res && (nch == 3))
2886 res = validate_arglist(CHILD(tree, 1));
2887 break;
2888 case LSQB:
2889 res = (validate_numnodes(tree, 3, "trailer")
2890 && validate_subscriptlist(CHILD(tree, 1))
2891 && validate_ntype(CHILD(tree, 2), RSQB));
2892 break;
2893 case DOT:
2894 res = (validate_numnodes(tree, 2, "trailer")
2895 && validate_ntype(CHILD(tree, 1), NAME));
2896 break;
2897 default:
2898 res = 0;
2899 break;
2900 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002901 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002902 else {
2903 (void) validate_numnodes(tree, 2, "trailer");
2904 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002905 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002906}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002907
2908
Guido van Rossum47478871996-08-21 14:32:37 +00002909/* subscriptlist:
2910 *
2911 * subscript (',' subscript)* [',']
2912 */
2913static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002914validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002915{
2916 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002917 validate_subscript, "subscriptlist"));
2918}
Guido van Rossum47478871996-08-21 14:32:37 +00002919
2920
Guido van Rossum3d602e31996-07-21 02:33:56 +00002921/* subscript:
2922 *
Guido van Rossum47478871996-08-21 14:32:37 +00002923 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002924 */
Guido van Rossum47478871996-08-21 14:32:37 +00002925static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002926validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002927{
Guido van Rossum47478871996-08-21 14:32:37 +00002928 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002929 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002930 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002931
Guido van Rossum47478871996-08-21 14:32:37 +00002932 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002933 if (!PyErr_Occurred())
2934 err_string("invalid number of arguments for subscript node");
2935 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002936 }
Guido van Rossum47478871996-08-21 14:32:37 +00002937 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002938 /* take care of ('.' '.' '.') possibility */
2939 return (validate_numnodes(tree, 3, "subscript")
2940 && validate_dot(CHILD(tree, 0))
2941 && validate_dot(CHILD(tree, 1))
2942 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002943 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002944 if (TYPE(CHILD(tree, 0)) == test)
2945 res = validate_test(CHILD(tree, 0));
2946 else
2947 res = validate_colon(CHILD(tree, 0));
2948 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002949 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002950 /* Must be [test] ':' [test] [sliceop],
2951 * but at least one of the optional components will
2952 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002953 */
2954 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002955 res = validate_test(CHILD(tree, 0));
2956 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002957 }
2958 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002959 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002960 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002961 int rem = nch - ++offset;
2962 if (rem) {
2963 if (TYPE(CHILD(tree, offset)) == test) {
2964 res = validate_test(CHILD(tree, offset));
2965 ++offset;
2966 --rem;
2967 }
2968 if (res && rem)
2969 res = validate_sliceop(CHILD(tree, offset));
2970 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002971 }
2972 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002973}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002974
2975
Guido van Rossum47478871996-08-21 14:32:37 +00002976static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002977validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002978{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002979 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002980 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002981 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002982 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002983 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002984 }
Guido van Rossum47478871996-08-21 14:32:37 +00002985 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002986 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002987 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002988 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002989
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002990 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002991}
Guido van Rossum47478871996-08-21 14:32:37 +00002992
2993
2994static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002995validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002996{
2997 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002998 validate_expr, "exprlist"));
2999}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003000
3001
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003002/*
3003 * dictorsetmaker:
Alexandre Vassalottib6465472010-01-11 22:36:12 +00003004 *
3005 * (test ':' test (comp_for | (',' test ':' test)* [','])) |
3006 * (test (comp_for | (',' test)* [',']))
3007 */
Guido van Rossum47478871996-08-21 14:32:37 +00003008static int
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003009validate_dictorsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003010{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003011 int nch = NCH(tree);
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003012 int ok = validate_ntype(tree, dictorsetmaker);
3013 int i = 0;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00003014 int check_trailing_comma = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003015
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003016 assert(nch > 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003017
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003018 if (ok && (nch == 1 || TYPE(CHILD(tree, 1)) == COMMA)) {
3019 /* We got a set:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003020 * test (',' test)* [',']
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003021 */
3022 ok = validate_test(CHILD(tree, i++));
3023 while (ok && nch - i >= 2) {
3024 ok = (validate_comma(CHILD(tree, i))
3025 && validate_test(CHILD(tree, i+1)));
3026 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00003027 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00003028 check_trailing_comma = 1;
3029 }
3030 else if (ok && TYPE(CHILD(tree, 1)) == comp_for) {
3031 /* We got a set comprehension:
3032 * test comp_for
3033 */
3034 ok = (validate_test(CHILD(tree, 0))
3035 && validate_comp_for(CHILD(tree, 1)));
3036 }
3037 else if (ok && NCH(tree) > 3 && TYPE(CHILD(tree, 3)) == comp_for) {
3038 /* We got a dict comprehension:
3039 * test ':' test comp_for
3040 */
3041 ok = (validate_test(CHILD(tree, 0))
3042 && validate_colon(CHILD(tree, 1))
3043 && validate_test(CHILD(tree, 2))
3044 && validate_comp_for(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003045 }
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003046 else if (ok) {
3047 /* We got a dict:
3048 * test ':' test (',' test ':' test)* [',']
3049 */
3050 if (nch >= 3) {
3051 ok = (validate_test(CHILD(tree, i))
3052 && validate_colon(CHILD(tree, i+1))
3053 && validate_test(CHILD(tree, i+2)));
3054 i += 3;
3055 }
3056 else {
3057 ok = 0;
3058 err_string("illegal number of nodes for dictorsetmaker");
3059 }
3060
3061 while (ok && nch - i >= 4) {
3062 ok = (validate_comma(CHILD(tree, i))
3063 && validate_test(CHILD(tree, i+1))
3064 && validate_colon(CHILD(tree, i+2))
3065 && validate_test(CHILD(tree, i+3)));
3066 i += 4;
3067 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00003068 check_trailing_comma = 1;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003069 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00003070 if (ok && check_trailing_comma) {
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003071 if (i == nch-1)
3072 ok = validate_comma(CHILD(tree, i));
3073 else if (i != nch) {
3074 ok = 0;
3075 err_string("illegal trailing nodes for dictorsetmaker");
3076 }
3077 }
3078
3079 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00003080}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003081
3082
Guido van Rossum47478871996-08-21 14:32:37 +00003083static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003084validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003085{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003086 int pos;
3087 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003088 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00003089 && (nch >= 2)
3090 && validate_testlist(CHILD(tree, 0))
3091 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003092
Guido van Rossum3d602e31996-07-21 02:33:56 +00003093 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00003094 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003095
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003096 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003097}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003098
3099
Guido van Rossum47478871996-08-21 14:32:37 +00003100static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003101validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003102{
Fred Drakeff9ea482000-04-19 13:54:15 +00003103 int nch = 0; /* num. children on current node */
3104 int res = 1; /* result value */
3105 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00003106
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00003107 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003108 nch = NCH(tree);
3109 next = 0;
3110 switch (TYPE(tree)) {
3111 /*
3112 * Definition nodes.
3113 */
3114 case funcdef:
3115 res = validate_funcdef(tree);
3116 break;
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00003117 case with_stmt:
3118 res = validate_with_stmt(tree);
3119 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003120 case classdef:
3121 res = validate_class(tree);
3122 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003123 case decorated:
3124 res = validate_decorated(tree);
3125 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003126 /*
3127 * "Trivial" parse tree nodes.
3128 * (Why did I call these trivial?)
3129 */
3130 case stmt:
3131 res = validate_stmt(tree);
3132 break;
3133 case small_stmt:
3134 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00003135 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00003136 * | import_stmt | global_stmt | exec_stmt | assert_stmt
3137 */
3138 res = validate_small_stmt(tree);
3139 break;
3140 case flow_stmt:
3141 res = (validate_numnodes(tree, 1, "flow_stmt")
3142 && ((TYPE(CHILD(tree, 0)) == break_stmt)
3143 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00003144 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00003145 || (TYPE(CHILD(tree, 0)) == return_stmt)
3146 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
3147 if (res)
3148 next = CHILD(tree, 0);
3149 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00003150 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003151 break;
Fred Drake02126f22001-07-17 02:59:15 +00003152 case yield_stmt:
3153 res = validate_yield_stmt(tree);
3154 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003155 /*
3156 * Compound statements.
3157 */
3158 case simple_stmt:
3159 res = validate_simple_stmt(tree);
3160 break;
3161 case compound_stmt:
3162 res = validate_compound_stmt(tree);
3163 break;
3164 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00003165 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00003166 */
3167 case expr_stmt:
3168 res = validate_expr_stmt(tree);
3169 break;
3170 case print_stmt:
3171 res = validate_print_stmt(tree);
3172 break;
3173 case del_stmt:
3174 res = validate_del_stmt(tree);
3175 break;
3176 case pass_stmt:
3177 res = (validate_numnodes(tree, 1, "pass")
3178 && validate_name(CHILD(tree, 0), "pass"));
3179 break;
3180 case break_stmt:
3181 res = (validate_numnodes(tree, 1, "break")
3182 && validate_name(CHILD(tree, 0), "break"));
3183 break;
3184 case continue_stmt:
3185 res = (validate_numnodes(tree, 1, "continue")
3186 && validate_name(CHILD(tree, 0), "continue"));
3187 break;
3188 case return_stmt:
3189 res = validate_return_stmt(tree);
3190 break;
3191 case raise_stmt:
3192 res = validate_raise_stmt(tree);
3193 break;
3194 case import_stmt:
3195 res = validate_import_stmt(tree);
3196 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003197 case import_name:
3198 res = validate_import_name(tree);
3199 break;
3200 case import_from:
3201 res = validate_import_from(tree);
3202 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003203 case global_stmt:
3204 res = validate_global_stmt(tree);
3205 break;
3206 case exec_stmt:
3207 res = validate_exec_stmt(tree);
3208 break;
3209 case assert_stmt:
3210 res = validate_assert_stmt(tree);
3211 break;
3212 case if_stmt:
3213 res = validate_if(tree);
3214 break;
3215 case while_stmt:
3216 res = validate_while(tree);
3217 break;
3218 case for_stmt:
3219 res = validate_for(tree);
3220 break;
3221 case try_stmt:
3222 res = validate_try(tree);
3223 break;
3224 case suite:
3225 res = validate_suite(tree);
3226 break;
3227 /*
3228 * Expression nodes.
3229 */
3230 case testlist:
3231 res = validate_testlist(tree);
3232 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003233 case yield_expr:
3234 res = validate_yield_expr(tree);
3235 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00003236 case testlist1:
3237 res = validate_testlist1(tree);
3238 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003239 case test:
3240 res = validate_test(tree);
3241 break;
3242 case and_test:
3243 res = validate_and_test(tree);
3244 break;
3245 case not_test:
3246 res = validate_not_test(tree);
3247 break;
3248 case comparison:
3249 res = validate_comparison(tree);
3250 break;
3251 case exprlist:
3252 res = validate_exprlist(tree);
3253 break;
3254 case comp_op:
3255 res = validate_comp_op(tree);
3256 break;
3257 case expr:
3258 res = validate_expr(tree);
3259 break;
3260 case xor_expr:
3261 res = validate_xor_expr(tree);
3262 break;
3263 case and_expr:
3264 res = validate_and_expr(tree);
3265 break;
3266 case shift_expr:
3267 res = validate_shift_expr(tree);
3268 break;
3269 case arith_expr:
3270 res = validate_arith_expr(tree);
3271 break;
3272 case term:
3273 res = validate_term(tree);
3274 break;
3275 case factor:
3276 res = validate_factor(tree);
3277 break;
3278 case power:
3279 res = validate_power(tree);
3280 break;
3281 case atom:
3282 res = validate_atom(tree);
3283 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003284
Fred Drakeff9ea482000-04-19 13:54:15 +00003285 default:
3286 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00003287 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003288 res = 0;
3289 break;
3290 }
3291 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003292 }
3293 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003294}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003295
3296
Guido van Rossum47478871996-08-21 14:32:37 +00003297static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003298validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003299{
3300 int res = validate_eval_input(tree);
3301
3302 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003303 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003304
3305 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003306}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003307
3308
Guido van Rossum3d602e31996-07-21 02:33:56 +00003309/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003310 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003311 */
Guido van Rossum47478871996-08-21 14:32:37 +00003312static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003313validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003314{
Fred Drakec2683dd2001-07-17 19:32:05 +00003315 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003316 int nch = NCH(tree) - 1;
3317 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003318 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003319
Fred Drakec2683dd2001-07-17 19:32:05 +00003320 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003321 if (TYPE(CHILD(tree, j)) == stmt)
3322 res = validate_stmt(CHILD(tree, j));
3323 else
3324 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003325 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003326 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003327 * user. Hopefully, this won't be needed. If a user reports getting
3328 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003329 */
3330 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003331 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003332
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003333 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003334}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003335
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003336static int
3337validate_encoding_decl(node *tree)
3338{
3339 int nch = NCH(tree);
3340 int res = ((nch == 1)
3341 && validate_file_input(CHILD(tree, 0)));
3342
3343 if (!res && !PyErr_Occurred())
3344 err_string("Error Parsing encoding_decl");
3345
3346 return res;
3347}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003348
Fred Drake43f8f9b1998-04-13 16:25:46 +00003349static PyObject*
3350pickle_constructor = NULL;
3351
3352
3353static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003354parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003355{
Fred Drake268397f1998-04-29 14:16:32 +00003356 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003357 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003358 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003359 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003360
Fred Drakec2683dd2001-07-17 19:32:05 +00003361 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003362 PyObject *newargs;
3363 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003364
Fred Drake2a6875e1999-09-20 22:32:18 +00003365 if ((empty_dict = PyDict_New()) == NULL)
3366 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003367 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003368 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003369 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003370 if (tuple != NULL) {
3371 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3372 Py_DECREF(tuple);
3373 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003374 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003375 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003376 }
3377 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003378 Py_XDECREF(empty_dict);
3379
Fred Drake43f8f9b1998-04-13 16:25:46 +00003380 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003381}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003382
3383
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003384/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003385 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003386 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003387 * We'd really have to write a wrapper around it all anyway to allow
3388 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003389 */
3390static PyMethodDef parser_functions[] = {
Georg Brandlf9efabb2008-07-23 15:16:45 +00003391 {"ast2tuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003392 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Georg Brandlf9efabb2008-07-23 15:16:45 +00003393 {"ast2list", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003394 PyDoc_STR("Creates a list-tree representation of an ST.")},
Georg Brandlf9efabb2008-07-23 15:16:45 +00003395 {"compileast", (PyCFunction)parser_compileast,PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003396 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003397 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003398 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003399 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003400 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003401 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003402 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003403 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003404 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003405 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003406 PyDoc_STR("Creates an ST object from a suite.")},
Georg Brandlf9efabb2008-07-23 15:16:45 +00003407 {"sequence2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003408 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003409 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003410 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003411 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003412 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003413 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003414 PyDoc_STR("Creates a list-tree representation of an ST.")},
Georg Brandlf9efabb2008-07-23 15:16:45 +00003415 {"tuple2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003416 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003417 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003418 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003419
Fred Drake43f8f9b1998-04-13 16:25:46 +00003420 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003421 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003422 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003423
Fred Drake268397f1998-04-29 14:16:32 +00003424 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003425 };
3426
3427
Mark Hammond62b1ab12002-07-23 06:31:15 +00003428PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003429
Mark Hammond62b1ab12002-07-23 06:31:15 +00003430PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00003431initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003432{
Fred Drake13130bc2001-08-15 16:44:56 +00003433 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003434
Christian Heimese93237d2007-12-19 02:37:44 +00003435 Py_TYPE(&PyST_Type) = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00003436 module = Py_InitModule("parser", parser_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003437 if (module == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003438 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003439
Fred Drake7a15ba51999-09-09 14:21:52 +00003440 if (parser_error == 0)
3441 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003442
Tim Peters6a627252003-07-21 14:25:23 +00003443 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00003444 /* caller will check PyErr_Occurred() */
3445 return;
Tim Peters6a627252003-07-21 14:25:23 +00003446 /* CAUTION: The code next used to skip bumping the refcount on
3447 * parser_error. That's a disaster if initparser() gets called more
3448 * than once. By incref'ing, we ensure that each module dict that
3449 * gets created owns its reference to the shared parser_error object,
3450 * and the file static parser_error vrbl owns a reference too.
3451 */
3452 Py_INCREF(parser_error);
3453 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3454 return;
3455
Fred Drakec2683dd2001-07-17 19:32:05 +00003456 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003457 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00003458 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003459 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003460
Fred Drake13130bc2001-08-15 16:44:56 +00003461 PyModule_AddStringConstant(module, "__copyright__",
3462 parser_copyright_string);
3463 PyModule_AddStringConstant(module, "__doc__",
3464 parser_doc_string);
3465 PyModule_AddStringConstant(module, "__version__",
3466 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003467
Fred Drake78bdb9b2001-07-19 20:17:15 +00003468 /* Register to support pickling.
3469 * If this fails, the import of this module will fail because an
3470 * exception will be raised here; should we clear the exception?
3471 */
Georg Brandldffbf5f2008-05-20 07:49:57 +00003472 copyreg = PyImport_ImportModuleNoBlock("copy_reg");
Fred Drake13130bc2001-08-15 16:44:56 +00003473 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003474 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003475
Fred Drake13130bc2001-08-15 16:44:56 +00003476 func = PyObject_GetAttrString(copyreg, "pickle");
3477 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3478 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003479 Py_XINCREF(pickle_constructor);
3480 if ((func != NULL) && (pickle_constructor != NULL)
3481 && (pickler != NULL)) {
3482 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003483
Georg Brandl684fd0c2006-05-25 19:15:31 +00003484 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3485 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003486 Py_XDECREF(res);
3487 }
3488 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003489 Py_XDECREF(pickle_constructor);
3490 Py_XDECREF(pickler);
3491 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003492 }
Fred Drakeff9ea482000-04-19 13:54:15 +00003493}