blob: 462cb74fa8e27756cb294bb5b029574973860b97 [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) {
353 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
354 }
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000355 if (col_option != NULL) {
356 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
357 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000358 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000359 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000360 * since it's known to work already.
361 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000362 res = node2tuple(((PyST_Object*)self)->st_node,
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000363 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000364 }
365 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000366}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000367
Georg Brandlf9efabb2008-07-23 15:16:45 +0000368static PyObject*
369parser_ast2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
370{
371 if (PyErr_WarnPy3k("ast2tuple is removed in 3.x; use st2tuple", 1) < 0)
372 return NULL;
373 return parser_st2tuple(self, args, kw);
374}
375
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000376
Fred Drakec2683dd2001-07-17 19:32:05 +0000377/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000378 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000379 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000380 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000381 *
382 */
383static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000384parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000385{
Guido van Rossum47478871996-08-21 14:32:37 +0000386 PyObject *line_option = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000387 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000388 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000389 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000390
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000391 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000392
Fred Drake503d8d61998-04-13 18:45:18 +0000393 if (self == NULL)
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000394 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
395 &PyST_Type, &self, &line_option,
396 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000397 else
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000398 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
399 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000400 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000401 int lineno = 0;
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000402 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000403 if (line_option != 0) {
404 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
405 }
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000406 if (col_option != NULL) {
407 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
408 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000409 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000410 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000411 * since it's known to work already.
412 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000413 res = node2tuple(self->st_node,
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000414 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000415 }
416 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000417}
Guido van Rossum47478871996-08-21 14:32:37 +0000418
Georg Brandlf9efabb2008-07-23 15:16:45 +0000419static PyObject*
420parser_ast2list(PyST_Object *self, PyObject *args, PyObject *kw)
421{
422 if (PyErr_WarnPy3k("ast2list is removed in 3.x; use st2list", 1) < 0)
423 return NULL;
424 return parser_st2list(self, args, kw);
425}
426
Guido van Rossum47478871996-08-21 14:32:37 +0000427
Fred Drakec2683dd2001-07-17 19:32:05 +0000428/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000429 *
430 * This function creates code objects from the parse tree represented by
431 * the passed-in data object. An optional file name is passed in as well.
432 *
433 */
434static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000435parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000436{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000437 PyObject* res = 0;
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000438 PyArena* arena;
439 mod_ty mod;
Fred Drakec2683dd2001-07-17 19:32:05 +0000440 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000441 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000442
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000443 static char *keywords[] = {"ast", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000444
Fred Drake503d8d61998-04-13 18:45:18 +0000445 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000446 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
447 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000448 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000449 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000450 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000451
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000452 if (ok) {
453 arena = PyArena_New();
454 if (arena) {
455 mod = PyAST_FromNode(self->st_node, &(self->st_flags), str, arena);
456 if (mod) {
457 res = (PyObject *)PyAST_Compile(mod, str, &(self->st_flags), arena);
458 }
459 PyArena_Free(arena);
460 }
461 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000462
463 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000464}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000465
Georg Brandlf9efabb2008-07-23 15:16:45 +0000466static PyObject*
467parser_compileast(PyST_Object *self, PyObject *args, PyObject *kw)
468{
469 if (PyErr_WarnPy3k("compileast is removed in 3.x; use compilest", 1) < 0)
470 return NULL;
471 return parser_compilest(self, args, kw);
472}
473
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000474
475/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
476 * PyObject* parser_issuite(PyObject* self, PyObject* args)
477 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000478 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000479 * a statement suite, respectively. The return is a Python truth value.
480 *
481 */
482static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000483parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000484{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000485 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000486 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000487
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000488 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000489
Fred Drake503d8d61998-04-13 18:45:18 +0000490 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000491 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000492 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000493 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000494 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000495
496 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000497 /* Check to see if the ST represents an expression or not. */
498 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000499 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000500 }
501 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000502}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000503
504
505static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000506parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000507{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000508 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000509 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000510
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000511 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000512
Fred Drake503d8d61998-04-13 18:45:18 +0000513 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000514 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000515 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000516 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000517 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000518
519 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000520 /* Check to see if the ST represents an expression or not. */
521 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000522 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000523 }
524 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000525}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000526
527
Fred Drake503d8d61998-04-13 18:45:18 +0000528static PyObject*
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000529parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000530{
Fred Drake503d8d61998-04-13 18:45:18 +0000531 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000532}
Fred Drake503d8d61998-04-13 18:45:18 +0000533
534
Guido van Rossum3d602e31996-07-21 02:33:56 +0000535/* err_string(char* message)
536 *
537 * Sets the error string for an exception of type ParserError.
538 *
539 */
540static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000541err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000542{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000543 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000544}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000545
546
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000547/* PyObject* parser_do_parse(PyObject* args, int type)
548 *
549 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000550 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000551 *
552 */
553static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000554parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000555{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000556 char* string = 0;
557 PyObject* res = 0;
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000558 int flags = 0;
559 perrdetail err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000560
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000561 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000562
563 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000564 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
565 &_PyParser_Grammar,
566 (type == PyST_EXPR)
567 ? eval_input : file_input,
568 &err, &flags);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000569
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000570 if (n) {
571 res = parser_newstobject(n, type);
Benjamin Petersondcee09d2008-10-31 02:16:05 +0000572 if (res)
573 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
574 }
575 else
576 PyParser_SetError(&err);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000577 }
578 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000579}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000580
581
582/* PyObject* parser_expr(PyObject* self, PyObject* args)
583 * PyObject* parser_suite(PyObject* self, PyObject* args)
584 *
585 * External interfaces to the parser itself. Which is called determines if
586 * the parser attempts to recognize an expression ('eval' form) or statement
587 * suite ('exec' form). The real work is done by parser_do_parse() above.
588 *
589 */
590static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000591parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000592{
Fred Drake268397f1998-04-29 14:16:32 +0000593 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000594 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000595}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000596
597
598static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000599parser_suite(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:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000603}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000604
605
606
Fred Drakec2683dd2001-07-17 19:32:05 +0000607/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000608 * object requires that the input tuple be valid without having to rely on
609 * catching an exception from the compiler. This is done to allow the
610 * compiler itself to remain fast, since most of its input will come from
611 * the parser directly, and therefore be known to be syntactically correct.
612 * This validation is done to ensure that we don't core dump the compile
613 * phase, returning an exception instead.
614 *
615 * Two aspects can be broken out in this code: creating a node tree from
616 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000617 * advantageous to expand the number of ST types to include funcdefs and
618 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000619 * here. They are not necessary, and not quite as useful in a raw form.
620 * For now, let's get expressions and suites working reliably.
621 */
622
623
Jeremy Hylton938ace62002-07-17 16:30:39 +0000624static node* build_node_tree(PyObject *tuple);
625static int validate_expr_tree(node *tree);
626static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000627static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000628
Fred Drakec2683dd2001-07-17 19:32:05 +0000629/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000630 *
631 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000632 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000633 * tuple can be validated. It does this by checking the first code of the
634 * tuple, and, if acceptable, builds the internal representation. If this
635 * step succeeds, the internal representation is validated as fully as
636 * possible with the various validate_*() routines defined below.
637 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000638 * This function must be changed if support is to be added for PyST_FRAGMENT
639 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000640 *
641 */
642static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000643parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000644{
Fred Drake268397f1998-04-29 14:16:32 +0000645 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000646 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000647 PyObject *tuple;
648 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000649
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000650 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000651
Fred Drakec2683dd2001-07-17 19:32:05 +0000652 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000653 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000654 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000655 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000656 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000657 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000658 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000659 }
660 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000661 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000662 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000663 tree = build_node_tree(tuple);
664 if (tree != 0) {
665 int start_sym = TYPE(tree);
666 if (start_sym == eval_input) {
667 /* Might be an eval form. */
668 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000669 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000670 else
671 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000672 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000673 else if (start_sym == file_input) {
674 /* This looks like an exec form so far. */
675 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000676 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000677 else
678 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000679 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000680 else if (start_sym == encoding_decl) {
681 /* This looks like an encoding_decl so far. */
682 if (validate_encoding_decl(tree))
683 st = parser_newstobject(tree, PyST_SUITE);
684 else
685 PyNode_Free(tree);
686 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000687 else {
688 /* This is a fragment, at best. */
689 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000690 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000691 }
Guido van Rossum47478871996-08-21 14:32:37 +0000692 }
Guido van Rossum47478871996-08-21 14:32:37 +0000693 /* Make sure we throw an exception on all errors. We should never
694 * get this, but we'd do well to be sure something is done.
695 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000696 if (st == NULL && !PyErr_Occurred())
697 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000698
Fred Drakec2683dd2001-07-17 19:32:05 +0000699 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000700}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000701
Georg Brandlf9efabb2008-07-23 15:16:45 +0000702static PyObject*
703parser_tuple2ast(PyST_Object *self, PyObject *args, PyObject *kw)
704{
705 if (PyErr_WarnPy3k("tuple2ast is removed in 3.x; use tuple2st", 1) < 0)
706 return NULL;
707 return parser_tuple2st(self, args, kw);
708}
709
Jesus Cea3e3192d2012-08-03 14:25:53 +0200710static PyObject *
711parser_sizeof(PyST_Object *st, void *unused)
712{
713 Py_ssize_t res;
714
715 res = sizeof(PyST_Object) + _PyNode_SizeOf(st->st_node);
716 return PyLong_FromSsize_t(res);
717}
718
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000719
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000720/* node* build_node_children()
721 *
722 * Iterate across the children of the current non-terminal node and build
723 * their structures. If successful, return the root of this portion of
724 * the tree, otherwise, 0. Any required exception will be specified already,
725 * and no memory will have been deallocated.
726 *
727 */
728static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000729build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000730{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000731 Py_ssize_t len = PyObject_Size(tuple);
732 Py_ssize_t i;
733 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000734
735 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000736 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000737 PyObject* elem = PySequence_GetItem(tuple, i);
738 int ok = elem != NULL;
739 long type = 0;
740 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000741
Fred Drakeff9ea482000-04-19 13:54:15 +0000742 if (ok)
743 ok = PySequence_Check(elem);
744 if (ok) {
745 PyObject *temp = PySequence_GetItem(elem, 0);
746 if (temp == NULL)
747 ok = 0;
748 else {
749 ok = PyInt_Check(temp);
750 if (ok)
751 type = PyInt_AS_LONG(temp);
752 Py_DECREF(temp);
753 }
754 }
755 if (!ok) {
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000756 PyObject *err = Py_BuildValue("os", elem,
757 "Illegal node construct.");
758 PyErr_SetObject(parser_error, err);
759 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000760 Py_XDECREF(elem);
761 return (0);
762 }
763 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000764 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000765 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000766
Fred Drake0ac9b072000-09-12 21:58:06 +0000767 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000768 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000769 return 0;
770 }
771 temp = PySequence_GetItem(elem, 1);
772 if (temp == NULL)
773 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000774 if (!PyString_Check(temp)) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000775 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000776 "second item in terminal node must be a string,"
777 " found %s",
Christian Heimese93237d2007-12-19 02:37:44 +0000778 Py_TYPE(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000779 Py_DECREF(temp);
Fred Drake0ac9b072000-09-12 21:58:06 +0000780 return 0;
781 }
782 if (len == 3) {
783 PyObject *o = PySequence_GetItem(elem, 2);
784 if (o != NULL) {
785 if (PyInt_Check(o))
786 *line_num = PyInt_AS_LONG(o);
787 else {
788 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000789 "third item in terminal node must be an"
790 " integer, found %s",
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000791 Py_TYPE(temp)->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000792 Py_DECREF(o);
793 Py_DECREF(temp);
794 return 0;
795 }
796 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000797 }
798 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000799 len = PyString_GET_SIZE(temp) + 1;
Tim Petersc9d78aa2006-03-26 23:27:58 +0000800 strn = (char *)PyObject_MALLOC(len);
Fred Drake0ac9b072000-09-12 21:58:06 +0000801 if (strn != NULL)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000802 (void) memcpy(strn, PyString_AS_STRING(temp), len);
Fred Drake0ac9b072000-09-12 21:58:06 +0000803 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000804 }
805 else if (!ISNONTERMINAL(type)) {
806 /*
807 * It has to be one or the other; this is an error.
808 * Throw an exception.
809 */
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000810 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
811 PyErr_SetObject(parser_error, err);
812 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000813 Py_XDECREF(elem);
814 return (0);
815 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000816 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000817 if (err == E_NOMEM) {
Tim Petersc9d78aa2006-03-26 23:27:58 +0000818 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000819 return (node *) PyErr_NoMemory();
820 }
821 if (err == E_OVERFLOW) {
Tim Petersc9d78aa2006-03-26 23:27:58 +0000822 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000823 PyErr_SetString(PyExc_ValueError,
824 "unsupported number of child nodes");
825 return NULL;
826 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000827
Fred Drakeff9ea482000-04-19 13:54:15 +0000828 if (ISNONTERMINAL(type)) {
829 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000830
Fred Drakeff9ea482000-04-19 13:54:15 +0000831 if (new_child != build_node_children(elem, new_child, line_num)) {
832 Py_XDECREF(elem);
833 return (0);
834 }
835 }
836 else if (type == NEWLINE) { /* It's true: we increment the */
837 ++(*line_num); /* line number *after* the newline! */
838 }
839 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000840 }
Tim Petersc9d78aa2006-03-26 23:27:58 +0000841 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000842}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000843
844
845static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000846build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000847{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000848 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000849 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000850 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000851
Guido van Rossum47478871996-08-21 14:32:37 +0000852 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000853 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000854 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000855 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000856 /*
857 * The tuple is simple, but it doesn't start with a start symbol.
858 * Throw an exception now and be done with it.
859 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000860 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000861 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000862 PyErr_SetObject(parser_error, tuple);
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000863 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000864 }
865 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000866 /*
867 * Not efficient, but that can be handled later.
868 */
869 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000870 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000871
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000872 if (num == encoding_decl) {
873 encoding = PySequence_GetItem(tuple, 2);
874 /* tuple isn't borrowed anymore here, need to DECREF */
875 tuple = PySequence_GetSlice(tuple, 0, 2);
876 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000877 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000878 if (res != NULL) {
879 if (res != build_node_children(tuple, res, &line_num)) {
880 PyNode_Free(res);
881 res = NULL;
882 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000883 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000884 Py_ssize_t len;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000885 len = PyString_GET_SIZE(encoding) + 1;
Tim Petersc9d78aa2006-03-26 23:27:58 +0000886 res->n_str = (char *)PyObject_MALLOC(len);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000887 if (res->n_str != NULL)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000888 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000889 Py_DECREF(encoding);
890 Py_DECREF(tuple);
891 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000892 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000893 }
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000894 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000895 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000896 * NONTERMINAL, we can't use it. Not sure the implementation
897 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000898 */
Neal Norwitzd1e0ef62006-03-20 04:08:12 +0000899 PyObject *err = Py_BuildValue("os", tuple,
900 "Illegal component tuple.");
901 PyErr_SetObject(parser_error, err);
902 Py_XDECREF(err);
903 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000904
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000905 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000906}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000907
908
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000909/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000910 * Validation routines used within the validation section:
911 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000912static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000913
Fred Drakeff9ea482000-04-19 13:54:15 +0000914#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
915#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
916#define validate_colon(ch) validate_terminal(ch, COLON, ":")
917#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
918#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
919#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
920#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
921#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
922#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
923#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
924#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
925#define validate_star(ch) validate_terminal(ch, STAR, "*")
926#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
927#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
928#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000929#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000930#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000931
Fred Drake0ac9b072000-09-12 21:58:06 +0000932#define VALIDATER(n) static int validate_##n(node *tree)
933
Fred Drakeff9ea482000-04-19 13:54:15 +0000934VALIDATER(node); VALIDATER(small_stmt);
935VALIDATER(class); VALIDATER(node);
936VALIDATER(parameters); VALIDATER(suite);
937VALIDATER(testlist); VALIDATER(varargslist);
938VALIDATER(fpdef); VALIDATER(fplist);
939VALIDATER(stmt); VALIDATER(simple_stmt);
940VALIDATER(expr_stmt); VALIDATER(power);
941VALIDATER(print_stmt); VALIDATER(del_stmt);
Fred Drakecff283c2000-08-21 22:24:43 +0000942VALIDATER(return_stmt); VALIDATER(list_iter);
Fred Drakeff9ea482000-04-19 13:54:15 +0000943VALIDATER(raise_stmt); VALIDATER(import_stmt);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000944VALIDATER(import_name); VALIDATER(import_from);
Fred Drakecff283c2000-08-21 22:24:43 +0000945VALIDATER(global_stmt); VALIDATER(list_if);
946VALIDATER(assert_stmt); VALIDATER(list_for);
Fred Drakeff9ea482000-04-19 13:54:15 +0000947VALIDATER(exec_stmt); VALIDATER(compound_stmt);
948VALIDATER(while); VALIDATER(for);
949VALIDATER(try); VALIDATER(except_clause);
950VALIDATER(test); VALIDATER(and_test);
951VALIDATER(not_test); VALIDATER(comparison);
952VALIDATER(comp_op); VALIDATER(expr);
953VALIDATER(xor_expr); VALIDATER(and_expr);
954VALIDATER(shift_expr); VALIDATER(arith_expr);
955VALIDATER(term); VALIDATER(factor);
956VALIDATER(atom); VALIDATER(lambdef);
957VALIDATER(trailer); VALIDATER(subscript);
958VALIDATER(subscriptlist); VALIDATER(sliceop);
Alexandre Vassalottiee936a22010-01-09 23:35:54 +0000959VALIDATER(exprlist); VALIDATER(dictorsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000960VALIDATER(arglist); VALIDATER(argument);
Fred Drake02126f22001-07-17 02:59:15 +0000961VALIDATER(listmaker); VALIDATER(yield_stmt);
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000962VALIDATER(testlist1); VALIDATER(comp_for);
963VALIDATER(comp_iter); VALIDATER(comp_if);
964VALIDATER(testlist_comp); VALIDATER(yield_expr);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000965VALIDATER(yield_or_testlist); VALIDATER(or_test);
966VALIDATER(old_test); VALIDATER(old_lambdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000967
Fred Drake0ac9b072000-09-12 21:58:06 +0000968#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000969
Fred Drakeff9ea482000-04-19 13:54:15 +0000970#define is_even(n) (((n) & 1) == 0)
971#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000972
973
974static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000975validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000976{
Fred Drake0ac9b072000-09-12 21:58:06 +0000977 if (TYPE(n) != t) {
978 PyErr_Format(parser_error, "Expected node type %d, got %d.",
979 t, TYPE(n));
980 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000981 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000982 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000983}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000984
985
Fred Drakee7ab64e2000-04-25 04:14:46 +0000986/* Verifies that the number of child nodes is exactly 'num', raising
987 * an exception if it isn't. The exception message does not indicate
988 * the exact number of nodes, allowing this to be used to raise the
989 * "right" exception when the wrong number of nodes is present in a
990 * specific variant of a statement's syntax. This is commonly used
991 * in that fashion.
992 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000993static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000994validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000995{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000996 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000997 PyErr_Format(parser_error,
998 "Illegal number of children for %s node.", name);
999 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001000 }
Fred Drake0ac9b072000-09-12 21:58:06 +00001001 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +00001002}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001003
1004
1005static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001006validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +00001007{
Guido van Rossum3d602e31996-07-21 02:33:56 +00001008 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +00001009 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001010
1011 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001012 PyErr_Format(parser_error,
1013 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001014 }
1015 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001016}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001017
1018
Guido van Rossum47478871996-08-21 14:32:37 +00001019/* X (',' X) [',']
1020 */
1021static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +00001022validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +00001023 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +00001024{
1025 int nch = NCH(tree);
1026 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +00001027 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +00001028
1029 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001030 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +00001031 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001032 if (is_even(nch))
1033 res = validate_comma(CHILD(tree, --nch));
1034 if (res && nch > 1) {
1035 int pos = 1;
1036 for ( ; res && pos < nch; pos += 2)
1037 res = (validate_comma(CHILD(tree, pos))
1038 && vfunc(CHILD(tree, pos + 1)));
1039 }
Guido van Rossum47478871996-08-21 14:32:37 +00001040 }
1041 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001042}
Guido van Rossum47478871996-08-21 14:32:37 +00001043
1044
Fred Drakecff283c2000-08-21 22:24:43 +00001045/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001046 *
1047 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001048 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00001049 */
Guido van Rossum47478871996-08-21 14:32:37 +00001050static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001051validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001052{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001053 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +00001054 int res = (validate_ntype(tree, classdef) &&
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001055 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001056
Guido van Rossum3d602e31996-07-21 02:33:56 +00001057 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001058 res = (validate_name(CHILD(tree, 0), "class")
1059 && validate_ntype(CHILD(tree, 1), NAME)
1060 && validate_colon(CHILD(tree, nch - 2))
1061 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001062 }
Brett Cannonf4189912005-04-09 02:30:16 +00001063 else {
Fred Drakeff9ea482000-04-19 13:54:15 +00001064 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +00001065 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001066
Brett Cannonf4189912005-04-09 02:30:16 +00001067 if (res) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001068 if (nch == 7) {
1069 res = ((validate_lparen(CHILD(tree, 2)) &&
1070 validate_testlist(CHILD(tree, 3)) &&
1071 validate_rparen(CHILD(tree, 4))));
1072 }
1073 else if (nch == 6) {
1074 res = (validate_lparen(CHILD(tree,2)) &&
1075 validate_rparen(CHILD(tree,3)));
1076 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001077 }
1078 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001079}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001080
1081
Guido van Rossum3d602e31996-07-21 02:33:56 +00001082/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001083 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001084 */
Guido van Rossum47478871996-08-21 14:32:37 +00001085static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001086validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001087{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001088 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001089 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001090 && (nch >= 4)
1091 && validate_name(CHILD(tree, 0), "if")
1092 && validate_test(CHILD(tree, 1))
1093 && validate_colon(CHILD(tree, 2))
1094 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001095
1096 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001097 /* ... 'else' ':' suite */
1098 res = (validate_name(CHILD(tree, nch - 3), "else")
1099 && validate_colon(CHILD(tree, nch - 2))
1100 && validate_suite(CHILD(tree, nch - 1)));
1101 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001102 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001103 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001104 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001105 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001106 /* Will catch the case for nch < 4 */
1107 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001108 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001109 /* ... ('elif' test ':' suite)+ ... */
1110 int j = 4;
1111 while ((j < nch) && res) {
1112 res = (validate_name(CHILD(tree, j), "elif")
1113 && validate_colon(CHILD(tree, j + 2))
1114 && validate_test(CHILD(tree, j + 1))
1115 && validate_suite(CHILD(tree, j + 3)));
1116 j += 4;
1117 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001118 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001119 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001120}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001121
1122
Guido van Rossum3d602e31996-07-21 02:33:56 +00001123/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001124 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001125 *
1126 */
Guido van Rossum47478871996-08-21 14:32:37 +00001127static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001128validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001129{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001130 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001131 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001132
Guido van Rossum3d602e31996-07-21 02:33:56 +00001133 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001134 res = (validate_lparen(CHILD(tree, 0))
1135 && validate_rparen(CHILD(tree, nch - 1)));
1136 if (res && (nch == 3))
1137 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001138 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001139 else {
1140 (void) validate_numnodes(tree, 2, "parameters");
1141 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001142 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001143}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001144
1145
Fred Drakecff283c2000-08-21 22:24:43 +00001146/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001147 *
1148 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001149 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001150 * | NEWLINE INDENT stmt+ DEDENT
1151 */
Guido van Rossum47478871996-08-21 14:32:37 +00001152static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001153validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001154{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001155 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001156 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001157
Guido van Rossum3d602e31996-07-21 02:33:56 +00001158 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001159 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001160 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001161 /* NEWLINE INDENT stmt+ DEDENT */
1162 res = (validate_newline(CHILD(tree, 0))
1163 && validate_indent(CHILD(tree, 1))
1164 && validate_stmt(CHILD(tree, 2))
1165 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001166
Fred Drakeff9ea482000-04-19 13:54:15 +00001167 if (res && (nch > 4)) {
1168 int i = 3;
1169 --nch; /* forget the DEDENT */
1170 for ( ; res && (i < nch); ++i)
1171 res = validate_stmt(CHILD(tree, i));
1172 }
1173 else if (nch < 4)
1174 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001175 }
1176 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001177}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001178
1179
Guido van Rossum47478871996-08-21 14:32:37 +00001180static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001181validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001182{
Guido van Rossum47478871996-08-21 14:32:37 +00001183 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001184 validate_test, "testlist"));
1185}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001186
1187
Guido van Rossum1c917072001-10-15 15:44:05 +00001188static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001189validate_testlist1(node *tree)
1190{
1191 return (validate_repeating_list(tree, testlist1,
1192 validate_test, "testlist1"));
1193}
1194
1195
1196static int
Guido van Rossum1c917072001-10-15 15:44:05 +00001197validate_testlist_safe(node *tree)
1198{
1199 return (validate_repeating_list(tree, testlist_safe,
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001200 validate_old_test, "testlist_safe"));
Guido van Rossum1c917072001-10-15 15:44:05 +00001201}
1202
1203
Fred Drakecff283c2000-08-21 22:24:43 +00001204/* '*' NAME [',' '**' NAME] | '**' NAME
1205 */
1206static int
1207validate_varargslist_trailer(node *tree, int start)
1208{
1209 int nch = NCH(tree);
1210 int res = 0;
1211 int sym;
1212
1213 if (nch <= start) {
1214 err_string("expected variable argument trailer for varargslist");
1215 return 0;
1216 }
1217 sym = TYPE(CHILD(tree, start));
1218 if (sym == STAR) {
1219 /*
1220 * ('*' NAME [',' '**' NAME]
1221 */
1222 if (nch-start == 2)
1223 res = validate_name(CHILD(tree, start+1), NULL);
1224 else if (nch-start == 5)
1225 res = (validate_name(CHILD(tree, start+1), NULL)
1226 && validate_comma(CHILD(tree, start+2))
1227 && validate_doublestar(CHILD(tree, start+3))
1228 && validate_name(CHILD(tree, start+4), NULL));
1229 }
1230 else if (sym == DOUBLESTAR) {
1231 /*
1232 * '**' NAME
1233 */
1234 if (nch-start == 2)
1235 res = validate_name(CHILD(tree, start+1), NULL);
1236 }
1237 if (!res)
1238 err_string("illegal variable argument trailer for varargslist");
1239 return res;
1240}
1241
1242
1243/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001244 *
1245 * varargslist:
Guido van Rossum3d602e31996-07-21 02:33:56 +00001246 * (fpdef ['=' test] ',')*
Fred Drakecff283c2000-08-21 22:24:43 +00001247 * ('*' NAME [',' '**' NAME]
1248 * | '**' NAME)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001249 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1250 *
1251 */
Guido van Rossum47478871996-08-21 14:32:37 +00001252static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001253validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001254{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001255 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001256 int res = validate_ntype(tree, varargslist) && (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001257 int sym;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001258
Fred Drakeb6429a22000-12-11 22:08:27 +00001259 if (!res)
1260 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001261 if (nch < 1) {
1262 err_string("varargslist missing child nodes");
1263 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001264 }
Fred Drakecff283c2000-08-21 22:24:43 +00001265 sym = TYPE(CHILD(tree, 0));
1266 if (sym == STAR || sym == DOUBLESTAR)
Fred Drakeb6429a22000-12-11 22:08:27 +00001267 /* whole thing matches:
1268 * '*' NAME [',' '**' NAME] | '**' NAME
1269 */
Fred Drakecff283c2000-08-21 22:24:43 +00001270 res = validate_varargslist_trailer(tree, 0);
1271 else if (sym == fpdef) {
1272 int i = 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001273
Fred Drakecff283c2000-08-21 22:24:43 +00001274 sym = TYPE(CHILD(tree, nch-1));
1275 if (sym == NAME) {
1276 /*
1277 * (fpdef ['=' test] ',')+
1278 * ('*' NAME [',' '**' NAME]
1279 * | '**' NAME)
1280 */
1281 /* skip over (fpdef ['=' test] ',')+ */
1282 while (res && (i+2 <= nch)) {
1283 res = validate_fpdef(CHILD(tree, i));
1284 ++i;
1285 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1286 res = (validate_equal(CHILD(tree, i))
1287 && validate_test(CHILD(tree, i+1)));
1288 if (res)
1289 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00001290 }
Fred Drakecff283c2000-08-21 22:24:43 +00001291 if (res && i < nch) {
1292 res = validate_comma(CHILD(tree, i));
Fred Drakeb6429a22000-12-11 22:08:27 +00001293 ++i;
1294 if (res && i < nch
1295 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1296 || TYPE(CHILD(tree, i)) == STAR))
1297 break;
Fred Drakecff283c2000-08-21 22:24:43 +00001298 }
1299 }
Fred Drakeb6429a22000-12-11 22:08:27 +00001300 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1301 * i --^^^
1302 */
Fred Drakecff283c2000-08-21 22:24:43 +00001303 if (res)
1304 res = validate_varargslist_trailer(tree, i);
1305 }
1306 else {
1307 /*
1308 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1309 */
Fred Drakeb6429a22000-12-11 22:08:27 +00001310 /* strip trailing comma node */
Fred Drakecff283c2000-08-21 22:24:43 +00001311 if (sym == COMMA) {
1312 res = validate_comma(CHILD(tree, nch-1));
1313 if (!res)
1314 return 0;
1315 --nch;
1316 }
1317 /*
1318 * fpdef ['=' test] (',' fpdef ['=' test])*
1319 */
1320 res = validate_fpdef(CHILD(tree, 0));
1321 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001322 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1323 res = (validate_equal(CHILD(tree, i))
1324 && validate_test(CHILD(tree, i+1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001325 i += 2;
1326 }
1327 /*
1328 * ... (',' fpdef ['=' test])*
1329 * i ---^^^
1330 */
1331 while (res && (nch - i) >= 2) {
1332 res = (validate_comma(CHILD(tree, i))
1333 && validate_fpdef(CHILD(tree, i+1)));
1334 i += 2;
Fred Drakeb6429a22000-12-11 22:08:27 +00001335 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1336 res = (validate_equal(CHILD(tree, i))
Fred Drakecff283c2000-08-21 22:24:43 +00001337 && validate_test(CHILD(tree, i+1)));
Fred Drakeb6429a22000-12-11 22:08:27 +00001338 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001339 }
1340 }
1341 if (res && nch - i != 0) {
1342 res = 0;
1343 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001344 }
1345 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001346 }
Fred Drakecff283c2000-08-21 22:24:43 +00001347 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001348}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001349
1350
Fred Drakecff283c2000-08-21 22:24:43 +00001351/* list_iter: list_for | list_if
1352 */
1353static int
1354validate_list_iter(node *tree)
1355{
1356 int res = (validate_ntype(tree, list_iter)
1357 && validate_numnodes(tree, 1, "list_iter"));
1358 if (res && TYPE(CHILD(tree, 0)) == list_for)
1359 res = validate_list_for(CHILD(tree, 0));
1360 else
1361 res = validate_list_if(CHILD(tree, 0));
1362
1363 return res;
1364}
1365
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001366/* comp_iter: comp_for | comp_if
Raymond Hettinger354433a2004-05-19 08:20:33 +00001367 */
1368static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001369validate_comp_iter(node *tree)
Raymond Hettinger354433a2004-05-19 08:20:33 +00001370{
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001371 int res = (validate_ntype(tree, comp_iter)
1372 && validate_numnodes(tree, 1, "comp_iter"));
1373 if (res && TYPE(CHILD(tree, 0)) == comp_for)
1374 res = validate_comp_for(CHILD(tree, 0));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001375 else
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001376 res = validate_comp_if(CHILD(tree, 0));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001377
1378 return res;
1379}
1380
Fred Drakecff283c2000-08-21 22:24:43 +00001381/* list_for: 'for' exprlist 'in' testlist [list_iter]
1382 */
1383static int
1384validate_list_for(node *tree)
1385{
1386 int nch = NCH(tree);
1387 int res;
1388
1389 if (nch == 5)
1390 res = validate_list_iter(CHILD(tree, 4));
1391 else
1392 res = validate_numnodes(tree, 4, "list_for");
1393
1394 if (res)
1395 res = (validate_name(CHILD(tree, 0), "for")
1396 && validate_exprlist(CHILD(tree, 1))
1397 && validate_name(CHILD(tree, 2), "in")
Guido van Rossum1c917072001-10-15 15:44:05 +00001398 && validate_testlist_safe(CHILD(tree, 3)));
Fred Drakecff283c2000-08-21 22:24:43 +00001399
1400 return res;
1401}
1402
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001403/* comp_for: 'for' exprlist 'in' test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001404 */
1405static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001406validate_comp_for(node *tree)
Raymond Hettinger354433a2004-05-19 08:20:33 +00001407{
1408 int nch = NCH(tree);
1409 int res;
1410
1411 if (nch == 5)
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001412 res = validate_comp_iter(CHILD(tree, 4));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001413 else
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001414 res = validate_numnodes(tree, 4, "comp_for");
Raymond Hettinger354433a2004-05-19 08:20:33 +00001415
1416 if (res)
1417 res = (validate_name(CHILD(tree, 0), "for")
1418 && validate_exprlist(CHILD(tree, 1))
1419 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001420 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001421
1422 return res;
1423}
1424
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001425/* list_if: 'if' old_test [list_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001426 */
1427static int
1428validate_list_if(node *tree)
1429{
1430 int nch = NCH(tree);
1431 int res;
1432
1433 if (nch == 3)
1434 res = validate_list_iter(CHILD(tree, 2));
1435 else
1436 res = validate_numnodes(tree, 2, "list_if");
1437
1438 if (res)
1439 res = (validate_name(CHILD(tree, 0), "if")
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001440 && validate_old_test(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001441
1442 return res;
1443}
1444
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001445/* comp_if: 'if' old_test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001446 */
1447static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001448validate_comp_if(node *tree)
Raymond Hettinger354433a2004-05-19 08:20:33 +00001449{
1450 int nch = NCH(tree);
1451 int res;
1452
1453 if (nch == 3)
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001454 res = validate_comp_iter(CHILD(tree, 2));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001455 else
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001456 res = validate_numnodes(tree, 2, "comp_if");
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001457
Raymond Hettinger354433a2004-05-19 08:20:33 +00001458 if (res)
1459 res = (validate_name(CHILD(tree, 0), "if")
Neal Norwitz4b194fa2006-04-12 05:24:39 +00001460 && validate_old_test(CHILD(tree, 1)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001461
1462 return res;
1463}
Fred Drakecff283c2000-08-21 22:24:43 +00001464
1465/* validate_fpdef()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001466 *
1467 * fpdef:
Fred Drakeff9ea482000-04-19 13:54:15 +00001468 * NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001469 * | '(' fplist ')'
1470 */
Guido van Rossum47478871996-08-21 14:32:37 +00001471static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001472validate_fpdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001473{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001474 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001475 int res = validate_ntype(tree, fpdef);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001476
Guido van Rossum3d602e31996-07-21 02:33:56 +00001477 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001478 if (nch == 1)
1479 res = validate_ntype(CHILD(tree, 0), NAME);
1480 else if (nch == 3)
1481 res = (validate_lparen(CHILD(tree, 0))
1482 && validate_fplist(CHILD(tree, 1))
1483 && validate_rparen(CHILD(tree, 2)));
1484 else
1485 res = validate_numnodes(tree, 1, "fpdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001486 }
1487 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001488}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001489
1490
Guido van Rossum47478871996-08-21 14:32:37 +00001491static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001492validate_fplist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001493{
Guido van Rossum47478871996-08-21 14:32:37 +00001494 return (validate_repeating_list(tree, fplist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001495 validate_fpdef, "fplist"));
1496}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001497
1498
Guido van Rossum3d602e31996-07-21 02:33:56 +00001499/* simple_stmt | compound_stmt
1500 *
1501 */
Guido van Rossum47478871996-08-21 14:32:37 +00001502static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001503validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001504{
1505 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001506 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001507
Guido van Rossum3d602e31996-07-21 02:33:56 +00001508 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001509 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001510
Fred Drakeff9ea482000-04-19 13:54:15 +00001511 if (TYPE(tree) == simple_stmt)
1512 res = validate_simple_stmt(tree);
1513 else
1514 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001515 }
1516 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001517}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001518
1519
Guido van Rossum3d602e31996-07-21 02:33:56 +00001520/* small_stmt (';' small_stmt)* [';'] NEWLINE
1521 *
1522 */
Guido van Rossum47478871996-08-21 14:32:37 +00001523static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001524validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001525{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001526 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001527 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001528 && (nch >= 2)
1529 && validate_small_stmt(CHILD(tree, 0))
1530 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001531
Guido van Rossum3d602e31996-07-21 02:33:56 +00001532 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001533 res = validate_numnodes(tree, 2, "simple_stmt");
1534 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001535 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001536 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001537 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001538 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001539
Fred Drakeff9ea482000-04-19 13:54:15 +00001540 for (i = 1; res && (i < nch); i += 2)
1541 res = (validate_semi(CHILD(tree, i))
1542 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001543 }
1544 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001545}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001546
1547
Guido van Rossum47478871996-08-21 14:32:37 +00001548static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001549validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001550{
1551 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001552 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001553
Fred Drake0ac9b072000-09-12 21:58:06 +00001554 if (res) {
1555 int ntype = TYPE(CHILD(tree, 0));
1556
1557 if ( (ntype == expr_stmt)
1558 || (ntype == print_stmt)
1559 || (ntype == del_stmt)
1560 || (ntype == pass_stmt)
1561 || (ntype == flow_stmt)
1562 || (ntype == import_stmt)
1563 || (ntype == global_stmt)
1564 || (ntype == assert_stmt)
1565 || (ntype == exec_stmt))
1566 res = validate_node(CHILD(tree, 0));
1567 else {
1568 res = 0;
1569 err_string("illegal small_stmt child type");
1570 }
1571 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001572 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001573 res = 0;
1574 PyErr_Format(parser_error,
1575 "Unrecognized child node of small_stmt: %d.",
1576 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001577 }
1578 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001579}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001580
1581
1582/* compound_stmt:
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00001583 * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
Guido van Rossum3d602e31996-07-21 02:33:56 +00001584 */
Guido van Rossum47478871996-08-21 14:32:37 +00001585static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001586validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001587{
1588 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001589 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001590 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001591
1592 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001593 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001594
1595 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001596 ntype = TYPE(tree);
1597 if ( (ntype == if_stmt)
1598 || (ntype == while_stmt)
1599 || (ntype == for_stmt)
1600 || (ntype == try_stmt)
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00001601 || (ntype == with_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001602 || (ntype == funcdef)
Christian Heimes5224d282008-02-23 15:01:05 +00001603 || (ntype == classdef)
1604 || (ntype == decorated))
Fred Drakeff9ea482000-04-19 13:54:15 +00001605 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001606 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001607 res = 0;
1608 PyErr_Format(parser_error,
1609 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001610 }
1611 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001612}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001613
Guido van Rossum47478871996-08-21 14:32:37 +00001614static int
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001615validate_yield_or_testlist(node *tree)
1616{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001617 if (TYPE(tree) == yield_expr)
1618 return validate_yield_expr(tree);
1619 else
1620 return validate_testlist(tree);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001621}
1622
1623static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001624validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001625{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001626 int j;
1627 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001628 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001629 && is_odd(nch)
1630 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001631
Fred Drake28f739a2000-08-25 22:42:40 +00001632 if (res && nch == 3
1633 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001634 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001635 && validate_yield_or_testlist(CHILD(tree, 2));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001636
Fred Drake28f739a2000-08-25 22:42:40 +00001637 if (res) {
1638 char *s = STR(CHILD(CHILD(tree, 1), 0));
1639
1640 res = (strcmp(s, "+=") == 0
1641 || strcmp(s, "-=") == 0
1642 || strcmp(s, "*=") == 0
1643 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001644 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001645 || strcmp(s, "%=") == 0
1646 || strcmp(s, "&=") == 0
1647 || strcmp(s, "|=") == 0
1648 || strcmp(s, "^=") == 0
1649 || strcmp(s, "<<=") == 0
1650 || strcmp(s, ">>=") == 0
1651 || strcmp(s, "**=") == 0);
1652 if (!res)
Ezio Melotti24b07bc2011-03-15 18:55:01 +02001653 err_string("illegal augmented assignment operator");
Fred Drake28f739a2000-08-25 22:42:40 +00001654 }
1655 }
1656 else {
1657 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001658 res = validate_equal(CHILD(tree, j))
1659 && validate_yield_or_testlist(CHILD(tree, j + 1));
Fred Drake28f739a2000-08-25 22:42:40 +00001660 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001661 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001662}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001663
1664
Guido van Rossum3d602e31996-07-21 02:33:56 +00001665/* print_stmt:
1666 *
Fred Drakecff283c2000-08-21 22:24:43 +00001667 * 'print' ( [ test (',' test)* [','] ]
1668 * | '>>' test [ (',' test)+ [','] ] )
Guido van Rossum3d602e31996-07-21 02:33:56 +00001669 */
Guido van Rossum47478871996-08-21 14:32:37 +00001670static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001671validate_print_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001672{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001673 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001674 int res = (validate_ntype(tree, print_stmt)
Fred Drakecff283c2000-08-21 22:24:43 +00001675 && (nch > 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001676 && validate_name(CHILD(tree, 0), "print"));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001677
Fred Drakecff283c2000-08-21 22:24:43 +00001678 if (res && nch > 1) {
1679 int sym = TYPE(CHILD(tree, 1));
1680 int i = 1;
1681 int allow_trailing_comma = 1;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001682
Fred Drakecff283c2000-08-21 22:24:43 +00001683 if (sym == test)
1684 res = validate_test(CHILD(tree, i++));
1685 else {
1686 if (nch < 3)
1687 res = validate_numnodes(tree, 3, "print_stmt");
1688 else {
1689 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1690 && validate_test(CHILD(tree, i+1)));
1691 i += 2;
1692 allow_trailing_comma = 0;
1693 }
1694 }
1695 if (res) {
1696 /* ... (',' test)* [','] */
1697 while (res && i+2 <= nch) {
1698 res = (validate_comma(CHILD(tree, i))
1699 && validate_test(CHILD(tree, i+1)));
1700 allow_trailing_comma = 1;
1701 i += 2;
1702 }
1703 if (res && !allow_trailing_comma)
1704 res = validate_numnodes(tree, i, "print_stmt");
1705 else if (res && i < nch)
1706 res = validate_comma(CHILD(tree, i));
1707 }
1708 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001709 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001710}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001711
1712
Guido van Rossum47478871996-08-21 14:32:37 +00001713static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001714validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001715{
1716 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001717 && validate_name(CHILD(tree, 0), "del")
1718 && validate_exprlist(CHILD(tree, 1)));
1719}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001720
1721
Guido van Rossum47478871996-08-21 14:32:37 +00001722static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001723validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001724{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001725 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001726 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001727 && ((nch == 1) || (nch == 2))
1728 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001729
Guido van Rossum3d602e31996-07-21 02:33:56 +00001730 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001731 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001732
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001733 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001734}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001735
1736
Guido van Rossum47478871996-08-21 14:32:37 +00001737static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001738validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001739{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001740 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001741 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001742 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001743
Guido van Rossum3d602e31996-07-21 02:33:56 +00001744 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001745 res = validate_name(CHILD(tree, 0), "raise");
1746 if (res && (nch >= 2))
1747 res = validate_test(CHILD(tree, 1));
1748 if (res && nch > 2) {
1749 res = (validate_comma(CHILD(tree, 2))
1750 && validate_test(CHILD(tree, 3)));
1751 if (res && (nch > 4))
1752 res = (validate_comma(CHILD(tree, 4))
1753 && validate_test(CHILD(tree, 5)));
1754 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001755 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001756 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001757 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001758 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001759 res = (validate_comma(CHILD(tree, 2))
1760 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001761
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001762 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001763}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001764
1765
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001766/* yield_expr: 'yield' [testlist]
1767 */
1768static int
1769validate_yield_expr(node *tree)
1770{
1771 int nch = NCH(tree);
1772 int res = (validate_ntype(tree, yield_expr)
1773 && ((nch == 1) || (nch == 2))
1774 && validate_name(CHILD(tree, 0), "yield"));
1775
1776 if (res && (nch == 2))
1777 res = validate_testlist(CHILD(tree, 1));
1778
1779 return (res);
1780}
1781
1782
1783/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001784 */
1785static int
1786validate_yield_stmt(node *tree)
1787{
1788 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001789 && validate_numnodes(tree, 1, "yield_stmt")
1790 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001791}
1792
1793
Fred Drakecff283c2000-08-21 22:24:43 +00001794static int
1795validate_import_as_name(node *tree)
1796{
1797 int nch = NCH(tree);
1798 int ok = validate_ntype(tree, import_as_name);
1799
1800 if (ok) {
1801 if (nch == 1)
1802 ok = validate_name(CHILD(tree, 0), NULL);
1803 else if (nch == 3)
1804 ok = (validate_name(CHILD(tree, 0), NULL)
1805 && validate_name(CHILD(tree, 1), "as")
1806 && validate_name(CHILD(tree, 2), NULL));
1807 else
1808 ok = validate_numnodes(tree, 3, "import_as_name");
1809 }
1810 return ok;
1811}
1812
1813
Fred Drake71137082001-01-07 05:59:59 +00001814/* dotted_name: NAME ("." NAME)*
1815 */
1816static int
1817validate_dotted_name(node *tree)
1818{
1819 int nch = NCH(tree);
1820 int res = (validate_ntype(tree, dotted_name)
1821 && is_odd(nch)
1822 && validate_name(CHILD(tree, 0), NULL));
1823 int i;
1824
1825 for (i = 1; res && (i < nch); i += 2) {
1826 res = (validate_dot(CHILD(tree, i))
1827 && validate_name(CHILD(tree, i+1), NULL));
1828 }
1829 return res;
1830}
1831
1832
Fred Drakecff283c2000-08-21 22:24:43 +00001833/* dotted_as_name: dotted_name [NAME NAME]
1834 */
1835static int
1836validate_dotted_as_name(node *tree)
1837{
1838 int nch = NCH(tree);
1839 int res = validate_ntype(tree, dotted_as_name);
1840
1841 if (res) {
1842 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001843 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001844 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001845 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001846 && validate_name(CHILD(tree, 1), "as")
1847 && validate_name(CHILD(tree, 2), NULL));
1848 else {
1849 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001850 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001851 }
1852 }
1853 return res;
1854}
1855
1856
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001857/* dotted_as_name (',' dotted_as_name)* */
1858static int
1859validate_dotted_as_names(node *tree)
1860{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001861 int nch = NCH(tree);
1862 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1863 int i;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001864
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001865 for (i = 1; res && (i < nch); i += 2)
1866 res = (validate_comma(CHILD(tree, i))
1867 && validate_dotted_as_name(CHILD(tree, i + 1)));
1868 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001869}
1870
1871
1872/* import_as_name (',' import_as_name)* [','] */
1873static int
1874validate_import_as_names(node *tree)
1875{
1876 int nch = NCH(tree);
1877 int res = validate_import_as_name(CHILD(tree, 0));
1878 int i;
1879
1880 for (i = 1; res && (i + 1 < nch); i += 2)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001881 res = (validate_comma(CHILD(tree, i))
1882 && validate_import_as_name(CHILD(tree, i + 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001883 return (res);
1884}
1885
1886
1887/* 'import' dotted_as_names */
1888static int
1889validate_import_name(node *tree)
1890{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001891 return (validate_ntype(tree, import_name)
1892 && validate_numnodes(tree, 2, "import_name")
1893 && validate_name(CHILD(tree, 0), "import")
1894 && validate_dotted_as_names(CHILD(tree, 1)));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001895}
1896
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001897/* Helper function to count the number of leading dots in
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001898 * 'from ...module import name'
1899 */
1900static int
1901count_from_dots(node *tree)
1902{
1903 int i;
Benjamin Peterson6624a9f2008-11-03 15:14:51 +00001904 for (i = 1; i < NCH(tree); i++)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001905 if (TYPE(CHILD(tree, i)) != DOT)
1906 break;
Benjamin Peterson6624a9f2008-11-03 15:14:51 +00001907 return i-1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001908}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001909
Mark Dickinson75b44b32010-07-04 16:47:56 +00001910/* import_from: ('from' ('.'* dotted_name | '.'+)
1911 * 'import' ('*' | '(' import_as_names ')' | import_as_names))
Guido van Rossum3d602e31996-07-21 02:33:56 +00001912 */
Guido van Rossum47478871996-08-21 14:32:37 +00001913static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001914validate_import_from(node *tree)
1915{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001916 int nch = NCH(tree);
1917 int ndots = count_from_dots(tree);
1918 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1919 int offset = ndots + havename;
1920 int res = validate_ntype(tree, import_from)
Mark Dickinson75b44b32010-07-04 16:47:56 +00001921 && (offset >= 1)
1922 && (nch >= 3 + offset)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001923 && validate_name(CHILD(tree, 0), "from")
1924 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1925 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001926
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001927 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1928 res = ((nch == offset + 5)
1929 && validate_lparen(CHILD(tree, offset + 2))
1930 && validate_import_as_names(CHILD(tree, offset + 3))
1931 && validate_rparen(CHILD(tree, offset + 4)));
1932 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1933 res = validate_import_as_names(CHILD(tree, offset + 2));
1934 return (res);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001935}
1936
1937
1938/* import_stmt: import_name | import_from */
1939static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001940validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001941{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001942 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001943 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001944
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001945 if (res) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001946 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001947
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001948 if (ntype == import_name || ntype == import_from)
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001949 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001950 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001951 res = 0;
1952 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001953 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001954 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001955 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001956 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001957 PyErr_Format(parser_error,
1958 "Unrecognized child node of import_stmt: %d.",
1959 TYPE(CHILD(tree, 0)));
1960 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001961 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001962}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001963
1964
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001965
1966
Guido van Rossum47478871996-08-21 14:32:37 +00001967static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001968validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001969{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001970 int j;
1971 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001972 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001973 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001974
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001975 if (!res && !PyErr_Occurred())
1976 err_string("illegal global statement");
1977
Guido van Rossum3d602e31996-07-21 02:33:56 +00001978 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001979 res = (validate_name(CHILD(tree, 0), "global")
1980 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001981 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001982 res = (validate_comma(CHILD(tree, j))
1983 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001984
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001985 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001986}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001987
1988
Guido van Rossum3d602e31996-07-21 02:33:56 +00001989/* exec_stmt:
1990 *
1991 * 'exec' expr ['in' test [',' test]]
1992 */
Guido van Rossum47478871996-08-21 14:32:37 +00001993static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001994validate_exec_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001995{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001996 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001997 int res = (validate_ntype(tree, exec_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001998 && ((nch == 2) || (nch == 4) || (nch == 6))
1999 && validate_name(CHILD(tree, 0), "exec")
2000 && validate_expr(CHILD(tree, 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002001
Guido van Rossum3d602e31996-07-21 02:33:56 +00002002 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002003 err_string("illegal exec statement");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002004 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002005 res = (validate_name(CHILD(tree, 2), "in")
2006 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002007 if (res && (nch == 6))
Fred Drakeff9ea482000-04-19 13:54:15 +00002008 res = (validate_comma(CHILD(tree, 4))
2009 && validate_test(CHILD(tree, 5)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002010
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002011 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002012}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002013
2014
Guido van Rossum925e5471997-04-02 05:32:13 +00002015/* assert_stmt:
2016 *
2017 * 'assert' test [',' test]
2018 */
2019static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002020validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00002021{
2022 int nch = NCH(tree);
2023 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002024 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00002025 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002026 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00002027
2028 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002029 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00002030 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002031 res = (validate_comma(CHILD(tree, 2))
2032 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00002033
2034 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002035}
Guido van Rossum925e5471997-04-02 05:32:13 +00002036
2037
Guido van Rossum47478871996-08-21 14:32:37 +00002038static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002039validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002040{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002041 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002042 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002043 && ((nch == 4) || (nch == 7))
2044 && validate_name(CHILD(tree, 0), "while")
2045 && validate_test(CHILD(tree, 1))
2046 && validate_colon(CHILD(tree, 2))
2047 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002048
Guido van Rossum3d602e31996-07-21 02:33:56 +00002049 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00002050 res = (validate_name(CHILD(tree, 4), "else")
2051 && validate_colon(CHILD(tree, 5))
2052 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002053
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002054 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002055}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002056
2057
Guido van Rossum47478871996-08-21 14:32:37 +00002058static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002059validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002060{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002061 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002062 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002063 && ((nch == 6) || (nch == 9))
2064 && validate_name(CHILD(tree, 0), "for")
2065 && validate_exprlist(CHILD(tree, 1))
2066 && validate_name(CHILD(tree, 2), "in")
2067 && validate_testlist(CHILD(tree, 3))
2068 && validate_colon(CHILD(tree, 4))
2069 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002070
Guido van Rossum3d602e31996-07-21 02:33:56 +00002071 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00002072 res = (validate_name(CHILD(tree, 6), "else")
2073 && validate_colon(CHILD(tree, 7))
2074 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002075
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002076 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002077}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002078
2079
Guido van Rossum3d602e31996-07-21 02:33:56 +00002080/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00002081 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Georg Brandlfe879e82008-12-05 12:09:41 +00002082 ['finally' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002083 * | 'try' ':' suite 'finally' ':' suite
2084 *
2085 */
Guido van Rossum47478871996-08-21 14:32:37 +00002086static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002087validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002088{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002089 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002090 int pos = 3;
2091 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002092 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002093
2094 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002095 res = (validate_name(CHILD(tree, 0), "try")
2096 && validate_colon(CHILD(tree, 1))
2097 && validate_suite(CHILD(tree, 2))
2098 && validate_colon(CHILD(tree, nch - 2))
2099 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00002100 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00002101 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00002102 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
2103 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00002104
2105 PyErr_Format(parser_error,
2106 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002107 }
Georg Brandlfe879e82008-12-05 12:09:41 +00002108 /* Handle try/finally statement */
2109 if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
2110 (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
2111 res = (validate_numnodes(tree, 6, "try/finally")
2112 && validate_colon(CHILD(tree, 4))
2113 && validate_suite(CHILD(tree, 5)));
2114 return (res);
2115 }
2116 /* try/except statement: skip past except_clause sections */
Antoine Pitrou42b5bcf2009-05-14 21:48:09 +00002117 while (res && pos < nch && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002118 res = (validate_except_clause(CHILD(tree, pos))
2119 && validate_colon(CHILD(tree, pos + 1))
2120 && validate_suite(CHILD(tree, pos + 2)));
2121 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002122 }
Georg Brandlfe879e82008-12-05 12:09:41 +00002123 /* skip else clause */
Antoine Pitrou42b5bcf2009-05-14 21:48:09 +00002124 if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) &&
Georg Brandlfe879e82008-12-05 12:09:41 +00002125 (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
2126 res = (validate_colon(CHILD(tree, pos + 1))
2127 && validate_suite(CHILD(tree, pos + 2)));
2128 pos += 3;
2129 }
2130 if (res && pos < nch) {
2131 /* last clause must be a finally */
2132 res = (validate_name(CHILD(tree, pos), "finally")
2133 && validate_numnodes(tree, pos + 3, "try/except/finally")
2134 && validate_colon(CHILD(tree, pos + 1))
2135 && validate_suite(CHILD(tree, pos + 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002136 }
2137 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002138}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002139
2140
Guido van Rossum47478871996-08-21 14:32:37 +00002141static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002142validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002143{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002144 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002145 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00002146 && ((nch == 1) || (nch == 2) || (nch == 4))
2147 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002148
Guido van Rossum3d602e31996-07-21 02:33:56 +00002149 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002150 res = validate_test(CHILD(tree, 1));
Mark Dickinson070f0ab2010-06-30 16:27:57 +00002151 if (res && (nch == 4)) {
2152 if (TYPE(CHILD(tree, 2)) == NAME)
2153 res = validate_name(CHILD(tree, 2), "as");
2154 else
2155 res = validate_comma(CHILD(tree, 2));
2156 res = res && validate_test(CHILD(tree, 3));
2157 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002158 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002159}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002160
2161
Guido van Rossum47478871996-08-21 14:32:37 +00002162static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002163validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002164{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002165 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002166 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002167
Guido van Rossum3d602e31996-07-21 02:33:56 +00002168 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00002169 res = ((nch == 1)
2170 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002171 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002172 res = validate_or_test(CHILD(tree, 0));
2173 res = (res && (nch == 1 || (nch == 5 &&
2174 validate_name(CHILD(tree, 1), "if") &&
2175 validate_or_test(CHILD(tree, 2)) &&
2176 validate_name(CHILD(tree, 3), "else") &&
2177 validate_test(CHILD(tree, 4)))));
2178 }
2179 return (res);
2180}
2181
2182static int
2183validate_old_test(node *tree)
2184{
2185 int nch = NCH(tree);
2186 int res = validate_ntype(tree, old_test) && (nch == 1);
2187
2188 if (res && (TYPE(CHILD(tree, 0)) == old_lambdef))
2189 res = (validate_old_lambdef(CHILD(tree, 0)));
2190 else if (res) {
2191 res = (validate_or_test(CHILD(tree, 0)));
2192 }
2193 return (res);
2194}
2195
2196static int
2197validate_or_test(node *tree)
2198{
2199 int nch = NCH(tree);
2200 int res = validate_ntype(tree, or_test) && is_odd(nch);
2201
2202 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002203 int pos;
2204 res = validate_and_test(CHILD(tree, 0));
2205 for (pos = 1; res && (pos < nch); pos += 2)
2206 res = (validate_name(CHILD(tree, pos), "or")
2207 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002208 }
2209 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002210}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002211
2212
Guido van Rossum47478871996-08-21 14:32:37 +00002213static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002214validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002215{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002216 int pos;
2217 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002218 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00002219 && is_odd(nch)
2220 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002221
Guido van Rossum3d602e31996-07-21 02:33:56 +00002222 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002223 res = (validate_name(CHILD(tree, pos), "and")
2224 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002225
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002226 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002227}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002228
2229
Guido van Rossum47478871996-08-21 14:32:37 +00002230static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002231validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002232{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002233 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002234 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002235
Guido van Rossum3d602e31996-07-21 02:33:56 +00002236 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002237 if (nch == 2)
2238 res = (validate_name(CHILD(tree, 0), "not")
2239 && validate_not_test(CHILD(tree, 1)));
2240 else if (nch == 1)
2241 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002242 }
2243 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002244}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002245
2246
Guido van Rossum47478871996-08-21 14:32:37 +00002247static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002248validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002249{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002250 int pos;
2251 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002252 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002253 && is_odd(nch)
2254 && validate_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002255
Guido van Rossum3d602e31996-07-21 02:33:56 +00002256 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002257 res = (validate_comp_op(CHILD(tree, pos))
2258 && validate_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002259
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002260 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002261}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002262
2263
Guido van Rossum47478871996-08-21 14:32:37 +00002264static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002265validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002266{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002267 int res = 0;
2268 int nch = NCH(tree);
2269
Guido van Rossum3d602e31996-07-21 02:33:56 +00002270 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002271 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002272 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002273 /*
2274 * Only child will be a terminal with a well-defined symbolic name
2275 * or a NAME with a string of either 'is' or 'in'
2276 */
2277 tree = CHILD(tree, 0);
2278 switch (TYPE(tree)) {
2279 case LESS:
2280 case GREATER:
2281 case EQEQUAL:
2282 case EQUAL:
2283 case LESSEQUAL:
2284 case GREATEREQUAL:
2285 case NOTEQUAL:
2286 res = 1;
2287 break;
2288 case NAME:
2289 res = ((strcmp(STR(tree), "in") == 0)
2290 || (strcmp(STR(tree), "is") == 0));
2291 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002292 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002293 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002294 }
2295 break;
2296 default:
Fred Drake661ea262000-10-24 19:57:45 +00002297 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002298 break;
2299 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002300 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002301 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002302 res = (validate_ntype(CHILD(tree, 0), NAME)
2303 && validate_ntype(CHILD(tree, 1), NAME)
2304 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2305 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2306 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2307 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2308 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002309 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002310 }
2311 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002312}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002313
2314
Guido van Rossum47478871996-08-21 14:32:37 +00002315static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002316validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002317{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002318 int j;
2319 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002320 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002321 && is_odd(nch)
2322 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002323
Guido van Rossum3d602e31996-07-21 02:33:56 +00002324 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002325 res = (validate_xor_expr(CHILD(tree, j))
2326 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002327
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002328 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002329}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002330
2331
Guido van Rossum47478871996-08-21 14:32:37 +00002332static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002333validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002334{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002335 int j;
2336 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002337 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002338 && is_odd(nch)
2339 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002340
Guido van Rossum3d602e31996-07-21 02:33:56 +00002341 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002342 res = (validate_circumflex(CHILD(tree, j - 1))
2343 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002344
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002345 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002346}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002347
2348
Guido van Rossum47478871996-08-21 14:32:37 +00002349static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002350validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002351{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002352 int pos;
2353 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002354 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002355 && is_odd(nch)
2356 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002357
Guido van Rossum3d602e31996-07-21 02:33:56 +00002358 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002359 res = (validate_ampersand(CHILD(tree, pos))
2360 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002361
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002362 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002363}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002364
2365
2366static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002367validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002368 {
2369 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002370 int nch = NCH(tree);
2371 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002372 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002373
Guido van Rossum3d602e31996-07-21 02:33:56 +00002374 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002375 if (TYPE(CHILD(tree, pos)) != op1)
2376 res = validate_ntype(CHILD(tree, pos), op2);
2377 if (res)
2378 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002379 }
2380 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002381}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002382
2383
Guido van Rossum47478871996-08-21 14:32:37 +00002384static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002385validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002386{
2387 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002388 && validate_chain_two_ops(tree, validate_arith_expr,
2389 LEFTSHIFT, RIGHTSHIFT));
2390}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002391
2392
Guido van Rossum47478871996-08-21 14:32:37 +00002393static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002394validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002395{
2396 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002397 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
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_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002403{
2404 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002405 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002406 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002407 && is_odd(nch)
2408 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002409
Guido van Rossum3d602e31996-07-21 02:33:56 +00002410 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002411 res = (((TYPE(CHILD(tree, pos)) == STAR)
2412 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002413 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002414 || (TYPE(CHILD(tree, pos)) == PERCENT))
2415 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002416
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002417 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002418}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002419
2420
Guido van Rossum3d602e31996-07-21 02:33:56 +00002421/* factor:
2422 *
2423 * factor: ('+'|'-'|'~') factor | power
2424 */
Guido van Rossum47478871996-08-21 14:32:37 +00002425static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002426validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002427{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002428 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002429 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002430 && (((nch == 2)
2431 && ((TYPE(CHILD(tree, 0)) == PLUS)
2432 || (TYPE(CHILD(tree, 0)) == MINUS)
2433 || (TYPE(CHILD(tree, 0)) == TILDE))
2434 && validate_factor(CHILD(tree, 1)))
2435 || ((nch == 1)
2436 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002437 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002438}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002439
2440
Guido van Rossum3d602e31996-07-21 02:33:56 +00002441/* power:
2442 *
2443 * power: atom trailer* ('**' factor)*
2444 */
Guido van Rossum47478871996-08-21 14:32:37 +00002445static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002446validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002447{
2448 int pos = 1;
2449 int nch = NCH(tree);
2450 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002451 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002452
2453 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002454 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002455 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002456 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002457 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002458 return (0);
2459 }
2460 for ( ; res && (pos < (nch - 1)); pos += 2)
2461 res = (validate_doublestar(CHILD(tree, pos))
2462 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002463 }
2464 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002465}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002466
2467
Guido van Rossum47478871996-08-21 14:32:37 +00002468static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002469validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002470{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002471 int pos;
2472 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002473 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002474
Fred Drakecff283c2000-08-21 22:24:43 +00002475 if (res && nch < 1)
2476 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002477 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002478 switch (TYPE(CHILD(tree, 0))) {
2479 case LPAR:
2480 res = ((nch <= 3)
2481 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002482
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002483 if (res && (nch == 3)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002484 if (TYPE(CHILD(tree, 1))==yield_expr)
2485 res = validate_yield_expr(CHILD(tree, 1));
2486 else
2487 res = validate_testlist_comp(CHILD(tree, 1));
2488 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002489 break;
2490 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002491 if (nch == 2)
2492 res = validate_ntype(CHILD(tree, 1), RSQB);
2493 else if (nch == 3)
2494 res = (validate_listmaker(CHILD(tree, 1))
2495 && validate_ntype(CHILD(tree, 2), RSQB));
2496 else {
2497 res = 0;
2498 err_string("illegal list display atom");
2499 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002500 break;
2501 case LBRACE:
2502 res = ((nch <= 3)
2503 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002504
Fred Drakeff9ea482000-04-19 13:54:15 +00002505 if (res && (nch == 3))
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002506 res = validate_dictorsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002507 break;
2508 case BACKQUOTE:
2509 res = ((nch == 3)
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002510 && validate_testlist1(CHILD(tree, 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00002511 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2512 break;
2513 case NAME:
2514 case NUMBER:
2515 res = (nch == 1);
2516 break;
2517 case STRING:
2518 for (pos = 1; res && (pos < nch); ++pos)
2519 res = validate_ntype(CHILD(tree, pos), STRING);
2520 break;
2521 default:
2522 res = 0;
2523 break;
2524 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002525 }
2526 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002527}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002528
2529
Fred Drake85bf3bb2000-08-23 15:35:26 +00002530/* listmaker:
2531 * test ( list_for | (',' test)* [','] )
2532 */
Fred Drakecff283c2000-08-21 22:24:43 +00002533static int
2534validate_listmaker(node *tree)
2535{
2536 int nch = NCH(tree);
2537 int ok = nch;
2538
2539 if (nch == 0)
2540 err_string("missing child nodes of listmaker");
2541 else
2542 ok = validate_test(CHILD(tree, 0));
2543
2544 /*
Raymond Hettinger354433a2004-05-19 08:20:33 +00002545 * list_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002546 */
Fred Drake85bf3bb2000-08-23 15:35:26 +00002547 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2548 ok = validate_list_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002549 else {
2550 /* (',' test)* [','] */
2551 int i = 1;
2552 while (ok && nch - i >= 2) {
2553 ok = (validate_comma(CHILD(tree, i))
2554 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002555 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002556 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002557 if (ok && i == nch-1)
2558 ok = validate_comma(CHILD(tree, i));
2559 else if (i != nch) {
2560 ok = 0;
2561 err_string("illegal trailing nodes for listmaker");
2562 }
Fred Drakecff283c2000-08-21 22:24:43 +00002563 }
2564 return ok;
2565}
2566
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002567/* testlist_comp:
2568 * test ( comp_for | (',' test)* [','] )
Raymond Hettinger354433a2004-05-19 08:20:33 +00002569 */
2570static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002571validate_testlist_comp(node *tree)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002572{
2573 int nch = NCH(tree);
2574 int ok = nch;
2575
2576 if (nch == 0)
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002577 err_string("missing child nodes of testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002578 else {
2579 ok = validate_test(CHILD(tree, 0));
2580 }
2581
2582 /*
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002583 * comp_for | (',' test)* [',']
Raymond Hettinger354433a2004-05-19 08:20:33 +00002584 */
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002585 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2586 ok = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002587 else {
2588 /* (',' test)* [','] */
2589 int i = 1;
2590 while (ok && nch - i >= 2) {
2591 ok = (validate_comma(CHILD(tree, i))
2592 && validate_test(CHILD(tree, i+1)));
2593 i += 2;
2594 }
2595 if (ok && i == nch-1)
2596 ok = validate_comma(CHILD(tree, i));
2597 else if (i != nch) {
2598 ok = 0;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002599 err_string("illegal trailing nodes for testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002600 }
2601 }
2602 return ok;
2603}
Fred Drakecff283c2000-08-21 22:24:43 +00002604
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002605/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002606 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002607 */
2608static int
2609validate_decorator(node *tree)
2610{
2611 int ok;
2612 int nch = NCH(tree);
2613 ok = (validate_ntype(tree, decorator) &&
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002614 (nch == 3 || nch == 5 || nch == 6) &&
2615 validate_at(CHILD(tree, 0)) &&
2616 validate_dotted_name(CHILD(tree, 1)) &&
2617 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002618
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002619 if (ok && nch != 3) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002620 ok = (validate_lparen(CHILD(tree, 2)) &&
2621 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002622
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002623 if (ok && nch == 6)
2624 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002625 }
2626
2627 return ok;
2628}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002629
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002630/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002631 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002632 */
2633static int
2634validate_decorators(node *tree)
2635{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002636 int i, nch, ok;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002637 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002638 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002639
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002640 for (i = 0; ok && i < nch; ++i)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002641 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002642
2643 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002644}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002645
Georg Brandl944f6842009-05-25 21:02:56 +00002646/* with_item:
2647 * test ['as' expr]
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002648 */
2649static int
Georg Brandl944f6842009-05-25 21:02:56 +00002650validate_with_item(node *tree)
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002651{
2652 int nch = NCH(tree);
Georg Brandl944f6842009-05-25 21:02:56 +00002653 int ok = (validate_ntype(tree, with_item)
2654 && (nch == 1 || nch == 3)
2655 && validate_test(CHILD(tree, 0)));
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002656 if (ok && nch == 3)
Georg Brandl944f6842009-05-25 21:02:56 +00002657 ok = (validate_name(CHILD(tree, 1), "as")
2658 && validate_expr(CHILD(tree, 2)));
2659 return ok;
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002660}
2661
Georg Brandl944f6842009-05-25 21:02:56 +00002662/* with_stmt:
2663 * 0 1 ... -2 -1
2664 * 'with' with_item (',' with_item)* ':' suite
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002665 */
2666static int
2667validate_with_stmt(node *tree)
2668{
Georg Brandl944f6842009-05-25 21:02:56 +00002669 int i;
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002670 int nch = NCH(tree);
2671 int ok = (validate_ntype(tree, with_stmt)
Georg Brandl944f6842009-05-25 21:02:56 +00002672 && (nch % 2 == 0)
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002673 && validate_name(CHILD(tree, 0), "with")
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002674 && validate_colon(RCHILD(tree, -2))
2675 && validate_suite(RCHILD(tree, -1)));
Georg Brandl944f6842009-05-25 21:02:56 +00002676 for (i = 1; ok && i < nch - 2; i += 2)
2677 ok = validate_with_item(CHILD(tree, i));
2678 return ok;
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00002679}
2680
Guido van Rossum3d602e31996-07-21 02:33:56 +00002681/* funcdef:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002682 *
Christian Heimes5224d282008-02-23 15:01:05 +00002683 * -5 -4 -3 -2 -1
2684 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002685 */
Guido van Rossum47478871996-08-21 14:32:37 +00002686static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002687validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002688{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002689 int nch = NCH(tree);
2690 int ok = (validate_ntype(tree, funcdef)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002691 && (nch == 5)
2692 && validate_name(RCHILD(tree, -5), "def")
2693 && validate_ntype(RCHILD(tree, -4), NAME)
2694 && validate_colon(RCHILD(tree, -2))
2695 && validate_parameters(RCHILD(tree, -3))
2696 && validate_suite(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002697 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002698}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002699
2700
Christian Heimes5224d282008-02-23 15:01:05 +00002701/* decorated
2702 * decorators (classdef | funcdef)
2703 */
2704static int
2705validate_decorated(node *tree)
2706{
Mark Dickinsona7ee59b2010-07-04 16:23:54 +00002707 int nch = NCH(tree);
2708 int ok = (validate_ntype(tree, decorated)
2709 && (nch == 2)
2710 && validate_decorators(RCHILD(tree, -2)));
2711 if (TYPE(RCHILD(tree, -1)) == funcdef)
2712 ok = ok && validate_funcdef(RCHILD(tree, -1));
2713 else
2714 ok = ok && validate_class(RCHILD(tree, -1));
2715 return ok;
Christian Heimes5224d282008-02-23 15:01:05 +00002716}
2717
Guido van Rossum47478871996-08-21 14:32:37 +00002718static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002719validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002720{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002721 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002722 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002723 && ((nch == 3) || (nch == 4))
2724 && validate_name(CHILD(tree, 0), "lambda")
2725 && validate_colon(CHILD(tree, nch - 2))
2726 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002727
Guido van Rossum3d602e31996-07-21 02:33:56 +00002728 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002729 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002730 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002731 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002732
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002733 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002734}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002735
2736
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002737static int
2738validate_old_lambdef(node *tree)
2739{
2740 int nch = NCH(tree);
2741 int res = (validate_ntype(tree, old_lambdef)
2742 && ((nch == 3) || (nch == 4))
2743 && validate_name(CHILD(tree, 0), "lambda")
2744 && validate_colon(CHILD(tree, nch - 2))
2745 && validate_test(CHILD(tree, nch - 1)));
2746
2747 if (res && (nch == 4))
2748 res = validate_varargslist(CHILD(tree, 1));
2749 else if (!res && !PyErr_Occurred())
2750 (void) validate_numnodes(tree, 3, "old_lambdef");
2751
2752 return (res);
2753}
2754
2755
Guido van Rossum3d602e31996-07-21 02:33:56 +00002756/* arglist:
2757 *
Fred Drakecff283c2000-08-21 22:24:43 +00002758 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002759 */
Guido van Rossum47478871996-08-21 14:32:37 +00002760static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002761validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002762{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002763 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002764 int i = 0;
2765 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002766
2767 if (nch <= 0)
2768 /* raise the right error from having an invalid number of children */
2769 return validate_numnodes(tree, nch + 1, "arglist");
2770
Raymond Hettinger354433a2004-05-19 08:20:33 +00002771 if (nch > 1) {
2772 for (i=0; i<nch; i++) {
2773 if (TYPE(CHILD(tree, i)) == argument) {
2774 node *ch = CHILD(tree, i);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002775 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
Raymond Hettinger354433a2004-05-19 08:20:33 +00002776 err_string("need '(', ')' for generator expression");
2777 return 0;
2778 }
2779 }
2780 }
2781 }
2782
Fred Drakecff283c2000-08-21 22:24:43 +00002783 while (ok && nch-i >= 2) {
2784 /* skip leading (argument ',') */
2785 ok = (validate_argument(CHILD(tree, i))
2786 && validate_comma(CHILD(tree, i+1)));
2787 if (ok)
2788 i += 2;
2789 else
2790 PyErr_Clear();
2791 }
2792 ok = 1;
2793 if (nch-i > 0) {
2794 /*
2795 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002796 */
Fred Drakecff283c2000-08-21 22:24:43 +00002797 int sym = TYPE(CHILD(tree, i));
2798
2799 if (sym == argument) {
2800 ok = validate_argument(CHILD(tree, i));
2801 if (ok && i+1 != nch) {
2802 err_string("illegal arglist specification"
2803 " (extra stuff on end)");
2804 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002805 }
Fred Drakecff283c2000-08-21 22:24:43 +00002806 }
2807 else if (sym == STAR) {
2808 ok = validate_star(CHILD(tree, i));
2809 if (ok && (nch-i == 2))
2810 ok = validate_test(CHILD(tree, i+1));
2811 else if (ok && (nch-i == 5))
2812 ok = (validate_test(CHILD(tree, i+1))
2813 && validate_comma(CHILD(tree, i+2))
2814 && validate_doublestar(CHILD(tree, i+3))
2815 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002816 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002817 err_string("illegal use of '*' in arglist");
2818 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002819 }
2820 }
Fred Drakecff283c2000-08-21 22:24:43 +00002821 else if (sym == DOUBLESTAR) {
2822 if (nch-i == 2)
2823 ok = (validate_doublestar(CHILD(tree, i))
2824 && validate_test(CHILD(tree, i+1)));
2825 else {
2826 err_string("illegal use of '**' in arglist");
2827 ok = 0;
2828 }
2829 }
2830 else {
2831 err_string("illegal arglist specification");
2832 ok = 0;
2833 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002834 }
2835 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002836}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002837
2838
2839
2840/* argument:
2841 *
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002842 * [test '='] test [comp_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002843 */
Guido van Rossum47478871996-08-21 14:32:37 +00002844static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002845validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002846{
2847 int nch = NCH(tree);
2848 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002849 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002850 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002851
Raymond Hettinger354433a2004-05-19 08:20:33 +00002852 if (res && (nch == 2))
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002853 res = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002854 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002855 res = (validate_equal(CHILD(tree, 1))
2856 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002857
2858 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002859}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002860
2861
2862
2863/* trailer:
2864 *
Guido van Rossum47478871996-08-21 14:32:37 +00002865 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002866 */
Guido van Rossum47478871996-08-21 14:32:37 +00002867static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002868validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002869{
2870 int nch = NCH(tree);
2871 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002872
2873 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002874 switch (TYPE(CHILD(tree, 0))) {
2875 case LPAR:
2876 res = validate_rparen(CHILD(tree, nch - 1));
2877 if (res && (nch == 3))
2878 res = validate_arglist(CHILD(tree, 1));
2879 break;
2880 case LSQB:
2881 res = (validate_numnodes(tree, 3, "trailer")
2882 && validate_subscriptlist(CHILD(tree, 1))
2883 && validate_ntype(CHILD(tree, 2), RSQB));
2884 break;
2885 case DOT:
2886 res = (validate_numnodes(tree, 2, "trailer")
2887 && validate_ntype(CHILD(tree, 1), NAME));
2888 break;
2889 default:
2890 res = 0;
2891 break;
2892 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002893 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002894 else {
2895 (void) validate_numnodes(tree, 2, "trailer");
2896 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002897 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002898}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002899
2900
Guido van Rossum47478871996-08-21 14:32:37 +00002901/* subscriptlist:
2902 *
2903 * subscript (',' subscript)* [',']
2904 */
2905static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002906validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002907{
2908 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002909 validate_subscript, "subscriptlist"));
2910}
Guido van Rossum47478871996-08-21 14:32:37 +00002911
2912
Guido van Rossum3d602e31996-07-21 02:33:56 +00002913/* subscript:
2914 *
Guido van Rossum47478871996-08-21 14:32:37 +00002915 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002916 */
Guido van Rossum47478871996-08-21 14:32:37 +00002917static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002918validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002919{
Guido van Rossum47478871996-08-21 14:32:37 +00002920 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002921 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002922 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002923
Guido van Rossum47478871996-08-21 14:32:37 +00002924 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002925 if (!PyErr_Occurred())
2926 err_string("invalid number of arguments for subscript node");
2927 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002928 }
Guido van Rossum47478871996-08-21 14:32:37 +00002929 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002930 /* take care of ('.' '.' '.') possibility */
2931 return (validate_numnodes(tree, 3, "subscript")
2932 && validate_dot(CHILD(tree, 0))
2933 && validate_dot(CHILD(tree, 1))
2934 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002935 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002936 if (TYPE(CHILD(tree, 0)) == test)
2937 res = validate_test(CHILD(tree, 0));
2938 else
2939 res = validate_colon(CHILD(tree, 0));
2940 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002941 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002942 /* Must be [test] ':' [test] [sliceop],
2943 * but at least one of the optional components will
2944 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002945 */
2946 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002947 res = validate_test(CHILD(tree, 0));
2948 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002949 }
2950 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002951 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002952 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002953 int rem = nch - ++offset;
2954 if (rem) {
2955 if (TYPE(CHILD(tree, offset)) == test) {
2956 res = validate_test(CHILD(tree, offset));
2957 ++offset;
2958 --rem;
2959 }
2960 if (res && rem)
2961 res = validate_sliceop(CHILD(tree, offset));
2962 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002963 }
2964 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002965}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002966
2967
Guido van Rossum47478871996-08-21 14:32:37 +00002968static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002969validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002970{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002971 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002972 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002973 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002974 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002975 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002976 }
Guido van Rossum47478871996-08-21 14:32:37 +00002977 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002978 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002979 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002980 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002981
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002982 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002983}
Guido van Rossum47478871996-08-21 14:32:37 +00002984
2985
2986static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002987validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002988{
2989 return (validate_repeating_list(tree, exprlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002990 validate_expr, "exprlist"));
2991}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002992
2993
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002994/*
2995 * dictorsetmaker:
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002996 *
2997 * (test ':' test (comp_for | (',' test ':' test)* [','])) |
2998 * (test (comp_for | (',' test)* [',']))
2999 */
Guido van Rossum47478871996-08-21 14:32:37 +00003000static int
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003001validate_dictorsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003002{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003003 int nch = NCH(tree);
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003004 int ok = validate_ntype(tree, dictorsetmaker);
3005 int i = 0;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00003006 int check_trailing_comma = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003007
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003008 assert(nch > 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003009
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003010 if (ok && (nch == 1 || TYPE(CHILD(tree, 1)) == COMMA)) {
3011 /* We got a set:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003012 * test (',' test)* [',']
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003013 */
3014 ok = validate_test(CHILD(tree, i++));
3015 while (ok && nch - i >= 2) {
3016 ok = (validate_comma(CHILD(tree, i))
3017 && validate_test(CHILD(tree, i+1)));
3018 i += 2;
Fred Drakeff9ea482000-04-19 13:54:15 +00003019 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00003020 check_trailing_comma = 1;
3021 }
3022 else if (ok && TYPE(CHILD(tree, 1)) == comp_for) {
3023 /* We got a set comprehension:
3024 * test comp_for
3025 */
3026 ok = (validate_test(CHILD(tree, 0))
3027 && validate_comp_for(CHILD(tree, 1)));
3028 }
3029 else if (ok && NCH(tree) > 3 && TYPE(CHILD(tree, 3)) == comp_for) {
3030 /* We got a dict comprehension:
3031 * test ':' test comp_for
3032 */
3033 ok = (validate_test(CHILD(tree, 0))
3034 && validate_colon(CHILD(tree, 1))
3035 && validate_test(CHILD(tree, 2))
3036 && validate_comp_for(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003037 }
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003038 else if (ok) {
3039 /* We got a dict:
3040 * test ':' test (',' test ':' test)* [',']
3041 */
3042 if (nch >= 3) {
3043 ok = (validate_test(CHILD(tree, i))
3044 && validate_colon(CHILD(tree, i+1))
3045 && validate_test(CHILD(tree, i+2)));
3046 i += 3;
3047 }
3048 else {
3049 ok = 0;
3050 err_string("illegal number of nodes for dictorsetmaker");
3051 }
3052
3053 while (ok && nch - i >= 4) {
3054 ok = (validate_comma(CHILD(tree, i))
3055 && validate_test(CHILD(tree, i+1))
3056 && validate_colon(CHILD(tree, i+2))
3057 && validate_test(CHILD(tree, i+3)));
3058 i += 4;
3059 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00003060 check_trailing_comma = 1;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003061 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00003062 if (ok && check_trailing_comma) {
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003063 if (i == nch-1)
3064 ok = validate_comma(CHILD(tree, i));
3065 else if (i != nch) {
3066 ok = 0;
3067 err_string("illegal trailing nodes for dictorsetmaker");
3068 }
3069 }
3070
3071 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00003072}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003073
3074
Guido van Rossum47478871996-08-21 14:32:37 +00003075static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003076validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003077{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003078 int pos;
3079 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003080 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00003081 && (nch >= 2)
3082 && validate_testlist(CHILD(tree, 0))
3083 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003084
Guido van Rossum3d602e31996-07-21 02:33:56 +00003085 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00003086 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003087
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003088 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003089}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003090
3091
Guido van Rossum47478871996-08-21 14:32:37 +00003092static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003093validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003094{
Fred Drakeff9ea482000-04-19 13:54:15 +00003095 int nch = 0; /* num. children on current node */
3096 int res = 1; /* result value */
3097 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00003098
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00003099 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003100 nch = NCH(tree);
3101 next = 0;
3102 switch (TYPE(tree)) {
3103 /*
3104 * Definition nodes.
3105 */
3106 case funcdef:
3107 res = validate_funcdef(tree);
3108 break;
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +00003109 case with_stmt:
3110 res = validate_with_stmt(tree);
3111 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003112 case classdef:
3113 res = validate_class(tree);
3114 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003115 case decorated:
3116 res = validate_decorated(tree);
3117 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003118 /*
3119 * "Trivial" parse tree nodes.
3120 * (Why did I call these trivial?)
3121 */
3122 case stmt:
3123 res = validate_stmt(tree);
3124 break;
3125 case small_stmt:
3126 /*
Fred Drake0ac9b072000-09-12 21:58:06 +00003127 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00003128 * | import_stmt | global_stmt | exec_stmt | assert_stmt
3129 */
3130 res = validate_small_stmt(tree);
3131 break;
3132 case flow_stmt:
3133 res = (validate_numnodes(tree, 1, "flow_stmt")
3134 && ((TYPE(CHILD(tree, 0)) == break_stmt)
3135 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00003136 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00003137 || (TYPE(CHILD(tree, 0)) == return_stmt)
3138 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
3139 if (res)
3140 next = CHILD(tree, 0);
3141 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00003142 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003143 break;
Fred Drake02126f22001-07-17 02:59:15 +00003144 case yield_stmt:
3145 res = validate_yield_stmt(tree);
3146 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003147 /*
3148 * Compound statements.
3149 */
3150 case simple_stmt:
3151 res = validate_simple_stmt(tree);
3152 break;
3153 case compound_stmt:
3154 res = validate_compound_stmt(tree);
3155 break;
3156 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00003157 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00003158 */
3159 case expr_stmt:
3160 res = validate_expr_stmt(tree);
3161 break;
3162 case print_stmt:
3163 res = validate_print_stmt(tree);
3164 break;
3165 case del_stmt:
3166 res = validate_del_stmt(tree);
3167 break;
3168 case pass_stmt:
3169 res = (validate_numnodes(tree, 1, "pass")
3170 && validate_name(CHILD(tree, 0), "pass"));
3171 break;
3172 case break_stmt:
3173 res = (validate_numnodes(tree, 1, "break")
3174 && validate_name(CHILD(tree, 0), "break"));
3175 break;
3176 case continue_stmt:
3177 res = (validate_numnodes(tree, 1, "continue")
3178 && validate_name(CHILD(tree, 0), "continue"));
3179 break;
3180 case return_stmt:
3181 res = validate_return_stmt(tree);
3182 break;
3183 case raise_stmt:
3184 res = validate_raise_stmt(tree);
3185 break;
3186 case import_stmt:
3187 res = validate_import_stmt(tree);
3188 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003189 case import_name:
3190 res = validate_import_name(tree);
3191 break;
3192 case import_from:
3193 res = validate_import_from(tree);
3194 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003195 case global_stmt:
3196 res = validate_global_stmt(tree);
3197 break;
3198 case exec_stmt:
3199 res = validate_exec_stmt(tree);
3200 break;
3201 case assert_stmt:
3202 res = validate_assert_stmt(tree);
3203 break;
3204 case if_stmt:
3205 res = validate_if(tree);
3206 break;
3207 case while_stmt:
3208 res = validate_while(tree);
3209 break;
3210 case for_stmt:
3211 res = validate_for(tree);
3212 break;
3213 case try_stmt:
3214 res = validate_try(tree);
3215 break;
3216 case suite:
3217 res = validate_suite(tree);
3218 break;
3219 /*
3220 * Expression nodes.
3221 */
3222 case testlist:
3223 res = validate_testlist(tree);
3224 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003225 case yield_expr:
3226 res = validate_yield_expr(tree);
3227 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00003228 case testlist1:
3229 res = validate_testlist1(tree);
3230 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00003231 case test:
3232 res = validate_test(tree);
3233 break;
3234 case and_test:
3235 res = validate_and_test(tree);
3236 break;
3237 case not_test:
3238 res = validate_not_test(tree);
3239 break;
3240 case comparison:
3241 res = validate_comparison(tree);
3242 break;
3243 case exprlist:
3244 res = validate_exprlist(tree);
3245 break;
3246 case comp_op:
3247 res = validate_comp_op(tree);
3248 break;
3249 case expr:
3250 res = validate_expr(tree);
3251 break;
3252 case xor_expr:
3253 res = validate_xor_expr(tree);
3254 break;
3255 case and_expr:
3256 res = validate_and_expr(tree);
3257 break;
3258 case shift_expr:
3259 res = validate_shift_expr(tree);
3260 break;
3261 case arith_expr:
3262 res = validate_arith_expr(tree);
3263 break;
3264 case term:
3265 res = validate_term(tree);
3266 break;
3267 case factor:
3268 res = validate_factor(tree);
3269 break;
3270 case power:
3271 res = validate_power(tree);
3272 break;
3273 case atom:
3274 res = validate_atom(tree);
3275 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003276
Fred Drakeff9ea482000-04-19 13:54:15 +00003277 default:
3278 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00003279 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00003280 res = 0;
3281 break;
3282 }
3283 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003284 }
3285 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003286}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003287
3288
Guido van Rossum47478871996-08-21 14:32:37 +00003289static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003290validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003291{
3292 int res = validate_eval_input(tree);
3293
3294 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003295 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003296
3297 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003298}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003299
3300
Guido van Rossum3d602e31996-07-21 02:33:56 +00003301/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00003302 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00003303 */
Guido van Rossum47478871996-08-21 14:32:37 +00003304static int
Fred Drakeff9ea482000-04-19 13:54:15 +00003305validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00003306{
Fred Drakec2683dd2001-07-17 19:32:05 +00003307 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00003308 int nch = NCH(tree) - 1;
3309 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00003310 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003311
Fred Drakec2683dd2001-07-17 19:32:05 +00003312 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003313 if (TYPE(CHILD(tree, j)) == stmt)
3314 res = validate_stmt(CHILD(tree, j));
3315 else
3316 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003317 }
Thomas Wouters7e474022000-07-16 12:04:32 +00003318 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00003319 * user. Hopefully, this won't be needed. If a user reports getting
3320 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003321 */
3322 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00003323 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00003324
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003325 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00003326}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003327
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00003328static int
3329validate_encoding_decl(node *tree)
3330{
3331 int nch = NCH(tree);
3332 int res = ((nch == 1)
3333 && validate_file_input(CHILD(tree, 0)));
3334
3335 if (!res && !PyErr_Occurred())
3336 err_string("Error Parsing encoding_decl");
3337
3338 return res;
3339}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003340
Fred Drake43f8f9b1998-04-13 16:25:46 +00003341static PyObject*
3342pickle_constructor = NULL;
3343
3344
3345static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00003346parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00003347{
Fred Drake268397f1998-04-29 14:16:32 +00003348 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00003349 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00003350 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00003351 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003352
Fred Drakec2683dd2001-07-17 19:32:05 +00003353 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003354 PyObject *newargs;
3355 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003356
Fred Drake2a6875e1999-09-20 22:32:18 +00003357 if ((empty_dict = PyDict_New()) == NULL)
3358 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003359 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00003360 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00003361 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003362 if (tuple != NULL) {
3363 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3364 Py_DECREF(tuple);
3365 }
Fred Drake2a6875e1999-09-20 22:32:18 +00003366 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00003367 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003368 }
3369 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00003370 Py_XDECREF(empty_dict);
3371
Fred Drake43f8f9b1998-04-13 16:25:46 +00003372 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003373}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003374
3375
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003376/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003377 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003378 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003379 * We'd really have to write a wrapper around it all anyway to allow
3380 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003381 */
3382static PyMethodDef parser_functions[] = {
Georg Brandlf9efabb2008-07-23 15:16:45 +00003383 {"ast2tuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003384 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Georg Brandlf9efabb2008-07-23 15:16:45 +00003385 {"ast2list", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003386 PyDoc_STR("Creates a list-tree representation of an ST.")},
Georg Brandlf9efabb2008-07-23 15:16:45 +00003387 {"compileast", (PyCFunction)parser_compileast,PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003388 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003389 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003390 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003391 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003392 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003393 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003394 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003395 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003396 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003397 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003398 PyDoc_STR("Creates an ST object from a suite.")},
Georg Brandlf9efabb2008-07-23 15:16:45 +00003399 {"sequence2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003400 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003401 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003402 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003403 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003404 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003405 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003406 PyDoc_STR("Creates a list-tree representation of an ST.")},
Georg Brandlf9efabb2008-07-23 15:16:45 +00003407 {"tuple2ast", (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 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003410 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003411
Fred Drake43f8f9b1998-04-13 16:25:46 +00003412 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003413 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003414 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003415
Fred Drake268397f1998-04-29 14:16:32 +00003416 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003417 };
3418
3419
Mark Hammond62b1ab12002-07-23 06:31:15 +00003420PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003421
Mark Hammond62b1ab12002-07-23 06:31:15 +00003422PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00003423initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003424{
Fred Drake13130bc2001-08-15 16:44:56 +00003425 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003426
Christian Heimese93237d2007-12-19 02:37:44 +00003427 Py_TYPE(&PyST_Type) = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00003428 module = Py_InitModule("parser", parser_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003429 if (module == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003430 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003431
Fred Drake7a15ba51999-09-09 14:21:52 +00003432 if (parser_error == 0)
3433 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003434
Tim Peters6a627252003-07-21 14:25:23 +00003435 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00003436 /* caller will check PyErr_Occurred() */
3437 return;
Tim Peters6a627252003-07-21 14:25:23 +00003438 /* CAUTION: The code next used to skip bumping the refcount on
3439 * parser_error. That's a disaster if initparser() gets called more
3440 * than once. By incref'ing, we ensure that each module dict that
3441 * gets created owns its reference to the shared parser_error object,
3442 * and the file static parser_error vrbl owns a reference too.
3443 */
3444 Py_INCREF(parser_error);
3445 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3446 return;
3447
Fred Drakec2683dd2001-07-17 19:32:05 +00003448 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003449 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00003450 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003451 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003452
Fred Drake13130bc2001-08-15 16:44:56 +00003453 PyModule_AddStringConstant(module, "__copyright__",
3454 parser_copyright_string);
3455 PyModule_AddStringConstant(module, "__doc__",
3456 parser_doc_string);
3457 PyModule_AddStringConstant(module, "__version__",
3458 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003459
Fred Drake78bdb9b2001-07-19 20:17:15 +00003460 /* Register to support pickling.
3461 * If this fails, the import of this module will fail because an
3462 * exception will be raised here; should we clear the exception?
3463 */
Georg Brandldffbf5f2008-05-20 07:49:57 +00003464 copyreg = PyImport_ImportModuleNoBlock("copy_reg");
Fred Drake13130bc2001-08-15 16:44:56 +00003465 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003466 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003467
Fred Drake13130bc2001-08-15 16:44:56 +00003468 func = PyObject_GetAttrString(copyreg, "pickle");
3469 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3470 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003471 Py_XINCREF(pickle_constructor);
3472 if ((func != NULL) && (pickle_constructor != NULL)
3473 && (pickler != NULL)) {
3474 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003475
Georg Brandl684fd0c2006-05-25 19:15:31 +00003476 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3477 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003478 Py_XDECREF(res);
3479 }
3480 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003481 Py_XDECREF(pickle_constructor);
3482 Py_XDECREF(pickler);
3483 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003484 }
Fred Drakeff9ea482000-04-19 13:54:15 +00003485}